When computing liveness marks for arguments of static subprograms bpf_compute_live_registers() should mark all arguments in the subprogram's BTF type as live. This over-approximates the program behaviour but avoids gaps in unreliable BTF detection: - Suppose subprogram has arguments A, B, C. - As a result of compiler optimizations, only arguments A and C are used and passed at call sites. - As it is now, bpf_compute_live_registers() would detect that and mark the register r2 as dead at call sites. - Dead registers are ignored during states pruning, meaning that a cached state from a path that initializes r2 can prune a path that leaves it uninitialized. - This would bypass btf_check_func_arg_match() call and thus would not set bpf_func_info_aux->unreliable mark for the subprogram. Revert the expectation in the test case [1] back to it's original value, subprogram `subprog()` is considered reliable again. [1] "exceptions/non-throwing extension -> non-throwing subprog" Fixes: 147400e9c27c ("bpf: Compute parameters liveness in bpf_compute_live_registers()") Signed-off-by: Eduard Zingerman --- kernel/bpf/btf.c | 8 ++++--- kernel/bpf/liveness.c | 15 +++++++------ kernel/bpf/verifier.c | 26 ++++++++++++++++------ .../testing/selftests/bpf/prog_tests/exceptions.c | 2 +- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index f4d119f3d5ef..5f4e211b1bdb 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -8634,7 +8634,6 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog) } args = (const struct btf_param *)(t + 1); nargs = btf_type_vlen(t); - sub->arg_slot_cnt = nargs; if (nargs > MAX_BPF_FUNC_ARGS) { bpf_log(log, "kernel supports at most %d parameters, function %s has %d\n", MAX_BPF_FUNC_ARGS, tname, nargs); @@ -8829,10 +8828,13 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog) return -EINVAL; } - err = btf_check_arg_slots(log, tname, is_global, slots_used, sub); + if (verifier_bug_if(slots_used != sub->arg_slot_cnt, env, + "func#%d argument slot count mismatch: %u != %u", + subprog, slots_used, sub->arg_slot_cnt)) + return -EFAULT; + err = btf_check_arg_slots(log, tname, is_global, sub->arg_slot_cnt, sub); if (err) return err; - sub->arg_slot_cnt = slots_used; sub->args_cached = true; diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c index e657e6d95408..024790097f96 100644 --- a/kernel/bpf/liveness.c +++ b/kernel/bpf/liveness.c @@ -2623,14 +2623,15 @@ static void compute_insn_live_regs(struct bpf_verifier_env *env, if (bpf_get_call_summary(env, insn, &cs)) { use = GENMASK(min_t(u8, cs.arg_slot_cnt, MAX_BPF_FUNC_REG_ARGS), 1); } else if (bpf_pseudo_call(insn)) { - /* a static callee's reads are added in bpf_compute_live_registers() */ + /* + * Use BTF signatures as a starting point. + * See special case for static subprograms + * in the bpf_compute_live_registers()'s main loop. + */ subprog = callee_subprog(env, insn); - if (bpf_subprog_is_global(env, subprog)) { - reg_args = min_t(u8, env->subprog_info[subprog].arg_slot_cnt, MAX_BPF_FUNC_REG_ARGS); - use = GENMASK(reg_args, 1); - } else { - use = 0; - } + reg_args = min_t(u8, env->subprog_info[subprog].arg_slot_cnt, + MAX_BPF_FUNC_REG_ARGS); + use = GENMASK(reg_args, 1); } def = mask_widen(def); use = mask_widen(use); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 2f190abe3448..fc1937b37dcf 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -14834,15 +14834,27 @@ static bool try_btf_prepare_func_args(struct bpf_verifier_env *env, int subprog) return !err; } -/* Prepare argument metadata for liveness; the main program is handled separately. */ +/* Initialize env->subprog_info[].arg_slot_cnt */ static void prepare_subprog_args(struct bpf_verifier_env *env) { + struct bpf_prog_aux *aux = env->prog->aux; + const struct btf_type *t; int i; - for (i = 1; i < env->subprog_cnt; i++) { - if (!try_btf_prepare_func_args(env, i) && bpf_subprog_is_global(env, i)) - /* Keep all argument registers live if the prototype is unavailable. */ - env->subprog_info[i].arg_slot_cnt = MAX_BPF_FUNC_REG_ARGS; + for (i = 0; i < env->subprog_cnt; i++) { + /* + * Keep all argument registers live if the prototype is unavailable. + * No need to include stack args, as those are not supported for + * subprograms w/o BTF. + */ + env->subprog_info[i].arg_slot_cnt = MAX_BPF_FUNC_REG_ARGS; + if (!aux->func_info) + continue; + /* BTF validation guarantees FUNC -> FUNC_PROTO. */ + t = btf_type_by_id(aux->btf, aux->func_info[i].type_id); + t = btf_type_by_id(aux->btf, t->type); + env->subprog_info[i].arg_slot_cnt = min_t(u32, btf_proto_slots(aux->btf, t), + MAX_BPF_FUNC_ARGS); } } @@ -23396,6 +23408,8 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, if (ret < 0) goto skip_full_check; + prepare_subprog_args(env); + ret = check_subprogs(env); if (ret < 0) goto skip_full_check; @@ -23458,8 +23472,6 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, if (ret < 0) goto skip_full_check; - prepare_subprog_args(env); - ret = bpf_compute_idoms(env); if (ret < 0) goto skip_full_check; diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions.c b/tools/testing/selftests/bpf/prog_tests/exceptions.c index cea01d638f6f..3705db18c1bb 100644 --- a/tools/testing/selftests/bpf/prog_tests/exceptions.c +++ b/tools/testing/selftests/bpf/prog_tests/exceptions.c @@ -322,7 +322,7 @@ static void test_exceptions_success(void) bpf_program__fd(skel->progs.exception_throw_subprog), "subprog"), "set_attach_target")) goto done; - }), "Cannot replace static functions", 0); + }), "subprog() is not a global function", 0); if (test__start_subtest("non-throwing extension -> throwing subprog")) /* non-throwing extension -> throwing subprog : BAD (!global) */ -- 2.53.0