Linux Input/HID development
 help / color / mirror / Atom feed
From: Tristan Madani <tristmd@gmail.com>
To: Jiri Kosina <jikos@kernel.org>, Benjamin Tissoires <bentiss@kernel.org>
Cc: linux-input@vger.kernel.org,
	Marcus Folkesson <marcus.folkesson@gmail.com>,
	Masaki Ota <masaki.ota@jp.alps.com>,
	Rahul Rameshbabu <sergeantsagara@protonmail.com>,
	Johannes Roith <johannes@gnu-linux.rocks>,
	"Daniel M . Lambea" <dmlambea@gmail.com>,
	"Derek J . Clark" <derekjohn.clark@gmail.com>,
	Zhouwang Huang <honjow311@gmail.com>,
	David Barksdale <dbarksdale@uplogix.com>,
	Don Prince <dhprince-devel@yahoo.co.uk>,
	Clemens Ladisch <clemens@ladisch.de>,
	Tristan Madani <tristan@talencesecurity.com>,
	stable@vger.kernel.org
Subject: [PATCH 7/8] HID: cp2112: validate report size in raw_event callback
Date: Sun, 18 Oct 2026 21:37:52 +0000	[thread overview]
Message-ID: <20261018213754.1951187-8-tristmd@gmail.com> (raw)
In-Reply-To: <20261018213754.1951187-1-tristmd@gmail.com>

From: Tristan Madani <tristan@talencesecurity.com>

cp2112_raw_event() processes incoming HID reports without validating
the report size against the data it accesses. Since hid-core calls the
driver's raw_event callback before it validates the report size in
hid_report_raw_event(), the driver must perform its own bounds checking.

For CP2112_TRANSFER_STATUS_RESPONSE, the code casts data to struct
cp2112_xfer_status_report (7 bytes) and accesses fields at offsets 1-6.

For CP2112_DATA_READ_RESPONSE, the code reads data[1], data[2], and
then copies data[2] bytes (capped to 61) starting from data[3]. If the
report is shorter than 3 + read_length bytes, the memcpy reads beyond
the buffer boundary.

Add size validation for both report types:
 - Transfer status: require sizeof(struct cp2112_xfer_status_report)
 - Data read: require at least 3 bytes for the header, then cap the
   copy length to what is actually available in the buffer

Fixes: e932d817866770d4 ("HID: add hid-cp2112 driver")
Cc: stable@vger.kernel.org
Signed-off-by: Tristan Madani <tristan@talencesecurity.com>
---
 drivers/hid/hid-cp2112.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/drivers/hid/hid-cp2112.c b/drivers/hid/hid-cp2112.c
index 04379db93571b..2508c8ea8b693 100644
--- a/drivers/hid/hid-cp2112.c
+++ b/drivers/hid/hid-cp2112.c
@@ -1430,6 +1430,10 @@ static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report,
 
 	switch (data[0]) {
 	case CP2112_TRANSFER_STATUS_RESPONSE:
+		if (size < sizeof(struct cp2112_xfer_status_report)) {
+			hid_err(hdev, "short transfer status report\n");
+			return 0;
+		}
 		hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n",
 			xfer->status0, xfer->status1,
 			be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length));
@@ -1463,11 +1467,17 @@ static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report,
 		atomic_set(&dev->xfer_avail, 1);
 		break;
 	case CP2112_DATA_READ_RESPONSE:
+		if (size < 3) {
+			hid_err(hdev, "short data read response\n");
+			return 0;
+		}
 		hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]);
 
 		dev->read_length = data[2];
 		if (dev->read_length > sizeof(dev->read_data))
 			dev->read_length = sizeof(dev->read_data);
+		if (dev->read_length > size - 3)
+			dev->read_length = size - 3;
 
 		memcpy(dev->read_data, &data[3], dev->read_length);
 		atomic_set(&dev->read_avail, 1);
-- 
2.47.3


  parent reply	other threads:[~2026-10-18 21:38 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-18 21:37 [PATCH 0/8] HID: validate report size in raw_event callbacks Tristan Madani
2026-10-18 21:37 ` [PATCH 1/8] HID: pxrc: validate report size in raw_event callback Tristan Madani
2026-10-18 21:37 ` [PATCH 2/8] HID: alps: validate report size in raw_event callbacks Tristan Madani
2026-10-18 21:45   ` sashiko-bot
2026-10-18 21:37 ` [PATCH 3/8] HID: mcp2200: validate report size in raw_event callback Tristan Madani
2026-10-18 21:37 ` [PATCH 4/8] HID: cougar: " Tristan Madani
2026-10-18 21:37 ` [PATCH 5/8] HID: zydacron: " Tristan Madani
2026-10-18 21:37 ` [PATCH 6/8] HID: oxp: validate report size in raw_event callbacks Tristan Madani
2026-10-18 21:48   ` sashiko-bot
2026-10-18 21:37 ` Tristan Madani [this message]
2026-10-18 21:37 ` [PATCH 8/8] HID: prodikeys: validate report size in raw_event callback Tristan Madani
2026-10-18 21:44   ` sashiko-bot

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=20261018213754.1951187-8-tristmd@gmail.com \
    --to=tristmd@gmail.com \
    --cc=bentiss@kernel.org \
    --cc=clemens@ladisch.de \
    --cc=dbarksdale@uplogix.com \
    --cc=derekjohn.clark@gmail.com \
    --cc=dhprince-devel@yahoo.co.uk \
    --cc=dmlambea@gmail.com \
    --cc=honjow311@gmail.com \
    --cc=jikos@kernel.org \
    --cc=johannes@gnu-linux.rocks \
    --cc=linux-input@vger.kernel.org \
    --cc=marcus.folkesson@gmail.com \
    --cc=masaki.ota@jp.alps.com \
    --cc=sergeantsagara@protonmail.com \
    --cc=stable@vger.kernel.org \
    --cc=tristan@talencesecurity.com \
    /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