Skip to contentSkip to conversation

tpm: fix handling of PCR banks unsupported by U-Boot

PATCH Superseded Tycho Kirchner 2 participants 15 messages Oct 29 – Nov 4 Library code

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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_auto_start() already, so that this happens before anything gets measured, also for users which do not go through tcg2_measurement_init().

Note that with CONFIG_TPM_PCR_ALLOCATE enabled, patch 5 makes tpm2_auto_start() 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.

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_auto_start()

 lib/tpm-v2.c | 75 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 64 insertions(+), 11 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 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.

Fixes 9d2bc92ba7bb ("tpm: add TPM2_PCR_Allocate command")
---
 lib/tpm-v2.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
lib/tpm-v2.c +19 -3
index 4c7dfc55650..137bbbd9593 100644
@@ -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
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] tpm: check allocationSuccess of TPM2_PCR_Allocate

Thanks Tycho

Quoted 8 lines · Tycho Kirchner

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.

Quoted 6 lines

Check allocationSuccess and return an error if the TPM refused the allocation.

Fixes 9d2bc92ba7bb ("tpm: add TPM2_PCR_Allocate command")

---

Quoted 47 lines
 lib/tpm-v2.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
lib/tpm-v2.c +19 -4
index 4c7dfc55650..137bbbd9593 100644
@@ -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)
--

2.47.3

#3 lore raw

PATCH 2/5 tpm: allow TPMs with more banks than U-Boot knows algorithms for

In reply to Tycho Kirchner

tpm2_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.

Fixes 97707f12fdab ("tpm: Support boot measurements")
---
 lib/tpm-v2.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
lib/tpm-v2.c +5 -3
index 137bbbd9593..43ca54621d8 100644
@@ -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
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] tpm: allow TPMs with more banks than U-Boot knows algorithms for

Quoted 8 lines · Tycho Kirchner

tpm2_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

Quoted 8 lines

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.

Yep that's a fossil from before dynamic PCR configuration.

Quoted 3 lines

Only limit the number of banks to the size of the selection array (TPM2_NUM_PCR_BANKS), which is what the parsing code requires.

I think the fixes tag can be stripped, and I can do that during the merge. My only concern is that this still won't work until the later patches are applied. But since it didn't work to begin with, I prefer the smaller patches

Quoted 30 lines
Fixes 97707f12fdab ("tpm: Support boot measurements")
---
 lib/tpm-v2.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
lib/tpm-v2.c +5 -4
index 137bbbd9593..43ca54621d8 100644
@@ -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;
        }
--

2.47.3

#5 lore raw

PATCH 3/5 tpm: detect active banks with algorithms unknown to U-Boot

In reply to Tycho Kirchner

tpm2_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.

Fixes b6228b2e785d ("tpm: add wrapper and helper APIs for PCR allocate")
---
 lib/tpm-v2.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
lib/tpm-v2.c +19 -2
index 43ca54621d8..b727c645d73 100644
@@ -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
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] tpm: detect active banks with algorithms unknown to U-Boot

Quoted 9 lines · Tycho Kirchner

tpm2_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

Quoted 8 lines

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.

Fixes b6228b2e785d ("tpm: add wrapper and helper APIs for PCR allocate")

---

Quoted 59 lines
 lib/tpm-v2.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
lib/tpm-v2.c +19 -3
index 43ca54621d8..b727c645d73 100644
@@ -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;
--

2.47.3

#7 lore raw

PATCH 4/5 tpm: deactivate only unsupported banks before PCR extend

In reply to Tycho Kirchner

Commit 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.

Fixes e7e166dabf12 ("tpm: PCR allocate during PCR extend to disable the unsupported algorithms")
---
 lib/tpm-v2.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
lib/tpm-v2.c +9 -3
index b727c645d73..18ec9eb960d 100644
@@ -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
Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH 4/5] tpm: deactivate only unsupported banks before PCR extend

Quoted 17 lines · Tycho Kirchner

Commit 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.

Quoted 5 lines

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

This makes sense. Raymond do you remember why tpm2_pcr_allocate() was called?

Quoted 33 lines
Fixes e7e166dabf12 ("tpm: PCR allocate during PCR extend to disable the unsupported algorithms")
---
 lib/tpm-v2.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
lib/tpm-v2.c +9 -4
index b727c645d73..18ec9eb960d 100644
@@ -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:
--

2.47.3

#9 lore raw

PATCH 5/5 tpm: deactivate unsupported banks in tpm2_auto_start()

In reply to Tycho Kirchner

With 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() 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_auto_start() already, before anything gets measured. If this fails, e.g. because the TPM refuses the allocation or no supported bank would remain active, return an error. The rationale is that with CONFIG_TPM_PCR_ALLOCATE enabled, U-Boot is expected to manage the banks, so fail early instead of at the first PCR extend. This is in line with the TPM_PCR_ALLOCATE help text, which does not specify at which point during boot the allocation takes place.

---
 lib/tpm-v2.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
lib/tpm-v2.c +12 -0
index 18ec9eb960d..bd049fed748 100644
@@ -232,6 +232,18 @@ u32 tpm2_auto_start(struct udevice *dev)
 	if (rc)
 		return rc;
 
+	/*
+	 * Early deactivate active banks with unsupported algorithms. On success,
+	 * this resets the board.
+	 */
+	if (IS_ENABLED(CONFIG_TPM_PCR_ALLOCATE)) {
+		rc = tpm2_activate_banks(dev, 0);
+		if (rc) {
+			log_err("TPM: failed to deactivate unsupported banks\n");
+			return rc;
+		}
+	}
+
 	return tpm2_update_active_banks(dev);
 }
 
Signature · 1 line
2.47.3
#10 lore raw

Re: [PATCH 5/5] tpm: deactivate unsupported banks in tpm2_auto_start()

Hi Tycho,

Quoted 6 lines · Tycho Kirchner

With 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() therefore only notice the unsupported banks when measuring, e.g.

Yes, but users that will call measurement_init etc will have initialized the TPM.
The PCR code exists because some users want to use the TPM only as an RNG.

IIRC we can't get rid of the EFI call, because it's there to configure algos that where handed over to us from a previous stage loader that only handed us an EventLog. Can't we fet rid of it in pcr_extend though? Also if you do get rid of, this needs to go in tpm_init() instead of autostart

Cheers
/Ilias

Quoted 43 lines

tcg2_create_digest: unsupported algorithm 28
Cannot extend PCRs if all the TPM enabled algorithms are not supported

Deactivate them in tpm2_auto_start() already, before anything gets measured. If this fails, e.g. because the TPM refuses the allocation or no supported bank would remain active, return an error. The rationale is that with CONFIG_TPM_PCR_ALLOCATE enabled, U-Boot is expected to manage the banks, so fail early instead of at the first PCR extend. This is in line with the TPM_PCR_ALLOCATE help text, which does not specify at which point during boot the allocation takes place.

---
 lib/tpm-v2.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
lib/tpm-v2.c +12 -1
index 18ec9eb960d..bd049fed748 100644
@@ -232,6 +232,18 @@ u32 tpm2_auto_start(struct udevice *dev)
        if (rc)
                return rc;
 
+       /*
+        * Early deactivate active banks with unsupported algorithms. On success,
+        * this resets the board.
+        */
+       if (IS_ENABLED(CONFIG_TPM_PCR_ALLOCATE)) {
+               rc = tpm2_activate_banks(dev, 0);
+               if (rc) {
+                       log_err("TPM: failed to deactivate unsupported banks\n");
+                       return rc;
+               }
+       }
+
        return tpm2_update_active_banks(dev);
 }
 
--

2.47.3

#11 lore raw

Re: [PATCH 5/5] tpm: deactivate unsupported banks in tpm2_auto_start()

Hi Ilias,

Thanks for the review!

Am 01.11.26 um 12:52 schrieb Ilias Apalodimas:

Quoted 3 lines

Yes, but users that will call measurement_init etc will have initialized the TPM.
The PCR code exists because some users want to use the TPM only as an RNG.

My use case is a custom measurement implementation on top of lib/tpm-v2.c, which cannot use the tcg2_* functions. So tcg2_log_prepare_buffer() never runs for us, and nothing makes sure the unsupported banks are deactivated. I think the low-level API together with CONFIG_TPM_PCR_ALLOCATE should be able to handle this.

Regarding RNG-only users: TPM_PCR_ALLOCATE depends on MEASURED_BOOT || EFI_TCG2_PROTOCOL and is off by default. Its help text states that U-Boot sends a PCR allocate and reboots, without specifying at which point during boot. So a board enabling it opts in to that reboot. With MEASURED_BOOT, the first measurement resets the board anyway (with patches 1-4). With only EFI_TCG2_PROTOCOL, the reset currently happens once the EFI subsystem is initialised; boards which boot without EFI do not get it today, but arguably should, if they enabled TPM_PCR_ALLOCATE.

Quoted 3 lines

IIRC we can't get rid of the EFI call, because it's there to configure algos that where handed over to us from a previous stage loader that only handed us an EventLog.

Agreed, that call needs to stay, since only there the banks can be aligned with the event log.

Quoted 1 line

Can't we fet rid of it in pcr_extend though?

I would rather keep it as a fallback: callers may use tpm2_startup() directly instead of tpm_auto_start(), e.g. custom board code or "tpm2 startup" followed by "tpm2 pcr_extend" on the command line. For them, the check in tpm2_pcr_extend() is the only place where active unsupported banks are caught. With patch 4, it only deactivates the unsupported banks, and only with CONFIG_TPM_PCR_ALLOCATE.

Thanks,
Tycho

Quoted 43 lines
Quoted 43 lines

tcg2_create_digest: unsupported algorithm 28
Cannot extend PCRs if all the TPM enabled algorithms are not supported

Deactivate them in tpm2_auto_start() already, before anything gets measured. If this fails, e.g. because the TPM refuses the allocation or no supported bank would remain active, return an error. The rationale is that with CONFIG_TPM_PCR_ALLOCATE enabled, U-Boot is expected to manage the banks, so fail early instead of at the first PCR extend. This is in line with the TPM_PCR_ALLOCATE help text, which does not specify at which point during boot the allocation takes place.

---
 lib/tpm-v2.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
lib/tpm-v2.c +12 -1
index 18ec9eb960d..bd049fed748 100644
@@ -232,6 +232,18 @@ u32 tpm2_auto_start(struct udevice *dev)
        if (rc)
                return rc;
 
+       /*
+        * Early deactivate active banks with unsupported algorithms. On success,
+        * this resets the board.
+        */
+       if (IS_ENABLED(CONFIG_TPM_PCR_ALLOCATE)) {
+               rc = tpm2_activate_banks(dev, 0);
+               if (rc) {
+                       log_err("TPM: failed to deactivate unsupported banks\n");
+                       return rc;
+               }
+       }
+
        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
#12 lore raw

Re: [PATCH 5/5] tpm: deactivate unsupported banks in tpm2_auto_start()

Quoted 4 lines · Tycho Kirchner

Hi Ilias,

Thanks for the review!

yw

Quoted 11 lines

Am 01.11.26 um 12:52 schrieb Ilias Apalodimas:

Quoted 3 lines

Yes, but users that will call measurement_init etc will have initialized the TPM.
The PCR code exists because some users want to use the TPM only as an RNG.

My use case is a custom measurement implementation on top of lib/tpm-v2.c, which cannot use the tcg2_* functions. So tcg2_log_prepare_buffer() never runs for us, and nothing makes sure the unsupported banks are deactivated. I think the low-level API together with CONFIG_TPM_PCR_ALLOCATE should be able to handle this.

Yep, I like where this is going.

Quoted 10 lines

Regarding RNG-only users: TPM_PCR_ALLOCATE depends on MEASURED_BOOT || EFI_TCG2_PROTOCOL and is off by default. Its help text states that U-Boot sends a PCR allocate and reboots, without specifying at which point during boot. So a board enabling it opts in to that reboot. With MEASURED_BOOT, the first measurement resets the board anyway (with patches 1-4). With only EFI_TCG2_PROTOCOL, the reset currently happens once the EFI subsystem is initialised; boards which boot without EFI do not get it today, but arguably should, if they enabled TPM_PCR_ALLOCATE.

Yes they should.

Quoted 8 lines
Quoted 4 lines

IIRC we can't get rid of the EFI call, because it's there to configure algos that where handed over to us from a previous stage loader that only handed us an EventLog.

Agreed, that call needs to stay, since only there the banks can be aligned with the event log.

Quoted 8 lines
Quoted 1 line

Can't we fet rid of it in pcr_extend though?

I would rather keep it as a fallback: callers may use tpm2_startup() directly instead of tpm_auto_start(), e.g. custom board code or "tpm2 startup" followed by "tpm2 pcr_extend" on the command line. For them, the check in tpm2_pcr_extend() is the only place where active unsupported banks are caught. With patch 4, it only deactivates the unsupported banks, and only with CONFIG_TPM_PCR_ALLOCATE.

Yes that's what I was implying, but I wasn't very clear. Is there a reason we can't move it to tpm2_startup()? tpm_auto_start() is calling it anyway unless it's already initialized.

Cheers
/Ilias

Quoted 66 lines

Thanks,
Tycho

Quoted 40 lines
Quoted 40 lines

tcg2_create_digest: unsupported algorithm 28
Cannot extend PCRs if all the TPM enabled algorithms are not supported

Deactivate them in tpm2_auto_start() already, before anything gets measured. If this fails, e.g. because the TPM refuses the allocation or no supported bank would remain active, return an error. The rationale is that with CONFIG_TPM_PCR_ALLOCATE enabled, U-Boot is expected to manage the banks, so fail early instead of at the first PCR extend. This is in line with the TPM_PCR_ALLOCATE help text, which does not specify at which point during boot the allocation takes place.

---
 lib/tpm-v2.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
lib/tpm-v2.c +12 -0
index 18ec9eb960d..bd049fed748 100644
@@ -232,6 +232,18 @@ u32 tpm2_auto_start(struct udevice *dev)
        if (rc)
                return rc;
 
+       /*
+        * Early deactivate active banks with unsupported algorithms. On success,
+        * this resets the board.
+        */
+       if (IS_ENABLED(CONFIG_TPM_PCR_ALLOCATE)) {
+               rc = tpm2_activate_banks(dev, 0);
+               if (rc) {
+                       log_err("TPM: failed to deactivate unsupported banks\n");
+                       return rc;
+               }
+       }
+
        return tpm2_update_active_banks(dev);
 }
 
Quoted 3 lines
Quoted 3 lines

--
2.47.3

--
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

#13 lore raw

Re: [PATCH 5/5] tpm: deactivate unsupported banks in tpm2_auto_start()

Hi Ilias,

Am 02.11.26 um 22:00 schrieb Ilias Apalodimas:

Quoted 11 lines · Tycho Kirchner
Quoted 6 lines

I would rather keep it as a fallback: callers may use tpm2_startup() directly instead of tpm_auto_start(), e.g. custom board code or "tpm2 startup" followed by "tpm2 pcr_extend" on the command line. For them, the check in tpm2_pcr_extend() is the only place where active unsupported banks are caught. With patch 4, it only deactivates the unsupported banks, and only with CONFIG_TPM_PCR_ALLOCATE.

Yes that's what I was implying, but I wasn't very clear. Is there a reason we can't move it to tpm2_startup()? tpm_auto_start() is calling it anyway unless it's already initialized.

No, that works. For v2, I would deactivate the unsupported banks in tpm2_startup() with CONFIG_TPM_PCR_ALLOCATE, but only if the TPM was actually started there, i.e. TPM2_Startup(TPM2_SU_CLEAR) returned success. If it returns TPM2_RC_INITIALIZE, an earlier stage (e.g. SPL or TF-A) has already started the TPM and is expected to have taken care of the banks. With an event log, tcg2_log_prepare_buffer() still aligns them afterwards. TPM2_SU_STATE, i.e. tpm_resume(), is left alone.

The fallback in tpm2_pcr_extend() from 4/5 stays, for the case that the earlier stage supports more algorithms than U-Boot.

I suggest something like:

```
ret = tpm_sendrecv_command(dev, command_v2, NULL, NULL);
if ((ret && ret != TPM2_RC_INITIALIZE) || !bon)
    return ret;

/*
 * Only the stage which actually starts the TPM deactivates the
 * unsupported banks. 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);
```

What do you think?

Thanks,
Tycho

Quoted 63 lines
Quoted 63 lines
Quoted 43 lines
Quoted 43 lines

tcg2_create_digest: unsupported algorithm 28
Cannot extend PCRs if all the TPM enabled algorithms are not supported

Deactivate them in tpm2_auto_start() already, before anything gets measured. If this fails, e.g. because the TPM refuses the allocation or no supported bank would remain active, return an error. The rationale is that with CONFIG_TPM_PCR_ALLOCATE enabled, U-Boot is expected to manage the banks, so fail early instead of at the first PCR extend. This is in line with the TPM_PCR_ALLOCATE help text, which does not specify at which point during boot the allocation takes place.

---
 lib/tpm-v2.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
lib/tpm-v2.c +12 -0
index 18ec9eb960d..bd049fed748 100644
@@ -232,6 +232,18 @@ u32 tpm2_auto_start(struct udevice *dev)
        if (rc)
                return rc;
 
+       /*
+        * Early deactivate active banks with unsupported algorithms. On success,
+        * this resets the board.
+        */
+       if (IS_ENABLED(CONFIG_TPM_PCR_ALLOCATE)) {
+               rc = tpm2_activate_banks(dev, 0);
+               if (rc) {
+                       log_err("TPM: failed to deactivate unsupported banks\n");
+                       return rc;
+               }
+       }
+
        return tpm2_update_active_banks(dev);
 }
 
Quoted 3 lines
Quoted 3 lines

--
2.47.3

--
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

#14 lore raw

Re: [PATCH 5/5] tpm: deactivate unsupported banks in tpm2_auto_start()

Quoted 51 lines · Tycho Kirchner

Hi Ilias,

Am 02.11.26 um 22:00 schrieb Ilias Apalodimas:

Quoted 11 lines · Tycho Kirchner
Quoted 6 lines

I would rather keep it as a fallback: callers may use tpm2_startup() directly instead of tpm_auto_start(), e.g. custom board code or "tpm2 startup" followed by "tpm2 pcr_extend" on the command line. For them, the check in tpm2_pcr_extend() is the only place where active unsupported banks are caught. With patch 4, it only deactivates the unsupported banks, and only with CONFIG_TPM_PCR_ALLOCATE.

Yes that's what I was implying, but I wasn't very clear. Is there a reason we can't move it to tpm2_startup()? tpm_auto_start() is calling it anyway unless it's already initialized.

No, that works. For v2, I would deactivate the unsupported banks in tpm2_startup() with CONFIG_TPM_PCR_ALLOCATE, but only if the TPM was actually started there, i.e. TPM2_Startup(TPM2_SU_CLEAR) returned success. If it returns TPM2_RC_INITIALIZE, an earlier stage (e.g. SPL or TF-A) has already started the TPM and is expected to have taken care of the banks. With an event log, tcg2_log_prepare_buffer() still aligns them afterwards. TPM2_SU_STATE, i.e. tpm_resume(), is left alone.

The fallback in tpm2_pcr_extend() from 4/5 stays, for the case that the earlier stage supports more algorithms than U-Boot.

I suggest something like:

```
ret = tpm_sendrecv_command(dev, command_v2, NULL, NULL);
if ((ret && ret != TPM2_RC_INITIALIZE) || !bon)
    return ret;

/*
 * Only the stage which actually starts the TPM deactivates the
 * unsupported banks. 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 1 line

What do you think?

Yes, that's what I had in mind!

Thanks
/Ilias

Quoted 72 lines

Thanks,
Tycho

Quoted 63 lines
Quoted 63 lines
Quoted 43 lines
Quoted 43 lines

tcg2_create_digest: unsupported algorithm 28
Cannot extend PCRs if all the TPM enabled algorithms are not supported

Deactivate them in tpm2_auto_start() already, before anything gets measured. If this fails, e.g. because the TPM refuses the allocation or no supported bank would remain active, return an error. The rationale is that with CONFIG_TPM_PCR_ALLOCATE enabled, U-Boot is expected to manage the banks, so fail early instead of at the first PCR extend. This is in line with the TPM_PCR_ALLOCATE help text, which does not specify at which point during boot the allocation takes place.

---
 lib/tpm-v2.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
lib/tpm-v2.c +12 -0
index 18ec9eb960d..bd049fed748 100644
@@ -232,6 +232,18 @@ u32 tpm2_auto_start(struct udevice *dev)
        if (rc)
                return rc;
 
+       /*
+        * Early deactivate active banks with unsupported algorithms. On success,
+        * this resets the board.
+        */
+       if (IS_ENABLED(CONFIG_TPM_PCR_ALLOCATE)) {
+               rc = tpm2_activate_banks(dev, 0);
+               if (rc) {
+                       log_err("TPM: failed to deactivate unsupported banks\n");
+                       return rc;
+               }
+       }
+
        return tpm2_update_active_banks(dev);
 }
 
Quoted 3 lines
Quoted 3 lines

--
2.47.3

--
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