1
0
Fork 0
alistair23-linux/drivers/char/tpm
Tyler Hicks 8ffd778aff tpm: efi: Don't create binary_bios_measurements file for an empty log
Mimic the pre-existing ACPI and Device Tree event log behavior by not
creating the binary_bios_measurements file when the EFI TPM event log is
empty.

This fixes the following NULL pointer dereference that can occur when
reading /sys/kernel/security/tpm0/binary_bios_measurements after the
kernel received an empty event log from the firmware:

 BUG: kernel NULL pointer dereference, address: 000000000000002c
 #PF: supervisor read access in kernel mode
 #PF: error_code(0x0000) - not-present page
 PGD 0 P4D 0
 Oops: 0000 [#1] SMP PTI
 CPU: 2 PID: 3932 Comm: fwupdtpmevlog Not tainted 5.9.0-00003-g629990edad62 #17
 Hardware name: LENOVO 20LCS03L00/20LCS03L00, BIOS N27ET38W (1.24 ) 11/28/2019
 RIP: 0010:tpm2_bios_measurements_start+0x3a/0x550
 Code: 54 53 48 83 ec 68 48 8b 57 70 48 8b 1e 65 48 8b 04 25 28 00 00 00 48 89 45 d0 31 c0 48 8b 82 c0 06 00 00 48 8b 8a c8 06 00 00 <44> 8b 60 1c 48 89 4d a0 4c 89 e2 49 83 c4 20 48 83 fb 00 75 2a 49
 RSP: 0018:ffffa9c901203db0 EFLAGS: 00010246
 RAX: 0000000000000010 RBX: 0000000000000000 RCX: 0000000000000010
 RDX: ffff8ba1eb99c000 RSI: ffff8ba1e4ce8280 RDI: ffff8ba1e4ce8258
 RBP: ffffa9c901203e40 R08: ffffa9c901203dd8 R09: ffff8ba1ec443300
 R10: ffffa9c901203e50 R11: 0000000000000000 R12: ffff8ba1e4ce8280
 R13: ffffa9c901203ef0 R14: ffffa9c901203ef0 R15: ffff8ba1e4ce8258
 FS:  00007f6595460880(0000) GS:ffff8ba1ef880000(0000) knlGS:0000000000000000
 CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
 CR2: 000000000000002c CR3: 00000007d8d18003 CR4: 00000000003706e0
 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
 Call Trace:
  ? __kmalloc_node+0x113/0x320
  ? kvmalloc_node+0x31/0x80
  seq_read+0x94/0x420
  vfs_read+0xa7/0x190
  ksys_read+0xa7/0xe0
  __x64_sys_read+0x1a/0x20
  do_syscall_64+0x37/0x80
  entry_SYSCALL_64_after_hwframe+0x44/0xa9

In this situation, the bios_event_log pointer in the tpm_bios_log struct
was not NULL but was equal to the ZERO_SIZE_PTR (0x10) value. This was
due to the following kmemdup() in tpm_read_log_efi():

int tpm_read_log_efi(struct tpm_chip *chip)
{
...
	/* malloc EventLog space */
	log->bios_event_log = kmemdup(log_tbl->log, log_size, GFP_KERNEL);
	if (!log->bios_event_log) {
		ret = -ENOMEM;
		goto out;
	}
...
}

When log_size is zero, due to an empty event log from firmware,
ZERO_SIZE_PTR is returned from kmemdup(). Upon a read of the
binary_bios_measurements file, the tpm2_bios_measurements_start()
function does not perform a ZERO_OR_NULL_PTR() check on the
bios_event_log pointer before dereferencing it.

Rather than add a ZERO_OR_NULL_PTR() check in functions that make use of
the bios_event_log pointer, simply avoid creating the
binary_bios_measurements_file as is done in other event log retrieval
backends.

Explicitly ignore all of the events in the final event log when the main
event log is empty. The list of events in the final event log cannot be
accurately parsed without referring to the first event in the main event
log (the event log header) so the final event log is useless in such a
situation.

Fixes: 58cc1e4faf ("tpm: parse TPM event logs based on EFI table")
Link: https://lore.kernel.org/linux-integrity/E1FDCCCB-CA51-4AEE-AC83-9CDE995EAE52@canonical.com/
Reported-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
Reported-by: Kenneth R. Crudup <kenny@panix.com>
Reported-by: Mimi Zohar <zohar@linux.ibm.com>
Cc: ThiƩbaud Weksteen <tweek@google.com>
Cc: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Tyler Hicks <tyhicks@linux.microsoft.com>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
2020-11-06 18:21:08 +02:00
..
eventlog tpm: efi: Don't create binary_bios_measurements file for an empty log 2020-11-06 18:21:08 +02:00
st33zp24 tpm/st33zp24: fix spelling mistake "drescription" -> "description" 2020-07-02 17:49:00 +03:00
Kconfig tpm: tis: add support for MMIO TPM on SynQuacer 2020-10-02 00:21:02 +03:00
Makefile tpm: tis: add support for MMIO TPM on SynQuacer 2020-10-02 00:21:02 +03:00
tpm-chip.c tpm: Unify the mismatching TPM space buffer sizes 2020-07-24 09:26:23 +03:00
tpm-dev-common.c tpm: Fix TIS locality timeout problems 2020-07-02 17:48:59 +03:00
tpm-dev.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 441 2019-06-05 17:37:17 +02:00
tpm-dev.h tpm: Handle negative priv->response_len in tpm_common_read() 2020-01-08 18:11:09 +02:00
tpm-interface.c tpm: fix wrong return value in tpm_pcr_extend 2020-04-21 00:06:13 +03:00
tpm-sysfs.c tpm: use %*ph to print small buffer 2020-10-02 00:21:03 +03:00
tpm.h tpm: Unify the mismatching TPM space buffer sizes 2020-07-24 09:26:23 +03:00
tpm1-cmd.c tpm: provide a way to override the chip returned durations 2019-11-12 21:45:36 +02:00
tpm2-cmd.c tpm: Export tpm2_get_cc_attrs_tbl for ibmvtpm driver as module 2020-04-21 00:06:13 +03:00
tpm2-space.c tpm: Unify the mismatching TPM space buffer sizes 2020-07-24 09:26:23 +03:00
tpm_atmel.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 441 2019-06-05 17:37:17 +02:00
tpm_atmel.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 441 2019-06-05 17:37:17 +02:00
tpm_crb.c tpm_crb: fix fTPM on AMD Zen+ CPUs 2019-11-12 21:45:37 +02:00
tpm_ftpm_tee.c Enable multi-stage OP-TEE bus enumeration 2020-07-13 15:11:44 +02:00
tpm_ftpm_tee.h tpm/tpm_ftpm_tee: A driver for firmware TPM running inside TEE 2019-09-02 17:08:35 +03:00
tpm_i2c_atmel.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 13 2019-05-21 11:28:45 +02:00
tpm_i2c_infineon.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 441 2019-06-05 17:37:17 +02:00
tpm_i2c_nuvoton.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 13 2019-05-21 11:28:45 +02:00
tpm_ibmvtpm.c tpm: ibmvtpm: Wait for ready buffer before probing for TPM2 attributes 2020-07-02 17:49:00 +03:00
tpm_ibmvtpm.h tpm: ibmvtpm: Wait for buffer to be set before proceeding 2020-03-15 20:12:52 +02:00
tpm_infineon.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 441 2019-06-05 17:37:17 +02:00
tpm_nsc.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 441 2019-06-05 17:37:17 +02:00
tpm_ppi.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 441 2019-06-05 17:37:17 +02:00
tpm_tis.c tpm_tis: Disable interrupts on ThinkPad T490s 2020-11-06 18:21:00 +02:00
tpm_tis_core.c tpm_tis: Add a check for invalid status 2020-10-02 00:21:03 +03:00
tpm_tis_core.h tpm_tis: Add a check for invalid status 2020-10-02 00:21:03 +03:00
tpm_tis_spi.h tpm: tpm_tis_spi: Support cr50 devices 2019-11-12 21:45:37 +02:00
tpm_tis_spi_cr50.c tpm: tpm_tis_spi_cr50: use new structure for SPI transfer delays 2020-03-13 03:53:16 +02:00
tpm_tis_spi_main.c tpm_tis_spi: Prefer async probe 2020-07-02 17:49:00 +03:00
tpm_tis_synquacer.c tpm: tis: add support for MMIO TPM on SynQuacer 2020-10-02 00:21:02 +03:00
tpm_vtpm_proxy.c compat_ioctl: move drivers to compat_ptr_ioctl 2019-10-23 17:23:43 +02:00
tpmrm-dev.c tpm: Unify the mismatching TPM space buffer sizes 2020-07-24 09:26:23 +03:00
xen-tpmfront.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500 2019-06-19 17:09:55 +02:00