Skip to contentSkip to conversation

HID: mcp2221: reject short input reports

PATCH New Yousef Alhouseen 2 participants 2 messages Jun 28

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

PATCH 1/1 HID: mcp2221: reject short input reports

The MCP2221 raw-event callback reads fixed offsets from the 64-byte input report, including data[50] and a variable payload beginning at data[4]. Raw-event callbacks run before HID core extends short reports to their declared size, so a malformed USB device can make these accesses run past the received buffer.

Reject reports whose size does not match the protocol and complete the pending command with -EMSGSIZE so its caller does not wait for a timeout.

Fixes 67a95c21463d ("HID: mcp2221: add usb to i2c-smbus host bridge")
Reported-by [email protected]
---
 drivers/hid/hid-mcp2221.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/hid/hid-mcp2221.c +6 -0
index e4ddd8e9293b..311b51c17b4e 100644
@@ -861,6 +861,12 @@ static int mcp2221_raw_event(struct hid_device *hdev,
 	u8 *buf;
 	struct mcp2221 *mcp = hid_get_drvdata(hdev);
 
+	if (size != sizeof(mcp->txbuf)) {
+		mcp->status = -EMSGSIZE;
+		complete(&mcp->wait_in_report);
+		return 1;
+	}
+
 	switch (data[0]) {
 
 	case MCP2221_I2C_WR_DATA:
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Use-After-Free and kernel stack corruption due to un-cleared mcp->rxbuf on timeout.
- [Critical] Stack buffer overflow due to using uninitialized memory as I2C read length.
- [High] Delayed USB reports indiscriminately complete pending waits, causing state desynchronization.
- [High] Static retries variable underflow causes infinite loop of workqueue rescheduling. --

commit 421d556dea19ceb8f40a640c782b6801d60f43cd
Author: Yousef Alhouseen <[email protected]>

HID: mcp2221: reject short input reports

The MCP2221 raw-event callback reads fixed offsets from the 64-byte input report, including data[50] and a variable payload beginning at data[4]. This rejects reports whose size does not match the protocol and completes the pending command with -EMSGSIZE.

Quoted 5 lines
drivers/hid/hid-mcp2221.c +0 -0
index e4ddd8e9293b6..311b51c17b4e4 100644

drivers/hid/hid-mcp2221.c static int mcp2221_raw_event(struct hid_device *hdev, · line 869

Quoted 9 lines
drivers/hid/hid-mcp2221.c +6 -0
@@ -861,6 +861,12 @@ static int mcp2221_raw_event(struct hid_device *hdev,
 	u8 *buf;
 	struct mcp2221 *mcp = hid_get_drvdata(hdev);
 
+	if (size != sizeof(mcp->txbuf)) {
+		mcp->status = -EMSGSIZE;
+		complete(&mcp->wait_in_report);
+		return 1;
+	}
+

[Severity: High]
This is a pre-existing issue, but does mcp2221_raw_event() indiscriminately complete pending waits for any incoming report without matching it to the pending request?

If a previous request times out, and a delayed USB response arrives later, it appears it could prematurely wake up a new command's wait queue with incorrect status and data:

drivers/hid/hid-mcp2221.c:mcp2221_raw_event() {
	...
	case MCP2221_I2C_WR_DATA:
	...
		complete(&mcp->wait_in_report);
		break;
	...
}

[Severity: Critical]
This isn't a bug introduced by this patch, but can this lead to stack corruption if a request times out?

In mcp_i2c_smbus_read(), if mcp_send_data_req_status() fails or times out, it returns immediately without clearing mcp->rxbuf:

drivers/hid/hid-mcp2221.c:mcp_i2c_smbus_read() {
	...
	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4);
	if (ret)
		return ret;
	...
}

Since mcp->rxbuf can point to a caller-provided stack buffer (like union i2c_smbus_data), the stack frame is popped upon return. If a delayed USB response arrives later, wouldn't mcp2221_raw_event() blindly write into this dangling mcp->rxbuf pointer?

drivers/hid/hid-mcp2221.c:mcp2221_raw_event() {
	...
			if (data[2] == MCP2221_I2C_READ_COMPL ||
			    data[2] == MCP2221_I2C_READ_PARTIAL) {
				...
				buf = mcp->rxbuf;
				memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
	...
}

[Severity: Critical]
This is a pre-existing issue, but does mcp_smbus_xfer() use uninitialized stack memory as the I2C read length during a block read?

drivers/hid/hid-mcp2221.c:mcp_smbus_xfer() {
	...
			ret = mcp_i2c_smbus_read(mcp, NULL,
						MCP2221_I2C_RD_RPT_START,
						addr, data->block[0] + 1,
						data->block);
	...
}

The union i2c_smbus_data provided by the caller is uninitialized stack memory on read. This configures mcp->rxbuf_size to an arbitrarily large value. Could this lead to a massive stack buffer overflow when mcp2221_raw_event() copies up to this value into the statically sized 34-byte data->block stack buffer?

[Severity: High]
This is a pre-existing issue, but could the statically scoped retries variable in mcp_init_work() cause an infinite reschedule loop?

drivers/hid/hid-mcp2221.c:mcp_init_work() {
	static int retries = 5;
	...
reschedule_task:
	...
	if (!retries--)
		return;

	/* Device is not ready to read SRAM or FLASH data, try again */
	schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100));
}

Since retries is static, when initialization fails 6 times, it reaches 0, returns, and decrements to -1. On a subsequent failure, retries starts at -1. Because !(-1) is false, it bypasses the return, decrements to -2, and unconditionally schedules the delayed work every 100ms forever.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1