From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795954316; b=T2riIlczjzeR9GaXH4dgr/kB436+eH/5ShbUqHtMMTJ/gz6qx1rn053t2Myp7rT8TW6X EaHbpRVVL0MDUmwBgEc9tAOBj8puz4hf8m5kZqsnjzfrxMPuwnEH3p6xcoAT3y6q588Oc MttONdtXp022g/mBAmk3l3vC20Fb6r5iZLcedofibtbXBAhoPNRHuXXRtW/ZU/Huz01Wc /cy+zQQNjm+KFxiaLgRlSWW9e2YDFNXeXnygGhiohJqqv5wUbLbPBiS0bzS5dr4L3NWHr 9f4xNJRpEszEWMf7Gt2JZ75E3R651a6sjRlcg/0c8SeHGd7fUxCetKs1dtM4Hm/sPtQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795954316; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=GL9ZQIciL/jlXu7RuM3XBKX/XWoKUtvWohXT5TyWn08=; b=K2yriRcU//mGOvAlR2maXh3jOPBQKf9v+Q+OKHtlZ7cSxRTDq+l0ZhTOOpVfNC7GqoWy nIoXGmnzz9ss/9qlStIPUv4j8SfrB1tOQIeIbgaTT0ZmS8xkSZox2tj8h0MxIqwLXApRs oH436fsKfv4k3aCV+LdfHKAaEWS+P0P1x0K64ITTvqL1nZyDdjj3VLfs81ameFIsovqVL p+c0JSO0zdVBY3aBwHypL3eprY54J7pDg8hVDXs1FSO6S0jcScsdPqtQn57QhoE6U3cR0 cXs/i0mgMztpf5b/LQocUvKMJHzWn3cas8KEn9BCaAC/iPdo9kFrAvqM3/6rZBUqVWQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZbDd4zjK; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id 4AD36606A31C; Sun, 29 Nov 2026 12:11:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZbDd4zjK Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 028501F00898; Sun, 29 Nov 2026 12:11:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795954315; bh=GL9ZQIciL/jlXu7RuM3XBKX/XWoKUtvWohXT5TyWn08=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=ZbDd4zjK5d8POFUvQwijvACZ5dvHSnCHAedkR9oD3JjRWxkW0ah3T5UCTx5rd/CON mp3oBLVckdECYFb8rw5YIwvU3IOzLoNNcbqm1OP2L32ryvXXPN6eB1BdrT58EnYMzN e32tm0auXjx6TxNExLZdQ46pOpyxr2GHZ1DxdyUwhMFM6NtlV2oiIytasrfIHHskpz sDFQOPR/8RNJllkGV6mqEHqU0LhBRMKKV2MQwGYXKZy5k3kLiSdX6Ij4NMmaFDOmVv VbTuZvvZjUAmk6PzhRnjKtp600hwLEeeMcSRFW3+6HQgUjTzS5rYJbEJaV5lPThlY9 AhSJ2UVBKu2xQ== Date: Sun, 29 Nov 2026 14:11:51 +0200 From: Jarkko Sakkinen To: Fang Xieyan Cc: Peter Huewe , Jason Gunthorpe , linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] tpm: infineon: validate the received packet length Message-ID: References: <20261129120315.83926-1-fangxy@xiaopeng.com> X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 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 Br, Jarkko