Skip to content

Commit 015a4a2

Browse files
author
Benjamin Tissoires
committed
selftests/hid: add tests for hid_hw_raw_request HID-BPF hooks
We add 3 new tests: - first, we make sure we can prevent the raw_request to happen - second, we make sure that we can detect that a given hidraw client was actually doing the request, and for that client only, call ourself hid_bpf_hw_request(), returning a custom value - last, we ensure that we can not loop between hooks for hid_hw_raw_request() and manual calls to hid_bpf_hw_request() from that hook Link: https://patch.msgid.link/[email protected] Acked-by: Jiri Kosina <[email protected]> Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent 7583910 commit 015a4a2

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed

tools/testing/selftests/hid/hid_bpf.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ static void detach_bpf(FIXTURE_DATA(hid_bpf) * self)
470470
close(self->hidraw_fd);
471471
self->hidraw_fd = 0;
472472

473+
if (!self->skel)
474+
return;
475+
476+
hid__detach(self->skel);
477+
473478
for (i = 0; i < ARRAY_SIZE(self->hid_links); i++) {
474479
if (self->hid_links[i])
475480
bpf_link__destroy(self->hid_links[i]);
@@ -575,6 +580,8 @@ static void load_programs(const struct test_program programs[],
575580
programs[i].name + 4);
576581
}
577582

583+
hid__attach(self->skel);
584+
578585
self->hidraw_fd = open_hidraw(self->dev_id);
579586
ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
580587
}
@@ -919,6 +926,108 @@ TEST_F(hid_bpf, test_hid_user_raw_request_call)
919926
ASSERT_EQ(args.data[1], 2);
920927
}
921928

929+
/*
930+
* Call hid_hw_raw_request against the given uhid device,
931+
* check that the program is called and prevents the
932+
* call to uhid.
933+
*/
934+
TEST_F(hid_bpf, test_hid_filter_raw_request_call)
935+
{
936+
const struct test_program progs[] = {
937+
{ .name = "hid_test_filter_raw_request" },
938+
};
939+
__u8 buf[10] = {0};
940+
int err;
941+
942+
LOAD_PROGRAMS(progs);
943+
944+
/* first check that we did not attach to device_event */
945+
946+
/* inject one event */
947+
buf[0] = 1;
948+
buf[1] = 42;
949+
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
950+
951+
/* read the data from hidraw */
952+
memset(buf, 0, sizeof(buf));
953+
err = read(self->hidraw_fd, buf, sizeof(buf));
954+
ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
955+
ASSERT_EQ(buf[0], 1);
956+
ASSERT_EQ(buf[1], 42);
957+
ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test");
958+
959+
/* now check that our program is preventing hid_hw_raw_request() */
960+
961+
/* emit hid_hw_raw_request from hidraw */
962+
/* Get Feature */
963+
memset(buf, 0, sizeof(buf));
964+
buf[0] = 0x1; /* Report Number */
965+
err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
966+
ASSERT_LT(err, 0) TH_LOG("unexpected success while reading HIDIOCGFEATURE: %d", err);
967+
ASSERT_EQ(errno, 20) TH_LOG("unexpected error code while reading HIDIOCGFEATURE: %d",
968+
errno);
969+
970+
/* remove our bpf program and check that we can now emit commands */
971+
972+
/* detach the program */
973+
detach_bpf(self);
974+
975+
self->hidraw_fd = open_hidraw(self->dev_id);
976+
ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
977+
978+
err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
979+
ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGFEATURE: %d", err);
980+
}
981+
982+
/*
983+
* Call hid_hw_raw_request against the given uhid device,
984+
* check that the program is called and can issue the call
985+
* to uhid and transform the answer.
986+
*/
987+
TEST_F(hid_bpf, test_hid_change_raw_request_call)
988+
{
989+
const struct test_program progs[] = {
990+
{ .name = "hid_test_hidraw_raw_request" },
991+
};
992+
__u8 buf[10] = {0};
993+
int err;
994+
995+
LOAD_PROGRAMS(progs);
996+
997+
/* emit hid_hw_raw_request from hidraw */
998+
/* Get Feature */
999+
memset(buf, 0, sizeof(buf));
1000+
buf[0] = 0x1; /* Report Number */
1001+
err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
1002+
ASSERT_EQ(err, 3) TH_LOG("unexpected returned size while reading HIDIOCGFEATURE: %d", err);
1003+
1004+
ASSERT_EQ(buf[0], 2);
1005+
ASSERT_EQ(buf[1], 3);
1006+
ASSERT_EQ(buf[2], 4);
1007+
}
1008+
1009+
/*
1010+
* Call hid_hw_raw_request against the given uhid device,
1011+
* check that the program is not making infinite loops.
1012+
*/
1013+
TEST_F(hid_bpf, test_hid_infinite_loop_raw_request_call)
1014+
{
1015+
const struct test_program progs[] = {
1016+
{ .name = "hid_test_infinite_loop_raw_request" },
1017+
};
1018+
__u8 buf[10] = {0};
1019+
int err;
1020+
1021+
LOAD_PROGRAMS(progs);
1022+
1023+
/* emit hid_hw_raw_request from hidraw */
1024+
/* Get Feature */
1025+
memset(buf, 0, sizeof(buf));
1026+
buf[0] = 0x1; /* Report Number */
1027+
err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
1028+
ASSERT_EQ(err, 3) TH_LOG("unexpected returned size while reading HIDIOCGFEATURE: %d", err);
1029+
}
1030+
9221031
/*
9231032
* Attach hid_insert{0,1,2} to the given uhid device,
9241033
* retrieve and open the matching hidraw node,

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,82 @@ SEC(".struct_ops.link")
306306
struct hid_bpf_ops test_insert3 = {
307307
.hid_device_event = (void *)hid_test_insert3,
308308
};
309+
310+
SEC("?struct_ops/hid_hw_request")
311+
int BPF_PROG(hid_test_filter_raw_request, struct hid_bpf_ctx *hctx, unsigned char reportnum,
312+
enum hid_report_type rtype, enum hid_class_request reqtype, __u64 source)
313+
{
314+
return -20;
315+
}
316+
317+
SEC(".struct_ops.link")
318+
struct hid_bpf_ops test_filter_raw_request = {
319+
.hid_hw_request = (void *)hid_test_filter_raw_request,
320+
};
321+
322+
static struct file *current_file;
323+
324+
SEC("fentry/hidraw_open")
325+
int BPF_PROG(hidraw_open, struct inode *inode, struct file *file)
326+
{
327+
current_file = file;
328+
return 0;
329+
}
330+
331+
SEC("?struct_ops.s/hid_hw_request")
332+
int BPF_PROG(hid_test_hidraw_raw_request, struct hid_bpf_ctx *hctx, unsigned char reportnum,
333+
enum hid_report_type rtype, enum hid_class_request reqtype, __u64 source)
334+
{
335+
__u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 3 /* size */);
336+
int ret;
337+
338+
if (!data)
339+
return 0; /* EPERM check */
340+
341+
/* check if the incoming request comes from our hidraw operation */
342+
if (source == (__u64)current_file) {
343+
data[0] = reportnum;
344+
345+
ret = hid_bpf_hw_request(hctx, data, 2, rtype, reqtype);
346+
if (ret != 2)
347+
return -1;
348+
data[0] = reportnum + 1;
349+
data[1] = reportnum + 2;
350+
data[2] = reportnum + 3;
351+
return 3;
352+
}
353+
354+
return 0;
355+
}
356+
357+
SEC(".struct_ops.link")
358+
struct hid_bpf_ops test_hidraw_raw_request = {
359+
.hid_hw_request = (void *)hid_test_hidraw_raw_request,
360+
};
361+
362+
SEC("?struct_ops.s/hid_hw_request")
363+
int BPF_PROG(hid_test_infinite_loop_raw_request, struct hid_bpf_ctx *hctx, unsigned char reportnum,
364+
enum hid_report_type rtype, enum hid_class_request reqtype, __u64 source)
365+
{
366+
__u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 3 /* size */);
367+
int ret;
368+
369+
if (!data)
370+
return 0; /* EPERM check */
371+
372+
/* always forward the request as-is to the device, hid-bpf should prevent
373+
* infinite loops.
374+
*/
375+
data[0] = reportnum;
376+
377+
ret = hid_bpf_hw_request(hctx, data, 2, rtype, reqtype);
378+
if (ret == 2)
379+
return 3;
380+
381+
return 0;
382+
}
383+
384+
SEC(".struct_ops.link")
385+
struct hid_bpf_ops test_infinite_loop_raw_request = {
386+
.hid_hw_request = (void *)hid_test_infinite_loop_raw_request,
387+
};

0 commit comments

Comments
 (0)