Linux Input/HID development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Haoxiang Li" <haoxiang_li2024@163.com>
Cc: dmitry.torokhov@gmail.com, linux-input@vger.kernel.org
Subject: Re: [PATCH v3 2/2] HID: sensor: custom: Fix field sysfs group cleanup on failure
Date: Tue, 07 Jul 2026 08:04:33 +0000	[thread overview]
Message-ID: <20260707080434.50D5B1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260707071545.3087073-3-haoxiang_li2024@163.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Lifetime mismatch between `devm_kzalloc` allocated `sensor_inst` and `miscdevice` file operations leads to a Use-After-Free.
- [High] Race condition in `hid_sensor_custom_remove()` between event callback removal and kfifo destruction allows Use-After-Free.
--

--- Patch [2]: [PATCH v3 2/2] HID: sensor: custom: Fix field sysfs group cleanup on failure ---
commit aa8aaa0518fc58fa29db9643742276c6a56cafe6
Author: Haoxiang Li <haoxiang_li2024@163.com>

HID: sensor: custom: Fix field sysfs group cleanup on failure
    
hid_sensor_custom_add_attributes() creates one sysfs group for each
custom sensor field. If sysfs_create_group() fails after some groups
have already been created, the function returns the error without
removing the previously created groups.
    
Add a local unwind path to remove the groups that were already created.
With enable_sensor exposed only after the field attributes are ready,
this path can free sensor_inst->fields without leaving enable_sensor
able to access pointers into that array.

Fixes: 4a7de0519df5 ("HID: sensor: Custom and Generic sensor support")
Cc: stable@vger.kernel.org

> diff --git a/drivers/hid/hid-sensor-custom.c b/drivers/hid/hid-sensor-custom.c
> index 6b0da2e0e1c93..c2b425afd951b 100644
> --- a/drivers/hid/hid-sensor-custom.c
> +++ b/drivers/hid/hid-sensor-custom.c
> @@ -609,7 +609,7 @@ static int hid_sensor_custom_add_attributes(struct hid_sensor_custom
[ ... ]
>  	return ret;
> +
> +err_remove_groups:
> +	while (--i >= 0)
> +		sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
> +				   &sensor_inst->fields[i].hid_custom_attribute_group);
> +	kfree(sensor_inst->fields);
> +	return ret;
>  }
>  
>  static void hid_sensor_custom_remove_attributes(struct hid_sensor_custom *

[Severity: High]
This is a pre-existing issue, but is there a lifetime mismatch between the
devm_kzalloc() allocated sensor_inst and the miscdevice file operations?

In hid_sensor_custom_probe(), sensor_inst is allocated with device-managed
memory:

    sensor_inst = devm_kzalloc(&pdev->dev, sizeof(*sensor_inst),
                               GFP_KERNEL);

When the device is unbound, devres will automatically free sensor_inst.
However, misc_deregister() does not revoke existing open file descriptors.

If a user has the character device open, unbinds the device, and then
interacts with the file descriptor, could this lead to a use-after-free?

For example, hid_sensor_custom_read() accesses the freed pointer:

    sensor_inst = container_of(file->private_data,
                               struct hid_sensor_custom, custom_dev);

Similarly, closing the file calls hid_sensor_custom_release() which modifies
the freed memory:

    clear_bit(0, &sensor_inst->misc_opened);

[Severity: High]
This is a pre-existing issue, but is there a race condition in
hid_sensor_custom_remove() during device teardown?

The function first frees the data buffer:

    hid_sensor_custom_dev_if_remove(sensor_inst); // calls kfifo_free()

It removes the callback later in the function:

    sensor_hub_remove_callback(hsdev, hsdev->usage);

If the sensor hub receives an asynchronous hardware event between these two
calls, wouldn't the still-active callback invoke hid_sensor_capture_sample()
and write incoming hardware data to the freed data_fifo?

    kfifo_in(&sensor_inst->data_fifo, (unsigned char *)raw_data,
             raw_len);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260707071545.3087073-1-haoxiang_li2024@163.com?part=2

  reply	other threads:[~2026-07-07  8:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07  7:15 [PATCH v3 0/2] HID: sensor: custom: Fix fields lifetime issues Haoxiang Li
2026-07-07  7:15 ` [PATCH v3 1/2] HID: sensor: custom: Fix use-after-free in enable_sensor Haoxiang Li
2026-07-07  7:50   ` sashiko-bot
2026-07-08 17:58   ` srinivas pandruvada
2026-07-07  7:15 ` [PATCH v3 2/2] HID: sensor: custom: Fix field sysfs group cleanup on failure Haoxiang Li
2026-07-07  8:04   ` sashiko-bot [this message]
2026-07-08 17:58   ` srinivas pandruvada
2026-07-11 23:59 ` [PATCH v3 0/2] HID: sensor: custom: Fix fields lifetime issues Jonathan Cameron

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=20260707080434.50D5B1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=haoxiang_li2024@163.com \
    --cc=linux-input@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox