Skip to content

Commit 989410c

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftests/bpf: add tests confirming type logic in kernel for __arg_ctx
Add a bunch of global subprogs across variety of program types to validate expected kernel type enforcement logic for __arg_ctx arguments. Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 0ba9715 commit 989410c

File tree

1 file changed

+161
-3
lines changed

1 file changed

+161
-3
lines changed

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

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

44
#include <vmlinux.h>
55
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
67
#include "bpf_misc.h"
78
#include "xdp_metadata.h"
89
#include "bpf_kfuncs.h"
@@ -138,25 +139,182 @@ __weak int subprog_ctx_tag(void *ctx __arg_ctx)
138139
return bpf_get_stack(ctx, stack, sizeof(stack), 0);
139140
}
140141

142+
__weak int raw_tp_canonical(struct bpf_raw_tracepoint_args *ctx __arg_ctx)
143+
{
144+
return 0;
145+
}
146+
147+
__weak int raw_tp_u64_array(u64 *ctx __arg_ctx)
148+
{
149+
return 0;
150+
}
151+
141152
SEC("?raw_tp")
142153
__success __log_level(2)
143154
int arg_tag_ctx_raw_tp(void *ctx)
144155
{
145-
return subprog_ctx_tag(ctx);
156+
return subprog_ctx_tag(ctx) + raw_tp_canonical(ctx) + raw_tp_u64_array(ctx);
157+
}
158+
159+
SEC("?raw_tp.w")
160+
__success __log_level(2)
161+
int arg_tag_ctx_raw_tp_writable(void *ctx)
162+
{
163+
return subprog_ctx_tag(ctx) + raw_tp_canonical(ctx) + raw_tp_u64_array(ctx);
164+
}
165+
166+
SEC("?tp_btf/sys_enter")
167+
__success __log_level(2)
168+
int arg_tag_ctx_raw_tp_btf(void *ctx)
169+
{
170+
return subprog_ctx_tag(ctx) + raw_tp_canonical(ctx) + raw_tp_u64_array(ctx);
171+
}
172+
173+
struct whatever { };
174+
175+
__weak int tp_whatever(struct whatever *ctx __arg_ctx)
176+
{
177+
return 0;
146178
}
147179

148180
SEC("?tp")
149181
__success __log_level(2)
150182
int arg_tag_ctx_tp(void *ctx)
151183
{
152-
return subprog_ctx_tag(ctx);
184+
return subprog_ctx_tag(ctx) + tp_whatever(ctx);
185+
}
186+
187+
__weak int kprobe_subprog_pt_regs(struct pt_regs *ctx __arg_ctx)
188+
{
189+
return 0;
190+
}
191+
192+
__weak int kprobe_subprog_typedef(bpf_user_pt_regs_t *ctx __arg_ctx)
193+
{
194+
return 0;
153195
}
154196

155197
SEC("?kprobe")
156198
__success __log_level(2)
157199
int arg_tag_ctx_kprobe(void *ctx)
158200
{
159-
return subprog_ctx_tag(ctx);
201+
return subprog_ctx_tag(ctx) +
202+
kprobe_subprog_pt_regs(ctx) +
203+
kprobe_subprog_typedef(ctx);
204+
}
205+
206+
__weak int perf_subprog_regs(
207+
#if defined(bpf_target_riscv)
208+
struct user_regs_struct *ctx __arg_ctx
209+
#elif defined(bpf_target_s390)
210+
/* user_pt_regs typedef is anonymous struct, so only `void *` works */
211+
void *ctx __arg_ctx
212+
#elif defined(bpf_target_loongarch) || defined(bpf_target_arm64) || defined(bpf_target_powerpc)
213+
struct user_pt_regs *ctx __arg_ctx
214+
#else
215+
struct pt_regs *ctx __arg_ctx
216+
#endif
217+
)
218+
{
219+
return 0;
220+
}
221+
222+
__weak int perf_subprog_typedef(bpf_user_pt_regs_t *ctx __arg_ctx)
223+
{
224+
return 0;
225+
}
226+
227+
__weak int perf_subprog_canonical(struct bpf_perf_event_data *ctx __arg_ctx)
228+
{
229+
return 0;
230+
}
231+
232+
SEC("?perf_event")
233+
__success __log_level(2)
234+
int arg_tag_ctx_perf(void *ctx)
235+
{
236+
return subprog_ctx_tag(ctx) +
237+
perf_subprog_regs(ctx) +
238+
perf_subprog_typedef(ctx) +
239+
perf_subprog_canonical(ctx);
240+
}
241+
242+
__weak int iter_subprog_void(void *ctx __arg_ctx)
243+
{
244+
return 0;
245+
}
246+
247+
__weak int iter_subprog_typed(struct bpf_iter__task *ctx __arg_ctx)
248+
{
249+
return 0;
250+
}
251+
252+
SEC("?iter/task")
253+
__success __log_level(2)
254+
int arg_tag_ctx_iter_task(struct bpf_iter__task *ctx)
255+
{
256+
return (iter_subprog_void(ctx) + iter_subprog_typed(ctx)) & 1;
257+
}
258+
259+
__weak int tracing_subprog_void(void *ctx __arg_ctx)
260+
{
261+
return 0;
262+
}
263+
264+
__weak int tracing_subprog_u64(u64 *ctx __arg_ctx)
265+
{
266+
return 0;
267+
}
268+
269+
int acc;
270+
271+
SEC("?fentry/" SYS_PREFIX "sys_nanosleep")
272+
__success __log_level(2)
273+
int BPF_PROG(arg_tag_ctx_fentry)
274+
{
275+
acc += tracing_subprog_void(ctx) + tracing_subprog_u64(ctx);
276+
return 0;
277+
}
278+
279+
SEC("?fexit/" SYS_PREFIX "sys_nanosleep")
280+
__success __log_level(2)
281+
int BPF_PROG(arg_tag_ctx_fexit)
282+
{
283+
acc += tracing_subprog_void(ctx) + tracing_subprog_u64(ctx);
284+
return 0;
285+
}
286+
287+
SEC("?fmod_ret/" SYS_PREFIX "sys_nanosleep")
288+
__success __log_level(2)
289+
int BPF_PROG(arg_tag_ctx_fmod_ret)
290+
{
291+
return tracing_subprog_void(ctx) + tracing_subprog_u64(ctx);
292+
}
293+
294+
SEC("?lsm/bpf")
295+
__success __log_level(2)
296+
int BPF_PROG(arg_tag_ctx_lsm)
297+
{
298+
return tracing_subprog_void(ctx) + tracing_subprog_u64(ctx);
299+
}
300+
301+
SEC("?struct_ops/test_1")
302+
__success __log_level(2)
303+
int BPF_PROG(arg_tag_ctx_struct_ops)
304+
{
305+
return tracing_subprog_void(ctx) + tracing_subprog_u64(ctx);
306+
}
307+
308+
SEC(".struct_ops")
309+
struct bpf_dummy_ops dummy_1 = {
310+
.test_1 = (void *)arg_tag_ctx_struct_ops,
311+
};
312+
313+
SEC("?syscall")
314+
__success __log_level(2)
315+
int arg_tag_ctx_syscall(void *ctx)
316+
{
317+
return tracing_subprog_void(ctx) + tracing_subprog_u64(ctx) + tp_whatever(ctx);
160318
}
161319

162320
__weak int subprog_dynptr(struct bpf_dynptr *dptr)

0 commit comments

Comments
 (0)