Skip to content

Commit 7891a87

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: arsh is not supported in 32 bit alu thus reject it
The following snippet was throwing an 'unknown opcode cc' warning in BPF interpreter: 0: (18) r0 = 0x0 2: (7b) *(u64 *)(r10 -16) = r0 3: (cc) (u32) r0 s>>= (u32) r0 4: (95) exit Although a number of JITs do support BPF_ALU | BPF_ARSH | BPF_{K,X} generation, not all of them do and interpreter does neither. We can leave existing ones and implement it later in bpf-next for the remaining ones, but reject this properly in verifier for the time being. Fixes: 17a5267 ("bpf: verifier (add verifier core)") Reported-by: [email protected] Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 4095034 commit 7891a87

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

kernel/bpf/verifier.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,11 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
24932493
return -EINVAL;
24942494
}
24952495

2496+
if (opcode == BPF_ARSH && BPF_CLASS(insn->code) != BPF_ALU64) {
2497+
verbose(env, "BPF_ARSH not supported for 32 bit ALU\n");
2498+
return -EINVAL;
2499+
}
2500+
24962501
if ((opcode == BPF_LSH || opcode == BPF_RSH ||
24972502
opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
24982503
int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,46 @@ static struct bpf_test tests[] = {
272272
.errstr = "invalid bpf_ld_imm64 insn",
273273
.result = REJECT,
274274
},
275+
{
276+
"arsh32 on imm",
277+
.insns = {
278+
BPF_MOV64_IMM(BPF_REG_0, 1),
279+
BPF_ALU32_IMM(BPF_ARSH, BPF_REG_0, 5),
280+
BPF_EXIT_INSN(),
281+
},
282+
.result = REJECT,
283+
.errstr = "BPF_ARSH not supported for 32 bit ALU",
284+
},
285+
{
286+
"arsh32 on reg",
287+
.insns = {
288+
BPF_MOV64_IMM(BPF_REG_0, 1),
289+
BPF_MOV64_IMM(BPF_REG_1, 5),
290+
BPF_ALU32_REG(BPF_ARSH, BPF_REG_0, BPF_REG_1),
291+
BPF_EXIT_INSN(),
292+
},
293+
.result = REJECT,
294+
.errstr = "BPF_ARSH not supported for 32 bit ALU",
295+
},
296+
{
297+
"arsh64 on imm",
298+
.insns = {
299+
BPF_MOV64_IMM(BPF_REG_0, 1),
300+
BPF_ALU64_IMM(BPF_ARSH, BPF_REG_0, 5),
301+
BPF_EXIT_INSN(),
302+
},
303+
.result = ACCEPT,
304+
},
305+
{
306+
"arsh64 on reg",
307+
.insns = {
308+
BPF_MOV64_IMM(BPF_REG_0, 1),
309+
BPF_MOV64_IMM(BPF_REG_1, 5),
310+
BPF_ALU64_REG(BPF_ARSH, BPF_REG_0, BPF_REG_1),
311+
BPF_EXIT_INSN(),
312+
},
313+
.result = ACCEPT,
314+
},
275315
{
276316
"no bpf_exit",
277317
.insns = {

0 commit comments

Comments
 (0)