All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Rishi Gupta <gupt21@gmail.com>,
	Jiri Kosina <jikos@kernel.org>,
	Benjamin Tissoires <bentiss@kernel.org>,
	Jiangshan Yi <yijiangshan@kylinos.cn>,
	linux-i2c@vger.kernel.org, linux-input@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 02/10] HID: mcp2221: Synchronise rxbuf access against raw_event callback
Date: Mon, 23 Nov 2026 15:50:46 +0000	[thread overview]
Message-ID: <20261123155138.2907049-3-lee@kernel.org> (raw)
In-Reply-To: <20261123155138.2907049-1-lee@kernel.org>

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@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 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 3f05d6e288f9..f3d14d5f11ab 100644
--- a/drivers/hid/hid-mcp2221.c
+++ b/drivers/hid/hid-mcp2221.c
@@ -20,6 +20,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;
@@ -125,6 +126,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;
@@ -320,6 +322,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;
@@ -330,22 +333,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 */
@@ -370,14 +373,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;
 }
@@ -925,22 +931,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;
@@ -1283,6 +1287,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.750.gf70dc682cf-goog


  parent reply	other threads:[~2026-11-23 15:52 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 15:50 [PATCH 00/10] HID: Assorted bug fixes across HID core and drivers Lee Jones
2026-11-23 15:50 ` [PATCH 01/10] HID: core: Protect hid->inputs traversal in hid_check_keys_pressed() Lee Jones
2026-11-23 15:50 ` Lee Jones [this message]
2026-11-23 15:50 ` [PATCH 03/10] HID: logitech-hidpp: Synchronise send_receive_buf against raw events Lee Jones
2026-11-23 16:14   ` sashiko-bot
2026-11-23 15:50 ` [PATCH 04/10] HID: sensor-hub: Serialise synchronous read teardown under data->lock Lee Jones
2026-11-23 16:13   ` sashiko-bot
2026-11-23 20:08   ` Srinivas Pandruvada
2026-11-23 15:50 ` [PATCH 05/10] HID: roccat: pyra: Resolve USB device from parent in actual_profile Lee Jones
2026-11-23 15:50 ` [PATCH 06/10] HID: hyperv: Validate VMBus packet and payload bounds on receive Lee Jones
2026-11-25 21:25   ` Hamza Mahfooz
2026-11-23 15:50 ` [PATCH 07/10] HID: hid-goodix-spi: Validate package sizes in goodix_hid_irq() Lee Jones
2026-11-23 15:50 ` [PATCH 08/10] HID: wacom: Stop HID hardware before releasing resources on mode change Lee Jones
2026-11-23 16:08   ` sashiko-bot
2026-11-23 15:50 ` [PATCH 09/10] HID: debug: Hold driver_input_lock while dumping raw report descriptor Lee Jones
2026-11-23 15:50 ` [PATCH 10/10] HID: playstation: Cancel DualSense output worker on create failure Lee Jones

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=20261123155138.2907049-3-lee@kernel.org \
    --to=lee@kernel.org \
    --cc=bentiss@kernel.org \
    --cc=gupt21@gmail.com \
    --cc=jikos@kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=yijiangshan@kylinos.cn \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.