Skip to content

Commit 87c1793

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: add couple of test cases for div/mod by zero
Add couple of missing test cases for eBPF div/mod by zero to the new test_verifier prog runtime feature. Also one for an empty prog and only exit. Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent fcd1c91 commit 87c1793

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,93 @@ static struct bpf_test tests[] = {
101101
.result = ACCEPT,
102102
.retval = -3,
103103
},
104+
{
105+
"DIV32 by 0, zero check 1",
106+
.insns = {
107+
BPF_MOV32_IMM(BPF_REG_0, 42),
108+
BPF_MOV32_IMM(BPF_REG_1, 0),
109+
BPF_MOV32_IMM(BPF_REG_2, 1),
110+
BPF_ALU32_REG(BPF_DIV, BPF_REG_2, BPF_REG_1),
111+
BPF_EXIT_INSN(),
112+
},
113+
.result = ACCEPT,
114+
.retval = 0,
115+
},
116+
{
117+
"DIV32 by 0, zero check 2",
118+
.insns = {
119+
BPF_MOV32_IMM(BPF_REG_0, 42),
120+
BPF_LD_IMM64(BPF_REG_1, 0xffffffff00000000LL),
121+
BPF_MOV32_IMM(BPF_REG_2, 1),
122+
BPF_ALU32_REG(BPF_DIV, BPF_REG_2, BPF_REG_1),
123+
BPF_EXIT_INSN(),
124+
},
125+
.result = ACCEPT,
126+
.retval = 0,
127+
},
128+
{
129+
"DIV64 by 0, zero check",
130+
.insns = {
131+
BPF_MOV32_IMM(BPF_REG_0, 42),
132+
BPF_MOV32_IMM(BPF_REG_1, 0),
133+
BPF_MOV32_IMM(BPF_REG_2, 1),
134+
BPF_ALU64_REG(BPF_DIV, BPF_REG_2, BPF_REG_1),
135+
BPF_EXIT_INSN(),
136+
},
137+
.result = ACCEPT,
138+
.retval = 0,
139+
},
140+
{
141+
"MOD32 by 0, zero check 1",
142+
.insns = {
143+
BPF_MOV32_IMM(BPF_REG_0, 42),
144+
BPF_MOV32_IMM(BPF_REG_1, 0),
145+
BPF_MOV32_IMM(BPF_REG_2, 1),
146+
BPF_ALU32_REG(BPF_MOD, BPF_REG_2, BPF_REG_1),
147+
BPF_EXIT_INSN(),
148+
},
149+
.result = ACCEPT,
150+
.retval = 0,
151+
},
152+
{
153+
"MOD32 by 0, zero check 2",
154+
.insns = {
155+
BPF_MOV32_IMM(BPF_REG_0, 42),
156+
BPF_LD_IMM64(BPF_REG_1, 0xffffffff00000000LL),
157+
BPF_MOV32_IMM(BPF_REG_2, 1),
158+
BPF_ALU32_REG(BPF_MOD, BPF_REG_2, BPF_REG_1),
159+
BPF_EXIT_INSN(),
160+
},
161+
.result = ACCEPT,
162+
.retval = 0,
163+
},
164+
{
165+
"MOD64 by 0, zero check",
166+
.insns = {
167+
BPF_MOV32_IMM(BPF_REG_0, 42),
168+
BPF_MOV32_IMM(BPF_REG_1, 0),
169+
BPF_MOV32_IMM(BPF_REG_2, 1),
170+
BPF_ALU64_REG(BPF_MOD, BPF_REG_2, BPF_REG_1),
171+
BPF_EXIT_INSN(),
172+
},
173+
.result = ACCEPT,
174+
.retval = 0,
175+
},
176+
{
177+
"empty prog",
178+
.insns = {
179+
},
180+
.errstr = "last insn is not an exit or jmp",
181+
.result = REJECT,
182+
},
183+
{
184+
"only exit insn",
185+
.insns = {
186+
BPF_EXIT_INSN(),
187+
},
188+
.errstr = "R0 !read_ok",
189+
.result = REJECT,
190+
},
104191
{
105192
"unreachable",
106193
.insns = {

0 commit comments

Comments
 (0)