Skip to content

Commit e12da42

Browse files
Alexei Starovoitovgregkh
authored andcommitted
bpf/verifier: fix verifier instability
[ Upstream commit a9c676b ] 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> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9906a93 commit e12da42

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
@@ -553,7 +553,9 @@ static void __mark_reg_not_init(struct bpf_reg_state *reg);
553553
*/
554554
static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
555555
{
556-
reg->id = 0;
556+
/* Clear id, off, and union(map_ptr, range) */
557+
memset(((u8 *)reg) + sizeof(reg->type), 0,
558+
offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
557559
reg->var_off = tnum_const(imm);
558560
reg->smin_value = (s64)imm;
559561
reg->smax_value = (s64)imm;
@@ -572,7 +574,6 @@ static void __mark_reg_known_zero(struct bpf_reg_state *reg)
572574
static void __mark_reg_const_zero(struct bpf_reg_state *reg)
573575
{
574576
__mark_reg_known(reg, 0);
575-
reg->off = 0;
576577
reg->type = SCALAR_VALUE;
577578
}
578579

@@ -683,9 +684,12 @@ static void __mark_reg_unbounded(struct bpf_reg_state *reg)
683684
/* Mark a register as having a completely unknown (scalar) value. */
684685
static void __mark_reg_unknown(struct bpf_reg_state *reg)
685686
{
687+
/*
688+
* Clear type, id, off, and union(map_ptr, range) and
689+
* padding between 'type' and union
690+
*/
691+
memset(reg, 0, offsetof(struct bpf_reg_state, var_off));
686692
reg->type = SCALAR_VALUE;
687-
reg->id = 0;
688-
reg->off = 0;
689693
reg->var_off = tnum_unknown;
690694
reg->frameno = 0;
691695
__mark_reg_unbounded(reg);
@@ -1727,9 +1731,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
17271731
else
17281732
mark_reg_known_zero(env, regs,
17291733
value_regno);
1730-
regs[value_regno].id = 0;
1731-
regs[value_regno].off = 0;
1732-
regs[value_regno].range = 0;
17331734
regs[value_regno].type = reg_type;
17341735
}
17351736

@@ -2580,7 +2581,6 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
25802581
regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
25812582
/* There is no offset yet applied, variable or fixed */
25822583
mark_reg_known_zero(env, regs, BPF_REG_0);
2583-
regs[BPF_REG_0].off = 0;
25842584
/* remember map_ptr, so that check_map_access()
25852585
* can check 'value_size' boundary of memory access
25862586
* to map element returned from bpf_map_lookup_elem()

0 commit comments

Comments
 (0)