Skip to content

Commit f326c5c

Browse files
blucaDaniel Kiper
authored andcommitted
commands/bli: Set LoaderTpm2ActivePcrBanks runtime variable
It turns out checking from userspace is not 100% reliable to figure out whether the firmware had TPM2 support enabled or not. For example with EDK2 arm64, the default upstream build config bundles TPM2 support with SecureBoot support, so if the latter is disabled, TPM2 is also unavailable. But still, the ACPI TPM2 table is created just as if it was enabled. So, /sys/firmware/acpi/tables/TPM2 exists and looks correct but there are no measurements, neither the firmware nor the loader/stub can do them, and /sys/kernel/security/tpm0/binary_bios_measurements does not exist. So, userspace cannot really tell what was going on in UEFI mode. The loader can use the apposite UEFI protocol to check, which is a more definitive answer. Export the bitmask with the list of active banks as-is. If it's not 0, then in userspace we can be sure a working TPM2 was available in UEFI mode. systemd-boot and systemd-stub v258 (current main) set this variable and userspace portion consumes it to be able to tell what was available in the firmware context. Signed-off-by: Luca Boccassi <luca.boccassi@gmail.com> Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
1 parent 0e36779 commit f326c5c

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

grub-core/commands/bli.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <grub/misc.h>
2929
#include <grub/mm.h>
3030
#include <grub/partition.h>
31+
#include <grub/tpm.h>
3132
#include <grub/types.h>
3233

3334
GRUB_MOD_LICENSE ("GPLv3+");
@@ -127,12 +128,34 @@ set_loader_device_part_uuid (void)
127128
return status;
128129
}
129130

131+
static grub_err_t
132+
set_loader_active_pcr_banks (void)
133+
{
134+
grub_efi_uint32_t active_pcr_banks;
135+
char *active_pcr_banks_str;
136+
grub_err_t status;
137+
138+
active_pcr_banks = grub_tpm2_active_pcr_banks();
139+
active_pcr_banks_str = grub_xasprintf ("0x%08x", active_pcr_banks);
140+
if (active_pcr_banks_str == NULL)
141+
return grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate active PCR banks string"));
142+
143+
status = grub_efi_set_variable_to_string ("LoaderTpm2ActivePcrBanks",
144+
&bli_vendor_guid,
145+
active_pcr_banks_str,
146+
GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
147+
GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
148+
grub_free (active_pcr_banks_str);
149+
return status;
150+
}
151+
130152
GRUB_MOD_INIT (bli)
131153
{
132154
grub_efi_set_variable_to_string ("LoaderInfo", &bli_vendor_guid, PACKAGE_STRING,
133155
GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
134156
GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
135157
set_loader_device_part_uuid ();
158+
set_loader_active_pcr_banks ();
136159
/* No error here is critical, other than being logged */
137160
grub_print_error ();
138161
}

grub-core/commands/efi/tpm.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,36 @@ grub_tpm_present (void)
332332
return grub_tpm2_present (tpm);
333333
}
334334
}
335+
336+
grub_uint32_t
337+
grub_tpm2_active_pcr_banks (void)
338+
{
339+
grub_efi_handle_t tpm_handle;
340+
grub_efi_uint8_t protocol_version;
341+
grub_efi_tpm2_protocol_t *tpm;
342+
grub_efi_uint32_t active_pcr_banks = 0;
343+
344+
if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
345+
return 0;
346+
347+
if (protocol_version == 1)
348+
return 0; /* We report TPM2 status */
349+
350+
tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
351+
GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
352+
if (tpm == NULL)
353+
{
354+
grub_dprintf ("tpm", "Cannot open TPM2 protocol\n");
355+
return 0;
356+
}
357+
358+
if (grub_tpm2_present (tpm))
359+
{
360+
grub_efi_status_t status = tpm->get_active_pcr_banks (tpm, &active_pcr_banks);
361+
362+
if (status != GRUB_EFI_SUCCESS)
363+
return 0; /* Assume none available if the call fails. */
364+
}
365+
366+
return active_pcr_banks;
367+
}

include/grub/tpm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
grub_err_t grub_tpm_measure (unsigned char *buf, grub_size_t size,
4040
grub_uint8_t pcr, const char *description);
4141
int grub_tpm_present (void);
42+
grub_uint32_t grub_tpm2_active_pcr_banks (void);
4243

4344
static inline bool
4445
grub_is_tpm_fail_fatal (void)

0 commit comments

Comments
 (0)