Commit 05b3b8f1 authored by Benjamin Tissoires's avatar Benjamin Tissoires
Browse files

selftests/hid: add subprog call test

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: default avatarBenjamin Tissoires <bentiss@kernel.org>
parent 4a86220e
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -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
 */
+24 −0
Original line number Diff line number Diff line
@@ -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)
{