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 
22 noinline void
23 bpf_testmod_test_mod_kfunc(int i)
24 {
25 	*(int *)this_cpu_ptr(&bpf_testmod_ksym_percpu) = i;
26 }
27 
28 struct bpf_testmod_btf_type_tag_1 {
29 	int a;
30 };
31 
32 struct bpf_testmod_btf_type_tag_2 {
33 	struct bpf_testmod_btf_type_tag_1 __user *p;
34 };
35 
36 noinline int
37 bpf_testmod_test_btf_type_tag_user_1(struct bpf_testmod_btf_type_tag_1 __user *arg) {
38 	BTF_TYPE_EMIT(func_proto_typedef);
39 	BTF_TYPE_EMIT(func_proto_typedef_nested1);
40 	BTF_TYPE_EMIT(func_proto_typedef_nested2);
41 	return arg->a;
42 }
43 
44 noinline int
45 bpf_testmod_test_btf_type_tag_user_2(struct bpf_testmod_btf_type_tag_2 *arg) {
46 	return arg->p->a;
47 }
48 
49 noinline int bpf_testmod_loop_test(int n)
50 {
51 	int i, sum = 0;
52 
53 	/* the primary goal of this test is to test LBR. Create a lot of
54 	 * branches in the function, so we can catch it easily.
55 	 */
56 	for (i = 0; i < n; i++)
57 		sum += i;
58 	return sum;
59 }
60 
61 __weak noinline struct file *bpf_testmod_return_ptr(int arg)
62 {
63 	static struct file f = {};
64 
65 	switch (arg) {
66 	case 1: return (void *)EINVAL;		/* user addr */
67 	case 2: return (void *)0xcafe4a11;	/* user addr */
68 	case 3: return (void *)-EINVAL;		/* canonical, but invalid */
69 	case 4: return (void *)(1ull << 60);	/* non-canonical and invalid */
70 	case 5: return (void *)~(1ull << 30);	/* trigger extable */
71 	case 6: return &f;			/* valid addr */
72 	case 7: return (void *)((long)&f | 1);	/* kernel tricks */
73 	default: return NULL;
74 	}
75 }
76 
77 noinline ssize_t
78 bpf_testmod_test_read(struct file *file, struct kobject *kobj,
79 		      struct bin_attribute *bin_attr,
80 		      char *buf, loff_t off, size_t len)
81 {
82 	struct bpf_testmod_test_read_ctx ctx = {
83 		.buf = buf,
84 		.off = off,
85 		.len = len,
86 	};
87 	int i = 1;
88 
89 	while (bpf_testmod_return_ptr(i))
90 		i++;
91 
92 	/* This is always true. Use the check to make sure the compiler
93 	 * doesn't remove bpf_testmod_loop_test.
94 	 */
95 	if (bpf_testmod_loop_test(101) > 100)
96 		trace_bpf_testmod_test_read(current, &ctx);
97 
98 	/* Magic number to enable writable tp */
99 	if (len == 64) {
100 		struct bpf_testmod_test_writable_ctx writable = {
101 			.val = 1024,
102 		};
103 		trace_bpf_testmod_test_writable_bare(&writable);
104 		if (writable.early_ret)
105 			return snprintf(buf, len, "%d\n", writable.val);
106 	}
107 
108 	return -EIO; /* always fail */
109 }
110 EXPORT_SYMBOL(bpf_testmod_test_read);
111 ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
112 
113 noinline ssize_t
114 bpf_testmod_test_write(struct file *file, struct kobject *kobj,
115 		      struct bin_attribute *bin_attr,
116 		      char *buf, loff_t off, size_t len)
117 {
118 	struct bpf_testmod_test_write_ctx ctx = {
119 		.buf = buf,
120 		.off = off,
121 		.len = len,
122 	};
123 
124 	trace_bpf_testmod_test_write_bare(current, &ctx);
125 
126 	return -EIO; /* always fail */
127 }
128 EXPORT_SYMBOL(bpf_testmod_test_write);
129 ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
130 
131 static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
132 	.attr = { .name = "bpf_testmod", .mode = 0666, },
133 	.read = bpf_testmod_test_read,
134 	.write = bpf_testmod_test_write,
135 };
136 
137 BTF_SET_START(bpf_testmod_check_kfunc_ids)
138 BTF_ID(func, bpf_testmod_test_mod_kfunc)
139 BTF_SET_END(bpf_testmod_check_kfunc_ids)
140 
141 static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
142 	.owner     = THIS_MODULE,
143 	.check_set = &bpf_testmod_check_kfunc_ids,
144 };
145 
146 extern int bpf_fentry_test1(int a);
147 
148 static int bpf_testmod_init(void)
149 {
150 	int ret;
151 
152 	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
153 	if (ret < 0)
154 		return ret;
155 	if (bpf_fentry_test1(0) < 0)
156 		return -EINVAL;
157 	return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
158 }
159 
160 static void bpf_testmod_exit(void)
161 {
162 	return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
163 }
164 
165 module_init(bpf_testmod_init);
166 module_exit(bpf_testmod_exit);
167 
168 MODULE_AUTHOR("Andrii Nakryiko");
169 MODULE_DESCRIPTION("BPF selftests module");
170 MODULE_LICENSE("Dual BSD/GPL");
171