* [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; 3+ 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] 3+ 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:03 ` [PATCH 2/2] HID: bpf: don't allocate the event buffer for an unbound device Xiang Mei
1 sibling, 0 replies; 3+ 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 [flat|nested] 3+ 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
1 sibling, 0 replies; 3+ 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 [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 22:03 UTC | newest]
Thread overview: 3+ 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:03 ` [PATCH 2/2] HID: bpf: don't allocate the event buffer for an unbound device Xiang Mei
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®