From: Yassine Ouamer 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 --- 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