Re: [PATCH bpf-next v5 04/11] bpf: Track R2 of register-pair returns in precision backtracking
Yonghong Song <[email protected]>
| Newsgroups | org.kernel.vger.bpf |
|---|---|
| Message-ID | <[email protected]> |
On 8/14/26 4:37 PM, Eduard Zingerman wrote:
> On Thu, 2026-08-13 at 13:02 -0700, Yonghong Song wrote:
>
> ...
>
>> @@ -520,7 +541,41 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
>> return -EFAULT;
>> }
>> } else if (opcode == BPF_EXIT) {
>> - bool r0_precise;
>> + bool from_subprog_call, r0_precise, r2_precise;
>> + struct bpf_insn *call;
>> + int subprog;
>> +
>> + /* BPF_EXIT in subprog or callback always returns
>> + * right after the call instruction, so by checking
>> + * whether the instruction at subseq_idx-1 is subprog
>> + * call or not we can distinguish actual exit from
>> + * *subprog* from exit from *callback*. In the former
>> + * case, we need to propagate the precision of the
>> + * return registers, if necessary. In the latter we
>> + * never do that.
>> + */
>> + from_subprog_call = subseq_idx - 1 >= 0 &&
>> + bpf_pseudo_call(&env->prog->insnsi[subseq_idx - 1]);
>> +
>> + /* Sample the return registers before the callback
>> + * handling below clears R1-R5: unlike R0, R2 is an
>> + * argument register as well, so that clear would drop
>> + * a pair return on the floor.
>> + */
>> + r0_precise = from_subprog_call && bt_is_reg_set(bt, BPF_REG_0);
>> + r2_precise = false;
>> + if (from_subprog_call && bt_is_reg_set(bt, BPF_REG_2)) {
>> + call = &env->prog->insnsi[subseq_idx - 1];
>> + subprog = bpf_find_subprog(env, subseq_idx + call->imm);
>> + if (subprog < 0)
>> + return -EFAULT;
>> + /* Only a callee that does return a pair defines
>> + * R2. Leave the mask alone otherwise, so that
>> + * the check below still catches an R2 that has
>> + * no business being set.
>> + */
>> + r2_precise = bpf_ret_reg_pair(env, subprog);
>> + }
>>
>> /* Backtracking to a nested function call, 'idx' is a part of
>> * the inner frame 'subseq_idx' is a part of the outer frame.
> I still think that the above complications are unnecessary.
> The patch could be simplified by assuming that R2 always propagates
> w/o loosing verification safety. E.g. as in the attachment.
Ack. Thanks and will adopt your suggestions.