Re: [PATCH 04/10] HID: sensor-hub: Serialise synchronous read teardown under data->lock

From: Srinivas Pandruvada

Date: Mon Nov 23 2026 - 15:08:55 EST


On Mon, 2026-11-23 at 15:50 +0000, Lee Jones wrote:
> 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 free any prior
> hsdev->pending.raw_data before kmemdup() in sensor_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-00915390f085
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
> ---
>  drivers/hid/hid-sensor-hub.c | 28 +++++++++++++++++++++-------
>  1 file changed, 21 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-
> hub.c
> index 81c8e723f9ba..ea846e0bb301 100644
> --- a/drivers/hid/hid-sensor-hub.c
> +++ b/drivers/hid/hid-sensor-hub.c
> @@ -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);

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


>   hsdev->pending.raw_data =
> kmemdup(ptr, sz, GFP_ATOMIC);
>   if (hsdev->pending.raw_data)
>   hsdev->pending.raw_size =
> sz;