xref: /openbmc/linux/net/bpf/test_run.c (revision d37cf9b63113f13d742713881ce691fc615d8b3b)
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/init.h>
9 #include <linux/vmalloc.h>
10 #include <linux/etherdevice.h>
11 #include <linux/filter.h>
12 #include <linux/rcupdate_trace.h>
13 #include <linux/sched/signal.h>
14 #include <net/bpf_sk_storage.h>
15 #include <net/sock.h>
16 #include <net/tcp.h>
17 #include <net/net_namespace.h>
18 #include <net/page_pool/helpers.h>
19 #include <linux/error-injection.h>
20 #include <linux/smp.h>
21 #include <linux/sock_diag.h>
22 #include <linux/netfilter.h>
23 #include <net/netdev_rx_queue.h>
24 #include <net/xdp.h>
25 #include <net/netfilter/nf_bpf_link.h>
26 
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/bpf_test_run.h>
29 
30 struct bpf_test_timer {
31 	enum { NO_PREEMPT, NO_MIGRATE } mode;
32 	u32 i;
33 	u64 time_start, time_spent;
34 };
35 
bpf_test_timer_enter(struct bpf_test_timer * t)36 static void bpf_test_timer_enter(struct bpf_test_timer *t)
37 	__acquires(rcu)
38 {
39 	rcu_read_lock();
40 	if (t->mode == NO_PREEMPT)
41 		preempt_disable();
42 	else
43 		migrate_disable();
44 
45 	t->time_start = ktime_get_ns();
46 }
47 
bpf_test_timer_leave(struct bpf_test_timer * t)48 static void bpf_test_timer_leave(struct bpf_test_timer *t)
49 	__releases(rcu)
50 {
51 	t->time_start = 0;
52 
53 	if (t->mode == NO_PREEMPT)
54 		preempt_enable();
55 	else
56 		migrate_enable();
57 	rcu_read_unlock();
58 }
59 
bpf_test_timer_continue(struct bpf_test_timer * t,int iterations,u32 repeat,int * err,u32 * duration)60 static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations,
61 				    u32 repeat, int *err, u32 *duration)
62 	__must_hold(rcu)
63 {
64 	t->i += iterations;
65 	if (t->i >= repeat) {
66 		/* We're done. */
67 		t->time_spent += ktime_get_ns() - t->time_start;
68 		do_div(t->time_spent, t->i);
69 		*duration = t->time_spent > U32_MAX ? U32_MAX : (u32)t->time_spent;
70 		*err = 0;
71 		goto reset;
72 	}
73 
74 	if (signal_pending(current)) {
75 		/* During iteration: we've been cancelled, abort. */
76 		*err = -EINTR;
77 		goto reset;
78 	}
79 
80 	if (need_resched()) {
81 		/* During iteration: we need to reschedule between runs. */
82 		t->time_spent += ktime_get_ns() - t->time_start;
83 		bpf_test_timer_leave(t);
84 		cond_resched();
85 		bpf_test_timer_enter(t);
86 	}
87 
88 	/* Do another round. */
89 	return true;
90 
91 reset:
92 	t->i = 0;
93 	return false;
94 }
95 
96 /* We put this struct at the head of each page with a context and frame
97  * initialised when the page is allocated, so we don't have to do this on each
98  * repetition of the test run.
99  */
100 struct xdp_page_head {
101 	struct xdp_buff orig_ctx;
102 	struct xdp_buff ctx;
103 	union {
104 		/* ::data_hard_start starts here */
105 		DECLARE_FLEX_ARRAY(struct xdp_frame, frame);
106 		DECLARE_FLEX_ARRAY(u8, data);
107 	};
108 };
109 
110 struct xdp_test_data {
111 	struct xdp_buff *orig_ctx;
112 	struct xdp_rxq_info rxq;
113 	struct net_device *dev;
114 	struct page_pool *pp;
115 	struct xdp_frame **frames;
116 	struct sk_buff **skbs;
117 	struct xdp_mem_info mem;
118 	u32 batch_size;
119 	u32 frame_cnt;
120 };
121 
122 /* tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c:%MAX_PKT_SIZE
123  * must be updated accordingly this gets changed, otherwise BPF selftests
124  * will fail.
125  */
126 #define TEST_XDP_FRAME_SIZE (PAGE_SIZE - sizeof(struct xdp_page_head))
127 #define TEST_XDP_MAX_BATCH 256
128 
xdp_test_run_init_page(struct page * page,void * arg)129 static void xdp_test_run_init_page(struct page *page, void *arg)
130 {
131 	struct xdp_page_head *head = phys_to_virt(page_to_phys(page));
132 	struct xdp_buff *new_ctx, *orig_ctx;
133 	u32 headroom = XDP_PACKET_HEADROOM;
134 	struct xdp_test_data *xdp = arg;
135 	size_t frm_len, meta_len;
136 	struct xdp_frame *frm;
137 	void *data;
138 
139 	orig_ctx = xdp->orig_ctx;
140 	frm_len = orig_ctx->data_end - orig_ctx->data_meta;
141 	meta_len = orig_ctx->data - orig_ctx->data_meta;
142 	headroom -= meta_len;
143 
144 	new_ctx = &head->ctx;
145 	frm = head->frame;
146 	data = head->data;
147 	memcpy(data + headroom, orig_ctx->data_meta, frm_len);
148 
149 	xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq);
150 	xdp_prepare_buff(new_ctx, data, headroom, frm_len, true);
151 	new_ctx->data = new_ctx->data_meta + meta_len;
152 
153 	xdp_update_frame_from_buff(new_ctx, frm);
154 	frm->mem = new_ctx->rxq->mem;
155 
156 	memcpy(&head->orig_ctx, new_ctx, sizeof(head->orig_ctx));
157 }
158 
xdp_test_run_setup(struct xdp_test_data * xdp,struct xdp_buff * orig_ctx)159 static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_ctx)
160 {
161 	struct page_pool *pp;
162 	int err = -ENOMEM;
163 	struct page_pool_params pp_params = {
164 		.order = 0,
165 		.flags = 0,
166 		.pool_size = xdp->batch_size,
167 		.nid = NUMA_NO_NODE,
168 		.init_callback = xdp_test_run_init_page,
169 		.init_arg = xdp,
170 	};
171 
172 	xdp->frames = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL);
173 	if (!xdp->frames)
174 		return -ENOMEM;
175 
176 	xdp->skbs = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL);
177 	if (!xdp->skbs)
178 		goto err_skbs;
179 
180 	pp = page_pool_create(&pp_params);
181 	if (IS_ERR(pp)) {
182 		err = PTR_ERR(pp);
183 		goto err_pp;
184 	}
185 
186 	/* will copy 'mem.id' into pp->xdp_mem_id */
187 	err = xdp_reg_mem_model(&xdp->mem, MEM_TYPE_PAGE_POOL, pp);
188 	if (err)
189 		goto err_mmodel;
190 
191 	xdp->pp = pp;
192 
193 	/* We create a 'fake' RXQ referencing the original dev, but with an
194 	 * xdp_mem_info pointing to our page_pool
195 	 */
196 	xdp_rxq_info_reg(&xdp->rxq, orig_ctx->rxq->dev, 0, 0);
197 	xdp->rxq.mem.type = MEM_TYPE_PAGE_POOL;
198 	xdp->rxq.mem.id = pp->xdp_mem_id;
199 	xdp->dev = orig_ctx->rxq->dev;
200 	xdp->orig_ctx = orig_ctx;
201 
202 	return 0;
203 
204 err_mmodel:
205 	page_pool_destroy(pp);
206 err_pp:
207 	kvfree(xdp->skbs);
208 err_skbs:
209 	kvfree(xdp->frames);
210 	return err;
211 }
212 
xdp_test_run_teardown(struct xdp_test_data * xdp)213 static void xdp_test_run_teardown(struct xdp_test_data *xdp)
214 {
215 	xdp_unreg_mem_model(&xdp->mem);
216 	page_pool_destroy(xdp->pp);
217 	kfree(xdp->frames);
218 	kfree(xdp->skbs);
219 }
220 
frame_was_changed(const struct xdp_page_head * head)221 static bool frame_was_changed(const struct xdp_page_head *head)
222 {
223 	/* xdp_scrub_frame() zeroes the data pointer, flags is the last field,
224 	 * i.e. has the highest chances to be overwritten. If those two are
225 	 * untouched, it's most likely safe to skip the context reset.
226 	 */
227 	return head->frame->data != head->orig_ctx.data ||
228 	       head->frame->flags != head->orig_ctx.flags;
229 }
230 
ctx_was_changed(struct xdp_page_head * head)231 static bool ctx_was_changed(struct xdp_page_head *head)
232 {
233 	return head->orig_ctx.data != head->ctx.data ||
234 		head->orig_ctx.data_meta != head->ctx.data_meta ||
235 		head->orig_ctx.data_end != head->ctx.data_end;
236 }
237 
reset_ctx(struct xdp_page_head * head)238 static void reset_ctx(struct xdp_page_head *head)
239 {
240 	if (likely(!frame_was_changed(head) && !ctx_was_changed(head)))
241 		return;
242 
243 	head->ctx.data = head->orig_ctx.data;
244 	head->ctx.data_meta = head->orig_ctx.data_meta;
245 	head->ctx.data_end = head->orig_ctx.data_end;
246 	xdp_update_frame_from_buff(&head->ctx, head->frame);
247 	head->frame->mem = head->orig_ctx.rxq->mem;
248 }
249 
xdp_recv_frames(struct xdp_frame ** frames,int nframes,struct sk_buff ** skbs,struct net_device * dev)250 static int xdp_recv_frames(struct xdp_frame **frames, int nframes,
251 			   struct sk_buff **skbs,
252 			   struct net_device *dev)
253 {
254 	gfp_t gfp = __GFP_ZERO | GFP_ATOMIC;
255 	int i, n;
256 	LIST_HEAD(list);
257 
258 	n = kmem_cache_alloc_bulk(skbuff_cache, gfp, nframes, (void **)skbs);
259 	if (unlikely(n == 0)) {
260 		for (i = 0; i < nframes; i++)
261 			xdp_return_frame(frames[i]);
262 		return -ENOMEM;
263 	}
264 
265 	for (i = 0; i < nframes; i++) {
266 		struct xdp_frame *xdpf = frames[i];
267 		struct sk_buff *skb = skbs[i];
268 
269 		skb = __xdp_build_skb_from_frame(xdpf, skb, dev);
270 		if (!skb) {
271 			xdp_return_frame(xdpf);
272 			continue;
273 		}
274 
275 		list_add_tail(&skb->list, &list);
276 	}
277 	netif_receive_skb_list(&list);
278 
279 	return 0;
280 }
281 
xdp_test_run_batch(struct xdp_test_data * xdp,struct bpf_prog * prog,u32 repeat)282 static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog,
283 			      u32 repeat)
284 {
285 	struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
286 	int err = 0, act, ret, i, nframes = 0, batch_sz;
287 	struct xdp_frame **frames = xdp->frames;
288 	struct xdp_page_head *head;
289 	struct xdp_frame *frm;
290 	bool redirect = false;
291 	struct xdp_buff *ctx;
292 	struct page *page;
293 
294 	batch_sz = min_t(u32, repeat, xdp->batch_size);
295 
296 	local_bh_disable();
297 	xdp_set_return_frame_no_direct();
298 
299 	for (i = 0; i < batch_sz; i++) {
300 		page = page_pool_dev_alloc_pages(xdp->pp);
301 		if (!page) {
302 			err = -ENOMEM;
303 			goto out;
304 		}
305 
306 		head = phys_to_virt(page_to_phys(page));
307 		reset_ctx(head);
308 		ctx = &head->ctx;
309 		frm = head->frame;
310 		xdp->frame_cnt++;
311 
312 		act = bpf_prog_run_xdp(prog, ctx);
313 
314 		/* if program changed pkt bounds we need to update the xdp_frame */
315 		if (unlikely(ctx_was_changed(head))) {
316 			ret = xdp_update_frame_from_buff(ctx, frm);
317 			if (ret) {
318 				xdp_return_buff(ctx);
319 				continue;
320 			}
321 		}
322 
323 		switch (act) {
324 		case XDP_TX:
325 			/* we can't do a real XDP_TX since we're not in the
326 			 * driver, so turn it into a REDIRECT back to the same
327 			 * index
328 			 */
329 			ri->tgt_index = xdp->dev->ifindex;
330 			ri->map_id = INT_MAX;
331 			ri->map_type = BPF_MAP_TYPE_UNSPEC;
332 			fallthrough;
333 		case XDP_REDIRECT:
334 			redirect = true;
335 			ret = xdp_do_redirect_frame(xdp->dev, ctx, frm, prog);
336 			if (ret)
337 				xdp_return_buff(ctx);
338 			break;
339 		case XDP_PASS:
340 			frames[nframes++] = frm;
341 			break;
342 		default:
343 			bpf_warn_invalid_xdp_action(NULL, prog, act);
344 			fallthrough;
345 		case XDP_DROP:
346 			xdp_return_buff(ctx);
347 			break;
348 		}
349 	}
350 
351 out:
352 	if (redirect)
353 		xdp_do_flush();
354 	if (nframes) {
355 		ret = xdp_recv_frames(frames, nframes, xdp->skbs, xdp->dev);
356 		if (ret)
357 			err = ret;
358 	}
359 
360 	xdp_clear_return_frame_no_direct();
361 	local_bh_enable();
362 	return err;
363 }
364 
bpf_test_run_xdp_live(struct bpf_prog * prog,struct xdp_buff * ctx,u32 repeat,u32 batch_size,u32 * time)365 static int bpf_test_run_xdp_live(struct bpf_prog *prog, struct xdp_buff *ctx,
366 				 u32 repeat, u32 batch_size, u32 *time)
367 
368 {
369 	struct xdp_test_data xdp = { .batch_size = batch_size };
370 	struct bpf_test_timer t = { .mode = NO_MIGRATE };
371 	int ret;
372 
373 	if (!repeat)
374 		repeat = 1;
375 
376 	ret = xdp_test_run_setup(&xdp, ctx);
377 	if (ret)
378 		return ret;
379 
380 	bpf_test_timer_enter(&t);
381 	do {
382 		xdp.frame_cnt = 0;
383 		ret = xdp_test_run_batch(&xdp, prog, repeat - t.i);
384 		if (unlikely(ret < 0))
385 			break;
386 	} while (bpf_test_timer_continue(&t, xdp.frame_cnt, repeat, &ret, time));
387 	bpf_test_timer_leave(&t);
388 
389 	xdp_test_run_teardown(&xdp);
390 	return ret;
391 }
392 
bpf_test_run(struct bpf_prog * prog,void * ctx,u32 repeat,u32 * retval,u32 * time,bool xdp)393 static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
394 			u32 *retval, u32 *time, bool xdp)
395 {
396 	struct bpf_prog_array_item item = {.prog = prog};
397 	struct bpf_run_ctx *old_ctx;
398 	struct bpf_cg_run_ctx run_ctx;
399 	struct bpf_test_timer t = { NO_MIGRATE };
400 	enum bpf_cgroup_storage_type stype;
401 	int ret;
402 
403 	for_each_cgroup_storage_type(stype) {
404 		item.cgroup_storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
405 		if (IS_ERR(item.cgroup_storage[stype])) {
406 			item.cgroup_storage[stype] = NULL;
407 			for_each_cgroup_storage_type(stype)
408 				bpf_cgroup_storage_free(item.cgroup_storage[stype]);
409 			return -ENOMEM;
410 		}
411 	}
412 
413 	if (!repeat)
414 		repeat = 1;
415 
416 	bpf_test_timer_enter(&t);
417 	old_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
418 	do {
419 		run_ctx.prog_item = &item;
420 		local_bh_disable();
421 		if (xdp)
422 			*retval = bpf_prog_run_xdp(prog, ctx);
423 		else
424 			*retval = bpf_prog_run(prog, ctx);
425 		local_bh_enable();
426 	} while (bpf_test_timer_continue(&t, 1, repeat, &ret, time));
427 	bpf_reset_run_ctx(old_ctx);
428 	bpf_test_timer_leave(&t);
429 
430 	for_each_cgroup_storage_type(stype)
431 		bpf_cgroup_storage_free(item.cgroup_storage[stype]);
432 
433 	return ret;
434 }
435 
bpf_test_finish(const union bpf_attr * kattr,union bpf_attr __user * uattr,const void * data,struct skb_shared_info * sinfo,u32 size,u32 retval,u32 duration)436 static int bpf_test_finish(const union bpf_attr *kattr,
437 			   union bpf_attr __user *uattr, const void *data,
438 			   struct skb_shared_info *sinfo, u32 size,
439 			   u32 retval, u32 duration)
440 {
441 	void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
442 	int err = -EFAULT;
443 	u32 copy_size = size;
444 
445 	/* Clamp copy if the user has provided a size hint, but copy the full
446 	 * buffer if not to retain old behaviour.
447 	 */
448 	if (kattr->test.data_size_out &&
449 	    copy_size > kattr->test.data_size_out) {
450 		copy_size = kattr->test.data_size_out;
451 		err = -ENOSPC;
452 	}
453 
454 	if (data_out) {
455 		int len = sinfo ? copy_size - sinfo->xdp_frags_size : copy_size;
456 
457 		if (len < 0) {
458 			err = -ENOSPC;
459 			goto out;
460 		}
461 
462 		if (copy_to_user(data_out, data, len))
463 			goto out;
464 
465 		if (sinfo) {
466 			int i, offset = len;
467 			u32 data_len;
468 
469 			for (i = 0; i < sinfo->nr_frags; i++) {
470 				skb_frag_t *frag = &sinfo->frags[i];
471 
472 				if (offset >= copy_size) {
473 					err = -ENOSPC;
474 					break;
475 				}
476 
477 				data_len = min_t(u32, copy_size - offset,
478 						 skb_frag_size(frag));
479 
480 				if (copy_to_user(data_out + offset,
481 						 skb_frag_address(frag),
482 						 data_len))
483 					goto out;
484 
485 				offset += data_len;
486 			}
487 		}
488 	}
489 
490 	if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
491 		goto out;
492 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
493 		goto out;
494 	if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
495 		goto out;
496 	if (err != -ENOSPC)
497 		err = 0;
498 out:
499 	trace_bpf_test_finish(&err);
500 	return err;
501 }
502 
503 /* Integer types of various sizes and pointer combinations cover variety of
504  * architecture dependent calling conventions. 7+ can be supported in the
505  * future.
506  */
507 __diag_push();
508 __diag_ignore_all("-Wmissing-prototypes",
509 		  "Global functions as their definitions will be in vmlinux BTF");
bpf_fentry_test1(int a)510 __bpf_kfunc int bpf_fentry_test1(int a)
511 {
512 	return a + 1;
513 }
514 EXPORT_SYMBOL_GPL(bpf_fentry_test1);
515 
bpf_fentry_test2(int a,u64 b)516 int noinline bpf_fentry_test2(int a, u64 b)
517 {
518 	return a + b;
519 }
520 
bpf_fentry_test3(char a,int b,u64 c)521 int noinline bpf_fentry_test3(char a, int b, u64 c)
522 {
523 	return a + b + c;
524 }
525 
bpf_fentry_test4(void * a,char b,int c,u64 d)526 int noinline bpf_fentry_test4(void *a, char b, int c, u64 d)
527 {
528 	return (long)a + b + c + d;
529 }
530 
bpf_fentry_test5(u64 a,void * b,short c,int d,u64 e)531 int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e)
532 {
533 	return a + (long)b + c + d + e;
534 }
535 
bpf_fentry_test6(u64 a,void * b,short c,int d,void * e,u64 f)536 int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f)
537 {
538 	return a + (long)b + c + d + (long)e + f;
539 }
540 
541 struct bpf_fentry_test_t {
542 	struct bpf_fentry_test_t *a;
543 };
544 
bpf_fentry_test7(struct bpf_fentry_test_t * arg)545 int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg)
546 {
547 	asm volatile ("": "+r"(arg));
548 	return (long)arg;
549 }
550 
bpf_fentry_test8(struct bpf_fentry_test_t * arg)551 int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg)
552 {
553 	return (long)arg->a;
554 }
555 
bpf_fentry_test9(u32 * a)556 __bpf_kfunc u32 bpf_fentry_test9(u32 *a)
557 {
558 	return *a;
559 }
560 
bpf_fentry_test_sinfo(struct skb_shared_info * sinfo)561 void noinline bpf_fentry_test_sinfo(struct skb_shared_info *sinfo)
562 {
563 }
564 
bpf_modify_return_test(int a,int * b)565 __bpf_kfunc int bpf_modify_return_test(int a, int *b)
566 {
567 	*b += 1;
568 	return a + *b;
569 }
570 
bpf_modify_return_test2(int a,int * b,short c,int d,void * e,char f,int g)571 __bpf_kfunc int bpf_modify_return_test2(int a, int *b, short c, int d,
572 					void *e, char f, int g)
573 {
574 	*b += 1;
575 	return a + *b + c + d + (long)e + f + g;
576 }
577 
bpf_fentry_shadow_test(int a)578 int noinline bpf_fentry_shadow_test(int a)
579 {
580 	return a + 1;
581 }
582 
583 struct prog_test_member1 {
584 	int a;
585 };
586 
587 struct prog_test_member {
588 	struct prog_test_member1 m;
589 	int c;
590 };
591 
592 struct prog_test_ref_kfunc {
593 	int a;
594 	int b;
595 	struct prog_test_member memb;
596 	struct prog_test_ref_kfunc *next;
597 	refcount_t cnt;
598 };
599 
bpf_kfunc_call_test_release(struct prog_test_ref_kfunc * p)600 __bpf_kfunc void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p)
601 {
602 	refcount_dec(&p->cnt);
603 }
604 
bpf_kfunc_call_memb_release(struct prog_test_member * p)605 __bpf_kfunc void bpf_kfunc_call_memb_release(struct prog_test_member *p)
606 {
607 }
608 
609 __diag_pop();
610 
611 BTF_SET8_START(bpf_test_modify_return_ids)
612 BTF_ID_FLAGS(func, bpf_modify_return_test)
613 BTF_ID_FLAGS(func, bpf_modify_return_test2)
614 BTF_ID_FLAGS(func, bpf_fentry_test1, KF_SLEEPABLE)
615 BTF_SET8_END(bpf_test_modify_return_ids)
616 
617 static const struct btf_kfunc_id_set bpf_test_modify_return_set = {
618 	.owner = THIS_MODULE,
619 	.set   = &bpf_test_modify_return_ids,
620 };
621 
622 BTF_SET8_START(test_sk_check_kfunc_ids)
BTF_ID_FLAGS(func,bpf_kfunc_call_test_release,KF_RELEASE)623 BTF_ID_FLAGS(func, bpf_kfunc_call_test_release, KF_RELEASE)
624 BTF_ID_FLAGS(func, bpf_kfunc_call_memb_release, KF_RELEASE)
625 BTF_SET8_END(test_sk_check_kfunc_ids)
626 
627 static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,
628 			   u32 size, u32 headroom, u32 tailroom)
629 {
630 	void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
631 	void *data;
632 
633 	if (user_size < ETH_HLEN || user_size > PAGE_SIZE - headroom - tailroom)
634 		return ERR_PTR(-EINVAL);
635 
636 	size = SKB_DATA_ALIGN(size);
637 	data = kzalloc(size + headroom + tailroom, GFP_USER);
638 	if (!data)
639 		return ERR_PTR(-ENOMEM);
640 
641 	if (copy_from_user(data + headroom, data_in, user_size)) {
642 		kfree(data);
643 		return ERR_PTR(-EFAULT);
644 	}
645 
646 	return data;
647 }
648 
bpf_prog_test_run_tracing(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)649 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
650 			      const union bpf_attr *kattr,
651 			      union bpf_attr __user *uattr)
652 {
653 	struct bpf_fentry_test_t arg = {};
654 	u16 side_effect = 0, ret = 0;
655 	int b = 2, err = -EFAULT;
656 	u32 retval = 0;
657 
658 	if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
659 		return -EINVAL;
660 
661 	switch (prog->expected_attach_type) {
662 	case BPF_TRACE_FENTRY:
663 	case BPF_TRACE_FEXIT:
664 		if (bpf_fentry_test1(1) != 2 ||
665 		    bpf_fentry_test2(2, 3) != 5 ||
666 		    bpf_fentry_test3(4, 5, 6) != 15 ||
667 		    bpf_fentry_test4((void *)7, 8, 9, 10) != 34 ||
668 		    bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 ||
669 		    bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 ||
670 		    bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 ||
671 		    bpf_fentry_test8(&arg) != 0 ||
672 		    bpf_fentry_test9(&retval) != 0)
673 			goto out;
674 		break;
675 	case BPF_MODIFY_RETURN:
676 		ret = bpf_modify_return_test(1, &b);
677 		if (b != 2)
678 			side_effect++;
679 		b = 2;
680 		ret += bpf_modify_return_test2(1, &b, 3, 4, (void *)5, 6, 7);
681 		if (b != 2)
682 			side_effect++;
683 		break;
684 	default:
685 		goto out;
686 	}
687 
688 	retval = ((u32)side_effect << 16) | ret;
689 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
690 		goto out;
691 
692 	err = 0;
693 out:
694 	trace_bpf_test_finish(&err);
695 	return err;
696 }
697 
698 struct bpf_raw_tp_test_run_info {
699 	struct bpf_prog *prog;
700 	void *ctx;
701 	u32 retval;
702 };
703 
704 static void
__bpf_prog_test_run_raw_tp(void * data)705 __bpf_prog_test_run_raw_tp(void *data)
706 {
707 	struct bpf_raw_tp_test_run_info *info = data;
708 	struct bpf_trace_run_ctx run_ctx = {};
709 	struct bpf_run_ctx *old_run_ctx;
710 
711 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
712 
713 	rcu_read_lock();
714 	info->retval = bpf_prog_run(info->prog, info->ctx);
715 	rcu_read_unlock();
716 
717 	bpf_reset_run_ctx(old_run_ctx);
718 }
719 
bpf_prog_test_run_raw_tp(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)720 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
721 			     const union bpf_attr *kattr,
722 			     union bpf_attr __user *uattr)
723 {
724 	void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
725 	__u32 ctx_size_in = kattr->test.ctx_size_in;
726 	struct bpf_raw_tp_test_run_info info;
727 	int cpu = kattr->test.cpu, err = 0;
728 	int current_cpu;
729 
730 	/* doesn't support data_in/out, ctx_out, duration, or repeat */
731 	if (kattr->test.data_in || kattr->test.data_out ||
732 	    kattr->test.ctx_out || kattr->test.duration ||
733 	    kattr->test.repeat || kattr->test.batch_size)
734 		return -EINVAL;
735 
736 	if (ctx_size_in < prog->aux->max_ctx_offset ||
737 	    ctx_size_in > MAX_BPF_FUNC_ARGS * sizeof(u64))
738 		return -EINVAL;
739 
740 	if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0)
741 		return -EINVAL;
742 
743 	if (ctx_size_in) {
744 		info.ctx = memdup_user(ctx_in, ctx_size_in);
745 		if (IS_ERR(info.ctx))
746 			return PTR_ERR(info.ctx);
747 	} else {
748 		info.ctx = NULL;
749 	}
750 
751 	info.prog = prog;
752 
753 	current_cpu = get_cpu();
754 	if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 ||
755 	    cpu == current_cpu) {
756 		__bpf_prog_test_run_raw_tp(&info);
757 	} else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {
758 		/* smp_call_function_single() also checks cpu_online()
759 		 * after csd_lock(). However, since cpu is from user
760 		 * space, let's do an extra quick check to filter out
761 		 * invalid value before smp_call_function_single().
762 		 */
763 		err = -ENXIO;
764 	} else {
765 		err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp,
766 					       &info, 1);
767 	}
768 	put_cpu();
769 
770 	if (!err &&
771 	    copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32)))
772 		err = -EFAULT;
773 
774 	kfree(info.ctx);
775 	return err;
776 }
777 
bpf_ctx_init(const union bpf_attr * kattr,u32 max_size)778 static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size)
779 {
780 	void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in);
781 	void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
782 	u32 size = kattr->test.ctx_size_in;
783 	void *data;
784 	int err;
785 
786 	if (!data_in && !data_out)
787 		return NULL;
788 
789 	data = kzalloc(max_size, GFP_USER);
790 	if (!data)
791 		return ERR_PTR(-ENOMEM);
792 
793 	if (data_in) {
794 		err = bpf_check_uarg_tail_zero(USER_BPFPTR(data_in), max_size, size);
795 		if (err) {
796 			kfree(data);
797 			return ERR_PTR(err);
798 		}
799 
800 		size = min_t(u32, max_size, size);
801 		if (copy_from_user(data, data_in, size)) {
802 			kfree(data);
803 			return ERR_PTR(-EFAULT);
804 		}
805 	}
806 	return data;
807 }
808 
bpf_ctx_finish(const union bpf_attr * kattr,union bpf_attr __user * uattr,const void * data,u32 size)809 static int bpf_ctx_finish(const union bpf_attr *kattr,
810 			  union bpf_attr __user *uattr, const void *data,
811 			  u32 size)
812 {
813 	void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
814 	int err = -EFAULT;
815 	u32 copy_size = size;
816 
817 	if (!data || !data_out)
818 		return 0;
819 
820 	if (copy_size > kattr->test.ctx_size_out) {
821 		copy_size = kattr->test.ctx_size_out;
822 		err = -ENOSPC;
823 	}
824 
825 	if (copy_to_user(data_out, data, copy_size))
826 		goto out;
827 	if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size)))
828 		goto out;
829 	if (err != -ENOSPC)
830 		err = 0;
831 out:
832 	return err;
833 }
834 
835 /**
836  * range_is_zero - test whether buffer is initialized
837  * @buf: buffer to check
838  * @from: check from this position
839  * @to: check up until (excluding) this position
840  *
841  * This function returns true if the there is a non-zero byte
842  * in the buf in the range [from,to).
843  */
range_is_zero(void * buf,size_t from,size_t to)844 static inline bool range_is_zero(void *buf, size_t from, size_t to)
845 {
846 	return !memchr_inv((u8 *)buf + from, 0, to - from);
847 }
848 
convert___skb_to_skb(struct sk_buff * skb,struct __sk_buff * __skb)849 static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
850 {
851 	struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
852 
853 	if (!__skb)
854 		return 0;
855 
856 	/* make sure the fields we don't use are zeroed */
857 	if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark)))
858 		return -EINVAL;
859 
860 	/* mark is allowed */
861 
862 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark),
863 			   offsetof(struct __sk_buff, priority)))
864 		return -EINVAL;
865 
866 	/* priority is allowed */
867 	/* ingress_ifindex is allowed */
868 	/* ifindex is allowed */
869 
870 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex),
871 			   offsetof(struct __sk_buff, cb)))
872 		return -EINVAL;
873 
874 	/* cb is allowed */
875 
876 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb),
877 			   offsetof(struct __sk_buff, tstamp)))
878 		return -EINVAL;
879 
880 	/* tstamp is allowed */
881 	/* wire_len is allowed */
882 	/* gso_segs is allowed */
883 
884 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs),
885 			   offsetof(struct __sk_buff, gso_size)))
886 		return -EINVAL;
887 
888 	/* gso_size is allowed */
889 
890 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size),
891 			   offsetof(struct __sk_buff, hwtstamp)))
892 		return -EINVAL;
893 
894 	/* hwtstamp is allowed */
895 
896 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, hwtstamp),
897 			   sizeof(struct __sk_buff)))
898 		return -EINVAL;
899 
900 	skb->mark = __skb->mark;
901 	skb->priority = __skb->priority;
902 	skb->skb_iif = __skb->ingress_ifindex;
903 	skb->tstamp = __skb->tstamp;
904 	memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN);
905 
906 	if (__skb->wire_len == 0) {
907 		cb->pkt_len = skb->len;
908 	} else {
909 		if (__skb->wire_len < skb->len ||
910 		    __skb->wire_len > GSO_LEGACY_MAX_SIZE)
911 			return -EINVAL;
912 		cb->pkt_len = __skb->wire_len;
913 	}
914 
915 	if (__skb->gso_segs > GSO_MAX_SEGS)
916 		return -EINVAL;
917 	skb_shinfo(skb)->gso_segs = __skb->gso_segs;
918 	skb_shinfo(skb)->gso_size = __skb->gso_size;
919 	skb_shinfo(skb)->hwtstamps.hwtstamp = __skb->hwtstamp;
920 
921 	return 0;
922 }
923 
convert_skb_to___skb(struct sk_buff * skb,struct __sk_buff * __skb)924 static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb)
925 {
926 	struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
927 
928 	if (!__skb)
929 		return;
930 
931 	__skb->mark = skb->mark;
932 	__skb->priority = skb->priority;
933 	__skb->ingress_ifindex = skb->skb_iif;
934 	__skb->ifindex = skb->dev->ifindex;
935 	__skb->tstamp = skb->tstamp;
936 	memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN);
937 	__skb->wire_len = cb->pkt_len;
938 	__skb->gso_segs = skb_shinfo(skb)->gso_segs;
939 	__skb->hwtstamp = skb_shinfo(skb)->hwtstamps.hwtstamp;
940 }
941 
942 static struct proto bpf_dummy_proto = {
943 	.name   = "bpf_dummy",
944 	.owner  = THIS_MODULE,
945 	.obj_size = sizeof(struct sock),
946 };
947 
bpf_prog_test_run_skb(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)948 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
949 			  union bpf_attr __user *uattr)
950 {
951 	bool is_l2 = false, is_direct_pkt_access = false;
952 	struct net *net = current->nsproxy->net_ns;
953 	struct net_device *dev = net->loopback_dev;
954 	u32 size = kattr->test.data_size_in;
955 	u32 repeat = kattr->test.repeat;
956 	struct __sk_buff *ctx = NULL;
957 	u32 retval, duration;
958 	int hh_len = ETH_HLEN;
959 	struct sk_buff *skb;
960 	struct sock *sk;
961 	void *data;
962 	int ret;
963 
964 	if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
965 		return -EINVAL;
966 
967 	data = bpf_test_init(kattr, kattr->test.data_size_in,
968 			     size, NET_SKB_PAD + NET_IP_ALIGN,
969 			     SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
970 	if (IS_ERR(data))
971 		return PTR_ERR(data);
972 
973 	ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
974 	if (IS_ERR(ctx)) {
975 		kfree(data);
976 		return PTR_ERR(ctx);
977 	}
978 
979 	switch (prog->type) {
980 	case BPF_PROG_TYPE_SCHED_CLS:
981 	case BPF_PROG_TYPE_SCHED_ACT:
982 		is_l2 = true;
983 		fallthrough;
984 	case BPF_PROG_TYPE_LWT_IN:
985 	case BPF_PROG_TYPE_LWT_OUT:
986 	case BPF_PROG_TYPE_LWT_XMIT:
987 		is_direct_pkt_access = true;
988 		break;
989 	default:
990 		break;
991 	}
992 
993 	sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
994 	if (!sk) {
995 		kfree(data);
996 		kfree(ctx);
997 		return -ENOMEM;
998 	}
999 	sock_init_data(NULL, sk);
1000 
1001 	skb = slab_build_skb(data);
1002 	if (!skb) {
1003 		kfree(data);
1004 		kfree(ctx);
1005 		sk_free(sk);
1006 		return -ENOMEM;
1007 	}
1008 	skb->sk = sk;
1009 
1010 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1011 	__skb_put(skb, size);
1012 	if (ctx && ctx->ifindex > 1) {
1013 		dev = dev_get_by_index(net, ctx->ifindex);
1014 		if (!dev) {
1015 			ret = -ENODEV;
1016 			goto out;
1017 		}
1018 	}
1019 	skb->protocol = eth_type_trans(skb, dev);
1020 	skb_reset_network_header(skb);
1021 
1022 	switch (skb->protocol) {
1023 	case htons(ETH_P_IP):
1024 		sk->sk_family = AF_INET;
1025 		if (sizeof(struct iphdr) <= skb_headlen(skb)) {
1026 			sk->sk_rcv_saddr = ip_hdr(skb)->saddr;
1027 			sk->sk_daddr = ip_hdr(skb)->daddr;
1028 		}
1029 		break;
1030 #if IS_ENABLED(CONFIG_IPV6)
1031 	case htons(ETH_P_IPV6):
1032 		sk->sk_family = AF_INET6;
1033 		if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) {
1034 			sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr;
1035 			sk->sk_v6_daddr = ipv6_hdr(skb)->daddr;
1036 		}
1037 		break;
1038 #endif
1039 	default:
1040 		break;
1041 	}
1042 
1043 	if (is_l2)
1044 		__skb_push(skb, hh_len);
1045 	if (is_direct_pkt_access)
1046 		bpf_compute_data_pointers(skb);
1047 	ret = convert___skb_to_skb(skb, ctx);
1048 	if (ret)
1049 		goto out;
1050 	ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false);
1051 	if (ret)
1052 		goto out;
1053 	if (!is_l2) {
1054 		if (skb_headroom(skb) < hh_len) {
1055 			int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
1056 
1057 			if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
1058 				ret = -ENOMEM;
1059 				goto out;
1060 			}
1061 		}
1062 		memset(__skb_push(skb, hh_len), 0, hh_len);
1063 	}
1064 	convert_skb_to___skb(skb, ctx);
1065 
1066 	size = skb->len;
1067 	/* bpf program can never convert linear skb to non-linear */
1068 	if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
1069 		size = skb_headlen(skb);
1070 	ret = bpf_test_finish(kattr, uattr, skb->data, NULL, size, retval,
1071 			      duration);
1072 	if (!ret)
1073 		ret = bpf_ctx_finish(kattr, uattr, ctx,
1074 				     sizeof(struct __sk_buff));
1075 out:
1076 	if (dev && dev != net->loopback_dev)
1077 		dev_put(dev);
1078 	kfree_skb(skb);
1079 	sk_free(sk);
1080 	kfree(ctx);
1081 	return ret;
1082 }
1083 
xdp_convert_md_to_buff(struct xdp_md * xdp_md,struct xdp_buff * xdp)1084 static int xdp_convert_md_to_buff(struct xdp_md *xdp_md, struct xdp_buff *xdp)
1085 {
1086 	unsigned int ingress_ifindex, rx_queue_index;
1087 	struct netdev_rx_queue *rxqueue;
1088 	struct net_device *device;
1089 
1090 	if (!xdp_md)
1091 		return 0;
1092 
1093 	if (xdp_md->egress_ifindex != 0)
1094 		return -EINVAL;
1095 
1096 	ingress_ifindex = xdp_md->ingress_ifindex;
1097 	rx_queue_index = xdp_md->rx_queue_index;
1098 
1099 	if (!ingress_ifindex && rx_queue_index)
1100 		return -EINVAL;
1101 
1102 	if (ingress_ifindex) {
1103 		device = dev_get_by_index(current->nsproxy->net_ns,
1104 					  ingress_ifindex);
1105 		if (!device)
1106 			return -ENODEV;
1107 
1108 		if (rx_queue_index >= device->real_num_rx_queues)
1109 			goto free_dev;
1110 
1111 		rxqueue = __netif_get_rx_queue(device, rx_queue_index);
1112 
1113 		if (!xdp_rxq_info_is_reg(&rxqueue->xdp_rxq))
1114 			goto free_dev;
1115 
1116 		xdp->rxq = &rxqueue->xdp_rxq;
1117 		/* The device is now tracked in the xdp->rxq for later
1118 		 * dev_put()
1119 		 */
1120 	}
1121 
1122 	xdp->data = xdp->data_meta + xdp_md->data;
1123 	return 0;
1124 
1125 free_dev:
1126 	dev_put(device);
1127 	return -EINVAL;
1128 }
1129 
xdp_convert_buff_to_md(struct xdp_buff * xdp,struct xdp_md * xdp_md)1130 static void xdp_convert_buff_to_md(struct xdp_buff *xdp, struct xdp_md *xdp_md)
1131 {
1132 	if (!xdp_md)
1133 		return;
1134 
1135 	xdp_md->data = xdp->data - xdp->data_meta;
1136 	xdp_md->data_end = xdp->data_end - xdp->data_meta;
1137 
1138 	if (xdp_md->ingress_ifindex)
1139 		dev_put(xdp->rxq->dev);
1140 }
1141 
bpf_prog_test_run_xdp(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)1142 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1143 			  union bpf_attr __user *uattr)
1144 {
1145 	bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES);
1146 	u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1147 	u32 batch_size = kattr->test.batch_size;
1148 	u32 retval = 0, duration, max_data_sz;
1149 	u32 size = kattr->test.data_size_in;
1150 	u32 headroom = XDP_PACKET_HEADROOM;
1151 	u32 repeat = kattr->test.repeat;
1152 	struct netdev_rx_queue *rxqueue;
1153 	struct skb_shared_info *sinfo;
1154 	struct xdp_buff xdp = {};
1155 	int i, ret = -EINVAL;
1156 	struct xdp_md *ctx;
1157 	void *data;
1158 
1159 	if (prog->expected_attach_type == BPF_XDP_DEVMAP ||
1160 	    prog->expected_attach_type == BPF_XDP_CPUMAP)
1161 		return -EINVAL;
1162 
1163 	if (kattr->test.flags & ~BPF_F_TEST_XDP_LIVE_FRAMES)
1164 		return -EINVAL;
1165 
1166 	if (bpf_prog_is_dev_bound(prog->aux))
1167 		return -EINVAL;
1168 
1169 	if (do_live) {
1170 		if (!batch_size)
1171 			batch_size = NAPI_POLL_WEIGHT;
1172 		else if (batch_size > TEST_XDP_MAX_BATCH)
1173 			return -E2BIG;
1174 
1175 		headroom += sizeof(struct xdp_page_head);
1176 	} else if (batch_size) {
1177 		return -EINVAL;
1178 	}
1179 
1180 	ctx = bpf_ctx_init(kattr, sizeof(struct xdp_md));
1181 	if (IS_ERR(ctx))
1182 		return PTR_ERR(ctx);
1183 
1184 	if (ctx) {
1185 		/* There can't be user provided data before the meta data */
1186 		if (ctx->data_meta || ctx->data_end != size ||
1187 		    ctx->data > ctx->data_end ||
1188 		    unlikely(xdp_metalen_invalid(ctx->data)) ||
1189 		    (do_live && (kattr->test.data_out || kattr->test.ctx_out)))
1190 			goto free_ctx;
1191 		/* Meta data is allocated from the headroom */
1192 		headroom -= ctx->data;
1193 	}
1194 
1195 	max_data_sz = 4096 - headroom - tailroom;
1196 	if (size > max_data_sz) {
1197 		/* disallow live data mode for jumbo frames */
1198 		if (do_live)
1199 			goto free_ctx;
1200 		size = max_data_sz;
1201 	}
1202 
1203 	data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom);
1204 	if (IS_ERR(data)) {
1205 		ret = PTR_ERR(data);
1206 		goto free_ctx;
1207 	}
1208 
1209 	rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
1210 	rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom;
1211 	xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq);
1212 	xdp_prepare_buff(&xdp, data, headroom, size, true);
1213 	sinfo = xdp_get_shared_info_from_buff(&xdp);
1214 
1215 	ret = xdp_convert_md_to_buff(ctx, &xdp);
1216 	if (ret)
1217 		goto free_data;
1218 
1219 	if (unlikely(kattr->test.data_size_in > size)) {
1220 		void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
1221 
1222 		while (size < kattr->test.data_size_in) {
1223 			struct page *page;
1224 			skb_frag_t *frag;
1225 			u32 data_len;
1226 
1227 			if (sinfo->nr_frags == MAX_SKB_FRAGS) {
1228 				ret = -ENOMEM;
1229 				goto out;
1230 			}
1231 
1232 			page = alloc_page(GFP_KERNEL);
1233 			if (!page) {
1234 				ret = -ENOMEM;
1235 				goto out;
1236 			}
1237 
1238 			frag = &sinfo->frags[sinfo->nr_frags++];
1239 
1240 			data_len = min_t(u32, kattr->test.data_size_in - size,
1241 					 PAGE_SIZE);
1242 			skb_frag_fill_page_desc(frag, page, 0, data_len);
1243 
1244 			if (copy_from_user(page_address(page), data_in + size,
1245 					   data_len)) {
1246 				ret = -EFAULT;
1247 				goto out;
1248 			}
1249 			sinfo->xdp_frags_size += data_len;
1250 			size += data_len;
1251 		}
1252 		xdp_buff_set_frags_flag(&xdp);
1253 	}
1254 
1255 	if (repeat > 1)
1256 		bpf_prog_change_xdp(NULL, prog);
1257 
1258 	if (do_live)
1259 		ret = bpf_test_run_xdp_live(prog, &xdp, repeat, batch_size, &duration);
1260 	else
1261 		ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true);
1262 	/* We convert the xdp_buff back to an xdp_md before checking the return
1263 	 * code so the reference count of any held netdevice will be decremented
1264 	 * even if the test run failed.
1265 	 */
1266 	xdp_convert_buff_to_md(&xdp, ctx);
1267 	if (ret)
1268 		goto out;
1269 
1270 	size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size;
1271 	ret = bpf_test_finish(kattr, uattr, xdp.data_meta, sinfo, size,
1272 			      retval, duration);
1273 	if (!ret)
1274 		ret = bpf_ctx_finish(kattr, uattr, ctx,
1275 				     sizeof(struct xdp_md));
1276 
1277 out:
1278 	if (repeat > 1)
1279 		bpf_prog_change_xdp(prog, NULL);
1280 free_data:
1281 	for (i = 0; i < sinfo->nr_frags; i++)
1282 		__free_page(skb_frag_page(&sinfo->frags[i]));
1283 	kfree(data);
1284 free_ctx:
1285 	kfree(ctx);
1286 	return ret;
1287 }
1288 
verify_user_bpf_flow_keys(struct bpf_flow_keys * ctx)1289 static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx)
1290 {
1291 	/* make sure the fields we don't use are zeroed */
1292 	if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags)))
1293 		return -EINVAL;
1294 
1295 	/* flags is allowed */
1296 
1297 	if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags),
1298 			   sizeof(struct bpf_flow_keys)))
1299 		return -EINVAL;
1300 
1301 	return 0;
1302 }
1303 
bpf_prog_test_run_flow_dissector(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)1304 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1305 				     const union bpf_attr *kattr,
1306 				     union bpf_attr __user *uattr)
1307 {
1308 	struct bpf_test_timer t = { NO_PREEMPT };
1309 	u32 size = kattr->test.data_size_in;
1310 	struct bpf_flow_dissector ctx = {};
1311 	u32 repeat = kattr->test.repeat;
1312 	struct bpf_flow_keys *user_ctx;
1313 	struct bpf_flow_keys flow_keys;
1314 	const struct ethhdr *eth;
1315 	unsigned int flags = 0;
1316 	u32 retval, duration;
1317 	void *data;
1318 	int ret;
1319 
1320 	if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
1321 		return -EINVAL;
1322 
1323 	if (size < ETH_HLEN)
1324 		return -EINVAL;
1325 
1326 	data = bpf_test_init(kattr, kattr->test.data_size_in, size, 0, 0);
1327 	if (IS_ERR(data))
1328 		return PTR_ERR(data);
1329 
1330 	eth = (struct ethhdr *)data;
1331 
1332 	if (!repeat)
1333 		repeat = 1;
1334 
1335 	user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys));
1336 	if (IS_ERR(user_ctx)) {
1337 		kfree(data);
1338 		return PTR_ERR(user_ctx);
1339 	}
1340 	if (user_ctx) {
1341 		ret = verify_user_bpf_flow_keys(user_ctx);
1342 		if (ret)
1343 			goto out;
1344 		flags = user_ctx->flags;
1345 	}
1346 
1347 	ctx.flow_keys = &flow_keys;
1348 	ctx.data = data;
1349 	ctx.data_end = (__u8 *)data + size;
1350 
1351 	bpf_test_timer_enter(&t);
1352 	do {
1353 		retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN,
1354 					  size, flags);
1355 	} while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration));
1356 	bpf_test_timer_leave(&t);
1357 
1358 	if (ret < 0)
1359 		goto out;
1360 
1361 	ret = bpf_test_finish(kattr, uattr, &flow_keys, NULL,
1362 			      sizeof(flow_keys), retval, duration);
1363 	if (!ret)
1364 		ret = bpf_ctx_finish(kattr, uattr, user_ctx,
1365 				     sizeof(struct bpf_flow_keys));
1366 
1367 out:
1368 	kfree(user_ctx);
1369 	kfree(data);
1370 	return ret;
1371 }
1372 
bpf_prog_test_run_sk_lookup(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)1373 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr,
1374 				union bpf_attr __user *uattr)
1375 {
1376 	struct bpf_test_timer t = { NO_PREEMPT };
1377 	struct bpf_prog_array *progs = NULL;
1378 	struct bpf_sk_lookup_kern ctx = {};
1379 	u32 repeat = kattr->test.repeat;
1380 	struct bpf_sk_lookup *user_ctx;
1381 	u32 retval, duration;
1382 	int ret = -EINVAL;
1383 
1384 	if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
1385 		return -EINVAL;
1386 
1387 	if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out ||
1388 	    kattr->test.data_size_out)
1389 		return -EINVAL;
1390 
1391 	if (!repeat)
1392 		repeat = 1;
1393 
1394 	user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx));
1395 	if (IS_ERR(user_ctx))
1396 		return PTR_ERR(user_ctx);
1397 
1398 	if (!user_ctx)
1399 		return -EINVAL;
1400 
1401 	if (user_ctx->sk)
1402 		goto out;
1403 
1404 	if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx)))
1405 		goto out;
1406 
1407 	if (user_ctx->local_port > U16_MAX) {
1408 		ret = -ERANGE;
1409 		goto out;
1410 	}
1411 
1412 	ctx.family = (u16)user_ctx->family;
1413 	ctx.protocol = (u16)user_ctx->protocol;
1414 	ctx.dport = (u16)user_ctx->local_port;
1415 	ctx.sport = user_ctx->remote_port;
1416 
1417 	switch (ctx.family) {
1418 	case AF_INET:
1419 		ctx.v4.daddr = (__force __be32)user_ctx->local_ip4;
1420 		ctx.v4.saddr = (__force __be32)user_ctx->remote_ip4;
1421 		break;
1422 
1423 #if IS_ENABLED(CONFIG_IPV6)
1424 	case AF_INET6:
1425 		ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6;
1426 		ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6;
1427 		break;
1428 #endif
1429 
1430 	default:
1431 		ret = -EAFNOSUPPORT;
1432 		goto out;
1433 	}
1434 
1435 	progs = bpf_prog_array_alloc(1, GFP_KERNEL);
1436 	if (!progs) {
1437 		ret = -ENOMEM;
1438 		goto out;
1439 	}
1440 
1441 	progs->items[0].prog = prog;
1442 
1443 	bpf_test_timer_enter(&t);
1444 	do {
1445 		ctx.selected_sk = NULL;
1446 		retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, bpf_prog_run);
1447 	} while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration));
1448 	bpf_test_timer_leave(&t);
1449 
1450 	if (ret < 0)
1451 		goto out;
1452 
1453 	user_ctx->cookie = 0;
1454 	if (ctx.selected_sk) {
1455 		if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) {
1456 			ret = -EOPNOTSUPP;
1457 			goto out;
1458 		}
1459 
1460 		user_ctx->cookie = sock_gen_cookie(ctx.selected_sk);
1461 	}
1462 
1463 	ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration);
1464 	if (!ret)
1465 		ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx));
1466 
1467 out:
1468 	bpf_prog_array_free(progs);
1469 	kfree(user_ctx);
1470 	return ret;
1471 }
1472 
bpf_prog_test_run_syscall(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)1473 int bpf_prog_test_run_syscall(struct bpf_prog *prog,
1474 			      const union bpf_attr *kattr,
1475 			      union bpf_attr __user *uattr)
1476 {
1477 	void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
1478 	__u32 ctx_size_in = kattr->test.ctx_size_in;
1479 	void *ctx = NULL;
1480 	u32 retval;
1481 	int err = 0;
1482 
1483 	/* doesn't support data_in/out, ctx_out, duration, or repeat or flags */
1484 	if (kattr->test.data_in || kattr->test.data_out ||
1485 	    kattr->test.ctx_out || kattr->test.duration ||
1486 	    kattr->test.repeat || kattr->test.flags ||
1487 	    kattr->test.batch_size)
1488 		return -EINVAL;
1489 
1490 	if (ctx_size_in < prog->aux->max_ctx_offset ||
1491 	    ctx_size_in > U16_MAX)
1492 		return -EINVAL;
1493 
1494 	if (ctx_size_in) {
1495 		ctx = memdup_user(ctx_in, ctx_size_in);
1496 		if (IS_ERR(ctx))
1497 			return PTR_ERR(ctx);
1498 	}
1499 
1500 	rcu_read_lock_trace();
1501 	retval = bpf_prog_run_pin_on_cpu(prog, ctx);
1502 	rcu_read_unlock_trace();
1503 
1504 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) {
1505 		err = -EFAULT;
1506 		goto out;
1507 	}
1508 	if (ctx_size_in)
1509 		if (copy_to_user(ctx_in, ctx, ctx_size_in))
1510 			err = -EFAULT;
1511 out:
1512 	kfree(ctx);
1513 	return err;
1514 }
1515 
verify_and_copy_hook_state(struct nf_hook_state * state,const struct nf_hook_state * user,struct net_device * dev)1516 static int verify_and_copy_hook_state(struct nf_hook_state *state,
1517 				      const struct nf_hook_state *user,
1518 				      struct net_device *dev)
1519 {
1520 	if (user->in || user->out)
1521 		return -EINVAL;
1522 
1523 	if (user->net || user->sk || user->okfn)
1524 		return -EINVAL;
1525 
1526 	switch (user->pf) {
1527 	case NFPROTO_IPV4:
1528 	case NFPROTO_IPV6:
1529 		switch (state->hook) {
1530 		case NF_INET_PRE_ROUTING:
1531 			state->in = dev;
1532 			break;
1533 		case NF_INET_LOCAL_IN:
1534 			state->in = dev;
1535 			break;
1536 		case NF_INET_FORWARD:
1537 			state->in = dev;
1538 			state->out = dev;
1539 			break;
1540 		case NF_INET_LOCAL_OUT:
1541 			state->out = dev;
1542 			break;
1543 		case NF_INET_POST_ROUTING:
1544 			state->out = dev;
1545 			break;
1546 		}
1547 
1548 		break;
1549 	default:
1550 		return -EINVAL;
1551 	}
1552 
1553 	state->pf = user->pf;
1554 	state->hook = user->hook;
1555 
1556 	return 0;
1557 }
1558 
nfproto_eth(int nfproto)1559 static __be16 nfproto_eth(int nfproto)
1560 {
1561 	switch (nfproto) {
1562 	case NFPROTO_IPV4:
1563 		return htons(ETH_P_IP);
1564 	case NFPROTO_IPV6:
1565 		break;
1566 	}
1567 
1568 	return htons(ETH_P_IPV6);
1569 }
1570 
bpf_prog_test_run_nf(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)1571 int bpf_prog_test_run_nf(struct bpf_prog *prog,
1572 			 const union bpf_attr *kattr,
1573 			 union bpf_attr __user *uattr)
1574 {
1575 	struct net *net = current->nsproxy->net_ns;
1576 	struct net_device *dev = net->loopback_dev;
1577 	struct nf_hook_state *user_ctx, hook_state = {
1578 		.pf = NFPROTO_IPV4,
1579 		.hook = NF_INET_LOCAL_OUT,
1580 	};
1581 	u32 size = kattr->test.data_size_in;
1582 	u32 repeat = kattr->test.repeat;
1583 	struct bpf_nf_ctx ctx = {
1584 		.state = &hook_state,
1585 	};
1586 	struct sk_buff *skb = NULL;
1587 	u32 retval, duration;
1588 	void *data;
1589 	int ret;
1590 
1591 	if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
1592 		return -EINVAL;
1593 
1594 	if (size < sizeof(struct iphdr))
1595 		return -EINVAL;
1596 
1597 	data = bpf_test_init(kattr, kattr->test.data_size_in, size,
1598 			     NET_SKB_PAD + NET_IP_ALIGN,
1599 			     SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
1600 	if (IS_ERR(data))
1601 		return PTR_ERR(data);
1602 
1603 	if (!repeat)
1604 		repeat = 1;
1605 
1606 	user_ctx = bpf_ctx_init(kattr, sizeof(struct nf_hook_state));
1607 	if (IS_ERR(user_ctx)) {
1608 		kfree(data);
1609 		return PTR_ERR(user_ctx);
1610 	}
1611 
1612 	if (user_ctx) {
1613 		ret = verify_and_copy_hook_state(&hook_state, user_ctx, dev);
1614 		if (ret)
1615 			goto out;
1616 	}
1617 
1618 	skb = slab_build_skb(data);
1619 	if (!skb) {
1620 		ret = -ENOMEM;
1621 		goto out;
1622 	}
1623 
1624 	data = NULL; /* data released via kfree_skb */
1625 
1626 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1627 	__skb_put(skb, size);
1628 
1629 	ret = -EINVAL;
1630 
1631 	if (hook_state.hook != NF_INET_LOCAL_OUT) {
1632 		if (size < ETH_HLEN + sizeof(struct iphdr))
1633 			goto out;
1634 
1635 		skb->protocol = eth_type_trans(skb, dev);
1636 		switch (skb->protocol) {
1637 		case htons(ETH_P_IP):
1638 			if (hook_state.pf == NFPROTO_IPV4)
1639 				break;
1640 			goto out;
1641 		case htons(ETH_P_IPV6):
1642 			if (size < ETH_HLEN + sizeof(struct ipv6hdr))
1643 				goto out;
1644 			if (hook_state.pf == NFPROTO_IPV6)
1645 				break;
1646 			goto out;
1647 		default:
1648 			ret = -EPROTO;
1649 			goto out;
1650 		}
1651 
1652 		skb_reset_network_header(skb);
1653 	} else {
1654 		skb->protocol = nfproto_eth(hook_state.pf);
1655 	}
1656 
1657 	ctx.skb = skb;
1658 
1659 	ret = bpf_test_run(prog, &ctx, repeat, &retval, &duration, false);
1660 	if (ret)
1661 		goto out;
1662 
1663 	ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration);
1664 
1665 out:
1666 	kfree(user_ctx);
1667 	kfree_skb(skb);
1668 	kfree(data);
1669 	return ret;
1670 }
1671 
1672 static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = {
1673 	.owner = THIS_MODULE,
1674 	.set   = &test_sk_check_kfunc_ids,
1675 };
1676 
1677 BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids)
BTF_ID(struct,prog_test_ref_kfunc)1678 BTF_ID(struct, prog_test_ref_kfunc)
1679 BTF_ID(func, bpf_kfunc_call_test_release)
1680 BTF_ID(struct, prog_test_member)
1681 BTF_ID(func, bpf_kfunc_call_memb_release)
1682 
1683 static int __init bpf_prog_test_run_init(void)
1684 {
1685 	const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = {
1686 		{
1687 		  .btf_id       = bpf_prog_test_dtor_kfunc_ids[0],
1688 		  .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1]
1689 		},
1690 		{
1691 		  .btf_id	= bpf_prog_test_dtor_kfunc_ids[2],
1692 		  .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[3],
1693 		},
1694 	};
1695 	int ret;
1696 
1697 	ret = register_btf_fmodret_id_set(&bpf_test_modify_return_set);
1698 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set);
1699 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_prog_test_kfunc_set);
1700 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_prog_test_kfunc_set);
1701 	return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc,
1702 						  ARRAY_SIZE(bpf_prog_test_dtor_kfunc),
1703 						  THIS_MODULE);
1704 }
1705 late_initcall(bpf_prog_test_run_init);
1706