Linux Input/HID development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tristan Madani" <tristmd@gmail.com>
Cc: linux-input@vger.kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH] HID: core: fix device cleanup on allocation failure
Date: Wed, 19 Aug 2026 01:52:47 +0000	[thread overview]
Message-ID: <20260819015247.ACBD51F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260819013149.889913-1-tristmd@gmail.com>

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

Pre-existing issues:
- [High] Concurrent `hidraw` operations can cause a use-after-free on the SRCU structure during device destruction because `hid_bpf_destroy_device()` runs before `hid_remove_device()`.
--

--- Patch [1]: [PATCH] HID: core: fix device cleanup on allocation failure ---
commit ef17fd6111206c1e7e801f388343922186fcf0dd
Author: Tristan Madani <tristan@talencesecurity.com>

HID: core: fix device cleanup on allocation failure

This commit replaces hid_destroy_device() with put_device() in the error
path of hid_allocate_device(). This prevents a general protection fault
caused by cleaning up an uninitialized SRCU structure when 
hid_bpf_device_init() fails.

> diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
> index cf123347a2af7..9534de5317783 100644
> --- a/drivers/hid/hid-core.c
> +++ b/drivers/hid/hid-core.c
> @@ -3060,7 +3060,12 @@ struct hid_device *hid_allocate_device(void)
>  	return hdev;
>  
>  out_err:
> -	hid_destroy_device(hdev);

[Severity: High]
This is a pre-existing issue, but looking at the implementation of 
hid_destroy_device(), it destroys the BPF structures before unregistering
the device:

void hid_destroy_device(struct hid_device *hdev)
{
	hid_bpf_destroy_device(hdev);
	hid_remove_device(hdev);
	put_device(&hdev->dev);
}

Can this order cause a use-after-free on the SRCU structure during a
concurrent device teardown? 

If a userspace process writes to the hidraw device concurrently with a 
hot-unplug, the hidraw writer could check the destroyed flag and then get 
preempted before taking the SRCU lock:

CPU1 (hidraw writer)
dispatch_hid_bpf_raw_requests()
	if (unlikely(hdev->bpf.destroyed))
		return -ENODEV;
	<preempted>

CPU2 (device unplug)
hid_destroy_device()
	hid_bpf_destroy_device()
		// sets bpf.destroyed = true
		cleanup_srcu_struct(&hdev->bpf.srcu);
	hid_remove_device() 
		// blocks waiting for CPU1

CPU1 (resumes)
	idx = srcu_read_lock(&hdev->bpf.srcu); 

Could CPU1 resume and dereference the freed SRCU internal pointers? 

Should hid_remove_device() be called before hid_bpf_destroy_device() to 
ensure userspace interfaces like hidraw are disconnected before the SRCU 
structure is destroyed?

> +	/*
> +	 * hid_destroy_device() cannot be used here because
> +	 * hid_bpf_device_init() failed and the SRCU struct was never
> +	 * initialized.  Release the device directly.
> +	 */
> +	put_device(&hdev->dev);
>  	return ERR_PTR(ret);
>  }
>  EXPORT_SYMBOL_GPL(hid_allocate_device);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260819013149.889913-1-tristmd@gmail.com?part=1

  reply	other threads:[~2026-08-19  1:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-19  1:31 [PATCH] HID: core: fix device cleanup on allocation failure Tristan Madani
2026-08-19  1:52 ` sashiko-bot [this message]
2026-08-19  5:52 ` Greg KH

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=20260819015247.ACBD51F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tristmd@gmail.com \
    /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