HID: Assorted bug fixes across HID core and drivers
Subsystems (14)
cover HID: Assorted bug fixes across HID core and drivers
These issues were reported by Sashiko.
None of these changes have been tested on physical hardware.
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 | 28 +++++++++++---- drivers/hid/wacom_sys.c | 4 +-- 10 files changed, 149 insertions(+), 72 deletions(-)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH 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().
--- drivers/hid/hid-core.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/hid/hid-core.c +12 -4
@@ -3216,18 +3216,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.750.gf70dc682cf-goog
PATCH 2/10 HID: mcp2221: Synchronise rxbuf access against raw_event callback
In reply to Lee Jonesmcp2221_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().
--- drivers/hid/hid-mcp2221.c | 47 ++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 21 deletions(-)
drivers/hid/hid-mcp2221.c +26 -21
@@ -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;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH 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.
--- drivers/hid/hid-logitech-hidpp.c | 58 +++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 20 deletions(-)
drivers/hid/hid-logitech-hidpp.c +38 -20
@@ -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.750.gf70dc682cf-goog
PATCH 4/10 HID: sensor-hub: Serialise synchronous read teardown under data->lock
In reply to Lee Jonessensor_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 free any prior hsdev->pending.raw_data before kmemdup() in sensor_hub_raw_event().
--- drivers/hid/hid-sensor-hub.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)
drivers/hid/hid-sensor-hub.c +21 -7
@@ -306,6 +306,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 ;
@@ -335,7 +338,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);
@@ -375,29 +381,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, } } }else { +kfree (hsdev->pending.raw_data); hsdev->pending.raw_data =kmemdup (ptr, sz,GFP_ATOMIC );if (hsdev->pending.raw_data) hsdev->pending.raw_size = sz;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH 5/10 HID: roccat: pyra: Resolve USB device from parent in actual_profile
In reply to Lee Jonespyra_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, matchingpyra_sysfs_show_firmware_version().
--- drivers/hid/hid-roccat-pyra.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/hid/hid-roccat-pyra.c +6 -3
@@ -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.750.gf70dc682cf-goog
PATCH 6/10 HID: hyperv: Validate VMBus packet and payload bounds on receive
In reply to Lee Jonesmousevsc_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.
--- drivers/hid/hid-hyperv.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
drivers/hid/hid-hyperv.c +12 -0
@@ -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.750.gf70dc682cf-goog
PATCH 7/10 HID: hid-goodix-spi: Validate package sizes in goodix_hid_irq()
In reply to Lee Jonesgoodix_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].
--- drivers/hid/hid-goodix-spi.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)
drivers/hid/hid-goodix-spi.c +24 -10
@@ -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.750.gf70dc682cf-goog
PATCH 8/10 HID: wacom: Stop HID hardware before releasing resources on mode change
In reply to Lee Joneswacom_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().
--- drivers/hid/wacom_sys.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hid/wacom_sys.c +2 -2
@@ -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; }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.750.gf70dc682cf-goog
PATCH 9/10 HID: debug: Hold driver_input_lock while dumping raw report descriptor
In reply to Lee Joneshid_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.
--- drivers/hid/hid-debug.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/hid/hid-debug.c +7 -5
@@ -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.750.gf70dc682cf-goog
PATCH 10/10 HID: playstation: Cancel DualSense output worker on create failure
In reply to Lee Jonesdualsense_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.
--- drivers/hid/hid-playstation.c | 1 + 1 file changed, 1 insertion(+)
drivers/hid/hid-playstation.c +1 -0
@@ -1905,6 +1905,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.750.gf70dc682cf-goog
[email protected]
AI reviewRe: [PATCH 08/10] HID: wacom: Stop HID hardware before releasing resources on mode change
In reply to Lee JonesThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] HID: wacom: resources released…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] HID: wacom: resources released before hid_hw_stop() in wacom_wireless_work() [1]
--
commit 32ce19fdb19359280af8da0840cd4996821af3f2
Author: Lee Jones <[email protected]>
HID: wacom: Stop HID hardware before releasing resources on mode change
This commit stops the pen and touch HID interfaces before calling wacom_release_resources() in wacom_mode_change_work() to prevent a concurrent wacom_raw_event() from accessing freed resources.
Quoted 13 lines
drivers/hid/wacom_sys.c +1
-1@@ -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: Critical]
This problem wasn't introduced by this patch, but does wacom_wireless_work() have the same issue [1] where wacom_release_resources() is called on wacom1 and wacom2 while their HID interfaces remain started?
drivers/hid/wacom_sys.c:wacom_wireless_work() {
...
/* Stylus interface */
hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
wacom1 = hid_get_drvdata(hdev1);
wacom_wac1 = &(wacom1->wacom_wac);
wacom_release_resources(wacom1);
/* Touch interface */
hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
wacom2 = hid_get_drvdata(hdev2);
wacom_wac2 = &(wacom2->wacom_wac);
wacom_release_resources(wacom2);
...
}When a wireless tablet connects or disconnects, wacom_release_resources() releases the devres group for both interfaces before hid_hw_stop() is called (and hid_hw_stop() is never called on disconnect, on an unknown PID, on hdev2 for pen-only tablets, or on the fail path after hdev1 has restarted). Because wacom_wac->features remains populated while the HID hardware is active, an incoming report in wacom_raw_event() can dereference the cleared shared or input pointers or access the freed pen_fifo and input devices, causing a NULL pointer dereference or use-after-free.
[1] https://sashiko.dev/#/bug/linux-0154f73a-a980-4d2c-8f09-407e588762d9
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
[email protected]
AI reviewRe: [PATCH 04/10] HID: sensor-hub: Serialise synchronous read teardown under data->lock
In reply to Lee JonesThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] HID: sensor-hub: use-after-free in…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] HID: sensor-hub: use-after-free in sensor_hub_raw_event()
[1]
--
commit 60026af9c0858d9a91fa3bec4905b88a658f6493
Author: Lee Jones <[email protected]>
HID: sensor-hub: Serialise synchronous read teardown under data->lock
This commit acquires data->lock when tearing down synchronous read state in sensor_hub_input_attr_read_values() and sensor_hub_input_attr_get_raw_value(), and frees any prior hsdev->pending.raw_data before allocating a new buffer in sensor_hub_raw_event().
Quoted 4 lines
drivers/hid/hid-sensor-hub.c +0
-0
[ ... ]
drivers/hid/hid-sensor-hub.c static int sensor_hub_raw_event(struct hid_device *hdev, · line 644
Quoted 8 lines
drivers/hid/hid-sensor-hub.c +1
-0@@ -625,6 +638,7 @@ static int sensor_hub_raw_event(struct hid_device *hdev, } } }else { +kfree (hsdev->pending.raw_data); hsdev->pending.raw_data =kmemdup (ptr, sz,GFP_ATOMIC );if (hsdev->pending.raw_data) hsdev->pending.raw_size = sz;
[Severity: Critical]
This problem wasn't introduced by this patch, but can sensor_hub_raw_event() race with sensor_hub_remove_callback() [1] and trigger a use-after-free on callback?
sensor_hub_get_callback() returns callback->pcallbacks after dropping pdata->dyn_callback_lock, while sensor_hub_remove_callback() only takes pdata->dyn_callback_lock before freeing the entry and returning to the client driver teardown path:
drivers/hid/hid-sensor-hub.c:sensor_hub_remove_callback() {
...
spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
list_for_each_entry(callback, &pdata->dyn_callback_list, list)
if (callback->usage_id == usage_id &&
callback->hsdev == hsdev) {
list_del(&callback->list);
kfree(callback);
break;
}
spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
...
}If a client driver unbinds or fails probe while an incoming report is being processed, the client driver frees its callback structure and driver data while sensor_hub_raw_event() still holds the returned pointer:
CPU0 CPU1
sensor_hub_raw_event()
spin_lock(&pdata->lock) callback = sensor_hub_get_callback()sensor_hub_remove_callback()
spin_lock(&pdata->dyn_callback_lock)
list_del(&callback->list)
kfree(callback)
spin_unlock(&pdata->dyn_callback_lock)
// client driver frees callback and priv
callback->capture_sample(...)
<-- use-after-freeAcquiring pdata->lock around the removal in sensor_hub_remove_callback(), or holding pdata->dyn_callback_lock across the callback invocations, would serialize teardown against in-flight events.
[1] https://sashiko.dev/#/bug/linux-a1e2f25c-d273-4b88-b53e-c38049ea2d5d
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH 03/10] HID: logitech-hidpp: Synchronise send_receive_buf against raw events
In reply to Lee JonesThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] HID: logitech-hidpp: out-of-bounds…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] HID: logitech-hidpp: out-of-bounds read in hidpp_raw_hidpp_event() [1]
--
commit 289546ca1e2e40f3f7b8470e29a3d15053ba75d0
Author: Lee Jones <[email protected]>
HID: logitech-hidpp: Synchronise send_receive_buf against raw events
This commit adds a spinlock to synchronize access to send_receive_buf and answer_available between __do_hidpp_send_message_sync() and hidpp_raw_hidpp_event().
Quoted 4 lines
drivers/hid/hid-logitech-hidpp.c +0
-0
[ ... ]
Quoted 13 lines
drivers/hid/hid-logitech-hidpp.c +4
-2@@ -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 ;
drivers/hid/hid-logitech-hidpp.c static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data, · line 4152
Quoted 10 lines
drivers/hid/hid-logitech-hidpp.c +2
-3-/* * 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;
[Severity: Critical]
This problem wasn't introduced by this patch, but can hidpp_raw_hidpp_event() read past the end of data [1] when an undersized HID++ report arrives? The function casts data to struct hidpp_report without validating the size parameter, so matching and copying *answer = *report unconditionally reads sizeof(struct hidpp_report) bytes from the incoming buffer, and the 27 MHz encryption-key check further below likewise reads data[0], data[2], and data[3] without checking that size >= 4.
[1] https://sashiko.dev/#/bug/linux-f6721ec8-6f81-400f-a691-6d48de7fd9b5
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Srinivas Pandruvada
maintainerRe: [PATCH 04/10] HID: sensor-hub: Serialise synchronous read teardown under data->lock
In reply to Lee JonesQuoted 108 lines · Lee Jones
sensor_hub_input_attr_read_values()andsensor_hub_input_attr_get_raw_value()sethsdev->pending.statusunderdata->lock, sleep inwait_for_completion_interruptible_timeout(), and then clearhsdev->pending.statusand consume or freehsdev->pending.raw_datawithout acquiring data->lock. When a wait is interrupted by a signal or times out whilesensor_hub_raw_event()holds
the same spinlock,sensor_hub_raw_event()canmemcpy()into the caller's popped stack buffer insensor_hub_input_attr_read_values()or
race withkfree()and leakkmemdup()allocations insensor_hub_input_attr_get_raw_value().Hold
data->lockwhen clearinghsdev->pending.statusand detachinghsdev->pending.raw_datain both helpers, and free any priorhsdev->pending.raw_databeforekmemdup()insensor_hub_raw_event().Fixes 401ca24fb34a ("HID: sensors: introduce sensor framework")Fixes f784fcea4506 ("HID: sensor-hub: Add
sensor_hub_input_attr_read_values()for multi-byte reads") Closes:
https://sashiko.dev/#/bug/linux-5bd5ea7c-72f9-4f22-ae1d-00915390f085Signed-off-by Lee Jones <[email protected]>---
drivers/hid/hid-sensor-hub.c | 28 +++++++++++++++++++++-------1 file changed, 21 insertions(+), 7 deletions(-)drivers/hid/hid-sensor- renamed from drivers/hid/hid-sensor-hub.c +0
-0
hub.c
index81c8e723f9ba..ea846e0bb301100644drivers/hid/hid-sensor-hub.c +0
-0@@ -306,6 +306,9 @@ int sensor_hub_input_attr_read_values(structhid_sensor_hub_device *hsdev, long cycles; int ret;drivers/hid/hid-sensor-hub.c +3
-0+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;drivers/hid/hid-sensor-hub.c +0
-0@@ -335,7 +338,10 @@ int sensor_hub_input_attr_read_values(struct hid_sensor_hub_device *hsdev, else if (cycles < 0) ret = cycles;drivers/hid/hid-sensor-hub.c +1
-0+spin_lock_irqsave (&data->lock, flags);
hsdev->pending.status= false;drivers/hid/hid-sensor-hub.c +2
-0+ hsdev->pending.raw_data =NULL ; +spin_unlock_irqrestore (&data->lock, flags);}
mutex_unlock(hsdev->mutex_ptr);drivers/hid/hid-sensor-hub.c +2
-0@@ -375,29 +381,36 @@ int sensor_hub_input_attr_get_raw_value(structhid_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-
Quoted 1 line
pending.ready, HZ*5);
drivers/hid/hid-sensor-hub.c +3
-1-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)drivers/hid/hid-sensor-hub.c +0
-1- ret_val = *(s8 *)hsdev-Quoted 1 line
pending.raw_data;
drivers/hid/hid-sensor-hub.c +1
-0+ ret_val = *(s8 *)raw_data;else
drivers/hid/hid-sensor-hub.c +0
-1- ret_val = *(u8 *)hsdev-Quoted 1 line
pending.raw_data;
drivers/hid/hid-sensor-hub.c +1
-0+ ret_val = *(u8 *)raw_data;break;
case 2:
if (is_signed)drivers/hid/hid-sensor-hub.c +0
-1- ret_val = *(s16 *)hsdev-Quoted 1 line
pending.raw_data;
drivers/hid/hid-sensor-hub.c +1
-0+ ret_val = *(s16 *)raw_data;else
drivers/hid/hid-sensor-hub.c +0
-1- ret_val = *(u16 *)hsdev-Quoted 1 line
pending.raw_data;
drivers/hid/hid-sensor-hub.c +1
-0+ ret_val = *(u16 *)raw_data;break;
case 4:drivers/hid/hid-sensor-hub.c +1
-1- ret_val = *(u32 *)hsdev->pending.raw_data; + ret_val = *(u32 *)raw_data;break;
default:
ret_val= 0;
}drivers/hid/hid-sensor-hub.c +2
-1-kfree (hsdev->pending.raw_data); + hsdev->pending.raw_data =NULL ; + hsdev->pending.raw_size =0 ;
hsdev->pending.status= false;drivers/hid/hid-sensor-hub.c +2
-0+spin_unlock_irqrestore (&data->lock, flags); +kfree (raw_data);}
mutex_unlock(hsdev->mutex_ptr);
drivers/hid/hid-sensor-hub.c static int sensor_hub_raw_event(struct hid_device · line 607
Quoted 5 lines
drivers/hid/hid-sensor-hub.c +1
-0@@ -590,6 +603,7 @@ static int sensor_hub_raw_event(struct hid_device *hdev, } } } else { +kfree (hsdev->pending.raw_data);
With this patch the waiter clears pending.status under data->lock on timeout/signal, so sensor_hub_raw_event() can no longer kmemdup() after the waiter has given up. I don't think the kfree() in sensor_hub_raw_event() is needed for the race described in the commit message.
The only case it covers is a spurious second matching report arriving after complete() but before the waiter takes the lock. That is a separate, pre-existing leak. Rather than freeing in the event path, I think clearing pending.status in sensor_hub_raw_event() when completing the request, so only the first matching report is consumed and raw_data is allocated at most once:
} else {
hsdev->pending.raw_data = kmemdup(ptr,
sz, GFP_ATOMIC);
...
---> hsdev->pending.status = false;
complete(&hsdev->pending.ready);
}BTW
The pre-existing callback removal race reported by Sashiko is already addressed by commit abe10ad9c5fd ("HID: sensor-hub: Synchronize callback removal with raw events") in linux-next (hid tree).
Thanks,
Srinivas
Quoted 5 lines
hsdev->pending.raw_data=kmemdup(ptr, sz, GFP_ATOMIC);
if (hsdev->pending.raw_data)hsdev->pending.raw_size= sz;