From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.28.4 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796127799; b=t+5JZzxVxiAXl3549ggJOgo5TBQaO12nc/Xn+yqqMuGTPZ073EU+m90W9WVzAWTkrb26 uDKTparDG0xtDx1Go6ToAGt+RonZLKKdwebiKynDUmpRqsAVutHxwPa762hQVNEFtL7Z8 pEXF/XZG+f1qalEqGXlX7hBSn1m2AtXmo10Ymz2JZJ2DbDuEol8oivZy4GX8G+Rv0r4Jl x1QxJi3Cspl9Nsy1wKQlKnskaP8hlDC9oz5xtDJGslCfcjjefiziDSRP6p0+M91RAQZKz FGFLc6HSyfYHkdtb5jSd9azGoL5JHL4t93KHdcHW8fZFgDEPBYiZJdr1Bon7prtKjHg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796127799; h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:From:To:Cc: Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=QSPlQkBOE9U2SsynXXh46t2uqBTe/uCZK9BsAJJMeL0=; b=QH5BMSDdgbVko+4z75n9h2L4br66QljiiEUFBTOEEciRtwjpW/kFBscHGSLRl3HJ6ZKt +McFjhgWZz1eZoeLeXfM8zvZ9I5JEAfB/NMf+scF5QFovj4Bxh7P3RiS7rTcil0whuNFA YcbF3fiuEyC+bAB8EYiqLOpAhbjY3DIORKWt56j8az06daRLmtS4Fc9oCQXe/6tbpCZtP Gkj2sE9J1JIrXbXel+Knub2LU1+7F9J/Iyzb8X5O+PXZp7qHDuxjpMRsE50bzfkqqJf9c QTGEQKeBTWK7sN5eTsOYI38Eyy9xBO+I8vDBIOgA3L8MUVGYaAEq3dhEfXsdNAJbybA== 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=CaRx2EZc; arc=none smtp.remote-ip=115.124.28.4 Received: from out28-4.mail.aliyun.com (out28-4.mail.aliyun.com [115.124.28.4]) (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 31942606A329; Tue, 1 Dec 2026 12:23:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 31942606A329 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) header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=CaRx2EZc DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=xiaopeng.com; s=default; t=1796127796; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=QSPlQkBOE9U2SsynXXh46t2uqBTe/uCZK9BsAJJMeL0=; b=CaRx2EZcg0T1YMtjMz4S2lTZsE11HeBpkmJkWt7w28K5t8jiOLPaHf8Jf1aeDe8aA649H5vn5elPh3oaLTidUMmY0W/pPttWBc+/ya+T2D7mUdrGs5mgaGjvWFLY9a03g6yHK+5XLBt2jtdzQ5Cl1nFo1rBqKvYVMtkZVsE45R8= X-Alimail-AntiSpam:AC=CONTINUE;BC=0.7909632|0.1195808;CH=green;DM=|AD|false|;DS=CONTINUE|ham_alarm|0.0216375-0.00053331-0.977829;FP=12633834822436521010|0|0|0|0|-1|-1|-1;HT=maildocker-contentspam033045023102;MF=zhugl3@xiaopeng.com;NM=1;PH=DS;RN=5;RT=5;SR=0;TI=SMTPD_---.kEBT741_1796127795; Received: from DESKTOP-UL5U09E.xiaopeng.local(mailfrom:zhugl3@xiaopeng.com fp:SMTPD_---.kEBT741_1796127795 cluster:ay29) by smtp.aliyun-inc.com; Tue, 01 Dec 2026 20:23:15 +0800 From: Guanglei Zhu To: dmitry.torokhov@gmail.com Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, liuc63@xiaopeng.com, stable@vger.kernel.org Subject: [PATCH v3 1/4] Input: adi - reject stream merges that do not fit the buffer Date: Tue, 1 Dec 2026 20:23:11 +0800 Message-ID: <20261201122314.361354-2-zhugl3@xiaopeng.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261201122314.361354-1-zhugl3@xiaopeng.com> References: <20261103084135.426629-3-zhugl3@xiaopeng.com> <20261201122314.361354-1-zhugl3@xiaopeng.com> 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 adi_move_bits() appends the second half of a two stream packet to the first at an offset taken from the 10-bit length field of the ID packet: for (i = 1; i <= adi[1].ret; i++) adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i]; Nothing compares the result with the size of the buffer. adi[0].data is the last member of struct adi and struct adi_port holds two of them back to back, so a large length field makes the loop write over the second struct adi and past the allocation, and the inflated ret then makes adi_get_bits() read past the buffer as well. A zero length, which the connect path can pass before the field has been validated, folds the second packet onto the first one instead. Reject a merge that does not fit, and compute the offset once. Use >= for the combined length so adi_get_bits() cannot walk to data[ret] with ret == ADI_MAX_LENGTH. Invalidate both half streams on the rejection so that the unmerged packets are not decoded as standalone reads. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Signed-off-by: Guanglei Zhu --- Tested with a user space replay of adi_move_bits(), built with ASan. On master a length of 1023 with a 200 byte first stream and a 255 byte second one writes at data[513..767]; struct adi_port is 912 bytes and adi[0].data sits at offset 200, so ASan reports a heap-buffer-overflow. A length of 0 moves the second stream to data[1..4], on top of the first one. With this patch both cases leave both streams at ret = -1 and the buffer untouched. drivers/input/joystick/adi.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/drivers/input/joystick/adi.c b/drivers/input/joystick/adi.c index e07b719..026f7de 100644 --- a/drivers/input/joystick/adi.c +++ b/drivers/input/joystick/adi.c @@ -161,7 +161,7 @@ static void adi_read_packet(struct adi_port *port) static void adi_move_bits(struct adi_port *port, int length) { - int i; + int i, offset; struct adi *adi = port->adi; adi[0].idx = adi[1].idx = 0; @@ -169,11 +169,24 @@ static void adi_move_bits(struct adi_port *port, int length) if (adi[0].ret <= 0 || adi[1].ret <= 0) return; if (adi[0].data[0] & 0x20 || ~adi[1].data[0] & 0x20) return; + if (length <= 0) + goto invalid; + + offset = ((length - 1) >> 1) + 1; + if (offset + adi[1].ret >= ADI_MAX_LENGTH || + adi[0].ret + adi[1].ret >= ADI_MAX_LENGTH) + goto invalid; + for (i = 1; i <= adi[1].ret; i++) - adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i]; + adi[0].data[offset + i] = adi[1].data[i]; adi[0].ret += adi[1].ret; adi[1].ret = -1; + return; + +invalid: + adi[0].ret = -1; + adi[1].ret = -1; } /* -- 2.43.0