* [PATCH v2 0/4] HID: bpf: two fixes for the device event buffer
@ 2026-12-04 20:36 Yassine Ouamer via B4 Relay
2026-12-04 20:36 ` [PATCH v2 1/4] HID: bpf: don't allocate an empty " Yassine Ouamer via B4 Relay
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Yassine Ouamer via B4 Relay @ 2026-12-04 20:36 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires, Greg Kroah-Hartman, Shuah Khan
Cc: linux-input, bpf, linux-kselftest, Yassine Ouamer, stable
Two bugs in how dispatch_hid_bpf_device_event() uses device_data, both
from 658ee5a64fcf ("HID: bpf: allocate data memory for device_event BPF
programs"), each fixed with a selftest that fails without it:
- a device whose own report descriptor is refused gets a 0-byte buffer
(ZERO_SIZE_PTR) when a struct_ops with both a report descriptor fixup
and a hid_device_event hook is attached, kept after the fixup's
reprobe, so its first input report oopses (patches 1-2). Found with
the Apple Siri Remote (4th generation) over Bluetooth LE.
- a report longer than the device's report descriptor declares is
copied whole into a buffer sized from the descriptor: a slab
out-of-bounds write with the device's data (patches 3-4).
Tested in a virtme-ng VM (QEMU, x86_64) on hid/for-next 47d0d5ce3cfc,
with the HID selftests' config and KASAN: tools/testing/selftests/hid/
hid_bpf passes 52/52 (2 skips, as before) with /sys/fs/bpf mounted;
without the fixes, the new tests oops and trigger the KASAN report
quoted in patch 3. Not tested: the hid-tools test suite, and the
patched kernel with the real device (its HID-BPF program avoids the
first bug by attaching the report descriptor fixup before its other
hooks).
The analysis, the patches and their commit messages were written with
an AI coding assistant (Claude, by Anthropic).
Signed-off-by: Yassine Ouamer <yassine.ouamer@proton.me>
---
Changes in v2:
- Patch 3: also cut the size given back to the HID core, not only the
one given to the programs: a program setting ctx->size to 0 kept the
device's size, and the HID core read past device_data in
hid_dump_report() and the driver's raw_event(). Reported by Sashiko
in its review of v1.
- Patch 4: test a program setting ctx->size to 0 too, and fix the
commit message: without patch 3, hidraw gets nothing, not the whole
report.
- Link to v1: https://patch.msgid.link/20261202-hid-bpf-event-buffer-v1-0-53e3b6ce8336@proton.me
To: Jiri Kosina <jikos@kernel.org>
To: Benjamin Tissoires <bentiss@kernel.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Shuah Khan <shuah@kernel.org>
Cc: linux-input@vger.kernel.org
Cc: bpf@vger.kernel.org
Cc: linux-kselftest@vger.kernel.org
---
Yassine Ouamer (4):
HID: bpf: don't allocate an empty device event buffer
selftests/hid: test a device event on a refused report descriptor
HID: bpf: don't copy a report longer than the device event buffer
selftests/hid: test a report longer than its descriptor declares
drivers/hid/bpf/hid_bpf_dispatch.c | 20 ++++-
tools/testing/selftests/hid/hid_bpf.c | 143 ++++++++++++++++++++++++++++++++
tools/testing/selftests/hid/progs/hid.c | 75 +++++++++++++++++
3 files changed, 237 insertions(+), 1 deletion(-)
---
base-commit: 47d0d5ce3cfca63a04eabcbeef7086d06240ff97
change-id: 20261202-hid-bpf-event-buffer-40948476084c
Best regards,
--
Yassine Ouamer <yassine.ouamer@proton.me>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/4] HID: bpf: don't allocate an empty device event buffer
2026-12-04 20:36 [PATCH v2 0/4] HID: bpf: two fixes for the device event buffer Yassine Ouamer via B4 Relay
@ 2026-12-04 20:36 ` Yassine Ouamer via B4 Relay
2026-12-04 20:51 ` sashiko-bot
2026-12-04 20:36 ` [PATCH v2 2/4] selftests/hid: test a device event on a refused report descriptor Yassine Ouamer via B4 Relay
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Yassine Ouamer via B4 Relay @ 2026-12-04 20:36 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires, Greg Kroah-Hartman, Shuah Khan
Cc: linux-input, bpf, linux-kselftest, Yassine Ouamer, stable
From: Yassine Ouamer <yassine.ouamer@proton.me>
A device whose own report descriptor the HID core refuses (for instance
one with a report size over 256 bits) has no reports until a HID-BPF
report descriptor fixup is attached and the device is probed again. If
the struct_ops carrying the fixup also has a hid_device_event hook,
hid_bpf_allocate_event_data() runs when it is attached, before that
probe, finds no report, and kzalloc(0) returns ZERO_SIZE_PTR. When the
probe then connects the device, hid_bpf_connect_device() sees a
non-NULL device_data, takes it as already allocated and keeps it, so
the first input report is copied to ZERO_SIZE_PTR:
BUG: kernel NULL pointer dereference, address: 0000000000000010
#PF: supervisor write access in kernel mode
RIP: 0010:memcpy_orig+0xcd/0x140
Call Trace:
dispatch_hid_bpf_device_event
__hid_input_report
hid_safe_input_report
uhid_char_write
Seen with the Apple Siri Remote (4th generation, 004C:0316) over
Bluetooth LE, whose report descriptor declares a single 1664-bit
report, and a HID-BPF program replacing it: the machine had to be
rebooted, bluetoothd being stuck in D state.
Allocate nothing while the device has no reports: device_data stays
NULL, which dispatch_hid_bpf_device_event() already takes as no buffer
yet, and hid_bpf_connect_device() allocates it once the fixed report
descriptor has been parsed.
The bug was found while writing a HID-BPF program for that device; the
analysis, this change and its commit message were written with an AI
coding assistant (Claude, by Anthropic).
Fixes: 658ee5a64fcf ("HID: bpf: allocate data memory for device_event BPF programs")
Cc: stable@vger.kernel.org
Signed-off-by: Yassine Ouamer <yassine.ouamer@proton.me>
---
drivers/hid/bpf/hid_bpf_dispatch.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c
index 6b467c74601c..714014560fb8 100644
--- a/drivers/hid/bpf/hid_bpf_dispatch.c
+++ b/drivers/hid/bpf/hid_bpf_dispatch.c
@@ -243,6 +243,15 @@ static int __hid_bpf_allocate_data(struct hid_device *hdev, u8 **data, u32 *size
}
}
+ /*
+ * A device whose own report descriptor was refused has no reports
+ * until a report descriptor fixup makes them and the device is probed
+ * again: nothing to allocate before, hid_bpf_connect_device() does it
+ * then. kzalloc(0) would give ZERO_SIZE_PTR, which looks allocated.
+ */
+ if (!max_report_len)
+ return 0;
+
/*
* Give us a little bit of extra space and some predictability in the
* buffer length we create. This way, we can tell users that they can
--
2.53.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 2/4] selftests/hid: test a device event on a refused report descriptor
2026-12-04 20:36 [PATCH v2 0/4] HID: bpf: two fixes for the device event buffer Yassine Ouamer via B4 Relay
2026-12-04 20:36 ` [PATCH v2 1/4] HID: bpf: don't allocate an empty " Yassine Ouamer via B4 Relay
@ 2026-12-04 20:36 ` Yassine Ouamer via B4 Relay
2026-12-04 20:52 ` sashiko-bot
2026-12-04 20:36 ` [PATCH v2 3/4] HID: bpf: don't copy a report longer than the device event buffer Yassine Ouamer via B4 Relay
2026-12-04 20:36 ` [PATCH v2 4/4] selftests/hid: test a report longer than its descriptor declares Yassine Ouamer via B4 Relay
3 siblings, 1 reply; 7+ messages in thread
From: Yassine Ouamer via B4 Relay @ 2026-12-04 20:36 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires, Greg Kroah-Hartman, Shuah Khan
Cc: linux-input, bpf, linux-kselftest, Yassine Ouamer
From: Yassine Ouamer <yassine.ouamer@proton.me>
Create a uhid device whose report descriptor the HID core refuses,
attach a struct_ops with both a report descriptor fixup that makes it
valid and a hid_device_event hook, then inject one event: the program
must see it and be able to change it. Without "HID: bpf: don't
allocate an empty device event buffer", this oopses in
dispatch_hid_bpf_device_event().
The device only starts once the fixup is attached, so the uhid events
are listened to after the programs are loaded.
The bug was found while writing a HID-BPF program for that device; the
analysis, this change and its commit message were written with an AI
coding assistant (Claude, by Anthropic).
Signed-off-by: Yassine Ouamer <yassine.ouamer@proton.me>
---
tools/testing/selftests/hid/hid_bpf.c | 73 +++++++++++++++++++++++++++++++++
tools/testing/selftests/hid/progs/hid.c | 53 ++++++++++++++++++++++++
2 files changed, 126 insertions(+)
diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c
index 8c41ebfb85e4..568d84e0eeaa 100644
--- a/tools/testing/selftests/hid/hid_bpf.c
+++ b/tools/testing/selftests/hid/hid_bpf.c
@@ -1010,6 +1010,79 @@ TEST_F(hid_bpf, test_rdesc_fixup_change_uniq_name_phys)
}
+/*
+ * A report descriptor the kernel refuses: a report size over 256 bits
+ */
+static __u8 refused_rdesc[] = {
+ 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
+ 0x09, 0x01, /* Usage (Vendor Usage 1) */
+ 0xa1, 0x01, /* COLLECTION (Application) */
+ 0x85, 0x01, /* REPORT_ID (1) */
+ 0x09, 0x01, /* Usage (Vendor Usage 1) */
+ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
+ 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
+ 0x95, 0x01, /* REPORT_COUNT (1) */
+ 0x76, 0x00, 0x04, /* REPORT_SIZE (1024) */
+ 0x81, 0x02, /* INPUT (Data,Var,Abs) */
+ 0xc0, /* END_COLLECTION */
+};
+
+/*
+ * A device whose own report descriptor the kernel refuses has no reports
+ * until a HID-BPF program fixes it. Attach one that fixes it and also
+ * has a hid_device_event hook, then inject an event: the program must see
+ * it and be able to change it.
+ */
+TEST_F(hid_bpf, test_rdesc_fixup_refused_device_event)
+{
+ const struct test_program progs[] = {
+ { .name = "hid_rdesc_fixup_refused" },
+ };
+ __u8 buf[MAX_BUF_SIZE] = {0};
+ void *uhid_err;
+ int err;
+
+ if (variant->rdesc != rdesc)
+ SKIP(return, "not compatible report descriptor");
+
+ /*
+ * The fixture's device replaced with one the kernel refuses. It starts
+ * only once the program fixed its descriptor, so the uhid events are
+ * listened to after that.
+ */
+ uhid_destroy(_metadata, &self->hid);
+ pthread_join(self->hid.tid, &uhid_err);
+ close(self->hid.uhid_fd);
+ self->hid.pid = 0x0a37;
+ self->hid.uhid_fd = open("/dev/uhid", O_RDWR | O_CLOEXEC);
+ ASSERT_GE(self->hid.uhid_fd, 0) TH_LOG("open uhid-cdev failed");
+ err = uhid_create(_metadata, self->hid.uhid_fd, self->hid.dev_id, self->hid.bus,
+ self->hid.vid, self->hid.pid, refused_rdesc, sizeof(refused_rdesc));
+ ASSERT_OK(err) TH_LOG("create uhid device failed");
+ self->hid.hid_id = get_hid_id(&self->hid);
+ ASSERT_GT(self->hid.hid_id, 0) TH_LOG("Could not locate uhid device id");
+
+ LOAD_PROGRAMS(progs);
+
+ err = uhid_start_listener(_metadata, &self->hid.tid, self->hid.uhid_fd);
+ ASSERT_OK(err) TH_LOG("could not start udev listener");
+
+ /* inject one event */
+ buf[0] = 1;
+ buf[1] = 42;
+ uhid_send_event(_metadata, &self->hid, buf, 9);
+
+ /* check that hid_refused_event() was executed */
+ ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1");
+
+ /* read the data from hidraw */
+ memset(buf, 0, sizeof(buf));
+ err = read(self->hidraw_fd, buf, sizeof(buf));
+ ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
+ ASSERT_EQ(buf[0], 1);
+ ASSERT_EQ(buf[2], 47);
+}
+
static int libbpf_print_fn(enum libbpf_print_level level,
const char *format, va_list args)
{
diff --git a/tools/testing/selftests/hid/progs/hid.c b/tools/testing/selftests/hid/progs/hid.c
index 48aa8088cc53..88aa122f4d94 100644
--- a/tools/testing/selftests/hid/progs/hid.c
+++ b/tools/testing/selftests/hid/progs/hid.c
@@ -639,3 +639,56 @@ SEC(".struct_ops.link")
struct hid_bpf_ops test_infinite_loop_input_report = {
.hid_device_event = (void *)hid_test_infinite_loop_input_report,
};
+
+/*
+ * A report descriptor for a device whose own one the kernel refuses: one
+ * input report, ID 1, of 8 bytes
+ */
+static const __u8 fixed_refused_rdesc[] = {
+ 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
+ 0x09, 0x01, /* Usage (Vendor Usage 1) */
+ 0xa1, 0x01, /* COLLECTION (Application) */
+ 0x85, 0x01, /* REPORT_ID (1) */
+ 0x19, 0x01, /* USAGE_MINIMUM (1) */
+ 0x29, 0x08, /* USAGE_MAXIMUM (8) */
+ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
+ 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
+ 0x95, 0x08, /* REPORT_COUNT (8) */
+ 0x75, 0x08, /* REPORT_SIZE (8) */
+ 0x81, 0x02, /* INPUT (Data,Var,Abs) */
+ 0xc0, /* END_COLLECTION */
+};
+
+SEC("?struct_ops.s/hid_rdesc_fixup")
+int BPF_PROG(hid_rdesc_fixup_refused, struct hid_bpf_ctx *hid_ctx)
+{
+ __u8 *data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 4096 /* size */);
+
+ if (!data)
+ return 0; /* EPERM check */
+
+ __builtin_memcpy(data, fixed_refused_rdesc, sizeof(fixed_refused_rdesc));
+
+ return sizeof(fixed_refused_rdesc);
+}
+
+SEC("?struct_ops/hid_device_event")
+int BPF_PROG(hid_refused_event, struct hid_bpf_ctx *hid_ctx, enum hid_report_type type)
+{
+ __u8 *rw_data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 3 /* size */);
+
+ if (!rw_data)
+ return 0; /* EPERM check */
+
+ callback_check = rw_data[1];
+
+ rw_data[2] = rw_data[1] + 5;
+
+ return hid_ctx->size;
+}
+
+SEC(".struct_ops.link")
+struct hid_bpf_ops rdesc_fixup_refused = {
+ .hid_rdesc_fixup = (void *)hid_rdesc_fixup_refused,
+ .hid_device_event = (void *)hid_refused_event,
+};
--
2.53.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 3/4] HID: bpf: don't copy a report longer than the device event buffer
2026-12-04 20:36 [PATCH v2 0/4] HID: bpf: two fixes for the device event buffer Yassine Ouamer via B4 Relay
2026-12-04 20:36 ` [PATCH v2 1/4] HID: bpf: don't allocate an empty " Yassine Ouamer via B4 Relay
2026-12-04 20:36 ` [PATCH v2 2/4] selftests/hid: test a device event on a refused report descriptor Yassine Ouamer via B4 Relay
@ 2026-12-04 20:36 ` Yassine Ouamer via B4 Relay
2026-12-04 20:36 ` [PATCH v2 4/4] selftests/hid: test a report longer than its descriptor declares Yassine Ouamer via B4 Relay
3 siblings, 0 replies; 7+ messages in thread
From: Yassine Ouamer via B4 Relay @ 2026-12-04 20:36 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires, Greg Kroah-Hartman, Shuah Khan
Cc: linux-input, bpf, linux-kselftest, Yassine Ouamer, stable
From: Yassine Ouamer <yassine.ouamer@proton.me>
dispatch_hid_bpf_device_event() copies each input report whole into
device_data, which is sized from the longest report the device's report
descriptor declares, rounded up to 64 bytes. Nothing makes a device
keep to its descriptor: through uhid (as bluetoothd does for Bluetooth
LE HID) or HIDP, a report is as long as the device sent it, and the HID
core accepts a longer report, ignoring the extra bytes. So once a
HID-BPF program with a hid_device_event hook is attached, a device can
write past device_data, with data of its choosing:
BUG: KASAN: slab-out-of-bounds in dispatch_hid_bpf_device_event+0x1a9/0x3a0
Write of size 128 at addr ffff888002baa500 by task hid_bpf/248
Call Trace:
__asan_memcpy
dispatch_hid_bpf_device_event
__hid_input_report
hid_safe_input_report
uhid_char_write
Copy only what fits and give the programs that size; the HID core and
hidraw then get the report cut the same way. Cut the size given back to
the HID core too, not only the programs' one: a program may set
ctx->size to 0, which keeps the size the report came with, and the HID
core would then read device_data up to the device's size, in
hid_dump_report() and the driver's raw_event(). The buffer still holds
the longest report the descriptor declares, so only bytes the descriptor
doesn't define are lost. Dropping such a report instead, as is done
when a program returns a size larger than the buffer, would lose every
report of a device that pads them as soon as a program is attached.
The bug was found while writing a HID-BPF program for that device; the
analysis, this change and its commit message were written with an AI
coding assistant (Claude, by Anthropic).
Fixes: 658ee5a64fcf ("HID: bpf: allocate data memory for device_event BPF programs")
Cc: stable@vger.kernel.org
Signed-off-by: Yassine Ouamer <yassine.ouamer@proton.me>
---
drivers/hid/bpf/hid_bpf_dispatch.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c
index 714014560fb8..656fabeff874 100644
--- a/drivers/hid/bpf/hid_bpf_dispatch.c
+++ b/drivers/hid/bpf/hid_bpf_dispatch.c
@@ -50,8 +50,17 @@ dispatch_hid_bpf_device_event(struct hid_device *hdev, enum hid_report_type type
if (!hdev->bpf.device_data)
return data;
+ /*
+ * A device may send a report longer than its report descriptor
+ * declares, and so longer than the buffer: the programs get what
+ * fits, which holds the longest report the descriptor declares.
+ * Cut *size itself: a program leaving ctx->size at 0 keeps it.
+ */
+ *size = min(*size, hdev->bpf.allocated_data);
+ ctx_kern.ctx.size = *size;
+
memset(ctx_kern.data, 0, hdev->bpf.allocated_data);
- memcpy(ctx_kern.data, data, *size);
+ memcpy(ctx_kern.data, data, ctx_kern.ctx.size);
rcu_read_lock();
list_for_each_entry_rcu(e, &hdev->bpf.prog_list, list) {
--
2.53.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 4/4] selftests/hid: test a report longer than its descriptor declares
2026-12-04 20:36 [PATCH v2 0/4] HID: bpf: two fixes for the device event buffer Yassine Ouamer via B4 Relay
` (2 preceding siblings ...)
2026-12-04 20:36 ` [PATCH v2 3/4] HID: bpf: don't copy a report longer than the device event buffer Yassine Ouamer via B4 Relay
@ 2026-12-04 20:36 ` Yassine Ouamer via B4 Relay
3 siblings, 0 replies; 7+ messages in thread
From: Yassine Ouamer via B4 Relay @ 2026-12-04 20:36 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires, Greg Kroah-Hartman, Shuah Khan
Cc: linux-input, bpf, linux-kselftest, Yassine Ouamer
From: Yassine Ouamer <yassine.ouamer@proton.me>
Inject a 128-byte report into a device whose longest declared report
is far shorter, with a hid_device_event program attached: the program
must still run, and hidraw must get the report cut to the HID-BPF
buffer. Do the same with a program that sets ctx->size to 0, which
keeps the size the report came with: hidraw must still get the report
cut.
Without "HID: bpf: don't copy a report longer than the device event
buffer", KASAN reports a slab-out-of-bounds write in
dispatch_hid_bpf_device_event() and both tests fail. If only the size
given to the programs is cut, the second one fails: the HID core gets
the buffer with the device's size and drops the report, so hidraw gets
nothing.
The bug was found while writing a HID-BPF program for that device; the
analysis, this change and its commit message were written with an AI
coding assistant (Claude, by Anthropic).
Signed-off-by: Yassine Ouamer <yassine.ouamer@proton.me>
---
tools/testing/selftests/hid/hid_bpf.c | 70 +++++++++++++++++++++++++++++++++
tools/testing/selftests/hid/progs/hid.c | 22 +++++++++++
2 files changed, 92 insertions(+)
diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c
index 568d84e0eeaa..29be6019e5e1 100644
--- a/tools/testing/selftests/hid/hid_bpf.c
+++ b/tools/testing/selftests/hid/hid_bpf.c
@@ -1083,6 +1083,76 @@ TEST_F(hid_bpf, test_rdesc_fixup_refused_device_event)
ASSERT_EQ(buf[2], 47);
}
+/*
+ * A device may send a report longer than its report descriptor declares,
+ * and so longer than the buffer HID-BPF gives its programs: the programs
+ * get what fits, and so does hidraw.
+ */
+TEST_F(hid_bpf, test_device_event_oversized_report)
+{
+ const struct test_program progs[] = {
+ { .name = "hid_first_event" },
+ };
+ __u8 buf[MAX_BUF_SIZE] = {0};
+ int err;
+
+ LOAD_PROGRAMS(progs);
+
+ /* inject one event, longer than any report the descriptor declares */
+ buf[0] = 1;
+ buf[1] = 42;
+ memset(&buf[3], 0xaa, sizeof(buf) - 3);
+ uhid_send_event(_metadata, &self->hid, buf, sizeof(buf));
+
+ /* check that hid_first_event() was executed */
+ ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1");
+
+ /* read the data from hidraw: cut to the buffer */
+ memset(buf, 0, sizeof(buf));
+ err = read(self->hidraw_fd, buf, sizeof(buf));
+ ASSERT_GT(err, 0) TH_LOG("read_hidraw");
+ ASSERT_LT(err, sizeof(buf)) TH_LOG("read_hidraw");
+ ASSERT_EQ(buf[0], 1);
+ ASSERT_EQ(buf[2], 47);
+ ASSERT_EQ(buf[err - 1], 0xaa);
+}
+
+/*
+ * Same, with a program that sets ctx->size to 0, which keeps the size the
+ * report came with: that size must be the one cut to the buffer, not the
+ * device's. Otherwise the HID core gets the buffer with the device's size:
+ * hid_report_raw_event() drops the report, so hidraw gets nothing, but
+ * hid_dump_report() and a driver's raw_event() read past the buffer.
+ */
+TEST_F(hid_bpf, test_device_event_oversized_report_zero_size)
+{
+ const struct test_program progs[] = {
+ { .name = "hid_zero_size_event" },
+ };
+ __u8 buf[MAX_BUF_SIZE] = {0};
+ int err;
+
+ LOAD_PROGRAMS(progs);
+
+ /* inject one event, longer than any report the descriptor declares */
+ buf[0] = 1;
+ buf[1] = 42;
+ memset(&buf[3], 0xaa, sizeof(buf) - 3);
+ uhid_send_event(_metadata, &self->hid, buf, sizeof(buf));
+
+ /* check that hid_zero_size_event() was executed */
+ ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1");
+
+ /* read the data from hidraw: cut to the buffer */
+ memset(buf, 0, sizeof(buf));
+ err = read(self->hidraw_fd, buf, sizeof(buf));
+ ASSERT_GT(err, 0) TH_LOG("read_hidraw");
+ ASSERT_LT(err, sizeof(buf)) TH_LOG("read_hidraw");
+ ASSERT_EQ(buf[0], 1);
+ ASSERT_EQ(buf[2], 47);
+ ASSERT_EQ(buf[err - 1], 0xaa);
+}
+
static int libbpf_print_fn(enum libbpf_print_level level,
const char *format, va_list args)
{
diff --git a/tools/testing/selftests/hid/progs/hid.c b/tools/testing/selftests/hid/progs/hid.c
index 88aa122f4d94..10567ffc01ce 100644
--- a/tools/testing/selftests/hid/progs/hid.c
+++ b/tools/testing/selftests/hid/progs/hid.c
@@ -96,6 +96,28 @@ struct hid_bpf_ops change_report_id = {
.hid_device_event = (void *)hid_change_report_id,
};
+SEC("?struct_ops/hid_device_event")
+int BPF_PROG(hid_zero_size_event, struct hid_bpf_ctx *hid_ctx, enum hid_report_type type)
+{
+ __u8 *rw_data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 3 /* size */);
+
+ if (!rw_data)
+ return 0; /* EPERM check */
+
+ callback_check = rw_data[1];
+
+ rw_data[2] = rw_data[1] + 5;
+
+ /* 0 keeps the size the report came with */
+ hid_ctx->size = 0;
+ return 0;
+}
+
+SEC(".struct_ops.link")
+struct hid_bpf_ops zero_size_event = {
+ .hid_device_event = (void *)hid_zero_size_event,
+};
+
struct hid_hw_request_syscall_args {
/* data needs to come at offset 0 so we can use it in calls */
__u8 data[128];
--
2.53.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/4] HID: bpf: don't allocate an empty device event buffer
2026-12-04 20:36 ` [PATCH v2 1/4] HID: bpf: don't allocate an empty " Yassine Ouamer via B4 Relay
@ 2026-12-04 20:51 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-04 20:51 UTC (permalink / raw)
To: Yassine Ouamer; +Cc: linux-input, bpf, dmitry.torokhov
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] HID: bpf: race between hid_bpf_reg() and
hid_bpf_connect_device() skips hooks
--
commit 0479a84986d87f27a71dc589c8c74d25d8357acb
Author: Yassine Ouamer <yassine.ouamer@proton.me>
HID: bpf: don't allocate an empty device event buffer
This patch skips allocating the HID-BPF device event buffer in
__hid_bpf_allocate_data() when the device currently has no parsed
reports, leaving allocation to hid_bpf_connect_device() after a
subsequent probe.
> diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c
> index 6b467c74601cb..714014560fb8d 100644
> --- a/drivers/hid/bpf/hid_bpf_dispatch.c
> +++ b/drivers/hid/bpf/hid_bpf_dispatch.c
> @@ -243,6 +243,15 @@ static int __hid_bpf_allocate_data(struct hid_device *hdev, u8 **data, u32 *size
> }
> }
>
> + /*
> + * A device whose own report descriptor was refused has no reports
> + * until a report descriptor fixup makes them and the device is probed
> + * again: nothing to allocate before, hid_bpf_connect_device() does it
> + * then. kzalloc(0) would give ZERO_SIZE_PTR, which looks allocated.
> + */
> + if (!max_report_len)
> + return 0;
[Severity: Medium]
Can this deferred allocation race with hid_bpf_connect_device() and leave
hdev->bpf.device_data NULL even after a hid_device_event program is
attached?
hid_bpf_reg() calls hid_bpf_allocate_event_data() before adding ops to
hdev->bpf.prog_list, while holding only hdev->bpf.prog_list_lock.
hid_bpf_connect_device() scans hdev->bpf.prog_list under rcu_read_lock()
without taking hdev->bpf.prog_list_lock.
When a program attaches concurrently with device probing (for example
triggered by KOBJ_ADD from device_add() before bus_probe_device()
finishes), the two paths can interleave:
CPU0 CPU1
hid_device_probe() hid_bpf_reg()
mutex_lock(&hdev->bpf.prog_list_lock)
hid_bpf_allocate_event_data(hdev)
max_report_len == 0 -> returns 0
hid_open_report()
hid_hw_start() -> hid_connect()
hid_bpf_connect_device()
scans prog_list (empty) <-- misses ops, skips allocation
list_add_rcu(&ops->list, ...prog_list)
mutex_unlock(&hdev->bpf.prog_list_lock)
The device ends up connected with ops on prog_list while
hdev->bpf.device_data remains NULL. Every subsequent input report then
hits the early NULL check in dispatch_hid_bpf_device_event() and silently
skips all attached hid_device_event programs.
Taking hdev->bpf.prog_list_lock in hid_bpf_connect_device() would
serialize the list check against hid_bpf_reg().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-hid-bpf-event-buffer-v2-0-188e8e960231@proton.me?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 2/4] selftests/hid: test a device event on a refused report descriptor
2026-12-04 20:36 ` [PATCH v2 2/4] selftests/hid: test a device event on a refused report descriptor Yassine Ouamer via B4 Relay
@ 2026-12-04 20:52 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-04 20:52 UTC (permalink / raw)
To: Yassine Ouamer; +Cc: linux-input, bpf, dmitry.torokhov
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Low] selftests/hid: double pthread_join() in FIXTURE_TEARDOWN() on
test failure
- [Low] selftests/hid: race between uhid device_add() and
LOAD_PROGRAMS()
- [Low] selftests/hid: fixture misuse and early-fail double
pthread_join()
- [Low] selftests/hid: hid_refused_event() duplicates hid_first_event()
--
commit 24965259c6874c1e527e7846a0a8da3ce17f15d1
Author: Yassine Ouamer <yassine.ouamer@proton.me>
selftests/hid: test a device event on a refused report descriptor
This commit adds a HID-BPF selftest that creates a uhid device with a
report descriptor refused by the HID core, attaches a struct_ops program
with both a report descriptor fixup and a hid_device_event hook, and
verifies that an injected event is seen and modified by the program.
> diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c
> index 8c41ebfb85e42..568d84e0eeaa5 100644
> --- a/tools/testing/selftests/hid/hid_bpf.c
> +++ b/tools/testing/selftests/hid/hid_bpf.c
> @@ -1010,6 +1010,79 @@ TEST_F(hid_bpf, test_rdesc_fixup_change_uniq_name_phys)
[ ... ]
> +TEST_F(hid_bpf, test_rdesc_fixup_refused_device_event)
> +{
> + const struct test_program progs[] = {
> + { .name = "hid_rdesc_fixup_refused" },
> + };
> + __u8 buf[MAX_BUF_SIZE] = {0};
> + void *uhid_err;
> + int err;
> +
> + if (variant->rdesc != rdesc)
> + SKIP(return, "not compatible report descriptor");
> +
> + /*
> + * The fixture's device replaced with one the kernel refuses. It starts
> + * only once the program fixed its descriptor, so the uhid events are
> + * listened to after that.
> + */
> + uhid_destroy(_metadata, &self->hid);
> + pthread_join(self->hid.tid, &uhid_err);
> + close(self->hid.uhid_fd);
> + self->hid.pid = 0x0a37;
[Severity: Low]
Should this test use its own fixture instead of reusing hid_bpf?
Neither hid_bpf variant uses variant->rdesc here: unnumbered emits a
spurious skip, and numbered creates a throwaway uhid device only to tear
it down and open-code device creation. Hardcoding 0x0a37 also collides
with the product ID already used in tools/testing/selftests/hid/hidraw.c.
A dedicated fixture or helper for creating a uhid device without starting
the listener would avoid the throwaway setup, skip, and ID collision.
> + self->hid.uhid_fd = open("/dev/uhid", O_RDWR | O_CLOEXEC);
> + ASSERT_GE(self->hid.uhid_fd, 0) TH_LOG("open uhid-cdev failed");
[Severity: Low]
Can an assertion failure before uhid_start_listener() trigger a double
pthread_join() in FIXTURE_TEARDOWN(hid_bpf)?
pthread_join(self->hid.tid, &uhid_err) has already joined the fixture's
listener thread, but self->hid.tid still holds that thread ID until
uhid_start_listener() overwrites it. If any assertion fails in between
(such as open() or LOAD_PROGRAMS()), kselftest_harness __bail() invokes
FIXTURE_TEARDOWN(hid_bpf), which unconditionally calls uhid_destroy() and
pthread_join(self->hid.tid, &uhid_err) a second time, risking a crash or
hang in teardown (and writing to fd -1 if open() failed) that masks the
original failure.
> + err = uhid_create(_metadata, self->hid.uhid_fd, self->hid.dev_id, self->hid.bus,
> + self->hid.vid, self->hid.pid, refused_rdesc, sizeof(refused_rdesc));
> + ASSERT_OK(err) TH_LOG("create uhid device failed");
> + self->hid.hid_id = get_hid_id(&self->hid);
> + ASSERT_GT(self->hid.hid_id, 0) TH_LOG("Could not locate uhid device id");
> +
> + LOAD_PROGRAMS(progs);
> +
> + err = uhid_start_listener(_metadata, &self->hid.tid, self->hid.uhid_fd);
[Severity: Low]
Can LOAD_PROGRAMS(progs) race with asynchronous uhid device registration?
uhid_create() schedules uhid_device_add_worker() on system_wq and returns
immediately. Unlike setup_uhid(), which waits for UHID_START first, this
test proceeds straight from get_hid_id() into LOAD_PROGRAMS(progs) as soon
as the sysfs uevent file appears, before device_add() finishes:
test thread kworker (uhid_device_add_worker)
uhid_create() -> UHID_CREATE2
hid_add_device() -> device_add()
kobject_add()
device_create_file(...uevent)
get_hid_id() (reads uevent)
LOAD_PROGRAMS(progs)
bpf_map__attach_struct_ops()
hid_bpf_reg()
hid_get_device()
bus_find_device() <-- -EINVAL, not on bus yet
bus_add_device()
dev_set_ready_to_probe()
bus_probe_device()
If hid_bpf_reg() runs between bus_add_device() and
dev_set_ready_to_probe(), hid_bpf_reconnect() hits -EPROBE_DEFER and
open_hidraw() can time out before the hidraw node appears. If it sets
rdesc_ops before bus_probe_device(), the initial probe plus reprobe can
queue UHID_START, UHID_STOP, UHID_START, causing uhid_event() to set
uhid_stopped and exit the listener thread prematurely once
uhid_start_listener() starts.
> diff --git a/tools/testing/selftests/hid/progs/hid.c b/tools/testing/selftests/hid/progs/hid.c
> index 48aa8088cc53e..88aa122f4d94f 100644
> --- a/tools/testing/selftests/hid/progs/hid.c
> +++ b/tools/testing/selftests/hid/progs/hid.c
> @@ -639,3 +639,56 @@ SEC(".struct_ops.link")
[ ... ]
> +SEC("?struct_ops/hid_device_event")
> +int BPF_PROG(hid_refused_event, struct hid_bpf_ctx *hid_ctx, enum hid_report_type type)
> +{
> + __u8 *rw_data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 3 /* size */);
> +
> + if (!rw_data)
> + return 0; /* EPERM check */
> +
> + callback_check = rw_data[1];
> +
> + rw_data[2] = rw_data[1] + 5;
> +
> + return hid_ctx->size;
> +}
> +
> +SEC(".struct_ops.link")
> +struct hid_bpf_ops rdesc_fixup_refused = {
> + .hid_rdesc_fixup = (void *)hid_rdesc_fixup_refused,
> + .hid_device_event = (void *)hid_refused_event,
> +};
[Severity: Low]
Can rdesc_fixup_refused reuse hid_first_event instead of adding
hid_refused_event?
hid_refused_event() is identical to the existing hid_first_event() in this
file, and libbpf supports referencing the same struct_ops program from
multiple .struct_ops.link maps.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-hid-bpf-event-buffer-v2-0-188e8e960231@proton.me?part=2
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-04 20:52 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 20:36 [PATCH v2 0/4] HID: bpf: two fixes for the device event buffer Yassine Ouamer via B4 Relay
2026-12-04 20:36 ` [PATCH v2 1/4] HID: bpf: don't allocate an empty " Yassine Ouamer via B4 Relay
2026-12-04 20:51 ` sashiko-bot
2026-12-04 20:36 ` [PATCH v2 2/4] selftests/hid: test a device event on a refused report descriptor Yassine Ouamer via B4 Relay
2026-12-04 20:52 ` sashiko-bot
2026-12-04 20:36 ` [PATCH v2 3/4] HID: bpf: don't copy a report longer than the device event buffer Yassine Ouamer via B4 Relay
2026-12-04 20:36 ` [PATCH v2 4/4] selftests/hid: test a report longer than its descriptor declares Yassine Ouamer via B4 Relay
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox