[PATCH v8 07/12] KEYS: trusted: Remove dead branch from tpm2_unseal_cmd

Jarkko Sakkinen posted 12 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH v8 07/12] KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
Posted by Jarkko Sakkinen 1 month, 3 weeks ago
TPM2_Unseal requires TPM2_ST_SESSIONS, and tpm2_unseal_cmd() always does
set up either password or HMAC session.

Remove the branch in tpm2_unseal_cmd() conditionally setting
TPM2_ST_NO_SESSIONS. It is faulty but luckily it is never exercised at
run-time, and thus does not cause regressions.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 security/keys/trusted-keys/trusted_tpm2.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index d3a5c5f2b926..3666e3e48eab 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -451,10 +451,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 			   struct trusted_key_options *options,
 			   u32 blob_handle)
 {
-	struct tpm_header *head;
 	struct tpm_buf buf;
 	u16 data_len;
-	int offset;
 	u8 *data;
 	int rc;
 
@@ -495,14 +493,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 		tpm_buf_append_u16(&buf, options->blobauth_len);
 		tpm_buf_append(&buf, options->blobauth, options->blobauth_len);
 
-		if (tpm2_chip_auth(chip)) {
+		if (tpm2_chip_auth(chip))
 			tpm_buf_append_hmac_session(chip, &buf, TPM2_SA_ENCRYPT, NULL, 0);
-		} else  {
-			offset = buf.handles * 4 + TPM_HEADER_SIZE;
-			head = (struct tpm_header *)buf.data;
-			if (tpm_buf_length(&buf) == offset)
-				head->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
-		}
 	}
 
 	rc = tpm_buf_fill_hmac_session(chip, &buf);
-- 
2.39.5
Re: [PATCH v8 07/12] KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
Posted by James Bottomley 1 month, 2 weeks ago
On Tue, 2025-12-16 at 11:21 +0200, Jarkko Sakkinen wrote:
> TPM2_Unseal requires TPM2_ST_SESSIONS, and tpm2_unseal_cmd() always
> does set up either password or HMAC session.
> 
> Remove the branch in tpm2_unseal_cmd() conditionally setting
> TPM2_ST_NO_SESSIONS. It is faulty but luckily it is never exercised
> at run-time, and thus does not cause regressions.

Shouldn't that also be

Fixes: b7960b904861 ("tpm2-sessions: Open code tpm_buf_append_hmac_session()")

> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
> ---
>  security/keys/trusted-keys/trusted_tpm2.c | 10 +---------
>  1 file changed, 1 insertion(+), 9 deletions(-)
> 
> diff --git a/security/keys/trusted-keys/trusted_tpm2.c
> b/security/keys/trusted-keys/trusted_tpm2.c
> index d3a5c5f2b926..3666e3e48eab 100644
> --- a/security/keys/trusted-keys/trusted_tpm2.c
> +++ b/security/keys/trusted-keys/trusted_tpm2.c
> @@ -451,10 +451,8 @@ static int tpm2_unseal_cmd(struct tpm_chip
> *chip,
>  			   struct trusted_key_options *options,
>  			   u32 blob_handle)
>  {
> -	struct tpm_header *head;
>  	struct tpm_buf buf;
>  	u16 data_len;
> -	int offset;
>  	u8 *data;
>  	int rc;
>  
> @@ -495,14 +493,8 @@ static int tpm2_unseal_cmd(struct tpm_chip
> *chip,
>  		tpm_buf_append_u16(&buf, options->blobauth_len);
>  		tpm_buf_append(&buf, options->blobauth, options-
> >blobauth_len);
>  
> -		if (tpm2_chip_auth(chip)) {
> +		if (tpm2_chip_auth(chip))

Since the statement above is that the if is always true, why do you
still have it here?

Regards,

James
Re: [PATCH v8 07/12] KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
Posted by Jarkko Sakkinen 1 month ago
On Fri, Dec 19, 2025 at 03:54:47PM -0500, James Bottomley wrote:
> On Tue, 2025-12-16 at 11:21 +0200, Jarkko Sakkinen wrote:
> > TPM2_Unseal requires TPM2_ST_SESSIONS, and tpm2_unseal_cmd() always
> > does set up either password or HMAC session.
> > 
> > Remove the branch in tpm2_unseal_cmd() conditionally setting
> > TPM2_ST_NO_SESSIONS. It is faulty but luckily it is never exercised
> > at run-time, and thus does not cause regressions.
> 
> Shouldn't that also be
> 
> Fixes: b7960b904861 ("tpm2-sessions: Open code tpm_buf_append_hmac_session()")

The implementation has pre-existed before that commit so it did
not really cause it. The call path was just more masked before
open coding it.

The code is of course exercised in !TCG_TPM2_HMAC case but it 
by definition does nothing.

> 
> > Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
> > ---
> >  security/keys/trusted-keys/trusted_tpm2.c | 10 +---------
> >  1 file changed, 1 insertion(+), 9 deletions(-)
> > 
> > diff --git a/security/keys/trusted-keys/trusted_tpm2.c
> > b/security/keys/trusted-keys/trusted_tpm2.c
> > index d3a5c5f2b926..3666e3e48eab 100644
> > --- a/security/keys/trusted-keys/trusted_tpm2.c
> > +++ b/security/keys/trusted-keys/trusted_tpm2.c
> > @@ -451,10 +451,8 @@ static int tpm2_unseal_cmd(struct tpm_chip
> > *chip,
> >  			   struct trusted_key_options *options,
> >  			   u32 blob_handle)
> >  {
> > -	struct tpm_header *head;
> >  	struct tpm_buf buf;
> >  	u16 data_len;
> > -	int offset;
> >  	u8 *data;
> >  	int rc;
> >  
> > @@ -495,14 +493,8 @@ static int tpm2_unseal_cmd(struct tpm_chip
> > *chip,
> >  		tpm_buf_append_u16(&buf, options->blobauth_len);
> >  		tpm_buf_append(&buf, options->blobauth, options-
> > >blobauth_len);
> >  
> > -		if (tpm2_chip_auth(chip)) {
> > +		if (tpm2_chip_auth(chip))
> 
> Since the statement above is that the if is always true, why do you
> still have it here?

This is still necessary for !TCG_TPM2_HMAC case. The commit is pretty
much exactly in its described scope.

> 
> Regards,
> 
> James
> 

BR, Jarkko
Re: [PATCH v8 07/12] KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
Posted by Jonathan McDowell 1 month, 2 weeks ago
On Tue, Dec 16, 2025 at 11:21:41AM +0200, Jarkko Sakkinen wrote:
>TPM2_Unseal requires TPM2_ST_SESSIONS, and tpm2_unseal_cmd() always does
>set up either password or HMAC session.
>
>Remove the branch in tpm2_unseal_cmd() conditionally setting
>TPM2_ST_NO_SESSIONS. It is faulty but luckily it is never exercised at
>run-time, and thus does not cause regressions.

Reviewed-by: Jonathan McDowell <noodles@meta.com>

>Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
>---
> security/keys/trusted-keys/trusted_tpm2.c | 10 +---------
> 1 file changed, 1 insertion(+), 9 deletions(-)
>
>diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
>index d3a5c5f2b926..3666e3e48eab 100644
>--- a/security/keys/trusted-keys/trusted_tpm2.c
>+++ b/security/keys/trusted-keys/trusted_tpm2.c
>@@ -451,10 +451,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
> 			   struct trusted_key_options *options,
> 			   u32 blob_handle)
> {
>-	struct tpm_header *head;
> 	struct tpm_buf buf;
> 	u16 data_len;
>-	int offset;
> 	u8 *data;
> 	int rc;
>
>@@ -495,14 +493,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
> 		tpm_buf_append_u16(&buf, options->blobauth_len);
> 		tpm_buf_append(&buf, options->blobauth, options->blobauth_len);
>
>-		if (tpm2_chip_auth(chip)) {
>+		if (tpm2_chip_auth(chip))
> 			tpm_buf_append_hmac_session(chip, &buf, TPM2_SA_ENCRYPT, NULL, 0);
>-		} else  {
>-			offset = buf.handles * 4 + TPM_HEADER_SIZE;
>-			head = (struct tpm_header *)buf.data;
>-			if (tpm_buf_length(&buf) == offset)
>-				head->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
>-		}
> 	}
>
> 	rc = tpm_buf_fill_hmac_session(chip, &buf);
>-- 
>2.39.5
>
>

J.

-- 
"Never test for an error condition you don't know how to handle."
  --Steinbach