From: Alexei Starovoitov A trampoline keeps only R0 of the value that its target returns. Thus fexit, fmod_ret and fsession cannot attach to a function that returns more than 8 bytes. The check reads the size from the function model. When BTF of a static subprog is unreliable, the model is not made from BTF. It has 5 u64 arguments and a u64 return value. The check passes, and the caller of the subprog gets R2 as the trampoline left it, where the verifier saw the upper half of the result. BTF is unreliable because of the arguments. The verifier still takes the return type from BTF, and it verifies the subprog and its callers with a result in R0:R2. Keep that decision in bpf_func_info_aux, and refuse the attach when it is set. fentry does not touch the result. It can still attach. Fixes: 0630ad00d96d ("bpf: Add verifier support for 16-byte returns in R0: R2") Signed-off-by: Alexei Starovoitov --- include/linux/bpf.h | 2 ++ kernel/bpf/verifier.c | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 5413fa5d2b2b..ef94a168c902 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1763,6 +1763,8 @@ struct bpf_subprog_arg_info { struct bpf_func_info_aux { u16 linkage; bool unreliable; + /* The function returns in R0:R2 */ + bool ret_reg_pair; bool called[BPF_FUNC_CTX_CNT]; bool verified[BPF_FUNC_CTX_CNT]; bool called_with_pkt_ptrs; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index fa53ee735762..dcfced2573ee 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -465,6 +465,7 @@ static int bpf_compute_subprog_ret_regs(struct bpf_verifier_env *env) return -EFAULT; if (ret_regs_cnt(size) > 1) { subprog_info(env, subprog)->ret_reg_pair = true; + subprog_aux(env, subprog)->ret_reg_pair = true; env->prog->jit_required = 1; } } @@ -22990,7 +22991,9 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, return ret; } - if (tgt_info->fmodel.ret_size > 8 && + /* The model for unreliable BTF has a u64. The function still returns R0:R2. */ + if ((tgt_info->fmodel.ret_size > 8 || + (tgt_prog && tgt_prog->aux->func_info_aux[subprog].ret_reg_pair)) && attach_uses_trampoline_retval(prog->expected_attach_type)) { bpf_log(log, "Attach to function %s with a >8 byte return value is not supported for this attach type\n", -- 2.55.0