xref: /linux/net/bpf/test_run.c (revision eb63cfcd2ee8ec3805f6881f43341f589c3d2278)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
3  */
4 #include <linux/bpf.h>
5 #include <linux/btf.h>
6 #include <linux/btf_ids.h>
7 #include <linux/slab.h>
8 #include <linux/vmalloc.h>
9 #include <linux/etherdevice.h>
10 #include <linux/filter.h>
11 #include <linux/rcupdate_trace.h>
12 #include <linux/sched/signal.h>
13 #include <net/bpf_sk_storage.h>
14 #include <net/sock.h>
15 #include <net/tcp.h>
16 #include <net/net_namespace.h>
17 #include <linux/error-injection.h>
18 #include <linux/smp.h>
19 #include <linux/sock_diag.h>
20 #include <net/xdp.h>
21 
22 #define CREATE_TRACE_POINTS
23 #include <trace/events/bpf_test_run.h>
24 
25 struct bpf_test_timer {
26 	enum { NO_PREEMPT, NO_MIGRATE } mode;
27 	u32 i;
28 	u64 time_start, time_spent;
29 };
30 
31 static void bpf_test_timer_enter(struct bpf_test_timer *t)
32 	__acquires(rcu)
33 {
34 	rcu_read_lock();
35 	if (t->mode == NO_PREEMPT)
36 		preempt_disable();
37 	else
38 		migrate_disable();
39 
40 	t->time_start = ktime_get_ns();
41 }
42 
43 static void bpf_test_timer_leave(struct bpf_test_timer *t)
44 	__releases(rcu)
45 {
46 	t->time_start = 0;
47 
48 	if (t->mode == NO_PREEMPT)
49 		preempt_enable();
50 	else
51 		migrate_enable();
52 	rcu_read_unlock();
53 }
54 
55 static bool bpf_test_timer_continue(struct bpf_test_timer *t, u32 repeat, int *err, u32 *duration)
56 	__must_hold(rcu)
57 {
58 	t->i++;
59 	if (t->i >= repeat) {
60 		/* We're done. */
61 		t->time_spent += ktime_get_ns() - t->time_start;
62 		do_div(t->time_spent, t->i);
63 		*duration = t->time_spent > U32_MAX ? U32_MAX : (u32)t->time_spent;
64 		*err = 0;
65 		goto reset;
66 	}
67 
68 	if (signal_pending(current)) {
69 		/* During iteration: we've been cancelled, abort. */
70 		*err = -EINTR;
71 		goto reset;
72 	}
73 
74 	if (need_resched()) {
75 		/* During iteration: we need to reschedule between runs. */
76 		t->time_spent += ktime_get_ns() - t->time_start;
77 		bpf_test_timer_leave(t);
78 		cond_resched();
79 		bpf_test_timer_enter(t);
80 	}
81 
82 	/* Do another round. */
83 	return true;
84 
85 reset:
86 	t->i = 0;
87 	return false;
88 }
89 
90 static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
91 			u32 *retval, u32 *time, bool xdp)
92 {
93 	struct bpf_prog_array_item item = {.prog = prog};
94 	struct bpf_run_ctx *old_ctx;
95 	struct bpf_cg_run_ctx run_ctx;
96 	struct bpf_test_timer t = { NO_MIGRATE };
97 	enum bpf_cgroup_storage_type stype;
98 	int ret;
99 
100 	for_each_cgroup_storage_type(stype) {
101 		item.cgroup_storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
102 		if (IS_ERR(item.cgroup_storage[stype])) {
103 			item.cgroup_storage[stype] = NULL;
104 			for_each_cgroup_storage_type(stype)
105 				bpf_cgroup_storage_free(item.cgroup_storage[stype]);
106 			return -ENOMEM;
107 		}
108 	}
109 
110 	if (!repeat)
111 		repeat = 1;
112 
113 	bpf_test_timer_enter(&t);
114 	old_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
115 	do {
116 		run_ctx.prog_item = &item;
117 		if (xdp)
118 			*retval = bpf_prog_run_xdp(prog, ctx);
119 		else
120 			*retval = bpf_prog_run(prog, ctx);
121 	} while (bpf_test_timer_continue(&t, repeat, &ret, time));
122 	bpf_reset_run_ctx(old_ctx);
123 	bpf_test_timer_leave(&t);
124 
125 	for_each_cgroup_storage_type(stype)
126 		bpf_cgroup_storage_free(item.cgroup_storage[stype]);
127 
128 	return ret;
129 }
130 
131 static int bpf_test_finish(const union bpf_attr *kattr,
132 			   union bpf_attr __user *uattr, const void *data,
133 			   u32 size, u32 retval, u32 duration)
134 {
135 	void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
136 	int err = -EFAULT;
137 	u32 copy_size = size;
138 
139 	/* Clamp copy if the user has provided a size hint, but copy the full
140 	 * buffer if not to retain old behaviour.
141 	 */
142 	if (kattr->test.data_size_out &&
143 	    copy_size > kattr->test.data_size_out) {
144 		copy_size = kattr->test.data_size_out;
145 		err = -ENOSPC;
146 	}
147 
148 	if (data_out && copy_to_user(data_out, data, copy_size))
149 		goto out;
150 	if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
151 		goto out;
152 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
153 		goto out;
154 	if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
155 		goto out;
156 	if (err != -ENOSPC)
157 		err = 0;
158 out:
159 	trace_bpf_test_finish(&err);
160 	return err;
161 }
162 
163 /* Integer types of various sizes and pointer combinations cover variety of
164  * architecture dependent calling conventions. 7+ can be supported in the
165  * future.
166  */
167 __diag_push();
168 __diag_ignore(GCC, 8, "-Wmissing-prototypes",
169 	      "Global functions as their definitions will be in vmlinux BTF");
170 int noinline bpf_fentry_test1(int a)
171 {
172 	return a + 1;
173 }
174 
175 int noinline bpf_fentry_test2(int a, u64 b)
176 {
177 	return a + b;
178 }
179 
180 int noinline bpf_fentry_test3(char a, int b, u64 c)
181 {
182 	return a + b + c;
183 }
184 
185 int noinline bpf_fentry_test4(void *a, char b, int c, u64 d)
186 {
187 	return (long)a + b + c + d;
188 }
189 
190 int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e)
191 {
192 	return a + (long)b + c + d + e;
193 }
194 
195 int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f)
196 {
197 	return a + (long)b + c + d + (long)e + f;
198 }
199 
200 struct bpf_fentry_test_t {
201 	struct bpf_fentry_test_t *a;
202 };
203 
204 int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg)
205 {
206 	return (long)arg;
207 }
208 
209 int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg)
210 {
211 	return (long)arg->a;
212 }
213 
214 int noinline bpf_modify_return_test(int a, int *b)
215 {
216 	*b += 1;
217 	return a + *b;
218 }
219 
220 u64 noinline bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d)
221 {
222 	return a + b + c + d;
223 }
224 
225 int noinline bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b)
226 {
227 	return a + b;
228 }
229 
230 struct sock * noinline bpf_kfunc_call_test3(struct sock *sk)
231 {
232 	return sk;
233 }
234 
235 __diag_pop();
236 
237 ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO);
238 
239 BTF_SET_START(test_sk_kfunc_ids)
240 BTF_ID(func, bpf_kfunc_call_test1)
241 BTF_ID(func, bpf_kfunc_call_test2)
242 BTF_ID(func, bpf_kfunc_call_test3)
243 BTF_SET_END(test_sk_kfunc_ids)
244 
245 bool bpf_prog_test_check_kfunc_call(u32 kfunc_id, struct module *owner)
246 {
247 	if (btf_id_set_contains(&test_sk_kfunc_ids, kfunc_id))
248 		return true;
249 	return bpf_check_mod_kfunc_call(&prog_test_kfunc_list, kfunc_id, owner);
250 }
251 
252 static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
253 			   u32 headroom, u32 tailroom)
254 {
255 	void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
256 	u32 user_size = kattr->test.data_size_in;
257 	void *data;
258 
259 	if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom)
260 		return ERR_PTR(-EINVAL);
261 
262 	if (user_size > size)
263 		return ERR_PTR(-EMSGSIZE);
264 
265 	data = kzalloc(size + headroom + tailroom, GFP_USER);
266 	if (!data)
267 		return ERR_PTR(-ENOMEM);
268 
269 	if (copy_from_user(data + headroom, data_in, user_size)) {
270 		kfree(data);
271 		return ERR_PTR(-EFAULT);
272 	}
273 
274 	return data;
275 }
276 
277 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
278 			      const union bpf_attr *kattr,
279 			      union bpf_attr __user *uattr)
280 {
281 	struct bpf_fentry_test_t arg = {};
282 	u16 side_effect = 0, ret = 0;
283 	int b = 2, err = -EFAULT;
284 	u32 retval = 0;
285 
286 	if (kattr->test.flags || kattr->test.cpu)
287 		return -EINVAL;
288 
289 	switch (prog->expected_attach_type) {
290 	case BPF_TRACE_FENTRY:
291 	case BPF_TRACE_FEXIT:
292 		if (bpf_fentry_test1(1) != 2 ||
293 		    bpf_fentry_test2(2, 3) != 5 ||
294 		    bpf_fentry_test3(4, 5, 6) != 15 ||
295 		    bpf_fentry_test4((void *)7, 8, 9, 10) != 34 ||
296 		    bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 ||
297 		    bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 ||
298 		    bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 ||
299 		    bpf_fentry_test8(&arg) != 0)
300 			goto out;
301 		break;
302 	case BPF_MODIFY_RETURN:
303 		ret = bpf_modify_return_test(1, &b);
304 		if (b != 2)
305 			side_effect = 1;
306 		break;
307 	default:
308 		goto out;
309 	}
310 
311 	retval = ((u32)side_effect << 16) | ret;
312 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
313 		goto out;
314 
315 	err = 0;
316 out:
317 	trace_bpf_test_finish(&err);
318 	return err;
319 }
320 
321 struct bpf_raw_tp_test_run_info {
322 	struct bpf_prog *prog;
323 	void *ctx;
324 	u32 retval;
325 };
326 
327 static void
328 __bpf_prog_test_run_raw_tp(void *data)
329 {
330 	struct bpf_raw_tp_test_run_info *info = data;
331 
332 	rcu_read_lock();
333 	info->retval = bpf_prog_run(info->prog, info->ctx);
334 	rcu_read_unlock();
335 }
336 
337 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
338 			     const union bpf_attr *kattr,
339 			     union bpf_attr __user *uattr)
340 {
341 	void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
342 	__u32 ctx_size_in = kattr->test.ctx_size_in;
343 	struct bpf_raw_tp_test_run_info info;
344 	int cpu = kattr->test.cpu, err = 0;
345 	int current_cpu;
346 
347 	/* doesn't support data_in/out, ctx_out, duration, or repeat */
348 	if (kattr->test.data_in || kattr->test.data_out ||
349 	    kattr->test.ctx_out || kattr->test.duration ||
350 	    kattr->test.repeat)
351 		return -EINVAL;
352 
353 	if (ctx_size_in < prog->aux->max_ctx_offset ||
354 	    ctx_size_in > MAX_BPF_FUNC_ARGS * sizeof(u64))
355 		return -EINVAL;
356 
357 	if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0)
358 		return -EINVAL;
359 
360 	if (ctx_size_in) {
361 		info.ctx = kzalloc(ctx_size_in, GFP_USER);
362 		if (!info.ctx)
363 			return -ENOMEM;
364 		if (copy_from_user(info.ctx, ctx_in, ctx_size_in)) {
365 			err = -EFAULT;
366 			goto out;
367 		}
368 	} else {
369 		info.ctx = NULL;
370 	}
371 
372 	info.prog = prog;
373 
374 	current_cpu = get_cpu();
375 	if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 ||
376 	    cpu == current_cpu) {
377 		__bpf_prog_test_run_raw_tp(&info);
378 	} else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {
379 		/* smp_call_function_single() also checks cpu_online()
380 		 * after csd_lock(). However, since cpu is from user
381 		 * space, let's do an extra quick check to filter out
382 		 * invalid value before smp_call_function_single().
383 		 */
384 		err = -ENXIO;
385 	} else {
386 		err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp,
387 					       &info, 1);
388 	}
389 	put_cpu();
390 
391 	if (!err &&
392 	    copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32)))
393 		err = -EFAULT;
394 
395 out:
396 	kfree(info.ctx);
397 	return err;
398 }
399 
400 static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size)
401 {
402 	void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in);
403 	void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
404 	u32 size = kattr->test.ctx_size_in;
405 	void *data;
406 	int err;
407 
408 	if (!data_in && !data_out)
409 		return NULL;
410 
411 	data = kzalloc(max_size, GFP_USER);
412 	if (!data)
413 		return ERR_PTR(-ENOMEM);
414 
415 	if (data_in) {
416 		err = bpf_check_uarg_tail_zero(USER_BPFPTR(data_in), max_size, size);
417 		if (err) {
418 			kfree(data);
419 			return ERR_PTR(err);
420 		}
421 
422 		size = min_t(u32, max_size, size);
423 		if (copy_from_user(data, data_in, size)) {
424 			kfree(data);
425 			return ERR_PTR(-EFAULT);
426 		}
427 	}
428 	return data;
429 }
430 
431 static int bpf_ctx_finish(const union bpf_attr *kattr,
432 			  union bpf_attr __user *uattr, const void *data,
433 			  u32 size)
434 {
435 	void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
436 	int err = -EFAULT;
437 	u32 copy_size = size;
438 
439 	if (!data || !data_out)
440 		return 0;
441 
442 	if (copy_size > kattr->test.ctx_size_out) {
443 		copy_size = kattr->test.ctx_size_out;
444 		err = -ENOSPC;
445 	}
446 
447 	if (copy_to_user(data_out, data, copy_size))
448 		goto out;
449 	if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size)))
450 		goto out;
451 	if (err != -ENOSPC)
452 		err = 0;
453 out:
454 	return err;
455 }
456 
457 /**
458  * range_is_zero - test whether buffer is initialized
459  * @buf: buffer to check
460  * @from: check from this position
461  * @to: check up until (excluding) this position
462  *
463  * This function returns true if the there is a non-zero byte
464  * in the buf in the range [from,to).
465  */
466 static inline bool range_is_zero(void *buf, size_t from, size_t to)
467 {
468 	return !memchr_inv((u8 *)buf + from, 0, to - from);
469 }
470 
471 static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
472 {
473 	struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
474 
475 	if (!__skb)
476 		return 0;
477 
478 	/* make sure the fields we don't use are zeroed */
479 	if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark)))
480 		return -EINVAL;
481 
482 	/* mark is allowed */
483 
484 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark),
485 			   offsetof(struct __sk_buff, priority)))
486 		return -EINVAL;
487 
488 	/* priority is allowed */
489 	/* ingress_ifindex is allowed */
490 	/* ifindex is allowed */
491 
492 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex),
493 			   offsetof(struct __sk_buff, cb)))
494 		return -EINVAL;
495 
496 	/* cb is allowed */
497 
498 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb),
499 			   offsetof(struct __sk_buff, tstamp)))
500 		return -EINVAL;
501 
502 	/* tstamp is allowed */
503 	/* wire_len is allowed */
504 	/* gso_segs is allowed */
505 
506 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs),
507 			   offsetof(struct __sk_buff, gso_size)))
508 		return -EINVAL;
509 
510 	/* gso_size is allowed */
511 
512 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size),
513 			   offsetof(struct __sk_buff, hwtstamp)))
514 		return -EINVAL;
515 
516 	/* hwtstamp is allowed */
517 
518 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, hwtstamp),
519 			   sizeof(struct __sk_buff)))
520 		return -EINVAL;
521 
522 	skb->mark = __skb->mark;
523 	skb->priority = __skb->priority;
524 	skb->skb_iif = __skb->ingress_ifindex;
525 	skb->tstamp = __skb->tstamp;
526 	memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN);
527 
528 	if (__skb->wire_len == 0) {
529 		cb->pkt_len = skb->len;
530 	} else {
531 		if (__skb->wire_len < skb->len ||
532 		    __skb->wire_len > GSO_MAX_SIZE)
533 			return -EINVAL;
534 		cb->pkt_len = __skb->wire_len;
535 	}
536 
537 	if (__skb->gso_segs > GSO_MAX_SEGS)
538 		return -EINVAL;
539 	skb_shinfo(skb)->gso_segs = __skb->gso_segs;
540 	skb_shinfo(skb)->gso_size = __skb->gso_size;
541 	skb_shinfo(skb)->hwtstamps.hwtstamp = __skb->hwtstamp;
542 
543 	return 0;
544 }
545 
546 static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb)
547 {
548 	struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
549 
550 	if (!__skb)
551 		return;
552 
553 	__skb->mark = skb->mark;
554 	__skb->priority = skb->priority;
555 	__skb->ingress_ifindex = skb->skb_iif;
556 	__skb->ifindex = skb->dev->ifindex;
557 	__skb->tstamp = skb->tstamp;
558 	memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN);
559 	__skb->wire_len = cb->pkt_len;
560 	__skb->gso_segs = skb_shinfo(skb)->gso_segs;
561 	__skb->hwtstamp = skb_shinfo(skb)->hwtstamps.hwtstamp;
562 }
563 
564 static struct proto bpf_dummy_proto = {
565 	.name   = "bpf_dummy",
566 	.owner  = THIS_MODULE,
567 	.obj_size = sizeof(struct sock),
568 };
569 
570 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
571 			  union bpf_attr __user *uattr)
572 {
573 	bool is_l2 = false, is_direct_pkt_access = false;
574 	struct net *net = current->nsproxy->net_ns;
575 	struct net_device *dev = net->loopback_dev;
576 	u32 size = kattr->test.data_size_in;
577 	u32 repeat = kattr->test.repeat;
578 	struct __sk_buff *ctx = NULL;
579 	u32 retval, duration;
580 	int hh_len = ETH_HLEN;
581 	struct sk_buff *skb;
582 	struct sock *sk;
583 	void *data;
584 	int ret;
585 
586 	if (kattr->test.flags || kattr->test.cpu)
587 		return -EINVAL;
588 
589 	data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
590 			     SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
591 	if (IS_ERR(data))
592 		return PTR_ERR(data);
593 
594 	ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
595 	if (IS_ERR(ctx)) {
596 		kfree(data);
597 		return PTR_ERR(ctx);
598 	}
599 
600 	switch (prog->type) {
601 	case BPF_PROG_TYPE_SCHED_CLS:
602 	case BPF_PROG_TYPE_SCHED_ACT:
603 		is_l2 = true;
604 		fallthrough;
605 	case BPF_PROG_TYPE_LWT_IN:
606 	case BPF_PROG_TYPE_LWT_OUT:
607 	case BPF_PROG_TYPE_LWT_XMIT:
608 		is_direct_pkt_access = true;
609 		break;
610 	default:
611 		break;
612 	}
613 
614 	sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
615 	if (!sk) {
616 		kfree(data);
617 		kfree(ctx);
618 		return -ENOMEM;
619 	}
620 	sock_init_data(NULL, sk);
621 
622 	skb = build_skb(data, 0);
623 	if (!skb) {
624 		kfree(data);
625 		kfree(ctx);
626 		sk_free(sk);
627 		return -ENOMEM;
628 	}
629 	skb->sk = sk;
630 
631 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
632 	__skb_put(skb, size);
633 	if (ctx && ctx->ifindex > 1) {
634 		dev = dev_get_by_index(net, ctx->ifindex);
635 		if (!dev) {
636 			ret = -ENODEV;
637 			goto out;
638 		}
639 	}
640 	skb->protocol = eth_type_trans(skb, dev);
641 	skb_reset_network_header(skb);
642 
643 	switch (skb->protocol) {
644 	case htons(ETH_P_IP):
645 		sk->sk_family = AF_INET;
646 		if (sizeof(struct iphdr) <= skb_headlen(skb)) {
647 			sk->sk_rcv_saddr = ip_hdr(skb)->saddr;
648 			sk->sk_daddr = ip_hdr(skb)->daddr;
649 		}
650 		break;
651 #if IS_ENABLED(CONFIG_IPV6)
652 	case htons(ETH_P_IPV6):
653 		sk->sk_family = AF_INET6;
654 		if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) {
655 			sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr;
656 			sk->sk_v6_daddr = ipv6_hdr(skb)->daddr;
657 		}
658 		break;
659 #endif
660 	default:
661 		break;
662 	}
663 
664 	if (is_l2)
665 		__skb_push(skb, hh_len);
666 	if (is_direct_pkt_access)
667 		bpf_compute_data_pointers(skb);
668 	ret = convert___skb_to_skb(skb, ctx);
669 	if (ret)
670 		goto out;
671 	ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false);
672 	if (ret)
673 		goto out;
674 	if (!is_l2) {
675 		if (skb_headroom(skb) < hh_len) {
676 			int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
677 
678 			if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
679 				ret = -ENOMEM;
680 				goto out;
681 			}
682 		}
683 		memset(__skb_push(skb, hh_len), 0, hh_len);
684 	}
685 	convert_skb_to___skb(skb, ctx);
686 
687 	size = skb->len;
688 	/* bpf program can never convert linear skb to non-linear */
689 	if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
690 		size = skb_headlen(skb);
691 	ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration);
692 	if (!ret)
693 		ret = bpf_ctx_finish(kattr, uattr, ctx,
694 				     sizeof(struct __sk_buff));
695 out:
696 	if (dev && dev != net->loopback_dev)
697 		dev_put(dev);
698 	kfree_skb(skb);
699 	sk_free(sk);
700 	kfree(ctx);
701 	return ret;
702 }
703 
704 static int xdp_convert_md_to_buff(struct xdp_md *xdp_md, struct xdp_buff *xdp)
705 {
706 	unsigned int ingress_ifindex, rx_queue_index;
707 	struct netdev_rx_queue *rxqueue;
708 	struct net_device *device;
709 
710 	if (!xdp_md)
711 		return 0;
712 
713 	if (xdp_md->egress_ifindex != 0)
714 		return -EINVAL;
715 
716 	ingress_ifindex = xdp_md->ingress_ifindex;
717 	rx_queue_index = xdp_md->rx_queue_index;
718 
719 	if (!ingress_ifindex && rx_queue_index)
720 		return -EINVAL;
721 
722 	if (ingress_ifindex) {
723 		device = dev_get_by_index(current->nsproxy->net_ns,
724 					  ingress_ifindex);
725 		if (!device)
726 			return -ENODEV;
727 
728 		if (rx_queue_index >= device->real_num_rx_queues)
729 			goto free_dev;
730 
731 		rxqueue = __netif_get_rx_queue(device, rx_queue_index);
732 
733 		if (!xdp_rxq_info_is_reg(&rxqueue->xdp_rxq))
734 			goto free_dev;
735 
736 		xdp->rxq = &rxqueue->xdp_rxq;
737 		/* The device is now tracked in the xdp->rxq for later
738 		 * dev_put()
739 		 */
740 	}
741 
742 	xdp->data = xdp->data_meta + xdp_md->data;
743 	return 0;
744 
745 free_dev:
746 	dev_put(device);
747 	return -EINVAL;
748 }
749 
750 static void xdp_convert_buff_to_md(struct xdp_buff *xdp, struct xdp_md *xdp_md)
751 {
752 	if (!xdp_md)
753 		return;
754 
755 	xdp_md->data = xdp->data - xdp->data_meta;
756 	xdp_md->data_end = xdp->data_end - xdp->data_meta;
757 
758 	if (xdp_md->ingress_ifindex)
759 		dev_put(xdp->rxq->dev);
760 }
761 
762 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
763 			  union bpf_attr __user *uattr)
764 {
765 	u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
766 	u32 headroom = XDP_PACKET_HEADROOM;
767 	u32 size = kattr->test.data_size_in;
768 	u32 repeat = kattr->test.repeat;
769 	struct netdev_rx_queue *rxqueue;
770 	struct xdp_buff xdp = {};
771 	u32 retval, duration;
772 	struct xdp_md *ctx;
773 	u32 max_data_sz;
774 	void *data;
775 	int ret = -EINVAL;
776 
777 	if (prog->expected_attach_type == BPF_XDP_DEVMAP ||
778 	    prog->expected_attach_type == BPF_XDP_CPUMAP)
779 		return -EINVAL;
780 
781 	ctx = bpf_ctx_init(kattr, sizeof(struct xdp_md));
782 	if (IS_ERR(ctx))
783 		return PTR_ERR(ctx);
784 
785 	if (ctx) {
786 		/* There can't be user provided data before the meta data */
787 		if (ctx->data_meta || ctx->data_end != size ||
788 		    ctx->data > ctx->data_end ||
789 		    unlikely(xdp_metalen_invalid(ctx->data)))
790 			goto free_ctx;
791 		/* Meta data is allocated from the headroom */
792 		headroom -= ctx->data;
793 	}
794 
795 	/* XDP have extra tailroom as (most) drivers use full page */
796 	max_data_sz = 4096 - headroom - tailroom;
797 
798 	data = bpf_test_init(kattr, max_data_sz, headroom, tailroom);
799 	if (IS_ERR(data)) {
800 		ret = PTR_ERR(data);
801 		goto free_ctx;
802 	}
803 
804 	rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
805 	xdp_init_buff(&xdp, headroom + max_data_sz + tailroom,
806 		      &rxqueue->xdp_rxq);
807 	xdp_prepare_buff(&xdp, data, headroom, size, true);
808 
809 	ret = xdp_convert_md_to_buff(ctx, &xdp);
810 	if (ret)
811 		goto free_data;
812 
813 	if (repeat > 1)
814 		bpf_prog_change_xdp(NULL, prog);
815 	ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true);
816 	/* We convert the xdp_buff back to an xdp_md before checking the return
817 	 * code so the reference count of any held netdevice will be decremented
818 	 * even if the test run failed.
819 	 */
820 	xdp_convert_buff_to_md(&xdp, ctx);
821 	if (ret)
822 		goto out;
823 
824 	if (xdp.data_meta != data + headroom ||
825 	    xdp.data_end != xdp.data_meta + size)
826 		size = xdp.data_end - xdp.data_meta;
827 
828 	ret = bpf_test_finish(kattr, uattr, xdp.data_meta, size, retval,
829 			      duration);
830 	if (!ret)
831 		ret = bpf_ctx_finish(kattr, uattr, ctx,
832 				     sizeof(struct xdp_md));
833 
834 out:
835 	if (repeat > 1)
836 		bpf_prog_change_xdp(prog, NULL);
837 free_data:
838 	kfree(data);
839 free_ctx:
840 	kfree(ctx);
841 	return ret;
842 }
843 
844 static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx)
845 {
846 	/* make sure the fields we don't use are zeroed */
847 	if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags)))
848 		return -EINVAL;
849 
850 	/* flags is allowed */
851 
852 	if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags),
853 			   sizeof(struct bpf_flow_keys)))
854 		return -EINVAL;
855 
856 	return 0;
857 }
858 
859 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
860 				     const union bpf_attr *kattr,
861 				     union bpf_attr __user *uattr)
862 {
863 	struct bpf_test_timer t = { NO_PREEMPT };
864 	u32 size = kattr->test.data_size_in;
865 	struct bpf_flow_dissector ctx = {};
866 	u32 repeat = kattr->test.repeat;
867 	struct bpf_flow_keys *user_ctx;
868 	struct bpf_flow_keys flow_keys;
869 	const struct ethhdr *eth;
870 	unsigned int flags = 0;
871 	u32 retval, duration;
872 	void *data;
873 	int ret;
874 
875 	if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
876 		return -EINVAL;
877 
878 	if (kattr->test.flags || kattr->test.cpu)
879 		return -EINVAL;
880 
881 	if (size < ETH_HLEN)
882 		return -EINVAL;
883 
884 	data = bpf_test_init(kattr, size, 0, 0);
885 	if (IS_ERR(data))
886 		return PTR_ERR(data);
887 
888 	eth = (struct ethhdr *)data;
889 
890 	if (!repeat)
891 		repeat = 1;
892 
893 	user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys));
894 	if (IS_ERR(user_ctx)) {
895 		kfree(data);
896 		return PTR_ERR(user_ctx);
897 	}
898 	if (user_ctx) {
899 		ret = verify_user_bpf_flow_keys(user_ctx);
900 		if (ret)
901 			goto out;
902 		flags = user_ctx->flags;
903 	}
904 
905 	ctx.flow_keys = &flow_keys;
906 	ctx.data = data;
907 	ctx.data_end = (__u8 *)data + size;
908 
909 	bpf_test_timer_enter(&t);
910 	do {
911 		retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN,
912 					  size, flags);
913 	} while (bpf_test_timer_continue(&t, repeat, &ret, &duration));
914 	bpf_test_timer_leave(&t);
915 
916 	if (ret < 0)
917 		goto out;
918 
919 	ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
920 			      retval, duration);
921 	if (!ret)
922 		ret = bpf_ctx_finish(kattr, uattr, user_ctx,
923 				     sizeof(struct bpf_flow_keys));
924 
925 out:
926 	kfree(user_ctx);
927 	kfree(data);
928 	return ret;
929 }
930 
931 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr,
932 				union bpf_attr __user *uattr)
933 {
934 	struct bpf_test_timer t = { NO_PREEMPT };
935 	struct bpf_prog_array *progs = NULL;
936 	struct bpf_sk_lookup_kern ctx = {};
937 	u32 repeat = kattr->test.repeat;
938 	struct bpf_sk_lookup *user_ctx;
939 	u32 retval, duration;
940 	int ret = -EINVAL;
941 
942 	if (prog->type != BPF_PROG_TYPE_SK_LOOKUP)
943 		return -EINVAL;
944 
945 	if (kattr->test.flags || kattr->test.cpu)
946 		return -EINVAL;
947 
948 	if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out ||
949 	    kattr->test.data_size_out)
950 		return -EINVAL;
951 
952 	if (!repeat)
953 		repeat = 1;
954 
955 	user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx));
956 	if (IS_ERR(user_ctx))
957 		return PTR_ERR(user_ctx);
958 
959 	if (!user_ctx)
960 		return -EINVAL;
961 
962 	if (user_ctx->sk)
963 		goto out;
964 
965 	if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx)))
966 		goto out;
967 
968 	if (user_ctx->local_port > U16_MAX || user_ctx->remote_port > U16_MAX) {
969 		ret = -ERANGE;
970 		goto out;
971 	}
972 
973 	ctx.family = (u16)user_ctx->family;
974 	ctx.protocol = (u16)user_ctx->protocol;
975 	ctx.dport = (u16)user_ctx->local_port;
976 	ctx.sport = (__force __be16)user_ctx->remote_port;
977 
978 	switch (ctx.family) {
979 	case AF_INET:
980 		ctx.v4.daddr = (__force __be32)user_ctx->local_ip4;
981 		ctx.v4.saddr = (__force __be32)user_ctx->remote_ip4;
982 		break;
983 
984 #if IS_ENABLED(CONFIG_IPV6)
985 	case AF_INET6:
986 		ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6;
987 		ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6;
988 		break;
989 #endif
990 
991 	default:
992 		ret = -EAFNOSUPPORT;
993 		goto out;
994 	}
995 
996 	progs = bpf_prog_array_alloc(1, GFP_KERNEL);
997 	if (!progs) {
998 		ret = -ENOMEM;
999 		goto out;
1000 	}
1001 
1002 	progs->items[0].prog = prog;
1003 
1004 	bpf_test_timer_enter(&t);
1005 	do {
1006 		ctx.selected_sk = NULL;
1007 		retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, bpf_prog_run);
1008 	} while (bpf_test_timer_continue(&t, repeat, &ret, &duration));
1009 	bpf_test_timer_leave(&t);
1010 
1011 	if (ret < 0)
1012 		goto out;
1013 
1014 	user_ctx->cookie = 0;
1015 	if (ctx.selected_sk) {
1016 		if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) {
1017 			ret = -EOPNOTSUPP;
1018 			goto out;
1019 		}
1020 
1021 		user_ctx->cookie = sock_gen_cookie(ctx.selected_sk);
1022 	}
1023 
1024 	ret = bpf_test_finish(kattr, uattr, NULL, 0, retval, duration);
1025 	if (!ret)
1026 		ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx));
1027 
1028 out:
1029 	bpf_prog_array_free(progs);
1030 	kfree(user_ctx);
1031 	return ret;
1032 }
1033 
1034 int bpf_prog_test_run_syscall(struct bpf_prog *prog,
1035 			      const union bpf_attr *kattr,
1036 			      union bpf_attr __user *uattr)
1037 {
1038 	void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
1039 	__u32 ctx_size_in = kattr->test.ctx_size_in;
1040 	void *ctx = NULL;
1041 	u32 retval;
1042 	int err = 0;
1043 
1044 	/* doesn't support data_in/out, ctx_out, duration, or repeat or flags */
1045 	if (kattr->test.data_in || kattr->test.data_out ||
1046 	    kattr->test.ctx_out || kattr->test.duration ||
1047 	    kattr->test.repeat || kattr->test.flags)
1048 		return -EINVAL;
1049 
1050 	if (ctx_size_in < prog->aux->max_ctx_offset ||
1051 	    ctx_size_in > U16_MAX)
1052 		return -EINVAL;
1053 
1054 	if (ctx_size_in) {
1055 		ctx = kzalloc(ctx_size_in, GFP_USER);
1056 		if (!ctx)
1057 			return -ENOMEM;
1058 		if (copy_from_user(ctx, ctx_in, ctx_size_in)) {
1059 			err = -EFAULT;
1060 			goto out;
1061 		}
1062 	}
1063 
1064 	rcu_read_lock_trace();
1065 	retval = bpf_prog_run_pin_on_cpu(prog, ctx);
1066 	rcu_read_unlock_trace();
1067 
1068 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) {
1069 		err = -EFAULT;
1070 		goto out;
1071 	}
1072 	if (ctx_size_in)
1073 		if (copy_to_user(ctx_in, ctx, ctx_size_in))
1074 			err = -EFAULT;
1075 out:
1076 	kfree(ctx);
1077 	return err;
1078 }
1079