Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2021. Huawei Technologies Co., Ltd
4 */
5#include <linux/kernel.h>
6#include <linux/bpf_verifier.h>
7#include <linux/bpf.h>
8#include <linux/btf.h>
9
10extern struct bpf_struct_ops bpf_bpf_dummy_ops;
11
12/* A common type for test_N with return value in bpf_dummy_ops */
13typedef int (*dummy_ops_test_ret_fn)(struct bpf_dummy_ops_state *state, ...);
14
15struct bpf_dummy_ops_test_args {
16 u64 args[MAX_BPF_FUNC_ARGS];
17 struct bpf_dummy_ops_state state;
18};
19
20static struct bpf_dummy_ops_test_args *
21dummy_ops_init_args(const union bpf_attr *kattr, unsigned int nr)
22{
23 __u32 size_in;
24 struct bpf_dummy_ops_test_args *args;
25 void __user *ctx_in;
26 void __user *u_state;
27
28 size_in = kattr->test.ctx_size_in;
29 if (size_in != sizeof(u64) * nr)
30 return ERR_PTR(-EINVAL);
31
32 args = kzalloc(sizeof(*args), GFP_KERNEL);
33 if (!args)
34 return ERR_PTR(-ENOMEM);
35
36 ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
37 if (copy_from_user(args->args, ctx_in, size_in))
38 goto out;
39
40 /* args[0] is 0 means state argument of test_N will be NULL */
41 u_state = u64_to_user_ptr(args->args[0]);
42 if (u_state && copy_from_user(&args->state, u_state,
43 sizeof(args->state)))
44 goto out;
45
46 return args;
47out:
48 kfree(args);
49 return ERR_PTR(-EFAULT);
50}
51
52static int dummy_ops_copy_args(struct bpf_dummy_ops_test_args *args)
53{
54 void __user *u_state;
55
56 u_state = u64_to_user_ptr(args->args[0]);
57 if (u_state && copy_to_user(u_state, &args->state, sizeof(args->state)))
58 return -EFAULT;
59
60 return 0;
61}
62
63static int dummy_ops_call_op(void *image, struct bpf_dummy_ops_test_args *args)
64{
65 dummy_ops_test_ret_fn test = (void *)image;
66 struct bpf_dummy_ops_state *state = NULL;
67
68 /* state needs to be NULL if args[0] is 0 */
69 if (args->args[0])
70 state = &args->state;
71 return test(state, args->args[1], args->args[2],
72 args->args[3], args->args[4]);
73}
74
75extern const struct bpf_link_ops bpf_struct_ops_link_lops;
76
77int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
78 union bpf_attr __user *uattr)
79{
80 const struct bpf_struct_ops *st_ops = &bpf_bpf_dummy_ops;
81 const struct btf_type *func_proto;
82 struct bpf_dummy_ops_test_args *args;
83 struct bpf_tramp_links *tlinks;
84 struct bpf_tramp_link *link = NULL;
85 void *image = NULL;
86 unsigned int op_idx;
87 int prog_ret;
88 int err;
89
90 if (prog->aux->attach_btf_id != st_ops->type_id)
91 return -EOPNOTSUPP;
92
93 func_proto = prog->aux->attach_func_proto;
94 args = dummy_ops_init_args(kattr, btf_type_vlen(func_proto));
95 if (IS_ERR(args))
96 return PTR_ERR(args);
97
98 tlinks = kcalloc(BPF_TRAMP_MAX, sizeof(*tlinks), GFP_KERNEL);
99 if (!tlinks) {
100 err = -ENOMEM;
101 goto out;
102 }
103
104 image = bpf_jit_alloc_exec(PAGE_SIZE);
105 if (!image) {
106 err = -ENOMEM;
107 goto out;
108 }
109 set_vm_flush_reset_perms(image);
110
111 link = kzalloc(sizeof(*link), GFP_USER);
112 if (!link) {
113 err = -ENOMEM;
114 goto out;
115 }
116 /* prog doesn't take the ownership of the reference from caller */
117 bpf_prog_inc(prog);
118 bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_link_lops, prog);
119
120 op_idx = prog->expected_attach_type;
121 err = bpf_struct_ops_prepare_trampoline(tlinks, link,
122 &st_ops->func_models[op_idx],
123 image, image + PAGE_SIZE);
124 if (err < 0)
125 goto out;
126
127 set_memory_rox((long)image, 1);
128 prog_ret = dummy_ops_call_op(image, args);
129
130 err = dummy_ops_copy_args(args);
131 if (err)
132 goto out;
133 if (put_user(prog_ret, &uattr->test.retval))
134 err = -EFAULT;
135out:
136 kfree(args);
137 bpf_jit_free_exec(image);
138 if (link)
139 bpf_link_put(&link->link);
140 kfree(tlinks);
141 return err;
142}
143
144static int bpf_dummy_init(struct btf *btf)
145{
146 return 0;
147}
148
149static bool bpf_dummy_ops_is_valid_access(int off, int size,
150 enum bpf_access_type type,
151 const struct bpf_prog *prog,
152 struct bpf_insn_access_aux *info)
153{
154 return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
155}
156
157static int bpf_dummy_ops_btf_struct_access(struct bpf_verifier_log *log,
158 const struct bpf_reg_state *reg,
159 int off, int size, enum bpf_access_type atype,
160 u32 *next_btf_id,
161 enum bpf_type_flag *flag)
162{
163 const struct btf_type *state;
164 const struct btf_type *t;
165 s32 type_id;
166 int err;
167
168 type_id = btf_find_by_name_kind(reg->btf, "bpf_dummy_ops_state",
169 BTF_KIND_STRUCT);
170 if (type_id < 0)
171 return -EINVAL;
172
173 t = btf_type_by_id(reg->btf, reg->btf_id);
174 state = btf_type_by_id(reg->btf, type_id);
175 if (t != state) {
176 bpf_log(log, "only access to bpf_dummy_ops_state is supported\n");
177 return -EACCES;
178 }
179
180 err = btf_struct_access(log, reg, off, size, atype, next_btf_id, flag);
181 if (err < 0)
182 return err;
183
184 return atype == BPF_READ ? err : NOT_INIT;
185}
186
187static const struct bpf_verifier_ops bpf_dummy_verifier_ops = {
188 .is_valid_access = bpf_dummy_ops_is_valid_access,
189 .btf_struct_access = bpf_dummy_ops_btf_struct_access,
190};
191
192static int bpf_dummy_init_member(const struct btf_type *t,
193 const struct btf_member *member,
194 void *kdata, const void *udata)
195{
196 return -EOPNOTSUPP;
197}
198
199static int bpf_dummy_reg(void *kdata)
200{
201 return -EOPNOTSUPP;
202}
203
204static void bpf_dummy_unreg(void *kdata)
205{
206}
207
208struct bpf_struct_ops bpf_bpf_dummy_ops = {
209 .verifier_ops = &bpf_dummy_verifier_ops,
210 .init = bpf_dummy_init,
211 .init_member = bpf_dummy_init_member,
212 .reg = bpf_dummy_reg,
213 .unreg = bpf_dummy_unreg,
214 .name = "bpf_dummy_ops",
215};
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2021. Huawei Technologies Co., Ltd
4 */
5#include <linux/kernel.h>
6#include <linux/bpf_verifier.h>
7#include <linux/bpf.h>
8#include <linux/btf.h>
9
10extern struct bpf_struct_ops bpf_bpf_dummy_ops;
11
12/* A common type for test_N with return value in bpf_dummy_ops */
13typedef int (*dummy_ops_test_ret_fn)(struct bpf_dummy_ops_state *state, ...);
14
15static int dummy_ops_test_ret_function(struct bpf_dummy_ops_state *state, ...)
16{
17 return 0;
18}
19
20struct bpf_dummy_ops_test_args {
21 u64 args[MAX_BPF_FUNC_ARGS];
22 struct bpf_dummy_ops_state state;
23};
24
25static struct bpf_dummy_ops_test_args *
26dummy_ops_init_args(const union bpf_attr *kattr, unsigned int nr)
27{
28 __u32 size_in;
29 struct bpf_dummy_ops_test_args *args;
30 void __user *ctx_in;
31 void __user *u_state;
32
33 size_in = kattr->test.ctx_size_in;
34 if (size_in != sizeof(u64) * nr)
35 return ERR_PTR(-EINVAL);
36
37 args = kzalloc(sizeof(*args), GFP_KERNEL);
38 if (!args)
39 return ERR_PTR(-ENOMEM);
40
41 ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
42 if (copy_from_user(args->args, ctx_in, size_in))
43 goto out;
44
45 /* args[0] is 0 means state argument of test_N will be NULL */
46 u_state = u64_to_user_ptr(args->args[0]);
47 if (u_state && copy_from_user(&args->state, u_state,
48 sizeof(args->state)))
49 goto out;
50
51 return args;
52out:
53 kfree(args);
54 return ERR_PTR(-EFAULT);
55}
56
57static int dummy_ops_copy_args(struct bpf_dummy_ops_test_args *args)
58{
59 void __user *u_state;
60
61 u_state = u64_to_user_ptr(args->args[0]);
62 if (u_state && copy_to_user(u_state, &args->state, sizeof(args->state)))
63 return -EFAULT;
64
65 return 0;
66}
67
68static int dummy_ops_call_op(void *image, struct bpf_dummy_ops_test_args *args)
69{
70 dummy_ops_test_ret_fn test = (void *)image + cfi_get_offset();
71 struct bpf_dummy_ops_state *state = NULL;
72
73 /* state needs to be NULL if args[0] is 0 */
74 if (args->args[0])
75 state = &args->state;
76 return test(state, args->args[1], args->args[2],
77 args->args[3], args->args[4]);
78}
79
80extern const struct bpf_link_ops bpf_struct_ops_link_lops;
81
82int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
83 union bpf_attr __user *uattr)
84{
85 const struct bpf_struct_ops *st_ops = &bpf_bpf_dummy_ops;
86 const struct btf_type *func_proto;
87 struct bpf_dummy_ops_test_args *args;
88 struct bpf_tramp_links *tlinks;
89 struct bpf_tramp_link *link = NULL;
90 void *image = NULL;
91 unsigned int op_idx;
92 int prog_ret;
93 int err;
94
95 if (prog->aux->attach_btf_id != st_ops->type_id)
96 return -EOPNOTSUPP;
97
98 func_proto = prog->aux->attach_func_proto;
99 args = dummy_ops_init_args(kattr, btf_type_vlen(func_proto));
100 if (IS_ERR(args))
101 return PTR_ERR(args);
102
103 tlinks = kcalloc(BPF_TRAMP_MAX, sizeof(*tlinks), GFP_KERNEL);
104 if (!tlinks) {
105 err = -ENOMEM;
106 goto out;
107 }
108
109 image = arch_alloc_bpf_trampoline(PAGE_SIZE);
110 if (!image) {
111 err = -ENOMEM;
112 goto out;
113 }
114
115 link = kzalloc(sizeof(*link), GFP_USER);
116 if (!link) {
117 err = -ENOMEM;
118 goto out;
119 }
120 /* prog doesn't take the ownership of the reference from caller */
121 bpf_prog_inc(prog);
122 bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_link_lops, prog);
123
124 op_idx = prog->expected_attach_type;
125 err = bpf_struct_ops_prepare_trampoline(tlinks, link,
126 &st_ops->func_models[op_idx],
127 &dummy_ops_test_ret_function,
128 image, image + PAGE_SIZE);
129 if (err < 0)
130 goto out;
131
132 arch_protect_bpf_trampoline(image, PAGE_SIZE);
133 prog_ret = dummy_ops_call_op(image, args);
134
135 err = dummy_ops_copy_args(args);
136 if (err)
137 goto out;
138 if (put_user(prog_ret, &uattr->test.retval))
139 err = -EFAULT;
140out:
141 kfree(args);
142 arch_free_bpf_trampoline(image, PAGE_SIZE);
143 if (link)
144 bpf_link_put(&link->link);
145 kfree(tlinks);
146 return err;
147}
148
149static int bpf_dummy_init(struct btf *btf)
150{
151 return 0;
152}
153
154static bool bpf_dummy_ops_is_valid_access(int off, int size,
155 enum bpf_access_type type,
156 const struct bpf_prog *prog,
157 struct bpf_insn_access_aux *info)
158{
159 return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
160}
161
162static int bpf_dummy_ops_check_member(const struct btf_type *t,
163 const struct btf_member *member,
164 const struct bpf_prog *prog)
165{
166 u32 moff = __btf_member_bit_offset(t, member) / 8;
167
168 switch (moff) {
169 case offsetof(struct bpf_dummy_ops, test_sleepable):
170 break;
171 default:
172 if (prog->aux->sleepable)
173 return -EINVAL;
174 }
175
176 return 0;
177}
178
179static int bpf_dummy_ops_btf_struct_access(struct bpf_verifier_log *log,
180 const struct bpf_reg_state *reg,
181 int off, int size)
182{
183 const struct btf_type *state;
184 const struct btf_type *t;
185 s32 type_id;
186
187 type_id = btf_find_by_name_kind(reg->btf, "bpf_dummy_ops_state",
188 BTF_KIND_STRUCT);
189 if (type_id < 0)
190 return -EINVAL;
191
192 t = btf_type_by_id(reg->btf, reg->btf_id);
193 state = btf_type_by_id(reg->btf, type_id);
194 if (t != state) {
195 bpf_log(log, "only access to bpf_dummy_ops_state is supported\n");
196 return -EACCES;
197 }
198
199 if (off + size > sizeof(struct bpf_dummy_ops_state)) {
200 bpf_log(log, "write access at off %d with size %d\n", off, size);
201 return -EACCES;
202 }
203
204 return NOT_INIT;
205}
206
207static const struct bpf_verifier_ops bpf_dummy_verifier_ops = {
208 .is_valid_access = bpf_dummy_ops_is_valid_access,
209 .btf_struct_access = bpf_dummy_ops_btf_struct_access,
210};
211
212static int bpf_dummy_init_member(const struct btf_type *t,
213 const struct btf_member *member,
214 void *kdata, const void *udata)
215{
216 return -EOPNOTSUPP;
217}
218
219static int bpf_dummy_reg(void *kdata)
220{
221 return -EOPNOTSUPP;
222}
223
224static void bpf_dummy_unreg(void *kdata)
225{
226}
227
228static int bpf_dummy_test_1(struct bpf_dummy_ops_state *cb)
229{
230 return 0;
231}
232
233static int bpf_dummy_test_2(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2,
234 char a3, unsigned long a4)
235{
236 return 0;
237}
238
239static int bpf_dummy_test_sleepable(struct bpf_dummy_ops_state *cb)
240{
241 return 0;
242}
243
244static struct bpf_dummy_ops __bpf_bpf_dummy_ops = {
245 .test_1 = bpf_dummy_test_1,
246 .test_2 = bpf_dummy_test_2,
247 .test_sleepable = bpf_dummy_test_sleepable,
248};
249
250struct bpf_struct_ops bpf_bpf_dummy_ops = {
251 .verifier_ops = &bpf_dummy_verifier_ops,
252 .init = bpf_dummy_init,
253 .check_member = bpf_dummy_ops_check_member,
254 .init_member = bpf_dummy_init_member,
255 .reg = bpf_dummy_reg,
256 .unreg = bpf_dummy_unreg,
257 .name = "bpf_dummy_ops",
258 .cfi_stubs = &__bpf_bpf_dummy_ops,
259};