libstub,tpm: fix small bugs and improve error reporting
Subsystems (2)
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 leads to a memblock_reserve on a range with an effectively negative length.
Additionally, there are silently ignored error conditions that are better explicitly reported.
Finally, there exists some bad interaction between tpm and kexec that causes the log version and the log size to become corrupted. The log size cannot be reasonably sanity checked, as the value is a u32 and there is no defined max-size per the spec - however the version can at least be sanity checked. This reports the error and avoids calling memblock_reserve with clearly corrupted arguments.
Gregory Price (6):
tpm: fix signed/unsigned bug when checking event logs
tpm: do not ignore memblock_reserve return value libstub,tpm: provide indication of failure when getting event log
tpm: sanity check the log version before using it
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 | 14 ++++++++---- drivers/firmware/efi/tpm.c | 34 +++++++++++++++++++++--------- include/linux/tpm_eventlog.h | 2 +- 3 files changed, 35 insertions(+), 15 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/6 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
PATCH 2/6 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 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/firmware/efi/tpm.c +5 -1
@@ -61,7 +61,11 @@ 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); + 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
Ilias Apalodimas
reviewerRe: [PATCH 2/6] tpm: do not ignore memblock_reserve return value
Hi Gregory,
Quoted 14 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 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/firmware/efi/tpm.c +0
-0
drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 68
Quoted 9 lines
drivers/firmware/efi/tpm.c +5
-1@@ -61,7 +61,11 @@ 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); + goto out; + }
ret is going to be 0 here. I haven't followed the rest of the code and where this function is used, but you probably need to return -ENOMEM
Thanks
/Ilias
Quoted 6 lines
drivers/firmware/efi/tpm.c +0
-1if (efi.tpm_final_log ==EFI_INVALID_TABLE_ADDR ) {pr_info ("TPM Final Events table not present\n" ); --2.43.0
Re: [PATCH 2/6] tpm: do not ignore memblock_reserve return value
Quoted 19 lines · Ilias Apalodimas
Hi Gregory,
Quoted 16 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 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/firmware/efi/tpm.c +0
-0@@ -61,7 +61,11 @@ int __init efi_tpm_eventlog_init(void) }
Quoted 11 lines
drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 68
Quoted 7 lines
drivers/firmware/efi/tpm.c +5
-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); + goto out; + }ret is going to be 0 here. I haven't followed the rest of the code and where this function is used, but you probably need to return
-ENOMEM
good catch, will send v2
Quoted 8 lines
Thanks
/IliasQuoted 6 lines
drivers/firmware/efi/tpm.c +0
-1if (efi.tpm_final_log ==EFI_INVALID_TABLE_ADDR ) {pr_info ("TPM Final Events table not present\n" ); --2.43.0
PATCH 3/6 libstub,tpm: provide indication of failure when getting event log
In reply to Gregory PriceIf get_event_log fails, at least provide an indicator of this failure to assist debugging later failures that attempt to interact with it.
--- drivers/firmware/efi/libstub/tpm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/tpm.c +3 -1
@@ -185,8 +185,10 @@ void efi_retrieve_eventlog(void)get_efi_config_table (EFI_CC_FINAL_EVENTS_TABLE_GUID ); } -if (status !=EFI_SUCCESS || !log_location) +if (status !=EFI_SUCCESS || !log_location) { +efi_err ("TPM unable to provide Event Log\n" );return ; + }efi_retrieve_tcg2_eventlog (version, log_location, log_last_entry, truncated, final_events_table);
Signature · 1 line
2.43.0
Ilias Apalodimas
reviewerRe: [PATCH 3/6] libstub,tpm: provide indication of failure when getting event log
Hi Gregory,
Quoted 14 lines · Gregory Price
If
get_event_logfails, at least provide an indicator of this failure to assist debugging later failures that attempt to interact with it.Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/libstub/tpm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/firmware/efi/libstub/tpm.c +0
-0
drivers/firmware/efi/libstub/tpm.c void efi_retrieve_eventlog(void) · line 189
Quoted 6 lines
drivers/firmware/efi/libstub/tpm.c +2
-1@@ -185,8 +185,10 @@ void efi_retrieve_eventlog(void)get_efi_config_table (EFI_CC_FINAL_EVENTS_TABLE_GUID ); } -if (status !=EFI_SUCCESS || !log_location) +if (status !=EFI_SUCCESS || !log_location) { +efi_err ("TPM unable to provide Event Log\n" );
s/provide/retrieve/ and yes the print is going to be useful. Do you know if the EventLog is mandatory. Reading at the spec GetEventlog only has 2 return values, which implies you can't return "Not supported", but it's not explicitly stated anywhere
Thanks
/Ilias
Quoted 8 lines
drivers/firmware/efi/libstub/tpm.c +1
-1return ; + }efi_retrieve_tcg2_eventlog (version, log_location, log_last_entry, truncated, final_events_table); --2.43.0
Re: [PATCH 3/6] libstub,tpm: provide indication of failure when getting event log
Quoted 20 lines · Ilias Apalodimas
Hi Gregory,
Quoted 17 lines · Gregory Price
If
get_event_logfails, at least provide an indicator of this failure to assist debugging later failures that attempt to interact with it.Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/libstub/tpm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/firmware/efi/libstub/tpm.c +0
-0@@ -185,8 +185,10 @@ void efi_retrieve_eventlog(void)get_efi_config_table (EFI_CC_FINAL_EVENTS_TABLE_GUID ); }
Quoted 9 lines
drivers/firmware/efi/libstub/tpm.c void efi_retrieve_eventlog(void) · line 189
Quoted 3 lines
drivers/firmware/efi/libstub/tpm.c +2
-1-if (status !=EFI_SUCCESS || !log_location) +if (status !=EFI_SUCCESS || !log_location) { +efi_err ("TPM unable to provide Event Log\n" );s/provide/retrieve/ and yes the print is going to be useful. Do you know if the EventLog is mandatory. Reading at the spec GetEventlog only has 2 return values, which implies you can't return "Not supported", but it's not explicitly stated anywhere
I believe it is mandatory from my reading of the spec - but the "Final Event Log" was only added in 2.0. We report an error when 2.0 is reported but the final event log is not supported, so i figure we should probably report when the event log fails as well.
Quoted 10 lines
Thanks
/IliasQuoted 8 lines
drivers/firmware/efi/libstub/tpm.c +1
-1return ; + }efi_retrieve_tcg2_eventlog (version, log_location, log_last_entry, truncated, final_events_table); --2.43.0
Ilias Apalodimas
reviewerRe: [PATCH 3/6] libstub,tpm: provide indication of failure when getting event log
Quoted 32 lines · Gregory Price
Quoted 20 lines · Ilias Apalodimas
Hi Gregory,
Quoted 17 lines · Gregory Price
If
get_event_logfails, at least provide an indicator of this failure to assist debugging later failures that attempt to interact with it.Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/libstub/tpm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/firmware/efi/libstub/tpm.c +0
-0@@ -185,8 +185,10 @@ void efi_retrieve_eventlog(void)get_efi_config_table (EFI_CC_FINAL_EVENTS_TABLE_GUID ); }Quoted 9 lines
drivers/firmware/efi/libstub/tpm.c void efi_retrieve_eventlog(void) · line 189
Quoted 3 lines
drivers/firmware/efi/libstub/tpm.c +2
-1-if (status !=EFI_SUCCESS || !log_location) +if (status !=EFI_SUCCESS || !log_location) { +efi_err ("TPM unable to provide Event Log\n" );s/provide/retrieve/ and yes the print is going to be useful. Do you know if the EventLog is mandatory. Reading at the spec GetEventlog only has 2 return values, which implies you can't return "Not supported", but it's not explicitly stated anywhere
Quoted 4 lines
I believe it is mandatory from my reading of the spec - but the "Final Event Log" was only added in 2.0. We report an error when 2.0 is reported but the final event log is not supported, so i figure we should probably report when the event log fails as well.
Yea I am fine with that, I was just wondering if we should do _err or _warn. I am fine with the error
/Ilias
Quoted 11 lines
Quoted 11 lines
Thanks
/IliasQuoted 8 lines
drivers/firmware/efi/libstub/tpm.c +1
-1return ; + }efi_retrieve_tcg2_eventlog (version, log_location, log_last_entry, truncated, final_events_table); --2.43.0
Re: [PATCH 3/6] libstub,tpm: provide indication of failure when getting event log
Quoted 17 lines · Ilias Apalodimas
Quoted 11 lines · Gregory Price
Quoted 10 lines
drivers/firmware/efi/libstub/tpm.c void efi_retrieve_eventlog(void) · line 189
Quoted 4 lines
drivers/firmware/efi/libstub/tpm.c +2
-1-if (status !=EFI_SUCCESS || !log_location) +if (status !=EFI_SUCCESS || !log_location) { +efi_err ("TPM unable to provide Event Log\n" );s/provide/retrieve/ and yes the print is going to be useful. Do you know if the EventLog is mandatory. Reading at the spec GetEventlog only has 2 return values, which implies you can't return "Not supported", but it's not explicitly stated anywhere
Quoted 4 lines
I believe it is mandatory from my reading of the spec - but the "Final Event Log" was only added in 2.0. We report an error when 2.0 is reported but the final event log is not supported, so i figure we should probably report when the event log fails as well.
Quoted 4 lines
Yea I am fine with that, I was just wondering if we should do
_error_warn. I am fine with the error/Ilias
Per Ard's notes on patch 6 i'm going to drop this. These prints apparently don't actually end up anywhere.
~Gregory
PATCH 4/6 tpm: sanity check the log version before using it
In reply to Gregory PriceIf the log version is not sane (0 or >2), don't attempt to use the rest of the log values for anything to avoid potential corruption.
--- drivers/firmware/efi/tpm.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/firmware/efi/tpm.c +9 -0
@@ -60,6 +60,15 @@ int __init efi_tpm_eventlog_init(void)return -ENOMEM ; } +if (!log_tbl->version || + log_tbl->version >EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 ) { +pr_err (FW_BUG "TPM Events table version invalid (%x)\n" , + log_tbl->version); +early_memunmap (log_tbl,sizeof (*log_tbl)); + efi.tpm_log =EFI_INVALID_TABLE_ADDR ; +return -EINVAL ; + } + tbl_size =sizeof (*log_tbl) + log_tbl->size;if (memblock_reserve (efi.tpm_log, tbl_size)) { pr_err("TPM Event Log memblock reserve fails (0x%lx, 0x%x)\n",
Signature · 1 line
2.43.0
Ilias Apalodimas
reviewerRe: [PATCH 4/6] tpm: sanity check the log version before using it
Hi Gregory,
Quoted 14 lines · Gregory Price
If the log version is not sane (0 or >2), don't attempt to use the rest of the log values for anything to avoid potential corruption.
Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/tpm.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/firmware/efi/tpm.c +0
-0
drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 69
Quoted 10 lines
drivers/firmware/efi/tpm.c +7
-0@@ -60,6 +60,15 @@ int __init efi_tpm_eventlog_init(void)return -ENOMEM ; } +if (!log_tbl->version || + log_tbl->version >EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 ) { +pr_err (FW_BUG "TPM Events table version invalid (%x)\n" , + log_tbl->version); +early_memunmap (log_tbl,sizeof (*log_tbl)); + efi.tpm_log =EFI_INVALID_TABLE_ADDR ; +return -EINVAL ;
I don't think we need this check at all. Did you actually see this happening? efi_retrieve_eventlog() that runs during the efistub tries to retrieve the log and the EFI protocol itself explicitly says that the firmware *must* return EFI_INVALID_PARAMETER if the event log is not in 1.2 or 2.0 format. If the firmware does something wrong, we should report the FW BUG in that function, instead of installing the config tables Linux uses internally to handover the log and catching it late.
Thanks
/Ilias
Quoted 8 lines
drivers/firmware/efi/tpm.c +2
-1+ } + tbl_size =sizeof (*log_tbl) + log_tbl->size;if (memblock_reserve (efi.tpm_log, tbl_size)) { pr_err("TPM Event Log memblock reserve fails (0x%lx, 0x%x)\n", --2.43.0
Re: [PATCH 4/6] tpm: sanity check the log version before using it
Quoted 36 lines · Ilias Apalodimas
Hi Gregory,
Quoted 14 lines · Gregory Price
If the log version is not sane (0 or >2), don't attempt to use the rest of the log values for anything to avoid potential corruption.
Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/tpm.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/firmware/efi/tpm.c +0
-0drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 69
Quoted 10 lines
drivers/firmware/efi/tpm.c +7
-0@@ -60,6 +60,15 @@ int __init efi_tpm_eventlog_init(void)return -ENOMEM ; } +if (!log_tbl->version || + log_tbl->version >EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 ) { +pr_err (FW_BUG "TPM Events table version invalid (%x)\n" , + log_tbl->version); +early_memunmap (log_tbl,sizeof (*log_tbl)); + efi.tpm_log =EFI_INVALID_TABLE_ADDR ; +return -EINVAL ;I don't think we need this check at all. Did you actually see this happening?
efi_retrieve_eventlog()that runs during the efistub tries to retrieve the log and the EFI protocol itself explicitly says that the firmware *must* returnEFI_INVALID_PARAMETERif the event log is not in 1.2 or 2.0 format. If the firmware does something wrong, we should report the FW BUG in that function, instead of installing the config tables Linux uses internally to handover the log and catching it late.
Quoted 3 lines
Thanks
/Ilias
We saw this happen and discovered it was a disagreement between EFI/OS/kexec causing the table to be overwritten during kexec. We've since found a fix for that. So the result was that it appeared the firmware was doing something wrong. The sanity check at least allowed us to boot without immediately crashing - because the tables don't get reinstalled, they get re-used (at least that's by best understanding of the whole interaction).
If the check seems superfluous, i can drop it.
Quoted 10 lines
Quoted 10 lines
drivers/firmware/efi/tpm.c +2
-1+ } + tbl_size =sizeof (*log_tbl) + log_tbl->size;if (memblock_reserve (efi.tpm_log, tbl_size)) { pr_err("TPM Event Log memblock reserve fails (0x%lx, 0x%x)\n", --2.43.0
Ilias Apalodimas
reviewerRe: [PATCH 4/6] tpm: sanity check the log version before using it
Quoted 42 lines · Gregory Price
Quoted 36 lines · Ilias Apalodimas
Hi Gregory,
Quoted 14 lines · Gregory Price
If the log version is not sane (0 or >2), don't attempt to use the rest of the log values for anything to avoid potential corruption.
Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/tpm.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/firmware/efi/tpm.c +0
-0drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 69
Quoted 10 lines
drivers/firmware/efi/tpm.c +7
-0@@ -60,6 +60,15 @@ int __init efi_tpm_eventlog_init(void)return -ENOMEM ; } +if (!log_tbl->version || + log_tbl->version >EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 ) { +pr_err (FW_BUG "TPM Events table version invalid (%x)\n" , + log_tbl->version); +early_memunmap (log_tbl,sizeof (*log_tbl)); + efi.tpm_log =EFI_INVALID_TABLE_ADDR ; +return -EINVAL ;I don't think we need this check at all. Did you actually see this happening?
efi_retrieve_eventlog()that runs during the efistub tries to retrieve the log and the EFI protocol itself explicitly says that the firmware *must* returnEFI_INVALID_PARAMETERif the event log is not in 1.2 or 2.0 format. If the firmware does something wrong, we should report the FW BUG in that function, instead of installing the config tables Linux uses internally to handover the log and catching it late.Quoted 3 lines
Thanks
/Ilias
Quoted 8 lines
We saw this happen and discovered it was a disagreement between EFI/OS/kexec causing the table to be overwritten during kexec. We've since found a fix for that. So the result was that it appeared the firmware was doing something wrong. The sanity check at least allowed us to boot without immediately crashing - because the tables don't get reinstalled, they get re-used (at least that's by best understanding of the whole interaction).
If the check seems superfluous, i can drop it.
Ok, that explains why it wasn't caught earlier at least. I would prefer dropping it tbh, but I am going to defer to Ard for that.
If we agree that this needs to go in btw, I think you should refactor it a bit. That function already defines an out: label, which unmaps memory. So you can rewrite the above as
If(....) {
ret = -EINVAL;
efi.tpm_log = EFI_INVALID_TABLE_ADDR; goto out;
}
Cheers
/Ilias
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/firmware/efi/tpm.c +2
-1+ } + tbl_size =sizeof (*log_tbl) + log_tbl->size;if (memblock_reserve (efi.tpm_log, tbl_size)) { pr_err("TPM Event Log memblock reserve fails (0x%lx, 0x%x)\n", --2.43.0
Ard Biesheuvel
maintainerRe: [PATCH 4/6] tpm: sanity check the log version before using it
Quoted 60 lines · Ilias Apalodimas
Quoted 42 lines · Gregory Price
Quoted 36 lines · Ilias Apalodimas
Hi Gregory,
Quoted 14 lines · Gregory Price
If the log version is not sane (0 or >2), don't attempt to use the rest of the log values for anything to avoid potential corruption.
Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/tpm.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/firmware/efi/tpm.c +0
-0drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 69
Quoted 10 lines
drivers/firmware/efi/tpm.c +7
-0@@ -60,6 +60,15 @@ int __init efi_tpm_eventlog_init(void)return -ENOMEM ; } +if (!log_tbl->version || + log_tbl->version >EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 ) { +pr_err (FW_BUG "TPM Events table version invalid (%x)\n" , + log_tbl->version); +early_memunmap (log_tbl,sizeof (*log_tbl)); + efi.tpm_log =EFI_INVALID_TABLE_ADDR ; +return -EINVAL ;I don't think we need this check at all. Did you actually see this happening?
efi_retrieve_eventlog()that runs during the efistub tries to retrieve the log and the EFI protocol itself explicitly says that the firmware *must* returnEFI_INVALID_PARAMETERif the event log is not in 1.2 or 2.0 format. If the firmware does something wrong, we should report the FW BUG in that function, instead of installing the config tables Linux uses internally to handover the log and catching it late.Quoted 3 lines
Thanks
/IliasQuoted 8 lines
We saw this happen and discovered it was a disagreement between EFI/OS/kexec causing the table to be overwritten during kexec. We've since found a fix for that. So the result was that it appeared the firmware was doing something wrong. The sanity check at least allowed us to boot without immediately crashing - because the tables don't get reinstalled, they get re-used (at least that's by best understanding of the whole interaction).
If the check seems superfluous, i can drop it.
Ok, that explains why it wasn't caught earlier at least. I would prefer dropping it tbh, but I am going to defer to Ard for that.
If we agree that this needs to go in btw, I think you should refactor it a bit. That function already defines an out: label, which unmaps memory. So you can rewrite the above as
Quoted 6 lines
If(....) { ret = -EINVAL; efi.tpm_log = EFI_INVALID_TABLE_ADDR;goto out;
}
Validating a table that was created by the EFI stub seems redundant. If the version check needs to be tightened, please do so in efi_retrieve_tcg2_eventlog() (in the stub).
Ard Biesheuvel
maintainerRe: [PATCH 4/6] tpm: sanity check the log version before using it
Quoted 70 lines · Ard Biesheuvel
Quoted 60 lines · Ilias Apalodimas
Quoted 42 lines · Gregory Price
Quoted 36 lines · Ilias Apalodimas
Hi Gregory,
Quoted 14 lines · Gregory Price
If the log version is not sane (0 or >2), don't attempt to use the rest of the log values for anything to avoid potential corruption.
Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/tpm.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/firmware/efi/tpm.c +0
-0drivers/firmware/efi/tpm.c int __init efi_tpm_eventlog_init(void) · line 69
Quoted 10 lines
drivers/firmware/efi/tpm.c +7
-0@@ -60,6 +60,15 @@ int __init efi_tpm_eventlog_init(void)return -ENOMEM ; } +if (!log_tbl->version || + log_tbl->version >EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 ) { +pr_err (FW_BUG "TPM Events table version invalid (%x)\n" , + log_tbl->version); +early_memunmap (log_tbl,sizeof (*log_tbl)); + efi.tpm_log =EFI_INVALID_TABLE_ADDR ; +return -EINVAL ;I don't think we need this check at all. Did you actually see this happening?
efi_retrieve_eventlog()that runs during the efistub tries to retrieve the log and the EFI protocol itself explicitly says that the firmware *must* returnEFI_INVALID_PARAMETERif the event log is not in 1.2 or 2.0 format. If the firmware does something wrong, we should report the FW BUG in that function, instead of installing the config tables Linux uses internally to handover the log and catching it late.Quoted 3 lines
Thanks
/IliasQuoted 8 lines
We saw this happen and discovered it was a disagreement between EFI/OS/kexec causing the table to be overwritten during kexec. We've since found a fix for that. So the result was that it appeared the firmware was doing something wrong. The sanity check at least allowed us to boot without immediately crashing - because the tables don't get reinstalled, they get re-used (at least that's by best understanding of the whole interaction).
If the check seems superfluous, i can drop it.
Ok, that explains why it wasn't caught earlier at least. I would prefer dropping it tbh, but I am going to defer to Ard for that.
If we agree that this needs to go in btw, I think you should refactor it a bit. That function already defines an out: label, which unmaps memory. So you can rewrite the above as
Quoted 6 lines
If(....) { ret = -EINVAL; efi.tpm_log = EFI_INVALID_TABLE_ADDR;goto out;
}
Quoted 3 lines
Validating a table that was created by the EFI stub seems redundant. If the version check needs to be tightened, please do so in
efi_retrieve_tcg2_eventlog()(in the stub).
... and actually, this version is set by the EFI stub based on which flavor of the TCG protocols it found.
So i don't think we need this check to begin with.
If we need to detect corruption of these tables, I'd prefer to add a checksum or something like that. But I don't think we should bother.
Re: [PATCH 4/6] tpm: sanity check the log version before using it
Quoted 15 lines · Ard Biesheuvel
Quoted 11 lines
Quoted 10 lines
If we agree that this needs to go in btw, I think you should refactor it a bit. That function already defines an out: label, which unmaps memory. So you can rewrite the above as
If(....) { ret = -EINVAL; efi.tpm_log = EFI_INVALID_TABLE_ADDR;goto out;
}Quoted 3 lines
Validating a table that was created by the EFI stub seems redundant. If the version check needs to be tightened, please do so in
efi_retrieve_tcg2_eventlog()(in the stub).
Quoted 7 lines
... and actually, this version is set by the EFI stub based on which flavor of the TCG protocols it found.
So i don't think we need this check to begin with.
If we need to detect corruption of these tables, I'd prefer to add a checksum or something like that. But I don't think we should bother.
Will drop, east enough. Will send v2 later today.
~Gregory
PATCH 5/6 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 6/6 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, report the error, and break early because all subsequent calls will also fail.
--- drivers/firmware/efi/libstub/tpm.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/firmware/efi/libstub/tpm.c +4 -0
@@ -124,6 +124,10 @@ 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 (!event_size) { +efi_err ("Invalid TPM Final Event Log Entry\n" ); +break ; + } final_events_size += event_size; i--; }
Signature · 1 line
2.43.0
Ard Biesheuvel
maintainerRe: [PATCH 6/6] libstub,tpm: do not ignore failure case when reading final event log
Quoted 15 lines · Gregory Price
Current code fails to check for an error case when reading events from final event log to calculate offsets. Check the error case, report the error, and break early because all subsequent calls will also fail.
Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/libstub/tpm.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/firmware/efi/libstub/tpm.c +0
-0
drivers/firmware/efi/libstub/tpm.c static void efi_retrieve_tcg2_eventlog(int version, efi_physical_addr_t log_loca · line 130
Quoted 7 lines
drivers/firmware/efi/libstub/tpm.c +4
-0@@ -124,6 +124,10 @@ 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 (!event_size) { +efi_err ("Invalid TPM Final Event Log Entry\n" ); +break ; + }
I don't object to this in principle, the only problem is that these log prints are not recorded anywhere: they are printed to the EFI boot console by the EFI stub, which may not even be visible, and is definitely not captured by the kernel logging routines.
Re: [PATCH 6/6] libstub,tpm: do not ignore failure case when reading final event log
Quoted 20 lines · Ard Biesheuvel
Quoted 19 lines · Gregory Price
Current code fails to check for an error case when reading events from final event log to calculate offsets. Check the error case, report the error, and break early because all subsequent calls will also fail.
Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/libstub/tpm.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/firmware/efi/libstub/tpm.c +1
-0@@ -124,6 +124,10 @@ 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 (!event_size) {
Quoted 8 lines
drivers/firmware/efi/libstub/tpm.c static void efi_retrieve_tcg2_eventlog(int version, efi_physical_addr_t log_loca · line 130
Quoted 3 lines
drivers/firmware/efi/libstub/tpm.c +3
-0+efi_err ("Invalid TPM Final Event Log Entry\n" ); +break ; + }I don't object to this in principle, the only problem is that these log prints are not recorded anywhere: they are printed to the EFI boot console by the EFI stub, which may not even be visible, and is definitely not captured by the kernel logging routines.
Could simply drop the err and break if you think it's just going to get lost anyway. Not sure there's a good way to generate a signal at this point.
~Gregory
Ard Biesheuvel
maintainerRe: [PATCH 6/6] libstub,tpm: do not ignore failure case when reading final event log
Quoted 31 lines · Gregory Price
Quoted 20 lines · Ard Biesheuvel
Quoted 19 lines · Gregory Price
Current code fails to check for an error case when reading events from final event log to calculate offsets. Check the error case, report the error, and break early because all subsequent calls will also fail.
Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/libstub/tpm.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/firmware/efi/libstub/tpm.c +1
-0@@ -124,6 +124,10 @@ 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 (!event_size) {Quoted 8 lines
drivers/firmware/efi/libstub/tpm.c static void efi_retrieve_tcg2_eventlog(int version, efi_physical_addr_t log_loca · line 130
Quoted 3 lines
drivers/firmware/efi/libstub/tpm.c +3
-0+efi_err ("Invalid TPM Final Event Log Entry\n" ); +break ; + }I don't object to this in principle, the only problem is that these log prints are not recorded anywhere: they are printed to the EFI boot console by the EFI stub, which may not even be visible, and is definitely not captured by the kernel logging routines.
Quoted 3 lines
Could simply drop the err and break if you think it's just going to get lost anyway. Not sure there's a good way to generate a signal at this point.
Yeah. For the record, I absolutely detest the kludgy code there, how we parse the map, parse and unmap the event header for every entry in the log.
So while I highly appreciate the effort you are putting in to polish this code, I wonder if it wouldn't be better to code this up properly instead.
Re: [PATCH 6/6] libstub,tpm: do not ignore failure case when reading final event log
Quoted 36 lines · Ard Biesheuvel
Quoted 31 lines · Gregory Price
Quoted 20 lines · Ard Biesheuvel
Quoted 19 lines · Gregory Price
Current code fails to check for an error case when reading events from final event log to calculate offsets. Check the error case, report the error, and break early because all subsequent calls will also fail.
Signed-off-by Gregory Price <[email protected]>--- drivers/firmware/efi/libstub/tpm.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/firmware/efi/libstub/tpm.c +1
-0@@ -124,6 +124,10 @@ 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 (!event_size) {Quoted 8 lines
drivers/firmware/efi/libstub/tpm.c static void efi_retrieve_tcg2_eventlog(int version, efi_physical_addr_t log_loca · line 130
Quoted 3 lines
drivers/firmware/efi/libstub/tpm.c +3
-0+efi_err ("Invalid TPM Final Event Log Entry\n" ); +break ; + }I don't object to this in principle, the only problem is that these log prints are not recorded anywhere: they are printed to the EFI boot console by the EFI stub, which may not even be visible, and is definitely not captured by the kernel logging routines.
Quoted 3 lines
Could simply drop the err and break if you think it's just going to get lost anyway. Not sure there's a good way to generate a signal at this point.
Quoted 7 lines
Yeah. For the record, I absolutely detest the kludgy code there, how we parse the map, parse and unmap the event header for every entry in the log.
So while I highly appreciate the effort you are putting in to polish this code, I wonder if it wouldn't be better to code this up properly instead.
Mostly I was both amused and triggered by a patch trying to fix a signed/unsigned bug introducing another signed/unsigned bug lol.
Then I found more and felt this injustice cannot stand x_x
I'll at least push another version of these fixed up and i'll just drop this print. Probably improving this code isn't worth it unless it's fundamentally broken (which it does not *appear* to be, i spent a day auditing it against the spec, and to my eye it looks largely ok).
~Gregory