Trusted PTR_TO_BTF_ID arg support in global subprogs
Subsystems (5)
Andrii Nakryiko
maintainerv3 cover Trusted PTR_TO_BTF_ID arg support in global subprogs
This patch set follows recent changes that added btf_decl_tag-based argument annotation support for global subprogs. This time we add ability to pass PTR_TO_BTF_ID (BTF-aware kernel pointers) arguments into global subprograms. We support explicitly trusted arguments only, for now.
Patch #1 adds logic for arg:trusted tag support on the verifier side. Default semantic of such arguments is non-NULL, enforced on caller side. But patch #2 adds arg:nullable tag that can be combined with arg:trusted to make callee explicitly do the NULL check, which helps implement "optional" PTR_TO_BTF_ID arguments.
Patch #3 adds libbpf-side __arg_trusted and __arg_nullable macros.
Patch #4 adds a bunch of tests validating __arg_trusted in combination with __arg_nullable.
v2->v3:
- went back to arg:nullable and __arg_nullable naming;
- rebased on latest bpf-next after prepartory patches landed;v1->v2:
- added fix up to type enforcement changes, landed earlier;
- dropped bpf_core_cast() changes, will post them separately, as they now
are not used in added tests;
- dropped arg:untrusted support (Alexei);
- renamed arg:nullable to arg:maybe_null (Alexei);
- and also added task_struct___local flavor tests (Alexei).
Andrii Nakryiko (4):
bpf: add __arg_trusted global func arg tag
bpf: add arg:nullable tag to be combined with trusted pointers
libbpf: add __arg_trusted and __arg_nullable tag macros selftests/bpf: add trusted global subprog arg tests
include/linux/bpf_verifier.h | 1 + kernel/bpf/btf.c | 109 ++++++++++-- kernel/bpf/verifier.c | 24 +++ tools/lib/bpf/bpf_helpers.h | 2 + .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_global_ptr_args.c | 156 ++++++++++++++++++ 6 files changed, 281 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/verifier_global_ptr_args.c
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 1/4 bpf: add __arg_trusted global func arg tag
Add support for passing PTR_TO_BTF_ID registers to global subprogs. Currently only PTR_TRUSTED flavor of PTR_TO_BTF_ID is supported. Non-NULL semantics is assumed, so caller will be forced to prove PTR_TO_BTF_ID can't be NULL.
Note, we disallow global subprogs to destroy passed in PTR_TO_BTF_ID arguments, even the trusted one. We achieve that by not setting ref_obj_id when validating subprog code. This basically enforces (in Rust terms) borrowing semantics vs move semantics. Borrowing semantics seems to be a better fit for isolated global subprog validation approach.
Implementation-wise, we utilize existing logic for matching user-provided BTF type to kernel-side BTF type, used by BPF CO-RE logic and following same matching rules. We enforce a unique match for types.
--- include/linux/bpf_verifier.h | 1 + kernel/bpf/btf.c | 99 +++++++++++++++++++++++++++++++----- kernel/bpf/verifier.c | 24 +++++++++ 3 files changed, 111 insertions(+), 13 deletions(-)
include/linux/bpf_verifier.h +1 -0
@@ -610,6 +610,7 @@ struct bpf_subprog_arg_info {enum bpf_arg_type arg_type;union {u32 mem_size; +u32 btf_id; }; };
kernel/bpf/btf.c +86 -13
132 lines not on this page · raw message
kernel/bpf/verifier.c +24 -0
@@ -9336,6 +9336,18 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, ret =process_dynptr_func (env, regno,-1 , arg->arg_type,0 );if (ret)return ret; + }else if (base_type (arg->arg_type) ==ARG_PTR_TO_BTF_ID ) { +struct bpf_call_arg_meta meta; +int err; + +if (register_is_null (reg) &&type_may_be_null (arg->arg_type)) +continue ; + +memset (&meta,0 ,sizeof (meta));/* leave func_id as zero */ + err =check_reg_type (env, regno, arg->arg_type, &arg->btf_id, &meta); + err = err ?:check_func_arg_reg_off (env, reg, regno, arg->arg_type); +if (err) +return err; }else {bpf_log (log,"verifier bug: unrecognized arg#%d type %d\n" , i, arg->arg_type);
@@ -20137,6 +20149,18 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog)mark_reg_known_zero (env, regs, i); reg->mem_size = arg->mem_size; 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 ) + reg->type |=PTR_MAYBE_NULL ; +if (arg->arg_type &PTR_UNTRUSTED ) + reg->type |=PTR_UNTRUSTED ; +if (arg->arg_type &PTR_TRUSTED ) + reg->type |=PTR_TRUSTED ; +mark_reg_known_zero (env, regs, i); + reg->btf =bpf_get_btf_vmlinux ();/* can't fail at this point */ + reg->btf_id = arg->btf_id; + reg->id = ++env->id_gen; }else {WARN_ONCE (1 ,"BUG: unhandled arg#%d type %d\n" , i -BPF_REG_1 , arg->arg_type);
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 2/4 bpf: add arg:nullable tag to be combined with trusted pointers
In reply to Andrii NakryikoAdd ability to mark arg:trusted arguments with optional arg:nullable tag to mark it as PTR_TO_BTF_ID_OR_NULL variant, which will allow callers to pass NULL, and subsequently will force global subprog's code to do NULL check. This allows to have "optional" PTR_TO_BTF_ID values passed into global subprogs.
For now arg:nullable cannot be combined with anything else.
--- kernel/bpf/btf.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
kernel/bpf/btf.c +10 -0
@@ -7056,6 +7056,7 @@ enum btf_arg_tag {ARG_TAG_CTX =0x1 ,ARG_TAG_NONNULL =0x2 ,ARG_TAG_TRUSTED =0x4 , + ARG_TAG_NULLABLE = 0x8, };/* Process BTF of a function to produce high-level expectation of function
@@ -7161,6 +7162,8 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog) tags |=ARG_TAG_TRUSTED ; }else if (strcmp (tag,"nonnull" ) ==0 ) { tags |=ARG_TAG_NONNULL ; + }else if (strcmp (tag,"nullable" ) ==0 ) { + tags |=ARG_TAG_NULLABLE ; }else {bpf_log (log,"arg#%d has unsupported set of tags\n" , i);return -EOPNOTSUPP ;
@@ -7210,12 +7213,19 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)return kern_type_id; sub->args[i].arg_type =ARG_PTR_TO_BTF_ID |PTR_TRUSTED ; +if (tags &ARG_TAG_NULLABLE ) + sub->args[i].arg_type |=PTR_MAYBE_NULL ; sub->args[i].btf_id = kern_type_id;continue ; }if (is_global) {/* generic user data pointer */ u32 mem_size; +if (tags &ARG_TAG_NULLABLE ) { +bpf_log (log,"arg#%d has invalid combination of tags\n" , i); +return -EINVAL ; + } + t =btf_type_skip_modifiers (btf, t->type,NULL ); ref_t =btf_resolve_size (btf, t, &mem_size);if (IS_ERR (ref_t)) {
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 3/4 libbpf: add __arg_trusted and __arg_nullable tag macros
In reply to Andrii NakryikoAdd __arg_trusted to annotate global func args that accept trusted PTR_TO_BTF_ID arguments.
Also add __arg_nullable to combine with __arg_trusted (and maybe other tags in the future) to force global subprog itself (i.e., callee) to do NULL checks, as opposed to default non-NULL semantics (and thus caller's responsibility to ensure non-NULL values).
--- tools/lib/bpf/bpf_helpers.h | 2 ++ 1 file changed, 2 insertions(+)
tools/lib/bpf/bpf_helpers.h +2 -0
@@ -190,6 +190,8 @@ enum libbpf_tristate {#define __arg_ctx __attribute__((btf_decl_tag("arg:ctx")))#define __arg_nonnull __attribute((btf_decl_tag("arg:nonnull"))) +#define __arg_nullable __attribute((btf_decl_tag("arg:nullable"))) +#define __arg_trusted __attribute((btf_decl_tag("arg:trusted")))#ifndef ___bpf_concat#define ___bpf_concat (a, b) a ## b
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 4/4 selftests/bpf: add trusted global subprog arg tests
In reply to Andrii NakryikoAdd a bunch of test cases validating behavior of __arg_trusted and its combination with __arg_nullable tag. We also validate CO-RE flavor support by kernel for __arg_trusted args.
--- .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_global_ptr_args.c | 156 ++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_global_ptr_args.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -28,6 +28,7 @@#include "verifier_div0.skel.h" #include "verifier_div_overflow.skel.h" #include "verifier_global_subprogs.skel.h" +#include "verifier_global_ptr_args.skel.h" #include "verifier_gotol.skel.h" #include "verifier_helper_access_var_len.skel.h" #include "verifier_helper_packet_access.skel.h"
@@ -140,6 +141,7 @@ void test_verifier_direct_stack_access_wraparound(void) { RUN(verifier_direct_stvoid test_verifier_div0 (void ) {RUN (verifier_div0); }void test_verifier_div_overflow (void ) {RUN (verifier_div_overflow); }void test_verifier_global_subprogs (void ) {RUN (verifier_global_subprogs); } +void test_verifier_global_ptr_args (void ) {RUN (verifier_global_ptr_args); }void test_verifier_gotol (void ) {RUN (verifier_gotol); }void test_verifier_helper_access_var_len (void ) {RUN (verifier_helper_access_var_len); }void test_verifier_helper_packet_access (void ) {RUN (verifier_helper_packet_access); }
tools/testing/selftests/bpf/progs/verifier_global_ptr_args.c new file +156 -0
156 lines not on this page · raw message
Signature · 1 line
2.34.1
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 5 lines · On Mon, 29 Jan 2024 16:06:44 -0800 you wrote:
This patch set follows recent changes that added btf_decl_tag-based argument annotation support for global subprogs. This time we add ability to pass
PTR_TO_BTF_ID(BTF-aware kernel pointers) arguments into global subprograms. We support explicitly trusted arguments only, for now.
Quoted 7 lines
Patch #1 adds logic for arg:trusted tag support on the verifier side. Default semantic of such arguments is non-NULL, enforced on caller side. But patch #2 adds arg:nullable tag that can be combined with arg:trusted to make callee explicitly do the NULL check, which helps implement "optional"
PTR_TO_BTF_IDarguments.[...]
Here is the summary with links:
- [v3,bpf-next,1/4] bpf: add __arg_trusted global func arg tag
https://git.kernel.org/bpf/bpf-next/c/e2b3c4ff5d18 - [v3,bpf-next,2/4] bpf: add arg:nullable tag to be combined with trusted pointers https://git.kernel.org/bpf/bpf-next/c/8f2b44cd9d69 - [v3,bpf-next,3/4] libbpf: add __arg_trusted and __arg_nullable tag macros https://git.kernel.org/bpf/bpf-next/c/d28bb1a86e68 - [v3,bpf-next,4/4] selftests/bpf: add trusted global subprog arg tests https://git.kernel.org/bpf/bpf-next/c/c381203eadb7
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html