From: Jarkko Sakkinen <jarkko@kernel.org>
To: Fang Xieyan <fangxy@xiaopeng.com>
Cc: Peter Huewe <peterhuewe@gmx.de>, Jason Gunthorpe <jgg@ziepe.ca>,
linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] tpm: infineon: validate the received packet length
Date: Sun, 29 Nov 2026 14:11:51 +0200 [thread overview]
Message-ID: <awwWh20X8cROHuYf@kernel.org> (raw)
In-Reply-To: <20261129120315.83926-1-fangxy@xiaopeng.com>
On Sun, Nov 29, 2026 at 08:03:15PM +0800, Fang Xieyan wrote:
> tpm_inf_recv() takes the packet length from the four-byte Infineon
> transport header and uses it as the bound of both the receive loop
> that fills the caller's buffer from the FIFO and the copy loop that
> moves the payload to the start of the buffer, stripping the six-byte
> data header. Neither loop is bounded by the caller-supplied count,
> and a length below six is also accepted.
>
> A device that reports a size larger than count writes FIFO bytes
> past the end of the response buffer during the receive loop, and
> the copy loop reads past the buffer even when size fits because
> it dereferences buf[i + 6] for every i < size. A size below six
> underflows the u32 accumulator at "size - 6" and returns a
> nonsensical length.
>
> Reject a packet whose size is smaller than the six-byte data
> header or larger than the caller's buffer, and bound the copy
> loop by size - 6 so it only moves the payload returned to the
> caller.
>
> Fixes: ebb81fdb3dd0 ("[PATCH] tpm: Support for Infineon TPM")
> Cc: stable@vger.kernel.org
> Signed-off-by: Fang Xieyan <fangxy@xiaopeng.com>
Thanks for doing a proper commit message; instead of commit slop
message, which too usual these days :-) You start to appreciate
them when you see these.
It's easy to read, understand and move upwards. That's awesome.
> ---
>
> Base: mainline d998a300b881. drivers/char/tpm/tpm_infineon.c
> receive path unchanged since Fixes:; no upstream fix for this sink
> has been merged.
>
> Boundary analysis with the fix (6 <= size <= count):
> - receive loop writes buf[0..size-1] (<= count)
> - copy loop reads buf[6..size-1], writes
> buf[0..size-7] (in-bounds)
> - return value is size - 6 (>= 0)
> size < 6 and size > count both return -EIO. Previously size >
> count OOB-wrote attacker-controlled FIFO bytes past the response
> buffer, and size < 6 underflowed the u32 accumulator.
>
> Testing: no QEMU model of the parallel Infineon TPM 1.2, so a
> DEBUG-ONLY hook was added to tpm_infineon.c to feed canned FIFO
> bytes into tpm_inf_recv() from a late_initcall. Built with
> CONFIG_KASAN=y CONFIG_TCG_INFINEON=y; the response buffer is a
> plain kmalloc so KASAN tracks its slab redzone.
>
> baseline (d998a300b881 + hook, fix reverted):
> size=512 count=128 -> BUG: KASAN: slab-out-of-bounds, Write of
> size 1 in tpm_inf_recv, "0 bytes to the right of allocated
> 128-byte region [ff110000042fe700, ff110000042fe780)";
> returns 506.
> size=3 count=128 -> returns -3 (u32 underflow of size - 6).
> size=128 count=128 -> returns 122.
> size=100 count=128 -> returns 94.
>
> patched (d998a300b881 + fix + hook):
> size=512 count=128 -> returns -EIO, no splat.
> size=3 count=128 -> returns -EIO.
> size=128 count=128 -> returns 122 (unchanged).
> size=100 count=128 -> returns 94 (unchanged).
> drivers/char/tpm/tpm_infineon.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/char/tpm/tpm_infineon.c b/drivers/char/tpm/tpm_infineon.c
> index 7638b65b851b..cd873b3a1243 100644
> --- a/drivers/char/tpm/tpm_infineon.c
> +++ b/drivers/char/tpm/tpm_infineon.c
> @@ -268,6 +268,9 @@ static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)
> /* size of the data received */
> size = ((buf[2] << 8) | buf[3]);
>
> + if (size < 6 || size > count)
> + return -EIO;
> +
> for (i = 0; i < size; i++) {
> wait(chip, STAT_RDA);
> buf[i] = tpm_data_in(RDFIFO);
> @@ -278,7 +281,7 @@ static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)
> return -EIO;
> }
>
> - for (i = 0; i < size; i++)
> + for (i = 0; i < size - 6; i++)
> buf[i] = buf[i + 6];
>
> size = size - 6;
> --
> 2.50.1
>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Br, Jarkko
prev parent reply other threads:[~2026-11-29 12:11 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 12:03 Fang Xieyan
2026-11-29 12:11 ` 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=awwWh20X8cROHuYf@kernel.org \
--to=jarkko@kernel.org \
--cc=fangxy@xiaopeng.com \
--cc=jgg@ziepe.ca \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=peterhuewe@gmx.de \
--cc=stable@vger.kernel.org \
/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
all inboxes | Powered by JetHome®