From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.28.76 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796174359; b=Db4DcrxW/jeMihhJAB+GAU/F+4hUQuUzoMDkFIj2pe2FIBMe4y1uhjJ2iR2wjsEr8acf QHO99QRASBtj0MPTOLE28ACRKJgmORRmdBnPFQw0piY4HWnfnwc/blMGgQUMFoWrOQmd2 DOWENIhGfAhrQSjwmuqBOdw89pJDE6KTSweCLRXr0R7zLjUT2E6YwiS5o2fGS+Zh4Znb1 0+462qd6lCCvXHit6t+UZ06YmzCUNJnEeoB7/brUGDcQMd3Ps1Wfc1skUZ8DqD1P7APLD LwLh0yvVW7TiKyP0v0M5vngYuFL2loDm45tllIs4kfcYh/Z4X8IkYE8UQ0ojST5hH0Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796174359; h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:From:To:Cc: Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=UHMH91WaWbavqIzagZe+TkfnL2CiZE9vXGDG4hsw3I8=; b=rBvlQMleu/XcV40ySdXMLKv1X1vd7ciH7xdUmnwEtPYe7FejQhgJ7zu3MqH1VI+hFVKc UuISFZ4jMzLQZ/6S5D6qhIWRPWmg2eDtDI0lKDgnnipaiXYgpbWbkIz6PLMrdvWcC7mme 1UzUvAgOMB53tkJVv2MH+Gb0CMfxouruk5gaTfdQuGh3gODs4rMcCxgpN907qYI2XUAvH X1n+8UJ0jMBENc8KLKuzVRlS4JH2mr6n9mzCMi9eC/iLmOZy8JRheCcs0U8Jznjw+rcfH uH2J9apC5ihUJttBt0GzolgyK9spyZVn6Vb7XX2RT81XS6KHLoG3iKurLkXcIRPLi8g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=xiaopeng.com; spf=pass smtp.mailfrom=xiaopeng.com; dkim=pass header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=SOMaWo5S; arc=none smtp.remote-ip=115.124.28.76 Received: from out28-76.mail.aliyun.com (out28-76.mail.aliyun.com [115.124.28.76]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D482F606A31A; Wed, 2 Dec 2026 01:19:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D482F606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=xiaopeng.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xiaopeng.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=SOMaWo5S DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=xiaopeng.com; s=default; t=1796174355; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=UHMH91WaWbavqIzagZe+TkfnL2CiZE9vXGDG4hsw3I8=; b=SOMaWo5SioYo4mTn8RP3TZO6RKGUKyRjLAyEVH5EWmrGtANOSCdqy5WMjjxkIRprWVvYzeWp3YSo4DmXaxWGd87o6HpOGXwo121EISq9fuE4gaCOlDUGpBaOjqu1Xx36sWtC+XKCugFM/GnEZ05pOqeEt4dQbc2qeUTpUn+ynzA= X-Alimail-AntiSpam:AC=CONTINUE;BC=0.04512517|-1;CH=green;DM=|CONTINUE|false|;DS=CONTINUE|ham_system_inform|0.00208775-0.0009193-0.996993;FP=11219675093824791770|0|0|0|0|-1|-1|-1;HT=maildocker-contentspam033037034184;MF=fangxy@xiaopeng.com;NM=1;PH=DS;RN=9;RT=9;SR=0;TI=SMTPD_---.kEXzHnX_1796174353; Received: from localhost.localdomain(mailfrom:fangxy@xiaopeng.com fp:SMTPD_---.kEXzHnX_1796174353 cluster:ay29) by smtp.aliyun-inc.com; Wed, 02 Dec 2026 09:19:14 +0800 From: Fang Xieyan To: Qunqin Zhao , Peter Huewe , Jarkko Sakkinen Cc: Jason Gunthorpe , linux-crypto@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Lin Qiaojie Subject: [PATCH v2] tpm: loongson: bound the response by the shared buffer size Date: Wed, 2 Dec 2026 09:19:13 +0800 Message-ID: <20261202011913.65166-1-fangxy@xiaopeng.com> X-Mailer: git-send-email 2.50.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Fang Xieyan --- 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