Skip to content

Commit d028f87

Browse files
image-dragonAlexei Starovoitov
authored andcommitted
bpf: make the verifier tracks the "not equal" for regs
We can derive some new information for BPF_JNE in regs_refine_cond_op(). Take following code for example: /* The type of "a" is u32 */ if (a > 0 && a < 100) { /* the range of the register for a is [0, 99], not [1, 99], * and will cause the following error: * * invalid zero-sized read * * as a can be 0. */ bpf_skb_store_bytes(skb, xx, xx, a, 0); } In the code above, "a > 0" will be compiled to "jmp xxx if a == 0". In the TRUE branch, the dst_reg will be marked as known to 0. However, in the fallthrough(FALSE) branch, the dst_reg will not be handled, which makes the [min, max] for a is [0, 99], not [1, 99]. For BPF_JNE, we can reduce the range of the dst reg if the src reg is a const and is exactly the edge of the dst reg. Signed-off-by: Menglong Dong <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: Shung-Hsi Yu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 1728df7 commit d028f87

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

kernel/bpf/verifier.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14336,7 +14336,43 @@ static void regs_refine_cond_op(struct bpf_reg_state *reg1, struct bpf_reg_state
1433614336
}
1433714337
break;
1433814338
case BPF_JNE:
14339-
/* we don't derive any new information for inequality yet */
14339+
if (!is_reg_const(reg2, is_jmp32))
14340+
swap(reg1, reg2);
14341+
if (!is_reg_const(reg2, is_jmp32))
14342+
break;
14343+
14344+
/* try to recompute the bound of reg1 if reg2 is a const and
14345+
* is exactly the edge of reg1.
14346+
*/
14347+
val = reg_const_value(reg2, is_jmp32);
14348+
if (is_jmp32) {
14349+
/* u32_min_value is not equal to 0xffffffff at this point,
14350+
* because otherwise u32_max_value is 0xffffffff as well,
14351+
* in such a case both reg1 and reg2 would be constants,
14352+
* jump would be predicted and reg_set_min_max() won't
14353+
* be called.
14354+
*
14355+
* Same reasoning works for all {u,s}{min,max}{32,64} cases
14356+
* below.
14357+
*/
14358+
if (reg1->u32_min_value == (u32)val)
14359+
reg1->u32_min_value++;
14360+
if (reg1->u32_max_value == (u32)val)
14361+
reg1->u32_max_value--;
14362+
if (reg1->s32_min_value == (s32)val)
14363+
reg1->s32_min_value++;
14364+
if (reg1->s32_max_value == (s32)val)
14365+
reg1->s32_max_value--;
14366+
} else {
14367+
if (reg1->umin_value == (u64)val)
14368+
reg1->umin_value++;
14369+
if (reg1->umax_value == (u64)val)
14370+
reg1->umax_value--;
14371+
if (reg1->smin_value == (s64)val)
14372+
reg1->smin_value++;
14373+
if (reg1->smax_value == (s64)val)
14374+
reg1->smax_value--;
14375+
}
1434014376
break;
1434114377
case BPF_JSET:
1434214378
if (!is_reg_const(reg2, is_jmp32))

0 commit comments

Comments
 (0)