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