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. In addition, data_len sits in DMA-coherent memory the engine can rewrite; without a single-read discipline, the value that passes the bounds checks and the value that reaches memcpy() or the return can differ. Read data_len once with READ_ONCE() so the bounds checks, the copy length and the returned value refer to the same number, and 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 Signed-off-by: Fang Xieyan --- Changes v1 -> v2: - Read cmd_ret->data_len once via READ_ONCE() into a local so the two bounds checks, the memcpy() length and the return value all operate on the same number; the field lives in DMA-coherent memory the SE engine can rewrite between reads (sashiko-bot, Medium). - Commit message: third paragraph now describes both actions; second paragraph notes the double-fetch consequence alongside the over-read. Base: d998a300b881 (mainline). v1 discussion: https://lore.kernel.org/all/20261130122041.59360-1-fangxy@xiaopeng.com/ sashiko review: https://sashiko.dev/#/patchset/20261130122041.59360-1-fangxy@xiaopeng.com?part=1 The receive path is unchanged from the code introduced by 5c83b07df9c5. Boundary analysis: data_len is loaded once from the shared DMA reply region via READ_ONCE(), so both bounds checks, memcpy() and the return value refer to the same number; the engine rewriting cmd_ret->data_len after that load cannot cause a value that passed a check to differ from the value used at the sink. With the checks in place, data_len <= count and data_len <= buffer_size 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. READ_ONCE() addresses the CPU-side repeated load of the length field; it does not stop the engine from rewriting data_buffer contents during the copy, which is outside the scope of this fix. 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 with W=1 (loongson64 defconfig) with the fix applied, and no new warnings appear. drivers/char/tpm/tpm_loongson.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/char/tpm/tpm_loongson.c b/drivers/char/tpm/tpm_loongson.c index 9e50250763d1..2dc3c9270873 100644 --- a/drivers/char/tpm/tpm_loongson.c +++ b/drivers/char/tpm/tpm_loongson.c @@ -19,13 +19,17 @@ static int tpm_loongson_recv(struct tpm_chip *chip, u8 *buf, size_t count) { struct loongson_se_engine *tpm_engine = dev_get_drvdata(&chip->dev); struct tpm_loongson_cmd *cmd_ret = tpm_engine->command_ret; + u32 data_len = READ_ONCE(cmd_ret->data_len); - if (cmd_ret->data_len > count) + if (data_len > count) return -EIO; - memcpy(buf, tpm_engine->data_buffer, cmd_ret->data_len); + if (data_len > tpm_engine->buffer_size) + return -EIO; + + memcpy(buf, tpm_engine->data_buffer, data_len); - return cmd_ret->data_len; + return data_len; } static int tpm_loongson_send(struct tpm_chip *chip, u8 *buf, size_t bufsiz, size_t count) -- 2.50.1