Linux Security Modules development
 help / color / mirror / Atom feed
* [PATCH] KEYS: trusted: Fix blob allocation size in tpm2_key_decode()
@ 2026-09-23 17:35 Stefano Garzarella
  2026-09-23 17:50 ` sashiko-bot
  2026-09-25 22:49 ` Jarkko Sakkinen
  0 siblings, 2 replies; 3+ messages in thread
From: Stefano Garzarella @ 2026-09-23 17:35 UTC (permalink / raw)
  To: linux-integrity
  Cc: Jarkko Sakkinen, keyrings, Mimi Zohar, Serge E. Hallyn,
	linux-kernel, David Howells, James Morris, linux-security-module,
	James Bottomley, Paul Moore, Stefano Garzarella

From: Stefano Garzarella <sgarzare@redhat.com>

tpm2_key_decode() allocates 4 bytes more than needed. The ASN.1
callbacks tpm2_key_priv() and tpm2_key_pub() provide the lengths
of TPM2B_PRIVATE and TPM2B_PUBLIC, so ctx.priv_len and ctx.pub_len
already account for the 2-byte `size` field each of those structures
starts with.

I noticed this while reviewing commit 114f00d738f1 ("KEYS: trusted:
Fix tpm2_load_cmd() boundary check"), which correctly reports
ctx.priv_len + ctx.pub_len as the decoded blob size [1].

Let's allocate exactly that amount, matching the data copied into
the blob.

[1] https://lore.kernel.org/linux-integrity/apfoKo-BdwaLXtkT@sgarzare-redhat/

Fixes: f2219745250f ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs")
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
 security/keys/trusted-keys/trusted_tpm2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index 01f18bb37047..a9b8a31a637c 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -115,7 +115,7 @@ static int tpm2_key_decode(struct trusted_key_payload *payload,
 	if (ctx.priv_len + ctx.pub_len > MAX_BLOB_SIZE)
 		return -EINVAL;
 
-	blob = kmalloc(ctx.priv_len + ctx.pub_len + 4, GFP_KERNEL);
+	blob = kmalloc(ctx.priv_len + ctx.pub_len, GFP_KERNEL);
 	if (!blob)
 		return -ENOMEM;
 
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] KEYS: trusted: Fix blob allocation size in tpm2_key_decode()
  2026-09-23 17:35 [PATCH] KEYS: trusted: Fix blob allocation size in tpm2_key_decode() Stefano Garzarella
@ 2026-09-23 17:50 ` sashiko-bot
  2026-09-25 22:49 ` Jarkko Sakkinen
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-09-23 17:50 UTC (permalink / raw)
  Cc: linux-security-module

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] keys: trusted: tpm2_key_decode() causes ZERO_SIZE_PTR dereference panic
--

commit 0b756ca20b74ac8e9fbfa99370ee0a0c61126972
Author: Stefano Garzarella <sgarzare@redhat.com>

KEYS: trusted: Fix blob allocation size in tpm2_key_decode()

This commit adjusts the allocation size for the TPM2 key blob in
tpm2_key_decode() to match the actual size of the decoded data, removing
an unnecessary 4-byte padding. The author notes that a separate commit,
114f00d738f1, fixes a boundary check in tpm2_load_cmd() that correctly
reports the decoded blob size.

> diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
> index 6340823f8b53c..ed88758c7e9bf 100644
> --- a/security/keys/trusted-keys/trusted_tpm2.c
> +++ b/security/keys/trusted-keys/trusted_tpm2.c
> @@ -116,7 +116,7 @@ static int tpm2_key_decode(struct trusted_key_payload *payload,
>  	if (ctx.priv_len + ctx.pub_len > MAX_BLOB_SIZE)
>  		return -EINVAL;
>  
> -	blob = kmalloc(ctx.priv_len + ctx.pub_len + 4, GFP_KERNEL);
> +	blob = kmalloc(ctx.priv_len + ctx.pub_len, GFP_KERNEL);
>  	if (!blob)
>  		return -ENOMEM;

[Severity: Critical]
Does this code handle the case where both lengths are zero?

If a payload contains empty strings for both privkey and pubkey,
asn1_ber_decoder() will succeed with ctx.priv_len and ctx.pub_len both
being 0 in tpm2_key_decode().

Because the padding has been removed, this results in:

    kmalloc(0, GFP_KERNEL)

which returns ZERO_SIZE_PTR (0x10). Since 0x10 is not NULL, the if (!blob)
check passes.

Later in security/keys/trusted-keys/trusted_tpm2.c:tpm2_load_cmd(), the
code unconditionally dereferences the newly allocated blob:

    private_len = get_unaligned_be16(blob);

The boundary check in tpm2_load_cmd() uses payload->blob_len (which is >= 15
for this ASN.1 sequence) instead of the decoded key length. Thus, the check
passes, dereferencing ZERO_SIZE_PTR and causing a kernel panic.

While the commit message mentions commit 114f00d738f1 as correctly reporting
the decoded blob size, that prerequisite commit is not present in this tree
or series. Applying this patch without its prerequisite introduces a
regression where this panic can occur.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260923173506.41519-1-sgarzare@redhat.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] KEYS: trusted: Fix blob allocation size in tpm2_key_decode()
  2026-09-23 17:35 [PATCH] KEYS: trusted: Fix blob allocation size in tpm2_key_decode() Stefano Garzarella
  2026-09-23 17:50 ` sashiko-bot
@ 2026-09-25 22:49 ` Jarkko Sakkinen
  1 sibling, 0 replies; 3+ messages in thread
From: Jarkko Sakkinen @ 2026-09-25 22:49 UTC (permalink / raw)
  To: Stefano Garzarella
  Cc: linux-integrity, keyrings, Mimi Zohar, Serge E. Hallyn,
	linux-kernel, David Howells, James Morris, linux-security-module,
	James Bottomley, Paul Moore

On Wed, Sep 23, 2026 at 07:35:06PM +0200, Stefano Garzarella wrote:
> From: Stefano Garzarella <sgarzare@redhat.com>
> 
> tpm2_key_decode() allocates 4 bytes more than needed. The ASN.1
> callbacks tpm2_key_priv() and tpm2_key_pub() provide the lengths
> of TPM2B_PRIVATE and TPM2B_PUBLIC, so ctx.priv_len and ctx.pub_len
> already account for the 2-byte `size` field each of those structures
> starts with.
> 
> I noticed this while reviewing commit 114f00d738f1 ("KEYS: trusted:
> Fix tpm2_load_cmd() boundary check"), which correctly reports
> ctx.priv_len + ctx.pub_len as the decoded blob size [1].
> 
> Let's allocate exactly that amount, matching the data copied into
> the blob.
> 
> [1] https://lore.kernel.org/linux-integrity/apfoKo-BdwaLXtkT@sgarzare-redhat/
> 
> Fixes: f2219745250f ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs")
> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
> ---
>  security/keys/trusted-keys/trusted_tpm2.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
> index 01f18bb37047..a9b8a31a637c 100644
> --- a/security/keys/trusted-keys/trusted_tpm2.c
> +++ b/security/keys/trusted-keys/trusted_tpm2.c
> @@ -115,7 +115,7 @@ static int tpm2_key_decode(struct trusted_key_payload *payload,
>  	if (ctx.priv_len + ctx.pub_len > MAX_BLOB_SIZE)
>  		return -EINVAL;
>  
> -	blob = kmalloc(ctx.priv_len + ctx.pub_len + 4, GFP_KERNEL);
> +	blob = kmalloc(ctx.priv_len + ctx.pub_len, GFP_KERNEL);
>  	if (!blob)
>  		return -ENOMEM;
>  
> -- 
> 2.55.0
> 

Yeah, we talked this over so not much to say about this:

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

Thanks.

Br, Jarkko

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-09-25 22:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-23 17:35 [PATCH] KEYS: trusted: Fix blob allocation size in tpm2_key_decode() Stefano Garzarella
2026-09-23 17:50 ` sashiko-bot
2026-09-25 22:49 ` Jarkko Sakkinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox