From: Jarkko Sakkinen <jarkko@kernel.org>
To: stable@vger.kernel.org
Cc: Jiangshan Yi <yijiangshan@kylinos.cn>,
Sashiko <sashiko-bot@kernel.org>,
Jarkko Sakkinen <jarkko@kernel.org>,
Peter Huewe <peterhuewe@gmx.de>, Jason Gunthorpe <jgg@ziepe.ca>,
Jonathan McDowell <noodles@meta.com>,
Roberto Sassu <roberto.sassu@huawei.com>,
Justinien Bouron <jbouron@amazon.com>,
Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>,
Gunnar Kudrjavets <gunnarku@amazon.com>,
Baoli Zhang <baoli.zhang@linux.intel.com>,
James Bottomley <James.Bottomley@HansenPartnership.com>,
linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 7.2.y] tpm: fix off-by-four bounds check in tpm2_get_random()
Date: Mon, 5 Oct 2026 10:54:45 +0300 [thread overview]
Message-ID: <20261005075447.98992-1-jarkko@kernel.org> (raw)
In-Reply-To: <2026100307-vacancy-udder-83a1@gregkh>
From: Jiangshan Yi <yijiangshan@kylinos.cn>
[ 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: stable@vger.kernel.org # v6.12+
Fixes: 1b6d7f9eb150 ("tpm: add session encryption protection to tpm2_get_random()")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260902074839.417419-1-yijiangshan%40kylinos.cn
Signed-off-by: Jiangshan Yi <yijiangshan@kylinos.cn>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Link: https://lore.kernel.org/r/20260903035837.219284-1-yijiangshan@kylinos.cn
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
drivers/char/tpm/tpm2-cmd.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
index 52ee350da867..e12e245faeb1 100644
--- a/drivers/char/tpm/tpm2-cmd.c
+++ b/drivers/char/tpm/tpm2-cmd.c
@@ -318,7 +318,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;
--
2.47.3
next prev parent reply other threads:[~2026-10-05 7:55 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-03 13:20 FAILED: patch "[PATCH] tpm: fix off-by-four bounds check in tpm2_get_random()" failed to apply to 7.2-stable tree gregkh
2026-10-04 1:39 ` [PATCH 7.2.y 1/8] tpm: Initial step to reorganize TPM public headers Sasha Levin
2026-10-04 1:39 ` [PATCH 7.2.y 2/8] tpm: Move TPM1 specific definitions to the command header Sasha Levin
2026-10-04 1:39 ` [PATCH 7.2.y 3/8] tpm: Move TPM2 " Sasha Levin
2026-10-04 1:39 ` [PATCH 7.2.y 4/8] tpm: Move TPM common base " Sasha Levin
2026-10-04 1:39 ` [PATCH 7.2.y 5/8] tpm-buf: Merge TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW Sasha Levin
2026-10-04 1:40 ` [PATCH 7.2.y 6/8] tpm-buf: Remove chip parameter from tpm_buf_append_handle() Sasha Levin
2026-10-04 1:40 ` [PATCH 7.2.y 7/8] tpm-buf: Memory-safe allocations Sasha Levin
2026-10-04 1:40 ` [PATCH 7.2.y 8/8] tpm: fix off-by-four bounds check in tpm2_get_random() Sasha Levin
2026-10-05 7:54 ` Jarkko Sakkinen [this message]
2026-10-05 13:50 ` [PATCH 7.2.y] " Sasha Levin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261005075447.98992-1-jarkko@kernel.org \
--to=jarkko@kernel.org \
--cc=James.Bottomley@HansenPartnership.com \
--cc=baoli.zhang@linux.intel.com \
--cc=gunnarku@amazon.com \
--cc=jbouron@amazon.com \
--cc=jgg@ziepe.ca \
--cc=krzysztof.kozlowski@oss.qualcomm.com \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=noodles@meta.com \
--cc=peterhuewe@gmx.de \
--cc=roberto.sassu@huawei.com \
--cc=sashiko-bot@kernel.org \
--cc=stable@vger.kernel.org \
--cc=yijiangshan@kylinos.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox