Skip to contentSkip to conversation

efi: reserve memory for tpm_log only if TPM final events table is valid

PATCH In review Usama Arif 2 participants 7 messages Aug 30, 2024 EFI

lore
Share & tools
lore

Usama Arif

author
#0 lore raw

PATCH 1/1 efi: reserve memory for tpm_log only if TPM final events table is valid

If efi.tpm_log is corrupted, log_tbl->size can be garbage (and negative). This can result in a large memblock reservation, resulting in the kernel booting without sufficient memory. Move the memblock reservation after log_tbl->version check, and check the value of both tbl_size and memblock_reserve.

---
 drivers/firmware/efi/tpm.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/firmware/efi/tpm.c +13 -3
index e8d69bd548f3..cfc6a065f441 100644
@@ -59,9 +59,6 @@ int __init efi_tpm_eventlog_init(void)
 		return -ENOMEM;
 	}
 
-	tbl_size = sizeof(*log_tbl) + log_tbl->size;
-	memblock_reserve(efi.tpm_log, tbl_size);
-
 	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
 		pr_info("TPM Final Events table not present\n");
 		goto out;
@@ -70,6 +67,19 @@ int __init efi_tpm_eventlog_init(void)
 		goto out;
 	}
 
+	tbl_size = sizeof(*log_tbl) + log_tbl->size;
+	if (tbl_size < 0) {
+		pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n");
+		ret = -EINVAL;
+		goto out;
+	}
+	if (memblock_reserve(efi.tpm_log, tbl_size)) {
+		pr_err("TPM Event Log memblock reserve fails 0x%lx - %x\n",
+		       efi.tpm_log, tbl_size);
+		ret = -ENOMEM;
+		goto out;
+	}
+
 	final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl));
 
 	if (!final_tbl) {
Signature · 1 line
2.43.5
#1 lore raw
Quoted 16 lines · Usama Arif

If efi.tpm_log is corrupted, log_tbl->size can be garbage (and negative). This can result in a large memblock reservation, resulting in the kernel booting without sufficient memory. Move the memblock reservation after log_tbl->version check, and check the value of both tbl_size and memblock_reserve.

---
 drivers/firmware/efi/tpm.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/firmware/efi/tpm.c +0 -0
index e8d69bd548f3..cfc6a065f441 100644

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 64

Quoted 9 lines
drivers/firmware/efi/tpm.c +0 -3
@@ -59,9 +59,6 @@ int __init efi_tpm_eventlog_init(void)
 		return -ENOMEM;
 	}
 
-	tbl_size = sizeof(*log_tbl) + log_tbl->size;
-	memblock_reserve(efi.tpm_log, tbl_size);
-
 	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
 		pr_info("TPM Final Events table not present\n");
 		goto out;

The final event table is not present in TCG 1_2 format, but the tpm log still needs to be mapped. So this change is incorrect for v1_2.

Quoted 23 lines
drivers/firmware/efi/tpm.c +13 -0
@@ -70,6 +67,19 @@ int __init efi_tpm_eventlog_init(void)
 		goto out;
 	}
 
+	tbl_size = sizeof(*log_tbl) + log_tbl->size;
+	if (tbl_size < 0) {
+		pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n");
+		ret = -EINVAL;
+		goto out;
+	}
+	if (memblock_reserve(efi.tpm_log, tbl_size)) {
+		pr_err("TPM Event Log memblock reserve fails 0x%lx - %x\n",
+		       efi.tpm_log, tbl_size);
+		ret = -ENOMEM;
+		goto out;
+	}
+
 	final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl));
 
 	if (!final_tbl) {
Signature · 1 line
2.43.5
author
#2 lore raw
Quoted 20 lines · Gregory Price
Quoted 19 lines · Usama Arif

If efi.tpm_log is corrupted, log_tbl->size can be garbage (and negative). This can result in a large memblock reservation, resulting in the kernel booting without sufficient memory. Move the memblock reservation after log_tbl->version check, and check the value of both tbl_size and memblock_reserve.

---
 drivers/firmware/efi/tpm.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/firmware/efi/tpm.c +0 -0
index e8d69bd548f3..cfc6a065f441 100644
@@ -59,9 +59,6 @@ int __init efi_tpm_eventlog_init(void)
 		return -ENOMEM;
 	}
 
Quoted 10 lines

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 64

Quoted 6 lines
drivers/firmware/efi/tpm.c +0 -3
-	tbl_size = sizeof(*log_tbl) + log_tbl->size;
-	memblock_reserve(efi.tpm_log, tbl_size);
-
 	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
 		pr_info("TPM Final Events table not present\n");
 		goto out;

The final event table is not present in TCG 1_2 format, but the tpm log still needs to be mapped. So this change is incorrect for v1_2.

hmm so we have

	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
		pr_info("TPM Final Events table not present\n");
		goto out;
	} else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
		pr_warn(FW_BUG "TPM Final Events table invalid\n");
		goto out;
	}

If the format is TCG 1_2, then log_tbl is not used?

Quoted 24 lines
Quoted 24 lines
drivers/firmware/efi/tpm.c +13 -0
@@ -70,6 +67,19 @@ int __init efi_tpm_eventlog_init(void)
 		goto out;
 	}
 
+	tbl_size = sizeof(*log_tbl) + log_tbl->size;
+	if (tbl_size < 0) {
+		pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n");
+		ret = -EINVAL;
+		goto out;
+	}
+	if (memblock_reserve(efi.tpm_log, tbl_size)) {
+		pr_err("TPM Event Log memblock reserve fails 0x%lx - %x\n",
+		       efi.tpm_log, tbl_size);
+		ret = -ENOMEM;
+		goto out;
+	}
+
 	final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl));
 
 	if (!final_tbl) {
Signature · 1 line
2.43.5
author
#3 lore raw
Quoted 44 lines · Usama Arif
Quoted 20 lines · Gregory Price
Quoted 19 lines · Usama Arif

If efi.tpm_log is corrupted, log_tbl->size can be garbage (and negative). This can result in a large memblock reservation, resulting in the kernel booting without sufficient memory. Move the memblock reservation after log_tbl->version check, and check the value of both tbl_size and memblock_reserve.

---
 drivers/firmware/efi/tpm.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/firmware/efi/tpm.c +0 -0
index e8d69bd548f3..cfc6a065f441 100644
@@ -59,9 +59,6 @@ int __init efi_tpm_eventlog_init(void)
 		return -ENOMEM;
 	}
 
Quoted 10 lines

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 64

Quoted 6 lines
drivers/firmware/efi/tpm.c +0 -3
-	tbl_size = sizeof(*log_tbl) + log_tbl->size;
-	memblock_reserve(efi.tpm_log, tbl_size);
-
 	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
 		pr_info("TPM Final Events table not present\n");
 		goto out;

The final event table is not present in TCG 1_2 format, but the tpm log still needs to be mapped. So this change is incorrect for v1_2.

hmm so we have

	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
		pr_info("TPM Final Events table not present\n");
		goto out;
	} else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
		pr_warn(FW_BUG "TPM Final Events table invalid\n");
		goto out;
	}
Quoted 2 lines

If the format is TCG 1_2, then log_tbl is not used?

Ah its the case that log_tbl->size will be valid, even if its TCG 2. Got it, Thanks!

Quoted 26 lines
Quoted 25 lines
Quoted 25 lines
drivers/firmware/efi/tpm.c +13 -0
@@ -70,6 +67,19 @@ int __init efi_tpm_eventlog_init(void)
 		goto out;
 	}
 
+	tbl_size = sizeof(*log_tbl) + log_tbl->size;
+	if (tbl_size < 0) {
+		pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n");
+		ret = -EINVAL;
+		goto out;
+	}
+	if (memblock_reserve(efi.tpm_log, tbl_size)) {
+		pr_err("TPM Event Log memblock reserve fails 0x%lx - %x\n",
+		       efi.tpm_log, tbl_size);
+		ret = -ENOMEM;
+		goto out;
+	}
+
 	final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl));
 
 	if (!final_tbl) {
Signature · 1 line
2.43.5
#4 lore raw
Quoted 47 lines · Usama Arif
Quoted 44 lines · Usama Arif
Quoted 20 lines · Gregory Price
Quoted 19 lines · Usama Arif

If efi.tpm_log is corrupted, log_tbl->size can be garbage (and negative). This can result in a large memblock reservation, resulting in the kernel booting without sufficient memory. Move the memblock reservation after log_tbl->version check, and check the value of both tbl_size and memblock_reserve.

---
 drivers/firmware/efi/tpm.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/firmware/efi/tpm.c +0 -0
index e8d69bd548f3..cfc6a065f441 100644
@@ -59,9 +59,6 @@ int __init efi_tpm_eventlog_init(void)
 		return -ENOMEM;
 	}
 
Quoted 10 lines

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 64

Quoted 6 lines
drivers/firmware/efi/tpm.c +0 -3
-	tbl_size = sizeof(*log_tbl) + log_tbl->size;
-	memblock_reserve(efi.tpm_log, tbl_size);
-
 	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
 		pr_info("TPM Final Events table not present\n");
 		goto out;

The final event table is not present in TCG 1_2 format, but the tpm log still needs to be mapped. So this change is incorrect for v1_2.

hmm so we have

	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
		pr_info("TPM Final Events table not present\n");
		goto out;
	} else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
		pr_warn(FW_BUG "TPM Final Events table invalid\n");
		goto out;
	}
Quoted 5 lines
Quoted 2 lines

If the format is TCG 1_2, then log_tbl is not used?

Ah its the case that log_tbl->size will be valid, even if its TCG 2. Got it, Thanks!

No, not necessarily. The corruption issue is entirely separate from the patches here. size can technically be 0xffffffff and treated as valid because as far as I can see the spec does not define a maximum size.

the tl;dr here is that the above checks on tpm_final_log and log_tbl->version gate mapping/operating on the final log - but do not have anything to say about the event log.

There isn't really a good sanity check for whether or not to memblock_reserve the log. Possibly you add a LOG_FORMAT_MAX and check if version >= MAX, but again that should be separate from the change that checks the return value of the memblock_reserve call.

~Gregory

author
#5 lore raw
Quoted 62 lines · Gregory Price
Quoted 47 lines · Usama Arif
Quoted 44 lines · Usama Arif
Quoted 20 lines · Gregory Price
Quoted 19 lines · Usama Arif

If efi.tpm_log is corrupted, log_tbl->size can be garbage (and negative). This can result in a large memblock reservation, resulting in the kernel booting without sufficient memory. Move the memblock reservation after log_tbl->version check, and check the value of both tbl_size and memblock_reserve.

---
 drivers/firmware/efi/tpm.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/firmware/efi/tpm.c +0 -0
index e8d69bd548f3..cfc6a065f441 100644
@@ -59,9 +59,6 @@ int __init efi_tpm_eventlog_init(void)
 		return -ENOMEM;
 	}
 
Quoted 10 lines

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 64

Quoted 6 lines
drivers/firmware/efi/tpm.c +0 -3
-	tbl_size = sizeof(*log_tbl) + log_tbl->size;
-	memblock_reserve(efi.tpm_log, tbl_size);
-
 	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
 		pr_info("TPM Final Events table not present\n");
 		goto out;

The final event table is not present in TCG 1_2 format, but the tpm log still needs to be mapped. So this change is incorrect for v1_2.

hmm so we have

	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
		pr_info("TPM Final Events table not present\n");
		goto out;
	} else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
		pr_warn(FW_BUG "TPM Final Events table invalid\n");
		goto out;
	}
Quoted 5 lines
Quoted 2 lines

If the format is TCG 1_2, then log_tbl is not used?

Ah its the case that log_tbl->size will be valid, even if its TCG 2. Got it, Thanks!

No, not necessarily. The corruption issue is entirely separate from the patches here. size can technically be 0xffffffff and treated as valid because as far as I can see the spec does not define a maximum size.

the tl;dr here is that the above checks on tpm_final_log and log_tbl->version gate mapping/operating on the final log - but do not have anything to say about the event log.

Quoted 6 lines

There isn't really a good sanity check for whether or not to memblock_reserve the log. Possibly you add a LOG_FORMAT_MAX and check if version >= MAX, but again that should be separate from the change that checks the return value of the memblock_reserve call.

~Gregory

Yes, makes sense. Thanks!

#6 lore raw
In reply to Usama Arif
Quoted 44 lines · Usama Arif
Quoted 20 lines · Gregory Price
Quoted 19 lines · Usama Arif

If efi.tpm_log is corrupted, log_tbl->size can be garbage (and negative). This can result in a large memblock reservation, resulting in the kernel booting without sufficient memory. Move the memblock reservation after log_tbl->version check, and check the value of both tbl_size and memblock_reserve.

---
 drivers/firmware/efi/tpm.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/firmware/efi/tpm.c +0 -0
index e8d69bd548f3..cfc6a065f441 100644
@@ -59,9 +59,6 @@ int __init efi_tpm_eventlog_init(void)
 		return -ENOMEM;
 	}
 
Quoted 10 lines

drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 64

Quoted 6 lines
drivers/firmware/efi/tpm.c +0 -3
-	tbl_size = sizeof(*log_tbl) + log_tbl->size;
-	memblock_reserve(efi.tpm_log, tbl_size);
-
 	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
 		pr_info("TPM Final Events table not present\n");
 		goto out;

The final event table is not present in TCG 1_2 format, but the tpm log still needs to be mapped. So this change is incorrect for v1_2.

hmm so we have

	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
		pr_info("TPM Final Events table not present\n");
		goto out;
	} else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
		pr_warn(FW_BUG "TPM Final Events table invalid\n");
		goto out;
	}
Quoted 3 lines

If the format is TCG 1_2, then log_tbl is not used?

if format is tcg 1_2 then log_tbl is used, which is reflected in libstub drivers/firmware/efi/libstub/tpm.c

        efi_guid_t tpm2_guid = EFI_TCG2_PROTOCOL_GUID;
        int version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;

        status = efi_bs_call(locate_protocol, &tpm2_guid, NULL, (void **)&tpm2);
        if (status == EFI_SUCCESS) {
                status = efi_call_proto(tpm2, get_event_log, version, &log_location,
                                        &log_last_entry, &truncated);

                if (status != EFI_SUCCESS || !log_location) {
                        version = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
                        status = efi_call_proto(tpm2, get_event_log, version,
                                                &log_location, &log_last_entry,
                                                &truncated);
			^^^^^^^^ log still present in TCG 1_2 ^^^^^^^^^^^
                } else {
                        final_events_table =
                                get_efi_config_table(EFI_TCG2_FINAL_EVENTS_TABLE_GUID);
                }
        } else {

~Gregory