Skip to content

Commit a9c676b

Browse files
author
Alexei Starovoitov
committed
bpf/verifier: fix verifier instability
Edward Cree says: In check_mem_access(), for the PTR_TO_CTX case, after check_ctx_access() has supplied a reg_type, the other members of the register state are set appropriately. Previously reg.range was set to 0, but as it is in a union with reg.map_ptr, which is larger, upper bytes of the latter were left in place. This then caused the memcmp() in regsafe() to fail, preventing some branches from being pruned (and occasionally causing the same program to take a varying number of processed insns on repeated verifier runs). Fix the instability by clearing bpf_reg_state in __mark_reg_[un]known() Fixes: f1174f7 ("bpf/verifier: rework value tracking") Debugged-by: Edward Cree <ecree@solarflare.com> Acked-by: Edward Cree <ecree@solarflare.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 69495d2 commit a9c676b

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

‎kernel/bpf/verifier.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,9 @@ static void __mark_reg_not_init(struct bpf_reg_state *reg);
570570
*/
571571
static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
572572
{
573-
reg->id = 0;
573+
/* Clear id, off, and union(map_ptr, range) */
574+
memset(((u8 *)reg) + sizeof(reg->type), 0,
575+
offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
574576
reg->var_off = tnum_const(imm);
575577
reg->smin_value = (s64)imm;
576578
reg->smax_value = (s64)imm;
@@ -589,7 +591,6 @@ static void __mark_reg_known_zero(struct bpf_reg_state *reg)
589591
static void __mark_reg_const_zero(struct bpf_reg_state *reg)
590592
{
591593
__mark_reg_known(reg, 0);
592-
reg->off = 0;
593594
reg->type = SCALAR_VALUE;
594595
}
595596

@@ -700,9 +701,12 @@ static void __mark_reg_unbounded(struct bpf_reg_state *reg)
700701
/* Mark a register as having a completely unknown (scalar) value. */
701702
static void __mark_reg_unknown(struct bpf_reg_state *reg)
702703
{
704+
/*
705+
* Clear type, id, off, and union(map_ptr, range) and
706+
* padding between 'type' and union
707+
*/
708+
memset(reg, 0, offsetof(struct bpf_reg_state, var_off));
703709
reg->type = SCALAR_VALUE;
704-
reg->id = 0;
705-
reg->off = 0;
706710
reg->var_off = tnum_unknown;
707711
reg->frameno = 0;
708712
__mark_reg_unbounded(reg);
@@ -1640,9 +1644,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
16401644
else
16411645
mark_reg_known_zero(env, regs,
16421646
value_regno);
1643-
regs[value_regno].id = 0;
1644-
regs[value_regno].off = 0;
1645-
regs[value_regno].range = 0;
16461647
regs[value_regno].type = reg_type;
16471648
}
16481649

@@ -2495,7 +2496,6 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
24952496
regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
24962497
/* There is no offset yet applied, variable or fixed */
24972498
mark_reg_known_zero(env, regs, BPF_REG_0);
2498-
regs[BPF_REG_0].off = 0;
24992499
/* remember map_ptr, so that check_map_access()
25002500
* can check 'value_size' boundary of memory access
25012501
* to map element returned from bpf_map_lookup_elem()

0 commit comments

Comments
 (0)