[PATCH 1/2] HID: core: release the HID-BPF event buffer on hid_connect()'s no-listeners exit
From: Xiang Mei
Date: Mon Nov 30 2026 - 17:03:36 EST
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@xxxxxxxxxxxxxxx
Fixes: 658ee5a64fcf ("HID: bpf: allocate data memory for device_event BPF programs")
Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Closes: https://lore.kernel.org/all/20260908202056.698371F00A3A@xxxxxxxxxxxxxxx/
Reported-by: co+86678873d3803945@xxxxxxx
Signed-off-by: Xiang Mei <xmei5@xxxxxxx>
---
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