Skip to content

Commit d3e1518

Browse files
author
Benjamin Tissoires
committed
selftests/hid: add an infinite loop test for hid_bpf_try_input_report
We don't want this call to allow an infinite loop in HID-BPF, so let's have some tests. Link: https://patch.msgid.link/[email protected] Acked-by: Jiri Kosina <[email protected]> Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent 62f2e1a commit d3e1518

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

tools/testing/selftests/hid/hid_bpf.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,47 @@ TEST_F(hid_bpf, test_multiply_events)
12041204
ASSERT_EQ(buf[1], 52);
12051205
}
12061206

1207+
/*
1208+
* Call hid_bpf_input_report against the given uhid device,
1209+
* check that the program is not making infinite loops.
1210+
*/
1211+
TEST_F(hid_bpf, test_hid_infinite_loop_input_report_call)
1212+
{
1213+
const struct test_program progs[] = {
1214+
{ .name = "hid_test_infinite_loop_input_report" },
1215+
};
1216+
__u8 buf[10] = {0};
1217+
int err;
1218+
1219+
LOAD_PROGRAMS(progs);
1220+
1221+
/* emit hid_hw_output_report from hidraw */
1222+
buf[0] = 1; /* report ID */
1223+
buf[1] = 2;
1224+
buf[2] = 42;
1225+
1226+
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
1227+
1228+
/* read the data from hidraw */
1229+
memset(buf, 0, sizeof(buf));
1230+
err = read(self->hidraw_fd, buf, sizeof(buf));
1231+
ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
1232+
ASSERT_EQ(buf[0], 1);
1233+
ASSERT_EQ(buf[1], 3);
1234+
1235+
/* read the data from hidraw: hid_bpf_try_input_report should work exactly one time */
1236+
memset(buf, 0, sizeof(buf));
1237+
err = read(self->hidraw_fd, buf, sizeof(buf));
1238+
ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
1239+
ASSERT_EQ(buf[0], 1);
1240+
ASSERT_EQ(buf[1], 4);
1241+
1242+
/* read the data from hidraw: there should be none */
1243+
memset(buf, 0, sizeof(buf));
1244+
err = read(self->hidraw_fd, buf, sizeof(buf));
1245+
ASSERT_EQ(err, -1) TH_LOG("read_hidraw");
1246+
}
1247+
12071248
/*
12081249
* Attach hid_insert{0,1,2} to the given uhid device,
12091250
* retrieve and open the matching hidraw node,

tools/testing/selftests/hid/progs/hid.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,40 @@ SEC(".struct_ops.link")
561561
struct hid_bpf_ops test_multiply_events = {
562562
.hid_device_event = (void *)hid_test_multiply_events,
563563
};
564+
565+
SEC("?struct_ops/hid_device_event")
566+
int BPF_PROG(hid_test_infinite_loop_input_report, struct hid_bpf_ctx *hctx,
567+
enum hid_report_type report_type, __u64 source)
568+
{
569+
__u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 6 /* size */);
570+
__u8 buf[6];
571+
572+
if (!data)
573+
return 0; /* EPERM check */
574+
575+
/*
576+
* we have to use an intermediate buffer as hid_bpf_input_report
577+
* will memset data to \0
578+
*/
579+
__builtin_memcpy(buf, data, sizeof(buf));
580+
581+
/* always forward the request as-is to the device, hid-bpf should prevent
582+
* infinite loops.
583+
* the return value is ignored so the event is passing to userspace.
584+
*/
585+
586+
hid_bpf_try_input_report(hctx, report_type, buf, sizeof(buf));
587+
588+
/* each time we process the event, we increment by one data[1]:
589+
* after each successful call to hid_bpf_try_input_report, buf
590+
* has been memcopied into data by the kernel.
591+
*/
592+
data[1] += 1;
593+
594+
return 0;
595+
}
596+
597+
SEC(".struct_ops.link")
598+
struct hid_bpf_ops test_infinite_loop_input_report = {
599+
.hid_device_event = (void *)hid_test_infinite_loop_input_report,
600+
};

0 commit comments

Comments
 (0)