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
bpf_testmod_test_struct_arg_1(struct bpf_testmod_struct_arg_2 a,int b,int c)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
bpf_testmod_test_struct_arg_2(int a,struct bpf_testmod_struct_arg_2 b,int c)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
bpf_testmod_test_struct_arg_3(int a,int b,struct bpf_testmod_struct_arg_2 c)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
bpf_testmod_test_struct_arg_4(struct bpf_testmod_struct_arg_1 a,int b,int c,int d,struct bpf_testmod_struct_arg_2 e)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
bpf_testmod_test_struct_arg_5(void)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
bpf_testmod_test_mod_kfunc(int i)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
bpf_testmod_test_btf_type_tag_user_1(struct bpf_testmod_btf_type_tag_1 __user * arg)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
bpf_testmod_test_btf_type_tag_user_2(struct bpf_testmod_btf_type_tag_2 * arg)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
bpf_testmod_test_btf_type_tag_percpu_1(struct bpf_testmod_btf_type_tag_1 __percpu * arg)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
bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 * arg)99 bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 *arg) {
100 	return arg->p->a;
101 }
102 
bpf_testmod_loop_test(int n)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 
bpf_testmod_return_ptr(int arg)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 ssize_t
bpf_testmod_test_read(struct file * file,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t len)132 bpf_testmod_test_read(struct file *file, struct kobject *kobj,
133 		      struct bin_attribute *bin_attr,
134 		      char *buf, loff_t off, size_t len)
135 {
136 	struct bpf_testmod_test_read_ctx ctx = {
137 		.buf = buf,
138 		.off = off,
139 		.len = len,
140 	};
141 	struct bpf_testmod_struct_arg_1 struct_arg1 = {10};
142 	struct bpf_testmod_struct_arg_2 struct_arg2 = {2, 3};
143 	int i = 1;
144 
145 	while (bpf_testmod_return_ptr(i))
146 		i++;
147 
148 	(void)bpf_testmod_test_struct_arg_1(struct_arg2, 1, 4);
149 	(void)bpf_testmod_test_struct_arg_2(1, struct_arg2, 4);
150 	(void)bpf_testmod_test_struct_arg_3(1, 4, struct_arg2);
151 	(void)bpf_testmod_test_struct_arg_4(struct_arg1, 1, 2, 3, struct_arg2);
152 	(void)bpf_testmod_test_struct_arg_5();
153 
154 	/* This is always true. Use the check to make sure the compiler
155 	 * doesn't remove bpf_testmod_loop_test.
156 	 */
157 	if (bpf_testmod_loop_test(101) > 100)
158 		trace_bpf_testmod_test_read(current, &ctx);
159 
160 	/* Magic number to enable writable tp */
161 	if (len == 64) {
162 		struct bpf_testmod_test_writable_ctx writable = {
163 			.val = 1024,
164 		};
165 		trace_bpf_testmod_test_writable_bare(&writable);
166 		if (writable.early_ret)
167 			return snprintf(buf, len, "%d\n", writable.val);
168 	}
169 
170 	return -EIO; /* always fail */
171 }
172 EXPORT_SYMBOL(bpf_testmod_test_read);
173 ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
174 
175 noinline ssize_t
bpf_testmod_test_write(struct file * file,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t len)176 bpf_testmod_test_write(struct file *file, struct kobject *kobj,
177 		      struct bin_attribute *bin_attr,
178 		      char *buf, loff_t off, size_t len)
179 {
180 	struct bpf_testmod_test_write_ctx ctx = {
181 		.buf = buf,
182 		.off = off,
183 		.len = len,
184 	};
185 
186 	trace_bpf_testmod_test_write_bare(current, &ctx);
187 
188 	return -EIO; /* always fail */
189 }
190 EXPORT_SYMBOL(bpf_testmod_test_write);
191 ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
192 
193 static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
194 	.attr = { .name = "bpf_testmod", .mode = 0666, },
195 	.read = bpf_testmod_test_read,
196 	.write = bpf_testmod_test_write,
197 };
198 
199 BTF_SET8_START(bpf_testmod_check_kfunc_ids)
200 BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc)
201 BTF_SET8_END(bpf_testmod_check_kfunc_ids)
202 
203 static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
204 	.owner = THIS_MODULE,
205 	.set   = &bpf_testmod_check_kfunc_ids,
206 };
207 
208 extern int bpf_fentry_test1(int a);
209 
bpf_testmod_init(void)210 static int bpf_testmod_init(void)
211 {
212 	int ret;
213 
214 	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
215 	if (ret < 0)
216 		return ret;
217 	if (bpf_fentry_test1(0) < 0)
218 		return -EINVAL;
219 	return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
220 }
221 
bpf_testmod_exit(void)222 static void bpf_testmod_exit(void)
223 {
224 	return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
225 }
226 
227 module_init(bpf_testmod_init);
228 module_exit(bpf_testmod_exit);
229 
230 MODULE_AUTHOR("Andrii Nakryiko");
231 MODULE_DESCRIPTION("BPF selftests module");
232 MODULE_LICENSE("Dual BSD/GPL");
233