Linux Input/HID development
 help / color / mirror / Atom feed
From: Guanglei Zhu <zhugl3@xiaopeng.com>
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 v2] Input: cyapa_gen3 - reject reports with too many fingers
Date: Tue,  1 Dec 2026 20:23:09 +0800	[thread overview]
Message-ID: <20261201122309.361277-1-zhugl3@xiaopeng.com> (raw)
In-Reply-To: <20261103084135.426629-8-zhugl3@xiaopeng.com>

The finger count is taken from the top nibble of a status byte the
trackpad firmware fills in, and used to walk the touch array:

        num_fingers = (data->finger_btn >> 4) & 0x0f;
        for (i = 0; i < num_fingers; i++) {
                const struct cyapa_touch *touch = &data->touches[i];

The nibble allows up to fifteen fingers, but the array holds the five
touches the chip is documented to report, so a packet that claims more
makes the driver report coordinates read from the stack past the
register block.

Clamping the count was my first attempt.  A packet that reports more
fingers than the chip has is corrupted, though, and reporting five
bogus touches to userspace is not better than rejecting it.  A bus
read that fails and returns 0xff for the whole report block is one way
in: it sets OP_STATUS_SRC, CYAPA_DEV_NORMAL and OP_DATA_VALID at once,
so the existing header check lets it through.

Reject such a report in the header validation of both the interrupt
and the poll path.  The driver already reinitializes the device when it
sees a bad state, so returning -EINVAL from these helpers takes that
path.  Size the array from GEN3_MAX_FINGERS, and read the count
through GEN3_FINGER_NUM() in the event path as well: the macro masked
with 0x07 while the event path masked with 0x0f, and under the old
mask a count between 8 and 15 folded back into the valid range.

Fixes: 9f1cd857518f ("Input: cyapa - re-design driver to support multi-trackpad in one driver")
Cc: stable@vger.kernel.org
Signed-off-by: Guanglei Zhu <zhugl3@xiaopeng.com>
---
v2: reject the report in the header validation instead of clamping the
    count, as asked; size the array from GEN3_MAX_FINGERS and use
    GEN3_FINGER_NUM() in the event path as well (the macro masked with
    0x07 while the event path masked with 0x0f).

Tested with a user space replay of the two helpers, built with ASan.
On master every count above five walks past the 27-byte report block
and ASan reports a read at offset 63 of the block; a header check that
keeps the 0x07 mask still lets counts 8..15 through, since 8 & 0x07 is
zero.  With this patch counts 0..5 behave as before, counts 6..15 and
the all-0xff glitch packet return -EINVAL and nothing is reported.

 drivers/input/mouse/cyapa_gen3.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/input/mouse/cyapa_gen3.c b/drivers/input/mouse/cyapa_gen3.c
index fc3fb95..f6860a7 100644
--- a/drivers/input/mouse/cyapa_gen3.c
+++ b/drivers/input/mouse/cyapa_gen3.c
@@ -25,7 +25,7 @@
 
 
 #define GEN3_MAX_FINGERS 5
-#define GEN3_FINGER_NUM(x) (((x) >> 4) & 0x07)
+#define GEN3_FINGER_NUM(x) (((x) >> 4) & 0x0f)
 
 #define BLK_HEAD_BYTES 32
 
@@ -113,7 +113,7 @@ struct cyapa_reg_data {
 	 */
 	u8 finger_btn;
 	/* CYAPA reports up to 5 touches per packet. */
-	struct cyapa_touch touches[5];
+	struct cyapa_touch touches[GEN3_MAX_FINGERS];
 } __packed;
 
 struct gen3_write_block_cmd {
@@ -1149,7 +1149,7 @@ static int cyapa_gen3_event_process(struct cyapa *cyapa,
 	int num_fingers;
 	int i;
 
-	num_fingers = (data->finger_btn >> 4) & 0x0f;
+	num_fingers = GEN3_FINGER_NUM(data->finger_btn);
 	for (i = 0; i < num_fingers; i++) {
 		const struct cyapa_touch *touch = &data->touches[i];
 		/* Note: touch->id range is 1 to 15; slots are 0 to 14. */
@@ -1200,6 +1200,11 @@ static int cyapa_gen3_irq_handler(struct cyapa *cyapa)
 		return -EINVAL;
 	}
 
+	if (GEN3_FINGER_NUM(data.finger_btn) > GEN3_MAX_FINGERS) {
+		dev_err(dev, "invalid finger count: %02x\n", data.finger_btn);
+		return -EINVAL;
+	}
+
 	return cyapa_gen3_event_process(cyapa, &data);
 }
 
@@ -1223,6 +1228,9 @@ static int cyapa_gen3_try_poll_handler(struct cyapa *cyapa)
 	    (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID)
 		return -EINVAL;
 
+	if (GEN3_FINGER_NUM(data.finger_btn) > GEN3_MAX_FINGERS)
+		return -EINVAL;
+
 	return cyapa_gen3_event_process(cyapa, &data);
 
 }
-- 
2.43.0


  parent reply	other threads:[~2026-12-01 12:23 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03  8:41 [PATCH] Input: joydev - avoid an undefined shift in the legacy 0x read Guanglei Zhu
2026-11-03  8:41 ` [PATCH] Input: joydev - avoid signed overflow in the broken-axis correction Guanglei Zhu
2026-11-03  8:48   ` sashiko-bot
2026-11-03  8:55   ` [PATCH v2] " Guanglei Zhu
2026-11-04  3:38     ` Dmitry Torokhov
2026-11-04  5:50       ` [PATCH v3] " Guanglei Zhu
2026-11-04 23:45         ` Dmitry Torokhov
2026-11-04  6:02       ` Guanglei Zhu
2026-11-03  8:41 ` [PATCH] Input: adi - bound the two-stream packet merge Guanglei Zhu
2026-11-03  8:55   ` sashiko-bot
2026-11-03 11:17   ` [PATCH v2] " Guanglei Zhu
2026-11-04 22:17     ` Dmitry Torokhov
2026-12-01 12:23   ` [PATCH v3 0/4] Input: adi - more bounds checks in the gameport packet path Guanglei Zhu
2026-12-01 12:23     ` [PATCH v3 1/4] Input: adi - reject stream merges that do not fit the buffer Guanglei Zhu
2026-12-01 12:23     ` [PATCH v3 2/4] Input: adi - fix the packet length guard in adi_read_packet() Guanglei Zhu
2026-12-01 12:23     ` [PATCH v3 3/4] Input: adi - reject ID packets with wrapped axis or button counts Guanglei Zhu
2026-12-01 12:23     ` [PATCH v3 4/4] Input: adi - check the decoded counts against the device tables Guanglei Zhu
2026-12-01 12:41     ` [PATCH v3 0/4] Input: adi - more bounds checks in the gameport packet path Guanglei Zhu
2026-11-03  8:41 ` [PATCH] Input: db9 - enlarge the Saturn multitap packet buffer Guanglei Zhu
2026-11-04 23:46   ` Dmitry Torokhov
2026-11-03  8:41 ` [PATCH] Input: lpc32xx-keys - reject matrices bigger than the hardware has Guanglei Zhu
2026-11-03 18:54   ` Dmitry Torokhov
2026-11-04  3:07     ` [PATCH v2] " Guanglei Zhu
2026-11-04  6:25       ` Dmitry Torokhov
2026-11-06  1:49         ` [PATCH v3] " Guanglei Zhu
2026-11-09  8:07           ` Dmitry Torokhov
2026-11-05 15:53       ` [PATCH v2] " sashiko-bot
2026-11-03  8:41 ` [PATCH] Input: mt6779-keypad - check the reported row and column Guanglei Zhu
2026-11-04 23:52   ` Dmitry Torokhov
2026-11-03  8:41 ` [PATCH] Input: tegra-kbc - check the pin numbers from the device tree Guanglei Zhu
2026-11-04 17:08   ` Dmitry Torokhov
2026-11-05  2:13   ` [PATCH v2] " Guanglei Zhu
2026-11-09  8:11     ` Dmitry Torokhov
2026-11-03  8:41 ` [PATCH] Input: cyapa_gen3 - clamp the reported finger count to the array Guanglei Zhu
2026-11-04 20:09   ` Dmitry Torokhov
2026-12-01 12:23   ` Guanglei Zhu [this message]
2026-11-03 18:45 ` [PATCH] Input: joydev - avoid an undefined shift in the legacy 0x read Dmitry Torokhov
2026-11-04  3:07   ` [PATCH v2] " Guanglei Zhu
2026-11-09  8:19     ` Dmitry Torokhov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201122309.361277-1-zhugl3@xiaopeng.com \
    --to=zhugl3@xiaopeng.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liuc63@xiaopeng.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox