BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Xiang Mei" <xmei5@asu.edu>
Cc: linux-input@vger.kernel.org, bpf@vger.kernel.org,
	dmitry.torokhov@gmail.com
Subject: Re: [PATCH 2/2] HID: bpf: don't allocate the event buffer for an unbound device
Date: Mon, 30 Nov 2026 22:21:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-242099@kernel.org> (raw)
In-Reply-To: <20261130220315.659533-3-xmei5@asu.edu>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] HID: bpf: ZERO_SIZE_PTR alloc in __hid_bpf_allocate_data() when
  reports empty
- [Medium] HID: bpf: unsynchronized HID_STAT_PARSED check in
  hid_bpf_reg() races unbind
- [Medium] HID: bpf: missing event buffer alloc when hid_hw_start()
  skips hid_connect()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] HID: bpf: data race in hid_bpf_disconnect_device() [1]
--

commit 0ebd0ca34564242602066bbb7c6f117841d9ff5a
Author: Xiang Mei <xmei5@asu.edu>

HID: bpf: don't allocate the event buffer for an unbound device

This commit gates hid_bpf_allocate_event_data() in hid_bpf_reg() on
HID_STAT_PARSED so that hdev->bpf.device_data is not allocated as
ZERO_SIZE_PTR when attaching a hid_device_event program to an unbound device.

> diff --git a/drivers/hid/bpf/hid_bpf_struct_ops.c b/drivers/hid/bpf/hid_bpf_struct_ops.c
> index c90b68956cb3b..1039adeac8ae2 100644
> --- a/drivers/hid/bpf/hid_bpf_struct_ops.c
> +++ b/drivers/hid/bpf/hid_bpf_struct_ops.c
> @@ -216,7 +216,7 @@ static int hid_bpf_reg(void *kdata, struct bpf_link *link)
>  		hdev->bpf.rdesc_ops = ops;
>  	}
>  
> -	if (ops->hid_device_event) {
> +	if (ops->hid_device_event && (hdev->status & HID_STAT_PARSED)) {
>  		err = hid_bpf_allocate_event_data(hdev);
>  		if (err)
>  			goto out_unlock;

[Severity: High]
Can __hid_bpf_allocate_data() still store ZERO_SIZE_PTR in
hdev->bpf.device_data even when HID_STAT_PARSED is set?

hid_open_report() -> hid_parse_collections() sets HID_STAT_PARSED even when
the report descriptor defines no reports (or only zero-length unnumbered
reports), and hidraw_connect() still claims the device during hid_connect().
When hid_bpf_allocate_event_data() runs on such a device, max_report_len is
0, so __hid_bpf_allocate_data() calls kzalloc(0, GFP_KERNEL) and sets
hdev->bpf.device_data to ZERO_SIZE_PTR with allocated_data == 0.  Delivering
an input report then hits the same memcpy() into ZERO_SIZE_PTR in
dispatch_hid_bpf_device_event().

[Severity: Medium]
What prevents hid_bpf_reg() from racing with hid_device_remove() and
hid_close_report() while checking HID_STAT_PARSED and walking
hdev->report_enum?

hid_bpf_reg() holds only hdev->bpf.prog_list_lock, whereas probe and remove
update hdev->report_enum and HID_STAT_PARSED under device_lock(&hdev->dev)
and hdev->driver_input_lock.  During unbind, hid_disconnect() calls
hid_bpf_disconnect_device() before hid_close_report() frees the reports and
clears HID_STAT_PARSED at the very end:

CPU0                                  CPU1
hid_device_remove()
  hid_hw_stop()
    hid_disconnect()
      hid_bpf_disconnect_device()
  ...
  hid_close_report()
    hid_free_report(report)
                                      hid_bpf_reg()
                                        if (hdev->status & HID_STAT_PARSED)
                                          hid_bpf_allocate_event_data()
                                            __hid_bpf_allocate_data()
                                              hid_report_len(report)
                                                <-- use-after-free
    memset(report_enum, 0, ...)
    device->status &= ~HID_STAT_PARSED

If __hid_bpf_allocate_data() runs after memset(report_enum, 0, ...) and
before HID_STAT_PARSED is cleared, it sees all reports zeroed and allocates
ZERO_SIZE_PTR into hdev->bpf.device_data after hid_bpf_disconnect_device()
has already run, crashing on the next input report after rebind.  If it runs
before hid_close_report() frees the reports (or during a failing
__hid_device_probe() after hid_open_report()), hdev->bpf.device_data is left
allocated on the unbound device.

[Severity: Medium]
Does skipping hid_bpf_allocate_event_data() here leave hdev->bpf.device_data
NULL for drivers that do not call hid_connect()?

When a hid_device_event program is attached while the device is unbound (or
reprobed via hid_rdesc_fixup), allocation is deferred to
hid_bpf_connect_device(), which is only called from hid_connect().  Drivers
such as hid-ft260, hid-mcp2200, hid-mcp2221 and hid-picolcd_core call
hid_parse() and hid_hw_start(hdev, 0) without calling hid_connect(), so
hdev->bpf.device_data stays NULL and dispatch_hid_bpf_device_event() silently
skips all attached hid_device_event programs.

[Severity: Critical]
This problem wasn't introduced by this patch, but nothing serializes
[1] hid_bpf_allocate_event_data() in hid_bpf_reg() against
hid_bpf_disconnect_device() during device unbind:

CPU0                                  CPU1
                                      hid_bpf_reg()
                                        mutex_lock(&hdev->bpf.prog_list_lock)
                                        hid_bpf_allocate_event_data()
                                          hdev->bpf.device_data = alloc_data
hid_bpf_disconnect_device()
  kfree(hdev->bpf.device_data)
  hdev->bpf.device_data = NULL
                                        mutex_unlock(&hdev->bpf.prog_list_lock)

hid_bpf_disconnect_device() runs under hdev->driver_input_lock without taking
hdev->bpf.prog_list_lock, so concurrent attach and unbind race directly on
hdev->bpf.device_data and hdev->bpf.allocated_data, either freeing the newly
allocated buffer while attach is in progress or leaking it if the allocation
stores into hdev->bpf.device_data after hid_bpf_disconnect_device() returns.

[1] https://sashiko.dev/#/bug/linux-a4c268e1-ac73-484a-9c4c-064a698eb6e6

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130220315.659533-1-xmei5@asu.edu?part=2

      reply	other threads:[~2026-11-30 22:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 22:03 [PATCH 0/2] HID: bpf: fix stale device_event buffer reuse Xiang Mei
2026-11-30 22:03 ` [PATCH 1/2] HID: core: release the HID-BPF event buffer on hid_connect()'s no-listeners exit Xiang Mei
2026-11-30 22:21   ` sashiko-bot
2026-11-30 22:03 ` [PATCH 2/2] HID: bpf: don't allocate the event buffer for an unbound device Xiang Mei
2026-11-30 22:21   ` sashiko-bot [this message]

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=sashiko-outbox-242099@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=xmei5@asu.edu \
    /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