Skip to content

Commit 958465e

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
selftests/bpf: tests for iterating callbacks
A set of test cases to check behavior of callback handling logic, check if verifier catches the following situations: - program not safe on second callback iteration; - program not safe on zero callback iterations; - infinite loop inside a callback. Verify that callback logic works for bpf_loop, bpf_for_each_map_elem, bpf_user_ringbuf_drain, bpf_find_vma. Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent ab5cfac commit 958465e

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

tools/testing/selftests/bpf/prog_tests/verifier.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "verifier_helper_restricted.skel.h"
3232
#include "verifier_helper_value_access.skel.h"
3333
#include "verifier_int_ptr.skel.h"
34+
#include "verifier_iterating_callbacks.skel.h"
3435
#include "verifier_jeq_infer_not_null.skel.h"
3536
#include "verifier_ld_ind.skel.h"
3637
#include "verifier_ldsx.skel.h"
@@ -139,6 +140,7 @@ void test_verifier_helper_packet_access(void) { RUN(verifier_helper_packet_acces
139140
void test_verifier_helper_restricted(void) { RUN(verifier_helper_restricted); }
140141
void test_verifier_helper_value_access(void) { RUN(verifier_helper_value_access); }
141142
void test_verifier_int_ptr(void) { RUN(verifier_int_ptr); }
143+
void test_verifier_iterating_callbacks(void) { RUN(verifier_iterating_callbacks); }
142144
void test_verifier_jeq_infer_not_null(void) { RUN(verifier_jeq_infer_not_null); }
143145
void test_verifier_ld_ind(void) { RUN(verifier_ld_ind); }
144146
void test_verifier_ldsx(void) { RUN(verifier_ldsx); }
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/bpf.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
6+
7+
struct {
8+
__uint(type, BPF_MAP_TYPE_ARRAY);
9+
__uint(max_entries, 8);
10+
__type(key, __u32);
11+
__type(value, __u64);
12+
} map SEC(".maps");
13+
14+
struct {
15+
__uint(type, BPF_MAP_TYPE_USER_RINGBUF);
16+
__uint(max_entries, 8);
17+
} ringbuf SEC(".maps");
18+
19+
struct vm_area_struct;
20+
struct bpf_map;
21+
22+
struct buf_context {
23+
char *buf;
24+
};
25+
26+
struct num_context {
27+
__u64 i;
28+
};
29+
30+
__u8 choice_arr[2] = { 0, 1 };
31+
32+
static int unsafe_on_2nd_iter_cb(__u32 idx, struct buf_context *ctx)
33+
{
34+
if (idx == 0) {
35+
ctx->buf = (char *)(0xDEAD);
36+
return 0;
37+
}
38+
39+
if (bpf_probe_read_user(ctx->buf, 8, (void *)(0xBADC0FFEE)))
40+
return 1;
41+
42+
return 0;
43+
}
44+
45+
SEC("?raw_tp")
46+
__failure __msg("R1 type=scalar expected=fp")
47+
int unsafe_on_2nd_iter(void *unused)
48+
{
49+
char buf[4];
50+
struct buf_context loop_ctx = { .buf = buf };
51+
52+
bpf_loop(100, unsafe_on_2nd_iter_cb, &loop_ctx, 0);
53+
return 0;
54+
}
55+
56+
static int unsafe_on_zero_iter_cb(__u32 idx, struct num_context *ctx)
57+
{
58+
ctx->i = 0;
59+
return 0;
60+
}
61+
62+
SEC("?raw_tp")
63+
__failure __msg("invalid access to map value, value_size=2 off=32 size=1")
64+
int unsafe_on_zero_iter(void *unused)
65+
{
66+
struct num_context loop_ctx = { .i = 32 };
67+
68+
bpf_loop(100, unsafe_on_zero_iter_cb, &loop_ctx, 0);
69+
return choice_arr[loop_ctx.i];
70+
}
71+
72+
static int loop_detection_cb(__u32 idx, struct num_context *ctx)
73+
{
74+
for (;;) {}
75+
return 0;
76+
}
77+
78+
SEC("?raw_tp")
79+
__failure __msg("infinite loop detected")
80+
int loop_detection(void *unused)
81+
{
82+
struct num_context loop_ctx = { .i = 0 };
83+
84+
bpf_loop(100, loop_detection_cb, &loop_ctx, 0);
85+
return 0;
86+
}
87+
88+
static __always_inline __u64 oob_state_machine(struct num_context *ctx)
89+
{
90+
switch (ctx->i) {
91+
case 0:
92+
ctx->i = 1;
93+
break;
94+
case 1:
95+
ctx->i = 32;
96+
break;
97+
}
98+
return 0;
99+
}
100+
101+
static __u64 for_each_map_elem_cb(struct bpf_map *map, __u32 *key, __u64 *val, void *data)
102+
{
103+
return oob_state_machine(data);
104+
}
105+
106+
SEC("?raw_tp")
107+
__failure __msg("invalid access to map value, value_size=2 off=32 size=1")
108+
int unsafe_for_each_map_elem(void *unused)
109+
{
110+
struct num_context loop_ctx = { .i = 0 };
111+
112+
bpf_for_each_map_elem(&map, for_each_map_elem_cb, &loop_ctx, 0);
113+
return choice_arr[loop_ctx.i];
114+
}
115+
116+
static __u64 ringbuf_drain_cb(struct bpf_dynptr *dynptr, void *data)
117+
{
118+
return oob_state_machine(data);
119+
}
120+
121+
SEC("?raw_tp")
122+
__failure __msg("invalid access to map value, value_size=2 off=32 size=1")
123+
int unsafe_ringbuf_drain(void *unused)
124+
{
125+
struct num_context loop_ctx = { .i = 0 };
126+
127+
bpf_user_ringbuf_drain(&ringbuf, ringbuf_drain_cb, &loop_ctx, 0);
128+
return choice_arr[loop_ctx.i];
129+
}
130+
131+
static __u64 find_vma_cb(struct task_struct *task, struct vm_area_struct *vma, void *data)
132+
{
133+
return oob_state_machine(data);
134+
}
135+
136+
SEC("?raw_tp")
137+
__failure __msg("invalid access to map value, value_size=2 off=32 size=1")
138+
int unsafe_find_vma(void *unused)
139+
{
140+
struct task_struct *task = bpf_get_current_task_btf();
141+
struct num_context loop_ctx = { .i = 0 };
142+
143+
bpf_find_vma(task, 0, find_vma_cb, &loop_ctx, 0);
144+
return choice_arr[loop_ctx.i];
145+
}
146+
147+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)