From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Jiri Kosina <jikos@kernel.org>,
Jonathan Cameron <jic23@kernel.org>,
Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>,
Benjamin Tissoires <bentiss@kernel.org>,
Zhang Lixu <lixu.zhang@intel.com>,
Andy Shevchenko <andriy.shevchenko@intel.com>,
linux-input@vger.kernel.org, linux-iio@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH v2 04/10] HID: sensor-hub: Serialise synchronous read teardown under data->lock
Date: Fri, 27 Nov 2026 17:06:22 +0000 [thread overview]
Message-ID: <20261127170712.3163443-5-lee@kernel.org> (raw)
In-Reply-To: <20261127170712.3163443-1-lee@kernel.org>
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")
Closes: https://sashiko.dev/#/bug/linux-5bd5ea7c-72f9-4f22-ae1d-00915390f085
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
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(-)
diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-hub.c
index 2a7f2475171f..180a913888a4 100644
--- a/drivers/hid/hid-sensor-hub.c
+++ b/drivers/hid/hid-sensor-hub.c
@@ -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);
}
}
--
2.56.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-11-27 17:07 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 17:06 [PATCH v2 00/10] HID: Fix race conditions, UAFs, and bounds bugs Lee Jones
2026-11-27 17:06 ` [PATCH v2 01/10] HID: core: Protect hid->inputs traversal in hid_check_keys_pressed() Lee Jones
2026-11-27 17:06 ` [PATCH v2 02/10] HID: mcp2221: Synchronise rxbuf access against raw_event callback Lee Jones
2026-11-27 17:06 ` [PATCH v2 03/10] HID: logitech-hidpp: Synchronise send_receive_buf against raw events Lee Jones
2026-11-27 17:06 ` Lee Jones [this message]
2026-11-27 17:06 ` [PATCH v2 05/10] HID: roccat: pyra: Resolve USB device from parent in actual_profile Lee Jones
2026-11-27 17:06 ` [PATCH v2 06/10] HID: hyperv: Validate VMBus packet and payload bounds on receive Lee Jones
2026-11-27 17:06 ` [PATCH v2 07/10] HID: hid-goodix-spi: Validate package sizes in goodix_hid_irq() Lee Jones
2026-11-27 17:06 ` [PATCH v2 08/10] HID: wacom: Stop HID hardware before releasing resources on mode change Lee Jones
2026-11-27 17:31 ` sashiko-bot
2026-11-27 17:06 ` [PATCH v2 09/10] HID: debug: Hold driver_input_lock while dumping raw report descriptor Lee Jones
2026-11-27 17:06 ` [PATCH v2 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=20261127170712.3163443-5-lee@kernel.org \
--to=lee@kernel.org \
--cc=andriy.shevchenko@intel.com \
--cc=bentiss@kernel.org \
--cc=jic23@kernel.org \
--cc=jikos@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lixu.zhang@intel.com \
--cc=srinivas.pandruvada@linux.intel.com \
--cc=stable@vger.kernel.org \
/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.