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