Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] KEYS: trusted: 32-bit param_size overflow in
  tpm2_asym_rsa_decrypt()
- [Low] KEYS: trusted: blob_len mismatch in tpm2_load_cmd() and
  tpm2_asym_load()
--

commit 9a2d1dec7eb8e2fb4164f93f474ab53d2345c7be
Author: Jarkko Sakkinen <[email protected]>

KEYS: trusted: Add asymmetric key support for TPM2

This commit extends the TPM 2.0 trusted key backend to load
OID_TPMLoadableKey RSA and ECC keys and expose query, sign, verify, and
RSA encrypt/decrypt operations through key_type_trusted.

> diff --git a/security/keys/trusted-keys/trusted_tpm2.c 
> b/security/keys/trusted-keys/trusted_tpm2.c
> index e3cd02e19857e..e4731724908a7 100644
> --- a/security/keys/trusted-keys/trusted_tpm2.c
> +++ b/security/keys/trusted-keys/trusted_tpm2.c
[ ... ]
> @@ -750,10 +987,799 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,
>       if (rc)
>               goto out;
>  
> -     rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);
> +     if (!private->alg_type)
> +             rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);
>       tpm2_flush_context(chip, blob_handle);
>  
>  out:
>       tpm_put_ops(chip);
>       return tpm_ret_to_err(rc);
>  }
> +
> +#ifdef CONFIG_TRUSTED_KEYS_ASYMMETRIC
> +static int tpm2_asym_load(struct tpm_chip *chip,
> +                       const struct trusted_key_payload *payload,
> +                       struct trusted_key_tpm *private,
> +                       u32 *key_handle)
> +{
> +     u8 *blob __free(kfree) = NULL;
> +     unsigned int blob_len;
> +     int rc;
> +
> +     rc = tpm2_key_decode(payload, private, NULL, &blob, &blob_len, NULL);
> +     if (rc)
> +             return rc;
> +
> +     return tpm2_load_blob(chip, private, blob, blob_len, key_handle);
> +}

[Severity: Low]
Can tpm2_asym_load() pass a different blob_len to tpm2_load_blob() than
tpm2_load_cmd() did when the key was instantiated?

In tpm2_load_cmd(), blob_len is clamped to private_len + public_len + 4,
which strips any trailing bytes in the ASN.1 OCTET STRINGs before loading.
tpm2_asym_load() passes the untruncated ctx.priv_len + ctx.pub_len from
tpm2_key_decode() directly to tpm2_load_blob(), so a key with trailing
bytes will load during add_key() but fail on subsequent pkey operations.

[ ... ]

> +static int tpm2_asym_rsa_decrypt(struct tpm_chip *chip,
> +                              const struct trusted_key_payload *payload,
> +                              struct trusted_key_tpm *private,
> +                              struct kernel_pkey_params *params,
> +                              const void *in, void *out)
> +{
[ ... ]
> +     offset = TPM_HEADER_SIZE;
> +     param_size = tpm_buf_read_u32(buf, &offset);
> +     decrypted_len = tpm_buf_read_u16(buf, &offset);
> +     if ((buf->flags & TPM_BUF_INVALID) ||
> +         param_size < 2 + decrypted_len ||
> +         offset - 2 + param_size > buf->length) {
> +             rc = -EIO;
> +             goto out_key;
> +     }
> +     if (params->out_len < decrypted_len) {
> +             rc = -EMSGSIZE;
> +             goto out_key;
> +     }
> +
> +     memcpy(out, &buf->data[offset], decrypted_len);

[Severity: Medium]
Can offset - 2 + param_size wrap around on 32-bit architectures?

When off_t is 32-bit, a malformed unauthenticated TPM response with
param_size >= 0xfffffff2 wraps offset - 2 + param_size past buf->length
while satisfying param_size >= 2 + decrypted_len, allowing memcpy() to
read up to decrypted_len bytes past buf->length from buf->data.  The same
32-bit overflow can occur in tpm2_asym_parse_signature() at
offset + param_size > buf->length.  Checking
param_size > buf->length - (offset - 2) and
param_size > buf->length - offset avoids the wraparound.

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

Reply via email to