* [PATCH 0/4] HID: bpf: two fixes for the device event buffer
@ 2026-12-02 21:22 Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 1/4] HID: bpf: don't allocate an empty " Yassine Ouamer via B4 Relay
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Yassine Ouamer via B4 Relay @ 2026-12-02 21:22 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 50/50 (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>
---
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 | 18 +++++-
tools/testing/selftests/hid/hid_bpf.c | 107 ++++++++++++++++++++++++++++++++
tools/testing/selftests/hid/progs/hid.c | 53 ++++++++++++++++
3 files changed, 177 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] 6+ messages in thread
* [PATCH 1/4] HID: bpf: don't allocate an empty device event buffer
2026-12-02 21:22 [PATCH 0/4] HID: bpf: two fixes for the device event buffer Yassine Ouamer via B4 Relay
@ 2026-12-02 21:22 ` Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 2/4] selftests/hid: test a device event on a refused report descriptor Yassine Ouamer via B4 Relay
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Yassine Ouamer via B4 Relay @ 2026-12-02 21:22 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] 6+ messages in thread
* [PATCH 2/4] selftests/hid: test a device event on a refused report descriptor
2026-12-02 21:22 [PATCH 0/4] HID: bpf: two fixes for the device event buffer Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 1/4] HID: bpf: don't allocate an empty " Yassine Ouamer via B4 Relay
@ 2026-12-02 21:22 ` Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 3/4] HID: bpf: don't copy a report longer than the device event buffer Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 4/4] selftests/hid: test a report longer than its descriptor declares Yassine Ouamer via B4 Relay
3 siblings, 0 replies; 6+ messages in thread
From: Yassine Ouamer via B4 Relay @ 2026-12-02 21:22 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] 6+ messages in thread
* [PATCH 3/4] HID: bpf: don't copy a report longer than the device event buffer
2026-12-02 21:22 [PATCH 0/4] HID: bpf: two fixes for the device event buffer Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 1/4] HID: bpf: don't allocate an empty " Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 2/4] selftests/hid: test a device event on a refused report descriptor Yassine Ouamer via B4 Relay
@ 2026-12-02 21:22 ` Yassine Ouamer via B4 Relay
2026-12-02 21:40 ` sashiko-bot
2026-12-02 21:22 ` [PATCH 4/4] selftests/hid: test a report longer than its descriptor declares Yassine Ouamer via B4 Relay
3 siblings, 1 reply; 6+ messages in thread
From: Yassine Ouamer via B4 Relay @ 2026-12-02 21:22 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. 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 | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c
index 714014560fb8..df4c30dd6c05 100644
--- a/drivers/hid/bpf/hid_bpf_dispatch.c
+++ b/drivers/hid/bpf/hid_bpf_dispatch.c
@@ -50,8 +50,15 @@ 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.
+ */
+ ctx_kern.ctx.size = min(*size, hdev->bpf.allocated_data);
+
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] 6+ messages in thread
* [PATCH 4/4] selftests/hid: test a report longer than its descriptor declares
2026-12-02 21:22 [PATCH 0/4] HID: bpf: two fixes for the device event buffer Yassine Ouamer via B4 Relay
` (2 preceding siblings ...)
2026-12-02 21:22 ` [PATCH 3/4] HID: bpf: don't copy a report longer than the device event buffer Yassine Ouamer via B4 Relay
@ 2026-12-02 21:22 ` Yassine Ouamer via B4 Relay
3 siblings, 0 replies; 6+ messages in thread
From: Yassine Ouamer via B4 Relay @ 2026-12-02 21:22 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. 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 hidraw gets the whole report.
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 | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c
index 568d84e0eeaa..341f74a7d6f2 100644
--- a/tools/testing/selftests/hid/hid_bpf.c
+++ b/tools/testing/selftests/hid/hid_bpf.c
@@ -1083,6 +1083,40 @@ 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);
+}
+
static int libbpf_print_fn(enum libbpf_print_level level,
const char *format, va_list args)
{
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 3/4] HID: bpf: don't copy a report longer than the device event buffer
2026-12-02 21:22 ` [PATCH 3/4] HID: bpf: don't copy a report longer than the device event buffer Yassine Ouamer via B4 Relay
@ 2026-12-02 21:40 ` sashiko-bot
0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-02 21:40 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: unclamped *size in dispatch_hid_bpf_device_event()
when size is 0
--
commit 427f1ddc5c111a0ad5c8eadb8072a9ea963b193e
Author: Yassine Ouamer <yassine.ouamer@proton.me>
HID: bpf: don't copy a report longer than the device event buffer
This commit clamps the input report size copied into the HID-BPF device
event buffer to hdev->bpf.allocated_data to prevent an out-of-bounds write
when a device sends a report larger than declared in its descriptor.
> diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c
> index 714014560fb8d..df4c30dd6c05d 100644
> --- a/drivers/hid/bpf/hid_bpf_dispatch.c
> +++ b/drivers/hid/bpf/hid_bpf_dispatch.c
> @@ -50,8 +50,15 @@ 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.
> + */
> + ctx_kern.ctx.size = min(*size, hdev->bpf.allocated_data);
> +
> 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) {
[Severity: Medium]
Can *size remain larger than hdev->bpf.allocated_data on return if a BPF
program sets ctx->size (or ctx->retval) to 0 and returns 0?
hid_bpf_ops_btf_struct_access() allows hid_device_event programs to write
to ctx->retval / ctx->size, and the exit path only writes ctx_kern.ctx.size
back to *size when it is non-zero:
drivers/hid/bpf/hid_bpf_dispatch.c:dispatch_hid_bpf_device_event() {
...
ret = ctx_kern.ctx.size;
if (ret) {
if (ret > ctx_kern.ctx.allocated_size)
return ERR_PTR(-EINVAL);
*size = ret;
}
*buf_size = ctx_kern.ctx.allocated_size;
return ctx_kern.data;
}
When ctx_kern.ctx.size is 0, dispatch_hid_bpf_device_event() returns
hdev->bpf.device_data while leaving *size at the original oversized report
length, so __hid_input_report() can read past the end of
hdev->bpf.device_data in hid_dump_report() and hdrv->raw_event(). Clamping
*size directly before initializing ctx_kern.ctx.size would avoid this.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202-hid-bpf-event-buffer-v1-0-53e3b6ce8336@proton.me?part=3
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-02 21:40 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 21:22 [PATCH 0/4] HID: bpf: two fixes for the device event buffer Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 1/4] HID: bpf: don't allocate an empty " Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 2/4] selftests/hid: test a device event on a refused report descriptor Yassine Ouamer via B4 Relay
2026-12-02 21:22 ` [PATCH 3/4] HID: bpf: don't copy a report longer than the device event buffer Yassine Ouamer via B4 Relay
2026-12-02 21:40 ` sashiko-bot
2026-12-02 21:22 ` [PATCH 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