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