summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
blob: 2974b44f80faf17a16ab6a012dac07ad925a1f6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */
#define _GNU_SOURCE
#include <pthread.h>
#include <sched.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <unistd.h>
#include <test_progs.h>
#include <network_helpers.h>
#include <bpf/btf.h>
#include "test_bpf_cookie.skel.h"
#include "kprobe_multi.skel.h"

/* uprobe attach point */
static void trigger_func(void)
{
	asm volatile ("");
}

static void kprobe_subtest(struct test_bpf_cookie *skel)
{
	DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts);
	struct bpf_link *link1 = NULL, *link2 = NULL;
	struct bpf_link *retlink1 = NULL, *retlink2 = NULL;

	/* attach two kprobes */
	opts.bpf_cookie = 0x1;
	opts.retprobe = false;
	link1 = bpf_program__attach_kprobe_opts(skel->progs.handle_kprobe,
						 SYS_NANOSLEEP_KPROBE_NAME, &opts);
	if (!ASSERT_OK_PTR(link1, "link1"))
		goto cleanup;

	opts.bpf_cookie = 0x2;
	opts.retprobe = false;
	link2 = bpf_program__attach_kprobe_opts(skel->progs.handle_kprobe,
						 SYS_NANOSLEEP_KPROBE_NAME, &opts);
	if (!ASSERT_OK_PTR(link2, "link2"))
		goto cleanup;

	/* attach two kretprobes */
	opts.bpf_cookie = 0x10;
	opts.retprobe = true;
	retlink1 = bpf_program__attach_kprobe_opts(skel->progs.handle_kretprobe,
						    SYS_NANOSLEEP_KPROBE_NAME, &opts);
	if (!ASSERT_OK_PTR(retlink1, "retlink1"))
		goto cleanup;

	opts.bpf_cookie = 0x20;
	opts.retprobe = true;
	retlink2 = bpf_program__attach_kprobe_opts(skel->progs.handle_kretprobe,
						    SYS_NANOSLEEP_KPROBE_NAME, &opts);
	if (!ASSERT_OK_PTR(retlink2, "retlink2"))
		goto cleanup;

	/* trigger kprobe && kretprobe */
	usleep(1);

	ASSERT_EQ(skel->bss->kprobe_res, 0x1 | 0x2, "kprobe_res");
	ASSERT_EQ(skel->bss->kretprobe_res, 0x10 | 0x20, "kretprobe_res");

cleanup:
	bpf_link__destroy(link1);
	bpf_link__destroy(link2);
	bpf_link__destroy(retlink1);
	bpf_link__destroy(retlink2);
}

static void kprobe_multi_test_run(struct kprobe_multi *skel)
{
	LIBBPF_OPTS(bpf_test_run_opts, topts);
	int err, prog_fd;

	prog_fd = bpf_program__fd(skel->progs.trigger);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	ASSERT_OK(err, "test_run");
	ASSERT_EQ(topts.retval, 0, "test_run");

	ASSERT_EQ(skel->bss->kprobe_test1_result, 1, "kprobe_test1_result");
	ASSERT_EQ(skel->bss->kprobe_test2_result, 1, "kprobe_test2_result");
	ASSERT_EQ(skel->bss->kprobe_test3_result, 1, "kprobe_test3_result");
	ASSERT_EQ(skel->bss->kprobe_test4_result, 1, "kprobe_test4_result");
	ASSERT_EQ(skel->bss->kprobe_test5_result, 1, "kprobe_test5_result");
	ASSERT_EQ(skel->bss->kprobe_test6_result, 1, "kprobe_test6_result");
	ASSERT_EQ(skel->bss->kprobe_test7_result, 1, "kprobe_test7_result");
	ASSERT_EQ(skel->bss->kprobe_test8_result, 1, "kprobe_test8_result");

	ASSERT_EQ(skel->bss->kretprobe_test1_result, 1, "kretprobe_test1_result");
	ASSERT_EQ(skel->bss->kretprobe_test2_result, 1, "kretprobe_test2_result");
	ASSERT_EQ(skel->bss->kretprobe_test3_result, 1, "kretprobe_test3_result");
	ASSERT_EQ(skel->bss->kretprobe_test4_result, 1, "kretprobe_test4_result");
	ASSERT_EQ(skel->bss->kretprobe_test5_result, 1, "kretprobe_test5_result");
	ASSERT_EQ(skel->bss->kretprobe_test6_result, 1, "kretprobe_test6_result");
	ASSERT_EQ(skel->bss->kretprobe_test7_result, 1, "kretprobe_test7_result");
	ASSERT_EQ(skel->bss->kretprobe_test8_result, 1, "kretprobe_test8_result");
}

static void kprobe_multi_link_api_subtest(void)
{
	int prog_fd, link1_fd = -1, link2_fd = -1;
	struct kprobe_multi *skel = NULL;
	LIBBPF_OPTS(bpf_link_create_opts, opts);
	unsigned long long addrs[8];
	__u64 cookies[8];

	if (!ASSERT_OK(load_kallsyms(), "load_kallsyms"))
		goto cleanup;

	skel = kprobe_multi__open_and_load();
	if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load"))
		goto cleanup;

	skel->bss->pid = getpid();
	skel->bss->test_cookie = true;

#define GET_ADDR(__sym, __addr) ({				\
	__addr = ksym_get_addr(__sym);				\
	if (!ASSERT_NEQ(__addr, 0, "ksym_get_addr " #__sym))	\
		goto cleanup;					\
})

	GET_ADDR("bpf_fentry_test1", addrs[0]);
	GET_ADDR("bpf_fentry_test3", addrs[1]);
	GET_ADDR("bpf_fentry_test4", addrs[2]);
	GET_ADDR("bpf_fentry_test5", addrs[3]);
	GET_ADDR("bpf_fentry_test6", addrs[4]);
	GET_ADDR("bpf_fentry_test7", addrs[5]);
	GET_ADDR("bpf_fentry_test2", addrs[6]);
	GET_ADDR("bpf_fentry_test8", addrs[7]);

#undef GET_ADDR

	cookies[0] = 1; /* bpf_fentry_test1 */
	cookies[1] = 2; /* bpf_fentry_test3 */
	cookies[2] = 3; /* bpf_fentry_test4 */
	cookies[3] = 4; /* bpf_fentry_test5 */
	cookies[4] = 5; /* bpf_fentry_test6 */
	cookies[5] = 6; /* bpf_fentry_test7 */
	cookies[6] = 7; /* bpf_fentry_test2 */
	cookies[7] = 8; /* bpf_fentry_test8 */

	opts.kprobe_multi.addrs = (const unsigned long *) &addrs;
	opts.kprobe_multi.cnt = ARRAY_SIZE(addrs);
	opts.kprobe_multi.cookies = (const __u64 *) &cookies;
	prog_fd = bpf_program__fd(skel->progs.test_kprobe);

	link1_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_KPROBE_MULTI, &opts);
	if (!ASSERT_GE(link1_fd, 0, "link1_fd"))
		goto cleanup;

	cookies[0] = 8; /* bpf_fentry_test1 */
	cookies[1] = 7; /* bpf_fentry_test3 */
	cookies[2] = 6; /* bpf_fentry_test4 */
	cookies[3] = 5; /* bpf_fentry_test5 */
	cookies[4] = 4; /* bpf_fentry_test6 */
	cookies[5] = 3; /* bpf_fentry_test7 */
	cookies[6] = 2; /* bpf_fentry_test2 */
	cookies[7] = 1; /* bpf_fentry_test8 */

	opts.kprobe_multi.flags = BPF_F_KPROBE_MULTI_RETURN;
	prog_fd = bpf_program__fd(skel->progs.test_kretprobe);

	link2_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_KPROBE_MULTI, &opts);
	if (!ASSERT_GE(link2_fd, 0, "link2_fd"))
		goto cleanup;

	kprobe_multi_test_run(skel);

cleanup:
	close(link1_fd);
	close(link2_fd);
	kprobe_multi__destroy(skel);
}

static void kprobe_multi_attach_api_subtest(void)
{
	struct bpf_link *link1 = NULL, *link2 = NULL;
	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
	LIBBPF_OPTS(bpf_test_run_opts, topts);
	struct kprobe_multi *skel = NULL;
	const char *syms[8] = {
		"bpf_fentry_test1",
		"bpf_fentry_test3",
		"bpf_fentry_test4",
		"bpf_fentry_test5",
		"bpf_fentry_test6",
		"bpf_fentry_test7",
		"bpf_fentry_test2",
		"bpf_fentry_test8",
	};
	__u64 cookies[8];

	skel = kprobe_multi__open_and_load();
	if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load"))
		goto cleanup;

	skel->bss->pid = getpid();
	skel->bss->test_cookie = true;

	cookies[0] = 1; /* bpf_fentry_test1 */
	cookies[1] = 2; /* bpf_fentry_test3 */
	cookies[2] = 3; /* bpf_fentry_test4 */
	cookies[3] = 4; /* bpf_fentry_test5 */
	cookies[4] = 5; /* bpf_fentry_test6 */
	cookies[5] = 6; /* bpf_fentry_test7 */
	cookies[6] = 7; /* bpf_fentry_test2 */
	cookies[7] = 8; /* bpf_fentry_test8 */

	opts.syms = syms;
	opts.cnt = ARRAY_SIZE(syms);
	opts.cookies = cookies;

	link1 = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe,
						      NULL, &opts);
	if (!ASSERT_OK_PTR(link1, "bpf_program__attach_kprobe_multi_opts"))
		goto cleanup;

	cookies[0] = 8; /* bpf_fentry_test1 */
	cookies[1] = 7; /* bpf_fentry_test3 */
	cookies[2] = 6; /* bpf_fentry_test4 */
	cookies[3] = 5; /* bpf_fentry_test5 */
	cookies[4] = 4; /* bpf_fentry_test6 */
	cookies[5] = 3; /* bpf_fentry_test7 */
	cookies[6] = 2; /* bpf_fentry_test2 */
	cookies[7] = 1; /* bpf_fentry_test8 */

	opts.retprobe = true;

	link2 = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kretprobe,
						      NULL, &opts);
	if (!ASSERT_OK_PTR(link2, "bpf_program__attach_kprobe_multi_opts"))
		goto cleanup;

	kprobe_multi_test_run(skel);

cleanup:
	bpf_link__destroy(link2);
	bpf_link__destroy(link1);
	kprobe_multi__destroy(skel);
}
static void uprobe_subtest(struct test_bpf_cookie *skel)
{
	DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
	struct bpf_link *link1 = NULL, *link2 = NULL;
	struct bpf_link *retlink1 = NULL, *retlink2 = NULL;
	ssize_t uprobe_offset;

	uprobe_offset = get_uprobe_offset(&trigger_func);
	if (!ASSERT_GE(uprobe_offset, 0, "uprobe_offset"))
		goto cleanup;

	/* attach two uprobes */
	opts.bpf_cookie = 0x100;
	opts.retprobe = false;
	link1 = bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe, 0 /* self pid */,
						"/proc/self/exe", uprobe_offset, &opts);
	if (!ASSERT_OK_PTR(link1, "link1"))
		goto cleanup;

	opts.bpf_cookie = 0x200;
	opts.retprobe = false;
	link2 = bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe, -1 /* any pid */,
						"/proc/self/exe", uprobe_offset, &opts);
	if (!ASSERT_OK_PTR(link2, "link2"))
		goto cleanup;

	/* attach two uretprobes */
	opts.bpf_cookie = 0x1000;
	opts.retprobe = true;
	retlink1 = bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe, -1 /* any pid */,
						   "/proc/self/exe", uprobe_offset, &opts);
	if (!ASSERT_OK_PTR(retlink1, "retlink1"))
		goto cleanup;

	opts.bpf_cookie = 0x2000;
	opts.retprobe = true;
	retlink2 = bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe, 0 /* self pid */,
						   "/proc/self/exe", uprobe_offset, &opts);
	if (!ASSERT_OK_PTR(retlink2, "retlink2"))
		goto cleanup;

	/* trigger uprobe && uretprobe */
	trigger_func();

	ASSERT_EQ(skel->bss->uprobe_res, 0x100 | 0x200, "uprobe_res");
	ASSERT_EQ(skel->bss->uretprobe_res, 0x1000 | 0x2000, "uretprobe_res");

cleanup:
	bpf_link__destroy(link1);
	bpf_link__destroy(link2);
	bpf_link__destroy(retlink1);
	bpf_link__destroy(retlink2);
}

static void tp_subtest(struct test_bpf_cookie *skel)
{
	DECLARE_LIBBPF_OPTS(bpf_tracepoint_opts, opts);
	struct bpf_link *link1 = NULL, *link2 = NULL, *link3 = NULL;

	/* attach first tp prog */
	opts.bpf_cookie = 0x10000;
	link1 = bpf_program__attach_tracepoint_opts(skel->progs.handle_tp1,
						    "syscalls", "sys_enter_nanosleep", &opts);
	if (!ASSERT_OK_PTR(link1, "link1"))
		goto cleanup;

	/* attach second tp prog */
	opts.bpf_cookie = 0x20000;
	link2 = bpf_program__attach_tracepoint_opts(skel->progs.handle_tp2,
						    "syscalls", "sys_enter_nanosleep", &opts);
	if (!ASSERT_OK_PTR(link2, "link2"))
		goto cleanup;

	/* trigger tracepoints */
	usleep(1);

	ASSERT_EQ(skel->bss->tp_res, 0x10000 | 0x20000, "tp_res1");

	/* now we detach first prog and will attach third one, which causes
	 * two internal calls to bpf_prog_array_copy(), shuffling
	 * bpf_prog_array_items around. We test here that we don't lose track
	 * of associated bpf_cookies.
	 */
	bpf_link__destroy(link1);
	link1 = NULL;
	kern_sync_rcu();
	skel->bss->tp_res = 0;

	/* attach third tp prog */
	opts.bpf_cookie = 0x40000;
	link3 = bpf_program__attach_tracepoint_opts(skel->progs.handle_tp3,
						    "syscalls", "sys_enter_nanosleep", &opts);
	if (!ASSERT_OK_PTR(link3, "link3"))
		goto cleanup;

	/* trigger tracepoints */
	usleep(1);

	ASSERT_EQ(skel->bss->tp_res, 0x20000 | 0x40000, "tp_res2");

cleanup:
	bpf_link__destroy(link1);
	bpf_link__destroy(link2);
	bpf_link__destroy(link3);
}

static void burn_cpu(void)
{
	volatile int j = 0;
	cpu_set_t cpu_set;
	int i, err;

	/* generate some branches on cpu 0 */
	CPU_ZERO(&cpu_set);
	CPU_SET(0, &cpu_set);
	err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set);
	ASSERT_OK(err, "set_thread_affinity");

	/* spin the loop for a while (random high number) */
	for (i = 0; i < 1000000; ++i)
		++j;
}

static void pe_subtest(struct test_bpf_cookie *skel)
{
	DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, opts);
	struct bpf_link *link = NULL;
	struct perf_event_attr attr;
	int pfd = -1;

	/* create perf event */
	memset(&attr, 0, sizeof(attr));
	attr.size = sizeof(attr);
	attr.type = PERF_TYPE_SOFTWARE;
	attr.config = PERF_COUNT_SW_CPU_CLOCK;
	attr.freq = 1;
	attr.sample_freq = 1000;
	pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
	if (!ASSERT_GE(pfd, 0, "perf_fd"))
		goto cleanup;

	opts.bpf_cookie = 0x100000;
	link = bpf_program__attach_perf_event_opts(skel->progs.handle_pe, pfd, &opts);
	if (!ASSERT_OK_PTR(link, "link1"))
		goto cleanup;

	burn_cpu(); /* trigger BPF prog */

	ASSERT_EQ(skel->bss->pe_res, 0x100000, "pe_res1");

	/* prevent bpf_link__destroy() closing pfd itself */
	bpf_link__disconnect(link);
	/* close BPF link's FD explicitly */
	close(bpf_link__fd(link));
	/* free up memory used by struct bpf_link */
	bpf_link__destroy(link);
	link = NULL;
	kern_sync_rcu();
	skel->bss->pe_res = 0;

	opts.bpf_cookie = 0x200000;
	link = bpf_program__attach_perf_event_opts(skel->progs.handle_pe, pfd, &opts);
	if (!ASSERT_OK_PTR(link, "link2"))
		goto cleanup;

	burn_cpu(); /* trigger BPF prog */

	ASSERT_EQ(skel->bss->pe_res, 0x200000, "pe_res2");

cleanup:
	close(pfd);
	bpf_link__destroy(link);
}

static void tracing_subtest(struct test_bpf_cookie *skel)
{
	__u64 cookie;
	int prog_fd;
	int fentry_fd = -1, fexit_fd = -1, fmod_ret_fd = -1;
	LIBBPF_OPTS(bpf_test_run_opts, opts);
	LIBBPF_OPTS(bpf_link_create_opts, link_opts);

	skel->bss->fentry_res = 0;
	skel->bss->fexit_res = 0;

	cookie = 0x10000000000000L;
	prog_fd = bpf_program__fd(skel->progs.fentry_test1);
	link_opts.tracing.cookie = cookie;
	fentry_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_FENTRY, &link_opts);
	if (!ASSERT_GE(fentry_fd, 0, "fentry.link_create"))
		goto cleanup;

	cookie = 0x20000000000000L;
	prog_fd = bpf_program__fd(skel->progs.fexit_test1);
	link_opts.tracing.cookie = cookie;
	fexit_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_FEXIT, &link_opts);
	if (!ASSERT_GE(fexit_fd, 0, "fexit.link_create"))
		goto cleanup;

	cookie = 0x30000000000000L;
	prog_fd = bpf_program__fd(skel->progs.fmod_ret_test);
	link_opts.tracing.cookie = cookie;
	fmod_ret_fd = bpf_link_create(prog_fd, 0, BPF_MODIFY_RETURN, &link_opts);
	if (!ASSERT_GE(fmod_ret_fd, 0, "fmod_ret.link_create"))
		goto cleanup;

	prog_fd = bpf_program__fd(skel->progs.fentry_test1);
	bpf_prog_test_run_opts(prog_fd, &opts);

	prog_fd = bpf_program__fd(skel->progs.fmod_ret_test);
	bpf_prog_test_run_opts(prog_fd, &opts);

	ASSERT_EQ(skel->bss->fentry_res, 0x10000000000000L, "fentry_res");
	ASSERT_EQ(skel->bss->fexit_res, 0x20000000000000L, "fexit_res");
	ASSERT_EQ(skel->bss->fmod_ret_res, 0x30000000000000L, "fmod_ret_res");

cleanup:
	if (fentry_fd >= 0)
		close(fentry_fd);
	if (fexit_fd >= 0)
		close(fexit_fd);
	if (fmod_ret_fd >= 0)
		close(fmod_ret_fd);
}

int stack_mprotect(void);

static void lsm_subtest(struct test_bpf_cookie *skel)
{
	__u64 cookie;
	int prog_fd;
	int lsm_fd = -1;
	LIBBPF_OPTS(bpf_link_create_opts, link_opts);

	skel->bss->lsm_res = 0;

	cookie = 0x90000000000090L;
	prog_fd = bpf_program__fd(skel->progs.test_int_hook);
	link_opts.tracing.cookie = cookie;
	lsm_fd = bpf_link_create(prog_fd, 0, BPF_LSM_MAC, &link_opts);
	if (!ASSERT_GE(lsm_fd, 0, "lsm.link_create"))
		goto cleanup;

	stack_mprotect();
	if (!ASSERT_EQ(errno, EPERM, "stack_mprotect"))
		goto cleanup;

	usleep(1);

	ASSERT_EQ(skel->bss->lsm_res, 0x90000000000090L, "fentry_res");

cleanup:
	if (lsm_fd >= 0)
		close(lsm_fd);
}

void test_bpf_cookie(void)
{
	struct test_bpf_cookie *skel;

	skel = test_bpf_cookie__open_and_load();
	if (!ASSERT_OK_PTR(skel, "skel_open"))
		return;

	skel->bss->my_tid = syscall(SYS_gettid);

	if (test__start_subtest("kprobe"))
		kprobe_subtest(skel);
	if (test__start_subtest("multi_kprobe_link_api"))
		kprobe_multi_link_api_subtest();
	if (test__start_subtest("multi_kprobe_attach_api"))
		kprobe_multi_attach_api_subtest();
	if (test__start_subtest("uprobe"))
		uprobe_subtest(skel);
	if (test__start_subtest("tracepoint"))
		tp_subtest(skel);
	if (test__start_subtest("perf_event"))
		pe_subtest(skel);
	if (test__start_subtest("trampoline"))
		tracing_subtest(skel);
	if (test__start_subtest("lsm"))
		lsm_subtest(skel);

	test_bpf_cookie__destroy(skel);
}