Skip to content

Commit 2945434

Browse files
liu-song-6Alexei Starovoitov
authored andcommitted
selftests/bpf: Add tests for BPF_NEG range tracking logic
BPF_REG now has range tracking logic. Add selftests for BPF_NEG. Specifically, return value of LSM hook lsm.s/socket_connect is used to show that the verifer tracks BPF_NEG(1) falls in the [-4095, 0] range; while BPF_NEG(100000) does not fall in that range. Signed-off-by: Song Liu <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent aced132 commit 2945434

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,74 @@ __naked void bpf_cond_op_not_r10(void)
231231
::: __clobber_all);
232232
}
233233

234+
SEC("lsm.s/socket_connect")
235+
__success __log_level(2)
236+
__msg("0: (b7) r0 = 1 ; R0_w=1")
237+
__msg("1: (84) w0 = -w0 ; R0_w=0xffffffff")
238+
__msg("mark_precise: frame0: last_idx 2 first_idx 0 subseq_idx -1")
239+
__msg("mark_precise: frame0: regs=r0 stack= before 1: (84) w0 = -w0")
240+
__msg("mark_precise: frame0: regs=r0 stack= before 0: (b7) r0 = 1")
241+
__naked int bpf_neg_2(void)
242+
{
243+
/*
244+
* lsm.s/socket_connect requires a return value within [-4095, 0].
245+
* Returning -1 is allowed
246+
*/
247+
asm volatile (
248+
"r0 = 1;"
249+
"w0 = -w0;"
250+
"exit;"
251+
::: __clobber_all);
252+
}
253+
254+
SEC("lsm.s/socket_connect")
255+
__failure __msg("At program exit the register R0 has")
256+
__naked int bpf_neg_3(void)
257+
{
258+
/*
259+
* lsm.s/socket_connect requires a return value within [-4095, 0].
260+
* Returning -10000 is not allowed.
261+
*/
262+
asm volatile (
263+
"r0 = 10000;"
264+
"w0 = -w0;"
265+
"exit;"
266+
::: __clobber_all);
267+
}
268+
269+
SEC("lsm.s/socket_connect")
270+
__success __log_level(2)
271+
__msg("0: (b7) r0 = 1 ; R0_w=1")
272+
__msg("1: (87) r0 = -r0 ; R0_w=-1")
273+
__msg("mark_precise: frame0: last_idx 2 first_idx 0 subseq_idx -1")
274+
__msg("mark_precise: frame0: regs=r0 stack= before 1: (87) r0 = -r0")
275+
__msg("mark_precise: frame0: regs=r0 stack= before 0: (b7) r0 = 1")
276+
__naked int bpf_neg_4(void)
277+
{
278+
/*
279+
* lsm.s/socket_connect requires a return value within [-4095, 0].
280+
* Returning -1 is allowed
281+
*/
282+
asm volatile (
283+
"r0 = 1;"
284+
"r0 = -r0;"
285+
"exit;"
286+
::: __clobber_all);
287+
}
288+
289+
SEC("lsm.s/socket_connect")
290+
__failure __msg("At program exit the register R0 has")
291+
__naked int bpf_neg_5(void)
292+
{
293+
/*
294+
* lsm.s/socket_connect requires a return value within [-4095, 0].
295+
* Returning -10000 is not allowed.
296+
*/
297+
asm volatile (
298+
"r0 = 10000;"
299+
"r0 = -r0;"
300+
"exit;"
301+
::: __clobber_all);
302+
}
303+
234304
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)