Skip to content

Commit 2ce61c6

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
selftests/bpf: Add tests for rejection of ALU ops with negative offsets
Define a simple program using MOD, DIV, ADD instructions, make sure that the program is rejected if invalid offset field is used for instruction. These are test cases for commit 55c0ced ("bpf: Reject negative offsets for ALU ops") Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Eduard Zingerman <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent f09f57c commit 2ce61c6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <linux/bpf.h>
55
#include <bpf/bpf_helpers.h>
6+
#include "../../../include/linux/filter.h"
67
#include "bpf_misc.h"
78

89
#define MAX_ENTRIES 11
@@ -183,4 +184,32 @@ __naked void flow_keys_illegal_variable_offset_alu(void)
183184
: __clobber_all);
184185
}
185186

187+
#define DEFINE_BAD_OFFSET_TEST(name, op, off, imm) \
188+
SEC("socket") \
189+
__failure __msg("BPF_ALU uses reserved fields") \
190+
__naked void name(void) \
191+
{ \
192+
asm volatile( \
193+
"r0 = 1;" \
194+
".8byte %[insn];" \
195+
"r0 = 0;" \
196+
"exit;" \
197+
: \
198+
: __imm_insn(insn, BPF_RAW_INSN((op), 0, 0, (off), (imm))) \
199+
: __clobber_all); \
200+
}
201+
202+
/*
203+
* Offset fields of 0 and 1 are legal for BPF_{DIV,MOD} instructions.
204+
* Offset fields of 0 are legal for the rest of ALU instructions.
205+
* Test that error is reported for illegal offsets, assuming that tests
206+
* for legal offsets exist.
207+
*/
208+
DEFINE_BAD_OFFSET_TEST(bad_offset_divx, BPF_ALU64 | BPF_DIV | BPF_X, -1, 0)
209+
DEFINE_BAD_OFFSET_TEST(bad_offset_modk, BPF_ALU64 | BPF_MOD | BPF_K, -1, 1)
210+
DEFINE_BAD_OFFSET_TEST(bad_offset_addx, BPF_ALU64 | BPF_ADD | BPF_X, -1, 0)
211+
DEFINE_BAD_OFFSET_TEST(bad_offset_divx2, BPF_ALU64 | BPF_DIV | BPF_X, 2, 0)
212+
DEFINE_BAD_OFFSET_TEST(bad_offset_modk2, BPF_ALU64 | BPF_MOD | BPF_K, 2, 1)
213+
DEFINE_BAD_OFFSET_TEST(bad_offset_addx2, BPF_ALU64 | BPF_ADD | BPF_X, 1, 0)
214+
186215
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)