Linux Integrity Measurement development
 help / color / mirror / Atom feed
* [PATCH] tpm: infineon: validate the received packet length
@ 2026-11-29 12:03 Fang Xieyan
  2026-11-29 12:11 ` Jarkko Sakkinen
  0 siblings, 1 reply; 2+ messages in thread
From: Fang Xieyan @ 2026-11-29 12:03 UTC (permalink / raw)
  To: Peter Huewe, Jarkko Sakkinen
  Cc: Jason Gunthorpe, linux-integrity, linux-kernel, stable

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>
---

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


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

end of thread, other threads:[~2026-11-29 12:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 12:03 [PATCH] tpm: infineon: validate the received packet length Fang Xieyan
2026-11-29 12:11 ` Jarkko Sakkinen

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