diff mbox series

[v2,1/2] tpm: Fix buffer access in tpm2_get_tpm_pt()

Message ID 20220513134152.270442-1-stefan.mahnke-hartmann@infineon.com (mailing list archive)
State New
Headers show
Series [v2,1/2] tpm: Fix buffer access in tpm2_get_tpm_pt() | expand

Commit Message

Stefan Mahnke-Hartmann May 13, 2022, 1:41 p.m. UTC
Under certain conditions uninitialized memory will be accessed.
As described by TCG Trusted Platform Module Library Specification,
rev. 1.59 (Part 3: Commands), if a TPM2_GetCapability is received,
requesting a capability, the TPM in field upgrade mode may return a
zero length list.
Check the property count in tpm2_get_tpm_pt().

Fixes: 2ab3241161b3 ("tpm: migrate tpm2_get_tpm_pt() to use struct tpm_buf")
Cc: stable@vger.kernel.org
Signed-off-by: Stefan Mahnke-Hartmann <stefan.mahnke-hartmann@infineon.com>
---
Changelog:
 * v2:
   * Add inline comment to indicate the root cause to may access unitilized
     memory.
   * Change 'field upgrade mode' to lower case.

 drivers/char/tpm/tpm2-cmd.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

Comments

Jarkko Sakkinen May 16, 2022, 6:38 p.m. UTC | #1
On Fri, May 13, 2022 at 03:41:51PM +0200, Stefan Mahnke-Hartmann wrote:
> Under certain conditions uninitialized memory will be accessed.
> As described by TCG Trusted Platform Module Library Specification,
> rev. 1.59 (Part 3: Commands), if a TPM2_GetCapability is received,
> requesting a capability, the TPM in field upgrade mode may return a
> zero length list.
> Check the property count in tpm2_get_tpm_pt().
> 
> Fixes: 2ab3241161b3 ("tpm: migrate tpm2_get_tpm_pt() to use struct tpm_buf")
> Cc: stable@vger.kernel.org
> Signed-off-by: Stefan Mahnke-Hartmann <stefan.mahnke-hartmann@infineon.com>
> ---
> Changelog:
>  * v2:
>    * Add inline comment to indicate the root cause to may access unitilized
>      memory.
>    * Change 'field upgrade mode' to lower case.
> 
>  drivers/char/tpm/tpm2-cmd.c | 11 ++++++++++-
>  1 file changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
> index 4704fa553098..04a3e23a4afc 100644
> --- a/drivers/char/tpm/tpm2-cmd.c
> +++ b/drivers/char/tpm/tpm2-cmd.c
> @@ -400,7 +400,16 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,  u32 *value,
>  	if (!rc) {
>  		out = (struct tpm2_get_cap_out *)
>  			&buf.data[TPM_HEADER_SIZE];
> -		*value = be32_to_cpu(out->value);
> +		/*
> +		 * To prevent failing boot up of some systems, Infineon TPM2.0
> +		 * returns SUCCESS on TPM2_Startup in field upgrade mode. Also
> +		 * the TPM2_Getcapability command returns a zero length list
> +		 * in field upgrade mode.
> +		 */
> +		if (be32_to_cpu(out->property_cnt) > 0)
> +			*value = be32_to_cpu(out->value);
> +		else
> +			rc = -ENODATA;
>  	}
>  	tpm_buf_destroy(&buf);
>  	return rc;
> -- 
> 2.25.1
> 

Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>

BR, Jarkko
diff mbox series

Patch

diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
index 4704fa553098..04a3e23a4afc 100644
--- a/drivers/char/tpm/tpm2-cmd.c
+++ b/drivers/char/tpm/tpm2-cmd.c
@@ -400,7 +400,16 @@  ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,  u32 *value,
 	if (!rc) {
 		out = (struct tpm2_get_cap_out *)
 			&buf.data[TPM_HEADER_SIZE];
-		*value = be32_to_cpu(out->value);
+		/*
+		 * To prevent failing boot up of some systems, Infineon TPM2.0
+		 * returns SUCCESS on TPM2_Startup in field upgrade mode. Also
+		 * the TPM2_Getcapability command returns a zero length list
+		 * in field upgrade mode.
+		 */
+		if (be32_to_cpu(out->property_cnt) > 0)
+			*value = be32_to_cpu(out->value);
+		else
+			rc = -ENODATA;
 	}
 	tpm_buf_destroy(&buf);
 	return rc;