mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Fang Xieyan <fangxy@xiaopeng.com>
To: Qunqin Zhao <zhaoqunqin@loongson.cn>,
	Peter Huewe <peterhuewe@gmx.de>,
	Jarkko Sakkinen <jarkko@kernel.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>,
	linux-crypto@vger.kernel.org, linux-integrity@vger.kernel.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	Lin Qiaojie <linqj@xiaopeng.com>
Subject: [PATCH v2] tpm: loongson: bound the response by the shared buffer size
Date: Wed,  2 Dec 2026 09:19:13 +0800	[thread overview]
Message-ID: <20261202011913.65166-1-fangxy@xiaopeng.com> (raw)

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 <linqj@xiaopeng.com>
Signed-off-by: Fang Xieyan <fangxy@xiaopeng.com>
---
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


                 reply	other threads:[~2026-12-02  1:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202011913.65166-1-fangxy@xiaopeng.com \
    --to=fangxy@xiaopeng.com \
    --cc=jarkko@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=linqj@xiaopeng.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterhuewe@gmx.de \
    --cc=stable@vger.kernel.org \
    --cc=zhaoqunqin@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®