[PATCH v2 02/10] HID: mcp2221: Synchronise rxbuf access against raw_event callback
From: Lee Jones
Date: Fri Nov 27 2026 - 12:07:42 EST
mcp2221_probe() enables incoming reports via hid_device_io_start(), so
mcp2221_raw_event() runs concurrently in interrupt context without
holding mcp->lock. mcp_i2c_smbus_read() points mcp->rxbuf at caller
buffers, including stack-allocated SMBus data in mcp_smbus_xfer(), and
clears mcp->rxbuf at exit without synchronisation against
mcp2221_raw_event(). If a transfer times out while mcp2221_raw_event()
has already loaded mcp->rxbuf, memcpy() writes up to 60 bytes into the
caller's destroyed stack frame or freed buffer. In addition,
mcp->rxbuf was published before initialising mcp->rxbuf_idx to zero.
Introduce a spinlock to serialise mcp->rxbuf, mcp->rxbuf_size, and
mcp->rxbuf_idx setup and teardown in mcp_i2c_smbus_read() against
MCP2221_I2C_GET_DATA handling in mcp2221_raw_event().
Fixes: 67a95c21463d ("HID: mcp2221: add usb to i2c-smbus host bridge")
Fixes: db2333f88729 ("HID: mcp2221: clear rxbuf after I2C/SMBus transfer completes")
Closes: https://sashiko.dev/#/bug/linux-4983b628-f605-439f-95bb-87560531ec95
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
v1 => v2:
- No changes.
drivers/hid/hid-mcp2221.c | 47 ++++++++++++++++++++++-----------------
1 file changed, 26 insertions(+), 21 deletions(-)
diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c
index c7e83b61b236..4f6fb75fcd01 100644
--- a/drivers/hid/hid-mcp2221.c
+++ b/drivers/hid/hid-mcp2221.c
@@ -19,6 +19,7 @@
#include <linux/iio/iio.h>
#include <linux/minmax.h>
#include <linux/moduleparam.h>
+#include <linux/spinlock.h>
#include "hid-ids.h"
static bool gpio_mode_enforce;
@@ -124,6 +125,7 @@ struct mcp2221 {
struct mutex lock;
struct completion wait_in_report;
struct delayed_work init_work;
+ spinlock_t rxbuf_lock; /* protects rxbuf, rxbuf_size, and rxbuf_idx */
u8 *rxbuf;
u8 txbuf[64];
int rxbuf_idx;
@@ -319,6 +321,7 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
struct i2c_msg *msg, int type, u16 smbus_addr,
u8 smbus_len, u8 *smbus_buf)
{
+ unsigned long flags;
int ret;
u16 total_len;
int retries = 0;
@@ -329,22 +332,22 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
mcp->txbuf[2] = msg->len >> 8;
mcp->txbuf[3] = (u8)(msg->addr << 1);
total_len = msg->len;
- mcp->rxbuf = msg->buf;
- mcp->rxbuf_size = msg->len;
} else {
mcp->txbuf[1] = smbus_len;
mcp->txbuf[2] = 0;
mcp->txbuf[3] = (u8)(smbus_addr << 1);
total_len = smbus_len;
- mcp->rxbuf = smbus_buf;
- mcp->rxbuf_size = smbus_len;
}
ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4);
if (ret)
- goto out;
+ return ret;
+ spin_lock_irqsave(&mcp->rxbuf_lock, flags);
+ mcp->rxbuf = msg ? msg->buf : smbus_buf;
+ mcp->rxbuf_size = total_len;
mcp->rxbuf_idx = 0;
+ spin_unlock_irqrestore(&mcp->rxbuf_lock, flags);
do {
/* Wait for the data to be read by the device */
@@ -369,14 +372,17 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
} else {
retries = 0;
}
- } while (mcp->rxbuf_idx < total_len);
+ } while (READ_ONCE(mcp->rxbuf_idx) < total_len);
usleep_range(980, 1000);
ret = mcp_chk_last_cmd_status_free_bus(mcp);
out:
+ spin_lock_irqsave(&mcp->rxbuf_lock, flags);
mcp->rxbuf = NULL;
mcp->rxbuf_size = 0;
+ mcp->rxbuf_idx = 0;
+ spin_unlock_irqrestore(&mcp->rxbuf_lock, flags);
return ret;
}
@@ -924,22 +930,20 @@ static int mcp2221_raw_event(struct hid_device *hdev,
}
if (data[2] == MCP2221_I2C_READ_COMPL ||
data[2] == MCP2221_I2C_READ_PARTIAL) {
- if (!mcp->rxbuf || mcp->rxbuf_idx < 0 || data[3] > 60) {
- mcp->status = -EINVAL;
- break;
- }
- if (mcp->rxbuf_idx + data[3] > mcp->rxbuf_size) {
- mcp->status = -EINVAL;
- break;
- }
- if (4 + data[3] > size) {
- mcp->status = -EINVAL;
- break;
- }
+ unsigned long flags;
+
+ spin_lock_irqsave(&mcp->rxbuf_lock, flags);
buf = mcp->rxbuf;
- memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
- mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];
- mcp->status = 0;
+ if (!buf || mcp->rxbuf_idx < 0 || data[3] > 60 ||
+ mcp->rxbuf_idx + data[3] > mcp->rxbuf_size ||
+ 4 + data[3] > size) {
+ mcp->status = -EINVAL;
+ } else {
+ memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
+ WRITE_ONCE(mcp->rxbuf_idx, mcp->rxbuf_idx + data[3]);
+ mcp->status = 0;
+ }
+ spin_unlock_irqrestore(&mcp->rxbuf_lock, flags);
break;
}
mcp->status = -EIO;
@@ -1282,6 +1286,7 @@ static int mcp2221_probe(struct hid_device *hdev,
}
mutex_init(&mcp->lock);
+ spin_lock_init(&mcp->rxbuf_lock);
init_completion(&mcp->wait_in_report);
hid_set_drvdata(hdev, mcp);
mcp->hdev = hdev;
--
2.56.1.839.g22a8290db9-goog