All of lore.kernel.org
 help / color / mirror / Atom feed
From: Benjamin Tissoires <benjamin.tissoires@redhat.com>
To: Greg KH <gregkh@linuxfoundation.org>,
	Jiri Kosina <jikos@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>, Shuah Khan <shuah@kernel.org>,
	Dave Marchevsky <davemarchevsky@fb.com>,
	Joe Stringer <joe@cilium.io>, Jonathan Corbet <corbet@lwn.net>
Cc: Tero Kristo <tero.kristo@linux.intel.com>,
	linux-kernel@vger.kernel.org, linux-input@vger.kernel.org,
	netdev@vger.kernel.org, bpf@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org,
	Benjamin Tissoires <benjamin.tissoires@redhat.com>
Subject: [PATCH bpf-next v7 20/24] selftests/bpf: add report descriptor fixup tests
Date: Thu, 21 Jul 2022 17:36:21 +0200	[thread overview]
Message-ID: <20220721153625.1282007-21-benjamin.tissoires@redhat.com> (raw)
In-Reply-To: <20220721153625.1282007-1-benjamin.tissoires@redhat.com>

Simple report descriptor override in HID: replace part of the report
descriptor from a static definition in the bpf kernel program.

Note that this test should be run last because we disconnect/reconnect
the device, meaning that it changes the overall uhid device.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>

---

no changes in v7

no changes in v6

changes in v5:
- amended for the new API

not in v4

changes in v3:
- added a comment to mention that this test needs to be run last

changes in v2:
- split the series by bpf/libbpf/hid/selftests and samples
---
 tools/testing/selftests/bpf/prog_tests/hid.c | 76 ++++++++++++++++++++
 tools/testing/selftests/bpf/progs/hid.c      | 53 ++++++++++++++
 2 files changed, 129 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/hid.c b/tools/testing/selftests/bpf/prog_tests/hid.c
index 19172d3e0f44..9dc5f0038472 100644
--- a/tools/testing/selftests/bpf/prog_tests/hid.c
+++ b/tools/testing/selftests/bpf/prog_tests/hid.c
@@ -9,6 +9,7 @@
 #include <dirent.h>
 #include <poll.h>
 #include <stdbool.h>
+#include <linux/hidraw.h>
 #include <linux/uhid.h>
 
 static unsigned char rdesc[] = {
@@ -769,6 +770,73 @@ static int test_hid_user_raw_request_call(int uhid_fd, int dev_id)
 	return ret;
 }
 
+/*
+ * Attach hid_rdesc_fixup to the given uhid device,
+ * retrieve and open the matching hidraw node,
+ * check that the hidraw report descriptor has been updated.
+ */
+static int test_rdesc_fixup(void)
+{
+	struct hidraw_report_descriptor rpt_desc = {0};
+	struct test_params params;
+	int err, uhid_fd, desc_size, hidraw_fd = -1, ret = -1;
+	int dev_id;
+	void *uhid_err;
+	pthread_t tid;
+	bool started = false;
+
+	dev_id = rand() % 1024;
+
+	uhid_fd = setup_uhid(dev_id);
+	if (!ASSERT_GE(uhid_fd, 0, "setup uhid"))
+		return uhid_fd;
+
+	err = prep_test(dev_id, "hid_rdesc_fixup", &params);
+	if (!ASSERT_EQ(err, 0, "prep_test(hid_rdesc_fixup)"))
+		goto cleanup;
+
+	err = uhid_start_listener(&tid, uhid_fd);
+	ASSERT_OK(err, "uhid_start_listener");
+
+	started = true;
+
+	hidraw_fd = params.hidraw_fd;
+
+	/* check that hid_rdesc_fixup() was executed */
+	ASSERT_EQ(params.skel->data->callback2_check, 0x21, "callback_check2");
+
+	/* read the exposed report descriptor from hidraw */
+	err = ioctl(hidraw_fd, HIDIOCGRDESCSIZE, &desc_size);
+	if (!ASSERT_GE(err, 0, "HIDIOCGRDESCSIZE"))
+		goto cleanup;
+
+	/* ensure the new size of the rdesc is bigger than the old one */
+	if (!ASSERT_GT(desc_size, sizeof(rdesc), "new_rdesc_size"))
+		goto cleanup;
+
+	rpt_desc.size = desc_size;
+	err = ioctl(hidraw_fd, HIDIOCGRDESC, &rpt_desc);
+	if (!ASSERT_GE(err, 0, "HIDIOCGRDESC"))
+		goto cleanup;
+
+	if (!ASSERT_EQ(rpt_desc.value[4], 0x42, "hid_rdesc_fixup"))
+		goto cleanup;
+
+	ret = 0;
+
+cleanup:
+	cleanup_test(&params);
+
+	if (started) {
+		destroy(uhid_fd);
+		pthread_join(tid, &uhid_err);
+		err = (int)(long)uhid_err;
+		CHECK_FAIL(err);
+	}
+
+	return ret;
+}
+
 void serial_test_hid_bpf(void)
 {
 	int err, uhid_fd;
@@ -799,6 +867,14 @@ void serial_test_hid_bpf(void)
 	err = test_hid_user_raw_request_call(uhid_fd, dev_id);
 	ASSERT_OK(err, "hid_user_raw_request");
 
+	/*
+	 * this test should be run last because we disconnect/reconnect
+	 * the device, meaning that it changes the overall uhid device
+	 * and messes up with the thread that reads uhid events.
+	 */
+	err = test_rdesc_fixup();
+	ASSERT_OK(err, "hid_rdesc_fixup");
+
 	destroy(uhid_fd);
 
 	pthread_join(tid, &uhid_err);
diff --git a/tools/testing/selftests/bpf/progs/hid.c b/tools/testing/selftests/bpf/progs/hid.c
index 0be319c11575..863c37ddf5ff 100644
--- a/tools/testing/selftests/bpf/progs/hid.c
+++ b/tools/testing/selftests/bpf/progs/hid.c
@@ -99,3 +99,56 @@ int hid_user_raw_request(struct hid_hw_request_syscall_args *args)
 
 	return 0;
 }
+
+static const __u8 rdesc[] = {
+	0x05, 0x01,				/* USAGE_PAGE (Generic Desktop) */
+	0x09, 0x32,				/* USAGE (Z) */
+	0x95, 0x01,				/* REPORT_COUNT (1) */
+	0x81, 0x06,				/* INPUT (Data,Var,Rel) */
+
+	0x06, 0x00, 0xff,			/* Usage Page (Vendor Defined Page 1) */
+	0x19, 0x01,				/* USAGE_MINIMUM (1) */
+	0x29, 0x03,				/* USAGE_MAXIMUM (3) */
+	0x15, 0x00,				/* LOGICAL_MINIMUM (0) */
+	0x25, 0x01,				/* LOGICAL_MAXIMUM (1) */
+	0x95, 0x03,				/* REPORT_COUNT (3) */
+	0x75, 0x01,				/* REPORT_SIZE (1) */
+	0x91, 0x02,				/* Output (Data,Var,Abs) */
+	0x95, 0x01,				/* REPORT_COUNT (1) */
+	0x75, 0x05,				/* REPORT_SIZE (5) */
+	0x91, 0x01,				/* Output (Cnst,Var,Abs) */
+
+	0x06, 0x00, 0xff,			/* Usage Page (Vendor Defined Page 1) */
+	0x19, 0x06,				/* USAGE_MINIMUM (6) */
+	0x29, 0x08,				/* USAGE_MAXIMUM (8) */
+	0x15, 0x00,				/* LOGICAL_MINIMUM (0) */
+	0x25, 0x01,				/* LOGICAL_MAXIMUM (1) */
+	0x95, 0x03,				/* REPORT_COUNT (3) */
+	0x75, 0x01,				/* REPORT_SIZE (1) */
+	0xb1, 0x02,				/* Feature (Data,Var,Abs) */
+	0x95, 0x01,				/* REPORT_COUNT (1) */
+	0x75, 0x05,				/* REPORT_SIZE (5) */
+	0x91, 0x01,				/* Output (Cnst,Var,Abs) */
+
+	0xc0,				/* END_COLLECTION */
+	0xc0,			/* END_COLLECTION */
+};
+
+SEC("?fmod_ret/hid_bpf_rdesc_fixup")
+int BPF_PROG(hid_rdesc_fixup, struct hid_bpf_ctx *hid_ctx)
+{
+	__u8 *data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 4096 /* size */);
+
+	if (!data)
+		return 0; /* EPERM check */
+
+	callback2_check = data[4];
+
+	/* insert rdesc at offset 73 */
+	__builtin_memcpy(&data[73], rdesc, sizeof(rdesc));
+
+	/* Change Usage Vendor globally */
+	data[4] = 0x42;
+
+	return sizeof(rdesc) + 73;
+}
-- 
2.36.1


  parent reply	other threads:[~2022-07-21 15:40 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-21 15:36 [PATCH bpf-next v7 00/24] Introduce eBPF support for HID devices Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 01/24] selftests/bpf: fix config for CLS_BPF Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 02/24] bpf/verifier: allow kfunc to read user provided context Benjamin Tissoires
2022-07-21 20:15   ` Kumar Kartikeya Dwivedi
2022-07-22  8:43     ` Benjamin Tissoires
2022-07-22  8:45   ` [PATCH bpf-next v8 " Benjamin Tissoires
2022-07-22 16:16     ` Alexei Starovoitov
2022-07-25 16:36       ` Benjamin Tissoires
2022-08-24  9:57         ` Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 03/24] bpf/verifier: do not clear meta in check_mem_size Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 04/24] selftests/bpf: add test for accessing ctx from syscall program type Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 05/24] bpf/verifier: allow kfunc to return an allocated mem Benjamin Tissoires
2022-07-21 21:05   ` Kumar Kartikeya Dwivedi
2022-07-21 15:36 ` [PATCH bpf-next v7 06/24] selftests/bpf: Add tests for kfunc returning a memory pointer Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 07/24] bpf: prepare for more bpf syscall to be used from kernel and user space Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 08/24] libbpf: add map_get_fd_by_id and map_delete_elem in light skeleton Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 09/24] HID: core: store the unique system identifier in hid_device Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 10/24] HID: export hid_report_type to uapi Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 11/24] HID: convert defines of HID class requests into a proper enum Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 12/24] HID: Kconfig: split HID support and hid-core compilation Benjamin Tissoires
2022-07-28 14:14   ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 13/24] HID: initial BPF implementation Benjamin Tissoires
2022-07-28 14:15   ` Greg KH
2022-08-24  9:41     ` Benjamin Tissoires
2022-07-28 14:19   ` Greg KH
2022-08-24  9:48     ` Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 14/24] selftests/bpf: add tests for the HID-bpf initial implementation Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 15/24] HID: bpf: allocate data memory for device_event BPF programs Benjamin Tissoires
2022-07-28 15:10   ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 16/24] selftests/bpf/hid: add test to change the report size Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 17/24] HID: bpf: introduce hid_hw_request() Benjamin Tissoires
2022-07-28 15:10   ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 18/24] selftests/bpf: add tests for bpf_hid_hw_request Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 19/24] HID: bpf: allow to change the report descriptor Benjamin Tissoires
2022-07-28 15:10   ` Greg KH
2022-07-21 15:36 ` Benjamin Tissoires [this message]
2022-07-21 15:36 ` [PATCH bpf-next v7 21/24] selftests/bpf: Add a test for BPF_F_INSERT_HEAD Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 22/24] samples/bpf: add new hid_mouse example Benjamin Tissoires
2022-07-28 15:13   ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 23/24] HID: bpf: add Surface Dial example Benjamin Tissoires
2022-07-28 15:12   ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 24/24] Documentation: add HID-BPF docs Benjamin Tissoires
2022-08-25  2:52   ` Bagas Sanjaya
2022-08-30 13:30     ` Benjamin Tissoires
2022-08-03  7:41 ` [PATCH bpf-next v7 00/24] Introduce eBPF support for HID devices Tero Kristo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220721153625.1282007-21-benjamin.tissoires@redhat.com \
    --to=benjamin.tissoires@redhat.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=corbet@lwn.net \
    --cc=daniel@iogearbox.net \
    --cc=davemarchevsky@fb.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jikos@kernel.org \
    --cc=joe@cilium.io \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=memxor@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=songliubraving@fb.com \
    --cc=tero.kristo@linux.intel.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.