Fix bugs related to TPM2 event log
Subsystems (3)
cover Fix bugs related to TPM2 event log
This series of patches fixes a couple of issues related to TPM2 event logs, such as the disappearance of the TPM2 log on QEMU machines running with UEFI (my fault) and a kernel fault due to an integer under- flow when reading the TPM 2 log multiple times.
Regards,
Stefan
Stefan Berger (3):
tpm: efi: Use local variable for calculating final log size
tpm: acpi: Check eventlog signature before using it
tpm: vtpm_proxy: Avoid reading host log when using a virtual device
drivers/char/tpm/eventlog/acpi.c | 31 +++++++++++++++++++++++++++++- drivers/char/tpm/eventlog/common.c | 3 +++ drivers/char/tpm/eventlog/efi.c | 10 ++++++---- 3 files changed, 39 insertions(+), 5 deletions(-)
Signature · 1 line
2.29.2
PATCH 2/3 tpm: acpi: Check eventlog signature before using it
Check the eventlog signature before using it. This avoids using an empty log, as may be the case when QEMU created the ACPI tables, rather than probing the EFI log next. This resolves an issue where the EFI log was empty since an empty ACPI log was used.
--- drivers/char/tpm/eventlog/acpi.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-)
drivers/char/tpm/eventlog/acpi.c +30 -1
@@ -41,6 +41,25 @@ struct acpi_tcpa { }; }; +/* check that the given log is indeed a TPM2 log */ +static int tpm_check_tpm2_log_header (void *bios_event_log,u64 len) +{ +struct tcg_efi_specid_event_head *efispecid; +struct tcg_pcr_event *event_header = bios_event_log; + +if (len <sizeof (*event_header)) +return 1 ; + len -=sizeof (*event_header); + + efispecid = (struct tcg_efi_specid_event_head *)event_header->event; +if (len <sizeof (*efispecid) || +memcmp (efispecid->signature,TCG_SPECID_SIG , +sizeof (TCG_SPECID_SIG ))) +return 1 ; + +return 0 ; +} +/* read binary bios log */ int tpm_read_log_acpi (struct tpm_chip *chip) {
@@ -52,6 +71,7 @@ int tpm_read_log_acpi(struct tpm_chip *chip)struct acpi_table_tpm2 *tbl;struct acpi_tpm2_phy *tpm2_phy;int format; +int ret; log = &chip->log;
@@ -112,6 +132,7 @@ int tpm_read_log_acpi(struct tpm_chip *chip) log->bios_event_log_end = log->bios_event_log + len; + ret = -EIO ; virt =acpi_os_map_iomem (start, len);if (!virt) goto err;
@@ -119,11 +140,19 @@ int tpm_read_log_acpi(struct tpm_chip *chip)memcpy_fromio (log->bios_event_log, virt, len);acpi_os_unmap_iomem (virt, len); + +if (chip->flags &TPM_CHIP_FLAG_TPM2 && +tpm_check_tpm2_log_header (log->bios_event_log, len)) { +/* try EFI log next */ + ret = -ENODEV ; + goto err; + } +return format; err:kfree (log->bios_event_log); log->bios_event_log =NULL ; -return -EIO ; +return ret; }
Signature · 1 line
2.29.2
PATCH 1/3 tpm: efi: Use local variable for calculating final log size
In reply to Stefan BergerWhen tpm_read_log_efi was called multiple times, which happens when one loads and unloads a TPM2 driver multiple times, then the global variable efi_tpm_final_log_size will at some point become a negative number due to the subtraction of final_events_preboot_size occurring each time. Use a local_efi_tpm_final_log_size to avoid this integer underflow.
The following issue is now resolved:
Mar 8 15:35:12 hibinst kernel: Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 Mar 8 15:35:12 hibinst kernel: Workqueue: tpm-vtpm vtpm_proxy_work [tpm_vtpm_proxy] Mar 8 15:35:12 hibinst kernel: RIP: 0010:__memcpy+0x12/0x20 Mar 8 15:35:12 hibinst kernel: Code: 00 b8 01 00 00 00 85 d2 74 0a c7 05 44 7b ef 00 0f 00 00 00 c3 cc cc cc 66 66 90 66 90 48 89 f8 48 89 d1 48 c1 e9 03 83 e2 07 <f3> 48 a5 89 d1 f3 a4 c3 66 0f 1f 44 00 00 48 89 f8 48 89 d1 f3 a4 Mar 8 15:35:12 hibinst kernel: RSP: 0018:ffff9ac4c0fcfde0 EFLAGS: 00010206 Mar 8 15:35:12 hibinst kernel: RAX: ffff88f878cefed5 RBX: ffff88f878ce9000 RCX: 1ffffffffffffe0f Mar 8 15:35:12 hibinst kernel: RDX: 0000000000000003 RSI: ffff9ac4c003bff9 RDI: ffff88f878cf0e4d Mar 8 15:35:12 hibinst kernel: RBP: ffff9ac4c003b000 R08: 0000000000001000 R09: 000000007e9d6073 Mar 8 15:35:12 hibinst kernel: R10: ffff9ac4c003b000 R11: ffff88f879ad3500 R12: 0000000000000ed5 Mar 8 15:35:12 hibinst kernel: R13: ffff88f878ce9760 R14: 0000000000000002 R15: ffff88f77de7f018 Mar 8 15:35:12 hibinst kernel: FS: 0000000000000000(0000) GS:ffff88f87bd00000(0000) knlGS:0000000000000000 Mar 8 15:35:12 hibinst kernel: CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 Mar 8 15:35:12 hibinst kernel: CR2: ffff9ac4c003c000 CR3: 00000001785a6004 CR4: 0000000000060ee0 Mar 8 15:35:12 hibinst kernel: Call Trace: Mar 8 15:35:12 hibinst kernel: tpm_read_log_efi+0x152/0x1a7 Mar 8 15:35:12 hibinst kernel: tpm_bios_log_setup+0xc8/0x1c0 Mar 8 15:35:12 hibinst kernel: tpm_chip_register+0x8f/0x260 Mar 8 15:35:12 hibinst kernel: vtpm_proxy_work+0x16/0x60 [tpm_vtpm_proxy] Mar 8 15:35:12 hibinst kernel: process_one_work+0x1b4/0x370 Mar 8 15:35:12 hibinst kernel: worker_thread+0x53/0x3e0 Mar 8 15:35:12 hibinst kernel: ? process_one_work+0x370/0x370
--- drivers/char/tpm/eventlog/efi.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/char/tpm/eventlog/efi.c +6 -4
@@ -18,6 +18,7 @@ int tpm_read_log_efi(struct tpm_chip *chip)struct efi_tcg2_final_events_table *final_tbl =NULL ;struct linux_efi_tpm_eventlog *log_tbl; +int local_efi_tpm_final_log_size;struct tpm_bios_log *log;u32 log_size;u8 tpm_log_version;
@@ -80,10 +81,11 @@ int tpm_read_log_efi(struct tpm_chip *chip) goto out; } - efi_tpm_final_log_size -= log_tbl->final_events_preboot_size; + local_efi_tpm_final_log_size = efi_tpm_final_log_size - + log_tbl->final_events_preboot_size; tmp =krealloc (log->bios_event_log, - log_size + efi_tpm_final_log_size, + log_size + local_efi_tpm_final_log_size,GFP_KERNEL );if (!tmp) {kfree (log->bios_event_log);
@@ -100,9 +102,9 @@ int tpm_read_log_efi(struct tpm_chip *chip)*/ memcpy ((void *)log->bios_event_log + log_size, final_tbl->events + log_tbl->final_events_preboot_size, - efi_tpm_final_log_size); + local_efi_tpm_final_log_size); log->bios_event_log_end = log->bios_event_log + - log_size + efi_tpm_final_log_size; + log_size + local_efi_tpm_final_log_size; out:memunmap (final_tbl);
Signature · 1 line
2.29.2
PATCH 3/3 tpm: vtpm_proxy: Avoid reading host log when using a virtual device
In reply to Stefan BergerAvoid allocating memory and reading the host log when a virtual device is used since this log is of no use to that driver. A virtual device can be identified through the flag TPM_CHIP_FLAG_VIRTUAL, which is only set for the tpm_vtpm_proxy driver.
--- drivers/char/tpm/eventlog/common.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/char/tpm/eventlog/common.c +3 -0
@@ -107,6 +107,9 @@ void tpm_bios_log_setup(struct tpm_chip *chip)int log_version;int rc =0 ; +if (chip->flags &TPM_CHIP_FLAG_VIRTUAL ) +return ; + rc =tpm_read_log (chip);if (rc <0 )return ;
Signature · 1 line
2.29.2
Jarkko Sakkinen
maintainerRe: [PATCH 1/3] tpm: efi: Use local variable for calculating final log size
In reply to Stefan BergerQuoted 48 lines · Stefan Berger
When
tpm_read_log_efiwas called multiple times, which happens when one loads and unloads a TPM2 driver multiple times, then the global variableefi_tpm_final_log_sizewill at some point become a negative number due to the subtraction offinal_events_preboot_sizeoccurring each time. Use alocal_efi_tpm_final_log_sizeto avoid this integer underflow.The following issue is now resolved:
Mar 8 15:35:12 hibinst kernel: Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 Mar 8 15:35:12 hibinst kernel: Workqueue: tpm-vtpm vtpm_proxy_work [tpm_vtpm_proxy] Mar 8 15:35:12 hibinst kernel: RIP: 0010:__memcpy+0x12/0x20 Mar 8 15:35:12 hibinst kernel: Code: 00 b8 01 00 00 00 85 d2 74 0a c7 05 44 7b ef 00 0f 00 00 00 c3 cc cc cc 66 66 90 66 90 48 89 f8 48 89 d1 48 c1 e9 03 83 e2 07 <f3> 48 a5 89 d1 f3 a4 c3 66 0f 1f 44 00 00 48 89 f8 48 89 d1 f3 a4 Mar 8 15:35:12 hibinst kernel: RSP: 0018:ffff9ac4c0fcfde0 EFLAGS: 00010206 Mar 8 15:35:12 hibinst kernel: RAX: ffff88f878cefed5 RBX: ffff88f878ce9000 RCX: 1ffffffffffffe0f Mar 8 15:35:12 hibinst kernel: RDX: 0000000000000003 RSI: ffff9ac4c003bff9 RDI: ffff88f878cf0e4d Mar 8 15:35:12 hibinst kernel: RBP: ffff9ac4c003b000 R08: 0000000000001000 R09: 000000007e9d6073 Mar 8 15:35:12 hibinst kernel: R10: ffff9ac4c003b000 R11: ffff88f879ad3500 R12: 0000000000000ed5 Mar 8 15:35:12 hibinst kernel: R13: ffff88f878ce9760 R14: 0000000000000002 R15: ffff88f77de7f018 Mar 8 15:35:12 hibinst kernel: FS: 0000000000000000(0000) GS:ffff88f87bd00000(0000) knlGS:0000000000000000 Mar 8 15:35:12 hibinst kernel: CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 Mar 8 15:35:12 hibinst kernel: CR2: ffff9ac4c003c000 CR3: 00000001785a6004 CR4: 0000000000060ee0 Mar 8 15:35:12 hibinst kernel: Call Trace: Mar 8 15:35:12 hibinst kernel: tpm_read_log_efi+0x152/0x1a7 Mar 8 15:35:12 hibinst kernel: tpm_bios_log_setup+0xc8/0x1c0 Mar 8 15:35:12 hibinst kernel: tpm_chip_register+0x8f/0x260 Mar 8 15:35:12 hibinst kernel: vtpm_proxy_work+0x16/0x60 [tpm_vtpm_proxy] Mar 8 15:35:12 hibinst kernel: process_one_work+0x1b4/0x370 Mar 8 15:35:12 hibinst kernel: worker_thread+0x53/0x3e0 Mar 8 15:35:12 hibinst kernel: ? process_one_work+0x370/0x370Signed-off-by Stefan Berger <[email protected]>--- drivers/char/tpm/eventlog/efi.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)drivers/char/tpm/eventlog/efi.c +1
-0@@ -18,6 +18,7 @@ int tpm_read_log_efi(struct tpm_chip *chip)struct efi_tcg2_final_events_table *final_tbl =NULL ;struct linux_efi_tpm_eventlog *log_tbl; +int local_efi_tpm_final_log_size;struct tpm_bios_log *log;u32 log_size;u8 tpm_log_version;
drivers/char/tpm/eventlog/efi.c int tpm_read_log_efi(struct tpm_chip *chip) · line 85
Quoted 6 lines
drivers/char/tpm/eventlog/efi.c +2
-1@@ -80,10 +81,11 @@ int tpm_read_log_efi(struct tpm_chip *chip) goto out; } - efi_tpm_final_log_size -= log_tbl->final_events_preboot_size; + local_efi_tpm_final_log_size = efi_tpm_final_log_size - + log_tbl->final_events_preboot_size;
This starts to have so many weird locals that an inline comment here in plain Enlighs would be nice explaining the calculation.
drivers/char/tpm/eventlog/efi.c int tpm_read_log_efi(struct tpm_chip *chip) · line 88
Quoted 4 lines
drivers/char/tpm/eventlog/efi.c +1
-1tmp = krealloc(log->bios_event_log, - log_size + efi_tpm_final_log_size, + log_size + local_efi_tpm_final_log_size,
Ditto.
drivers/char/tpm/eventlog/efi.c int tpm_read_log_efi(struct tpm_chip *chip) · line 107
Quoted 12 lines
drivers/char/tpm/eventlog/efi.c +2
-2GFP_KERNEL);if (!tmp) {kfree (log->bios_event_log);@@ -100,9 +102,9 @@ int tpm_read_log_efi(struct tpm_chip *chip)*/ memcpy ((void *)log->bios_event_log + log_size, final_tbl->events + log_tbl->final_events_preboot_size, - efi_tpm_final_log_size); + local_efi_tpm_final_log_size); log->bios_event_log_end = log->bios_event_log + - log_size + efi_tpm_final_log_size; + log_size + local_efi_tpm_final_log_size;
Ditto.
Quoted 7 lines
drivers/char/tpm/eventlog/efi.c +0
-0out:memunmap (final_tbl);Signature · 1 line
2.29.2
I think this is good chance to improve the documentation a bit.
/Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH 2/3] tpm: acpi: Check eventlog signature before using it
In reply to Stefan BergerQuoted 16 lines · Stefan Berger
Check the eventlog signature before using it. This avoids using an empty log, as may be the case when QEMU created the ACPI tables, rather than probing the EFI log next. This resolves an issue where the EFI log was empty since an empty ACPI log was used.
Fixes 85467f63a05c ("tpm: Add support for event log pointer found in TPM2 ACPI table")Signed-off-by Stefan Berger <[email protected]>--- drivers/char/tpm/eventlog/acpi.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-)drivers/char/tpm/eventlog/acpi.c +0
-0
drivers/char/tpm/eventlog/acpi.c struct acpi_tcpa { · line 44
Quoted 4 lines
drivers/char/tpm/eventlog/acpi.c +1
-0@@ -41,6 +41,25 @@ struct acpi_tcpa { }; }; +/* check that the given log is indeed a TPM2 log */
/* Check that the given log is indeed a TPM2 log. */
drivers/char/tpm/eventlog/acpi.c struct acpi_tcpa { · line 45
Quoted 1 line
drivers/char/tpm/eventlog/acpi.c +1
-0+static int tpm_check_tpm2_log_header (void *bios_event_log,u64 len)
Just by this name does not give any clue what the function does. "check" can refer to almost anything.
Perhaps just tpm_is_tpm2_log()?
drivers/char/tpm/eventlog/acpi.c struct acpi_tcpa { · line 48
Quoted 3 lines
drivers/char/tpm/eventlog/acpi.c +3
-0+{ +struct tcg_efi_specid_event_head *efispecid; +struct tcg_pcr_event *event_header = bios_event_log;
Please, reorder these declarations (reverse christmas tree).
drivers/char/tpm/eventlog/acpi.c struct acpi_tcpa { · line 57
Quoted 9 lines
drivers/char/tpm/eventlog/acpi.c +9
-0+ +if (len <sizeof (*event_header)) +return 1 ; + len -=sizeof (*event_header); + + efispecid = (struct tcg_efi_specid_event_head *)event_header->event; +if (len <sizeof (*efispecid) || +memcmp (efispecid->signature,TCG_SPECID_SIG , +sizeof (TCG_SPECID_SIG )))
Please never put memcmp() inside a conditional statement.
Quoted 49 lines
drivers/char/tpm/eventlog/acpi.c +16
-1+return 1 ; + +return 0 ; +} +/* read binary bios log */ int tpm_read_log_acpi (struct tpm_chip *chip) {@@ -52,6 +71,7 @@ int tpm_read_log_acpi(struct tpm_chip *chip)struct acpi_table_tpm2 *tbl;struct acpi_tpm2_phy *tpm2_phy;int format; +int ret; log = &chip->log;@@ -112,6 +132,7 @@ int tpm_read_log_acpi(struct tpm_chip *chip) log->bios_event_log_end = log->bios_event_log + len; + ret = -EIO ; virt =acpi_os_map_iomem (start, len);if (!virt) goto err;@@ -119,11 +140,19 @@ int tpm_read_log_acpi(struct tpm_chip *chip)memcpy_fromio (log->bios_event_log, virt, len);acpi_os_unmap_iomem (virt, len); + +if (chip->flags &TPM_CHIP_FLAG_TPM2 && +tpm_check_tpm2_log_header (log->bios_event_log, len)) { +/* try EFI log next */ + ret = -ENODEV ; + goto err; + } +return format; err:kfree (log->bios_event_log); log->bios_event_log =NULL ; -return -EIO ; +return ret; }Signature · 1 line
2.29.2
/Jarkko