tpm: fix handling of PCR banks unsupported by U-Boot
v3 cover tpm: fix handling of PCR banks unsupported by U-Boot
A TPM implementing more PCR banks than U-Boot knows algorithms for is currently rejected by tpm2_get_pcr_info(), even if the unknown banks are inactive. Ours implements six banks, including SHA3-384:
tpm2_get_pcr_info: too many pcrs: 6
Once this limit is lifted (patch 2), the existing code for deactivating unsupported banks with CONFIG_TPM_PCR_ALLOCATE turns out not to work:
- the result of TPM2_PCR_Allocate (allocationSuccess) is ignored, so a refused allocation goes unnoticed (patch 1)
- active banks with algorithms not listed in hash_algo_list at all are never detected (patch 3)
- the fallback in tpm2_pcr_extend() deactivates *all* banks and does not reset the board (patch 4)
Patch 5 deactivates the unsupported banks in tpm2_startup() already, if the TPM is actually started there, so that this happens before anything gets measured, also for users which do not go through tcg2_measurement_init() or the EFI TCG2 protocol. Note that with CONFIG_TPM_PCR_ALLOCATE enabled, this lets tpm2_startup() fail if the unsupported banks cannot be deactivated. The rationale is that enabling the option asks U-Boot to manage the banks, so failing early seems preferable to failing at the first PCR extend.
Tested on a downstream U-Boot based on v2025.04 with a TPM implementing six PCR banks, with the unsupported banks both inactive and active. Build- and unit-tested (ut dm tpm, ut lib) on sandbox against main.
Changes in v2:
- 5/5: deactivate the unsupported banks in tpm2_startup() instead of tpm2_auto_start(), and only if TPM2_Startup(TPM2_SU_CLEAR) succeeded, i.e. not if an earlier boot stage already started the TPM, and not for TPM2_SU_STATE (Ilias)
- rebased onto current main
Changes in v3:
- Add Reviewed-by tags from Ilias to patches 1-4
v1: https://lore.kernel.org/u-boot/[email protected]/
v2: https://lore.kernel.org/u-boot/[email protected]/
Tycho Kirchner (5):
tpm: check allocationSuccess of TPM2_PCR_Allocate
tpm: allow TPMs with more banks than U-Boot knows algorithms for
tpm: detect active banks with algorithms unknown to U-Boot
tpm: deactivate only unsupported banks before PCR extend
tpm: deactivate unsupported banks in tpm2_startup()
lib/tpm-v2.c | 76 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 11 deletions(-)
Signature · 1 line
2.47.3
PATCH v3 1/5 tpm: check allocationSuccess of TPM2_PCR_Allocate
TPM2_PCR_Allocate may return TPM_RC_SUCCESS without performing the new allocation, e.g. if the TPM lacks the memory for it. This is only reported in the allocationSuccess field of the response, see the TPM 2.0 Library Specification, Part 3: Commands, TPM2_PCR_Allocate. U-Boot ignores this field, so a refused allocation goes unnoticed and the board reboots into the unchanged allocation over and over again.
Check allocationSuccess and return an error if the TPM refused the allocation.
--- lib/tpm-v2.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-)
lib/tpm-v2.c +19 -3
@@ -596,6 +596,7 @@ u32 tpm2_send_pcr_allocate(struct udevice *dev, const char *pw, };u8 response[COMMAND_BUFFER_SIZE ];size_t response_len =COMMAND_BUFFER_SIZE ; +u8 success;u32 i;int ret;
@@ -642,10 +643,25 @@ u32 tpm2_send_pcr_allocate(struct udevice *dev, const char *pw, } ret =tpm_sendrecv_command (dev, command_v2, response, &response_len); -if (!ret) -tpm_init (dev); +if (ret) +return ret; + +/* +* The TPM may refuse the new allocation without returning an error +* code, e.g. if it lacks the memory for it. Check allocationSuccess, +* located after the response header and the parameterSize field. +*/ +if (unpack_byte_string (response, response_len,"b" , +TPM2_HDR_LEN +sizeof (u32), &success)) +return TPM_LIB_ERROR ; +if (success !=TPMI_YES ) { +log_err ("TPM refused the PCR allocation\n" ); +return TPM_LIB_ERROR ; + } -return ret; +tpm_init (dev); + +return 0 ; }static int tpm2_get_num_pcr (struct udevice *dev,u32 *num_pcr)
Signature · 1 line
2.47.3
PATCH v3 2/5 tpm: allow TPMs with more banks than U-Boot knows algorithms for
In reply to Tycho Kirchnertpm2_get_pcr_info() rejects a TPM that implements more PCR banks than U-Boot has entries in hash_algo_list. A TPM with six banks, for instance, fails with
tpm2_get_pcr_info: too many pcrs: 6
Error: -90
even if all banks with an algorithm unknown to U-Boot are inactive.
Each bank corresponds to one hash algorithm, but only the *active* banks need to be supported for measured boot. This is already checked
elsewhere: tpm2_check_active_banks() and tpm2_scan_masks() detect active unsupported banks and, with CONFIG_TPM_PCR_ALLOCATE, deactivate them. These checks are never reached, however, because tpm2_get_pcr_info() fails beforehand.
Only limit the number of banks to the size of the selection array (TPM2_NUM_PCR_BANKS), which is what the parsing code requires.
--- lib/tpm-v2.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
lib/tpm-v2.c +5 -3
@@ -702,10 +702,12 @@ int tpm2_get_pcr_info(struct udevice *dev, struct tpml_pcr_selection *pcrs) pcrs->count =get_unaligned_be32 (response);/* -* check against the supported algorithms in hash_algo_list, -* instead of TPM2_NUM_PCR_BANKS +* The TPM may implement banks with algorithms unknown to U-Boot. +* Only the active ones must be supported, which is checked by +* tpm2_check_active_banks() and tpm2_scan_masks(), so accept up to +* TPM2_NUM_PCR_BANKS here. */ -if (pcrs->count >ARRAY_SIZE (hash_algo_list) || pcrs->count <1 ) { +if (pcrs->count >ARRAY_SIZE (pcrs->selection) || pcrs->count <1 ) {printf ("%s: too many pcrs: %u\n" , __func__, pcrs->count);return -EMSGSIZE ; }
Signature · 1 line
2.47.3
PATCH v3 3/5 tpm: detect active banks with algorithms unknown to U-Boot
In reply to Tycho Kirchnertpm2_scan_masks() detects active banks with unsupported algorithms by comparing bitmasks taken from hash_algo_list. Algorithms which are not listed there at all, e.g. SHA3, have no bitmask, so active banks using them are never detected and never deactivated. Measured boot then fails later on when the digest for such a bank cannot be created:
tcg2_create_digest: unsupported algorithm 28
Track active unsupported banks with a separate flag instead. Also return an error if no supported bank would remain active: nothing could be measured with such an allocation, and TPMs may refuse it anyway, as observed with our model.
--- lib/tpm-v2.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
lib/tpm-v2.c +19 -2
@@ -66,6 +66,7 @@ int tpm2_scan_masks(struct udevice *dev, u32 log_active, u32 *mask)struct tpml_pcr_selection pcrs;u32 active =0 ;u32 supported =0 ; +bool unsupported_active = false;int rc, i; *mask =0 ;
@@ -87,8 +88,19 @@ int tpm2_scan_masks(struct udevice *dev, u32 log_active, u32 *mask)if (tpm2_algorithm_supported (sel->hash)) supported |= hash_mask; -if (tpm2_is_active_bank (sel)) +if (tpm2_is_active_bank (sel)) { active |= hash_mask; +/* +* Algorithms unknown to U-Boot have no hash_mask, so +* track unsupported active banks separately +*/ +if (!tpm2_algorithm_supported (sel->hash)) { + unsupported_active = true; +if (!hash_mask) +log_warning ("TPM active algorithm 0x%x unknown to U-Boot\n" , + sel->hash); + } + } }/* All eventlog algorithm(s) must be supported */
@@ -104,13 +116,18 @@ int tpm2_scan_masks(struct udevice *dev, u32 log_active, u32 *mask) }/* Any active algorithm(s) which are not supported must be removed */ -if (active & ~supported) { +if (unsupported_active) {log_warning ("TPM active algorithm(s) unsupported by u-boot\n" );tpm2_print_selected_algorithm_name (active & ~supported);if (*mask) *mask = active & supported & *mask;else *mask = active & supported; + +if (!*mask) { +log_err ("No supported TPM algorithm would remain active\n" ); + rc = -EOPNOTSUPP ; + } }return rc;
Signature · 1 line
2.47.3
PATCH v3 4/5 tpm: deactivate only unsupported banks before PCR extend
In reply to Tycho KirchnerCommit e7e166dabf12 ("tpm: PCR allocate during PCR extend to disable the unsupported algorithms") intends to deactivate active banks with unsupported algorithms when extending a PCR. However, it calls tpm2_pcr_allocate() with an algorithm mask of 0, which deactivates *all* banks, including the supported ones:
set bank[0] sha1 off
set bank[1] sha256 off
...
If the TPM refuses this allocation, nothing changes, and the same happens again on every boot. If the TPM accepts it, no bank is active after the next TPM2_Startup, so nothing can be measured on any later boot either. Moreover, the allocation is changed regardless of CONFIG_TPM_PCR_ALLOCATE.
Instead, if CONFIG_TPM_PCR_ALLOCATE is enabled, call tpm2_activate_banks(), which keeps the supported active banks, deactivates the others and resets the board. Otherwise, leave the allocation untouched. In both cases, the extend fails while unsupported banks are active.
--- lib/tpm-v2.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
lib/tpm-v2.c +9 -3
@@ -362,9 +362,15 @@ u32 tpm2_pcr_extend(struct udevice *dev, u32 index, u32 algorithm,if (!tpm2_check_active_banks (dev)) {log_err ("Cannot extend PCRs if all the TPM enabled algorithms are not supported\n" ); - ret =tpm2_pcr_allocate (dev,0 ); -if (ret) -return -EINVAL ; +/* +* Deactivate the unsupported banks. On success this resets +* the board, since the new allocation only takes effect after +* the next TPM2_Startup. +*/ +if (IS_ENABLED (CONFIG_TPM_PCR_ALLOCATE )) +tpm2_activate_banks (dev,0 ); + +return -EINVAL ; }/* * Fill the command structure starting from the first buffer:
Signature · 1 line
2.47.3
PATCH v3 5/5 tpm: deactivate unsupported banks in tpm2_startup()
In reply to Tycho KirchnerWith CONFIG_TPM_PCR_ALLOCATE, active banks with unsupported algorithms are only deactivated by tcg2_log_prepare_buffer() or, as a fallback, when a PCR is extended. Users of the TPM which do not go through tcg2_measurement_init() or the EFI TCG2 protocol therefore only notice the unsupported banks when measuring, e.g.
tcg2_create_digest: unsupported algorithm 28
Cannot extend PCRs if all the TPM enabled algorithms are not supported
Deactivate them in tpm2_startup() already, before anything gets measured, but only if the TPM was actually started there, i.e. TPM2_Startup(TPM2_SU_CLEAR) succeeded. This also covers tpm2_auto_start(), which calls tpm2_startup() if the TPM was not started yet.
If TPM2_Startup returns TPM2_RC_INITIALIZE, an earlier boot stage (e.g. SPL or TF-A) has already started the TPM and is expected to take care of the banks. With an event log, tcg2_log_prepare_buffer() still aligns them afterwards. The fallback in tpm2_pcr_extend() remains for the case that the earlier stage supports more algorithms than U-Boot. TPM2_SU_STATE, used by tpm_resume(), is left alone.
If the deactivation fails, e.g. because the TPM refuses the allocation or no supported bank would remain active, return an error. With CONFIG_TPM_PCR_ALLOCATE enabled, U-Boot is expected to manage the banks, so fail early instead of at the first PCR extend.
--- lib/tpm-v2.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
lib/tpm-v2.c +13 -0
@@ -201,6 +201,19 @@ u32 tpm2_startup(struct udevice *dev, bool bon, enum tpm2_startup_types mode)if ((ret && ret !=TPM2_RC_INITIALIZE ) || !bon)return ret; +/* +* Only the boot stage which actually starts the TPM deactivates active +* banks with unsupported algorithms. On success, this resets the board. +*/ +if (IS_ENABLED (CONFIG_TPM_PCR_ALLOCATE ) && !ret && + mode ==TPM2_SU_CLEAR ) { + ret =tpm2_activate_banks (dev,0 ); +if (ret) { +log_err ("TPM: failed to deactivate unsupported banks\n" ); +return ret; + } + } +return tpm2_update_active_banks (dev); }
Signature · 1 line
2.47.3
Re: [PATCH v3 5/5] tpm: deactivate unsupported banks in tpm2_startup()
Quoted 16 lines · Tycho Kirchner
With
CONFIG_TPM_PCR_ALLOCATE, active banks with unsupported algorithms are only deactivated bytcg2_log_prepare_buffer()or, as a fallback, when a PCR is extended. Users of the TPM which do not go throughtcg2_measurement_init()or the EFI TCG2 protocol therefore only notice the unsupported banks when measuring, e.g.
tcg2_create_digest: unsupported algorithm 28
Cannot extend PCRs if all the TPM enabled algorithms are not supportedDeactivate them in
tpm2_startup()already, before anything gets measured, but only if the TPM was actually started there, i.e.TPM2_Startup(TPM2_SU_CLEAR)succeeded. This also coverstpm2_auto_start(), which callstpm2_startup()if the TPM was not started yet.
Quoted 5 lines
If
TPM2_StartupreturnsTPM2_RC_INITIALIZE, an earlier boot stage (e.g. SPL or TF-A) has already started the TPM and is expected to take care of the banks. With an event log,tcg2_log_prepare_buffer()still aligns them afterwards. The fallback intpm2_pcr_extend()remains for the case that the earlier stage supports more algorithms than U-Boot.
I don't mind keeping it, but if the previous boot stage supports more algorithms than U-Boot, this is actively blocked in the TCG code, IIRC.
Quoted 39 lines
TPM2_SU_STATE, used bytpm_resume(), is left alone.If the deactivation fails, e.g. because the TPM refuses the allocation or no supported bank would remain active, return an error. With
CONFIG_TPM_PCR_ALLOCATEenabled, U-Boot is expected to manage the banks, so fail early instead of at the first PCR extend.Signed-off-by Tycho Kirchner <[email protected]>--- lib/tpm-v2.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)lib/tpm-v2.c +13
-1@@ -201,6 +201,19 @@ u32 tpm2_startup(struct udevice *dev, bool bon, enum tpm2_startup_types mode)if ((ret && ret !=TPM2_RC_INITIALIZE ) || !bon)return ret; +/* +* Only the boot stage which actually starts the TPM deactivates active +* banks with unsupported algorithms. On success, this resets the board. +*/ +if (IS_ENABLED (CONFIG_TPM_PCR_ALLOCATE ) && !ret && + mode ==TPM2_SU_CLEAR ) { + ret =tpm2_activate_banks (dev,0 ); +if (ret) { +log_err ("TPM: failed to deactivate unsupported banks\n" ); +return ret; + } + } +return tpm2_update_active_banks (dev); } --2.47.3
Re: [PATCH v3 5/5] tpm: deactivate unsupported banks in tpm2_startup()
Hi Ilias,
Am 06.11.26 um 09:53 schrieb Ilias Apalodimas:
Quoted 3 lines
I don't mind keeping it, but if the previous boot stage supports more algorithms than U-Boot, this is actively blocked in the TCG code, IIRC.
Right, with an event log tcg2_log_parse() rejects algorithms unknown to U-Boot, so the fallback is not reached there. It is meant for users which do not go through tcg2_log_prepare_buffer() at all, e.g. a custom measurement implementation on top of lib/tpm-v2.c (my case) or, more importantly "tpm2 pcr_extend", if an earlier stage already started the TPM.
Thanks for the review and testing!
Tycho
Quoted 41 lines
Reviewed-by Ilias Apalodimas <[email protected]>Tested-by Ilias Apalodimas <[email protected]>Quoted 39 lines
TPM2_SU_STATE, used bytpm_resume(), is left alone.If the deactivation fails, e.g. because the TPM refuses the allocation or no supported bank would remain active, return an error. With
CONFIG_TPM_PCR_ALLOCATEenabled, U-Boot is expected to manage the banks, so fail early instead of at the first PCR extend.Signed-off-by Tycho Kirchner <[email protected]>--- lib/tpm-v2.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)lib/tpm-v2.c +13
-1@@ -201,6 +201,19 @@ u32 tpm2_startup(struct udevice *dev, bool bon, enum tpm2_startup_types mode)if ((ret && ret !=TPM2_RC_INITIALIZE ) || !bon)return ret; +/* +* Only the boot stage which actually starts the TPM deactivates active +* banks with unsupported algorithms. On success, this resets the board. +*/ +if (IS_ENABLED (CONFIG_TPM_PCR_ALLOCATE ) && !ret && + mode ==TPM2_SU_CLEAR ) { + ret =tpm2_activate_banks (dev,0 ); +if (ret) { +log_err ("TPM: failed to deactivate unsupported banks\n" ); +return ret; + } + } +return tpm2_update_active_banks (dev); } --2.47.3
Signature · 14 lines
M.Sc. Tycho Kirchner emlix GmbH Headquarters: Berliner Str. 12, 37073 Göttingen, Germany Phone +49 (0)551 30664-0, e-mail [email protected] District Court of Göttingen, Registry Number HR B 3160 Managing Directors: Heike Jordan, Dr. Uwe Kracke VAT ID No. DE 205 198 055 Office Berlin: Panoramastr. 1, 10178 Berlin, Germany Office Bonn: Bachstr. 6, 53115 Bonn, Germany Office München: Am Knie 16, 81241 München, Germany http://www.emlix.com emlix - your embedded Linux partner
Re: [PATCH v3 5/5] tpm: deactivate unsupported banks in tpm2_startup()
Quoted 9 lines · Tycho Kirchner
Hi Ilias,
Am 06.11.26 um 09:53 schrieb Ilias Apalodimas:
Quoted 3 lines
I don't mind keeping it, but if the previous boot stage supports more algorithms than U-Boot, this is actively blocked in the TCG code, IIRC.
Quoted 6 lines
Right, with an event log
tcg2_log_parse()rejects algorithms unknown to U-Boot, so the fallback is not reached there. It is meant for users which do not go throughtcg2_log_prepare_buffer()at all, e.g. a custom measurement implementation on top oflib/tpm-v2.c(my case) or, more importantly "tpm2pcr_extend", if an earlier stage already started the TPM.
Ah yes, fair point. I keep forgetting that boards might start the TPM earlier properly.
Ok we need to keep this check indeed.
Cheers
/Ilias
Quoted 64 lines
Thanks for the review and testing!
Tycho
Quoted 38 lines
Reviewed-by Ilias Apalodimas <[email protected]>Tested-by Ilias Apalodimas <[email protected]>Quoted 36 lines
TPM2_SU_STATE, used bytpm_resume(), is left alone.If the deactivation fails, e.g. because the TPM refuses the allocation or no supported bank would remain active, return an error. With
CONFIG_TPM_PCR_ALLOCATEenabled, U-Boot is expected to manage the banks, so fail early instead of at the first PCR extend.Signed-off-by Tycho Kirchner <[email protected]>--- lib/tpm-v2.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)lib/tpm-v2.c +13
-0@@ -201,6 +201,19 @@ u32 tpm2_startup(struct udevice *dev, bool bon, enum tpm2_startup_types mode)if ((ret && ret !=TPM2_RC_INITIALIZE ) || !bon)return ret; +/* +* Only the boot stage which actually starts the TPM deactivates active +* banks with unsupported algorithms. On success, this resets the board. +*/ +if (IS_ENABLED (CONFIG_TPM_PCR_ALLOCATE ) && !ret && + mode ==TPM2_SU_CLEAR ) { + ret =tpm2_activate_banks (dev,0 ); +if (ret) { +log_err ("TPM: failed to deactivate unsupported banks\n" ); +return ret; + } + } +return tpm2_update_active_banks (dev); }Quoted 3 lines
Quoted 3 lines
--
2.47.3--
M.Sc. Tycho Kirchneremlix GmbH
Headquarters: Berliner Str. 12, 37073 Göttingen, Germany Phone +49 (0)551 30664-0, e-mail [email protected] District Court of Göttingen, Registry Number HR B 3160 Managing Directors: Heike Jordan, Dr. Uwe Kracke VAT ID No. DE 205 198 055
Office Berlin: Panoramastr. 1, 10178 Berlin, Germany Office Bonn: Bachstr. 6, 53115 Bonn, Germany Office München: Am Knie 16, 81241 München, Germany
http://www.emlix.comemlix - your embedded Linux partner