From: Eduard Zingerman <eddyz87@gmail.com>
To: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org
Cc: daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com,
yonghong.song@linux.dev, eddyz87@gmail.com,
Alexei Starovoitov <alexei.starovoitov@gmail.com>
Subject: [PATCH bpf-next v1 7/8] bpf: support for void/primitive __arg_untrusted global func params
Date: Wed, 2 Jul 2025 15:42:08 -0700 [thread overview]
Message-ID: <20250702224209.3300396-8-eddyz87@gmail.com> (raw)
In-Reply-To: <20250702224209.3300396-1-eddyz87@gmail.com>
Allow specifying __arg_untrusted for void */char */int */long *
parameters. Treat such parameters as
PTR_TO_MEM|MEM_RDONLY|PTR_UNTRUSTED of size zero.
Intended usage is as follows:
int memcmp(char *a __arg_untrusted, char *b __arg_untrusted, size_t n) {
bpf_for(i, 0, n) {
if (a[i] - b[i]) // load at any offset is allowed
return a[i] - b[i];
}
return 0;
}
Allocate register id for ARG_PTR_TO_MEM parameters only when
PTR_MAYBE_NULL is set. Register id for PTR_TO_MEM is used only to
propagate non-null status after conditionals.
Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
include/linux/btf.h | 1 +
kernel/bpf/btf.c | 13 +++++++++++++
kernel/bpf/verifier.c | 7 ++++---
3 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/include/linux/btf.h b/include/linux/btf.h
index a40beb9cf160..9eda6b113f9b 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -223,6 +223,7 @@ u32 btf_nr_types(const struct btf *btf);
struct btf *btf_base_btf(const struct btf *btf);
bool btf_type_is_i32(const struct btf_type *t);
bool btf_type_is_i64(const struct btf_type *t);
+bool btf_type_is_primitive(const struct btf_type *t);
bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
const struct btf_member *m,
u32 expected_offset, u32 expected_size);
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 28cb0a2a5402..ffe560c0ec65 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -891,6 +891,12 @@ bool btf_type_is_i64(const struct btf_type *t)
return btf_type_is_int(t) && __btf_type_int_is_regular(t, 8);
}
+bool btf_type_is_primitive(const struct btf_type *t)
+{
+ return (btf_type_is_int(t) && btf_type_int_is_regular(t)) ||
+ btf_is_any_enum(t);
+}
+
/*
* Check that given struct member is a regular int with expected
* offset and size.
@@ -7829,6 +7835,13 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
return -EINVAL;
}
+ ref_t = btf_type_skip_modifiers(btf, t->type, NULL);
+ if (btf_type_is_void(ref_t) || btf_type_is_primitive(ref_t)) {
+ sub->args[i].arg_type = ARG_PTR_TO_MEM | MEM_RDONLY | PTR_UNTRUSTED;
+ sub->args[i].mem_size = 0;
+ continue;
+ }
+
kern_type_id = btf_get_ptr_to_btf_id(log, i, btf, t);
if (kern_type_id < 0)
return kern_type_id;
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index dfb5a2f8e58f..53f70ef9adc0 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -23158,11 +23158,12 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog)
__mark_dynptr_reg(reg, BPF_DYNPTR_TYPE_LOCAL, true, ++env->id_gen);
} else if (base_type(arg->arg_type) == ARG_PTR_TO_MEM) {
reg->type = PTR_TO_MEM;
- if (arg->arg_type & PTR_MAYBE_NULL)
- reg->type |= PTR_MAYBE_NULL;
+ reg->type |= arg->arg_type &
+ (PTR_MAYBE_NULL | PTR_UNTRUSTED | MEM_RDONLY);
mark_reg_known_zero(env, regs, i);
reg->mem_size = arg->mem_size;
- reg->id = ++env->id_gen;
+ if (arg->arg_type & PTR_MAYBE_NULL)
+ reg->id = ++env->id_gen;
} else if (base_type(arg->arg_type) == ARG_PTR_TO_BTF_ID) {
reg->type = PTR_TO_BTF_ID;
if (arg->arg_type & PTR_MAYBE_NULL)
--
2.47.1
next prev parent reply other threads:[~2025-07-02 22:42 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-02 22:42 [PATCH bpf-next v1 0/8] bpf: additional use-cases for untrusted PTR_TO_MEM Eduard Zingerman
2025-07-02 22:42 ` [PATCH bpf-next v1 1/8] bpf: make makr_btf_ld_reg return error for unexpected reg types Eduard Zingerman
2025-07-04 17:20 ` Kumar Kartikeya Dwivedi
2025-07-02 22:42 ` [PATCH bpf-next v1 2/8] bpf: rdonly_untrusted_mem for btf id walk pointer leafs Eduard Zingerman
2025-07-04 17:27 ` Kumar Kartikeya Dwivedi
2025-07-02 22:42 ` [PATCH bpf-next v1 3/8] selftests/bpf: ptr_to_btf_id struct walk ending with primitive pointer Eduard Zingerman
2025-07-04 17:34 ` Kumar Kartikeya Dwivedi
2025-07-02 22:42 ` [PATCH bpf-next v1 4/8] bpf: attribute __arg_untrusted for global function parameters Eduard Zingerman
2025-07-03 3:18 ` Alexei Starovoitov
2025-07-03 21:25 ` Eduard Zingerman
2025-07-04 18:17 ` Alexei Starovoitov
2025-07-04 18:03 ` Kumar Kartikeya Dwivedi
2025-07-04 18:28 ` Eduard Zingerman
2025-07-04 18:33 ` Eduard Zingerman
2025-07-04 18:50 ` Kumar Kartikeya Dwivedi
2025-07-04 19:07 ` Eduard Zingerman
2025-07-04 19:15 ` Kumar Kartikeya Dwivedi
2025-07-04 19:23 ` Eduard Zingerman
2025-07-04 20:05 ` Alexei Starovoitov
2025-07-04 20:20 ` Kumar Kartikeya Dwivedi
2025-07-04 20:34 ` Eduard Zingerman
2025-07-04 20:47 ` Alexei Starovoitov
2025-07-02 22:42 ` [PATCH bpf-next v1 5/8] libbpf: __arg_untrusted in bpf_helpers.h Eduard Zingerman
2025-07-04 18:04 ` Kumar Kartikeya Dwivedi
2025-07-02 22:42 ` [PATCH bpf-next v1 6/8] selftests/bpf: test cases for __arg_untrusted Eduard Zingerman
2025-07-04 18:05 ` Kumar Kartikeya Dwivedi
2025-07-02 22:42 ` Eduard Zingerman [this message]
2025-07-03 3:20 ` [PATCH bpf-next v1 7/8] bpf: support for void/primitive __arg_untrusted global func params Alexei Starovoitov
2025-07-03 21:49 ` Eduard Zingerman
2025-07-04 18:11 ` Alexei Starovoitov
2025-07-04 18:09 ` Kumar Kartikeya Dwivedi
2025-07-02 22:42 ` [PATCH bpf-next v1 8/8] selftests/bpf: tests for __arg_untrusted void * " Eduard Zingerman
2025-07-04 18:12 ` Kumar Kartikeya Dwivedi
2025-07-04 18:35 ` Eduard Zingerman
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=20250702224209.3300396-8-eddyz87@gmail.com \
--to=eddyz87@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=martin.lau@linux.dev \
--cc=yonghong.song@linux.dev \
/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