* [PATCH] tpm: loongson: bound the response by the shared buffer size
@ 2026-11-30 12:20 Fang Xieyan
0 siblings, 0 replies; only message 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 [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-30 12:20 UTC | newest]
Thread overview: (only message) (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
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®