1 // SPDX-License-Identifier: GPL-2.0-only
2 #define _GNU_SOURCE
3 #include <sched.h>
4 #include <sys/prctl.h>
5 #include <test_progs.h>
6 
7 #define MAX_TRAMP_PROGS 38
8 
9 struct inst {
10 	struct bpf_object *obj;
11 	struct bpf_link   *link_fentry;
12 	struct bpf_link   *link_fexit;
13 };
14 
15 static int test_task_rename(void)
16 {
17 	int fd, duration = 0, err;
18 	char buf[] = "test_overhead";
19 
20 	fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
21 	if (CHECK(fd < 0, "open /proc", "err %d", errno))
22 		return -1;
23 	err = write(fd, buf, sizeof(buf));
24 	if (err < 0) {
25 		CHECK(err < 0, "task rename", "err %d", errno);
26 		close(fd);
27 		return -1;
28 	}
29 	close(fd);
30 	return 0;
31 }
32 
33 static struct bpf_link *load(struct bpf_object *obj, const char *name)
34 {
35 	struct bpf_program *prog;
36 	int duration = 0;
37 
38 	prog = bpf_object__find_program_by_title(obj, name);
39 	if (CHECK(!prog, "find_probe", "prog '%s' not found\n", name))
40 		return ERR_PTR(-EINVAL);
41 	return bpf_program__attach_trace(prog);
42 }
43 
44 void test_trampoline_count(void)
45 {
46 	const char *fentry_name = "fentry/__set_task_comm";
47 	const char *fexit_name = "fexit/__set_task_comm";
48 	const char *object = "test_trampoline_count.o";
49 	struct inst inst[MAX_TRAMP_PROGS] = {};
50 	int err, i = 0, duration = 0;
51 	struct bpf_object *obj;
52 	struct bpf_link *link;
53 	char comm[16] = {};
54 
55 	/* attach 'allowed' trampoline programs */
56 	for (i = 0; i < MAX_TRAMP_PROGS; i++) {
57 		obj = bpf_object__open_file(object, NULL);
58 		if (!ASSERT_OK_PTR(obj, "obj_open_file")) {
59 			obj = NULL;
60 			goto cleanup;
61 		}
62 
63 		err = bpf_object__load(obj);
64 		if (CHECK(err, "obj_load", "err %d\n", err))
65 			goto cleanup;
66 		inst[i].obj = obj;
67 		obj = NULL;
68 
69 		if (rand() % 2) {
70 			link = load(inst[i].obj, fentry_name);
71 			if (!ASSERT_OK_PTR(link, "attach_prog")) {
72 				link = NULL;
73 				goto cleanup;
74 			}
75 			inst[i].link_fentry = link;
76 		} else {
77 			link = load(inst[i].obj, fexit_name);
78 			if (!ASSERT_OK_PTR(link, "attach_prog")) {
79 				link = NULL;
80 				goto cleanup;
81 			}
82 			inst[i].link_fexit = link;
83 		}
84 	}
85 
86 	/* and try 1 extra.. */
87 	obj = bpf_object__open_file(object, NULL);
88 	if (!ASSERT_OK_PTR(obj, "obj_open_file")) {
89 		obj = NULL;
90 		goto cleanup;
91 	}
92 
93 	err = bpf_object__load(obj);
94 	if (CHECK(err, "obj_load", "err %d\n", err))
95 		goto cleanup_extra;
96 
97 	/* ..that needs to fail */
98 	link = load(obj, fentry_name);
99 	err = libbpf_get_error(link);
100 	if (!ASSERT_ERR_PTR(link, "cannot attach over the limit")) {
101 		bpf_link__destroy(link);
102 		goto cleanup_extra;
103 	}
104 
105 	/* with E2BIG error */
106 	ASSERT_EQ(err, -E2BIG, "proper error check");
107 	ASSERT_EQ(link, NULL, "ptr_is_null");
108 
109 	/* and finaly execute the probe */
110 	if (CHECK_FAIL(prctl(PR_GET_NAME, comm, 0L, 0L, 0L)))
111 		goto cleanup_extra;
112 	CHECK_FAIL(test_task_rename());
113 	CHECK_FAIL(prctl(PR_SET_NAME, comm, 0L, 0L, 0L));
114 
115 cleanup_extra:
116 	bpf_object__close(obj);
117 cleanup:
118 	if (i >= MAX_TRAMP_PROGS)
119 		i = MAX_TRAMP_PROGS - 1;
120 	for (; i >= 0; i--) {
121 		bpf_link__destroy(inst[i].link_fentry);
122 		bpf_link__destroy(inst[i].link_fexit);
123 		bpf_object__close(inst[i].obj);
124 	}
125 }
126