Allow variable offsets for syscall PTR_TO_CTX
Subsystems (5)
Kumar Kartikeya Dwivedi
maintainerv2 cover Allow variable offsets for syscall PTR_TO_CTX
Enable pointer modification with variable offsets accumulated in the register for PTR_TO_CTX for syscall programs where it won't be rewritten, and the context is user-supplied and checked against the max offset. See patches for details. Fixed offset support landed in [0].
By combining this set with [0], examples like the one below should succeed verification now.
SEC("syscall")
int prog(void *ctx) {
int *arr = ctx;
int i;
bpf_for(i, 0, 100)
arr[i] *= i;
return 0;}
[0]: https://lore.kernel.org/bpf/[email protected]
Changelog:
----------
v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]
* Harden check_func_arg_reg_off check with ARG_PTR_TO_CTX.
* Add tests for unmodified ctx into tail calls.
* Squash unmodified ctx change into base commit.
* Add Reviewed-by's from Emil.
Kumar Kartikeya Dwivedi (3):
bpf: Support variable offsets for syscall PTR_TO_CTX selftests/bpf: Add syscall ctx variable offset tests selftests/bpf: Test modified syscall ctx for ARG_PTR_TO_CTX
kernel/bpf/verifier.c | 65 ++-- .../selftests/bpf/prog_tests/verifier.c | 34 +- .../selftests/bpf/progs/verifier_ctx.c | 356 +++++++++++++++--- .../bpf/progs/verifier_global_subprogs.c | 95 ++++- .../selftests/bpf/test_kmods/bpf_testmod.c | 2 +- 5 files changed, 461 insertions(+), 91 deletions(-)
base-commit: 77378dabb50f593c756d393d8eacb0b91b758863
Signature · 1 line
2.52.0
Kumar Kartikeya Dwivedi
maintainerPATCH v2 1/3 bpf: Support variable offsets for syscall PTR_TO_CTX
Allow accessing PTR_TO_CTX with variable offsets in syscall programs. Fixed offsets are already enabled for all program types that do not convert their ctx accesses, since the changes we made in the commit de6c7d99f898 ("bpf: Relax fixed offset check for PTR_TO_CTX"). Note that we also lift the restriction on passing syscall context into helpers, which was not permitted before, and passing modified syscall context into kfuncs.
The structure of check_mem_access can be mostly shared and preserved, but we must use check_mem_region_access to correctly verify access with variable offsets.
The check made in check_helper_mem_access is hardened to only allow PTR_TO_CTX for syscall programs to be passed in as helper memory. This was the original intention of the existing code anyway, and it makes little sense for other program types' context to be utilized as a memory buffer. In case a convincing example presents itself in the future, this check can be relaxed further.
We also no longer use the last-byte access to simulate helper memory access, but instead go through check_mem_region_access. Since this no longer updates our max_ctx_offset, we must do so manually, to keep track of the maximum offset at which the program ctx may be accessed.
Take care to ensure that when arg_type is ARG_PTR_TO_CTX, we do not relax any fixed or variable offset constraints around PTR_TO_CTX even in syscall programs, and require them to be passed unmodified. There are several reasons why this is necessary. First, if we pass a modified ctx, then the global subprog's accesses will not update the max_ctx_offset to its true maximum offset, and can lead to out of bounds accesses. Second, tail called program (or extension program replacing global subprog) where their max_ctx_offset exceeds the program they are being called from can also cause issues. For the latter, unmodified PTR_TO_CTX is the first requirement for the fix, the second is ensuring max_ctx_offset >= the program they are being called from, which has to be a separate change not made in this commit.
All in all, we can hint using arg_type when we expect ARG_PTR_TO_CTX and make our relaxation around offsets conditional on it.
--- kernel/bpf/verifier.c | 65 ++++++++++++------- .../bpf/progs/verifier_global_subprogs.c | 1 - 2 files changed, 42 insertions(+), 24 deletions(-)
kernel/bpf/verifier.c +42 -23
@@ -7843,6 +7843,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn* Program types that don't rewrite ctx accesses can safely * dereference ctx pointers with fixed offsets. */ +bool var_off_ok =resolve_prog_type (env->prog) ==BPF_PROG_TYPE_SYSCALL ;bool fixed_off_ok = !env->ops->convert_ctx_access;struct bpf_retval_range range;struct bpf_insn_access_aux info = {
@@ -7857,16 +7858,26 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnreturn -EACCES ; } - err =__check_ptr_off_reg (env, reg, regno, fixed_off_ok); -if (err <0 ) -return err; -/* * Fold the register's constant offset into the insn offset so -* that is_valid_access() sees the true effective offset. +* that is_valid_access() sees the true effective offset. If the +* register's offset is allowed to be variable, then the maximum +* possible offset is simulated (which is equal to var_off.value +* when var_off is constant). */ -if (fixed_off_ok) - off += reg->var_off.value; +if (var_off_ok) { + err =check_mem_region_access (env, regno, off, size,U16_MAX , false); +if (err) +return err; + off += reg->umax_value; + }else { + err =__check_ptr_off_reg (env, reg, regno, fixed_off_ok); +if (err <0 ) +return err; +if (fixed_off_ok) + off += reg->var_off.value; + } + err =check_ctx_access (env, insn_idx, off, size, t, &info);if (err)verbose_linfo (env, insn_idx,"; " );
@@ -8442,22 +8453,16 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,return check_ptr_to_btf_access (env, regs, regno,0 , access_size,BPF_READ ,-1 );case PTR_TO_CTX : -/* in case the function doesn't know how to access the context, -* (because we are in a program of type SYSCALL for example), we -* can not statically check its size. -* Dynamically check it now. -*/ -if (!env->ops->convert_ctx_access) { -int offset = access_size -1 ; - -/* Allow zero-byte read from PTR_TO_CTX */ -if (access_size ==0 ) -return zero_size_allowed ?0 : -EACCES ; - -return check_mem_access (env, env->insn_idx, regno, offset,BPF_B , - access_type,-1 , false, false); +/* Only permit reading or writing syscall context using helper calls. */ +if (resolve_prog_type (env->prog) ==BPF_PROG_TYPE_SYSCALL ) { +int err =check_mem_region_access (env, regno,0 , access_size,U16_MAX , + zero_size_allowed); +if (err) +return err; +if (env->prog->aux->max_ctx_offset < reg->umax_value + access_size) + env->prog->aux->max_ctx_offset = reg->umax_value + access_size; +return 0 ; } - fallthrough;default :/* scalar_value or invalid ptr */ /* Allow zero-byte read from NULL, regardless of pointer type */
@@ -9401,6 +9406,7 @@ static const struct bpf_reg_types mem_types = { PTR_TO_MEM | MEM_RINGBUF, PTR_TO_BUF, PTR_TO_BTF_ID | PTR_TRUSTED, + PTR_TO_CTX, }, };
@@ -9710,6 +9716,17 @@ static int check_func_arg_reg_off(struct bpf_verifier_env *env,* still need to do checks instead of returning. */ return __check_ptr_off_reg (env, reg, regno, true); +case PTR_TO_CTX : +/* +* Allow fixed and variable offsets for syscall context, but +* only when the argument is passed as memory, not ctx, +* otherwise we may get modified ctx in tail called programs and +* global subprogs (that may act as extension prog hooks). +*/ +if (arg_type !=ARG_PTR_TO_CTX && +resolve_prog_type (env->prog) ==BPF_PROG_TYPE_SYSCALL ) +return 0 ; + fallthrough;default :return __check_ptr_off_reg (env, reg, regno, false); }
@@ -10757,7 +10774,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,* invalid memory access. */ }else if (arg->arg_type ==ARG_PTR_TO_CTX ) { - ret =check_func_arg_reg_off (env, reg, regno,ARG_DONTCARE ); + ret =check_func_arg_reg_off (env, reg, regno,ARG_PTR_TO_CTX );if (ret <0 )return ret;/* If function expects ctx type in BTF check that caller
@@ -13566,6 +13583,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ } fallthrough;case KF_ARG_PTR_TO_CTX : + arg_type =ARG_PTR_TO_CTX ; +break ;case KF_ARG_PTR_TO_DYNPTR :case KF_ARG_PTR_TO_ITER :case KF_ARG_PTR_TO_LIST_HEAD :
tools/testing/selftests/bpf/progs/verifier_global_subprogs.c +0 -1
@@ -134,7 +134,6 @@ __noinline __weak int subprog_user_anon_mem(user_struct_t *t) SEC("?tracepoint") __failure __log_level(2) -__msg("invalid bpf_context access") __msg("Caller passes invalid args into func#1 ('subprog_user_anon_mem')")int anon_user_mem_invalid (void *ctx) {
Signature · 1 line
2.52.0
Kumar Kartikeya Dwivedi
maintainerPATCH v2 2/3 selftests/bpf: Add syscall ctx variable offset tests
In reply to Kumar Kartikeya DwivediAdd various tests to exercise fixed and variable offsets on PTR_TO_CTX for syscall programs, and cover disallowed cases for other program types lacking convert_ctx_access callback. Load verifier_ctx with CAP_SYS_ADMIN so that kfunc related logic can be tested. While at it, convert assembly tests to C.
--- .../selftests/bpf/prog_tests/verifier.c | 34 +- .../selftests/bpf/progs/verifier_ctx.c | 356 +++++++++++++++--- .../selftests/bpf/test_kmods/bpf_testmod.c | 2 +- 3 files changed, 325 insertions(+), 67 deletions(-)
tools/testing/selftests/bpf/prog_tests/verifier.c +21 -13
@@ -126,29 +126,35 @@ struct test_val {__maybe_unused static void run_tests_aux (const char *skel_name,skel_elf_bytes_fn elf_bytes_factory, -pre_execution_cb pre_execution_cb) +pre_execution_cb pre_execution_cb, +bool drop_sys_admin) {struct test_loader tester = {};__u64 old_caps;int err; -/* test_verifier tests are executed w/o CAP_SYS_ADMIN, do the same here */ - err =cap_disable_effective (1ULL <<CAP_SYS_ADMIN , &old_caps); -if (err) { -PRINT_FAIL ("failed to drop CAP_SYS_ADMIN: %i, %s\n" , err,strerror (-err)); -return ; +if (drop_sys_admin) { +/* test_verifier tests are executed w/o CAP_SYS_ADMIN, do the same here */ + err =cap_disable_effective (1ULL <<CAP_SYS_ADMIN , &old_caps); +if (err) { +PRINT_FAIL ("failed to drop CAP_SYS_ADMIN: %i, %s\n" , err,strerror (-err)); +return ; + } }test_loader__set_pre_execution_cb (&tester, pre_execution_cb);test_loader__run_subtests (&tester, skel_name, elf_bytes_factory);test_loader_fini (&tester); - err =cap_enable_effective (old_caps,NULL ); -if (err) -PRINT_FAIL ("failed to restore CAP_SYS_ADMIN: %i, %s\n" , err,strerror (-err)); +if (drop_sys_admin) { + err =cap_enable_effective (old_caps,NULL ); +if (err) +PRINT_FAIL ("failed to restore CAP_SYS_ADMIN: %i, %s\n" , err,strerror (-err)); + } } -#define RUN (skel) run_tests_aux(#skel, skel##__elf_bytes, NULL) +#define RUN (skel) run_tests_aux(#skel, skel##__elf_bytes, NULL, true) +#define RUN_WITH_CAP_SYS_ADMIN (skel) run_tests_aux(#skel, skel##__elf_bytes, NULL, false)void test_verifier_align (void ) {RUN (verifier_align); }void test_verifier_and (void ) {RUN (verifier_and); }
@@ -173,7 +179,7 @@ void test_verifier_cgroup_skb(void) { RUN(verifier_cgroup_skb); }void test_verifier_cgroup_storage (void ) {RUN (verifier_cgroup_storage); }void test_verifier_const (void ) {RUN (verifier_const); }void test_verifier_const_or (void ) {RUN (verifier_const_or); } -void test_verifier_ctx (void ) {RUN (verifier_ctx); } +void test_verifier_ctx (void ) {RUN_WITH_CAP_SYS_ADMIN (verifier_ctx); }void test_verifier_ctx_sk_msg (void ) {RUN (verifier_ctx_sk_msg); }void test_verifier_d_path (void ) {RUN (verifier_d_path); }void test_verifier_default_trusted_ptr (void ) {RUN_TESTS (verifier_default_trusted_ptr); }
@@ -293,7 +299,8 @@ void test_verifier_array_access(void) {run_tests_aux ("verifier_array_access" , verifier_array_access__elf_bytes, - init_array_access_maps); + init_array_access_maps, + true); }void test_verifier_async_cb_context (void ) {RUN (verifier_async_cb_context); }
@@ -306,5 +313,6 @@ void test_verifier_value_ptr_arith(void) {run_tests_aux ("verifier_value_ptr_arith" , verifier_value_ptr_arith__elf_bytes, - init_value_ptr_arith_maps); + init_value_ptr_arith_maps, + true); }
tools/testing/selftests/bpf/progs/verifier_ctx.c +303 -53
393 lines not on this page · raw message
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +1 -1
@@ -716,6 +716,7 @@ BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_value) BTF_ID_FLAGS(func, bpf_kfunc_common_test) +BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1) BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test) BTF_ID_FLAGS(func, bpf_kfunc_nested_acquire_nonzero_offset_test, KF_ACQUIRE)BTF_ID_FLAGS (func, bpf_kfunc_nested_acquire_zero_offset_test,KF_ACQUIRE )
@@ -1280,7 +1281,6 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test2) BTF_ID_FLAGS(func, bpf_kfunc_call_test3) BTF_ID_FLAGS(func, bpf_kfunc_call_test4) BTF_ID_FLAGS(func, bpf_kfunc_call_test5) -BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1) BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1) BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2)BTF_ID_FLAGS (func, bpf_kfunc_call_test_acquire,KF_ACQUIRE |KF_RET_NULL )
Signature · 1 line
2.52.0
Kumar Kartikeya Dwivedi
maintainerPATCH v2 3/3 selftests/bpf: Test modified syscall ctx for ARG_PTR_TO_CTX
In reply to Kumar Kartikeya DwivediEnsure that global subprogs and tail calls can only accept an unmodified PTR_TO_CTX for syscall programs. For all other program types, fixed or variable offsets on PTR_TO_CTX is rejected when passed into an argument of any call instruction type, through the unified logic of check_func_arg_reg_off.
Finally, add a positive example of a case that should succeed with all our previous changes.
--- .../bpf/progs/verifier_global_subprogs.c | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+)
tools/testing/selftests/bpf/progs/verifier_global_subprogs.c +94 -0
@@ -357,6 +357,100 @@ int arg_tag_ctx_syscall(void *ctx)return tracing_subprog_void (ctx) +tracing_subprog_u64 (ctx) +tp_whatever (ctx); } +__weak int syscall_array_bpf_for(void *ctx __arg_ctx) +{ +int *arr = ctx; +int i; + +bpf_for (i,0 ,100 ) + arr[i] *= i; + +return 0 ; +} + +SEC("?syscall") +__success __log_level(2) +int arg_tag_ctx_syscall_bpf_for(void *ctx) +{ +return syscall_array_bpf_for (ctx); +} + +SEC("syscall") +__auxiliary +int syscall_tailcall_target (void *ctx) +{ +return syscall_array_bpf_for (ctx); +} + +struct { + __uint(type, BPF_MAP_TYPE_PROG_ARRAY); + __uint(max_entries, 1); + __uint(key_size, sizeof(__u32)); + __array(values, int (void *)); +} syscall_prog_array SEC(".maps") = { + .values = { + [0] = (void *)&syscall_tailcall_target, + }, +}; + +SEC("?syscall") +__success __log_level(2) +int arg_tag_ctx_syscall_tailcall(void *ctx) +{ +bpf_tail_call (ctx, &syscall_prog_array,0 ); +return 0 ; +} + +SEC("?syscall") +__failure __log_level(2) +__msg("dereference of modified ctx ptr R1 off=8 disallowed") +int arg_tag_ctx_syscall_tailcall_fixed_off_bad (void *ctx) +{ +char *p = ctx; + + p +=8 ; +bpf_tail_call (p, &syscall_prog_array,0 ); +return 0 ; +} + +SEC("?syscall") +__failure __log_level(2) +__msg("variable ctx access var_off=(0x0; 0x4) disallowed") +int arg_tag_ctx_syscall_tailcall_var_off_bad (void *ctx) +{ +__u64 off =bpf_get_prandom_u32 (); +char *p = ctx; + + off &=4 ; + p += off; +bpf_tail_call (p, &syscall_prog_array,0 ); +return 0 ; +} + +SEC("?syscall") +__failure __log_level(2) +__msg("dereference of modified ctx ptr R1 off=8 disallowed") +int arg_tag_ctx_syscall_fixed_off_bad (void *ctx) +{ +char *p = ctx; + + p +=8 ; +return subprog_ctx_tag (p); +} + +SEC("?syscall") +__failure __log_level(2) +__msg("variable ctx access var_off=(0x0; 0x4) disallowed") +int arg_tag_ctx_syscall_var_off_bad (void *ctx) +{ +__u64 off =bpf_get_prandom_u32 (); +char *p = ctx; + + off &=4 ; + p += off; +return subprog_ctx_tag (p); +} +__weak int subprog_dynptr (struct bpf_dynptr *dptr) {long *d, t, buf[1 ] = {};
Signature · 1 line
2.52.0