Skip to content

Commit c86dff4

Browse files
borkmanngregkh
authored andcommitted
bpf: Fix clobbering of r2 in bpf_gen_ld_abs
[ Upstream commit e6a18d3 ] Bryce reported that he saw the following with: 0: r6 = r1 1: r1 = 12 2: r0 = *(u16 *)skb[r1] The xlated sequence was incorrectly clobbering r2 with pointer value of r6 ... 0: (bf) r6 = r1 1: (b7) r1 = 12 2: (bf) r1 = r6 3: (bf) r2 = r1 4: (85) call bpf_skb_load_helper_16_no_cache#7692160 ... and hence call to the load helper never succeeded given the offset was too high. Fix it by reordering the load of r6 to r1. Other than that the insn has similar calling convention than BPF helpers, that is, r0 - r5 are scratch regs, so nothing else affected after the insn. Fixes: e0cea7c ("bpf: implement ld_abs/ld_ind in native bpf") Reported-by: Bryce Kahle <bryce.kahle@datadoghq.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/cace836e4d07bb63b1a53e49c5dfb238a040c298.1599512096.git.daniel@iogearbox.net Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 36133c2 commit c86dff4

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

‎net/core/filter.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6998,15 +6998,15 @@ static int bpf_gen_ld_abs(const struct bpf_insn *orig,
69986998
bool indirect = BPF_MODE(orig->code) == BPF_IND;
69996999
struct bpf_insn *insn = insn_buf;
70007000

7001-
/* We're guaranteed here that CTX is in R6. */
7002-
*insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
70037001
if (!indirect) {
70047002
*insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
70057003
} else {
70067004
*insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
70077005
if (orig->imm)
70087006
*insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
70097007
}
7008+
/* We're guaranteed here that CTX is in R6. */
7009+
*insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
70107010

70117011
switch (BPF_SIZE(orig->code)) {
70127012
case BPF_B:

0 commit comments

Comments
 (0)