Linux cryptographic layer development
 help / color / mirror / Atom feed
* [PATCH] tpm: loongson: bound the response by the shared buffer size
@ 2026-11-30 12:20 Fang Xieyan
  2026-11-30 12:28 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Fang Xieyan @ 2026-11-30 12:20 UTC (permalink / raw)
  To: Qunqin Zhao, Peter Huewe, Jarkko Sakkinen
  Cc: Jason Gunthorpe, linux-crypto, linux-integrity, linux-kernel,
	stable, Lin Qiaojie

tpm_loongson_recv() reads the response length from the SE engine's
command_ret->data_len and copies that many bytes out of the engine's
shared data buffer into the caller's buffer.  The length is checked
against the caller-supplied count but not against
tpm_engine->buffer_size, which is the size of the source window the
copy reads from.  A faulty or compromised engine can report a length
larger than its own window.

A reported data_len above buffer_size makes memcpy() read past the
end of the TPM engine's DMA window into subsequent portions of the
shared SE DMA region.  The over-read bytes are copied into the
caller's response buffer, and the returned length makes the TPM
core treat them as response payload.  Large values can also run off
the end of the entire DMA region.

Bound data_len against tpm_engine->buffer_size on the receive path,
matching the check the send path already applies against the same
buffer.

Fixes: 5c83b07df9c5 ("tpm: Add a driver for Loongson TPM device")
Cc: stable@vger.kernel.org
Reviewed-by: Lin Qiaojie <linqj@xiaopeng.com>
Signed-off-by: Fang Xieyan <fangxy@xiaopeng.com>
---

Base: d998a300b881 (mainline).

The receive path is unchanged from the code introduced by
5c83b07df9c5.

Boundary analysis: with the fix, data_len <= buffer_size and
data_len <= count both hold before memcpy() runs, so the copy reads
[0, data_len) inside the TPM engine's own DMA window and writes
[0, data_len) inside the caller's buffer.

Testing: the over-read source (tpm_engine->data_buffer) is
dmam_alloc_coherent() memory rather than a KASAN-tracked slab, so a
KASAN harness would only fire by mocking the buffer with kmalloc(),
which would be weaker evidence than the real allocation path.  No
QEMU device model of the Loongson Security Engine exists.  Verified
by the static bound analysis above; the driver builds cleanly for
ARCH=loongarch (CC=clang LLVM=1, loongson64 defconfig) with the fix
applied.
 drivers/char/tpm/tpm_loongson.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/char/tpm/tpm_loongson.c b/drivers/char/tpm/tpm_loongson.c
index 9e50250763d1..c58cf04115d9 100644
--- a/drivers/char/tpm/tpm_loongson.c
+++ b/drivers/char/tpm/tpm_loongson.c
@@ -23,6 +23,9 @@ static int tpm_loongson_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 	if (cmd_ret->data_len > count)
 		return -EIO;
 
+	if (cmd_ret->data_len > tpm_engine->buffer_size)
+		return -EIO;
+
 	memcpy(buf, tpm_engine->data_buffer, cmd_ret->data_len);
 
 	return cmd_ret->data_len;
-- 
2.50.1


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

* Re: [PATCH] tpm: loongson: bound the response by the shared buffer size
  2026-11-30 12:20 [PATCH] tpm: loongson: bound the response by the shared buffer size Fang Xieyan
@ 2026-11-30 12:28 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 12:28 UTC (permalink / raw)
  To: Fang Xieyan, Qunqin Zhao, Peter Huewe, Jarkko Sakkinen
  Cc: Jason Gunthorpe, linux-crypto, Lin Qiaojie

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] tpm: loongson: double-fetch of cmd_ret->data_len in
  tpm_loongson_recv()
--

commit 320be338222fef4657828ffd0cbc0aa310c881af
Author: Fang Xieyan <fangxy@xiaopeng.com>

tpm: loongson: bound the response by the shared buffer size

This commit bounds cmd_ret->data_len against tpm_engine->buffer_size in
tpm_loongson_recv() to prevent reading past the TPM engine's shared DMA
buffer when a faulty or compromised Security Engine reports an out-of-bounds
response length.

> diff --git a/drivers/char/tpm/tpm_loongson.c b/drivers/char/tpm/tpm_loongson.c
> index 9e50250763d16..c58cf04115d94 100644
> --- a/drivers/char/tpm/tpm_loongson.c
> +++ b/drivers/char/tpm/tpm_loongson.c
> @@ -23,6 +23,9 @@ static int tpm_loongson_recv(struct tpm_chip *chip, u8 *buf, size_t count)
>  	if (cmd_ret->data_len > count)
>  		return -EIO;
>  
> +	if (cmd_ret->data_len > tpm_engine->buffer_size)
> +		return -EIO;
> +
>  	memcpy(buf, tpm_engine->data_buffer, cmd_ret->data_len);
>  
>  	return cmd_ret->data_len;

[Severity: Medium]
Can the Security Engine bypass these bounds checks by modifying
cmd_ret->data_len in shared DMA memory between the checks, memcpy(), and the
return statement?

tpm_engine->command_ret points into DMA-coherent memory shared with the
engine (se->dmam_base in loongson_se_init_engine()), and cmd_ret->data_len
is read four separate times without READ_ONCE().  Reading cmd_ret->data_len
once into a local variable with READ_ONCE() and using that variable for both
checks, memcpy(), and the return value would avoid a double-fetch.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130122041.59360-1-fangxy@xiaopeng.com?part=1

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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 12:20 [PATCH] tpm: loongson: bound the response by the shared buffer size Fang Xieyan
2026-11-30 12:28 ` sashiko-bot

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