Re: [PATCH v2] tpm: loongson: bound the response by the shared buffer size

From: Qunqin Zhao

Date: Tue Dec 01 2026 - 22:11:02 EST



在 2026/12/2 09:19, Fang Xieyan 写道:
> @@ -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;

The hardware returns correct data and the transfer itself succeeds. 

The issue is that the DMA buffer allocated by the kernel is too small, 

hence -EIO should not be returned here. I guess we can do it like this:


@@ -20,9 +20,12 @@ 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;

-       if (cmd_ret->data_len > count)
+       if (data_len > TPM_BUFFSIZE)
                return -EIO;

+       if (count < data_len)
+               return -EINVAL;
+
        memcpy(buf, tpm_engine->data_buffer, cmd_ret->data_len);

        return cmd_ret->data_len;
@@ -58,6 +61,10 @@ 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)
+               return -ENOMEM;
+
        cmd = tpm_engine->command;
        cmd->cmd_id = SE_CMD_TPM;
        cmd->data_off = tpm_engine->buffer_off;

Qunqin,

Thanks.