]> www.infradead.org Git - linux.git/commitdiff
bpf: Allow 'may_goto 0' instruction in verifier
authorYonghong Song <yonghong.song@linux.dev>
Sat, 18 Jan 2025 19:20:24 +0000 (11:20 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 20 Jan 2025 17:43:29 +0000 (09:43 -0800)
Commit 011832b97b31 ("bpf: Introduce may_goto instruction") added support
for may_goto insn. The 'may_goto 0' insn is disallowed since the insn is
equivalent to a nop as both branch will go to the next insn.

But it is possible that compiler transformation may generate 'may_goto 0'
insn. Emil Tsalapatis from Meta reported such a case which caused
verification failure. For example, for the following code,
   int i, tmp[3];
   for (i = 0; i < 3 && can_loop; i++)
     tmp[i] = 0;
   ...

clang 20 may generate code like
   may_goto 2;
   may_goto 1;
   may_goto 0;
   r1 = 0; /* tmp[0] = 0; */
   r2 = 0; /* tmp[1] = 0; */
   r3 = 0; /* tmp[2] = 0; */

Let us permit 'may_goto 0' insn to avoid verification failure for codes
like the above.

Reported-by: Emil Tsalapatis <etsal@meta.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/r/20250118192024.2124059-1-yonghong.song@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/verifier.c

index 245f1f3f1aecc52dfb863a430f4eb74fcce3cf9f..963dfda81c0612d49651651a1aceb1fdee95748b 100644 (file)
@@ -15972,9 +15972,8 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
 
                if (insn->code != (BPF_JMP | BPF_JCOND) ||
                    insn->src_reg != BPF_MAY_GOTO ||
-                   insn->dst_reg || insn->imm || insn->off == 0) {
-                       verbose(env, "invalid may_goto off %d imm %d\n",
-                               insn->off, insn->imm);
+                   insn->dst_reg || insn->imm) {
+                       verbose(env, "invalid may_goto imm %d\n", insn->imm);
                        return -EINVAL;
                }
                prev_st = find_prev_entry(env, cur_st->parent, idx);