1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com 2 * 3 * This program is free software; you can redistribute it and/or 4 * modify it under the terms of version 2 of the GNU General Public 5 * License as published by the Free Software Foundation. 6 * 7 * This program is distributed in the hope that it will be useful, but 8 * WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 * General Public License for more details. 11 */ 12 #include <linux/bpf.h> 13 #include <linux/bpf_trace.h> 14 #include <linux/bpf_lirc.h> 15 #include <linux/btf.h> 16 #include <linux/syscalls.h> 17 #include <linux/slab.h> 18 #include <linux/sched/signal.h> 19 #include <linux/vmalloc.h> 20 #include <linux/mmzone.h> 21 #include <linux/anon_inodes.h> 22 #include <linux/fdtable.h> 23 #include <linux/file.h> 24 #include <linux/fs.h> 25 #include <linux/license.h> 26 #include <linux/filter.h> 27 #include <linux/version.h> 28 #include <linux/kernel.h> 29 #include <linux/idr.h> 30 #include <linux/cred.h> 31 #include <linux/timekeeping.h> 32 #include <linux/ctype.h> 33 #include <linux/nospec.h> 34 35 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ 36 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ 37 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ 38 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) 39 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) 40 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map)) 41 42 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) 43 44 DEFINE_PER_CPU(int, bpf_prog_active); 45 static DEFINE_IDR(prog_idr); 46 static DEFINE_SPINLOCK(prog_idr_lock); 47 static DEFINE_IDR(map_idr); 48 static DEFINE_SPINLOCK(map_idr_lock); 49 50 int sysctl_unprivileged_bpf_disabled __read_mostly; 51 52 static const struct bpf_map_ops * const bpf_map_types[] = { 53 #define BPF_PROG_TYPE(_id, _ops) 54 #define BPF_MAP_TYPE(_id, _ops) \ 55 [_id] = &_ops, 56 #include <linux/bpf_types.h> 57 #undef BPF_PROG_TYPE 58 #undef BPF_MAP_TYPE 59 }; 60 61 /* 62 * If we're handed a bigger struct than we know of, ensure all the unknown bits 63 * are 0 - i.e. new user-space does not rely on any kernel feature extensions 64 * we don't know about yet. 65 * 66 * There is a ToCToU between this function call and the following 67 * copy_from_user() call. However, this is not a concern since this function is 68 * meant to be a future-proofing of bits. 69 */ 70 int bpf_check_uarg_tail_zero(void __user *uaddr, 71 size_t expected_size, 72 size_t actual_size) 73 { 74 unsigned char __user *addr; 75 unsigned char __user *end; 76 unsigned char val; 77 int err; 78 79 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ 80 return -E2BIG; 81 82 if (unlikely(!access_ok(uaddr, actual_size))) 83 return -EFAULT; 84 85 if (actual_size <= expected_size) 86 return 0; 87 88 addr = uaddr + expected_size; 89 end = uaddr + actual_size; 90 91 for (; addr < end; addr++) { 92 err = get_user(val, addr); 93 if (err) 94 return err; 95 if (val) 96 return -E2BIG; 97 } 98 99 return 0; 100 } 101 102 const struct bpf_map_ops bpf_map_offload_ops = { 103 .map_alloc = bpf_map_offload_map_alloc, 104 .map_free = bpf_map_offload_map_free, 105 .map_check_btf = map_check_no_btf, 106 }; 107 108 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) 109 { 110 const struct bpf_map_ops *ops; 111 u32 type = attr->map_type; 112 struct bpf_map *map; 113 int err; 114 115 if (type >= ARRAY_SIZE(bpf_map_types)) 116 return ERR_PTR(-EINVAL); 117 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); 118 ops = bpf_map_types[type]; 119 if (!ops) 120 return ERR_PTR(-EINVAL); 121 122 if (ops->map_alloc_check) { 123 err = ops->map_alloc_check(attr); 124 if (err) 125 return ERR_PTR(err); 126 } 127 if (attr->map_ifindex) 128 ops = &bpf_map_offload_ops; 129 map = ops->map_alloc(attr); 130 if (IS_ERR(map)) 131 return map; 132 map->ops = ops; 133 map->map_type = type; 134 return map; 135 } 136 137 void *bpf_map_area_alloc(size_t size, int numa_node) 138 { 139 /* We definitely need __GFP_NORETRY, so OOM killer doesn't 140 * trigger under memory pressure as we really just want to 141 * fail instead. 142 */ 143 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO; 144 void *area; 145 146 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { 147 area = kmalloc_node(size, GFP_USER | flags, numa_node); 148 if (area != NULL) 149 return area; 150 } 151 152 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags, 153 __builtin_return_address(0)); 154 } 155 156 void bpf_map_area_free(void *area) 157 { 158 kvfree(area); 159 } 160 161 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) 162 { 163 map->map_type = attr->map_type; 164 map->key_size = attr->key_size; 165 map->value_size = attr->value_size; 166 map->max_entries = attr->max_entries; 167 map->map_flags = attr->map_flags; 168 map->numa_node = bpf_map_attr_numa_node(attr); 169 } 170 171 int bpf_map_precharge_memlock(u32 pages) 172 { 173 struct user_struct *user = get_current_user(); 174 unsigned long memlock_limit, cur; 175 176 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 177 cur = atomic_long_read(&user->locked_vm); 178 free_uid(user); 179 if (cur + pages > memlock_limit) 180 return -EPERM; 181 return 0; 182 } 183 184 static int bpf_charge_memlock(struct user_struct *user, u32 pages) 185 { 186 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 187 188 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) { 189 atomic_long_sub(pages, &user->locked_vm); 190 return -EPERM; 191 } 192 return 0; 193 } 194 195 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages) 196 { 197 atomic_long_sub(pages, &user->locked_vm); 198 } 199 200 static int bpf_map_init_memlock(struct bpf_map *map) 201 { 202 struct user_struct *user = get_current_user(); 203 int ret; 204 205 ret = bpf_charge_memlock(user, map->pages); 206 if (ret) { 207 free_uid(user); 208 return ret; 209 } 210 map->user = user; 211 return ret; 212 } 213 214 static void bpf_map_release_memlock(struct bpf_map *map) 215 { 216 struct user_struct *user = map->user; 217 bpf_uncharge_memlock(user, map->pages); 218 free_uid(user); 219 } 220 221 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages) 222 { 223 int ret; 224 225 ret = bpf_charge_memlock(map->user, pages); 226 if (ret) 227 return ret; 228 map->pages += pages; 229 return ret; 230 } 231 232 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages) 233 { 234 bpf_uncharge_memlock(map->user, pages); 235 map->pages -= pages; 236 } 237 238 static int bpf_map_alloc_id(struct bpf_map *map) 239 { 240 int id; 241 242 idr_preload(GFP_KERNEL); 243 spin_lock_bh(&map_idr_lock); 244 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); 245 if (id > 0) 246 map->id = id; 247 spin_unlock_bh(&map_idr_lock); 248 idr_preload_end(); 249 250 if (WARN_ON_ONCE(!id)) 251 return -ENOSPC; 252 253 return id > 0 ? 0 : id; 254 } 255 256 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) 257 { 258 unsigned long flags; 259 260 /* Offloaded maps are removed from the IDR store when their device 261 * disappears - even if someone holds an fd to them they are unusable, 262 * the memory is gone, all ops will fail; they are simply waiting for 263 * refcnt to drop to be freed. 264 */ 265 if (!map->id) 266 return; 267 268 if (do_idr_lock) 269 spin_lock_irqsave(&map_idr_lock, flags); 270 else 271 __acquire(&map_idr_lock); 272 273 idr_remove(&map_idr, map->id); 274 map->id = 0; 275 276 if (do_idr_lock) 277 spin_unlock_irqrestore(&map_idr_lock, flags); 278 else 279 __release(&map_idr_lock); 280 } 281 282 /* called from workqueue */ 283 static void bpf_map_free_deferred(struct work_struct *work) 284 { 285 struct bpf_map *map = container_of(work, struct bpf_map, work); 286 287 bpf_map_release_memlock(map); 288 security_bpf_map_free(map); 289 /* implementation dependent freeing */ 290 map->ops->map_free(map); 291 } 292 293 static void bpf_map_put_uref(struct bpf_map *map) 294 { 295 if (atomic_dec_and_test(&map->usercnt)) { 296 if (map->ops->map_release_uref) 297 map->ops->map_release_uref(map); 298 } 299 } 300 301 /* decrement map refcnt and schedule it for freeing via workqueue 302 * (unrelying map implementation ops->map_free() might sleep) 303 */ 304 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) 305 { 306 if (atomic_dec_and_test(&map->refcnt)) { 307 /* bpf_map_free_id() must be called first */ 308 bpf_map_free_id(map, do_idr_lock); 309 btf_put(map->btf); 310 INIT_WORK(&map->work, bpf_map_free_deferred); 311 schedule_work(&map->work); 312 } 313 } 314 315 void bpf_map_put(struct bpf_map *map) 316 { 317 __bpf_map_put(map, true); 318 } 319 EXPORT_SYMBOL_GPL(bpf_map_put); 320 321 void bpf_map_put_with_uref(struct bpf_map *map) 322 { 323 bpf_map_put_uref(map); 324 bpf_map_put(map); 325 } 326 327 static int bpf_map_release(struct inode *inode, struct file *filp) 328 { 329 struct bpf_map *map = filp->private_data; 330 331 if (map->ops->map_release) 332 map->ops->map_release(map, filp); 333 334 bpf_map_put_with_uref(map); 335 return 0; 336 } 337 338 #ifdef CONFIG_PROC_FS 339 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) 340 { 341 const struct bpf_map *map = filp->private_data; 342 const struct bpf_array *array; 343 u32 owner_prog_type = 0; 344 u32 owner_jited = 0; 345 346 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { 347 array = container_of(map, struct bpf_array, map); 348 owner_prog_type = array->owner_prog_type; 349 owner_jited = array->owner_jited; 350 } 351 352 seq_printf(m, 353 "map_type:\t%u\n" 354 "key_size:\t%u\n" 355 "value_size:\t%u\n" 356 "max_entries:\t%u\n" 357 "map_flags:\t%#x\n" 358 "memlock:\t%llu\n" 359 "map_id:\t%u\n", 360 map->map_type, 361 map->key_size, 362 map->value_size, 363 map->max_entries, 364 map->map_flags, 365 map->pages * 1ULL << PAGE_SHIFT, 366 map->id); 367 368 if (owner_prog_type) { 369 seq_printf(m, "owner_prog_type:\t%u\n", 370 owner_prog_type); 371 seq_printf(m, "owner_jited:\t%u\n", 372 owner_jited); 373 } 374 } 375 #endif 376 377 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, 378 loff_t *ppos) 379 { 380 /* We need this handler such that alloc_file() enables 381 * f_mode with FMODE_CAN_READ. 382 */ 383 return -EINVAL; 384 } 385 386 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, 387 size_t siz, loff_t *ppos) 388 { 389 /* We need this handler such that alloc_file() enables 390 * f_mode with FMODE_CAN_WRITE. 391 */ 392 return -EINVAL; 393 } 394 395 const struct file_operations bpf_map_fops = { 396 #ifdef CONFIG_PROC_FS 397 .show_fdinfo = bpf_map_show_fdinfo, 398 #endif 399 .release = bpf_map_release, 400 .read = bpf_dummy_read, 401 .write = bpf_dummy_write, 402 }; 403 404 int bpf_map_new_fd(struct bpf_map *map, int flags) 405 { 406 int ret; 407 408 ret = security_bpf_map(map, OPEN_FMODE(flags)); 409 if (ret < 0) 410 return ret; 411 412 return anon_inode_getfd("bpf-map", &bpf_map_fops, map, 413 flags | O_CLOEXEC); 414 } 415 416 int bpf_get_file_flag(int flags) 417 { 418 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) 419 return -EINVAL; 420 if (flags & BPF_F_RDONLY) 421 return O_RDONLY; 422 if (flags & BPF_F_WRONLY) 423 return O_WRONLY; 424 return O_RDWR; 425 } 426 427 /* helper macro to check that unused fields 'union bpf_attr' are zero */ 428 #define CHECK_ATTR(CMD) \ 429 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ 430 sizeof(attr->CMD##_LAST_FIELD), 0, \ 431 sizeof(*attr) - \ 432 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ 433 sizeof(attr->CMD##_LAST_FIELD)) != NULL 434 435 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes. 436 * Return 0 on success and < 0 on error. 437 */ 438 static int bpf_obj_name_cpy(char *dst, const char *src) 439 { 440 const char *end = src + BPF_OBJ_NAME_LEN; 441 442 memset(dst, 0, BPF_OBJ_NAME_LEN); 443 444 /* Copy all isalnum() and '_' char */ 445 while (src < end && *src) { 446 if (!isalnum(*src) && *src != '_') 447 return -EINVAL; 448 *dst++ = *src++; 449 } 450 451 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */ 452 if (src == end) 453 return -EINVAL; 454 455 return 0; 456 } 457 458 int map_check_no_btf(const struct bpf_map *map, 459 const struct btf *btf, 460 const struct btf_type *key_type, 461 const struct btf_type *value_type) 462 { 463 return -ENOTSUPP; 464 } 465 466 static int map_check_btf(struct bpf_map *map, const struct btf *btf, 467 u32 btf_key_id, u32 btf_value_id) 468 { 469 const struct btf_type *key_type, *value_type; 470 u32 key_size, value_size; 471 int ret = 0; 472 473 key_type = btf_type_id_size(btf, &btf_key_id, &key_size); 474 if (!key_type || key_size != map->key_size) 475 return -EINVAL; 476 477 value_type = btf_type_id_size(btf, &btf_value_id, &value_size); 478 if (!value_type || value_size != map->value_size) 479 return -EINVAL; 480 481 map->spin_lock_off = btf_find_spin_lock(btf, value_type); 482 483 if (map_value_has_spin_lock(map)) { 484 if (map->map_type != BPF_MAP_TYPE_HASH && 485 map->map_type != BPF_MAP_TYPE_ARRAY && 486 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE) 487 return -ENOTSUPP; 488 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) > 489 map->value_size) { 490 WARN_ONCE(1, 491 "verifier bug spin_lock_off %d value_size %d\n", 492 map->spin_lock_off, map->value_size); 493 return -EFAULT; 494 } 495 } 496 497 if (map->ops->map_check_btf) 498 ret = map->ops->map_check_btf(map, btf, key_type, value_type); 499 500 return ret; 501 } 502 503 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id 504 /* called via syscall */ 505 static int map_create(union bpf_attr *attr) 506 { 507 int numa_node = bpf_map_attr_numa_node(attr); 508 struct bpf_map *map; 509 int f_flags; 510 int err; 511 512 err = CHECK_ATTR(BPF_MAP_CREATE); 513 if (err) 514 return -EINVAL; 515 516 f_flags = bpf_get_file_flag(attr->map_flags); 517 if (f_flags < 0) 518 return f_flags; 519 520 if (numa_node != NUMA_NO_NODE && 521 ((unsigned int)numa_node >= nr_node_ids || 522 !node_online(numa_node))) 523 return -EINVAL; 524 525 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ 526 map = find_and_alloc_map(attr); 527 if (IS_ERR(map)) 528 return PTR_ERR(map); 529 530 err = bpf_obj_name_cpy(map->name, attr->map_name); 531 if (err) 532 goto free_map_nouncharge; 533 534 atomic_set(&map->refcnt, 1); 535 atomic_set(&map->usercnt, 1); 536 537 if (attr->btf_key_type_id || attr->btf_value_type_id) { 538 struct btf *btf; 539 540 if (!attr->btf_key_type_id || !attr->btf_value_type_id) { 541 err = -EINVAL; 542 goto free_map_nouncharge; 543 } 544 545 btf = btf_get_by_fd(attr->btf_fd); 546 if (IS_ERR(btf)) { 547 err = PTR_ERR(btf); 548 goto free_map_nouncharge; 549 } 550 551 err = map_check_btf(map, btf, attr->btf_key_type_id, 552 attr->btf_value_type_id); 553 if (err) { 554 btf_put(btf); 555 goto free_map_nouncharge; 556 } 557 558 map->btf = btf; 559 map->btf_key_type_id = attr->btf_key_type_id; 560 map->btf_value_type_id = attr->btf_value_type_id; 561 } else { 562 map->spin_lock_off = -EINVAL; 563 } 564 565 err = security_bpf_map_alloc(map); 566 if (err) 567 goto free_map_nouncharge; 568 569 err = bpf_map_init_memlock(map); 570 if (err) 571 goto free_map_sec; 572 573 err = bpf_map_alloc_id(map); 574 if (err) 575 goto free_map; 576 577 err = bpf_map_new_fd(map, f_flags); 578 if (err < 0) { 579 /* failed to allocate fd. 580 * bpf_map_put_with_uref() is needed because the above 581 * bpf_map_alloc_id() has published the map 582 * to the userspace and the userspace may 583 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. 584 */ 585 bpf_map_put_with_uref(map); 586 return err; 587 } 588 589 return err; 590 591 free_map: 592 bpf_map_release_memlock(map); 593 free_map_sec: 594 security_bpf_map_free(map); 595 free_map_nouncharge: 596 btf_put(map->btf); 597 map->ops->map_free(map); 598 return err; 599 } 600 601 /* if error is returned, fd is released. 602 * On success caller should complete fd access with matching fdput() 603 */ 604 struct bpf_map *__bpf_map_get(struct fd f) 605 { 606 if (!f.file) 607 return ERR_PTR(-EBADF); 608 if (f.file->f_op != &bpf_map_fops) { 609 fdput(f); 610 return ERR_PTR(-EINVAL); 611 } 612 613 return f.file->private_data; 614 } 615 616 /* prog's and map's refcnt limit */ 617 #define BPF_MAX_REFCNT 32768 618 619 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) 620 { 621 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { 622 atomic_dec(&map->refcnt); 623 return ERR_PTR(-EBUSY); 624 } 625 if (uref) 626 atomic_inc(&map->usercnt); 627 return map; 628 } 629 EXPORT_SYMBOL_GPL(bpf_map_inc); 630 631 struct bpf_map *bpf_map_get_with_uref(u32 ufd) 632 { 633 struct fd f = fdget(ufd); 634 struct bpf_map *map; 635 636 map = __bpf_map_get(f); 637 if (IS_ERR(map)) 638 return map; 639 640 map = bpf_map_inc(map, true); 641 fdput(f); 642 643 return map; 644 } 645 646 /* map_idr_lock should have been held */ 647 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, 648 bool uref) 649 { 650 int refold; 651 652 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0); 653 654 if (refold >= BPF_MAX_REFCNT) { 655 __bpf_map_put(map, false); 656 return ERR_PTR(-EBUSY); 657 } 658 659 if (!refold) 660 return ERR_PTR(-ENOENT); 661 662 if (uref) 663 atomic_inc(&map->usercnt); 664 665 return map; 666 } 667 668 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) 669 { 670 return -ENOTSUPP; 671 } 672 673 static void *__bpf_copy_key(void __user *ukey, u64 key_size) 674 { 675 if (key_size) 676 return memdup_user(ukey, key_size); 677 678 if (ukey) 679 return ERR_PTR(-EINVAL); 680 681 return NULL; 682 } 683 684 /* last field in 'union bpf_attr' used by this command */ 685 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags 686 687 static int map_lookup_elem(union bpf_attr *attr) 688 { 689 void __user *ukey = u64_to_user_ptr(attr->key); 690 void __user *uvalue = u64_to_user_ptr(attr->value); 691 int ufd = attr->map_fd; 692 struct bpf_map *map; 693 void *key, *value, *ptr; 694 u32 value_size; 695 struct fd f; 696 int err; 697 698 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) 699 return -EINVAL; 700 701 if (attr->flags & ~BPF_F_LOCK) 702 return -EINVAL; 703 704 f = fdget(ufd); 705 map = __bpf_map_get(f); 706 if (IS_ERR(map)) 707 return PTR_ERR(map); 708 709 if (!(f.file->f_mode & FMODE_CAN_READ)) { 710 err = -EPERM; 711 goto err_put; 712 } 713 714 if ((attr->flags & BPF_F_LOCK) && 715 !map_value_has_spin_lock(map)) { 716 err = -EINVAL; 717 goto err_put; 718 } 719 720 key = __bpf_copy_key(ukey, map->key_size); 721 if (IS_ERR(key)) { 722 err = PTR_ERR(key); 723 goto err_put; 724 } 725 726 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 727 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || 728 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || 729 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) 730 value_size = round_up(map->value_size, 8) * num_possible_cpus(); 731 else if (IS_FD_MAP(map)) 732 value_size = sizeof(u32); 733 else 734 value_size = map->value_size; 735 736 err = -ENOMEM; 737 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 738 if (!value) 739 goto free_key; 740 741 if (bpf_map_is_dev_bound(map)) { 742 err = bpf_map_offload_lookup_elem(map, key, value); 743 goto done; 744 } 745 746 preempt_disable(); 747 this_cpu_inc(bpf_prog_active); 748 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 749 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 750 err = bpf_percpu_hash_copy(map, key, value); 751 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 752 err = bpf_percpu_array_copy(map, key, value); 753 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { 754 err = bpf_percpu_cgroup_storage_copy(map, key, value); 755 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { 756 err = bpf_stackmap_copy(map, key, value); 757 } else if (IS_FD_ARRAY(map)) { 758 err = bpf_fd_array_map_lookup_elem(map, key, value); 759 } else if (IS_FD_HASH(map)) { 760 err = bpf_fd_htab_map_lookup_elem(map, key, value); 761 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { 762 err = bpf_fd_reuseport_array_lookup_elem(map, key, value); 763 } else if (map->map_type == BPF_MAP_TYPE_QUEUE || 764 map->map_type == BPF_MAP_TYPE_STACK) { 765 err = map->ops->map_peek_elem(map, value); 766 } else { 767 rcu_read_lock(); 768 ptr = map->ops->map_lookup_elem(map, key); 769 if (IS_ERR(ptr)) { 770 err = PTR_ERR(ptr); 771 } else if (!ptr) { 772 err = -ENOENT; 773 } else { 774 err = 0; 775 if (attr->flags & BPF_F_LOCK) 776 /* lock 'ptr' and copy everything but lock */ 777 copy_map_value_locked(map, value, ptr, true); 778 else 779 copy_map_value(map, value, ptr); 780 /* mask lock, since value wasn't zero inited */ 781 check_and_init_map_lock(map, value); 782 } 783 rcu_read_unlock(); 784 } 785 this_cpu_dec(bpf_prog_active); 786 preempt_enable(); 787 788 done: 789 if (err) 790 goto free_value; 791 792 err = -EFAULT; 793 if (copy_to_user(uvalue, value, value_size) != 0) 794 goto free_value; 795 796 err = 0; 797 798 free_value: 799 kfree(value); 800 free_key: 801 kfree(key); 802 err_put: 803 fdput(f); 804 return err; 805 } 806 807 static void maybe_wait_bpf_programs(struct bpf_map *map) 808 { 809 /* Wait for any running BPF programs to complete so that 810 * userspace, when we return to it, knows that all programs 811 * that could be running use the new map value. 812 */ 813 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || 814 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) 815 synchronize_rcu(); 816 } 817 818 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags 819 820 static int map_update_elem(union bpf_attr *attr) 821 { 822 void __user *ukey = u64_to_user_ptr(attr->key); 823 void __user *uvalue = u64_to_user_ptr(attr->value); 824 int ufd = attr->map_fd; 825 struct bpf_map *map; 826 void *key, *value; 827 u32 value_size; 828 struct fd f; 829 int err; 830 831 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) 832 return -EINVAL; 833 834 f = fdget(ufd); 835 map = __bpf_map_get(f); 836 if (IS_ERR(map)) 837 return PTR_ERR(map); 838 839 if (!(f.file->f_mode & FMODE_CAN_WRITE)) { 840 err = -EPERM; 841 goto err_put; 842 } 843 844 if ((attr->flags & BPF_F_LOCK) && 845 !map_value_has_spin_lock(map)) { 846 err = -EINVAL; 847 goto err_put; 848 } 849 850 key = __bpf_copy_key(ukey, map->key_size); 851 if (IS_ERR(key)) { 852 err = PTR_ERR(key); 853 goto err_put; 854 } 855 856 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 857 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || 858 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || 859 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) 860 value_size = round_up(map->value_size, 8) * num_possible_cpus(); 861 else 862 value_size = map->value_size; 863 864 err = -ENOMEM; 865 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 866 if (!value) 867 goto free_key; 868 869 err = -EFAULT; 870 if (copy_from_user(value, uvalue, value_size) != 0) 871 goto free_value; 872 873 /* Need to create a kthread, thus must support schedule */ 874 if (bpf_map_is_dev_bound(map)) { 875 err = bpf_map_offload_update_elem(map, key, value, attr->flags); 876 goto out; 877 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP || 878 map->map_type == BPF_MAP_TYPE_SOCKHASH || 879 map->map_type == BPF_MAP_TYPE_SOCKMAP) { 880 err = map->ops->map_update_elem(map, key, value, attr->flags); 881 goto out; 882 } 883 884 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from 885 * inside bpf map update or delete otherwise deadlocks are possible 886 */ 887 preempt_disable(); 888 __this_cpu_inc(bpf_prog_active); 889 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 890 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 891 err = bpf_percpu_hash_update(map, key, value, attr->flags); 892 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 893 err = bpf_percpu_array_update(map, key, value, attr->flags); 894 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { 895 err = bpf_percpu_cgroup_storage_update(map, key, value, 896 attr->flags); 897 } else if (IS_FD_ARRAY(map)) { 898 rcu_read_lock(); 899 err = bpf_fd_array_map_update_elem(map, f.file, key, value, 900 attr->flags); 901 rcu_read_unlock(); 902 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { 903 rcu_read_lock(); 904 err = bpf_fd_htab_map_update_elem(map, f.file, key, value, 905 attr->flags); 906 rcu_read_unlock(); 907 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { 908 /* rcu_read_lock() is not needed */ 909 err = bpf_fd_reuseport_array_update_elem(map, key, value, 910 attr->flags); 911 } else if (map->map_type == BPF_MAP_TYPE_QUEUE || 912 map->map_type == BPF_MAP_TYPE_STACK) { 913 err = map->ops->map_push_elem(map, value, attr->flags); 914 } else { 915 rcu_read_lock(); 916 err = map->ops->map_update_elem(map, key, value, attr->flags); 917 rcu_read_unlock(); 918 } 919 __this_cpu_dec(bpf_prog_active); 920 preempt_enable(); 921 maybe_wait_bpf_programs(map); 922 out: 923 free_value: 924 kfree(value); 925 free_key: 926 kfree(key); 927 err_put: 928 fdput(f); 929 return err; 930 } 931 932 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key 933 934 static int map_delete_elem(union bpf_attr *attr) 935 { 936 void __user *ukey = u64_to_user_ptr(attr->key); 937 int ufd = attr->map_fd; 938 struct bpf_map *map; 939 struct fd f; 940 void *key; 941 int err; 942 943 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) 944 return -EINVAL; 945 946 f = fdget(ufd); 947 map = __bpf_map_get(f); 948 if (IS_ERR(map)) 949 return PTR_ERR(map); 950 951 if (!(f.file->f_mode & FMODE_CAN_WRITE)) { 952 err = -EPERM; 953 goto err_put; 954 } 955 956 key = __bpf_copy_key(ukey, map->key_size); 957 if (IS_ERR(key)) { 958 err = PTR_ERR(key); 959 goto err_put; 960 } 961 962 if (bpf_map_is_dev_bound(map)) { 963 err = bpf_map_offload_delete_elem(map, key); 964 goto out; 965 } 966 967 preempt_disable(); 968 __this_cpu_inc(bpf_prog_active); 969 rcu_read_lock(); 970 err = map->ops->map_delete_elem(map, key); 971 rcu_read_unlock(); 972 __this_cpu_dec(bpf_prog_active); 973 preempt_enable(); 974 maybe_wait_bpf_programs(map); 975 out: 976 kfree(key); 977 err_put: 978 fdput(f); 979 return err; 980 } 981 982 /* last field in 'union bpf_attr' used by this command */ 983 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key 984 985 static int map_get_next_key(union bpf_attr *attr) 986 { 987 void __user *ukey = u64_to_user_ptr(attr->key); 988 void __user *unext_key = u64_to_user_ptr(attr->next_key); 989 int ufd = attr->map_fd; 990 struct bpf_map *map; 991 void *key, *next_key; 992 struct fd f; 993 int err; 994 995 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) 996 return -EINVAL; 997 998 f = fdget(ufd); 999 map = __bpf_map_get(f); 1000 if (IS_ERR(map)) 1001 return PTR_ERR(map); 1002 1003 if (!(f.file->f_mode & FMODE_CAN_READ)) { 1004 err = -EPERM; 1005 goto err_put; 1006 } 1007 1008 if (ukey) { 1009 key = __bpf_copy_key(ukey, map->key_size); 1010 if (IS_ERR(key)) { 1011 err = PTR_ERR(key); 1012 goto err_put; 1013 } 1014 } else { 1015 key = NULL; 1016 } 1017 1018 err = -ENOMEM; 1019 next_key = kmalloc(map->key_size, GFP_USER); 1020 if (!next_key) 1021 goto free_key; 1022 1023 if (bpf_map_is_dev_bound(map)) { 1024 err = bpf_map_offload_get_next_key(map, key, next_key); 1025 goto out; 1026 } 1027 1028 rcu_read_lock(); 1029 err = map->ops->map_get_next_key(map, key, next_key); 1030 rcu_read_unlock(); 1031 out: 1032 if (err) 1033 goto free_next_key; 1034 1035 err = -EFAULT; 1036 if (copy_to_user(unext_key, next_key, map->key_size) != 0) 1037 goto free_next_key; 1038 1039 err = 0; 1040 1041 free_next_key: 1042 kfree(next_key); 1043 free_key: 1044 kfree(key); 1045 err_put: 1046 fdput(f); 1047 return err; 1048 } 1049 1050 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value 1051 1052 static int map_lookup_and_delete_elem(union bpf_attr *attr) 1053 { 1054 void __user *ukey = u64_to_user_ptr(attr->key); 1055 void __user *uvalue = u64_to_user_ptr(attr->value); 1056 int ufd = attr->map_fd; 1057 struct bpf_map *map; 1058 void *key, *value; 1059 u32 value_size; 1060 struct fd f; 1061 int err; 1062 1063 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM)) 1064 return -EINVAL; 1065 1066 f = fdget(ufd); 1067 map = __bpf_map_get(f); 1068 if (IS_ERR(map)) 1069 return PTR_ERR(map); 1070 1071 if (!(f.file->f_mode & FMODE_CAN_WRITE)) { 1072 err = -EPERM; 1073 goto err_put; 1074 } 1075 1076 key = __bpf_copy_key(ukey, map->key_size); 1077 if (IS_ERR(key)) { 1078 err = PTR_ERR(key); 1079 goto err_put; 1080 } 1081 1082 value_size = map->value_size; 1083 1084 err = -ENOMEM; 1085 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 1086 if (!value) 1087 goto free_key; 1088 1089 if (map->map_type == BPF_MAP_TYPE_QUEUE || 1090 map->map_type == BPF_MAP_TYPE_STACK) { 1091 err = map->ops->map_pop_elem(map, value); 1092 } else { 1093 err = -ENOTSUPP; 1094 } 1095 1096 if (err) 1097 goto free_value; 1098 1099 if (copy_to_user(uvalue, value, value_size) != 0) 1100 goto free_value; 1101 1102 err = 0; 1103 1104 free_value: 1105 kfree(value); 1106 free_key: 1107 kfree(key); 1108 err_put: 1109 fdput(f); 1110 return err; 1111 } 1112 1113 static const struct bpf_prog_ops * const bpf_prog_types[] = { 1114 #define BPF_PROG_TYPE(_id, _name) \ 1115 [_id] = & _name ## _prog_ops, 1116 #define BPF_MAP_TYPE(_id, _ops) 1117 #include <linux/bpf_types.h> 1118 #undef BPF_PROG_TYPE 1119 #undef BPF_MAP_TYPE 1120 }; 1121 1122 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) 1123 { 1124 const struct bpf_prog_ops *ops; 1125 1126 if (type >= ARRAY_SIZE(bpf_prog_types)) 1127 return -EINVAL; 1128 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); 1129 ops = bpf_prog_types[type]; 1130 if (!ops) 1131 return -EINVAL; 1132 1133 if (!bpf_prog_is_dev_bound(prog->aux)) 1134 prog->aux->ops = ops; 1135 else 1136 prog->aux->ops = &bpf_offload_prog_ops; 1137 prog->type = type; 1138 return 0; 1139 } 1140 1141 /* drop refcnt on maps used by eBPF program and free auxilary data */ 1142 static void free_used_maps(struct bpf_prog_aux *aux) 1143 { 1144 enum bpf_cgroup_storage_type stype; 1145 int i; 1146 1147 for_each_cgroup_storage_type(stype) { 1148 if (!aux->cgroup_storage[stype]) 1149 continue; 1150 bpf_cgroup_storage_release(aux->prog, 1151 aux->cgroup_storage[stype]); 1152 } 1153 1154 for (i = 0; i < aux->used_map_cnt; i++) 1155 bpf_map_put(aux->used_maps[i]); 1156 1157 kfree(aux->used_maps); 1158 } 1159 1160 int __bpf_prog_charge(struct user_struct *user, u32 pages) 1161 { 1162 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 1163 unsigned long user_bufs; 1164 1165 if (user) { 1166 user_bufs = atomic_long_add_return(pages, &user->locked_vm); 1167 if (user_bufs > memlock_limit) { 1168 atomic_long_sub(pages, &user->locked_vm); 1169 return -EPERM; 1170 } 1171 } 1172 1173 return 0; 1174 } 1175 1176 void __bpf_prog_uncharge(struct user_struct *user, u32 pages) 1177 { 1178 if (user) 1179 atomic_long_sub(pages, &user->locked_vm); 1180 } 1181 1182 static int bpf_prog_charge_memlock(struct bpf_prog *prog) 1183 { 1184 struct user_struct *user = get_current_user(); 1185 int ret; 1186 1187 ret = __bpf_prog_charge(user, prog->pages); 1188 if (ret) { 1189 free_uid(user); 1190 return ret; 1191 } 1192 1193 prog->aux->user = user; 1194 return 0; 1195 } 1196 1197 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) 1198 { 1199 struct user_struct *user = prog->aux->user; 1200 1201 __bpf_prog_uncharge(user, prog->pages); 1202 free_uid(user); 1203 } 1204 1205 static int bpf_prog_alloc_id(struct bpf_prog *prog) 1206 { 1207 int id; 1208 1209 idr_preload(GFP_KERNEL); 1210 spin_lock_bh(&prog_idr_lock); 1211 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); 1212 if (id > 0) 1213 prog->aux->id = id; 1214 spin_unlock_bh(&prog_idr_lock); 1215 idr_preload_end(); 1216 1217 /* id is in [1, INT_MAX) */ 1218 if (WARN_ON_ONCE(!id)) 1219 return -ENOSPC; 1220 1221 return id > 0 ? 0 : id; 1222 } 1223 1224 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock) 1225 { 1226 /* cBPF to eBPF migrations are currently not in the idr store. 1227 * Offloaded programs are removed from the store when their device 1228 * disappears - even if someone grabs an fd to them they are unusable, 1229 * simply waiting for refcnt to drop to be freed. 1230 */ 1231 if (!prog->aux->id) 1232 return; 1233 1234 if (do_idr_lock) 1235 spin_lock_bh(&prog_idr_lock); 1236 else 1237 __acquire(&prog_idr_lock); 1238 1239 idr_remove(&prog_idr, prog->aux->id); 1240 prog->aux->id = 0; 1241 1242 if (do_idr_lock) 1243 spin_unlock_bh(&prog_idr_lock); 1244 else 1245 __release(&prog_idr_lock); 1246 } 1247 1248 static void __bpf_prog_put_rcu(struct rcu_head *rcu) 1249 { 1250 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); 1251 1252 free_used_maps(aux); 1253 bpf_prog_uncharge_memlock(aux->prog); 1254 security_bpf_prog_free(aux); 1255 bpf_prog_free(aux->prog); 1256 } 1257 1258 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) 1259 { 1260 if (atomic_dec_and_test(&prog->aux->refcnt)) { 1261 /* bpf_prog_free_id() must be called first */ 1262 bpf_prog_free_id(prog, do_idr_lock); 1263 bpf_prog_kallsyms_del_all(prog); 1264 btf_put(prog->aux->btf); 1265 kvfree(prog->aux->func_info); 1266 bpf_prog_free_linfo(prog); 1267 1268 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); 1269 } 1270 } 1271 1272 void bpf_prog_put(struct bpf_prog *prog) 1273 { 1274 __bpf_prog_put(prog, true); 1275 } 1276 EXPORT_SYMBOL_GPL(bpf_prog_put); 1277 1278 static int bpf_prog_release(struct inode *inode, struct file *filp) 1279 { 1280 struct bpf_prog *prog = filp->private_data; 1281 1282 bpf_prog_put(prog); 1283 return 0; 1284 } 1285 1286 static void bpf_prog_get_stats(const struct bpf_prog *prog, 1287 struct bpf_prog_stats *stats) 1288 { 1289 u64 nsecs = 0, cnt = 0; 1290 int cpu; 1291 1292 for_each_possible_cpu(cpu) { 1293 const struct bpf_prog_stats *st; 1294 unsigned int start; 1295 u64 tnsecs, tcnt; 1296 1297 st = per_cpu_ptr(prog->aux->stats, cpu); 1298 do { 1299 start = u64_stats_fetch_begin_irq(&st->syncp); 1300 tnsecs = st->nsecs; 1301 tcnt = st->cnt; 1302 } while (u64_stats_fetch_retry_irq(&st->syncp, start)); 1303 nsecs += tnsecs; 1304 cnt += tcnt; 1305 } 1306 stats->nsecs = nsecs; 1307 stats->cnt = cnt; 1308 } 1309 1310 #ifdef CONFIG_PROC_FS 1311 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) 1312 { 1313 const struct bpf_prog *prog = filp->private_data; 1314 char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 1315 struct bpf_prog_stats stats; 1316 1317 bpf_prog_get_stats(prog, &stats); 1318 bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 1319 seq_printf(m, 1320 "prog_type:\t%u\n" 1321 "prog_jited:\t%u\n" 1322 "prog_tag:\t%s\n" 1323 "memlock:\t%llu\n" 1324 "prog_id:\t%u\n" 1325 "run_time_ns:\t%llu\n" 1326 "run_cnt:\t%llu\n", 1327 prog->type, 1328 prog->jited, 1329 prog_tag, 1330 prog->pages * 1ULL << PAGE_SHIFT, 1331 prog->aux->id, 1332 stats.nsecs, 1333 stats.cnt); 1334 } 1335 #endif 1336 1337 const struct file_operations bpf_prog_fops = { 1338 #ifdef CONFIG_PROC_FS 1339 .show_fdinfo = bpf_prog_show_fdinfo, 1340 #endif 1341 .release = bpf_prog_release, 1342 .read = bpf_dummy_read, 1343 .write = bpf_dummy_write, 1344 }; 1345 1346 int bpf_prog_new_fd(struct bpf_prog *prog) 1347 { 1348 int ret; 1349 1350 ret = security_bpf_prog(prog); 1351 if (ret < 0) 1352 return ret; 1353 1354 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, 1355 O_RDWR | O_CLOEXEC); 1356 } 1357 1358 static struct bpf_prog *____bpf_prog_get(struct fd f) 1359 { 1360 if (!f.file) 1361 return ERR_PTR(-EBADF); 1362 if (f.file->f_op != &bpf_prog_fops) { 1363 fdput(f); 1364 return ERR_PTR(-EINVAL); 1365 } 1366 1367 return f.file->private_data; 1368 } 1369 1370 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) 1371 { 1372 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { 1373 atomic_sub(i, &prog->aux->refcnt); 1374 return ERR_PTR(-EBUSY); 1375 } 1376 return prog; 1377 } 1378 EXPORT_SYMBOL_GPL(bpf_prog_add); 1379 1380 void bpf_prog_sub(struct bpf_prog *prog, int i) 1381 { 1382 /* Only to be used for undoing previous bpf_prog_add() in some 1383 * error path. We still know that another entity in our call 1384 * path holds a reference to the program, thus atomic_sub() can 1385 * be safely used in such cases! 1386 */ 1387 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); 1388 } 1389 EXPORT_SYMBOL_GPL(bpf_prog_sub); 1390 1391 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) 1392 { 1393 return bpf_prog_add(prog, 1); 1394 } 1395 EXPORT_SYMBOL_GPL(bpf_prog_inc); 1396 1397 /* prog_idr_lock should have been held */ 1398 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) 1399 { 1400 int refold; 1401 1402 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0); 1403 1404 if (refold >= BPF_MAX_REFCNT) { 1405 __bpf_prog_put(prog, false); 1406 return ERR_PTR(-EBUSY); 1407 } 1408 1409 if (!refold) 1410 return ERR_PTR(-ENOENT); 1411 1412 return prog; 1413 } 1414 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); 1415 1416 bool bpf_prog_get_ok(struct bpf_prog *prog, 1417 enum bpf_prog_type *attach_type, bool attach_drv) 1418 { 1419 /* not an attachment, just a refcount inc, always allow */ 1420 if (!attach_type) 1421 return true; 1422 1423 if (prog->type != *attach_type) 1424 return false; 1425 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) 1426 return false; 1427 1428 return true; 1429 } 1430 1431 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, 1432 bool attach_drv) 1433 { 1434 struct fd f = fdget(ufd); 1435 struct bpf_prog *prog; 1436 1437 prog = ____bpf_prog_get(f); 1438 if (IS_ERR(prog)) 1439 return prog; 1440 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { 1441 prog = ERR_PTR(-EINVAL); 1442 goto out; 1443 } 1444 1445 prog = bpf_prog_inc(prog); 1446 out: 1447 fdput(f); 1448 return prog; 1449 } 1450 1451 struct bpf_prog *bpf_prog_get(u32 ufd) 1452 { 1453 return __bpf_prog_get(ufd, NULL, false); 1454 } 1455 1456 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, 1457 bool attach_drv) 1458 { 1459 return __bpf_prog_get(ufd, &type, attach_drv); 1460 } 1461 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); 1462 1463 /* Initially all BPF programs could be loaded w/o specifying 1464 * expected_attach_type. Later for some of them specifying expected_attach_type 1465 * at load time became required so that program could be validated properly. 1466 * Programs of types that are allowed to be loaded both w/ and w/o (for 1467 * backward compatibility) expected_attach_type, should have the default attach 1468 * type assigned to expected_attach_type for the latter case, so that it can be 1469 * validated later at attach time. 1470 * 1471 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if 1472 * prog type requires it but has some attach types that have to be backward 1473 * compatible. 1474 */ 1475 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) 1476 { 1477 switch (attr->prog_type) { 1478 case BPF_PROG_TYPE_CGROUP_SOCK: 1479 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't 1480 * exist so checking for non-zero is the way to go here. 1481 */ 1482 if (!attr->expected_attach_type) 1483 attr->expected_attach_type = 1484 BPF_CGROUP_INET_SOCK_CREATE; 1485 break; 1486 } 1487 } 1488 1489 static int 1490 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type, 1491 enum bpf_attach_type expected_attach_type) 1492 { 1493 switch (prog_type) { 1494 case BPF_PROG_TYPE_CGROUP_SOCK: 1495 switch (expected_attach_type) { 1496 case BPF_CGROUP_INET_SOCK_CREATE: 1497 case BPF_CGROUP_INET4_POST_BIND: 1498 case BPF_CGROUP_INET6_POST_BIND: 1499 return 0; 1500 default: 1501 return -EINVAL; 1502 } 1503 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 1504 switch (expected_attach_type) { 1505 case BPF_CGROUP_INET4_BIND: 1506 case BPF_CGROUP_INET6_BIND: 1507 case BPF_CGROUP_INET4_CONNECT: 1508 case BPF_CGROUP_INET6_CONNECT: 1509 case BPF_CGROUP_UDP4_SENDMSG: 1510 case BPF_CGROUP_UDP6_SENDMSG: 1511 return 0; 1512 default: 1513 return -EINVAL; 1514 } 1515 default: 1516 return 0; 1517 } 1518 } 1519 1520 /* last field in 'union bpf_attr' used by this command */ 1521 #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt 1522 1523 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) 1524 { 1525 enum bpf_prog_type type = attr->prog_type; 1526 struct bpf_prog *prog; 1527 int err; 1528 char license[128]; 1529 bool is_gpl; 1530 1531 if (CHECK_ATTR(BPF_PROG_LOAD)) 1532 return -EINVAL; 1533 1534 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | BPF_F_ANY_ALIGNMENT)) 1535 return -EINVAL; 1536 1537 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && 1538 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && 1539 !capable(CAP_SYS_ADMIN)) 1540 return -EPERM; 1541 1542 /* copy eBPF program license from user space */ 1543 if (strncpy_from_user(license, u64_to_user_ptr(attr->license), 1544 sizeof(license) - 1) < 0) 1545 return -EFAULT; 1546 license[sizeof(license) - 1] = 0; 1547 1548 /* eBPF programs must be GPL compatible to use GPL-ed functions */ 1549 is_gpl = license_is_gpl_compatible(license); 1550 1551 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS) 1552 return -E2BIG; 1553 if (type != BPF_PROG_TYPE_SOCKET_FILTER && 1554 type != BPF_PROG_TYPE_CGROUP_SKB && 1555 !capable(CAP_SYS_ADMIN)) 1556 return -EPERM; 1557 1558 bpf_prog_load_fixup_attach_type(attr); 1559 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type)) 1560 return -EINVAL; 1561 1562 /* plain bpf_prog allocation */ 1563 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); 1564 if (!prog) 1565 return -ENOMEM; 1566 1567 prog->expected_attach_type = attr->expected_attach_type; 1568 1569 prog->aux->offload_requested = !!attr->prog_ifindex; 1570 1571 err = security_bpf_prog_alloc(prog->aux); 1572 if (err) 1573 goto free_prog_nouncharge; 1574 1575 err = bpf_prog_charge_memlock(prog); 1576 if (err) 1577 goto free_prog_sec; 1578 1579 prog->len = attr->insn_cnt; 1580 1581 err = -EFAULT; 1582 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), 1583 bpf_prog_insn_size(prog)) != 0) 1584 goto free_prog; 1585 1586 prog->orig_prog = NULL; 1587 prog->jited = 0; 1588 1589 atomic_set(&prog->aux->refcnt, 1); 1590 prog->gpl_compatible = is_gpl ? 1 : 0; 1591 1592 if (bpf_prog_is_dev_bound(prog->aux)) { 1593 err = bpf_prog_offload_init(prog, attr); 1594 if (err) 1595 goto free_prog; 1596 } 1597 1598 /* find program type: socket_filter vs tracing_filter */ 1599 err = find_prog_type(type, prog); 1600 if (err < 0) 1601 goto free_prog; 1602 1603 prog->aux->load_time = ktime_get_boot_ns(); 1604 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name); 1605 if (err) 1606 goto free_prog; 1607 1608 /* run eBPF verifier */ 1609 err = bpf_check(&prog, attr, uattr); 1610 if (err < 0) 1611 goto free_used_maps; 1612 1613 prog = bpf_prog_select_runtime(prog, &err); 1614 if (err < 0) 1615 goto free_used_maps; 1616 1617 err = bpf_prog_alloc_id(prog); 1618 if (err) 1619 goto free_used_maps; 1620 1621 err = bpf_prog_new_fd(prog); 1622 if (err < 0) { 1623 /* failed to allocate fd. 1624 * bpf_prog_put() is needed because the above 1625 * bpf_prog_alloc_id() has published the prog 1626 * to the userspace and the userspace may 1627 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID. 1628 */ 1629 bpf_prog_put(prog); 1630 return err; 1631 } 1632 1633 bpf_prog_kallsyms_add(prog); 1634 return err; 1635 1636 free_used_maps: 1637 bpf_prog_free_linfo(prog); 1638 kvfree(prog->aux->func_info); 1639 btf_put(prog->aux->btf); 1640 bpf_prog_kallsyms_del_subprogs(prog); 1641 free_used_maps(prog->aux); 1642 free_prog: 1643 bpf_prog_uncharge_memlock(prog); 1644 free_prog_sec: 1645 security_bpf_prog_free(prog->aux); 1646 free_prog_nouncharge: 1647 bpf_prog_free(prog); 1648 return err; 1649 } 1650 1651 #define BPF_OBJ_LAST_FIELD file_flags 1652 1653 static int bpf_obj_pin(const union bpf_attr *attr) 1654 { 1655 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) 1656 return -EINVAL; 1657 1658 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); 1659 } 1660 1661 static int bpf_obj_get(const union bpf_attr *attr) 1662 { 1663 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || 1664 attr->file_flags & ~BPF_OBJ_FLAG_MASK) 1665 return -EINVAL; 1666 1667 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), 1668 attr->file_flags); 1669 } 1670 1671 struct bpf_raw_tracepoint { 1672 struct bpf_raw_event_map *btp; 1673 struct bpf_prog *prog; 1674 }; 1675 1676 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp) 1677 { 1678 struct bpf_raw_tracepoint *raw_tp = filp->private_data; 1679 1680 if (raw_tp->prog) { 1681 bpf_probe_unregister(raw_tp->btp, raw_tp->prog); 1682 bpf_prog_put(raw_tp->prog); 1683 } 1684 bpf_put_raw_tracepoint(raw_tp->btp); 1685 kfree(raw_tp); 1686 return 0; 1687 } 1688 1689 static const struct file_operations bpf_raw_tp_fops = { 1690 .release = bpf_raw_tracepoint_release, 1691 .read = bpf_dummy_read, 1692 .write = bpf_dummy_write, 1693 }; 1694 1695 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd 1696 1697 static int bpf_raw_tracepoint_open(const union bpf_attr *attr) 1698 { 1699 struct bpf_raw_tracepoint *raw_tp; 1700 struct bpf_raw_event_map *btp; 1701 struct bpf_prog *prog; 1702 char tp_name[128]; 1703 int tp_fd, err; 1704 1705 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name), 1706 sizeof(tp_name) - 1) < 0) 1707 return -EFAULT; 1708 tp_name[sizeof(tp_name) - 1] = 0; 1709 1710 btp = bpf_get_raw_tracepoint(tp_name); 1711 if (!btp) 1712 return -ENOENT; 1713 1714 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); 1715 if (!raw_tp) { 1716 err = -ENOMEM; 1717 goto out_put_btp; 1718 } 1719 raw_tp->btp = btp; 1720 1721 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd, 1722 BPF_PROG_TYPE_RAW_TRACEPOINT); 1723 if (IS_ERR(prog)) { 1724 err = PTR_ERR(prog); 1725 goto out_free_tp; 1726 } 1727 1728 err = bpf_probe_register(raw_tp->btp, prog); 1729 if (err) 1730 goto out_put_prog; 1731 1732 raw_tp->prog = prog; 1733 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, 1734 O_CLOEXEC); 1735 if (tp_fd < 0) { 1736 bpf_probe_unregister(raw_tp->btp, prog); 1737 err = tp_fd; 1738 goto out_put_prog; 1739 } 1740 return tp_fd; 1741 1742 out_put_prog: 1743 bpf_prog_put(prog); 1744 out_free_tp: 1745 kfree(raw_tp); 1746 out_put_btp: 1747 bpf_put_raw_tracepoint(btp); 1748 return err; 1749 } 1750 1751 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, 1752 enum bpf_attach_type attach_type) 1753 { 1754 switch (prog->type) { 1755 case BPF_PROG_TYPE_CGROUP_SOCK: 1756 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 1757 return attach_type == prog->expected_attach_type ? 0 : -EINVAL; 1758 default: 1759 return 0; 1760 } 1761 } 1762 1763 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags 1764 1765 #define BPF_F_ATTACH_MASK \ 1766 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI) 1767 1768 static int bpf_prog_attach(const union bpf_attr *attr) 1769 { 1770 enum bpf_prog_type ptype; 1771 struct bpf_prog *prog; 1772 int ret; 1773 1774 if (!capable(CAP_NET_ADMIN)) 1775 return -EPERM; 1776 1777 if (CHECK_ATTR(BPF_PROG_ATTACH)) 1778 return -EINVAL; 1779 1780 if (attr->attach_flags & ~BPF_F_ATTACH_MASK) 1781 return -EINVAL; 1782 1783 switch (attr->attach_type) { 1784 case BPF_CGROUP_INET_INGRESS: 1785 case BPF_CGROUP_INET_EGRESS: 1786 ptype = BPF_PROG_TYPE_CGROUP_SKB; 1787 break; 1788 case BPF_CGROUP_INET_SOCK_CREATE: 1789 case BPF_CGROUP_INET4_POST_BIND: 1790 case BPF_CGROUP_INET6_POST_BIND: 1791 ptype = BPF_PROG_TYPE_CGROUP_SOCK; 1792 break; 1793 case BPF_CGROUP_INET4_BIND: 1794 case BPF_CGROUP_INET6_BIND: 1795 case BPF_CGROUP_INET4_CONNECT: 1796 case BPF_CGROUP_INET6_CONNECT: 1797 case BPF_CGROUP_UDP4_SENDMSG: 1798 case BPF_CGROUP_UDP6_SENDMSG: 1799 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 1800 break; 1801 case BPF_CGROUP_SOCK_OPS: 1802 ptype = BPF_PROG_TYPE_SOCK_OPS; 1803 break; 1804 case BPF_CGROUP_DEVICE: 1805 ptype = BPF_PROG_TYPE_CGROUP_DEVICE; 1806 break; 1807 case BPF_SK_MSG_VERDICT: 1808 ptype = BPF_PROG_TYPE_SK_MSG; 1809 break; 1810 case BPF_SK_SKB_STREAM_PARSER: 1811 case BPF_SK_SKB_STREAM_VERDICT: 1812 ptype = BPF_PROG_TYPE_SK_SKB; 1813 break; 1814 case BPF_LIRC_MODE2: 1815 ptype = BPF_PROG_TYPE_LIRC_MODE2; 1816 break; 1817 case BPF_FLOW_DISSECTOR: 1818 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR; 1819 break; 1820 default: 1821 return -EINVAL; 1822 } 1823 1824 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 1825 if (IS_ERR(prog)) 1826 return PTR_ERR(prog); 1827 1828 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { 1829 bpf_prog_put(prog); 1830 return -EINVAL; 1831 } 1832 1833 switch (ptype) { 1834 case BPF_PROG_TYPE_SK_SKB: 1835 case BPF_PROG_TYPE_SK_MSG: 1836 ret = sock_map_get_from_fd(attr, prog); 1837 break; 1838 case BPF_PROG_TYPE_LIRC_MODE2: 1839 ret = lirc_prog_attach(attr, prog); 1840 break; 1841 case BPF_PROG_TYPE_FLOW_DISSECTOR: 1842 ret = skb_flow_dissector_bpf_prog_attach(attr, prog); 1843 break; 1844 default: 1845 ret = cgroup_bpf_prog_attach(attr, ptype, prog); 1846 } 1847 1848 if (ret) 1849 bpf_prog_put(prog); 1850 return ret; 1851 } 1852 1853 #define BPF_PROG_DETACH_LAST_FIELD attach_type 1854 1855 static int bpf_prog_detach(const union bpf_attr *attr) 1856 { 1857 enum bpf_prog_type ptype; 1858 1859 if (!capable(CAP_NET_ADMIN)) 1860 return -EPERM; 1861 1862 if (CHECK_ATTR(BPF_PROG_DETACH)) 1863 return -EINVAL; 1864 1865 switch (attr->attach_type) { 1866 case BPF_CGROUP_INET_INGRESS: 1867 case BPF_CGROUP_INET_EGRESS: 1868 ptype = BPF_PROG_TYPE_CGROUP_SKB; 1869 break; 1870 case BPF_CGROUP_INET_SOCK_CREATE: 1871 case BPF_CGROUP_INET4_POST_BIND: 1872 case BPF_CGROUP_INET6_POST_BIND: 1873 ptype = BPF_PROG_TYPE_CGROUP_SOCK; 1874 break; 1875 case BPF_CGROUP_INET4_BIND: 1876 case BPF_CGROUP_INET6_BIND: 1877 case BPF_CGROUP_INET4_CONNECT: 1878 case BPF_CGROUP_INET6_CONNECT: 1879 case BPF_CGROUP_UDP4_SENDMSG: 1880 case BPF_CGROUP_UDP6_SENDMSG: 1881 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 1882 break; 1883 case BPF_CGROUP_SOCK_OPS: 1884 ptype = BPF_PROG_TYPE_SOCK_OPS; 1885 break; 1886 case BPF_CGROUP_DEVICE: 1887 ptype = BPF_PROG_TYPE_CGROUP_DEVICE; 1888 break; 1889 case BPF_SK_MSG_VERDICT: 1890 return sock_map_get_from_fd(attr, NULL); 1891 case BPF_SK_SKB_STREAM_PARSER: 1892 case BPF_SK_SKB_STREAM_VERDICT: 1893 return sock_map_get_from_fd(attr, NULL); 1894 case BPF_LIRC_MODE2: 1895 return lirc_prog_detach(attr); 1896 case BPF_FLOW_DISSECTOR: 1897 return skb_flow_dissector_bpf_prog_detach(attr); 1898 default: 1899 return -EINVAL; 1900 } 1901 1902 return cgroup_bpf_prog_detach(attr, ptype); 1903 } 1904 1905 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt 1906 1907 static int bpf_prog_query(const union bpf_attr *attr, 1908 union bpf_attr __user *uattr) 1909 { 1910 if (!capable(CAP_NET_ADMIN)) 1911 return -EPERM; 1912 if (CHECK_ATTR(BPF_PROG_QUERY)) 1913 return -EINVAL; 1914 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) 1915 return -EINVAL; 1916 1917 switch (attr->query.attach_type) { 1918 case BPF_CGROUP_INET_INGRESS: 1919 case BPF_CGROUP_INET_EGRESS: 1920 case BPF_CGROUP_INET_SOCK_CREATE: 1921 case BPF_CGROUP_INET4_BIND: 1922 case BPF_CGROUP_INET6_BIND: 1923 case BPF_CGROUP_INET4_POST_BIND: 1924 case BPF_CGROUP_INET6_POST_BIND: 1925 case BPF_CGROUP_INET4_CONNECT: 1926 case BPF_CGROUP_INET6_CONNECT: 1927 case BPF_CGROUP_UDP4_SENDMSG: 1928 case BPF_CGROUP_UDP6_SENDMSG: 1929 case BPF_CGROUP_SOCK_OPS: 1930 case BPF_CGROUP_DEVICE: 1931 break; 1932 case BPF_LIRC_MODE2: 1933 return lirc_prog_query(attr, uattr); 1934 default: 1935 return -EINVAL; 1936 } 1937 1938 return cgroup_bpf_prog_query(attr, uattr); 1939 } 1940 1941 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration 1942 1943 static int bpf_prog_test_run(const union bpf_attr *attr, 1944 union bpf_attr __user *uattr) 1945 { 1946 struct bpf_prog *prog; 1947 int ret = -ENOTSUPP; 1948 1949 if (!capable(CAP_SYS_ADMIN)) 1950 return -EPERM; 1951 if (CHECK_ATTR(BPF_PROG_TEST_RUN)) 1952 return -EINVAL; 1953 1954 prog = bpf_prog_get(attr->test.prog_fd); 1955 if (IS_ERR(prog)) 1956 return PTR_ERR(prog); 1957 1958 if (prog->aux->ops->test_run) 1959 ret = prog->aux->ops->test_run(prog, attr, uattr); 1960 1961 bpf_prog_put(prog); 1962 return ret; 1963 } 1964 1965 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id 1966 1967 static int bpf_obj_get_next_id(const union bpf_attr *attr, 1968 union bpf_attr __user *uattr, 1969 struct idr *idr, 1970 spinlock_t *lock) 1971 { 1972 u32 next_id = attr->start_id; 1973 int err = 0; 1974 1975 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) 1976 return -EINVAL; 1977 1978 if (!capable(CAP_SYS_ADMIN)) 1979 return -EPERM; 1980 1981 next_id++; 1982 spin_lock_bh(lock); 1983 if (!idr_get_next(idr, &next_id)) 1984 err = -ENOENT; 1985 spin_unlock_bh(lock); 1986 1987 if (!err) 1988 err = put_user(next_id, &uattr->next_id); 1989 1990 return err; 1991 } 1992 1993 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id 1994 1995 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) 1996 { 1997 struct bpf_prog *prog; 1998 u32 id = attr->prog_id; 1999 int fd; 2000 2001 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) 2002 return -EINVAL; 2003 2004 if (!capable(CAP_SYS_ADMIN)) 2005 return -EPERM; 2006 2007 spin_lock_bh(&prog_idr_lock); 2008 prog = idr_find(&prog_idr, id); 2009 if (prog) 2010 prog = bpf_prog_inc_not_zero(prog); 2011 else 2012 prog = ERR_PTR(-ENOENT); 2013 spin_unlock_bh(&prog_idr_lock); 2014 2015 if (IS_ERR(prog)) 2016 return PTR_ERR(prog); 2017 2018 fd = bpf_prog_new_fd(prog); 2019 if (fd < 0) 2020 bpf_prog_put(prog); 2021 2022 return fd; 2023 } 2024 2025 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags 2026 2027 static int bpf_map_get_fd_by_id(const union bpf_attr *attr) 2028 { 2029 struct bpf_map *map; 2030 u32 id = attr->map_id; 2031 int f_flags; 2032 int fd; 2033 2034 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || 2035 attr->open_flags & ~BPF_OBJ_FLAG_MASK) 2036 return -EINVAL; 2037 2038 if (!capable(CAP_SYS_ADMIN)) 2039 return -EPERM; 2040 2041 f_flags = bpf_get_file_flag(attr->open_flags); 2042 if (f_flags < 0) 2043 return f_flags; 2044 2045 spin_lock_bh(&map_idr_lock); 2046 map = idr_find(&map_idr, id); 2047 if (map) 2048 map = bpf_map_inc_not_zero(map, true); 2049 else 2050 map = ERR_PTR(-ENOENT); 2051 spin_unlock_bh(&map_idr_lock); 2052 2053 if (IS_ERR(map)) 2054 return PTR_ERR(map); 2055 2056 fd = bpf_map_new_fd(map, f_flags); 2057 if (fd < 0) 2058 bpf_map_put_with_uref(map); 2059 2060 return fd; 2061 } 2062 2063 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, 2064 unsigned long addr) 2065 { 2066 int i; 2067 2068 for (i = 0; i < prog->aux->used_map_cnt; i++) 2069 if (prog->aux->used_maps[i] == (void *)addr) 2070 return prog->aux->used_maps[i]; 2071 return NULL; 2072 } 2073 2074 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog) 2075 { 2076 const struct bpf_map *map; 2077 struct bpf_insn *insns; 2078 u64 imm; 2079 int i; 2080 2081 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), 2082 GFP_USER); 2083 if (!insns) 2084 return insns; 2085 2086 for (i = 0; i < prog->len; i++) { 2087 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) { 2088 insns[i].code = BPF_JMP | BPF_CALL; 2089 insns[i].imm = BPF_FUNC_tail_call; 2090 /* fall-through */ 2091 } 2092 if (insns[i].code == (BPF_JMP | BPF_CALL) || 2093 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) { 2094 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) 2095 insns[i].code = BPF_JMP | BPF_CALL; 2096 if (!bpf_dump_raw_ok()) 2097 insns[i].imm = 0; 2098 continue; 2099 } 2100 2101 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW)) 2102 continue; 2103 2104 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; 2105 map = bpf_map_from_imm(prog, imm); 2106 if (map) { 2107 insns[i].src_reg = BPF_PSEUDO_MAP_FD; 2108 insns[i].imm = map->id; 2109 insns[i + 1].imm = 0; 2110 continue; 2111 } 2112 } 2113 2114 return insns; 2115 } 2116 2117 static int set_info_rec_size(struct bpf_prog_info *info) 2118 { 2119 /* 2120 * Ensure info.*_rec_size is the same as kernel expected size 2121 * 2122 * or 2123 * 2124 * Only allow zero *_rec_size if both _rec_size and _cnt are 2125 * zero. In this case, the kernel will set the expected 2126 * _rec_size back to the info. 2127 */ 2128 2129 if ((info->nr_func_info || info->func_info_rec_size) && 2130 info->func_info_rec_size != sizeof(struct bpf_func_info)) 2131 return -EINVAL; 2132 2133 if ((info->nr_line_info || info->line_info_rec_size) && 2134 info->line_info_rec_size != sizeof(struct bpf_line_info)) 2135 return -EINVAL; 2136 2137 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && 2138 info->jited_line_info_rec_size != sizeof(__u64)) 2139 return -EINVAL; 2140 2141 info->func_info_rec_size = sizeof(struct bpf_func_info); 2142 info->line_info_rec_size = sizeof(struct bpf_line_info); 2143 info->jited_line_info_rec_size = sizeof(__u64); 2144 2145 return 0; 2146 } 2147 2148 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, 2149 const union bpf_attr *attr, 2150 union bpf_attr __user *uattr) 2151 { 2152 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); 2153 struct bpf_prog_info info = {}; 2154 u32 info_len = attr->info.info_len; 2155 struct bpf_prog_stats stats; 2156 char __user *uinsns; 2157 u32 ulen; 2158 int err; 2159 2160 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); 2161 if (err) 2162 return err; 2163 info_len = min_t(u32, sizeof(info), info_len); 2164 2165 if (copy_from_user(&info, uinfo, info_len)) 2166 return -EFAULT; 2167 2168 info.type = prog->type; 2169 info.id = prog->aux->id; 2170 info.load_time = prog->aux->load_time; 2171 info.created_by_uid = from_kuid_munged(current_user_ns(), 2172 prog->aux->user->uid); 2173 info.gpl_compatible = prog->gpl_compatible; 2174 2175 memcpy(info.tag, prog->tag, sizeof(prog->tag)); 2176 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); 2177 2178 ulen = info.nr_map_ids; 2179 info.nr_map_ids = prog->aux->used_map_cnt; 2180 ulen = min_t(u32, info.nr_map_ids, ulen); 2181 if (ulen) { 2182 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); 2183 u32 i; 2184 2185 for (i = 0; i < ulen; i++) 2186 if (put_user(prog->aux->used_maps[i]->id, 2187 &user_map_ids[i])) 2188 return -EFAULT; 2189 } 2190 2191 err = set_info_rec_size(&info); 2192 if (err) 2193 return err; 2194 2195 bpf_prog_get_stats(prog, &stats); 2196 info.run_time_ns = stats.nsecs; 2197 info.run_cnt = stats.cnt; 2198 2199 if (!capable(CAP_SYS_ADMIN)) { 2200 info.jited_prog_len = 0; 2201 info.xlated_prog_len = 0; 2202 info.nr_jited_ksyms = 0; 2203 info.nr_jited_func_lens = 0; 2204 info.nr_func_info = 0; 2205 info.nr_line_info = 0; 2206 info.nr_jited_line_info = 0; 2207 goto done; 2208 } 2209 2210 ulen = info.xlated_prog_len; 2211 info.xlated_prog_len = bpf_prog_insn_size(prog); 2212 if (info.xlated_prog_len && ulen) { 2213 struct bpf_insn *insns_sanitized; 2214 bool fault; 2215 2216 if (prog->blinded && !bpf_dump_raw_ok()) { 2217 info.xlated_prog_insns = 0; 2218 goto done; 2219 } 2220 insns_sanitized = bpf_insn_prepare_dump(prog); 2221 if (!insns_sanitized) 2222 return -ENOMEM; 2223 uinsns = u64_to_user_ptr(info.xlated_prog_insns); 2224 ulen = min_t(u32, info.xlated_prog_len, ulen); 2225 fault = copy_to_user(uinsns, insns_sanitized, ulen); 2226 kfree(insns_sanitized); 2227 if (fault) 2228 return -EFAULT; 2229 } 2230 2231 if (bpf_prog_is_dev_bound(prog->aux)) { 2232 err = bpf_prog_offload_info_fill(&info, prog); 2233 if (err) 2234 return err; 2235 goto done; 2236 } 2237 2238 /* NOTE: the following code is supposed to be skipped for offload. 2239 * bpf_prog_offload_info_fill() is the place to fill similar fields 2240 * for offload. 2241 */ 2242 ulen = info.jited_prog_len; 2243 if (prog->aux->func_cnt) { 2244 u32 i; 2245 2246 info.jited_prog_len = 0; 2247 for (i = 0; i < prog->aux->func_cnt; i++) 2248 info.jited_prog_len += prog->aux->func[i]->jited_len; 2249 } else { 2250 info.jited_prog_len = prog->jited_len; 2251 } 2252 2253 if (info.jited_prog_len && ulen) { 2254 if (bpf_dump_raw_ok()) { 2255 uinsns = u64_to_user_ptr(info.jited_prog_insns); 2256 ulen = min_t(u32, info.jited_prog_len, ulen); 2257 2258 /* for multi-function programs, copy the JITed 2259 * instructions for all the functions 2260 */ 2261 if (prog->aux->func_cnt) { 2262 u32 len, free, i; 2263 u8 *img; 2264 2265 free = ulen; 2266 for (i = 0; i < prog->aux->func_cnt; i++) { 2267 len = prog->aux->func[i]->jited_len; 2268 len = min_t(u32, len, free); 2269 img = (u8 *) prog->aux->func[i]->bpf_func; 2270 if (copy_to_user(uinsns, img, len)) 2271 return -EFAULT; 2272 uinsns += len; 2273 free -= len; 2274 if (!free) 2275 break; 2276 } 2277 } else { 2278 if (copy_to_user(uinsns, prog->bpf_func, ulen)) 2279 return -EFAULT; 2280 } 2281 } else { 2282 info.jited_prog_insns = 0; 2283 } 2284 } 2285 2286 ulen = info.nr_jited_ksyms; 2287 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; 2288 if (ulen) { 2289 if (bpf_dump_raw_ok()) { 2290 unsigned long ksym_addr; 2291 u64 __user *user_ksyms; 2292 u32 i; 2293 2294 /* copy the address of the kernel symbol 2295 * corresponding to each function 2296 */ 2297 ulen = min_t(u32, info.nr_jited_ksyms, ulen); 2298 user_ksyms = u64_to_user_ptr(info.jited_ksyms); 2299 if (prog->aux->func_cnt) { 2300 for (i = 0; i < ulen; i++) { 2301 ksym_addr = (unsigned long) 2302 prog->aux->func[i]->bpf_func; 2303 if (put_user((u64) ksym_addr, 2304 &user_ksyms[i])) 2305 return -EFAULT; 2306 } 2307 } else { 2308 ksym_addr = (unsigned long) prog->bpf_func; 2309 if (put_user((u64) ksym_addr, &user_ksyms[0])) 2310 return -EFAULT; 2311 } 2312 } else { 2313 info.jited_ksyms = 0; 2314 } 2315 } 2316 2317 ulen = info.nr_jited_func_lens; 2318 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; 2319 if (ulen) { 2320 if (bpf_dump_raw_ok()) { 2321 u32 __user *user_lens; 2322 u32 func_len, i; 2323 2324 /* copy the JITed image lengths for each function */ 2325 ulen = min_t(u32, info.nr_jited_func_lens, ulen); 2326 user_lens = u64_to_user_ptr(info.jited_func_lens); 2327 if (prog->aux->func_cnt) { 2328 for (i = 0; i < ulen; i++) { 2329 func_len = 2330 prog->aux->func[i]->jited_len; 2331 if (put_user(func_len, &user_lens[i])) 2332 return -EFAULT; 2333 } 2334 } else { 2335 func_len = prog->jited_len; 2336 if (put_user(func_len, &user_lens[0])) 2337 return -EFAULT; 2338 } 2339 } else { 2340 info.jited_func_lens = 0; 2341 } 2342 } 2343 2344 if (prog->aux->btf) 2345 info.btf_id = btf_id(prog->aux->btf); 2346 2347 ulen = info.nr_func_info; 2348 info.nr_func_info = prog->aux->func_info_cnt; 2349 if (info.nr_func_info && ulen) { 2350 char __user *user_finfo; 2351 2352 user_finfo = u64_to_user_ptr(info.func_info); 2353 ulen = min_t(u32, info.nr_func_info, ulen); 2354 if (copy_to_user(user_finfo, prog->aux->func_info, 2355 info.func_info_rec_size * ulen)) 2356 return -EFAULT; 2357 } 2358 2359 ulen = info.nr_line_info; 2360 info.nr_line_info = prog->aux->nr_linfo; 2361 if (info.nr_line_info && ulen) { 2362 __u8 __user *user_linfo; 2363 2364 user_linfo = u64_to_user_ptr(info.line_info); 2365 ulen = min_t(u32, info.nr_line_info, ulen); 2366 if (copy_to_user(user_linfo, prog->aux->linfo, 2367 info.line_info_rec_size * ulen)) 2368 return -EFAULT; 2369 } 2370 2371 ulen = info.nr_jited_line_info; 2372 if (prog->aux->jited_linfo) 2373 info.nr_jited_line_info = prog->aux->nr_linfo; 2374 else 2375 info.nr_jited_line_info = 0; 2376 if (info.nr_jited_line_info && ulen) { 2377 if (bpf_dump_raw_ok()) { 2378 __u64 __user *user_linfo; 2379 u32 i; 2380 2381 user_linfo = u64_to_user_ptr(info.jited_line_info); 2382 ulen = min_t(u32, info.nr_jited_line_info, ulen); 2383 for (i = 0; i < ulen; i++) { 2384 if (put_user((__u64)(long)prog->aux->jited_linfo[i], 2385 &user_linfo[i])) 2386 return -EFAULT; 2387 } 2388 } else { 2389 info.jited_line_info = 0; 2390 } 2391 } 2392 2393 ulen = info.nr_prog_tags; 2394 info.nr_prog_tags = prog->aux->func_cnt ? : 1; 2395 if (ulen) { 2396 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; 2397 u32 i; 2398 2399 user_prog_tags = u64_to_user_ptr(info.prog_tags); 2400 ulen = min_t(u32, info.nr_prog_tags, ulen); 2401 if (prog->aux->func_cnt) { 2402 for (i = 0; i < ulen; i++) { 2403 if (copy_to_user(user_prog_tags[i], 2404 prog->aux->func[i]->tag, 2405 BPF_TAG_SIZE)) 2406 return -EFAULT; 2407 } 2408 } else { 2409 if (copy_to_user(user_prog_tags[0], 2410 prog->tag, BPF_TAG_SIZE)) 2411 return -EFAULT; 2412 } 2413 } 2414 2415 done: 2416 if (copy_to_user(uinfo, &info, info_len) || 2417 put_user(info_len, &uattr->info.info_len)) 2418 return -EFAULT; 2419 2420 return 0; 2421 } 2422 2423 static int bpf_map_get_info_by_fd(struct bpf_map *map, 2424 const union bpf_attr *attr, 2425 union bpf_attr __user *uattr) 2426 { 2427 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); 2428 struct bpf_map_info info = {}; 2429 u32 info_len = attr->info.info_len; 2430 int err; 2431 2432 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); 2433 if (err) 2434 return err; 2435 info_len = min_t(u32, sizeof(info), info_len); 2436 2437 info.type = map->map_type; 2438 info.id = map->id; 2439 info.key_size = map->key_size; 2440 info.value_size = map->value_size; 2441 info.max_entries = map->max_entries; 2442 info.map_flags = map->map_flags; 2443 memcpy(info.name, map->name, sizeof(map->name)); 2444 2445 if (map->btf) { 2446 info.btf_id = btf_id(map->btf); 2447 info.btf_key_type_id = map->btf_key_type_id; 2448 info.btf_value_type_id = map->btf_value_type_id; 2449 } 2450 2451 if (bpf_map_is_dev_bound(map)) { 2452 err = bpf_map_offload_info_fill(&info, map); 2453 if (err) 2454 return err; 2455 } 2456 2457 if (copy_to_user(uinfo, &info, info_len) || 2458 put_user(info_len, &uattr->info.info_len)) 2459 return -EFAULT; 2460 2461 return 0; 2462 } 2463 2464 static int bpf_btf_get_info_by_fd(struct btf *btf, 2465 const union bpf_attr *attr, 2466 union bpf_attr __user *uattr) 2467 { 2468 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); 2469 u32 info_len = attr->info.info_len; 2470 int err; 2471 2472 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len); 2473 if (err) 2474 return err; 2475 2476 return btf_get_info_by_fd(btf, attr, uattr); 2477 } 2478 2479 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info 2480 2481 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, 2482 union bpf_attr __user *uattr) 2483 { 2484 int ufd = attr->info.bpf_fd; 2485 struct fd f; 2486 int err; 2487 2488 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) 2489 return -EINVAL; 2490 2491 f = fdget(ufd); 2492 if (!f.file) 2493 return -EBADFD; 2494 2495 if (f.file->f_op == &bpf_prog_fops) 2496 err = bpf_prog_get_info_by_fd(f.file->private_data, attr, 2497 uattr); 2498 else if (f.file->f_op == &bpf_map_fops) 2499 err = bpf_map_get_info_by_fd(f.file->private_data, attr, 2500 uattr); 2501 else if (f.file->f_op == &btf_fops) 2502 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr); 2503 else 2504 err = -EINVAL; 2505 2506 fdput(f); 2507 return err; 2508 } 2509 2510 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level 2511 2512 static int bpf_btf_load(const union bpf_attr *attr) 2513 { 2514 if (CHECK_ATTR(BPF_BTF_LOAD)) 2515 return -EINVAL; 2516 2517 if (!capable(CAP_SYS_ADMIN)) 2518 return -EPERM; 2519 2520 return btf_new_fd(attr); 2521 } 2522 2523 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id 2524 2525 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) 2526 { 2527 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) 2528 return -EINVAL; 2529 2530 if (!capable(CAP_SYS_ADMIN)) 2531 return -EPERM; 2532 2533 return btf_get_fd_by_id(attr->btf_id); 2534 } 2535 2536 static int bpf_task_fd_query_copy(const union bpf_attr *attr, 2537 union bpf_attr __user *uattr, 2538 u32 prog_id, u32 fd_type, 2539 const char *buf, u64 probe_offset, 2540 u64 probe_addr) 2541 { 2542 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); 2543 u32 len = buf ? strlen(buf) : 0, input_len; 2544 int err = 0; 2545 2546 if (put_user(len, &uattr->task_fd_query.buf_len)) 2547 return -EFAULT; 2548 input_len = attr->task_fd_query.buf_len; 2549 if (input_len && ubuf) { 2550 if (!len) { 2551 /* nothing to copy, just make ubuf NULL terminated */ 2552 char zero = '\0'; 2553 2554 if (put_user(zero, ubuf)) 2555 return -EFAULT; 2556 } else if (input_len >= len + 1) { 2557 /* ubuf can hold the string with NULL terminator */ 2558 if (copy_to_user(ubuf, buf, len + 1)) 2559 return -EFAULT; 2560 } else { 2561 /* ubuf cannot hold the string with NULL terminator, 2562 * do a partial copy with NULL terminator. 2563 */ 2564 char zero = '\0'; 2565 2566 err = -ENOSPC; 2567 if (copy_to_user(ubuf, buf, input_len - 1)) 2568 return -EFAULT; 2569 if (put_user(zero, ubuf + input_len - 1)) 2570 return -EFAULT; 2571 } 2572 } 2573 2574 if (put_user(prog_id, &uattr->task_fd_query.prog_id) || 2575 put_user(fd_type, &uattr->task_fd_query.fd_type) || 2576 put_user(probe_offset, &uattr->task_fd_query.probe_offset) || 2577 put_user(probe_addr, &uattr->task_fd_query.probe_addr)) 2578 return -EFAULT; 2579 2580 return err; 2581 } 2582 2583 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr 2584 2585 static int bpf_task_fd_query(const union bpf_attr *attr, 2586 union bpf_attr __user *uattr) 2587 { 2588 pid_t pid = attr->task_fd_query.pid; 2589 u32 fd = attr->task_fd_query.fd; 2590 const struct perf_event *event; 2591 struct files_struct *files; 2592 struct task_struct *task; 2593 struct file *file; 2594 int err; 2595 2596 if (CHECK_ATTR(BPF_TASK_FD_QUERY)) 2597 return -EINVAL; 2598 2599 if (!capable(CAP_SYS_ADMIN)) 2600 return -EPERM; 2601 2602 if (attr->task_fd_query.flags != 0) 2603 return -EINVAL; 2604 2605 task = get_pid_task(find_vpid(pid), PIDTYPE_PID); 2606 if (!task) 2607 return -ENOENT; 2608 2609 files = get_files_struct(task); 2610 put_task_struct(task); 2611 if (!files) 2612 return -ENOENT; 2613 2614 err = 0; 2615 spin_lock(&files->file_lock); 2616 file = fcheck_files(files, fd); 2617 if (!file) 2618 err = -EBADF; 2619 else 2620 get_file(file); 2621 spin_unlock(&files->file_lock); 2622 put_files_struct(files); 2623 2624 if (err) 2625 goto out; 2626 2627 if (file->f_op == &bpf_raw_tp_fops) { 2628 struct bpf_raw_tracepoint *raw_tp = file->private_data; 2629 struct bpf_raw_event_map *btp = raw_tp->btp; 2630 2631 err = bpf_task_fd_query_copy(attr, uattr, 2632 raw_tp->prog->aux->id, 2633 BPF_FD_TYPE_RAW_TRACEPOINT, 2634 btp->tp->name, 0, 0); 2635 goto put_file; 2636 } 2637 2638 event = perf_get_event(file); 2639 if (!IS_ERR(event)) { 2640 u64 probe_offset, probe_addr; 2641 u32 prog_id, fd_type; 2642 const char *buf; 2643 2644 err = bpf_get_perf_event_info(event, &prog_id, &fd_type, 2645 &buf, &probe_offset, 2646 &probe_addr); 2647 if (!err) 2648 err = bpf_task_fd_query_copy(attr, uattr, prog_id, 2649 fd_type, buf, 2650 probe_offset, 2651 probe_addr); 2652 goto put_file; 2653 } 2654 2655 err = -ENOTSUPP; 2656 put_file: 2657 fput(file); 2658 out: 2659 return err; 2660 } 2661 2662 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) 2663 { 2664 union bpf_attr attr = {}; 2665 int err; 2666 2667 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) 2668 return -EPERM; 2669 2670 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); 2671 if (err) 2672 return err; 2673 size = min_t(u32, size, sizeof(attr)); 2674 2675 /* copy attributes from user space, may be less than sizeof(bpf_attr) */ 2676 if (copy_from_user(&attr, uattr, size) != 0) 2677 return -EFAULT; 2678 2679 err = security_bpf(cmd, &attr, size); 2680 if (err < 0) 2681 return err; 2682 2683 switch (cmd) { 2684 case BPF_MAP_CREATE: 2685 err = map_create(&attr); 2686 break; 2687 case BPF_MAP_LOOKUP_ELEM: 2688 err = map_lookup_elem(&attr); 2689 break; 2690 case BPF_MAP_UPDATE_ELEM: 2691 err = map_update_elem(&attr); 2692 break; 2693 case BPF_MAP_DELETE_ELEM: 2694 err = map_delete_elem(&attr); 2695 break; 2696 case BPF_MAP_GET_NEXT_KEY: 2697 err = map_get_next_key(&attr); 2698 break; 2699 case BPF_PROG_LOAD: 2700 err = bpf_prog_load(&attr, uattr); 2701 break; 2702 case BPF_OBJ_PIN: 2703 err = bpf_obj_pin(&attr); 2704 break; 2705 case BPF_OBJ_GET: 2706 err = bpf_obj_get(&attr); 2707 break; 2708 case BPF_PROG_ATTACH: 2709 err = bpf_prog_attach(&attr); 2710 break; 2711 case BPF_PROG_DETACH: 2712 err = bpf_prog_detach(&attr); 2713 break; 2714 case BPF_PROG_QUERY: 2715 err = bpf_prog_query(&attr, uattr); 2716 break; 2717 case BPF_PROG_TEST_RUN: 2718 err = bpf_prog_test_run(&attr, uattr); 2719 break; 2720 case BPF_PROG_GET_NEXT_ID: 2721 err = bpf_obj_get_next_id(&attr, uattr, 2722 &prog_idr, &prog_idr_lock); 2723 break; 2724 case BPF_MAP_GET_NEXT_ID: 2725 err = bpf_obj_get_next_id(&attr, uattr, 2726 &map_idr, &map_idr_lock); 2727 break; 2728 case BPF_PROG_GET_FD_BY_ID: 2729 err = bpf_prog_get_fd_by_id(&attr); 2730 break; 2731 case BPF_MAP_GET_FD_BY_ID: 2732 err = bpf_map_get_fd_by_id(&attr); 2733 break; 2734 case BPF_OBJ_GET_INFO_BY_FD: 2735 err = bpf_obj_get_info_by_fd(&attr, uattr); 2736 break; 2737 case BPF_RAW_TRACEPOINT_OPEN: 2738 err = bpf_raw_tracepoint_open(&attr); 2739 break; 2740 case BPF_BTF_LOAD: 2741 err = bpf_btf_load(&attr); 2742 break; 2743 case BPF_BTF_GET_FD_BY_ID: 2744 err = bpf_btf_get_fd_by_id(&attr); 2745 break; 2746 case BPF_TASK_FD_QUERY: 2747 err = bpf_task_fd_query(&attr, uattr); 2748 break; 2749 case BPF_MAP_LOOKUP_AND_DELETE_ELEM: 2750 err = map_lookup_and_delete_elem(&attr); 2751 break; 2752 default: 2753 err = -EINVAL; 2754 break; 2755 } 2756 2757 return err; 2758 } 2759