Skip to contentSkip to conversation

HID: mcp2221: validate report size in raw_event handler

PATCH New Sebastian Josue Alba Vives 2 participants 3 messages Mar 24 – Apr 9

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 HID: mcp2221: validate report size in raw_event handler

mcp2221_raw_event() accesses the data buffer at offsets up to 55 without validating the size parameter. Since __hid_input_report() invokes the driver's raw_event callback before hid_report_raw_event() performs its own report-size validation, a device sending a truncated HID report can cause out-of-bounds heap reads in the kernel.

The most critical access is the memcpy from data[50] into mcp->adc_values (6 bytes) when CONFIG_IIO is reachable. Other unchecked accesses include data[20] and a memcpy at data[22]. Additionally, a memcpy with device-controlled length (data[3], up to 60 bytes) from data[4] does not verify that size is large enough to cover the copy.

MCP2221 devices use 64-byte HID reports. Add a check at the top of the handler to reject any report shorter than expected.

---
 drivers/hid/hid-mcp2221.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/hid/hid-mcp2221.c +4 -0
index ef3b5c77c..fcac37491 100644
@@ -851,6 +851,10 @@ static int mcp2221_raw_event(struct hid_device *hdev,
 	u8 *buf;
 	struct mcp2221 *mcp = hid_get_drvdata(hdev);
 
+	/* MCP2221 always sends 64-byte reports */
+	if (size < 64)
+		return 0;
+
 	switch (data[0]) {
 
 	case MCP2221_I2C_WR_DATA:
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/1 HID: mcp2221: validate report size in raw_event handler

mcp2221_raw_event() accesses the data buffer at offsets up to 55 without validating the size parameter. Since __hid_input_report() invokes the driver's raw_event callback before hid_report_raw_event() performs its own report-size validation, a device sending a truncated HID report can cause out-of-bounds heap reads in the kernel.

The most critical access is the memcpy from data[50] into mcp->adc_values (6 bytes) when CONFIG_IIO is reachable. Other unchecked accesses include data[20] and a memcpy at data[22]. Additionally, a memcpy with device-controlled length (data[3], up to 60 bytes) from data[4] does not verify that size is large enough to cover the copy.

MCP2221 devices use 64-byte HID reports. Add a check at the top of the handler to reject any report shorter than expected, and log a warning to aid debugging.

---
 drivers/hid/hid-mcp2221.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/hid/hid-mcp2221.c +5 -0
index ef3b5c77c..770c305d8 100644
@@ -850,6 +850,11 @@ static int mcp2221_raw_event(struct hid_device *hdev,
 {
 	u8 *buf;
 	struct mcp2221 *mcp = hid_get_drvdata(hdev);
+	/* MCP2221 always sends 64-byte reports */
+	if (size < 64) {
+		hid_warn(hdev, "report too short: %d < 64\n", size);
+		return 0;
+	}
 
 	switch (data[0]) {
 
Signature · 1 line
2.43.0
maintainer
#2 lore raw
Quoted 3 lines · Sebastian Josue Alba Vives

MCP2221 devices use 64-byte HID reports.
Add a check at the top of the handler to reject any report shorter than expected, and log a warning to aid debugging.

Similarly to ft260 -- where is the claim that the device can't send shorter reports coming from, please?

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs