From: Benjamin Tissoires <bentiss@kernel.org>
To: Shuah Khan <shuah@kernel.org>, Jiri Kosina <jikos@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
Alexei Starovoitov <ast@kernel.org>
Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
bpf@vger.kernel.org, linux-input@vger.kernel.org,
linux-doc@vger.kernel.org,
Benjamin Tissoires <bentiss@kernel.org>
Subject: [PATCH HID v2 15/16] HID: bpf: rework hid_bpf_ops_btf_struct_access
Date: Fri, 07 Jun 2024 17:28:35 +0200 [thread overview]
Message-ID: <20240607-hid_bpf_struct_ops-v2-15-3f95f4d02292@kernel.org> (raw)
In-Reply-To: <20240607-hid_bpf_struct_ops-v2-0-3f95f4d02292@kernel.org>
The idea is to provide a list of stucts and their editable fields.
Currently no functional changes are introduced here, we will add some
more writeable fields in the next patch.
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
---
new in v2
---
drivers/hid/bpf/hid_bpf_struct_ops.c | 91 +++++++++++++++++++++++++++---------
1 file changed, 69 insertions(+), 22 deletions(-)
diff --git a/drivers/hid/bpf/hid_bpf_struct_ops.c b/drivers/hid/bpf/hid_bpf_struct_ops.c
index 056d05d96962..944e6d91a36b 100644
--- a/drivers/hid/bpf/hid_bpf_struct_ops.c
+++ b/drivers/hid/bpf/hid_bpf_struct_ops.c
@@ -16,6 +16,7 @@
#include <linux/hid_bpf.h>
#include <linux/init.h>
#include <linux/module.h>
+#include <linux/stddef.h>
#include <linux/workqueue.h>
#include "hid_bpf_dispatch.h"
@@ -52,40 +53,86 @@ static int hid_bpf_ops_check_member(const struct btf_type *t,
return 0;
}
+struct hid_bpf_offset_write_range {
+ const char *struct_name;
+ u32 struct_length;
+ u32 start;
+ u32 end;
+};
+
static int hid_bpf_ops_btf_struct_access(struct bpf_verifier_log *log,
const struct bpf_reg_state *reg,
int off, int size)
{
- const struct btf_type *state;
- const struct btf_type *t;
- s32 type_id;
+#define WRITE_RANGE(_name, _field, _start_offset, _end_offset) \
+ { \
+ .struct_name = #_name, \
+ .struct_length = sizeof(struct _name), \
+ .start = offsetof(struct _name, _field) + _start_offset, \
+ .end = offsetofend(struct _name, _field) + _end_offset, \
+ }
- type_id = btf_find_by_name_kind(reg->btf, "hid_bpf_ctx",
- BTF_KIND_STRUCT);
- if (type_id < 0)
- return -EINVAL;
+ const struct hid_bpf_offset_write_range write_ranges[] = {
+ WRITE_RANGE(hid_bpf_ctx, retval, 0, 0),
+ };
+#undef WRITE_RANGE
+ const struct btf_type *state = NULL;
+ const struct btf_type *t;
+ const char *cur = NULL;
+ int i;
t = btf_type_by_id(reg->btf, reg->btf_id);
- state = btf_type_by_id(reg->btf, type_id);
- if (t != state) {
- bpf_log(log, "only access to hid_bpf_ctx is supported\n");
- return -EACCES;
- }
- /* out-of-bound access in hid_bpf_ctx */
- if (off + size > sizeof(struct hid_bpf_ctx)) {
- bpf_log(log, "write access at off %d with size %d\n", off, size);
- return -EACCES;
+ for (i = 0; i < ARRAY_SIZE(write_ranges); i++) {
+ const struct hid_bpf_offset_write_range *write_range = &write_ranges[i];
+ s32 type_id;
+
+ /* we already found a writeable struct, but there is a
+ * new one, let's break the loop.
+ */
+ if (t == state && write_range->struct_name != cur)
+ break;
+
+ /* new struct to look for */
+ if (write_range->struct_name != cur) {
+ type_id = btf_find_by_name_kind(reg->btf, write_range->struct_name,
+ BTF_KIND_STRUCT);
+ if (type_id < 0)
+ return -EINVAL;
+
+ state = btf_type_by_id(reg->btf, type_id);
+ }
+
+ /* this is not the struct we are looking for */
+ if (t != state) {
+ cur = write_range->struct_name;
+ continue;
+ }
+
+ /* first time we see this struct, check for out of bounds */
+ if (cur != write_range->struct_name &&
+ off + size > write_range->struct_length) {
+ bpf_log(log, "write access for struct %s at off %d with size %d\n",
+ write_range->struct_name, off, size);
+ return -EACCES;
+ }
+
+ /* now check if we are in our boundaries */
+ if (off >= write_range->start && off + size <= write_range->end)
+ return NOT_INIT;
+
+ cur = write_range->struct_name;
}
- if (off < offsetof(struct hid_bpf_ctx, retval)) {
+
+ if (t != state)
+ bpf_log(log, "write access to this struct is not supported\n");
+ else
bpf_log(log,
- "write access at off %d with size %d on read-only part of hid_bpf_ctx\n",
- off, size);
- return -EACCES;
- }
+ "write access at off %d with size %d on read-only part of %s\n",
+ off, size, cur);
- return NOT_INIT;
+ return -EACCES;
}
static const struct bpf_verifier_ops hid_bpf_verifier_ops = {
--
2.44.0
next prev parent reply other threads:[~2024-06-07 15:29 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-07 15:28 [PATCH HID v2 00/16] HID: convert HID-BPF into using bpf_struct_ops Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 01/16] HID: rename struct hid_bpf_ops into hid_ops Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 02/16] HID: bpf: add hid_get/put_device() helpers Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 03/16] HID: bpf: implement HID-BPF through bpf_struct_ops Benjamin Tissoires
2024-06-07 16:51 ` Alexei Starovoitov
2024-06-08 8:00 ` Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 04/16] selftests/hid: convert the hid_bpf selftests with struct_ops Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 05/16] HID: samples: convert the 2 HID-BPF samples into struct_ops Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 06/16] HID: bpf: add defines for HID-BPF SEC in in-tree bpf fixes Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 07/16] HID: bpf: convert in-tree fixes into struct_ops Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 08/16] HID: bpf: remove tracing HID-BPF capability Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 09/16] selftests/hid: add subprog call test Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 10/16] Documentation: HID: amend HID-BPF for struct_ops Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 11/16] Documentation: HID: add a small blurb on udev-hid-bpf Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 12/16] HID: bpf: Artist24: remove unused variable Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 13/16] HID: bpf: error on warnings when compiling bpf objects Benjamin Tissoires
2024-06-07 15:28 ` [PATCH HID v2 14/16] bpf: allow bpf helpers to be used into HID-BPF struct_ops Benjamin Tissoires
2024-06-07 15:28 ` Benjamin Tissoires [this message]
2024-06-07 16:57 ` [PATCH HID v2 15/16] HID: bpf: rework hid_bpf_ops_btf_struct_access Alexei Starovoitov
2024-06-07 15:28 ` [PATCH HID v2 16/16] HID: bpf: make part of struct hid_device writable 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=20240607-hid_bpf_struct_ops-v2-15-3f95f4d02292@kernel.org \
--to=bentiss@kernel.org \
--cc=ast@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