* [PATCH 0/2] HID: bpf: fix stale device_event buffer reuse
@ 2026-11-30 22:03 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:03 ` [PATCH 2/2] HID: bpf: don't allocate the event buffer for an unbound device Xiang Mei
0 siblings, 2 replies; 5+ messages in thread
From: Xiang Mei @ 2026-11-30 22:03 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: linux-input, bpf, linux-kernel, Sashiko, co+86678873d3803945,
co+9a1ebd50dd939705, Xiang Mei
hdev->bpf.device_data is allocated once, sized from the report descriptor
parsed at that moment, and then reused: hid_bpf_allocate_event_data()
returns early whenever the pointer is already set. If the buffer
outlives the descriptor it was sized for, the next probe binds with a
larger descriptor and dispatch_hid_bpf_device_event() copies a full
report into the stale buffer.
Two paths leave such a buffer behind:
1/2 hid_connect()'s "no listeners" exit returns without freeing the
buffer it just allocated (KASAN slab-out-of-bounds write of 1024
bytes into kmalloc-64). The leak was also pointed out by the
sashiko review in [1].
2/2 hid_bpf_reg() allocates for an unbound device, whose report enums
are empty, so the buffer is ZERO_SIZE_PTR with allocated_data 0
(NULL-pointer write at 0x10).
Both are reachable from userspace with a uhid device and HID-BPF
struct_ops programs using ->hid_rdesc_fixup and ->hid_device_event.
Each patch was tested against its own reproducer on a KASAN kernel: the
crash fires without the patch and does not with it.
These fix the buffer lifetime and are independent of the pending
change that bounds the memcpy() itself [2]; that one is still needed
for uhid reports larger than the descriptor allows.
[1] https://lore.kernel.org/all/20260908202056.698371F00A3A@smtp.kernel.org/
[2] https://lore.kernel.org/all/20261102112630.53400-1-alon@botanica.software/
Xiang Mei (2):
HID: core: release the HID-BPF event buffer on hid_connect()'s
no-listeners exit
HID: bpf: don't allocate the event buffer for an unbound device
drivers/hid/bpf/hid_bpf_struct_ops.c | 2 +-
drivers/hid/hid-core.c | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH 1/2] HID: core: release the HID-BPF event buffer on hid_connect()'s no-listeners exit 2026-11-30 22:03 [PATCH 0/2] HID: bpf: fix stale device_event buffer reuse Xiang Mei @ 2026-11-30 22:03 ` 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 1 sibling, 1 reply; 5+ messages in thread From: Xiang Mei @ 2026-11-30 22:03 UTC (permalink / raw) To: Jiri Kosina, Benjamin Tissoires Cc: linux-input, bpf, linux-kernel, Sashiko, co+86678873d3803945, co+9a1ebd50dd939705, Xiang Mei, stable hid_connect() allocates hdev->bpf.device_data in hid_bpf_connect_device(), but its "device has no listeners" exit returns -ENODEV without freeing it. hid_hw_start() then only calls ll_driver->stop(), so hid_disconnect(), the sole caller of hid_bpf_disconnect_device(), never runs. The stale buffer and its allocated_data are reused by the next successful probe, since hid_bpf_allocate_event_data() returns early when device_data is set, and dispatch_hid_bpf_device_event() copies a full report into it unclamped. A HID-BPF program with ->hid_rdesc_fixup and ->hid_device_event can do this: attaching it reprobes with a small, listener-less descriptor (64-byte buffer leaked), detaching reprobes with the original one, and a 1024-byte UHID_INPUT2 overflows the buffer: BUG: KASAN: slab-out-of-bounds in dispatch_hid_bpf_device_event (drivers/hid/bpf/hid_bpf_dispatch.c:54) Write of size 1024 at addr ffff88800a401480 by task exploit/87 Call Trace: ... __asan_memcpy (mm/kasan/shadow.c:106 (discriminator 1)) dispatch_hid_bpf_device_event (drivers/hid/bpf/hid_bpf_dispatch.c:54) __hid_input_report (drivers/hid/hid-core.c:2150) hid_safe_input_report (drivers/hid/hid-core.c:2227) uhid_char_write (drivers/hid/uhid.c:609 drivers/hid/uhid.c:767) vfs_write (fs/read_write.c:665) ksys_write (fs/read_write.c:719) do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) ... which belongs to the cache kmalloc-64 of size 64 ... Oops: general protection fault, probably for non-canonical address 0x4847464544434261: ... RIP: 0010:__refill_objects_node (mm/slub.c:584 mm/slub.c:597 mm/slub.c:7420) Call Trace: ... __kmalloc_noprof (mm/slub.c:4887 mm/slub.c:5021 mm/slub.c:5530 mm/slub.c:5556) __hid_bpf_allocate_data (... drivers/hid/bpf/hid_bpf_dispatch.c:254) hid_connect (drivers/hid/hid-core.c:2309) hid_hw_start (drivers/hid/hid-core.c:2436 drivers/hid/hid-core.c:2427) hid_device_probe (drivers/hid/hid-core.c:2839 drivers/hid/hid-core.c:2878) ... device_reprobe (drivers/base/bus.c:873 drivers/base/bus.c:907) hid_bpf_reg (drivers/hid/bpf/hid_bpf_struct_ops.c:239) bpf_struct_ops_map_update_elem (kernel/bpf/bpf_struct_ops.c:925) ... __sys_bpf (kernel/bpf/syscall.c:6443) ... do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) Kernel panic - not syncing: Fatal exception Release the buffer on that exit. hid_bpf_disconnect_device() also clears device_data, so later paths through hid_disconnect() are unaffected. Cc: stable@vger.kernel.org Fixes: 658ee5a64fcf ("HID: bpf: allocate data memory for device_event BPF programs") Reported-by: Sashiko <sashiko-bot@kernel.org> Closes: https://lore.kernel.org/all/20260908202056.698371F00A3A@smtp.kernel.org/ Reported-by: co+86678873d3803945@bugs.sh Signed-off-by: Xiang Mei <xmei5@asu.edu> --- drivers/hid/hid-core.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 4a47deb9d833..3f93b0d48537 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -2339,6 +2339,7 @@ int hid_connect(struct hid_device *hdev, unsigned int connect_mask) * to any other listener. */ if (!hdev->claimed && !hdev->driver->raw_event) { hid_err(hdev, "device has no listeners, quitting\n"); + hid_bpf_disconnect_device(hdev); return -ENODEV; } -- 2.43.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] HID: core: release the HID-BPF event buffer on hid_connect()'s no-listeners exit 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 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-11-30 22:21 UTC (permalink / raw) To: Xiang Mei; +Cc: linux-input, bpf, dmitry.torokhov Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] HID: bpf: out-of-bounds read in dispatch_hid_bpf_device_event() [1] -- commit c402dbbc172b7dd281582dc06c3550cedb6797b3 Author: Xiang Mei <xmei5@asu.edu> HID: core: release the HID-BPF event buffer on hid_connect()'s no-listeners exit This commit calls hid_bpf_disconnect_device() on hid_connect()'s no-listeners error path so that a stale hdev->bpf.device_data buffer is not retained across a subsequent reprobe. > diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c > index 4a47deb9d833f..3f93b0d485376 100644 > --- a/drivers/hid/hid-core.c > +++ b/drivers/hid/hid-core.c > @@ -2339,6 +2339,7 @@ int hid_connect(struct hid_device *hdev, unsigned int connect_mask) > * to any other listener. */ > if (!hdev->claimed && !hdev->driver->raw_event) { > hid_err(hdev, "device has no listeners, quitting\n"); > + hid_bpf_disconnect_device(hdev); > return -ENODEV; > } [Severity: Critical] This problem wasn't introduced by this patch, but can dispatch_hid_bpf_device_event() still read past the end of data (or write past the end of ctx_kern.data) [1] when *size exceeds *buf_size or hdev->bpf.allocated_data? __hid_input_report() calls dispatch_hid_bpf_device_event() before hid_report_raw_event() checks the report length against bufsize: drivers/hid/bpf/hid_bpf_dispatch.c:dispatch_hid_bpf_device_event() { ... if (!hdev->bpf.device_data) return data; memset(ctx_kern.data, 0, hdev->bpf.allocated_data); memcpy(ctx_kern.data, data, *size); ... *buf_size = ctx_kern.ctx.allocated_size; return ctx_kern.data; } When hdev->bpf.device_data is allocated, memcpy() copies *size bytes from data into ctx_kern.data without checking that *size fits within either *buf_size or hdev->bpf.allocated_data, and then overwrites *buf_size with ctx_kern.ctx.allocated_size so the later bounds check in hid_report_raw_event() cannot reject a report where *size exceeded the caller's source buffer. If a caller of hid_safe_input_report() passes a report length larger than its transport buffer capacity, memcpy() reads out of bounds from data into the BPF context buffer and downstream hidraw listeners, or overflows ctx_kern.data if *size also exceeds hdev->bpf.allocated_data. [1] https://sashiko.dev/#/bug/linux-5fe97c31-7531-4aca-913a-1d4f5712eb64 -- Sashiko AI review · https://sashiko.dev/#/patchset/20261130220315.659533-1-xmei5@asu.edu?part=1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] HID: bpf: don't allocate the event buffer for an unbound device 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:03 ` Xiang Mei 2026-11-30 22:21 ` sashiko-bot 1 sibling, 1 reply; 5+ messages in thread From: Xiang Mei @ 2026-11-30 22:03 UTC (permalink / raw) To: Jiri Kosina, Benjamin Tissoires Cc: linux-input, bpf, linux-kernel, Sashiko, co+86678873d3803945, co+9a1ebd50dd939705, Xiang Mei, stable hid_bpf_reg() allocates hdev->bpf.device_data for a ->hid_device_event program even when the device is not bound. hid_close_report() has then cleared the report enums, so the buffer is sized for no reports: kzalloc(0) returns ZERO_SIZE_PTR and allocated_data is 0. The next successful hid_connect() keeps it, because hid_bpf_allocate_event_data() returns early when device_data is set, and the first input report is copied into it. Attaching a ->hid_device_event program while a ->hid_rdesc_fixup program keeps the device unbound, then detaching the latter, rebinds it with the original descriptor: BUG: KASAN: null-ptr-deref in dispatch_hid_bpf_device_event (drivers/hid/bpf/hid_bpf_dispatch.c:54) Write of size 1024 at addr 0000000000000010 by task exploit/87 Call Trace: ... __asan_memcpy (mm/kasan/shadow.c:106 (discriminator 1)) dispatch_hid_bpf_device_event (drivers/hid/bpf/hid_bpf_dispatch.c:54) __hid_input_report (drivers/hid/hid-core.c:2150) hid_safe_input_report (drivers/hid/hid-core.c:2227) uhid_char_write (drivers/hid/uhid.c:609 drivers/hid/uhid.c:767) vfs_write (fs/read_write.c:665) ksys_write (fs/read_write.c:719) do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) ... BUG: kernel NULL pointer dereference, address: 0000000000000010 Oops: Oops: 0002 [#1] SMP KASAN NOPTI RIP: 0010:memcpy (arch/x86/lib/memcpy_64.S:37) Call Trace: dispatch_hid_bpf_device_event (drivers/hid/bpf/hid_bpf_dispatch.c:54) __hid_input_report (drivers/hid/hid-core.c:2150) hid_safe_input_report (drivers/hid/hid-core.c:2227) uhid_char_write (drivers/hid/uhid.c:609 drivers/hid/uhid.c:767) vfs_write (fs/read_write.c:665) ksys_write (fs/read_write.c:719) do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) Kernel panic - not syncing: Fatal exception Only allocate when the report descriptor is parsed; otherwise hid_bpf_connect_device() allocates it when the device binds. Cc: stable@vger.kernel.org Fixes: ebc0d8093e8c ("HID: bpf: implement HID-BPF through bpf_struct_ops") Reported-by: co+9a1ebd50dd939705@bugs.sh Signed-off-by: Xiang Mei <xmei5@asu.edu> --- drivers/hid/bpf/hid_bpf_struct_ops.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/hid/bpf/hid_bpf_struct_ops.c b/drivers/hid/bpf/hid_bpf_struct_ops.c index c90b68956cb3..1039adeac8ae 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; -- 2.43.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] HID: bpf: don't allocate the event buffer for an unbound device 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 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-11-30 22:21 UTC (permalink / raw) To: Xiang Mei; +Cc: linux-input, bpf, dmitry.torokhov 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-30 22:21 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox