From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Benjamin Tissoires <bentiss@kernel.org>
Cc: Jiri Kosina <jikos@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Shuah Khan <shuah@kernel.org>, Jonathan Corbet <corbet@lwn.net>,
"open list:HID CORE LAYER" <linux-input@vger.kernel.org>,
LKML <linux-kernel@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
"open list:KERNEL SELFTEST FRAMEWORK"
<linux-kselftest@vger.kernel.org>,
"open list:DOCUMENTATION" <linux-doc@vger.kernel.org>
Subject: Re: [PATCH HID v2 04/13] HID: bpf: add HID-BPF hooks for hid_hw_raw_requests
Date: Wed, 26 Jun 2024 09:29:57 -0700 [thread overview]
Message-ID: <CAADnVQLZRE_QxuEaqbTpKNQt+0VUB=DK37FqScaxtqwbn9UorA@mail.gmail.com> (raw)
In-Reply-To: <20240626-hid_hw_req_bpf-v2-4-cfd60fb6c79f@kernel.org>
On Wed, Jun 26, 2024 at 6:46 AM Benjamin Tissoires <bentiss@kernel.org> wrote:
>
> This allows to intercept and prevent or change the behavior of
> hid_hw_raw_request() from a bpf program.
>
> The intent is to solve a couple of use case:
> - firewalling a HID device: a firewall can monitor who opens the hidraw
> nodes and then prevent or allow access to write operations on that
> hidraw node.
> - change the behavior of a device and emulate a new HID feature request
>
> The hook is allowed to be run as sleepable so it can itself call
> hid_bpf_hw_request(), which allows to "convert" one feature request into
> another or even call the feature request on a different HID device on the
> same physical device.
>
> Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
>
> ---
>
> changes in v2:
> - make use of SRCU
> ---
> drivers/hid/bpf/hid_bpf_dispatch.c | 37 ++++++++++++++++++++++++++++++++++++
> drivers/hid/bpf/hid_bpf_struct_ops.c | 1 +
> drivers/hid/hid-core.c | 6 ++++++
> include/linux/hid_bpf.h | 35 ++++++++++++++++++++++++++++++++++
> 4 files changed, 79 insertions(+)
>
> diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c
> index c026248e3d73..ac98bab4c96d 100644
> --- a/drivers/hid/bpf/hid_bpf_dispatch.c
> +++ b/drivers/hid/bpf/hid_bpf_dispatch.c
> @@ -74,6 +74,43 @@ dispatch_hid_bpf_device_event(struct hid_device *hdev, enum hid_report_type type
> }
> EXPORT_SYMBOL_GPL(dispatch_hid_bpf_device_event);
>
> +int dispatch_hid_bpf_raw_requests(struct hid_device *hdev,
> + unsigned char reportnum, u8 *buf,
> + u32 size, enum hid_report_type rtype,
> + enum hid_class_request reqtype,
> + u64 source)
> +{
> + struct hid_bpf_ctx_kern ctx_kern = {
> + .ctx = {
> + .hid = hdev,
> + .allocated_size = size,
> + .size = size,
> + },
> + .data = buf,
> + };
> + struct hid_bpf_ops *e;
> + int ret, idx;
> +
> + if (rtype >= HID_REPORT_TYPES)
> + return -EINVAL;
> +
> + idx = srcu_read_lock(&hdev->bpf.srcu);
> + list_for_each_entry_srcu(e, &hdev->bpf.prog_list, list,
> + srcu_read_lock_held(&hdev->bpf.srcu)) {
> + if (e->hid_hw_request) {
> + ret = e->hid_hw_request(&ctx_kern.ctx, reportnum, rtype, reqtype, source);
> + if (ret)
> + goto out;
> + }
> + }
here and in patch 7 I would reduce indent by doing:
if (!e->hid_hw_request)
continue;
ret = e->hid_hw_request(...);
otherwise lgtm
next prev parent reply other threads:[~2024-06-26 16:30 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-26 13:46 [PATCH HID v2 00/13] HID: bpf_struct_ops, part 2 Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 01/13] HID: bpf: fix dispatch_hid_bpf_device_event uninitialized ret value Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 02/13] HID: add source argument to HID low level functions Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 03/13] HID: bpf: protect HID-BPF prog_list access by a SRCU Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 04/13] HID: bpf: add HID-BPF hooks for hid_hw_raw_requests Benjamin Tissoires
2024-06-26 16:29 ` Alexei Starovoitov [this message]
2024-06-27 9:45 ` Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 05/13] HID: bpf: prevent infinite recursions with hid_hw_raw_requests hooks Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 06/13] selftests/hid: add tests for hid_hw_raw_request HID-BPF hooks Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 07/13] HID: bpf: add HID-BPF hooks for hid_hw_output_report Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 08/13] selftests/hid: add tests for hid_hw_output_report HID-BPF hooks Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 09/13] HID: bpf: make hid_bpf_input_report() sleep until the device is ready Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 10/13] selftests/hid: add wq test for hid_bpf_input_report() Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 11/13] HID: bpf: allow hid_device_event hooks to inject input reports on self Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 12/13] selftests/hid: add another test for injecting an event from an event hook Benjamin Tissoires
2024-06-26 13:46 ` [PATCH HID v2 13/13] selftests/hid: add an infinite loop test for hid_bpf_try_input_report Benjamin Tissoires
2024-06-27 9:44 ` [PATCH HID v2 00/13] HID: bpf_struct_ops, part 2 Benjamin Tissoires
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='CAADnVQLZRE_QxuEaqbTpKNQt+0VUB=DK37FqScaxtqwbn9UorA@mail.gmail.com' \
--to=alexei.starovoitov@gmail.com \
--cc=ast@kernel.org \
--cc=bentiss@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=corbet@lwn.net \
--cc=jikos@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=shuah@kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).