diff mbox series

[4/8] tpm: Correct the define-space command in TPMv2

Message ID 20220301001125.1554442-5-sjg@chromium.org
State Changes Requested
Delegated to: Ilias Apalodimas
Headers show
Series tpm: Various minor fixes and enhancements | expand

Commit Message

Simon Glass March 1, 2022, 12:11 a.m. UTC
The message format is incorrect. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 lib/tpm-v2.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

Comments

Ilias Apalodimas June 7, 2022, 8:46 a.m. UTC | #1
Hi Simon, 

Thanks for fixing this. 
On Mon, Feb 28, 2022 at 05:11:21PM -0700, Simon Glass wrote:
> The message format is incorrect. Fix it.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
> 
>  lib/tpm-v2.c | 18 +++++++++++-------
>  1 file changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/lib/tpm-v2.c b/lib/tpm-v2.c
> index 6058f2e1e4..fe99b9c863 100644
> --- a/lib/tpm-v2.c
> +++ b/lib/tpm-v2.c
> @@ -89,14 +89,14 @@ u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
>  	 * Calculate the offset of the nv_policy piece by adding each of the
>  	 * chunks below.
>  	 */
> -	uint offset = 10 + 8 + 13 + 14;
> +	uint offset = 10 + 4 + 13 + 14;
>  	u8 command_v2[COMMAND_BUFFER_SIZE] = {
>  		/* header 10 bytes */
>  		tpm_u16(TPM2_ST_SESSIONS),	/* TAG */
> -		tpm_u32(offset + nv_policy_size),/* Length */
> +		tpm_u32(offset + nv_policy_size + 2),/* Length */
>  		tpm_u32(TPM2_CC_NV_DEFINE_SPACE),/* Command code */
>  
> -		/* handles 8 bytes */
> +		/* handles 4 bytes */
>  		tpm_u32(TPM2_RH_PLATFORM),	/* Primary platform seed */

Similar to my comment on the previous patches,  please send a version with
defines that gets rid of the magic numbers (offset, +2 on nv_policy_size etc)

>  
>  		/* session header 13 bytes */
> @@ -107,12 +107,15 @@ u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
>  		tpm_u16(0),			/* auth_size */
>  
>  		/* message 14 bytes + policy */
> -		tpm_u16(12 + nv_policy_size),	/* size */
> +		tpm_u16(12 + nv_policy_size + 2),	/* size */
>  		tpm_u32(space_index),
>  		tpm_u16(TPM2_ALG_SHA256),
>  		tpm_u32(nv_attributes),
>  		tpm_u16(nv_policy_size),
> -		/* nv_policy */
> +		/*
> +		 * nv_policy
> +		 * space_size
> +		 */
>  	};
>  	int ret;
>  
> @@ -120,8 +123,9 @@ u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
>  	 * Fill the command structure starting from the first buffer:
>  	 *     - the password (if any)
>  	 */
> -	ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
> -			       offset, nv_policy, nv_policy_size);
> +	ret = pack_byte_string(command_v2, sizeof(command_v2), "sw",
> +			       offset, nv_policy, nv_policy_size,
> +			       offset + nv_policy_size, space_size);
>  	if (ret)
>  		return TPM_LIB_ERROR;
>  
> -- 
> 2.35.1.574.g5d30c73bfb-goog
> 


Cheers
/Ilias
Simon Glass Aug. 14, 2022, 11:29 p.m. UTC | #2
Hi Ilias,

On Tue, 7 Jun 2022 at 02:46, Ilias Apalodimas
<ilias.apalodimas@linaro.org> wrote:
>
> Hi Simon,
>
> Thanks for fixing this.
> On Mon, Feb 28, 2022 at 05:11:21PM -0700, Simon Glass wrote:
> > The message format is incorrect. Fix it.
> >
> > Signed-off-by: Simon Glass <sjg@chromium.org>
> > ---
> >
> >  lib/tpm-v2.c | 18 +++++++++++-------
> >  1 file changed, 11 insertions(+), 7 deletions(-)
> >
> > diff --git a/lib/tpm-v2.c b/lib/tpm-v2.c
> > index 6058f2e1e4..fe99b9c863 100644
> > --- a/lib/tpm-v2.c
> > +++ b/lib/tpm-v2.c
> > @@ -89,14 +89,14 @@ u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
> >        * Calculate the offset of the nv_policy piece by adding each of the
> >        * chunks below.
> >        */
> > -     uint offset = 10 + 8 + 13 + 14;
> > +     uint offset = 10 + 4 + 13 + 14;
> >       u8 command_v2[COMMAND_BUFFER_SIZE] = {
> >               /* header 10 bytes */
> >               tpm_u16(TPM2_ST_SESSIONS),      /* TAG */
> > -             tpm_u32(offset + nv_policy_size),/* Length */
> > +             tpm_u32(offset + nv_policy_size + 2),/* Length */
> >               tpm_u32(TPM2_CC_NV_DEFINE_SPACE),/* Command code */
> >
> > -             /* handles 8 bytes */
> > +             /* handles 4 bytes */
> >               tpm_u32(TPM2_RH_PLATFORM),      /* Primary platform seed */
>
> Similar to my comment on the previous patches,  please send a version with
> defines that gets rid of the magic numbers (offset, +2 on nv_policy_size etc)

OK I have fixed these, with const int.

>
> >
> >               /* session header 13 bytes */
> > @@ -107,12 +107,15 @@ u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
> >               tpm_u16(0),                     /* auth_size */
> >
> >               /* message 14 bytes + policy */
> > -             tpm_u16(12 + nv_policy_size),   /* size */
> > +             tpm_u16(12 + nv_policy_size + 2),       /* size */
> >               tpm_u32(space_index),
> >               tpm_u16(TPM2_ALG_SHA256),
> >               tpm_u32(nv_attributes),
> >               tpm_u16(nv_policy_size),
> > -             /* nv_policy */
> > +             /*
> > +              * nv_policy
> > +              * space_size
> > +              */
> >       };
> >       int ret;
> >
> > @@ -120,8 +123,9 @@ u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
> >        * Fill the command structure starting from the first buffer:
> >        *     - the password (if any)
> >        */
> > -     ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
> > -                            offset, nv_policy, nv_policy_size);
> > +     ret = pack_byte_string(command_v2, sizeof(command_v2), "sw",
> > +                            offset, nv_policy, nv_policy_size,
> > +                            offset + nv_policy_size, space_size);
> >       if (ret)
> >               return TPM_LIB_ERROR;
> >
> > --
> > 2.35.1.574.g5d30c73bfb-goog
> >
>
>
> Cheers
> /Ilias

Regards,
Simon
diff mbox series

Patch

diff --git a/lib/tpm-v2.c b/lib/tpm-v2.c
index 6058f2e1e4..fe99b9c863 100644
--- a/lib/tpm-v2.c
+++ b/lib/tpm-v2.c
@@ -89,14 +89,14 @@  u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
 	 * Calculate the offset of the nv_policy piece by adding each of the
 	 * chunks below.
 	 */
-	uint offset = 10 + 8 + 13 + 14;
+	uint offset = 10 + 4 + 13 + 14;
 	u8 command_v2[COMMAND_BUFFER_SIZE] = {
 		/* header 10 bytes */
 		tpm_u16(TPM2_ST_SESSIONS),	/* TAG */
-		tpm_u32(offset + nv_policy_size),/* Length */
+		tpm_u32(offset + nv_policy_size + 2),/* Length */
 		tpm_u32(TPM2_CC_NV_DEFINE_SPACE),/* Command code */
 
-		/* handles 8 bytes */
+		/* handles 4 bytes */
 		tpm_u32(TPM2_RH_PLATFORM),	/* Primary platform seed */
 
 		/* session header 13 bytes */
@@ -107,12 +107,15 @@  u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
 		tpm_u16(0),			/* auth_size */
 
 		/* message 14 bytes + policy */
-		tpm_u16(12 + nv_policy_size),	/* size */
+		tpm_u16(12 + nv_policy_size + 2),	/* size */
 		tpm_u32(space_index),
 		tpm_u16(TPM2_ALG_SHA256),
 		tpm_u32(nv_attributes),
 		tpm_u16(nv_policy_size),
-		/* nv_policy */
+		/*
+		 * nv_policy
+		 * space_size
+		 */
 	};
 	int ret;
 
@@ -120,8 +123,9 @@  u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
 	 * Fill the command structure starting from the first buffer:
 	 *     - the password (if any)
 	 */
-	ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
-			       offset, nv_policy, nv_policy_size);
+	ret = pack_byte_string(command_v2, sizeof(command_v2), "sw",
+			       offset, nv_policy, nv_policy_size,
+			       offset + nv_policy_size, space_size);
 	if (ret)
 		return TPM_LIB_ERROR;