Skip to content

Commit 229394e

Browse files
vitkyrkadavem330
authored andcommitted
net: bpf: reject invalid shifts
On ARM64, a BUG() is triggered in the eBPF JIT if a filter with a constant shift that can't be encoded in the immediate field of the UBFM/SBFM instructions is passed to the JIT. Since these shifts amounts, which are negative or >= regsize, are invalid, reject them in the eBPF verifier and the classic BPF filter checker, for all architectures. Signed-off-by: Rabin Vincent <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 7aaed57 commit 229394e

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

kernel/bpf/verifier.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,16 @@ static int check_alu_op(struct verifier_env *env, struct bpf_insn *insn)
11211121
return -EINVAL;
11221122
}
11231123

1124+
if ((opcode == BPF_LSH || opcode == BPF_RSH ||
1125+
opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
1126+
int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
1127+
1128+
if (insn->imm < 0 || insn->imm >= size) {
1129+
verbose("invalid shift %d\n", insn->imm);
1130+
return -EINVAL;
1131+
}
1132+
}
1133+
11241134
/* pattern match 'bpf_add Rx, imm' instruction */
11251135
if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
11261136
regs[insn->dst_reg].type == FRAME_PTR &&

net/core/filter.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,11 @@ static int bpf_check_classic(const struct sock_filter *filter,
785785
if (ftest->k == 0)
786786
return -EINVAL;
787787
break;
788+
case BPF_ALU | BPF_LSH | BPF_K:
789+
case BPF_ALU | BPF_RSH | BPF_K:
790+
if (ftest->k >= 32)
791+
return -EINVAL;
792+
break;
788793
case BPF_LD | BPF_MEM:
789794
case BPF_LDX | BPF_MEM:
790795
case BPF_ST:

0 commit comments

Comments
 (0)