Skip to content

Commit a55bb49

Browse files
poetteringbluca
authored andcommitted
efi-api: check /sys/class/tpm/tpm0/tpm_version_major, too
If the ceck for the ACPI TPM2 table did not work we currently check if the EFI TPM table exists to check if the firmware supports TPM2. Specifically we check if /sys/kernel/security/tpm0/binary_bios_measurements exists. But that's not enough, since that also exists on TPM1.2 systems. Hence, let's also check /sys/class/tpm/tpm0/tpm_version_major which should exist under similar conditions and tells us the kernel's idea of the TPM version in use. I originally intended to read the signature of the /sys/kernel/security/tpm0/binary_bios_measurements contents for this, but this is not ideal since that file has tight access mode, and our TPM availability check would thus not work anymore if invoked unpriv. Follow-up for 4b33911 Fixes: #33077 (cherry picked from commit aeaac9a) (cherry picked from commit b2046c3)
1 parent f224a9d commit a55bb49

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/shared/efi-api.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "efi-api.h"
88
#include "efivars.h"
99
#include "fd-util.h"
10+
#include "fileio.h"
1011
#include "sort-util.h"
1112
#include "stat-util.h"
1213
#include "stdio-util.h"
@@ -521,37 +522,43 @@ int efi_get_boot_options(uint16_t **ret_options) {
521522

522523
bool efi_has_tpm2(void) {
523524
static int cache = -1;
525+
int r;
524526

525527
/* Returns whether the system has a TPM2 chip which is known to the EFI firmware. */
526528

527529
if (cache >= 0)
528530
return cache;
529531

530532
/* First, check if we are on an EFI boot at all. */
531-
if (!is_efi_boot()) {
532-
cache = 0;
533-
return cache;
534-
}
533+
if (!is_efi_boot())
534+
return (cache = false);
535535

536536
/* Then, check if the ACPI table "TPM2" exists, which is the TPM2 event log table, see:
537537
* https://trustedcomputinggroup.org/wp-content/uploads/TCG_ACPIGeneralSpecification_v1.20_r8.pdf
538-
* This table exists whenever the firmware is hooked up to TPM2. */
539-
cache = access("/sys/firmware/acpi/tables/TPM2", F_OK) >= 0;
540-
if (cache)
541-
return cache;
542-
538+
* This table exists whenever the firmware knows ACPI and is hooked up to TPM2. */
539+
if (access("/sys/firmware/acpi/tables/TPM2", F_OK) >= 0)
540+
return (cache = true);
543541
if (errno != ENOENT)
544542
log_debug_errno(errno, "Unable to test whether /sys/firmware/acpi/tables/TPM2 exists, assuming it doesn't: %m");
545543

546544
/* As the last try, check if the EFI firmware provides the EFI_TCG2_FINAL_EVENTS_TABLE
547545
* stored in EFI configuration table, see:
548-
* https://trustedcomputinggroup.org/wp-content/uploads/EFI-Protocol-Specification-rev13-160330final.pdf
549-
*/
550-
cache = access("/sys/kernel/security/tpm0/binary_bios_measurements", F_OK) >= 0;
551-
if (!cache && errno != ENOENT)
552-
log_debug_errno(errno, "Unable to test whether /sys/kernel/security/tpm0/binary_bios_measurements exists, assuming it doesn't: %m");
546+
*
547+
* https://trustedcomputinggroup.org/wp-content/uploads/EFI-Protocol-Specification-rev13-160330final.pdf */
548+
if (access("/sys/kernel/security/tpm0/binary_bios_measurements", F_OK) >= 0) {
549+
_cleanup_free_ char *major = NULL;
550+
551+
/* The EFI table might exist for TPM 1.2 as well, hence let's check explicitly which TPM version we are looking at here. */
552+
r = read_virtual_file("/sys/class/tpm/tpm0/tpm_version_major", SIZE_MAX, &major, /* ret_size= */ NULL);
553+
if (r >= 0)
554+
return (cache = streq(strstrip(major), "2"));
555+
556+
log_debug_errno(r, "Unable to read /sys/class/tpm/tpm0/tpm_version_major, assuming TPM does not qualify as TPM2: %m");
557+
558+
} else if (errno != ENOENT)
559+
log_debug_errno(errno, "Unable to test whether /sys/kernel/security/tpm0/binary_bios_measurements exists, assuming it doesn't: %m");
553560

554-
return cache;
561+
return (cache = false);
555562
}
556563

557564
#endif

0 commit comments

Comments
 (0)