Skip to content

Commit bfe9446

Browse files
eddyz87gregkh
authored andcommitted
bpf: sync_linked_regs() must preserve subreg_def
commit e9bd9c4 upstream. Range propagation must not affect subreg_def marks, otherwise the following example is rewritten by verifier incorrectly when BPF_F_TEST_RND_HI32 flag is set: 0: call bpf_ktime_get_ns call bpf_ktime_get_ns 1: r0 &= 0x7fffffff after verifier r0 &= 0x7fffffff 2: w1 = w0 rewrites w1 = w0 3: if w0 < 10 goto +0 --------------> r11 = 0x2f5674a6 (r) 4: r1 >>= 32 r11 <<= 32 (r) 5: r0 = r1 r1 |= r11 (r) 6: exit; if w0 < 0xa goto pc+0 r1 >>= 32 r0 = r1 exit (or zero extension of w1 at (2) is missing for architectures that require zero extension for upper register half). The following happens w/o this patch: - r0 is marked as not a subreg at (0); - w1 is marked as subreg at (2); - w1 subreg_def is overridden at (3) by copy_register_state(); - w1 is read at (5) but mark_insn_zext() does not mark (2) for zero extension, because w1 subreg_def is not set; - because of BPF_F_TEST_RND_HI32 flag verifier inserts random value for hi32 bits of (2) (marked (r)); - this random value is read at (5). Fixes: 7574883 ("bpf: Propagate scalar ranges through register assignments.") Reported-by: Lonial Con <[email protected]> Signed-off-by: Lonial Con <[email protected]> Signed-off-by: Eduard Zingerman <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Closes: https://lore.kernel.org/bpf/[email protected] Link: https://lore.kernel.org/bpf/[email protected] [ shung-hsi.yu: sync_linked_regs() was called find_equal_scalars() before commit 4bf79f9 ("bpf: Track equal scalars history on per-instruction level"), and modification is done because there is only a single call to copy_register_state() before commit 98d7ca3 ("bpf: Track delta between "linked" registers."). ] Signed-off-by: Shung-Hsi Yu <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 4e76efd commit bfe9446

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

kernel/bpf/verifier.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14497,8 +14497,11 @@ static void find_equal_scalars(struct bpf_verifier_state *vstate,
1449714497
struct bpf_reg_state *reg;
1449814498

1449914499
bpf_for_each_reg_in_vstate(vstate, state, reg, ({
14500-
if (reg->type == SCALAR_VALUE && reg->id == known_reg->id)
14500+
if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) {
14501+
s32 saved_subreg_def = reg->subreg_def;
1450114502
copy_register_state(reg, known_reg);
14503+
reg->subreg_def = saved_subreg_def;
14504+
}
1450214505
}));
1450314506
}
1450414507

0 commit comments

Comments
 (0)