Skip to content

Commit 179ee84

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: Fix incorrect pruning due to atomic fetch precision tracking
When backtrack_insn encounters a BPF_STX instruction with BPF_ATOMIC and BPF_FETCH, the src register (or r0 for BPF_CMPXCHG) also acts as a destination, thus receiving the old value from the memory location. The current backtracking logic does not account for this. It treats atomic fetch operations the same as regular stores where the src register is only an input. This leads the backtrack_insn to fail to propagate precision to the stack location, which is then not marked as precise! Later, the verifier's path pruning can incorrectly consider two states equivalent when they differ in terms of stack state. Meaning, two branches can be treated as equivalent and thus get pruned when they should not be seen as such. Fix it as follows: Extend the BPF_LDX handling in backtrack_insn to also cover atomic fetch operations via is_atomic_fetch_insn() helper. When the fetch dst register is being tracked for precision, clear it, and propagate precision over to the stack slot. For non-stack memory, the precision walk stops at the atomic instruction, same as regular BPF_LDX. This covers all fetch variants. Before: 0: (b7) r1 = 8 ; R1=8 1: (7b) *(u64 *)(r10 -8) = r1 ; R1=8 R10=fp0 fp-8=8 2: (b7) r2 = 0 ; R2=0 3: (db) r2 = atomic64_fetch_add((u64 *)(r10 -8), r2) ; R2=8 R10=fp0 fp-8=mmmmmmmm 4: (bf) r3 = r10 ; R3=fp0 R10=fp0 5: (0f) r3 += r2 mark_precise: frame0: last_idx 5 first_idx 0 subseq_idx -1 mark_precise: frame0: regs=r2 stack= before 4: (bf) r3 = r10 mark_precise: frame0: regs=r2 stack= before 3: (db) r2 = atomic64_fetch_add((u64 *)(r10 -8), r2) mark_precise: frame0: regs=r2 stack= before 2: (b7) r2 = 0 6: R2=8 R3=fp8 6: (b7) r0 = 0 ; R0=0 7: (95) exit After: 0: (b7) r1 = 8 ; R1=8 1: (7b) *(u64 *)(r10 -8) = r1 ; R1=8 R10=fp0 fp-8=8 2: (b7) r2 = 0 ; R2=0 3: (db) r2 = atomic64_fetch_add((u64 *)(r10 -8), r2) ; R2=8 R10=fp0 fp-8=mmmmmmmm 4: (bf) r3 = r10 ; R3=fp0 R10=fp0 5: (0f) r3 += r2 mark_precise: frame0: last_idx 5 first_idx 0 subseq_idx -1 mark_precise: frame0: regs=r2 stack= before 4: (bf) r3 = r10 mark_precise: frame0: regs=r2 stack= before 3: (db) r2 = atomic64_fetch_add((u64 *)(r10 -8), r2) mark_precise: frame0: regs= stack=-8 before 2: (b7) r2 = 0 mark_precise: frame0: regs= stack=-8 before 1: (7b) *(u64 *)(r10 -8) = r1 mark_precise: frame0: regs=r1 stack= before 0: (b7) r1 = 8 6: R2=8 R3=fp8 6: (b7) r0 = 0 ; R0=0 7: (95) exit Fixes: 5ffa255 ("bpf: Add instructions for atomic_[cmp]xchg") Fixes: 5ca419f ("bpf: Add BPF_FETCH field / create atomic_fetch_add instruction") Reported-by: STAR Labs SG <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent eb7024b commit 179ee84

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,13 @@ static bool is_atomic_load_insn(const struct bpf_insn *insn)
617617
insn->imm == BPF_LOAD_ACQ;
618618
}
619619

620+
static bool is_atomic_fetch_insn(const struct bpf_insn *insn)
621+
{
622+
return BPF_CLASS(insn->code) == BPF_STX &&
623+
BPF_MODE(insn->code) == BPF_ATOMIC &&
624+
(insn->imm & BPF_FETCH);
625+
}
626+
620627
static int __get_spi(s32 off)
621628
{
622629
return (-off - 1) / BPF_REG_SIZE;
@@ -4447,10 +4454,24 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
44474454
* dreg still needs precision before this insn
44484455
*/
44494456
}
4450-
} else if (class == BPF_LDX || is_atomic_load_insn(insn)) {
4451-
if (!bt_is_reg_set(bt, dreg))
4457+
} else if (class == BPF_LDX ||
4458+
is_atomic_load_insn(insn) ||
4459+
is_atomic_fetch_insn(insn)) {
4460+
u32 load_reg = dreg;
4461+
4462+
/*
4463+
* Atomic fetch operation writes the old value into
4464+
* a register (sreg or r0) and if it was tracked for
4465+
* precision, propagate to the stack slot like we do
4466+
* in regular ldx.
4467+
*/
4468+
if (is_atomic_fetch_insn(insn))
4469+
load_reg = insn->imm == BPF_CMPXCHG ?
4470+
BPF_REG_0 : sreg;
4471+
4472+
if (!bt_is_reg_set(bt, load_reg))
44524473
return 0;
4453-
bt_clear_reg(bt, dreg);
4474+
bt_clear_reg(bt, load_reg);
44544475

44554476
/* scalars can only be spilled into stack w/o losing precision.
44564477
* Load from any other memory can be zero extended.

0 commit comments

Comments
 (0)