Skip to contentSkip to conversation

HID: Fix race conditions, UAFs, and bounds bugs

PATCH New Lee Jones 2 participants 12 messages Nov 27

Subsystems (14)
Stable branch BPF HID IIO +10 more subsystems
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

v2 cover HID: Fix race conditions, UAFs, and bounds bugs

These issues were reported by Sashiko.

This v2 series addresses Srinivas Pandruvada's review feedback on hid-sensor-hub and collects Reviewed-by and Tested-by tags from v1.

v1 => v2:
- Drop kfree() from sensor_hub_raw_event() and clear hsdev->pending.status before complete() under data->lock in patch 04, per Srinivas Pandruvada's review.
- Add Hamza Mahfooz's Reviewed-by tag on patch 06.
- Add Ping Cheng's Reviewed-by and Tested-by tags on patch 08.

Cc "Barnabás Pőcze" <[email protected]>
Cc "Benoît Sevens" <[email protected]>
Cc "Filipe Laíns" <[email protected]>
Cc "He, Bo" <[email protected]>
Cc "K. Y. Srinivasan" <[email protected]>
Cc "Zhang, Jun" <[email protected]>
Cc Andreas Gal <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Bastien Nocera <[email protected]>
Cc Benjamin Tissoires <[email protected]>
Cc Charles Wang <[email protected]>
Cc Dexuan Cui <[email protected]>
Cc Dmitry Torokhov <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Jason Gerecke <[email protected]>
Cc Jiangshan Yi <[email protected]>
Cc Jiri Kosina <[email protected]>
Cc Jonathan Cameron <[email protected]>
Cc Lopez Casado <[email protected]>
Cc Marcel Holtmann <[email protected]>
Cc message to <[email protected]>
Cc Michael Bommarito <[email protected]>
Cc Michael Haboustak <[email protected]>
Cc Pan Chuang <[email protected]>
Cc Ping Cheng <[email protected]>
Cc Rishi Gupta <[email protected]>
Cc Roderick Colenbrander <[email protected]>
Cc Srinivas Pandruvada <[email protected]>
Cc Stefan Achatz <[email protected]>
Cc Tianchu Chen <[email protected]>
Cc Vojtech Pavlik <[email protected]>
Cc Wei Liu <[email protected]>
Cc Zhang Lixu <[email protected]>

Lee Jones (10):
HID: core: Protect hid->inputs traversal in hid_check_keys_pressed()
HID: mcp2221: Synchronise rxbuf access against raw_event callback
HID: logitech-hidpp: Synchronise send_receive_buf against raw events
HID: sensor-hub: Serialise synchronous read teardown under data->lock
HID: roccat: pyra: Resolve USB device from parent in actual_profile
HID: hyperv: Validate VMBus packet and payload bounds on receive
HID: hid-goodix-spi: Validate package sizes in goodix_hid_irq()
HID: wacom: Stop HID hardware before releasing resources on mode
change
HID: debug: Hold driver_input_lock while dumping raw report descriptor
HID: playstation: Cancel DualSense output worker on create failure

 drivers/hid/hid-core.c           | 16 ++++++---
 drivers/hid/hid-debug.c          | 12 ++++---
 drivers/hid/hid-goodix-spi.c     | 34 +++++++++++++------
 drivers/hid/hid-hyperv.c         | 12 +++++++
 drivers/hid/hid-logitech-hidpp.c | 58 +++++++++++++++++++++-----------
 drivers/hid/hid-mcp2221.c        | 47 ++++++++++++++------------
 drivers/hid/hid-playstation.c    |  1 +
 drivers/hid/hid-roccat-pyra.c    |  9 +++--
 drivers/hid/hid-sensor-hub.c     | 29 ++++++++++++----
 drivers/hid/wacom_sys.c          |  4 +--
 10 files changed, 150 insertions(+), 72 deletions(-)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#1 lore raw

PATCH v2 1/10 HID: core: Protect hid->inputs traversal in hid_check_keys_pressed()

hid_check_keys_pressed() is invoked from USB HID interrupt URB completion in softirq context without holding hid->driver_input_lock. During driver unbind or device removal, hid_device_remove() holds hid->driver_input_lock and calls hid_hw_stop() -> hid_disconnect() -> hidinput_disconnect(), which unlinks and frees hid_input entries from hid->inputs before hid->claimed clears HID_CLAIMED_INPUT and before the low-level transport stops interrupt URBs when another user such as hidraw keeps the device open, leading to a use-after-free.

Guard hid_check_keys_pressed() with down_trylock() on hid->driver_input_lock, matching __hid_input_report().

Fixes c4c259bcc27c ("HID: consolidate connect and disconnect into core code")

---
v1 => v2:
- No changes.

 drivers/hid/hid-core.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/hid/hid-core.c +12 -4
index 64ac7606927c..938ad3fe6a74 100644
@@ -3217,18 +3217,26 @@ EXPORT_SYMBOL_GPL(hid_unregister_driver);
 int hid_check_keys_pressed(struct hid_device *hid)
 {
 	struct hid_input *hidinput;
+	int ret = 0;
 	int i;
 
+	if (down_trylock(&hid->driver_input_lock))
+		return 0;
+
 	if (!(hid->claimed & HID_CLAIMED_INPUT))
-		return 0;
+		goto out;
 
 	list_for_each_entry(hidinput, &hid->inputs, list) {
 		for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
-			if (hidinput->input->key[i])
-				return 1;
+			if (hidinput->input->key[i]) {
+				ret = 1;
+				goto out;
+			}
 	}
 
-	return 0;
+out:
+	up(&hid->driver_input_lock);
+	return ret;
 }
 EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
 
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#2 lore raw

PATCH v2 2/10 HID: mcp2221: Synchronise rxbuf access against raw_event callback

In reply to Lee Jones

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")

---
v1 => v2:
- No changes.

 drivers/hid/hid-mcp2221.c | 47 ++++++++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 21 deletions(-)
drivers/hid/hid-mcp2221.c +26 -21
index c7e83b61b236..4f6fb75fcd01 100644
@@ -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;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#3 lore raw

PATCH v2 3/10 HID: logitech-hidpp: Synchronise send_receive_buf against raw events

In reply to Lee Jones

__do_hidpp_send_message_sync() stores a pointer to the caller's stack-allocated response buffer in hidpp->send_receive_buf while holding hidpp->send_mutex, whereas hidpp_raw_hidpp_event() runs in interrupt context without holding hidpp->send_mutex. Clearing hidpp->send_receive_buf before releasing hidpp->send_mutex is
insufficient: if hidpp_raw_hidpp_event() loads hidpp->send_receive_buf just before wait_event_timeout() expires and the caller returns, writing *answer = *report corrupts up to 64 bytes of the popped kernel stack frame. Furthermore, hidpp->send_receive_buf was published before initialising *response = *message, and error paths zeroed *response while hidpp->send_receive_buf remained visible.

Protect hidpp->send_receive_buf and hidpp->answer_available with a spinlock across setup, completion, and teardown so that hidpp_raw_hidpp_event() cannot access the response buffer once __do_hidpp_send_message_sync() finishes.

Fixes 2f31c5252910 ("HID: Introduce hidpp, a module to handle Logitech hid++ devices")
Fixes e2aaf2d3ad92 ("HID: logitech-hidpp: fix race condition when accessing stale stack pointer")

---
v1 => v2:
- No changes.

 drivers/hid/hid-logitech-hidpp.c | 58 +++++++++++++++++++++-----------
 1 file changed, 38 insertions(+), 20 deletions(-)
drivers/hid/hid-logitech-hidpp.c +38 -20
index 60fc6b86a085..6c95c4bcdd42 100644
@@ -18,6 +18,7 @@
 #include <linux/slab.h>
 #include <linux/sched.h>
 #include <linux/sched/clock.h>
+#include <linux/spinlock.h>
 #include <linux/kfifo.h>
 #include <linux/input/mt.h>
 #include <linux/workqueue.h>
@@ -185,6 +186,8 @@ struct hidpp_device {
 	struct hid_device *hid_dev;
 	struct input_dev *input;
 	struct mutex send_mutex;
+	/* protects send_receive_buf and answer_available */
+	spinlock_t send_receive_lock;
 	void *send_receive_buf;
 	char *name;		/* will never be NULL and should not be freed */
 	wait_queue_head_t wait;
@@ -296,39 +299,44 @@ static int __do_hidpp_send_message_sync(struct hidpp_device *hidpp,
 	struct hidpp_report *message,
 	struct hidpp_report *response)
 {
+	unsigned long flags;
 	int ret;
 
 	__must_hold(&hidpp->send_mutex);
 
-	hidpp->send_receive_buf = response;
-	hidpp->answer_available = false;
-
 	/*
 	 * So that we can later validate the answer when it arrives
 	 * in hidpp_raw_event
 	 */
 	*response = *message;
 
+	spin_lock_irqsave(&hidpp->send_receive_lock, flags);
+	hidpp->send_receive_buf = response;
+	hidpp->answer_available = false;
+	spin_unlock_irqrestore(&hidpp->send_receive_lock, flags);
+
 	ret = __hidpp_send_report(hidpp->hid_dev, message);
 	if (ret) {
 		dbg_hid("__hidpp_send_report returned err: %d\n", ret);
-		memset(response, 0, sizeof(struct hidpp_report));
-		goto out;
+		goto err_zero;
 	}
 
-	if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
-				5*HZ)) {
+	if (!wait_event_timeout(hidpp->wait,
+				READ_ONCE(hidpp->answer_available), 5 * HZ)) {
 		dbg_hid("%s:timeout waiting for response\n", __func__);
-		memset(response, 0, sizeof(struct hidpp_report));
 		ret = -ETIMEDOUT;
-		goto out;
+		goto err_zero;
 	}
 
+	spin_lock_irqsave(&hidpp->send_receive_lock, flags);
+	hidpp->send_receive_buf = NULL;
+	spin_unlock_irqrestore(&hidpp->send_receive_lock, flags);
+
 	if (response->report_id == REPORT_ID_HIDPP_SHORT &&
 	    response->rap.sub_id == HIDPP_ERROR) {
 		ret = response->rap.params[1];
 		dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
-		goto out;
+		return ret;
 	}
 
 	if ((response->report_id == REPORT_ID_HIDPP_LONG ||
@@ -336,13 +344,16 @@ static int __do_hidpp_send_message_sync(struct hidpp_device *hidpp,
 	    response->fap.feature_index == HIDPP20_ERROR) {
 		ret = response->fap.params[1];
 		dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
-		goto out;
+		return ret;
 	}
 
-	ret = 0;
+	return 0;
 
-out:
+err_zero:
+	spin_lock_irqsave(&hidpp->send_receive_lock, flags);
 	hidpp->send_receive_buf = NULL;
+	spin_unlock_irqrestore(&hidpp->send_receive_lock, flags);
+	memset(response, 0, sizeof(struct hidpp_report));
 	return ret;
 }
 
@@ -4125,27 +4136,33 @@ static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
 	 * previously sent command.
 	 */
 	if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
+		unsigned long flags;
+		bool matched = false;
+
+		spin_lock_irqsave(&hidpp->send_receive_lock, flags);
 		question = hidpp->send_receive_buf;
 		answer = hidpp->send_receive_buf;
 
-		if (!question)
-			return 0;
-
 		/*
 		 * Check for a correct hidpp20 answer or the corresponding
 		 * error
 		 */
-		if (hidpp_match_answer(question, report) ||
-				hidpp_match_error(question, report)) {
+		if (question && (hidpp_match_answer(question, report) ||
+				 hidpp_match_error(question, report))) {
 			*answer = *report;
-			hidpp->answer_available = true;
+			hidpp->send_receive_buf = NULL;
+			WRITE_ONCE(hidpp->answer_available, true);
+			matched = true;
+		}
+		spin_unlock_irqrestore(&hidpp->send_receive_lock, flags);
+
+		if (matched) {
 			wake_up(&hidpp->wait);
 			/*
 			 * This was an answer to a command that this driver sent
 			 * We return 1 to hid-core to avoid forwarding the
 			 * command upstream as it has been treated by the driver
 			 */
-
 			return 1;
 		}
 	}
@@ -4723,6 +4740,7 @@ static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
 	INIT_WORK(&hidpp->work, hidpp_connect_event);
 	INIT_WORK(&hidpp->reset_hi_res_work, hidpp_reset_hi_res_handler);
 	mutex_init(&hidpp->send_mutex);
+	spin_lock_init(&hidpp->send_receive_lock);
 	init_waitqueue_head(&hidpp->wait);
 
 	/* indicates we are handling the battery properties in the kernel */
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#4 lore raw

PATCH v2 4/10 HID: sensor-hub: Serialise synchronous read teardown under data->lock

In reply to Lee Jones

sensor_hub_input_attr_read_values() and sensor_hub_input_attr_get_raw_value() set hsdev->pending.status under data->lock, sleep in wait_for_completion_interruptible_timeout(), and then clear hsdev->pending.status and consume or free hsdev->pending.raw_data without acquiring data->lock. When a wait is interrupted by a signal or times out while sensor_hub_raw_event() holds the same spinlock, sensor_hub_raw_event() can memcpy() into the caller's popped stack buffer in sensor_hub_input_attr_read_values() or race with kfree() and leak kmemdup() allocations in sensor_hub_input_attr_get_raw_value().

Hold data->lock when clearing hsdev->pending.status and detaching hsdev->pending.raw_data in both helpers, and clear hsdev->pending.status in sensor_hub_raw_event() when completing the request so only the first matching report is consumed.

Fixes 401ca24fb34a ("HID: sensors: introduce sensor framework")
Fixes f784fcea4506 ("HID: sensor-hub: Add sensor_hub_input_attr_read_values() for multi-byte reads")

---
v1 => v2:
- Remove kfree() from sensor_hub_raw_event() and clear hsdev->pending.status before complete() under data->lock, per Srinivas Pandruvada's review.

 drivers/hid/hid-sensor-hub.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)
drivers/hid/hid-sensor-hub.c +22 -7
index 2a7f2475171f..180a913888a4 100644
@@ -310,6 +310,9 @@ int sensor_hub_input_attr_read_values(struct hid_sensor_hub_device *hsdev,
 	long cycles;
 	int ret;
 
+	if (flag == SENSOR_HUB_SYNC && (!buffer || !buffer_size))
+		return -EINVAL;
+
 	report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
 	if (!report)
 		return -EINVAL;
@@ -339,7 +342,10 @@ int sensor_hub_input_attr_read_values(struct hid_sensor_hub_device *hsdev,
 		else if (cycles < 0)
 			ret = cycles;
 
+		spin_lock_irqsave(&data->lock, flags);
 		hsdev->pending.status = false;
+		hsdev->pending.raw_data = NULL;
+		spin_unlock_irqrestore(&data->lock, flags);
 	}
 	mutex_unlock(hsdev->mutex_ptr);
 
@@ -379,29 +385,36 @@ int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
 	mutex_unlock(&data->mutex);
 	if (flag == SENSOR_HUB_SYNC) {
+		u8 *raw_data;
+
 		wait_for_completion_interruptible_timeout(
 						&hsdev->pending.ready, HZ*5);
-		switch (hsdev->pending.raw_size) {
+		spin_lock_irqsave(&data->lock, flags);
+		raw_data = hsdev->pending.raw_data;
+		switch (raw_data ? hsdev->pending.raw_size : 0) {
 		case 1:
 			if (is_signed)
-				ret_val = *(s8 *)hsdev->pending.raw_data;
+				ret_val = *(s8 *)raw_data;
 			else
-				ret_val = *(u8 *)hsdev->pending.raw_data;
+				ret_val = *(u8 *)raw_data;
 			break;
 		case 2:
 			if (is_signed)
-				ret_val = *(s16 *)hsdev->pending.raw_data;
+				ret_val = *(s16 *)raw_data;
 			else
-				ret_val = *(u16 *)hsdev->pending.raw_data;
+				ret_val = *(u16 *)raw_data;
 			break;
 		case 4:
-			ret_val = *(u32 *)hsdev->pending.raw_data;
+			ret_val = *(u32 *)raw_data;
 			break;
 		default:
 			ret_val = 0;
 		}
-		kfree(hsdev->pending.raw_data);
+		hsdev->pending.raw_data = NULL;
+		hsdev->pending.raw_size = 0;
 		hsdev->pending.status = false;
+		spin_unlock_irqrestore(&data->lock, flags);
+		kfree(raw_data);
 	}
 	mutex_unlock(hsdev->mutex_ptr);
 
@@ -590,6 +603,7 @@ static int sensor_hub_raw_event(struct hid_device *hdev,
 					hsdev->pending.index += copy_size;
 					if (hsdev->pending.index >= hsdev->pending.max_raw_size) {
 						hsdev->pending.raw_size = hsdev->pending.index;
+						hsdev->pending.status = false;
 						complete(&hsdev->pending.ready);
 					}
 				}
@@ -599,6 +613,7 @@ static int sensor_hub_raw_event(struct hid_device *hdev,
 					hsdev->pending.raw_size = sz;
 				else
 					hsdev->pending.raw_size = 0;
+				hsdev->pending.status = false;
 				complete(&hsdev->pending.ready);
 			}
 		}
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#5 lore raw

PATCH v2 5/10 HID: roccat: pyra: Resolve USB device from parent in actual_profile

In reply to Lee Jones

pyra_sysfs_show_actual_profile() is a sysfs attribute callback on the roccat pyra class device, whose parent is the HID device and grandparent is the USB interface. While it resolves pyra via dev->parent->parent, it passes dev directly to to_usb_interface(), casting the HID device to a USB device and passing the corrupted pointer into roccat_common2_receive() and usb_control_msg().

Walk up to dev->parent->parent first, matching
pyra_sysfs_show_firmware_version().

Fixes be34380ef818 ("HID: roccat: cleanup of pyra module")

---
v1 => v2:
- No changes.

 drivers/hid/hid-roccat-pyra.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/hid/hid-roccat-pyra.c +6 -3
index 44c2b416f2db..b3dde2998850 100644
@@ -290,11 +290,14 @@ static DEVICE_ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL);
 static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
 		struct device_attribute *attr, char *buf)
 {
-	struct pyra_device *pyra =
-			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
-	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
+	struct pyra_device *pyra;
+	struct usb_device *usb_dev;
 	struct pyra_settings settings;
 
+	dev = dev->parent->parent;
+	pyra = hid_get_drvdata(dev_get_drvdata(dev));
+	usb_dev = interface_to_usbdev(to_usb_interface(dev));
+
 	mutex_lock(&pyra->pyra_lock);
 	roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
 			&settings, PYRA_SIZE_SETTINGS);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#6 lore raw

PATCH v2 6/10 HID: hyperv: Validate VMBus packet and payload bounds on receive

In reply to Lee Jones

mousevsc_on_receive() dereferences pipe_msg and hid_msg_hdr without verifying hv_pkt_datalen(packet), passes the untrusted pipe_msg->size to mousevsc_on_receive_device_info() without checking that the VMBus packet actually contains pipe_msg->size bytes, and copies up to sizeof(input_dev->input_buf) (16 KiB) from input_report->buffer in SYNTH_HID_INPUT_REPORT without checking input_report->header.size against the received packet buffer.

Validate that hv_pkt_datalen(packet) covers the pipe and SynthHID headers, that pipe_msg->size fits within the received VMBus packet, and that input_report->header.size fits within pipe_msg->size.

Fixes b95f5bcb811e ("HID: Move the hid-hyperv driver out of staging")
Fixes 934b7778aa7b ("HID: hyperv: validate initial device info bounds")

---
v1 => v2:
- Add Reviewed-by tag from Hamza Mahfooz.

 drivers/hid/hid-hyperv.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/hid/hid-hyperv.c +12 -0
index cfc061dbdd24..1de03e38f363 100644
@@ -272,14 +272,22 @@ static void mousevsc_on_receive(struct hv_device *device,
 	struct synthhid_msg_hdr *hid_msg_hdr;
 	struct mousevsc_dev *input_dev = hv_get_drvdata(device);
 	struct synthhid_input_report *input_report;
+	u32 pkt_len = hv_pkt_datalen(packet);
 	size_t len;
 
+	if (pkt_len < sizeof(struct pipe_prt_msg) + sizeof(struct synthhid_msg_hdr))
+		return;
+
 	pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
 						(packet->offset8 << 3));
 
 	if (pipe_msg->type != PIPE_MESSAGE_DATA)
 		return;
 
+	if (pipe_msg->size < sizeof(struct synthhid_msg_hdr) ||
+	    pipe_msg->size > pkt_len - sizeof(struct pipe_prt_msg))
+		return;
+
 	hid_msg_hdr = (struct synthhid_msg_hdr *)pipe_msg->data;
 
 	switch (hid_msg_hdr->type) {
@@ -317,6 +325,10 @@ static void mousevsc_on_receive(struct hv_device *device,
 		if (!input_dev->init_complete)
 			break;
 
+		if (input_report->header.size >
+		    pipe_msg->size - sizeof(*input_report))
+			break;
+
 		len = min(input_report->header.size,
 			  (u32)sizeof(input_dev->input_buf));
 		memcpy(input_dev->input_buf, input_report->buffer, len);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#7 lore raw

PATCH v2 7/10 HID: hid-goodix-spi: Validate package sizes in goodix_hid_irq()

In reply to Lee Jones

goodix_hid_irq() reads incoming SPI reports into the 95-byte ts->event_buf heap buffer. For the coordinate package, pkg->size is only checked against the 2-byte minimum header size with no upper bound check against GOODIX_HID_COOR_PKG_LEN (84 bytes), allowing an out-of-bounds read of up to 65533 bytes via hid_input_report(). For the trailing attachment package, neither the minimum header size nor the remaining report byte count is checked, allowing both a 32-bit unsigned length underflow when pkg->size < 2 and an out-of-bounds read past ts->event_buf.

Validate that the coordinate package size does not exceed GOODIX_HID_COOR_PKG_LEN, that the remaining report bytes hold a full package header before reading the attachment, and that the attachment package size lies within [GOODIX_HID_PKG_LEN_SIZE, rem_size].

Fixes 75e16c8ce283 ("HID: hid-goodix: Add Goodix HID-over-SPI driver")

---
v1 => v2:
- No changes.

 drivers/hid/hid-goodix-spi.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)
drivers/hid/hid-goodix-spi.c +24 -10
index a2c6b92f74ee..c8c158a74235 100644
@@ -574,7 +574,7 @@ static irqreturn_t goodix_hid_irq(int irq, void *data)
 	struct goodix_ts_data *ts = data;
 	struct goodix_hid_report_event *event;
 	struct goodix_hid_report_package *pkg;
-	u16 report_size;
+	u16 report_size, pkg_size, rem_size;
 
 	if (!test_bit(GOODIX_HID_STARTED, &ts->flags))
 		return IRQ_HANDLED;
@@ -600,13 +600,15 @@ static irqreturn_t goodix_hid_irq(int irq, void *data)
 		return IRQ_HANDLED;
 
 	pkg = (struct goodix_hid_report_package *)event->data;
-	if (le16_to_cpu(pkg->size) < GOODIX_HID_PKG_LEN_SIZE) {
-		dev_err(ts->dev, "invalid coordinate event package size, %d",
-			le16_to_cpu(pkg->size));
+	pkg_size = le16_to_cpu(pkg->size);
+	if (pkg_size < GOODIX_HID_PKG_LEN_SIZE ||
+	    pkg_size > GOODIX_HID_COOR_PKG_LEN) {
+		dev_err(ts->dev, "invalid coordinate event package size, %u\n",
+			pkg_size);
 		return IRQ_HANDLED;
 	}
 	hid_input_report(ts->hid, HID_INPUT_REPORT, pkg->data,
-			 le16_to_cpu(pkg->size) - GOODIX_HID_PKG_LEN_SIZE, 1);
+			 pkg_size - GOODIX_HID_PKG_LEN_SIZE, 1);
 
 	report_size = le16_to_cpu(event->hdr.size);
 	/* Check if there are other packages */
@@ -614,22 +616,34 @@ static irqreturn_t goodix_hid_irq(int irq, void *data)
 		return IRQ_HANDLED;
 
 	if (report_size >= ts->hid_max_event_sz) {
-		dev_err(ts->dev, "package size exceed limit %d vs %d",
+		dev_err(ts->dev, "package size exceed limit %d vs %d\n",
 			report_size, ts->hid_max_event_sz);
 		return IRQ_HANDLED;
 	}
 
+	rem_size = report_size - GOODIX_HID_COOR_PKG_LEN;
+	if (rem_size < GOODIX_HID_PKG_LEN_SIZE) {
+		dev_err(ts->dev, "invalid attachment data length, %u\n", rem_size);
+		return IRQ_HANDLED;
+	}
+
 	/* Read the package behind the coordinate data */
 	pkg = goodix_get_event_report(ts, ts->hid_report_addr + sizeof(*event),
-				      ts->event_buf,
-				      report_size - GOODIX_HID_COOR_PKG_LEN);
+				      ts->event_buf, rem_size);
 	if (!pkg) {
-		dev_err(ts->dev, "failed read attachment data content");
+		dev_err(ts->dev, "failed read attachment data content\n");
+		return IRQ_HANDLED;
+	}
+
+	pkg_size = le16_to_cpu(pkg->size);
+	if (pkg_size < GOODIX_HID_PKG_LEN_SIZE || pkg_size > rem_size) {
+		dev_err(ts->dev, "invalid attachment package size, %u vs %u\n",
+			pkg_size, rem_size);
 		return IRQ_HANDLED;
 	}
 
 	hid_input_report(ts->hid, HID_INPUT_REPORT, pkg->data,
-			 le16_to_cpu(pkg->size) - GOODIX_HID_PKG_LEN_SIZE, 1);
+			 pkg_size - GOODIX_HID_PKG_LEN_SIZE, 1);
 
 	return IRQ_HANDLED;
 }
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#8 lore raw

PATCH v2 8/10 HID: wacom: Stop HID hardware before releasing resources on mode change

In reply to Lee Jones

wacom_mode_change_work() runs asynchronously on the system workqueue without holding driver_input_lock and calls wacom_release_resources() before hid_hw_stop() on both the pen and touch interfaces. Because wacom_release_resources() releases the devres group containing pen_fifo, input devices, and shared tablet state while USB URBs and HID transport remain active, a concurrent wacom_raw_event() triggers a use-after-free.

Call hid_hw_stop() before wacom_release_resources() for both interfaces to match the teardown order in wacom_remove().

---
v1 => v2:
- Add Reviewed-by and Tested-by tags from Ping Cheng.

 drivers/hid/wacom_sys.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hid/wacom_sys.c +2 -2
index 2e3ce9b63d7a..5bacd016c686 100644
@@ -2890,16 +2890,16 @@ static void wacom_mode_change_work(struct work_struct *work)
 
 	if (pen) {
 		wacom1 = hid_get_drvdata(pen);
-		wacom_release_resources(wacom1);
 		hid_hw_stop(wacom1->hdev);
+		wacom_release_resources(wacom1);
 		wacom1->wacom_wac.has_mode_change = true;
 		wacom1->wacom_wac.is_direct_mode = is_direct;
 	}
 
 	if (touch) {
 		wacom2 = hid_get_drvdata(touch);
-		wacom_release_resources(wacom2);
 		hid_hw_stop(wacom2->hdev);
+		wacom_release_resources(wacom2);
 		wacom2->wacom_wac.has_mode_change = true;
 		wacom2->wacom_wac.is_direct_mode = is_direct;
 	}
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#9 lore raw

Re: [PATCH v2 08/10] HID: wacom: Stop HID hardware before releasing resources on mode change

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] HID: wacom: hid_hw_stop() without driver_input_lock in wacom_mode_change_work() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] HID: wacom: hid_hw_stop() without driver_input_lock in wacom_mode_change_work()
--

commit 8829d08a1079cac40d2eeee82adefcbf5c67e3e6
Author: Lee Jones <[email protected]>

HID: wacom: Stop HID hardware before releasing resources on mode change

This commit moves hid_hw_stop() ahead of wacom_release_resources() for both the pen and touch interfaces in wacom_mode_change_work().

Quoted 13 lines
drivers/hid/wacom_sys.c +1 -1
index 2d2a4d56a8da7..121bead90147c 100644
@@ -2887,16 +2887,16 @@ static void wacom_mode_change_work(struct work_struct *work)
 
 	if (pen) {
 		wacom1 = hid_get_drvdata(pen);
-		wacom_release_resources(wacom1);
 		hid_hw_stop(wacom1->hdev);
+		wacom_release_resources(wacom1);
 		wacom1->wacom_wac.has_mode_change = true;
 		wacom1->wacom_wac.is_direct_mode = is_direct;

drivers/hid/wacom_sys.c static void wacom_mode_change_work(struct work_struct *work) · line 2902

Quoted 10 lines
drivers/hid/wacom_sys.c +1 -1
 	}
 
 	if (touch) {
 		wacom2 = hid_get_drvdata(touch);
-		wacom_release_resources(wacom2);
 		hid_hw_stop(wacom2->hdev);
+		wacom_release_resources(wacom2);
 		wacom2->wacom_wac.has_mode_change = true;
 		wacom2->wacom_wac.is_direct_mode = is_direct;
 	}

[Severity: High]
Can calling hid_hw_stop() before wacom_release_resources() without holding hdev->driver_input_lock cause a use-after-free on hdev->hidraw?

wacom_probe() does not call hid_device_io_start(), so hdev->io_started remains false and hid_device_probe() releases hdev->driver_input_lock when probe finishes. When an input device is open in userspace, wacom_open() calls hid_hw_open(), starting the low-level transport (such as usbhid->urbin).

Previously, wacom_release_resources() unregistered the input devices first, invoking wacom_close() -> hid_hw_close() to stop the transport before hid_hw_stop() ran (when /dev/hidrawX was not open). Now hid_hw_stop() runs while the input devices are still open and delivering reports:

drivers/hid/hid-core.c:hid_hw_stop() {
	if (hdev->io_started)
		hid_device_io_stop(hdev);
	hid_disconnect(hdev);
	hdev->ll_driver->stop(hdev);
}

Because hdev->io_started is false, hid_hw_stop() skips hid_device_io_stop() (which would acquire driver_input_lock) and calls hid_disconnect() before ll_driver->stop() kills active URBs. If /dev/hidrawX is not open (hidraw->open == 0), hidraw_disconnect() -> drop_ref() frees hdev->hidraw before hdev->claimed is cleared:

CPU0 (kworker) CPU1 (softirq)
wacom_mode_change_work()
hid_hw_stop(hdev)

    hid_disconnect(hdev)
      hidraw_disconnect(hdev)
        drop_ref(hidraw, 1)
          kfree(hidraw)
                                  hid_input_report()
                                    down_trylock(&hdev->driver_input_lock)
                                    __hid_input_report()
                                      hid_report_raw_event()
                                        /* hdev->claimed & HID_CLAIMED_HIDRAW */
                                        hidraw_report_event()
                                          dev = hdev->hidraw;
                                          spin_lock_irqsave(&dev->list_lock)
                                          <-- use-after-free
      hdev->claimed = 0;
    hdev->ll_driver->stop(hdev)

Acquiring hdev->driver_input_lock around the teardown and re-registration (or calling hid_device_io_stop() before hid_hw_stop()) would serialize against concurrent hid_input_report() calls.

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

PATCH v2 9/10 HID: debug: Hold driver_input_lock while dumping raw report descriptor

In reply to Lee Jones

hid_debug_rdesc_show() reads hdev->rdesc and hdev->rsize and formats the raw report descriptor before acquiring hdev->driver_input_lock. When a HID driver with report_fixup() unbinds or HID-BPF reprobes the device, hid_device_remove() and __hid_device_probe() free hdev->rdesc and hdev->bpf_rdesc under hdev->driver_input_lock while the debugfs entry remains accessible, causing a use-after-free in hid_debug_rdesc_show().

Acquire hdev->driver_input_lock at the start of hid_debug_rdesc_show() before reading hdev->rdesc and hdev->rsize.

Fixes cef0d4948cb0 ("HID: debug: fix race condition with between rdesc_show() and device removal")

---
v1 => v2:
- No changes.

 drivers/hid/hid-debug.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/hid/hid-debug.c +7 -5
index f44e6e708404..56207dea4aa7 100644
@@ -3654,10 +3654,15 @@ static void hid_dump_input_mapping(struct hid_device *hid, struct seq_file *f)
 static int hid_debug_rdesc_show(struct seq_file *f, void *p)
 {
 	struct hid_device *hdev = f->private;
-	const __u8 *rdesc = hdev->rdesc;
-	unsigned rsize = hdev->rsize;
+	const __u8 *rdesc;
+	unsigned int rsize;
 	int i;
 
+	if (down_interruptible(&hdev->driver_input_lock))
+		return 0;
+
+	rdesc = hdev->rdesc;
+	rsize = hdev->rsize;
 	if (!rdesc) {
 		rdesc = hdev->dev_rdesc;
 		rsize = hdev->dev_rsize;
@@ -3669,9 +3674,6 @@ static int hid_debug_rdesc_show(struct seq_file *f, void *p)
 	seq_printf(f, "\n\n");
 
 	/* dump parsed data and input mappings */
-	if (down_interruptible(&hdev->driver_input_lock))
-		return 0;
-
 	hid_dump_device(hdev, f);
 	seq_printf(f, "\n");
 	hid_dump_input_mapping(hdev, f);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#11 lore raw

PATCH v2 10/10 HID: playstation: Cancel DualSense output worker on create failure

In reply to Lee Jones

dualsense_create() schedules ds->output_worker via dualsense_set_lightbar() before registering player LEDs and assigning a player ID. If either step fails, the error path returns without cancelling ds->output_worker, leaving queued or running work accessing ds after devres frees it in hid_device_probe().

Call dualsense_remove() in the dualsense_create() error path to disable further scheduling and cancel ds->output_worker synchronously.

Fixes 949aaccda019 ("HID: playstation: add DualSense player LED support.")
Fixes 182934a1e93b ("HID: playstation: stop DualSense output work on remove.")

---
v1 => v2:
- No changes.

 drivers/hid/hid-playstation.c | 1 +
 1 file changed, 1 insertion(+)
drivers/hid/hid-playstation.c +1 -0
index 24e448076e35..9650bbe233dc 100644
@@ -1906,6 +1906,7 @@ static struct ps_device *dualsense_create(struct hid_device *hdev)
 	return &ds->base;
 
 err:
+	dualsense_remove(ps_dev);
 	ps_devices_list_remove(ps_dev);
 	return ERR_PTR(ret);
 }
Signature · 1 line
2.56.1.839.g22a8290db9-goog