selftests/hid: add subprog call test
authorBenjamin Tissoires <bentiss@kernel.org>
Sat, 8 Jun 2024 09:01:21 +0000 (11:01 +0200)
committerBenjamin Tissoires <bentiss@kernel.org>
Fri, 14 Jun 2024 09:20:20 +0000 (11:20 +0200)
I got a weird verifier error with a subprog once, so let's have a test
for it.

Link: https://lore.kernel.org/r/20240608-hid_bpf_struct_ops-v3-9-6ac6ade58329@kernel.org
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
tools/testing/selftests/hid/hid_bpf.c
tools/testing/selftests/hid/progs/hid.c

index 967dfe6b58cbb122143f98410be310403f0297b0..45e173db35bdd71b846a07739ba66e2e089eff11 100644 (file)
@@ -638,6 +638,47 @@ TEST_F(hid_bpf, raw_event)
        ASSERT_EQ(buf[2], 52);
 }
 
+/*
+ * Attach hid_first_event to the given uhid device,
+ * retrieve and open the matching hidraw node,
+ * inject one event in the uhid device,
+ * check that the program sees it and can change the data
+ */
+TEST_F(hid_bpf, subprog_raw_event)
+{
+       const struct test_program progs[] = {
+               { .name = "hid_subprog_first_event" },
+       };
+       __u8 buf[10] = {0};
+       int err;
+
+       LOAD_PROGRAMS(progs);
+
+       /* inject one event */
+       buf[0] = 1;
+       buf[1] = 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[2], 47);
+
+       /* inject another event */
+       memset(buf, 0, sizeof(buf));
+       buf[0] = 1;
+       buf[1] = 47;
+       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[2], 52);
+}
+
 /*
  * Ensures that we can attach/detach programs
  */
index 614f1aa3264910fd0f88e6075f802f45d4aca03b..2e7e5a736dc6de278059df5a84597e1a18222d07 100644 (file)
@@ -35,6 +35,30 @@ struct hid_bpf_ops first_event = {
        .hid_id = 2,
 };
 
+int __hid_subprog_first_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 */
+
+       rw_data[2] = rw_data[1] + 5;
+
+       return hid_ctx->size;
+}
+
+SEC("?struct_ops/hid_device_event")
+int BPF_PROG(hid_subprog_first_event, struct hid_bpf_ctx *hid_ctx, enum hid_report_type type)
+{
+       return __hid_subprog_first_event(hid_ctx, type);
+}
+
+SEC(".struct_ops.link")
+struct hid_bpf_ops subprog_first_event = {
+       .hid_device_event = (void *)hid_subprog_first_event,
+       .hid_id = 2,
+};
+
 SEC("?struct_ops/hid_device_event")
 int BPF_PROG(hid_second_event, struct hid_bpf_ctx *hid_ctx, enum hid_report_type type)
 {