From: Jarkko Sakkinen <jarkko@kernel.org>
To: Jiangshan Yi <yijiangshan@kylinos.cn>
Cc: peterhuewe@gmx.de, jgg@ziepe.ca,
James.Bottomley@hansenpartnership.com,
linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org,
13667453960@163.com, Sashiko <sashiko-bot@kernel.org>,
stable@vger.kernel.org
Subject: Re: [PATCH] tpm: fix off-by-four bounds check in tpm2_get_random()
Date: Thu, 10 Sep 2026 01:40:14 +0300 [thread overview]
Message-ID: <aqHgTqQQDQhUFFno@kernel.org> (raw)
In-Reply-To: <20260903035837.219284-1-yijiangshan@kylinos.cn>
On Thu, Sep 03, 2026 at 11:58:37AM +0800, Jiangshan Yi wrote:
> 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.
>
> 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
> Cc: stable@vger.kernel.org
> Signed-off-by: Jiangshan Yi <yijiangshan@kylinos.cn>
> ---
> 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 48cec39995fe..d74df7808a3f 100644
> --- a/drivers/char/tpm/tpm2-cmd.c
> +++ b/drivers/char/tpm/tpm2-cmd.c
> @@ -305,7 +305,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);
> --
> 2.25.1
>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
BR, Jarkko
prev parent reply other threads:[~2026-09-09 22:40 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-03 3:58 [PATCH] tpm: fix off-by-four bounds check in tpm2_get_random() Jiangshan Yi
2026-09-09 22:40 ` Jarkko Sakkinen [this message]
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=aqHgTqQQDQhUFFno@kernel.org \
--to=jarkko@kernel.org \
--cc=13667453960@163.com \
--cc=James.Bottomley@hansenpartnership.com \
--cc=jgg@ziepe.ca \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=peterhuewe@gmx.de \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.