xref: /linux/net/bpf/test_run.c (revision a7d22ca2a483d6c69c0791954447464297315ffa)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
3  */
4 #include <linux/bpf.h>
5 #include <linux/slab.h>
6 #include <linux/vmalloc.h>
7 #include <linux/etherdevice.h>
8 #include <linux/filter.h>
9 #include <linux/sched/signal.h>
10 #include <net/bpf_sk_storage.h>
11 #include <net/sock.h>
12 #include <net/tcp.h>
13 
14 #define CREATE_TRACE_POINTS
15 #include <trace/events/bpf_test_run.h>
16 
17 static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
18 			u32 *retval, u32 *time, bool xdp)
19 {
20 	struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { NULL };
21 	enum bpf_cgroup_storage_type stype;
22 	u64 time_start, time_spent = 0;
23 	int ret = 0;
24 	u32 i;
25 
26 	for_each_cgroup_storage_type(stype) {
27 		storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
28 		if (IS_ERR(storage[stype])) {
29 			storage[stype] = NULL;
30 			for_each_cgroup_storage_type(stype)
31 				bpf_cgroup_storage_free(storage[stype]);
32 			return -ENOMEM;
33 		}
34 	}
35 
36 	if (!repeat)
37 		repeat = 1;
38 
39 	rcu_read_lock();
40 	preempt_disable();
41 	time_start = ktime_get_ns();
42 	for (i = 0; i < repeat; i++) {
43 		bpf_cgroup_storage_set(storage);
44 
45 		if (xdp)
46 			*retval = bpf_prog_run_xdp(prog, ctx);
47 		else
48 			*retval = BPF_PROG_RUN(prog, ctx);
49 
50 		if (signal_pending(current)) {
51 			ret = -EINTR;
52 			break;
53 		}
54 
55 		if (need_resched()) {
56 			time_spent += ktime_get_ns() - time_start;
57 			preempt_enable();
58 			rcu_read_unlock();
59 
60 			cond_resched();
61 
62 			rcu_read_lock();
63 			preempt_disable();
64 			time_start = ktime_get_ns();
65 		}
66 	}
67 	time_spent += ktime_get_ns() - time_start;
68 	preempt_enable();
69 	rcu_read_unlock();
70 
71 	do_div(time_spent, repeat);
72 	*time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent;
73 
74 	for_each_cgroup_storage_type(stype)
75 		bpf_cgroup_storage_free(storage[stype]);
76 
77 	return ret;
78 }
79 
80 static int bpf_test_finish(const union bpf_attr *kattr,
81 			   union bpf_attr __user *uattr, const void *data,
82 			   u32 size, u32 retval, u32 duration)
83 {
84 	void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
85 	int err = -EFAULT;
86 	u32 copy_size = size;
87 
88 	/* Clamp copy if the user has provided a size hint, but copy the full
89 	 * buffer if not to retain old behaviour.
90 	 */
91 	if (kattr->test.data_size_out &&
92 	    copy_size > kattr->test.data_size_out) {
93 		copy_size = kattr->test.data_size_out;
94 		err = -ENOSPC;
95 	}
96 
97 	if (data_out && copy_to_user(data_out, data, copy_size))
98 		goto out;
99 	if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
100 		goto out;
101 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
102 		goto out;
103 	if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
104 		goto out;
105 	if (err != -ENOSPC)
106 		err = 0;
107 out:
108 	trace_bpf_test_finish(&err);
109 	return err;
110 }
111 
112 /* Integer types of various sizes and pointer combinations cover variety of
113  * architecture dependent calling conventions. 7+ can be supported in the
114  * future.
115  */
116 int noinline bpf_fentry_test1(int a)
117 {
118 	return a + 1;
119 }
120 
121 int noinline bpf_fentry_test2(int a, u64 b)
122 {
123 	return a + b;
124 }
125 
126 int noinline bpf_fentry_test3(char a, int b, u64 c)
127 {
128 	return a + b + c;
129 }
130 
131 int noinline bpf_fentry_test4(void *a, char b, int c, u64 d)
132 {
133 	return (long)a + b + c + d;
134 }
135 
136 int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e)
137 {
138 	return a + (long)b + c + d + e;
139 }
140 
141 int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f)
142 {
143 	return a + (long)b + c + d + (long)e + f;
144 }
145 
146 static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
147 			   u32 headroom, u32 tailroom)
148 {
149 	void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
150 	void *data;
151 
152 	if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom)
153 		return ERR_PTR(-EINVAL);
154 
155 	data = kzalloc(size + headroom + tailroom, GFP_USER);
156 	if (!data)
157 		return ERR_PTR(-ENOMEM);
158 
159 	if (copy_from_user(data + headroom, data_in, size)) {
160 		kfree(data);
161 		return ERR_PTR(-EFAULT);
162 	}
163 	if (bpf_fentry_test1(1) != 2 ||
164 	    bpf_fentry_test2(2, 3) != 5 ||
165 	    bpf_fentry_test3(4, 5, 6) != 15 ||
166 	    bpf_fentry_test4((void *)7, 8, 9, 10) != 34 ||
167 	    bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 ||
168 	    bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111) {
169 		kfree(data);
170 		return ERR_PTR(-EFAULT);
171 	}
172 	return data;
173 }
174 
175 static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size)
176 {
177 	void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in);
178 	void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
179 	u32 size = kattr->test.ctx_size_in;
180 	void *data;
181 	int err;
182 
183 	if (!data_in && !data_out)
184 		return NULL;
185 
186 	data = kzalloc(max_size, GFP_USER);
187 	if (!data)
188 		return ERR_PTR(-ENOMEM);
189 
190 	if (data_in) {
191 		err = bpf_check_uarg_tail_zero(data_in, max_size, size);
192 		if (err) {
193 			kfree(data);
194 			return ERR_PTR(err);
195 		}
196 
197 		size = min_t(u32, max_size, size);
198 		if (copy_from_user(data, data_in, size)) {
199 			kfree(data);
200 			return ERR_PTR(-EFAULT);
201 		}
202 	}
203 	return data;
204 }
205 
206 static int bpf_ctx_finish(const union bpf_attr *kattr,
207 			  union bpf_attr __user *uattr, const void *data,
208 			  u32 size)
209 {
210 	void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
211 	int err = -EFAULT;
212 	u32 copy_size = size;
213 
214 	if (!data || !data_out)
215 		return 0;
216 
217 	if (copy_size > kattr->test.ctx_size_out) {
218 		copy_size = kattr->test.ctx_size_out;
219 		err = -ENOSPC;
220 	}
221 
222 	if (copy_to_user(data_out, data, copy_size))
223 		goto out;
224 	if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size)))
225 		goto out;
226 	if (err != -ENOSPC)
227 		err = 0;
228 out:
229 	return err;
230 }
231 
232 /**
233  * range_is_zero - test whether buffer is initialized
234  * @buf: buffer to check
235  * @from: check from this position
236  * @to: check up until (excluding) this position
237  *
238  * This function returns true if the there is a non-zero byte
239  * in the buf in the range [from,to).
240  */
241 static inline bool range_is_zero(void *buf, size_t from, size_t to)
242 {
243 	return !memchr_inv((u8 *)buf + from, 0, to - from);
244 }
245 
246 static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
247 {
248 	struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
249 
250 	if (!__skb)
251 		return 0;
252 
253 	/* make sure the fields we don't use are zeroed */
254 	if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, priority)))
255 		return -EINVAL;
256 
257 	/* priority is allowed */
258 
259 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, priority),
260 			   offsetof(struct __sk_buff, cb)))
261 		return -EINVAL;
262 
263 	/* cb is allowed */
264 
265 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb),
266 			   offsetof(struct __sk_buff, tstamp)))
267 		return -EINVAL;
268 
269 	/* tstamp is allowed */
270 	/* wire_len is allowed */
271 	/* gso_segs is allowed */
272 
273 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs),
274 			   sizeof(struct __sk_buff)))
275 		return -EINVAL;
276 
277 	skb->priority = __skb->priority;
278 	skb->tstamp = __skb->tstamp;
279 	memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN);
280 
281 	if (__skb->wire_len == 0) {
282 		cb->pkt_len = skb->len;
283 	} else {
284 		if (__skb->wire_len < skb->len ||
285 		    __skb->wire_len > GSO_MAX_SIZE)
286 			return -EINVAL;
287 		cb->pkt_len = __skb->wire_len;
288 	}
289 
290 	if (__skb->gso_segs > GSO_MAX_SEGS)
291 		return -EINVAL;
292 	skb_shinfo(skb)->gso_segs = __skb->gso_segs;
293 
294 	return 0;
295 }
296 
297 static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb)
298 {
299 	struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
300 
301 	if (!__skb)
302 		return;
303 
304 	__skb->priority = skb->priority;
305 	__skb->tstamp = skb->tstamp;
306 	memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN);
307 	__skb->wire_len = cb->pkt_len;
308 	__skb->gso_segs = skb_shinfo(skb)->gso_segs;
309 }
310 
311 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
312 			  union bpf_attr __user *uattr)
313 {
314 	bool is_l2 = false, is_direct_pkt_access = false;
315 	u32 size = kattr->test.data_size_in;
316 	u32 repeat = kattr->test.repeat;
317 	struct __sk_buff *ctx = NULL;
318 	u32 retval, duration;
319 	int hh_len = ETH_HLEN;
320 	struct sk_buff *skb;
321 	struct sock *sk;
322 	void *data;
323 	int ret;
324 
325 	data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
326 			     SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
327 	if (IS_ERR(data))
328 		return PTR_ERR(data);
329 
330 	ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
331 	if (IS_ERR(ctx)) {
332 		kfree(data);
333 		return PTR_ERR(ctx);
334 	}
335 
336 	switch (prog->type) {
337 	case BPF_PROG_TYPE_SCHED_CLS:
338 	case BPF_PROG_TYPE_SCHED_ACT:
339 		is_l2 = true;
340 		/* fall through */
341 	case BPF_PROG_TYPE_LWT_IN:
342 	case BPF_PROG_TYPE_LWT_OUT:
343 	case BPF_PROG_TYPE_LWT_XMIT:
344 		is_direct_pkt_access = true;
345 		break;
346 	default:
347 		break;
348 	}
349 
350 	sk = kzalloc(sizeof(struct sock), GFP_USER);
351 	if (!sk) {
352 		kfree(data);
353 		kfree(ctx);
354 		return -ENOMEM;
355 	}
356 	sock_net_set(sk, current->nsproxy->net_ns);
357 	sock_init_data(NULL, sk);
358 
359 	skb = build_skb(data, 0);
360 	if (!skb) {
361 		kfree(data);
362 		kfree(ctx);
363 		kfree(sk);
364 		return -ENOMEM;
365 	}
366 	skb->sk = sk;
367 
368 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
369 	__skb_put(skb, size);
370 	skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev);
371 	skb_reset_network_header(skb);
372 
373 	if (is_l2)
374 		__skb_push(skb, hh_len);
375 	if (is_direct_pkt_access)
376 		bpf_compute_data_pointers(skb);
377 	ret = convert___skb_to_skb(skb, ctx);
378 	if (ret)
379 		goto out;
380 	ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false);
381 	if (ret)
382 		goto out;
383 	if (!is_l2) {
384 		if (skb_headroom(skb) < hh_len) {
385 			int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
386 
387 			if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
388 				ret = -ENOMEM;
389 				goto out;
390 			}
391 		}
392 		memset(__skb_push(skb, hh_len), 0, hh_len);
393 	}
394 	convert_skb_to___skb(skb, ctx);
395 
396 	size = skb->len;
397 	/* bpf program can never convert linear skb to non-linear */
398 	if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
399 		size = skb_headlen(skb);
400 	ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration);
401 	if (!ret)
402 		ret = bpf_ctx_finish(kattr, uattr, ctx,
403 				     sizeof(struct __sk_buff));
404 out:
405 	kfree_skb(skb);
406 	bpf_sk_storage_free(sk);
407 	kfree(sk);
408 	kfree(ctx);
409 	return ret;
410 }
411 
412 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
413 			  union bpf_attr __user *uattr)
414 {
415 	u32 size = kattr->test.data_size_in;
416 	u32 repeat = kattr->test.repeat;
417 	struct netdev_rx_queue *rxqueue;
418 	struct xdp_buff xdp = {};
419 	u32 retval, duration;
420 	void *data;
421 	int ret;
422 
423 	if (kattr->test.ctx_in || kattr->test.ctx_out)
424 		return -EINVAL;
425 
426 	data = bpf_test_init(kattr, size, XDP_PACKET_HEADROOM + NET_IP_ALIGN, 0);
427 	if (IS_ERR(data))
428 		return PTR_ERR(data);
429 
430 	xdp.data_hard_start = data;
431 	xdp.data = data + XDP_PACKET_HEADROOM + NET_IP_ALIGN;
432 	xdp.data_meta = xdp.data;
433 	xdp.data_end = xdp.data + size;
434 
435 	rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
436 	xdp.rxq = &rxqueue->xdp_rxq;
437 	bpf_prog_change_xdp(NULL, prog);
438 	ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true);
439 	if (ret)
440 		goto out;
441 	if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
442 	    xdp.data_end != xdp.data + size)
443 		size = xdp.data_end - xdp.data;
444 	ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration);
445 out:
446 	bpf_prog_change_xdp(prog, NULL);
447 	kfree(data);
448 	return ret;
449 }
450 
451 static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx)
452 {
453 	/* make sure the fields we don't use are zeroed */
454 	if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags)))
455 		return -EINVAL;
456 
457 	/* flags is allowed */
458 
459 	if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags),
460 			   sizeof(struct bpf_flow_keys)))
461 		return -EINVAL;
462 
463 	return 0;
464 }
465 
466 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
467 				     const union bpf_attr *kattr,
468 				     union bpf_attr __user *uattr)
469 {
470 	u32 size = kattr->test.data_size_in;
471 	struct bpf_flow_dissector ctx = {};
472 	u32 repeat = kattr->test.repeat;
473 	struct bpf_flow_keys *user_ctx;
474 	struct bpf_flow_keys flow_keys;
475 	u64 time_start, time_spent = 0;
476 	const struct ethhdr *eth;
477 	unsigned int flags = 0;
478 	u32 retval, duration;
479 	void *data;
480 	int ret;
481 	u32 i;
482 
483 	if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
484 		return -EINVAL;
485 
486 	if (size < ETH_HLEN)
487 		return -EINVAL;
488 
489 	data = bpf_test_init(kattr, size, 0, 0);
490 	if (IS_ERR(data))
491 		return PTR_ERR(data);
492 
493 	eth = (struct ethhdr *)data;
494 
495 	if (!repeat)
496 		repeat = 1;
497 
498 	user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys));
499 	if (IS_ERR(user_ctx)) {
500 		kfree(data);
501 		return PTR_ERR(user_ctx);
502 	}
503 	if (user_ctx) {
504 		ret = verify_user_bpf_flow_keys(user_ctx);
505 		if (ret)
506 			goto out;
507 		flags = user_ctx->flags;
508 	}
509 
510 	ctx.flow_keys = &flow_keys;
511 	ctx.data = data;
512 	ctx.data_end = (__u8 *)data + size;
513 
514 	rcu_read_lock();
515 	preempt_disable();
516 	time_start = ktime_get_ns();
517 	for (i = 0; i < repeat; i++) {
518 		retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN,
519 					  size, flags);
520 
521 		if (signal_pending(current)) {
522 			preempt_enable();
523 			rcu_read_unlock();
524 
525 			ret = -EINTR;
526 			goto out;
527 		}
528 
529 		if (need_resched()) {
530 			time_spent += ktime_get_ns() - time_start;
531 			preempt_enable();
532 			rcu_read_unlock();
533 
534 			cond_resched();
535 
536 			rcu_read_lock();
537 			preempt_disable();
538 			time_start = ktime_get_ns();
539 		}
540 	}
541 	time_spent += ktime_get_ns() - time_start;
542 	preempt_enable();
543 	rcu_read_unlock();
544 
545 	do_div(time_spent, repeat);
546 	duration = time_spent > U32_MAX ? U32_MAX : (u32)time_spent;
547 
548 	ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
549 			      retval, duration);
550 	if (!ret)
551 		ret = bpf_ctx_finish(kattr, uattr, user_ctx,
552 				     sizeof(struct bpf_flow_keys));
553 
554 out:
555 	kfree(user_ctx);
556 	kfree(data);
557 	return ret;
558 }
559