From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.28.121 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796041256; b=k8UkL/bj5s3u7gKJqtRq8kiWZSPIRevyttAszFSdH3bCxxbA1EerhNscQbo+i8q3f8d9 QQ9gUdKxXxbZ2VRYKae41Nw/KJ5/YWzKxCct1ROx2dr7dnSSPehesTjkKDiO4u/zy9XOc 2YJyCFLqP9CnsjET0/mCsD7gSZpIXIYrLy+kTQEUxloAaO/nvEVZh94FidYc8Jv/aYfPQ 8a5BNO8c9+JIMw0xJB896efBl9AMR9CzJOB70zcKNKlBbO3UiSV1VPCrcjWiL/vFHqiZY jgO1pi9n8IPCQHpVhFojlNCJ4yuQn1CDbZ5LQxhHzEaw1nz+0nQS+hgv67VtpHdi8iw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796041256; h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:From:To:Cc: Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=sJalg/XjnrPqkp3Psa5wUXGDvNXfihEepYjiWDdJVLk=; b=G+4V/6IFD86sSOU7w/AFv+LPExkm2lJWqJlLo33rCWcS/9SrNL118gsWBciMymxUlvHp /fBr4+5UK/ZLxBFGU71S8FOw8Foy97SVesDIFMAyxadTAs1m1Ho6VtXTgYCc0NVAaA9uW W/y8vu2OwtGQV9spFt66oUp1wZbmJjU7Q+gujq+Cqk84U6TTCinA9B8YvvSJIqdw9y9c8 PlYTrD6lr1ZsHmHADPCUfHUPV+mRORc1FozGsYL7mOjyLmHmpW44igJaR7tdkLvpgsLAe Y5gWWT8SWaL/gHUlRsatDkqSGVi7wJp2q/tdjx3fuyn3IxExf+ATNU4FSPNkxbzqeUA== 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=QWXhFWcl; arc=none smtp.remote-ip=115.124.28.121 Received: from out28-121.mail.aliyun.com (out28-121.mail.aliyun.com [115.124.28.121]) (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 0C9FD606A31C; Mon, 30 Nov 2026 12:20:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0C9FD606A31C 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=QWXhFWcl DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=xiaopeng.com; s=default; t=1796041243; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=sJalg/XjnrPqkp3Psa5wUXGDvNXfihEepYjiWDdJVLk=; b=QWXhFWclxP3/Sk3lXAwxU5smjf4izKh8asGJtqW/UQuU1sPqtYFb7WVInaZsZXfK2bOgnHACMgMfkRHcCmoenjcN2ASoi/eXwQU9zZx5+easLXEC6bRW79q+vIU6BwUMgZah4Yd3XPgizTF8n91SGFn241IPBsW9UhmQ9lvLUfs= X-Alimail-AntiSpam:AC=CONTINUE;BC=0.08013224|-1;CH=green;DM=|CONTINUE|false|;DS=CONTINUE|ham_regular_dialog|0.00382008-0.000230627-0.995949;FP=10318982175037932762|0|0|0|0|-1|-1|-1;HT=maildocker-contentspam033037034017;MF=fangxy@xiaopeng.com;NM=1;PH=DS;RN=9;RT=9;SR=0;TI=SMTPD_---.kDNmOy7_1796041241; Received: from localhost.localdomain(mailfrom:fangxy@xiaopeng.com fp:SMTPD_---.kDNmOy7_1796041241 cluster:ay29) by smtp.aliyun-inc.com; Mon, 30 Nov 2026 20:20:42 +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] tpm: loongson: bound the response by the shared buffer size Date: Mon, 30 Nov 2026 20:20:41 +0800 Message-ID: <20261130122041.59360-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. 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 --- Base: d998a300b881 (mainline). The receive path is unchanged from the code introduced by 5c83b07df9c5. Boundary analysis: with the fix, data_len <= buffer_size and data_len <= count both 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. 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 (CC=clang LLVM=1, loongson64 defconfig) with the fix applied. drivers/char/tpm/tpm_loongson.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/char/tpm/tpm_loongson.c b/drivers/char/tpm/tpm_loongson.c index 9e50250763d1..c58cf04115d9 100644 --- a/drivers/char/tpm/tpm_loongson.c +++ b/drivers/char/tpm/tpm_loongson.c @@ -23,6 +23,9 @@ static int tpm_loongson_recv(struct tpm_chip *chip, u8 *buf, size_t count) if (cmd_ret->data_len > count) return -EIO; + if (cmd_ret->data_len > tpm_engine->buffer_size) + return -EIO; + memcpy(buf, tpm_engine->data_buffer, cmd_ret->data_len); return cmd_ret->data_len; -- 2.50.1