]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
selftests/hid: add an infinite loop test for hid_bpf_try_input_report
authorBenjamin Tissoires <bentiss@kernel.org>
Wed, 26 Jun 2024 13:46:34 +0000 (15:46 +0200)
committerBenjamin Tissoires <bentiss@kernel.org>
Thu, 27 Jun 2024 09:00:49 +0000 (11:00 +0200)
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/20240626-hid_hw_req_bpf-v2-13-cfd60fb6c79f@kernel.org
Acked-by: Jiri Kosina <jkosina@suse.com>
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
tools/testing/selftests/hid/hid_bpf.c
tools/testing/selftests/hid/progs/hid.c

index 36bbad8e0f9f2374dbad2965aa1f9ae603680144..dc0408a831d07c7296ba9c0ff1ede75ff07c3481 100644 (file)
@@ -1204,6 +1204,47 @@ TEST_F(hid_bpf, test_multiply_events)
        ASSERT_EQ(buf[1], 52);
 }
 
+/*
+ * Call hid_bpf_input_report against the given uhid device,
+ * check that the program is not making infinite loops.
+ */
+TEST_F(hid_bpf, test_hid_infinite_loop_input_report_call)
+{
+       const struct test_program progs[] = {
+               { .name = "hid_test_infinite_loop_input_report" },
+       };
+       __u8 buf[10] = {0};
+       int err;
+
+       LOAD_PROGRAMS(progs);
+
+       /* emit hid_hw_output_report from hidraw */
+       buf[0] = 1; /* report ID */
+       buf[1] = 2;
+       buf[2] = 42;
+
+       uhid_send_event(_metadata, self->uhid_fd, buf, 6);
+
+       /* read the data from hidraw */
+       memset(buf, 0, sizeof(buf));
+       err = read(self->hidraw_fd, buf, sizeof(buf));
+       ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
+       ASSERT_EQ(buf[0], 1);
+       ASSERT_EQ(buf[1], 3);
+
+       /* read the data from hidraw: hid_bpf_try_input_report should work exactly one time */
+       memset(buf, 0, sizeof(buf));
+       err = read(self->hidraw_fd, buf, sizeof(buf));
+       ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
+       ASSERT_EQ(buf[0], 1);
+       ASSERT_EQ(buf[1], 4);
+
+       /* read the data from hidraw: there should be none */
+       memset(buf, 0, sizeof(buf));
+       err = read(self->hidraw_fd, buf, sizeof(buf));
+       ASSERT_EQ(err, -1) TH_LOG("read_hidraw");
+}
+
 /*
  * Attach hid_insert{0,1,2} to the given uhid device,
  * retrieve and open the matching hidraw node,
index 46feeb91d1d5de35b84f01c91b4eb333ae5052d3..ee9bbbcf751b4871796518c6545faac6de603575 100644 (file)
@@ -561,3 +561,40 @@ SEC(".struct_ops.link")
 struct hid_bpf_ops test_multiply_events = {
        .hid_device_event = (void *)hid_test_multiply_events,
 };
+
+SEC("?struct_ops/hid_device_event")
+int BPF_PROG(hid_test_infinite_loop_input_report, struct hid_bpf_ctx *hctx,
+            enum hid_report_type report_type, __u64 source)
+{
+       __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 6 /* size */);
+       __u8 buf[6];
+
+       if (!data)
+               return 0; /* EPERM check */
+
+       /*
+        * we have to use an intermediate buffer as hid_bpf_input_report
+        * will memset data to \0
+        */
+       __builtin_memcpy(buf, data, sizeof(buf));
+
+       /* always forward the request as-is to the device, hid-bpf should prevent
+        * infinite loops.
+        * the return value is ignored so the event is passing to userspace.
+        */
+
+       hid_bpf_try_input_report(hctx, report_type, buf, sizeof(buf));
+
+       /* each time we process the event, we increment by one data[1]:
+        * after each successful call to hid_bpf_try_input_report, buf
+        * has been memcopied into data by the kernel.
+        */
+       data[1] += 1;
+
+       return 0;
+}
+
+SEC(".struct_ops.link")
+struct hid_bpf_ops test_infinite_loop_input_report = {
+       .hid_device_event = (void *)hid_test_infinite_loop_input_report,
+};