1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #include <linux/btf.h>
4 #include <linux/btf_ids.h>
5 #include <linux/error-injection.h>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/percpu-defs.h>
9 #include <linux/sysfs.h>
10 #include <linux/tracepoint.h>
11 #include "bpf_testmod.h"
12 
13 #define CREATE_TRACE_POINTS
14 #include "bpf_testmod-events.h"
15 
16 typedef int (*func_proto_typedef)(long);
17 typedef int (*func_proto_typedef_nested1)(func_proto_typedef);
18 typedef int (*func_proto_typedef_nested2)(func_proto_typedef_nested1);
19 
20 DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
21 long bpf_testmod_test_struct_arg_result;
22 
23 struct bpf_testmod_struct_arg_1 {
24 	int a;
25 };
26 struct bpf_testmod_struct_arg_2 {
27 	long a;
28 	long b;
29 };
30 
31 noinline int
32 bpf_testmod_test_struct_arg_1(struct bpf_testmod_struct_arg_2 a, int b, int c) {
33 	bpf_testmod_test_struct_arg_result = a.a + a.b  + b + c;
34 	return bpf_testmod_test_struct_arg_result;
35 }
36 
37 noinline int
38 bpf_testmod_test_struct_arg_2(int a, struct bpf_testmod_struct_arg_2 b, int c) {
39 	bpf_testmod_test_struct_arg_result = a + b.a + b.b + c;
40 	return bpf_testmod_test_struct_arg_result;
41 }
42 
43 noinline int
44 bpf_testmod_test_struct_arg_3(int a, int b, struct bpf_testmod_struct_arg_2 c) {
45 	bpf_testmod_test_struct_arg_result = a + b + c.a + c.b;
46 	return bpf_testmod_test_struct_arg_result;
47 }
48 
49 noinline int
50 bpf_testmod_test_struct_arg_4(struct bpf_testmod_struct_arg_1 a, int b,
51 			      int c, int d, struct bpf_testmod_struct_arg_2 e) {
52 	bpf_testmod_test_struct_arg_result = a.a + b + c + d + e.a + e.b;
53 	return bpf_testmod_test_struct_arg_result;
54 }
55 
56 noinline int
57 bpf_testmod_test_struct_arg_5(void) {
58 	bpf_testmod_test_struct_arg_result = 1;
59 	return bpf_testmod_test_struct_arg_result;
60 }
61 
62 noinline void
63 bpf_testmod_test_mod_kfunc(int i)
64 {
65 	*(int *)this_cpu_ptr(&bpf_testmod_ksym_percpu) = i;
66 }
67 
68 struct bpf_testmod_btf_type_tag_1 {
69 	int a;
70 };
71 
72 struct bpf_testmod_btf_type_tag_2 {
73 	struct bpf_testmod_btf_type_tag_1 __user *p;
74 };
75 
76 struct bpf_testmod_btf_type_tag_3 {
77 	struct bpf_testmod_btf_type_tag_1 __percpu *p;
78 };
79 
80 noinline int
81 bpf_testmod_test_btf_type_tag_user_1(struct bpf_testmod_btf_type_tag_1 __user *arg) {
82 	BTF_TYPE_EMIT(func_proto_typedef);
83 	BTF_TYPE_EMIT(func_proto_typedef_nested1);
84 	BTF_TYPE_EMIT(func_proto_typedef_nested2);
85 	return arg->a;
86 }
87 
88 noinline int
89 bpf_testmod_test_btf_type_tag_user_2(struct bpf_testmod_btf_type_tag_2 *arg) {
90 	return arg->p->a;
91 }
92 
93 noinline int
94 bpf_testmod_test_btf_type_tag_percpu_1(struct bpf_testmod_btf_type_tag_1 __percpu *arg) {
95 	return arg->a;
96 }
97 
98 noinline int
99 bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 *arg) {
100 	return arg->p->a;
101 }
102 
103 noinline int bpf_testmod_loop_test(int n)
104 {
105 	int i, sum = 0;
106 
107 	/* the primary goal of this test is to test LBR. Create a lot of
108 	 * branches in the function, so we can catch it easily.
109 	 */
110 	for (i = 0; i < n; i++)
111 		sum += i;
112 	return sum;
113 }
114 
115 __weak noinline struct file *bpf_testmod_return_ptr(int arg)
116 {
117 	static struct file f = {};
118 
119 	switch (arg) {
120 	case 1: return (void *)EINVAL;		/* user addr */
121 	case 2: return (void *)0xcafe4a11;	/* user addr */
122 	case 3: return (void *)-EINVAL;		/* canonical, but invalid */
123 	case 4: return (void *)(1ull << 60);	/* non-canonical and invalid */
124 	case 5: return (void *)~(1ull << 30);	/* trigger extable */
125 	case 6: return &f;			/* valid addr */
126 	case 7: return (void *)((long)&f | 1);	/* kernel tricks */
127 	default: return NULL;
128 	}
129 }
130 
131 noinline int bpf_testmod_fentry_test1(int a)
132 {
133 	return a + 1;
134 }
135 
136 noinline int bpf_testmod_fentry_test2(int a, u64 b)
137 {
138 	return a + b;
139 }
140 
141 noinline int bpf_testmod_fentry_test3(char a, int b, u64 c)
142 {
143 	return a + b + c;
144 }
145 
146 int bpf_testmod_fentry_ok;
147 
148 noinline ssize_t
149 bpf_testmod_test_read(struct file *file, struct kobject *kobj,
150 		      struct bin_attribute *bin_attr,
151 		      char *buf, loff_t off, size_t len)
152 {
153 	struct bpf_testmod_test_read_ctx ctx = {
154 		.buf = buf,
155 		.off = off,
156 		.len = len,
157 	};
158 	struct bpf_testmod_struct_arg_1 struct_arg1 = {10};
159 	struct bpf_testmod_struct_arg_2 struct_arg2 = {2, 3};
160 	int i = 1;
161 
162 	while (bpf_testmod_return_ptr(i))
163 		i++;
164 
165 	(void)bpf_testmod_test_struct_arg_1(struct_arg2, 1, 4);
166 	(void)bpf_testmod_test_struct_arg_2(1, struct_arg2, 4);
167 	(void)bpf_testmod_test_struct_arg_3(1, 4, struct_arg2);
168 	(void)bpf_testmod_test_struct_arg_4(struct_arg1, 1, 2, 3, struct_arg2);
169 	(void)bpf_testmod_test_struct_arg_5();
170 
171 	/* This is always true. Use the check to make sure the compiler
172 	 * doesn't remove bpf_testmod_loop_test.
173 	 */
174 	if (bpf_testmod_loop_test(101) > 100)
175 		trace_bpf_testmod_test_read(current, &ctx);
176 
177 	/* Magic number to enable writable tp */
178 	if (len == 64) {
179 		struct bpf_testmod_test_writable_ctx writable = {
180 			.val = 1024,
181 		};
182 		trace_bpf_testmod_test_writable_bare(&writable);
183 		if (writable.early_ret)
184 			return snprintf(buf, len, "%d\n", writable.val);
185 	}
186 
187 	if (bpf_testmod_fentry_test1(1) != 2 ||
188 	    bpf_testmod_fentry_test2(2, 3) != 5 ||
189 	    bpf_testmod_fentry_test3(4, 5, 6) != 15)
190 		goto out;
191 
192 	bpf_testmod_fentry_ok = 1;
193 out:
194 	return -EIO; /* always fail */
195 }
196 EXPORT_SYMBOL(bpf_testmod_test_read);
197 ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
198 
199 noinline ssize_t
200 bpf_testmod_test_write(struct file *file, struct kobject *kobj,
201 		      struct bin_attribute *bin_attr,
202 		      char *buf, loff_t off, size_t len)
203 {
204 	struct bpf_testmod_test_write_ctx ctx = {
205 		.buf = buf,
206 		.off = off,
207 		.len = len,
208 	};
209 
210 	trace_bpf_testmod_test_write_bare(current, &ctx);
211 
212 	return -EIO; /* always fail */
213 }
214 EXPORT_SYMBOL(bpf_testmod_test_write);
215 ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
216 
217 static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
218 	.attr = { .name = "bpf_testmod", .mode = 0666, },
219 	.read = bpf_testmod_test_read,
220 	.write = bpf_testmod_test_write,
221 };
222 
223 BTF_SET8_START(bpf_testmod_check_kfunc_ids)
224 BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc)
225 BTF_SET8_END(bpf_testmod_check_kfunc_ids)
226 
227 static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
228 	.owner = THIS_MODULE,
229 	.set   = &bpf_testmod_check_kfunc_ids,
230 };
231 
232 extern int bpf_fentry_test1(int a);
233 
234 static int bpf_testmod_init(void)
235 {
236 	int ret;
237 
238 	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
239 	if (ret < 0)
240 		return ret;
241 	if (bpf_fentry_test1(0) < 0)
242 		return -EINVAL;
243 	return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
244 }
245 
246 static void bpf_testmod_exit(void)
247 {
248 	return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
249 }
250 
251 module_init(bpf_testmod_init);
252 module_exit(bpf_testmod_exit);
253 
254 MODULE_AUTHOR("Andrii Nakryiko");
255 MODULE_DESCRIPTION("BPF selftests module");
256 MODULE_LICENSE("Dual BSD/GPL");
257