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 Cc: stable@vger.kernel.org Reviewed-by: Lin Qiaojie Signed-off-by: Fang Xieyan --- 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