Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] tpm: OOB read and premature state mutation in
  tpm2_parse_create_primary()
--

commit 92830ac1679c3da842e2672d38ef677266816bbc
Author: Jarkko Sakkinen <[email protected]>

tpm: Add persistent hierarchies to tpm2_create_primary()

This commit updates tpm2_parse_create_primary() to only populate
chip->null_ec_key_x and chip->null_ec_key_y when the hierarchy is
TPM2_RH_NULL, verifies the returned key name against the locally computed
TPMT_PUBLIC digest, and flushes the transient handle on validation failure.

> diff --git a/drivers/char/tpm/tpm2-sessions.c 
> b/drivers/char/tpm/tpm2-sessions.c
> index b3b4f7a89d9d3..98f668874c3e2 100644
> --- a/drivers/char/tpm/tpm2-sessions.c
> +++ b/drivers/char/tpm/tpm2-sessions.c
[ ... ]
> @@ -1170,85 +1170,87 @@ static int tpm2_parse_create_primary(struct tpm_chip 
> *chip, struct tpm_buf *buf,
>       param_len += TPM_HEADER_SIZE;
>  
>       if (param_len + 8 > total_len)
> -             return -EINVAL;
> +             goto err;
>       len = tpm_buf_read_u16(buf, &offset_r);
>       offset_t = offset_r;
>       if (offset_r + len > tpm_buf_length(buf))
> -             return -EINVAL;
> -     if (name) {
> -             /*
> -              * now we have the public area, compute the name of
> -              * the object
> -              */
> -             put_unaligned_be16(TPM_ALG_SHA256, name);
> -             sha256(&buf->data[offset_r], len, name + 2);
> -     }
> +             goto err;
> +
> +     /*
> +      * Now we have the public area, compute the name of the object.
> +      */
> +     put_unaligned_be16(TPM_ALG_SHA256, resp_name);
> +     sha256(&buf->data[offset_r], len, resp_name + 2);
> +     if (name)
> +             memcpy(name, resp_name, sizeof(resp_name));
[ ... ]
>       /* extract public key (x and y points) */
>       val = tpm_buf_read_u16(buf, &offset_t);
>       if (val != EC_PT_SZ)
> -             return -EINVAL;
> +             goto err;
>  #ifdef CONFIG_TCG_TPM2_HMAC
> -     memcpy(chip->null_ec_key_x, &buf->data[offset_t], val);
> +     if (hierarchy == TPM2_RH_NULL)
> +             memcpy(chip->null_ec_key_x, &buf->data[offset_t], val);
>  #endif
>       offset_t += val;
>       val = tpm_buf_read_u16(buf, &offset_t);
>       if (val != EC_PT_SZ)
> -             return -EINVAL;
> +             goto err;
>  #ifdef CONFIG_TCG_TPM2_HMAC
> -     memcpy(chip->null_ec_key_y, &buf->data[offset_t], val);
> +     if (hierarchy == TPM2_RH_NULL)
> +             memcpy(chip->null_ec_key_y, &buf->data[offset_t], val);
>  #endif
>       offset_t += val;
>  
> @@ -1257,38 +1259,38 @@ static int tpm2_parse_create_primary(struct tpm_chip 
> *chip, struct tpm_buf *buf,
>  
>       /* should have exactly consumed the TPM2B public structure */
>       if (offset_t != offset_r)
> -             return -EINVAL;
> +             goto err;

[Severity: Medium]
Can a truncated TPM2B_PUBLIC response cause the memcpy() calls into
chip->null_ec_key_x and chip->null_ec_key_y to read past buf->length and
overwrite chip->null_ec_key_{x,y} before validation completes?

At the start of tpm2_parse_create_primary(), only offset_r + len <=
tpm_buf_length(buf) is checked, without a minimum bound on len, and the
exact consumption check (offset_t != offset_r) runs only after both
32-byte copies have already executed.

In addition, unlike *handle = keyhandle (which was moved to the end of the
function), the caller's name buffer and chip->null_ec_key_{x,y} are still
updated before the public area, creation ticket, and returned object name
are validated.  Because this patch also removes the comparison against
chip->null_key_name, a post-boot TPM2_RH_NULL call with a mismatched
response overwrites the EC coordinates used by tpm2_start_auth_session()
to encrypt session salts.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=4

Reply via email to