libstub,tpm: fix small bugs and improve error reporting
Subsystems (2)
v2 cover libstub,tpm: fix small bugs and improve error reporting
The efi/tpm code has a number of small signed/unsigned bugs and inaccuracies are prone to cause further bugs in a difficult to debug manner. For example, there is a signed/unsigned mismatch in efi/tpm.c that can lead to calling memblock_reserve on a range with an effectively negative length.
Additionally, there are silently ignored error conditions may result in undefined behavior. Address these.
Gregory Price (4):
tpm: fix signed/unsigned bug when checking event logs
tpm: do not ignore memblock_reserve return value
tpm: fix unsigned/signed mismatch errors related to
__calc_tpm2_event_size
libstub,tpm: do not ignore failure case when reading final event log
drivers/firmware/efi/libstub/tpm.c | 9 ++++++--- drivers/firmware/efi/tpm.c | 26 ++++++++++++++++---------- include/linux/tpm_eventlog.h | 2 +- 3 files changed, 23 insertions(+), 14 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/4 tpm: fix signed/unsigned bug when checking event logs
A prior bugfix that fixes a signed/unsigned error causes another signed unsigned error.
A situation where log_tbl->size is invalid can cause the size passed to memblock_reserve to become negative.
log_size from the main event log is an unsigned int, and the code reduces to the following
u64 value = (int)unsigned_value;
This results in sign extension, and the value sent to memblock_reserve becomes effectively negative.
--- drivers/firmware/efi/tpm.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/firmware/efi/tpm.c +9 -8
@@ -40,7 +40,8 @@ int __init efi_tpm_eventlog_init(void) {struct linux_efi_tpm_eventlog *log_tbl;struct efi_tcg2_final_events_table *final_tbl; -int tbl_size; +unsigned int tbl_size; +int final_tbl_size;int ret =0 ;if (efi.tpm_log ==EFI_INVALID_TABLE_ADDR ) {
@@ -80,26 +81,26 @@ int __init efi_tpm_eventlog_init(void) goto out; } - tbl_size =0 ; + final_tbl_size =0 ;if (final_tbl->nr_events !=0 ) {void *events = (void *)efi.tpm_final_log +sizeof (final_tbl->version) +sizeof (final_tbl->nr_events); - tbl_size =tpm2_calc_event_log_size (events, - final_tbl->nr_events, - log_tbl->log); + final_tbl_size =tpm2_calc_event_log_size (events, + final_tbl->nr_events, + log_tbl->log); } -if (tbl_size <0 ) { +if (final_tbl_size <0 ) {pr_err (FW_BUG "Failed to parse event in TPM Final Events Log\n" ); ret = -EINVAL ; goto out_calc; }memblock_reserve (efi.tpm_final_log, - tbl_size +sizeof (*final_tbl)); - efi_tpm_final_log_size = tbl_size; + final_tbl_size +sizeof (*final_tbl)); + efi_tpm_final_log_size = final_tbl_size; out_calc:early_memunmap (final_tbl,sizeof (*final_tbl));
Signature · 1 line
2.43.0
Re: [PATCH v2 1/4] tpm: fix signed/unsigned bug when checking event logs
Quoted 26 lines · Gregory Price
A prior bugfix that fixes a signed/unsigned error causes another signed unsigned error.
A situation where
log_tbl->sizeis invalid can cause the size passed tomemblock_reserveto become negative.
log_sizefrom the main event log is an unsigned int, and the code reduces to the followingu64 value = (int)unsigned_value;
This results in sign extension, and the value sent to
memblock_reservebecomes effectively negative.Fixes be59d57f9806 ("efi/tpm: Fix sanity check of unsigned tbl_size being less than zero")Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/tpm.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)drivers/firmware/efi/tpm.c +0
-0
drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 43
Quoted 5 lines
drivers/firmware/efi/tpm.c +1
-1@@ -40,7 +40,8 @@ int __init efi_tpm_eventlog_init(void) {struct linux_efi_tpm_eventlog *log_tbl;struct efi_tcg2_final_events_table *final_tbl; -int tbl_size; +unsigned int tbl_size;
nit, perhaps u32 is better here
Quoted 39 lines
drivers/firmware/efi/tpm.c +8
-8+int final_tbl_size;int ret =0 ;if (efi.tpm_log ==EFI_INVALID_TABLE_ADDR ) {@@ -80,26 +81,26 @@ int __init efi_tpm_eventlog_init(void) goto out; } - tbl_size =0 ; + final_tbl_size =0 ;if (final_tbl->nr_events !=0 ) {void *events = (void *)efi.tpm_final_log +sizeof (final_tbl->version) +sizeof (final_tbl->nr_events); - tbl_size =tpm2_calc_event_log_size (events, - final_tbl->nr_events, - log_tbl->log); + final_tbl_size =tpm2_calc_event_log_size (events, + final_tbl->nr_events, + log_tbl->log); } -if (tbl_size <0 ) { +if (final_tbl_size <0 ) {pr_err (FW_BUG "Failed to parse event in TPM Final Events Log\n" ); ret = -EINVAL ; goto out_calc; }memblock_reserve (efi.tpm_final_log, - tbl_size +sizeof (*final_tbl)); - efi_tpm_final_log_size = tbl_size; + final_tbl_size +sizeof (*final_tbl)); + efi_tpm_final_log_size = final_tbl_size; out_calc:early_memunmap (final_tbl,sizeof (*final_tbl)); --
Quoted 2 lines
2.43.0
PATCH v2 2/4 tpm: do not ignore memblock_reserve return value
In reply to Gregory Pricetpm code currently ignores a relevant failure case silently. Add an error to make this failure non-silent.
--- drivers/firmware/efi/tpm.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/firmware/efi/tpm.c +6 -1
@@ -61,7 +61,12 @@ int __init efi_tpm_eventlog_init(void) } tbl_size =sizeof (*log_tbl) + log_tbl->size; -memblock_reserve (efi.tpm_log, tbl_size); +if (memblock_reserve (efi.tpm_log, tbl_size)) { +pr_err ("TPM Event Log memblock reserve fails (0x%lx, 0x%x)\n" , + efi.tpm_log, tbl_size); + ret = -ENOMEM ; + goto out; + }if (efi.tpm_final_log ==EFI_INVALID_TABLE_ADDR ) {pr_info ("TPM Final Events table not present\n" );
Signature · 1 line
2.43.0
Ard Biesheuvel
maintainerRe: [PATCH v2 2/4] tpm: do not ignore memblock_reserve return value
(cc Dave)
Quoted 15 lines · Gregory Price
tpm code currently ignores a relevant failure case silently. Add an error to make this failure non-silent.
Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/tpm.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)drivers/firmware/efi/tpm.c +0
-0@@ -61,7 +61,12 @@ int __init efi_tpm_eventlog_init(void) }
drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 70
Quoted 10 lines
drivers/firmware/efi/tpm.c +6
-1tbl_size =sizeof (*log_tbl) + log_tbl->size; -memblock_reserve (efi.tpm_log, tbl_size); +if (memblock_reserve (efi.tpm_log, tbl_size)) { +pr_err ("TPM Event Log memblock reserve fails (0x%lx, 0x%x)\n" , + efi.tpm_log, tbl_size); + ret = -ENOMEM ; + goto out; + }
Given the discussion in the other thread, I wonder if this should be efi_mem_reserve() instead - might as well fix that too.
Dave?
Re: [PATCH v2 2/4] tpm: do not ignore memblock_reserve return value
Quoted 32 lines · Ard Biesheuvel
(cc Dave)
Quoted 15 lines · Gregory Price
tpm code currently ignores a relevant failure case silently. Add an error to make this failure non-silent.
Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/tpm.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)drivers/firmware/efi/tpm.c +0
-0@@ -61,7 +61,12 @@ int __init efi_tpm_eventlog_init(void) }drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 70
Quoted 10 lines
drivers/firmware/efi/tpm.c +6
-1tbl_size =sizeof (*log_tbl) + log_tbl->size; -memblock_reserve (efi.tpm_log, tbl_size); +if (memblock_reserve (efi.tpm_log, tbl_size)) { +pr_err ("TPM Event Log memblock reserve fails (0x%lx, 0x%x)\n" , + efi.tpm_log, tbl_size); + ret = -ENOMEM ; + goto out; + }Given the discussion in the other thread, I wonder if this should be
efi_mem_reserve()instead - might as well fix that too.
Quoted 1 line
Dave?
I dont believe efi_mem_reserve is needed after your patch in
https://lore.kernel.org/all/[email protected]/
which will cover both kexec_load and kexec_file_load cases.
Thanks,
Usama
PATCH v2 3/4 tpm: fix unsigned/signed mismatch errors related to __calc_tpm2_event_size
In reply to Gregory Price__calc_tpm2_event_size returns 0 or a positive length, but return values are often interpreted as ints. Convert everything over to u32 to avoid signed/unsigned logic errors.
--- drivers/firmware/efi/libstub/tpm.c | 6 +++--- drivers/firmware/efi/tpm.c | 2 +- include/linux/tpm_eventlog.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-)
drivers/firmware/efi/libstub/tpm.c +3 -3
@@ -57,7 +57,7 @@ static void efi_retrieve_tcg2_eventlog(int version, efi_physical_addr_t log_locastruct linux_efi_tpm_eventlog *log_tbl =NULL ;unsigned long first_entry_addr, last_entry_addr;size_t log_size, last_entry_size; -int final_events_size =0 ; +u32 final_events_size =0 ; first_entry_addr = (unsigned long ) log_location;
@@ -110,9 +110,9 @@ static void efi_retrieve_tcg2_eventlog(int version, efi_physical_addr_t log_loca*/ if (final_events_table && final_events_table->nr_events) {struct tcg_pcr_event2_head *header; -int offset; +u32 offset;void *data; -int event_size; +u32 event_size;int i = final_events_table->nr_events; data = (void *)final_events_table;
drivers/firmware/efi/tpm.c +1 -1
@@ -19,7 +19,7 @@ EXPORT_SYMBOL(efi_tpm_final_log_size);static int __init tpm2_calc_event_log_size (void *data,int count,void *size_info) {struct tcg_pcr_event2_head *header; -int event_size, size =0 ; +u32 event_size, size =0 ;while (count >0 ) { header = data + size;
include/linux/tpm_eventlog.h +1 -1
@@ -157,7 +157,7 @@ struct tcg_algorithm_info {* Return: size of the event on success, 0 on failure */ -static __always_inline int __calc_tpm2_event_size(struct tcg_pcr_event2_head *event, +static __always_inline u32 __calc_tpm2_event_size(struct tcg_pcr_event2_head *event,struct tcg_pcr_event *event_header,bool do_mapping) {
Signature · 1 line
2.43.0
PATCH v2 4/4 libstub,tpm: do not ignore failure case when reading final event log
In reply to Gregory PriceCurrent code fails to check for an error case when reading events from final event log to calculate offsets. Check the error case, and break early because all subsequent calls will also fail.
--- drivers/firmware/efi/libstub/tpm.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/firmware/efi/libstub/tpm.c +3 -0
@@ -124,6 +124,9 @@ static void efi_retrieve_tcg2_eventlog(int version, efi_physical_addr_t log_loca event_size =__calc_tpm2_event_size (header, (void *)(long )log_location, false); +/* If calc fails this is a malformed log */ +if (!event_size) +break ; final_events_size += event_size; i--; }
Signature · 1 line
2.43.0
Ard Biesheuvel
maintainerQuoted 10 lines · Gregory Price
The efi/tpm code has a number of small signed/unsigned bugs and inaccuracies are prone to cause further bugs in a difficult to debug manner. For example, there is a signed/unsigned mismatch in
efi/tpm.cthat can lead to callingmemblock_reserveon a range with an effectively negative length.Additionally, there are silently ignored error conditions may result in undefined behavior. Address these.
Quoted 9 lines
Signed-off-by Gregory Price <[email protected]>Gregory Price (4):
tpm: fix signed/unsigned bug when checking event logs
tpm: do not ignorememblock_reservereturn value
tpm: fix unsigned/signed mismatch errors related to__calc_tpm2_event_sizelibstub,tpm: do not ignore failure case when reading final event log
Now queued up - thanks.
Quoted 8 lines
drivers/firmware/efi/libstub/tpm.c | 9 ++++++--- drivers/firmware/efi/tpm.c | 26 ++++++++++++++++---------- include/linux/tpm_eventlog.h | 2 +- 3 files changed, 23 insertions(+), 14 deletions(-)--
2.43.0