Skip to contentSkip to conversation

FAILED: patch "[PATCH] tpm: fix off-by-four bounds check in tpm2_get_random()" failed to apply to 6.12-stable tree

PATCH New [email protected] 3 participants 5 messages Oct 3 – Oct 5

Subsystems (3)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

The patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

To reproduce the conflict and resubmit, you may use the following commands:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkout FETCH_HEAD
git cherry-pick -x 9b522400bf5c082feb9a0037a053ea822a2c7e4d
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026100308-drew-squeamish-a28c@gregkh' --subject-prefix 'PATCH 6.12.y' 'HEAD^..'

Possible dependencies:

thanks,

greg k-h

------------------ original commit in Linus's tree ------------------

From 9b522400bf5c082feb9a0037a053ea822a2c7e4d Mon Sep 17 00:00:00 2001
From: Jiangshan Yi <[email protected]>
Date: Thu, 3 Sep 2026 11:58:37 +0800
Subject: [PATCH] tpm: fix off-by-four bounds check in tpm2_get_random()

When the response carries the TPM2_ST_SESSIONS tag, tpm2_get_random() skips the 4-byte parameter size field before locating the random data, but the bounds check still validates the response length against TPM_HEADER_SIZE. A truncated response can pass the check and make memcpy() read up to 4 bytes past the response end, so stale buffer contents end up in the caller's random bytes.

Fix this by checking the response length against 'offset', which already includes the skipped parameter size field.

drivers/char/tpm/tpm2-cmd.c +1 -1
index 96e0e06bbbf0..ae22295df798 100644
@@ -307,7 +307,7 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
 		out = (struct tpm2_get_random_out *)&buf->data[offset];
 		recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
 		if (tpm_buf_length(buf) <
-		    TPM_HEADER_SIZE +
+		    offset +
 		    offsetof(struct tpm2_get_random_out, buffer) +
 		    recd) {
 			tpm2_end_auth_session(chip);
maintainer
#1 lore raw

PATCH 1/1 tpm: fix off-by-four bounds check in tpm2_get_random()

From: Jiangshan Yi <[email protected]>

[ Upstream commit 9b522400bf5c082feb9a0037a053ea822a2c7e4d ]

When the response carries the TPM2_ST_SESSIONS tag, tpm2_get_random() skips the 4-byte parameter size field before locating the random data, but the bounds check still validates the response length against TPM_HEADER_SIZE. A truncated response can pass the check and make memcpy() read up to 4 bytes past the response end, so stale buffer contents end up in the caller's random bytes.

Fix this by checking the response length against 'offset', which already includes the skipped parameter size field.

Cc [email protected] # v6.12+
Fixes 1b6d7f9eb150 ("tpm: add session encryption protection to tpm2_get_random()")
[ sashal: adapted buf pointer access to a local structure variable ]
---
 drivers/char/tpm/tpm2-cmd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/tpm/tpm2-cmd.c +1 -1
index c710128f49b11..63d26aebf55db 100644
@@ -361,7 +361,7 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
 		out = (struct tpm2_get_random_out *)&buf.data[offset];
 		recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
 		if (tpm_buf_length(&buf) <
-		    TPM_HEADER_SIZE +
+		    offset +
 		    offsetof(struct tpm2_get_random_out, buffer) +
 		    recd) {
 			err = -EFAULT;
Signature · 1 line
2.53.0
maintainer
#2 lore raw

PATCH 1/1 tpm: fix off-by-four bounds check in tpm2_get_random()

In reply to [email protected]

From: Jiangshan Yi <[email protected]>

[ Upstream commit 9b522400bf5c082feb9a0037a053ea822a2c7e4d ]

When the response carries the TPM2_ST_SESSIONS tag, tpm2_get_random() skips the 4-byte parameter size field before locating the random data, but the bounds check still validates the response length against TPM_HEADER_SIZE. A truncated response can pass the check and make memcpy() read up to 4 bytes past the response end, so stale buffer contents end up in the caller's random bytes.

Fix this by checking the response length against 'offset', which already includes the skipped parameter size field.

---
 drivers/char/tpm/tpm2-cmd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/tpm/tpm2-cmd.c +1 -1
index c710128f49b1..63d26aebf55d 100644
@@ -361,7 +361,7 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
 		out = (struct tpm2_get_random_out *)&buf.data[offset];
 		recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
 		if (tpm_buf_length(&buf) <
-		    TPM_HEADER_SIZE +
+		    offset +
 		    offsetof(struct tpm2_get_random_out, buffer) +
 		    recd) {
 			err = -EFAULT;
Signature · 1 line
2.47.3
maintainer Applied
#3 lore raw

Re: [PATCH 6.12.y] tpm: fix off-by-four bounds check in tpm2_get_random()

Quoted 2 lines

Fix this by checking the response length against 'offset', which already includes the skipped parameter size field.

Queued for 6.12, thanks.

Signature · 2 lines
Thanks,
Sasha
maintainer
#4 lore raw

Re: [PATCH 6.12.y] tpm: fix off-by-four bounds check in tpm2_get_random()

Quoted 8 lines · Sasha Levin
Quoted 2 lines

Fix this by checking the response length against 'offset', which already includes the skipped parameter size field.

Queued for 6.12, thanks.

Signature · 2 lines
Thanks,
Sasha

Phew, good to hear, thank you!

There might be for some time more than usual merge conflicts in TPM driver given making tpm_buf memory layout flat for the sake of being able to use __free for of its allocations, and thus completely prevent any possible memory leaks from transient buffers.

Right, and also merge conflicts could happen also in trusted keys.

They are cheap to fix and I'll monitor this situation proactively, and try to react fast.

Br, Jarkko