Linux cryptographic layer development
 help / color / mirror / Atom feed
* [PATCH v4] tpm: loongson: bound the response by the DMA window
@ 2026-12-04 15:52 Fang Xieyan
  2026-12-04 16:10 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Fang Xieyan @ 2026-12-04 15:52 UTC (permalink / raw)
  To: Qunqin Zhao, Peter Huewe, Jarkko Sakkinen
  Cc: Jason Gunthorpe, Lee Jones, Huacai Chen, Yinggang Gu,
	linux-crypto, linux-integrity, linux-kernel, stable, Lin Qiaojie

The Security Engine MFD driver splits the ACPI "dmam_size" DMA region
into SE_ENGINE_MAX (16) equal windows, so the TPM engine holds at most
dmam_size / 16 bytes of response, while the TPM core sizes the recv()
buffer from TPM_BUFSIZE and may raise that constant.

tpm_loongson_recv() only bounds the response against count, so a reply
longer than the engine window but no longer than count makes memcpy()
read into the neighbouring engine windows of the shared DMA region.

Bound the response by the engine window as well and report -EIO when the
engine returns more than it can hold.  Publish the window size in the
first reserved word of tpm_loongson_cmd, so the limit is also available
to the engine, and load data_len once with READ_ONCE() so the checks,
the copy length and the returned length use 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 v3 -> v4:
- Drop the probe-time rejection; it would stop probing a working platform
  once TPM_BUFSIZE grows past the engine window.
- Bound the response by the engine window on the receive path again.
- Publish the window size in the first reserved word of tpm_loongson_cmd;
  the structure keeps its size and layout.

Base: d998a300b881 (mainline).

v3 discussion:
  https://lore.kernel.org/all/20261202114445.37551-1-fangxy@xiaopeng.com/

Boundary analysis: data_len is loaded once and must not exceed either
count or buffer_size, so memcpy() reads [0, data_len) from inside the
engine window and writes [0, data_len) inside the caller's buffer.

Testing: builds with W=1 for ARCH=loongarch with CONFIG_MFD_LOONGSON_SE
and CONFIG_TCG_LOONGSON enabled.  No runtime test, as no Loongson
platform with a Security Engine is available.

 drivers/char/tpm/tpm_loongson.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/drivers/char/tpm/tpm_loongson.c b/drivers/char/tpm/tpm_loongson.c
index 9e50250763d1..59f3947bec5d 100644
--- a/drivers/char/tpm/tpm_loongson.c
+++ b/drivers/char/tpm/tpm_loongson.c
@@ -12,20 +12,22 @@ struct tpm_loongson_cmd {
 	u32 cmd_id;
 	u32 data_off;
 	u32 data_len;
-	u32 pad[5];
+	u32 buffer_size;
+	u32 pad[4];
 };
 
 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 || data_len > tpm_engine->buffer_size)
 		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)
@@ -61,6 +63,7 @@ static int tpm_loongson_probe(struct platform_device *pdev)
 	cmd = tpm_engine->command;
 	cmd->cmd_id = SE_CMD_TPM;
 	cmd->data_off = tpm_engine->buffer_off;
+	cmd->buffer_size = tpm_engine->buffer_size;
 
 	chip = tpmm_chip_alloc(dev, &tpm_loongson_ops);
 	if (IS_ERR(chip))
-- 
2.50.1


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

* Re: [PATCH v4] tpm: loongson: bound the response by the DMA window
  2026-12-04 15:52 [PATCH v4] tpm: loongson: bound the response by the DMA window Fang Xieyan
@ 2026-12-04 16:10 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 16:10 UTC (permalink / raw)
  To: Fang Xieyan, Qunqin Zhao, Peter Huewe, Jarkko Sakkinen
  Cc: Jason Gunthorpe, Lee Jones, Huacai Chen, Yinggang Gu,
	linux-crypto, Lin Qiaojie

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] tpm: loongson: untested firmware ABI change in
  tpm_loongson_probe()
--

commit 4578f644ae7983bb4262bd7e41b5d35fe9618363
Author: Fang Xieyan <fangxy@xiaopeng.com>

tpm: loongson: bound the response by the DMA window

This commit bounds the response length read by tpm_loongson_recv() against
the engine's DMA buffer size using READ_ONCE(), and writes buffer_size into
the first reserved word of struct tpm_loongson_cmd during
tpm_loongson_probe().

> diff --git a/drivers/char/tpm/tpm_loongson.c b/drivers/char/tpm/tpm_loongson.c
> index 9e50250763d16..59f3947bec5d5 100644
> --- a/drivers/char/tpm/tpm_loongson.c
> +++ b/drivers/char/tpm/tpm_loongson.c
> @@ -12,20 +12,22 @@ struct tpm_loongson_cmd {
>  	u32 cmd_id;
>  	u32 data_off;
>  	u32 data_len;
> -	u32 pad[5];
> +	u32 buffer_size;
> +	u32 pad[4];
>  };

[ ... ]

> @@ -61,6 +63,7 @@ static int tpm_loongson_probe(struct platform_device *pdev)
>  	cmd = tpm_engine->command;
>  	cmd->cmd_id = SE_CMD_TPM;
>  	cmd->data_off = tpm_engine->buffer_off;
> +	cmd->buffer_size = tpm_engine->buffer_size;

[Severity: Medium]
Can writing a non-zero value to cmd->buffer_size break deployed Security
Engine firmware that expects reserved words in struct tpm_loongson_cmd to
be zero?

loongson_se_probe() allocates the shared DMA region with
dmam_alloc_coherent(), so this word was previously always zero and no
firmware version check guards it.  If existing firmware rejects non-zero
reserved fields, TPM command execution will break on existing platforms.

Because tpm_loongson_recv() already prevents the out-of-bounds read on the
host side without cmd->buffer_size, and this patch targets stable without
hardware testing, splitting the cmd->buffer_size change into a separate
patch tested against deployed firmware would avoid that regression risk.

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

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

end of thread, other threads:[~2026-12-04 16:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 15:52 [PATCH v4] tpm: loongson: bound the response by the DMA window Fang Xieyan
2026-12-04 16:10 ` sashiko-bot

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