* [PATCH v3] tpm: loongson: reject an undersized DMA window
@ 2026-12-02 11:44 Fang Xieyan
2026-12-02 12:00 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Fang Xieyan @ 2026-12-02 11:44 UTC (permalink / raw)
To: Qunqin Zhao, Peter Huewe, Jarkko Sakkinen
Cc: Jason Gunthorpe, Yinggang Gu, Lee Jones, Huacai Chen,
linux-crypto, linux-integrity, linux-kernel, stable, Lin Qiaojie
The Loongson Security Engine MFD driver splits the DMA region described by
the ACPI "dmam_size" property into 16 equal windows, so the TPM engine gets
dmam_size / 16 bytes for a response, while the TPM core passes recv() a
buffer of up to TPM_BUFSIZE (4096) bytes. A dmam_size below
16 * TPM_BUFSIZE therefore leaves the engine window too small for the
largest response the core can request.
tpm_loongson_recv() bounds the response against the caller's count only.
When the window is smaller than count, a response longer than the window
but no longer than count passes that check, and memcpy() reads past the end
of the TPM engine's window into the following engine windows of the shared
DMA region. The over-read bytes are then returned as response payload.
Reject such a configuration at probe time: if the TPM engine's window is
smaller than TPM_BUFSIZE, report the value and return -EINVAL instead of
registering a chip that cannot hold a valid response. Read data_len once
with READ_ONCE() as well, so the bounds check, the copy length and the
returned length all refer to the same value.
Fixes: 5c83b07df9c5 ("tpm: Add a driver for Loongson TPM device")
Suggested-by: Qunqin Zhao <zhaoqunqin@loongson.cn>
Cc: stable@vger.kernel.org
Reviewed-by: Lin Qiaojie <linqj@xiaopeng.com>
Signed-off-by: Fang Xieyan <fangxy@xiaopeng.com>
---
Changes v2 -> v3:
- Check the TPM engine's DMA window in probe and refuse to register the
chip when it cannot hold a TPM_BUFSIZE response, instead of reporting an
error on the receive path.
- Drop the buffer_size bound from tpm_loongson_recv(); the probe-time
invariant covers it.
- Subject and commit message now describe the DMA window sizing.
Base: d998a300b881 (mainline).
v2 discussion:
https://lore.kernel.org/all/20261202011913.65166-1-fangxy@xiaopeng.com/
Boundary analysis: the probe-time check establishes
buffer_size >= TPM_BUFSIZE before tpm_chip_register(), and
tpm_try_transmit() clamps the buffer handed to recv() to TPM_BUFSIZE, so
count <= TPM_BUFSIZE. Rejecting data_len > count then keeps data_len
within the TPM engine's own window, so memcpy() reads [0, data_len) from
inside that window. READ_ONCE() folds the former repeated loads of
cmd_ret->data_len into a single load.
Testing: builds with W=1 for ARCH=loongarch (defconfig plus
CONFIG_MFD_LOONGSON_SE=y and CONFIG_TCG_LOONGSON=y). No runtime test, as
no Loongson platform with a Security Engine is available.
drivers/char/tpm/tpm_loongson.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/drivers/char/tpm/tpm_loongson.c b/drivers/char/tpm/tpm_loongson.c
index 9e50250763d1..bbb131097d39 100644
--- a/drivers/char/tpm/tpm_loongson.c
+++ b/drivers/char/tpm/tpm_loongson.c
@@ -4,6 +4,7 @@
#include <linux/device.h>
#include <linux/mfd/loongson-se.h>
#include <linux/platform_device.h>
+#include <linux/tpm_command.h>
#include <linux/wait.h>
#include "tpm.h"
@@ -19,13 +20,14 @@ 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);
+ 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)
@@ -58,6 +60,13 @@ static int tpm_loongson_probe(struct platform_device *pdev)
tpm_engine = loongson_se_init_engine(dev->parent, SE_ENGINE_TPM);
if (!tpm_engine)
return -ENODEV;
+
+ if (tpm_engine->buffer_size < TPM_BUFSIZE) {
+ dev_err(dev, "TPM DMA window too small: %u < %u\n",
+ tpm_engine->buffer_size, TPM_BUFSIZE);
+ return -EINVAL;
+ }
+
cmd = tpm_engine->command;
cmd->cmd_id = SE_CMD_TPM;
cmd->data_off = tpm_engine->buffer_off;
--
2.50.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v3] tpm: loongson: reject an undersized DMA window
2026-12-02 11:44 [PATCH v3] tpm: loongson: reject an undersized DMA window Fang Xieyan
@ 2026-12-02 12:00 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 12:00 UTC (permalink / raw)
To: Fang Xieyan, Qunqin Zhao, Peter Huewe, Jarkko Sakkinen
Cc: Jason Gunthorpe, Yinggang Gu, Lee Jones, Huacai Chen,
linux-crypto, Lin Qiaojie
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] tpm: loongson: probe rejects 4KB DMA window due to 8KB
TPM_BUFSIZE
--
commit 99e627b732ef70357aa6e5acc30fcca7ecbd8581
Author: Fang Xieyan <fangxy@xiaopeng.com>
tpm: loongson: reject an undersized DMA window
This commit rejects Loongson TPM devices whose per-engine DMA window is
smaller than TPM_BUFSIZE at probe time and reads cmd_ret->data_len once
with READ_ONCE() in tpm_loongson_recv().
> diff --git a/drivers/char/tpm/tpm_loongson.c b/drivers/char/tpm/tpm_loongson.c
> index 9e50250763d16..bbb131097d39f 100644
> --- a/drivers/char/tpm/tpm_loongson.c
> +++ b/drivers/char/tpm/tpm_loongson.c
[ ... ]
> @@ -58,6 +60,13 @@ static int tpm_loongson_probe(struct platform_device *pdev)
> tpm_engine = loongson_se_init_engine(dev->parent, SE_ENGINE_TPM);
> if (!tpm_engine)
> return -ENODEV;
> +
> + if (tpm_engine->buffer_size < TPM_BUFSIZE) {
> + dev_err(dev, "TPM DMA window too small: %u < %u\n",
> + tpm_engine->buffer_size, TPM_BUFSIZE);
> + return -EINVAL;
> + }
> +
> cmd = tpm_engine->command;
> cmd->cmd_id = SE_CMD_TPM;
> cmd->data_off = tpm_engine->buffer_off;
[Severity: Medium]
Does checking tpm_engine->buffer_size < TPM_BUFSIZE cause a probe
regression on platforms with a 4096-byte per-engine DMA window (64 KiB
total dmam_size)?
The commit message assumes TPM_BUFSIZE is 4096, but commit 1e41cfc5549c
("tpm: Increase TPM_BUFSIZE to 8KB for chunking support") raised
TPM_BUFSIZE in include/linux/tpm_command.h to 8192 so the core can build
larger commands for transports with chunking while smaller fixed-window
transports enforce their own buffer limits in send() and recv().
Requiring buffer_size >= TPM_BUFSIZE here rejects 4 KiB windows with
-EINVAL and makes the count > tpm_engine->buffer_size check in
tpm_loongson_send() unreachable because tpm_try_transmit() already clamps
transfers to TPM_BUFSIZE.
Could tpm_loongson_recv() bound data_len against both count and
tpm_engine->buffer_size instead?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202114445.37551-1-fangxy@xiaopeng.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 12:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 11:44 [PATCH v3] tpm: loongson: reject an undersized DMA window Fang Xieyan
2026-12-02 12:00 ` sashiko-bot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.