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/btf.h> 15 #include <linux/syscalls.h> 16 #include <linux/slab.h> 17 #include <linux/sched/signal.h> 18 #include <linux/vmalloc.h> 19 #include <linux/mmzone.h> 20 #include <linux/anon_inodes.h> 21 #include <linux/file.h> 22 #include <linux/license.h> 23 #include <linux/filter.h> 24 #include <linux/version.h> 25 #include <linux/kernel.h> 26 #include <linux/idr.h> 27 #include <linux/cred.h> 28 #include <linux/timekeeping.h> 29 #include <linux/ctype.h> 30 #include <linux/btf.h> 31 32 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ 33 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ 34 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ 35 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) 36 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) 37 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map)) 38 39 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) 40 41 DEFINE_PER_CPU(int, bpf_prog_active); 42 static DEFINE_IDR(prog_idr); 43 static DEFINE_SPINLOCK(prog_idr_lock); 44 static DEFINE_IDR(map_idr); 45 static DEFINE_SPINLOCK(map_idr_lock); 46 47 int sysctl_unprivileged_bpf_disabled __read_mostly; 48 49 static const struct bpf_map_ops * const bpf_map_types[] = { 50 #define BPF_PROG_TYPE(_id, _ops) 51 #define BPF_MAP_TYPE(_id, _ops) \ 52 [_id] = &_ops, 53 #include <linux/bpf_types.h> 54 #undef BPF_PROG_TYPE 55 #undef BPF_MAP_TYPE 56 }; 57 58 /* 59 * If we're handed a bigger struct than we know of, ensure all the unknown bits 60 * are 0 - i.e. new user-space does not rely on any kernel feature extensions 61 * we don't know about yet. 62 * 63 * There is a ToCToU between this function call and the following 64 * copy_from_user() call. However, this is not a concern since this function is 65 * meant to be a future-proofing of bits. 66 */ 67 static int check_uarg_tail_zero(void __user *uaddr, 68 size_t expected_size, 69 size_t actual_size) 70 { 71 unsigned char __user *addr; 72 unsigned char __user *end; 73 unsigned char val; 74 int err; 75 76 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ 77 return -E2BIG; 78 79 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size))) 80 return -EFAULT; 81 82 if (actual_size <= expected_size) 83 return 0; 84 85 addr = uaddr + expected_size; 86 end = uaddr + actual_size; 87 88 for (; addr < end; addr++) { 89 err = get_user(val, addr); 90 if (err) 91 return err; 92 if (val) 93 return -E2BIG; 94 } 95 96 return 0; 97 } 98 99 const struct bpf_map_ops bpf_map_offload_ops = { 100 .map_alloc = bpf_map_offload_map_alloc, 101 .map_free = bpf_map_offload_map_free, 102 }; 103 104 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) 105 { 106 const struct bpf_map_ops *ops; 107 struct bpf_map *map; 108 int err; 109 110 if (attr->map_type >= ARRAY_SIZE(bpf_map_types)) 111 return ERR_PTR(-EINVAL); 112 ops = bpf_map_types[attr->map_type]; 113 if (!ops) 114 return ERR_PTR(-EINVAL); 115 116 if (ops->map_alloc_check) { 117 err = ops->map_alloc_check(attr); 118 if (err) 119 return ERR_PTR(err); 120 } 121 if (attr->map_ifindex) 122 ops = &bpf_map_offload_ops; 123 map = ops->map_alloc(attr); 124 if (IS_ERR(map)) 125 return map; 126 map->ops = ops; 127 map->map_type = attr->map_type; 128 return map; 129 } 130 131 void *bpf_map_area_alloc(size_t size, int numa_node) 132 { 133 /* We definitely need __GFP_NORETRY, so OOM killer doesn't 134 * trigger under memory pressure as we really just want to 135 * fail instead. 136 */ 137 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO; 138 void *area; 139 140 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { 141 area = kmalloc_node(size, GFP_USER | flags, numa_node); 142 if (area != NULL) 143 return area; 144 } 145 146 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags, 147 __builtin_return_address(0)); 148 } 149 150 void bpf_map_area_free(void *area) 151 { 152 kvfree(area); 153 } 154 155 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) 156 { 157 map->map_type = attr->map_type; 158 map->key_size = attr->key_size; 159 map->value_size = attr->value_size; 160 map->max_entries = attr->max_entries; 161 map->map_flags = attr->map_flags; 162 map->numa_node = bpf_map_attr_numa_node(attr); 163 } 164 165 int bpf_map_precharge_memlock(u32 pages) 166 { 167 struct user_struct *user = get_current_user(); 168 unsigned long memlock_limit, cur; 169 170 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 171 cur = atomic_long_read(&user->locked_vm); 172 free_uid(user); 173 if (cur + pages > memlock_limit) 174 return -EPERM; 175 return 0; 176 } 177 178 static int bpf_map_charge_memlock(struct bpf_map *map) 179 { 180 struct user_struct *user = get_current_user(); 181 unsigned long memlock_limit; 182 183 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 184 185 atomic_long_add(map->pages, &user->locked_vm); 186 187 if (atomic_long_read(&user->locked_vm) > memlock_limit) { 188 atomic_long_sub(map->pages, &user->locked_vm); 189 free_uid(user); 190 return -EPERM; 191 } 192 map->user = user; 193 return 0; 194 } 195 196 static void bpf_map_uncharge_memlock(struct bpf_map *map) 197 { 198 struct user_struct *user = map->user; 199 200 atomic_long_sub(map->pages, &user->locked_vm); 201 free_uid(user); 202 } 203 204 static int bpf_map_alloc_id(struct bpf_map *map) 205 { 206 int id; 207 208 idr_preload(GFP_KERNEL); 209 spin_lock_bh(&map_idr_lock); 210 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); 211 if (id > 0) 212 map->id = id; 213 spin_unlock_bh(&map_idr_lock); 214 idr_preload_end(); 215 216 if (WARN_ON_ONCE(!id)) 217 return -ENOSPC; 218 219 return id > 0 ? 0 : id; 220 } 221 222 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) 223 { 224 unsigned long flags; 225 226 /* Offloaded maps are removed from the IDR store when their device 227 * disappears - even if someone holds an fd to them they are unusable, 228 * the memory is gone, all ops will fail; they are simply waiting for 229 * refcnt to drop to be freed. 230 */ 231 if (!map->id) 232 return; 233 234 if (do_idr_lock) 235 spin_lock_irqsave(&map_idr_lock, flags); 236 else 237 __acquire(&map_idr_lock); 238 239 idr_remove(&map_idr, map->id); 240 map->id = 0; 241 242 if (do_idr_lock) 243 spin_unlock_irqrestore(&map_idr_lock, flags); 244 else 245 __release(&map_idr_lock); 246 } 247 248 /* called from workqueue */ 249 static void bpf_map_free_deferred(struct work_struct *work) 250 { 251 struct bpf_map *map = container_of(work, struct bpf_map, work); 252 253 bpf_map_uncharge_memlock(map); 254 security_bpf_map_free(map); 255 btf_put(map->btf); 256 /* implementation dependent freeing */ 257 map->ops->map_free(map); 258 } 259 260 static void bpf_map_put_uref(struct bpf_map *map) 261 { 262 if (atomic_dec_and_test(&map->usercnt)) { 263 if (map->ops->map_release_uref) 264 map->ops->map_release_uref(map); 265 } 266 } 267 268 /* decrement map refcnt and schedule it for freeing via workqueue 269 * (unrelying map implementation ops->map_free() might sleep) 270 */ 271 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) 272 { 273 if (atomic_dec_and_test(&map->refcnt)) { 274 /* bpf_map_free_id() must be called first */ 275 bpf_map_free_id(map, do_idr_lock); 276 INIT_WORK(&map->work, bpf_map_free_deferred); 277 schedule_work(&map->work); 278 } 279 } 280 281 void bpf_map_put(struct bpf_map *map) 282 { 283 __bpf_map_put(map, true); 284 } 285 EXPORT_SYMBOL_GPL(bpf_map_put); 286 287 void bpf_map_put_with_uref(struct bpf_map *map) 288 { 289 bpf_map_put_uref(map); 290 bpf_map_put(map); 291 } 292 293 static int bpf_map_release(struct inode *inode, struct file *filp) 294 { 295 struct bpf_map *map = filp->private_data; 296 297 if (map->ops->map_release) 298 map->ops->map_release(map, filp); 299 300 bpf_map_put_with_uref(map); 301 return 0; 302 } 303 304 #ifdef CONFIG_PROC_FS 305 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) 306 { 307 const struct bpf_map *map = filp->private_data; 308 const struct bpf_array *array; 309 u32 owner_prog_type = 0; 310 u32 owner_jited = 0; 311 312 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { 313 array = container_of(map, struct bpf_array, map); 314 owner_prog_type = array->owner_prog_type; 315 owner_jited = array->owner_jited; 316 } 317 318 seq_printf(m, 319 "map_type:\t%u\n" 320 "key_size:\t%u\n" 321 "value_size:\t%u\n" 322 "max_entries:\t%u\n" 323 "map_flags:\t%#x\n" 324 "memlock:\t%llu\n", 325 map->map_type, 326 map->key_size, 327 map->value_size, 328 map->max_entries, 329 map->map_flags, 330 map->pages * 1ULL << PAGE_SHIFT); 331 332 if (owner_prog_type) { 333 seq_printf(m, "owner_prog_type:\t%u\n", 334 owner_prog_type); 335 seq_printf(m, "owner_jited:\t%u\n", 336 owner_jited); 337 } 338 } 339 #endif 340 341 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, 342 loff_t *ppos) 343 { 344 /* We need this handler such that alloc_file() enables 345 * f_mode with FMODE_CAN_READ. 346 */ 347 return -EINVAL; 348 } 349 350 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, 351 size_t siz, loff_t *ppos) 352 { 353 /* We need this handler such that alloc_file() enables 354 * f_mode with FMODE_CAN_WRITE. 355 */ 356 return -EINVAL; 357 } 358 359 const struct file_operations bpf_map_fops = { 360 #ifdef CONFIG_PROC_FS 361 .show_fdinfo = bpf_map_show_fdinfo, 362 #endif 363 .release = bpf_map_release, 364 .read = bpf_dummy_read, 365 .write = bpf_dummy_write, 366 }; 367 368 int bpf_map_new_fd(struct bpf_map *map, int flags) 369 { 370 int ret; 371 372 ret = security_bpf_map(map, OPEN_FMODE(flags)); 373 if (ret < 0) 374 return ret; 375 376 return anon_inode_getfd("bpf-map", &bpf_map_fops, map, 377 flags | O_CLOEXEC); 378 } 379 380 int bpf_get_file_flag(int flags) 381 { 382 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) 383 return -EINVAL; 384 if (flags & BPF_F_RDONLY) 385 return O_RDONLY; 386 if (flags & BPF_F_WRONLY) 387 return O_WRONLY; 388 return O_RDWR; 389 } 390 391 /* helper macro to check that unused fields 'union bpf_attr' are zero */ 392 #define CHECK_ATTR(CMD) \ 393 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ 394 sizeof(attr->CMD##_LAST_FIELD), 0, \ 395 sizeof(*attr) - \ 396 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ 397 sizeof(attr->CMD##_LAST_FIELD)) != NULL 398 399 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes. 400 * Return 0 on success and < 0 on error. 401 */ 402 static int bpf_obj_name_cpy(char *dst, const char *src) 403 { 404 const char *end = src + BPF_OBJ_NAME_LEN; 405 406 memset(dst, 0, BPF_OBJ_NAME_LEN); 407 408 /* Copy all isalnum() and '_' char */ 409 while (src < end && *src) { 410 if (!isalnum(*src) && *src != '_') 411 return -EINVAL; 412 *dst++ = *src++; 413 } 414 415 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */ 416 if (src == end) 417 return -EINVAL; 418 419 return 0; 420 } 421 422 #define BPF_MAP_CREATE_LAST_FIELD btf_value_id 423 /* called via syscall */ 424 static int map_create(union bpf_attr *attr) 425 { 426 int numa_node = bpf_map_attr_numa_node(attr); 427 struct bpf_map *map; 428 int f_flags; 429 int err; 430 431 err = CHECK_ATTR(BPF_MAP_CREATE); 432 if (err) 433 return -EINVAL; 434 435 f_flags = bpf_get_file_flag(attr->map_flags); 436 if (f_flags < 0) 437 return f_flags; 438 439 if (numa_node != NUMA_NO_NODE && 440 ((unsigned int)numa_node >= nr_node_ids || 441 !node_online(numa_node))) 442 return -EINVAL; 443 444 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ 445 map = find_and_alloc_map(attr); 446 if (IS_ERR(map)) 447 return PTR_ERR(map); 448 449 err = bpf_obj_name_cpy(map->name, attr->map_name); 450 if (err) 451 goto free_map_nouncharge; 452 453 atomic_set(&map->refcnt, 1); 454 atomic_set(&map->usercnt, 1); 455 456 if (bpf_map_support_seq_show(map) && 457 (attr->btf_key_id || attr->btf_value_id)) { 458 struct btf *btf; 459 460 if (!attr->btf_key_id || !attr->btf_value_id) { 461 err = -EINVAL; 462 goto free_map_nouncharge; 463 } 464 465 btf = btf_get_by_fd(attr->btf_fd); 466 if (IS_ERR(btf)) { 467 err = PTR_ERR(btf); 468 goto free_map_nouncharge; 469 } 470 471 err = map->ops->map_check_btf(map, btf, attr->btf_key_id, 472 attr->btf_value_id); 473 if (err) { 474 btf_put(btf); 475 goto free_map_nouncharge; 476 } 477 478 map->btf = btf; 479 map->btf_key_id = attr->btf_key_id; 480 map->btf_value_id = attr->btf_value_id; 481 } 482 483 err = security_bpf_map_alloc(map); 484 if (err) 485 goto free_map_nouncharge; 486 487 err = bpf_map_charge_memlock(map); 488 if (err) 489 goto free_map_sec; 490 491 err = bpf_map_alloc_id(map); 492 if (err) 493 goto free_map; 494 495 err = bpf_map_new_fd(map, f_flags); 496 if (err < 0) { 497 /* failed to allocate fd. 498 * bpf_map_put() is needed because the above 499 * bpf_map_alloc_id() has published the map 500 * to the userspace and the userspace may 501 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. 502 */ 503 bpf_map_put(map); 504 return err; 505 } 506 507 return err; 508 509 free_map: 510 bpf_map_uncharge_memlock(map); 511 free_map_sec: 512 security_bpf_map_free(map); 513 free_map_nouncharge: 514 btf_put(map->btf); 515 map->ops->map_free(map); 516 return err; 517 } 518 519 /* if error is returned, fd is released. 520 * On success caller should complete fd access with matching fdput() 521 */ 522 struct bpf_map *__bpf_map_get(struct fd f) 523 { 524 if (!f.file) 525 return ERR_PTR(-EBADF); 526 if (f.file->f_op != &bpf_map_fops) { 527 fdput(f); 528 return ERR_PTR(-EINVAL); 529 } 530 531 return f.file->private_data; 532 } 533 534 /* prog's and map's refcnt limit */ 535 #define BPF_MAX_REFCNT 32768 536 537 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) 538 { 539 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { 540 atomic_dec(&map->refcnt); 541 return ERR_PTR(-EBUSY); 542 } 543 if (uref) 544 atomic_inc(&map->usercnt); 545 return map; 546 } 547 EXPORT_SYMBOL_GPL(bpf_map_inc); 548 549 struct bpf_map *bpf_map_get_with_uref(u32 ufd) 550 { 551 struct fd f = fdget(ufd); 552 struct bpf_map *map; 553 554 map = __bpf_map_get(f); 555 if (IS_ERR(map)) 556 return map; 557 558 map = bpf_map_inc(map, true); 559 fdput(f); 560 561 return map; 562 } 563 564 /* map_idr_lock should have been held */ 565 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, 566 bool uref) 567 { 568 int refold; 569 570 refold = __atomic_add_unless(&map->refcnt, 1, 0); 571 572 if (refold >= BPF_MAX_REFCNT) { 573 __bpf_map_put(map, false); 574 return ERR_PTR(-EBUSY); 575 } 576 577 if (!refold) 578 return ERR_PTR(-ENOENT); 579 580 if (uref) 581 atomic_inc(&map->usercnt); 582 583 return map; 584 } 585 586 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) 587 { 588 return -ENOTSUPP; 589 } 590 591 /* last field in 'union bpf_attr' used by this command */ 592 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value 593 594 static int map_lookup_elem(union bpf_attr *attr) 595 { 596 void __user *ukey = u64_to_user_ptr(attr->key); 597 void __user *uvalue = u64_to_user_ptr(attr->value); 598 int ufd = attr->map_fd; 599 struct bpf_map *map; 600 void *key, *value, *ptr; 601 u32 value_size; 602 struct fd f; 603 int err; 604 605 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) 606 return -EINVAL; 607 608 f = fdget(ufd); 609 map = __bpf_map_get(f); 610 if (IS_ERR(map)) 611 return PTR_ERR(map); 612 613 if (!(f.file->f_mode & FMODE_CAN_READ)) { 614 err = -EPERM; 615 goto err_put; 616 } 617 618 key = memdup_user(ukey, map->key_size); 619 if (IS_ERR(key)) { 620 err = PTR_ERR(key); 621 goto err_put; 622 } 623 624 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 625 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || 626 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) 627 value_size = round_up(map->value_size, 8) * num_possible_cpus(); 628 else if (IS_FD_MAP(map)) 629 value_size = sizeof(u32); 630 else 631 value_size = map->value_size; 632 633 err = -ENOMEM; 634 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 635 if (!value) 636 goto free_key; 637 638 if (bpf_map_is_dev_bound(map)) { 639 err = bpf_map_offload_lookup_elem(map, key, value); 640 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 641 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 642 err = bpf_percpu_hash_copy(map, key, value); 643 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 644 err = bpf_percpu_array_copy(map, key, value); 645 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { 646 err = bpf_stackmap_copy(map, key, value); 647 } else if (IS_FD_ARRAY(map)) { 648 err = bpf_fd_array_map_lookup_elem(map, key, value); 649 } else if (IS_FD_HASH(map)) { 650 err = bpf_fd_htab_map_lookup_elem(map, key, value); 651 } else { 652 rcu_read_lock(); 653 ptr = map->ops->map_lookup_elem(map, key); 654 if (ptr) 655 memcpy(value, ptr, value_size); 656 rcu_read_unlock(); 657 err = ptr ? 0 : -ENOENT; 658 } 659 660 if (err) 661 goto free_value; 662 663 err = -EFAULT; 664 if (copy_to_user(uvalue, value, value_size) != 0) 665 goto free_value; 666 667 err = 0; 668 669 free_value: 670 kfree(value); 671 free_key: 672 kfree(key); 673 err_put: 674 fdput(f); 675 return err; 676 } 677 678 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags 679 680 static int map_update_elem(union bpf_attr *attr) 681 { 682 void __user *ukey = u64_to_user_ptr(attr->key); 683 void __user *uvalue = u64_to_user_ptr(attr->value); 684 int ufd = attr->map_fd; 685 struct bpf_map *map; 686 void *key, *value; 687 u32 value_size; 688 struct fd f; 689 int err; 690 691 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) 692 return -EINVAL; 693 694 f = fdget(ufd); 695 map = __bpf_map_get(f); 696 if (IS_ERR(map)) 697 return PTR_ERR(map); 698 699 if (!(f.file->f_mode & FMODE_CAN_WRITE)) { 700 err = -EPERM; 701 goto err_put; 702 } 703 704 key = memdup_user(ukey, map->key_size); 705 if (IS_ERR(key)) { 706 err = PTR_ERR(key); 707 goto err_put; 708 } 709 710 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 711 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || 712 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) 713 value_size = round_up(map->value_size, 8) * num_possible_cpus(); 714 else 715 value_size = map->value_size; 716 717 err = -ENOMEM; 718 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 719 if (!value) 720 goto free_key; 721 722 err = -EFAULT; 723 if (copy_from_user(value, uvalue, value_size) != 0) 724 goto free_value; 725 726 /* Need to create a kthread, thus must support schedule */ 727 if (bpf_map_is_dev_bound(map)) { 728 err = bpf_map_offload_update_elem(map, key, value, attr->flags); 729 goto out; 730 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) { 731 err = map->ops->map_update_elem(map, key, value, attr->flags); 732 goto out; 733 } 734 735 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from 736 * inside bpf map update or delete otherwise deadlocks are possible 737 */ 738 preempt_disable(); 739 __this_cpu_inc(bpf_prog_active); 740 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 741 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 742 err = bpf_percpu_hash_update(map, key, value, attr->flags); 743 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 744 err = bpf_percpu_array_update(map, key, value, attr->flags); 745 } else if (IS_FD_ARRAY(map)) { 746 rcu_read_lock(); 747 err = bpf_fd_array_map_update_elem(map, f.file, key, value, 748 attr->flags); 749 rcu_read_unlock(); 750 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { 751 rcu_read_lock(); 752 err = bpf_fd_htab_map_update_elem(map, f.file, key, value, 753 attr->flags); 754 rcu_read_unlock(); 755 } else { 756 rcu_read_lock(); 757 err = map->ops->map_update_elem(map, key, value, attr->flags); 758 rcu_read_unlock(); 759 } 760 __this_cpu_dec(bpf_prog_active); 761 preempt_enable(); 762 out: 763 free_value: 764 kfree(value); 765 free_key: 766 kfree(key); 767 err_put: 768 fdput(f); 769 return err; 770 } 771 772 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key 773 774 static int map_delete_elem(union bpf_attr *attr) 775 { 776 void __user *ukey = u64_to_user_ptr(attr->key); 777 int ufd = attr->map_fd; 778 struct bpf_map *map; 779 struct fd f; 780 void *key; 781 int err; 782 783 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) 784 return -EINVAL; 785 786 f = fdget(ufd); 787 map = __bpf_map_get(f); 788 if (IS_ERR(map)) 789 return PTR_ERR(map); 790 791 if (!(f.file->f_mode & FMODE_CAN_WRITE)) { 792 err = -EPERM; 793 goto err_put; 794 } 795 796 key = memdup_user(ukey, map->key_size); 797 if (IS_ERR(key)) { 798 err = PTR_ERR(key); 799 goto err_put; 800 } 801 802 if (bpf_map_is_dev_bound(map)) { 803 err = bpf_map_offload_delete_elem(map, key); 804 goto out; 805 } 806 807 preempt_disable(); 808 __this_cpu_inc(bpf_prog_active); 809 rcu_read_lock(); 810 err = map->ops->map_delete_elem(map, key); 811 rcu_read_unlock(); 812 __this_cpu_dec(bpf_prog_active); 813 preempt_enable(); 814 out: 815 kfree(key); 816 err_put: 817 fdput(f); 818 return err; 819 } 820 821 /* last field in 'union bpf_attr' used by this command */ 822 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key 823 824 static int map_get_next_key(union bpf_attr *attr) 825 { 826 void __user *ukey = u64_to_user_ptr(attr->key); 827 void __user *unext_key = u64_to_user_ptr(attr->next_key); 828 int ufd = attr->map_fd; 829 struct bpf_map *map; 830 void *key, *next_key; 831 struct fd f; 832 int err; 833 834 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) 835 return -EINVAL; 836 837 f = fdget(ufd); 838 map = __bpf_map_get(f); 839 if (IS_ERR(map)) 840 return PTR_ERR(map); 841 842 if (!(f.file->f_mode & FMODE_CAN_READ)) { 843 err = -EPERM; 844 goto err_put; 845 } 846 847 if (ukey) { 848 key = memdup_user(ukey, map->key_size); 849 if (IS_ERR(key)) { 850 err = PTR_ERR(key); 851 goto err_put; 852 } 853 } else { 854 key = NULL; 855 } 856 857 err = -ENOMEM; 858 next_key = kmalloc(map->key_size, GFP_USER); 859 if (!next_key) 860 goto free_key; 861 862 if (bpf_map_is_dev_bound(map)) { 863 err = bpf_map_offload_get_next_key(map, key, next_key); 864 goto out; 865 } 866 867 rcu_read_lock(); 868 err = map->ops->map_get_next_key(map, key, next_key); 869 rcu_read_unlock(); 870 out: 871 if (err) 872 goto free_next_key; 873 874 err = -EFAULT; 875 if (copy_to_user(unext_key, next_key, map->key_size) != 0) 876 goto free_next_key; 877 878 err = 0; 879 880 free_next_key: 881 kfree(next_key); 882 free_key: 883 kfree(key); 884 err_put: 885 fdput(f); 886 return err; 887 } 888 889 static const struct bpf_prog_ops * const bpf_prog_types[] = { 890 #define BPF_PROG_TYPE(_id, _name) \ 891 [_id] = & _name ## _prog_ops, 892 #define BPF_MAP_TYPE(_id, _ops) 893 #include <linux/bpf_types.h> 894 #undef BPF_PROG_TYPE 895 #undef BPF_MAP_TYPE 896 }; 897 898 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) 899 { 900 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type]) 901 return -EINVAL; 902 903 if (!bpf_prog_is_dev_bound(prog->aux)) 904 prog->aux->ops = bpf_prog_types[type]; 905 else 906 prog->aux->ops = &bpf_offload_prog_ops; 907 prog->type = type; 908 return 0; 909 } 910 911 /* drop refcnt on maps used by eBPF program and free auxilary data */ 912 static void free_used_maps(struct bpf_prog_aux *aux) 913 { 914 int i; 915 916 for (i = 0; i < aux->used_map_cnt; i++) 917 bpf_map_put(aux->used_maps[i]); 918 919 kfree(aux->used_maps); 920 } 921 922 int __bpf_prog_charge(struct user_struct *user, u32 pages) 923 { 924 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 925 unsigned long user_bufs; 926 927 if (user) { 928 user_bufs = atomic_long_add_return(pages, &user->locked_vm); 929 if (user_bufs > memlock_limit) { 930 atomic_long_sub(pages, &user->locked_vm); 931 return -EPERM; 932 } 933 } 934 935 return 0; 936 } 937 938 void __bpf_prog_uncharge(struct user_struct *user, u32 pages) 939 { 940 if (user) 941 atomic_long_sub(pages, &user->locked_vm); 942 } 943 944 static int bpf_prog_charge_memlock(struct bpf_prog *prog) 945 { 946 struct user_struct *user = get_current_user(); 947 int ret; 948 949 ret = __bpf_prog_charge(user, prog->pages); 950 if (ret) { 951 free_uid(user); 952 return ret; 953 } 954 955 prog->aux->user = user; 956 return 0; 957 } 958 959 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) 960 { 961 struct user_struct *user = prog->aux->user; 962 963 __bpf_prog_uncharge(user, prog->pages); 964 free_uid(user); 965 } 966 967 static int bpf_prog_alloc_id(struct bpf_prog *prog) 968 { 969 int id; 970 971 idr_preload(GFP_KERNEL); 972 spin_lock_bh(&prog_idr_lock); 973 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); 974 if (id > 0) 975 prog->aux->id = id; 976 spin_unlock_bh(&prog_idr_lock); 977 idr_preload_end(); 978 979 /* id is in [1, INT_MAX) */ 980 if (WARN_ON_ONCE(!id)) 981 return -ENOSPC; 982 983 return id > 0 ? 0 : id; 984 } 985 986 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock) 987 { 988 /* cBPF to eBPF migrations are currently not in the idr store. 989 * Offloaded programs are removed from the store when their device 990 * disappears - even if someone grabs an fd to them they are unusable, 991 * simply waiting for refcnt to drop to be freed. 992 */ 993 if (!prog->aux->id) 994 return; 995 996 if (do_idr_lock) 997 spin_lock_bh(&prog_idr_lock); 998 else 999 __acquire(&prog_idr_lock); 1000 1001 idr_remove(&prog_idr, prog->aux->id); 1002 prog->aux->id = 0; 1003 1004 if (do_idr_lock) 1005 spin_unlock_bh(&prog_idr_lock); 1006 else 1007 __release(&prog_idr_lock); 1008 } 1009 1010 static void __bpf_prog_put_rcu(struct rcu_head *rcu) 1011 { 1012 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); 1013 1014 free_used_maps(aux); 1015 bpf_prog_uncharge_memlock(aux->prog); 1016 security_bpf_prog_free(aux); 1017 bpf_prog_free(aux->prog); 1018 } 1019 1020 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) 1021 { 1022 if (atomic_dec_and_test(&prog->aux->refcnt)) { 1023 int i; 1024 1025 /* bpf_prog_free_id() must be called first */ 1026 bpf_prog_free_id(prog, do_idr_lock); 1027 1028 for (i = 0; i < prog->aux->func_cnt; i++) 1029 bpf_prog_kallsyms_del(prog->aux->func[i]); 1030 bpf_prog_kallsyms_del(prog); 1031 1032 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); 1033 } 1034 } 1035 1036 void bpf_prog_put(struct bpf_prog *prog) 1037 { 1038 __bpf_prog_put(prog, true); 1039 } 1040 EXPORT_SYMBOL_GPL(bpf_prog_put); 1041 1042 static int bpf_prog_release(struct inode *inode, struct file *filp) 1043 { 1044 struct bpf_prog *prog = filp->private_data; 1045 1046 bpf_prog_put(prog); 1047 return 0; 1048 } 1049 1050 #ifdef CONFIG_PROC_FS 1051 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) 1052 { 1053 const struct bpf_prog *prog = filp->private_data; 1054 char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 1055 1056 bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 1057 seq_printf(m, 1058 "prog_type:\t%u\n" 1059 "prog_jited:\t%u\n" 1060 "prog_tag:\t%s\n" 1061 "memlock:\t%llu\n", 1062 prog->type, 1063 prog->jited, 1064 prog_tag, 1065 prog->pages * 1ULL << PAGE_SHIFT); 1066 } 1067 #endif 1068 1069 const struct file_operations bpf_prog_fops = { 1070 #ifdef CONFIG_PROC_FS 1071 .show_fdinfo = bpf_prog_show_fdinfo, 1072 #endif 1073 .release = bpf_prog_release, 1074 .read = bpf_dummy_read, 1075 .write = bpf_dummy_write, 1076 }; 1077 1078 int bpf_prog_new_fd(struct bpf_prog *prog) 1079 { 1080 int ret; 1081 1082 ret = security_bpf_prog(prog); 1083 if (ret < 0) 1084 return ret; 1085 1086 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, 1087 O_RDWR | O_CLOEXEC); 1088 } 1089 1090 static struct bpf_prog *____bpf_prog_get(struct fd f) 1091 { 1092 if (!f.file) 1093 return ERR_PTR(-EBADF); 1094 if (f.file->f_op != &bpf_prog_fops) { 1095 fdput(f); 1096 return ERR_PTR(-EINVAL); 1097 } 1098 1099 return f.file->private_data; 1100 } 1101 1102 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) 1103 { 1104 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { 1105 atomic_sub(i, &prog->aux->refcnt); 1106 return ERR_PTR(-EBUSY); 1107 } 1108 return prog; 1109 } 1110 EXPORT_SYMBOL_GPL(bpf_prog_add); 1111 1112 void bpf_prog_sub(struct bpf_prog *prog, int i) 1113 { 1114 /* Only to be used for undoing previous bpf_prog_add() in some 1115 * error path. We still know that another entity in our call 1116 * path holds a reference to the program, thus atomic_sub() can 1117 * be safely used in such cases! 1118 */ 1119 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); 1120 } 1121 EXPORT_SYMBOL_GPL(bpf_prog_sub); 1122 1123 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) 1124 { 1125 return bpf_prog_add(prog, 1); 1126 } 1127 EXPORT_SYMBOL_GPL(bpf_prog_inc); 1128 1129 /* prog_idr_lock should have been held */ 1130 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) 1131 { 1132 int refold; 1133 1134 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0); 1135 1136 if (refold >= BPF_MAX_REFCNT) { 1137 __bpf_prog_put(prog, false); 1138 return ERR_PTR(-EBUSY); 1139 } 1140 1141 if (!refold) 1142 return ERR_PTR(-ENOENT); 1143 1144 return prog; 1145 } 1146 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); 1147 1148 bool bpf_prog_get_ok(struct bpf_prog *prog, 1149 enum bpf_prog_type *attach_type, bool attach_drv) 1150 { 1151 /* not an attachment, just a refcount inc, always allow */ 1152 if (!attach_type) 1153 return true; 1154 1155 if (prog->type != *attach_type) 1156 return false; 1157 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) 1158 return false; 1159 1160 return true; 1161 } 1162 1163 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, 1164 bool attach_drv) 1165 { 1166 struct fd f = fdget(ufd); 1167 struct bpf_prog *prog; 1168 1169 prog = ____bpf_prog_get(f); 1170 if (IS_ERR(prog)) 1171 return prog; 1172 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { 1173 prog = ERR_PTR(-EINVAL); 1174 goto out; 1175 } 1176 1177 prog = bpf_prog_inc(prog); 1178 out: 1179 fdput(f); 1180 return prog; 1181 } 1182 1183 struct bpf_prog *bpf_prog_get(u32 ufd) 1184 { 1185 return __bpf_prog_get(ufd, NULL, false); 1186 } 1187 1188 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, 1189 bool attach_drv) 1190 { 1191 return __bpf_prog_get(ufd, &type, attach_drv); 1192 } 1193 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); 1194 1195 /* Initially all BPF programs could be loaded w/o specifying 1196 * expected_attach_type. Later for some of them specifying expected_attach_type 1197 * at load time became required so that program could be validated properly. 1198 * Programs of types that are allowed to be loaded both w/ and w/o (for 1199 * backward compatibility) expected_attach_type, should have the default attach 1200 * type assigned to expected_attach_type for the latter case, so that it can be 1201 * validated later at attach time. 1202 * 1203 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if 1204 * prog type requires it but has some attach types that have to be backward 1205 * compatible. 1206 */ 1207 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) 1208 { 1209 switch (attr->prog_type) { 1210 case BPF_PROG_TYPE_CGROUP_SOCK: 1211 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't 1212 * exist so checking for non-zero is the way to go here. 1213 */ 1214 if (!attr->expected_attach_type) 1215 attr->expected_attach_type = 1216 BPF_CGROUP_INET_SOCK_CREATE; 1217 break; 1218 } 1219 } 1220 1221 static int 1222 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type, 1223 enum bpf_attach_type expected_attach_type) 1224 { 1225 switch (prog_type) { 1226 case BPF_PROG_TYPE_CGROUP_SOCK: 1227 switch (expected_attach_type) { 1228 case BPF_CGROUP_INET_SOCK_CREATE: 1229 case BPF_CGROUP_INET4_POST_BIND: 1230 case BPF_CGROUP_INET6_POST_BIND: 1231 return 0; 1232 default: 1233 return -EINVAL; 1234 } 1235 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 1236 switch (expected_attach_type) { 1237 case BPF_CGROUP_INET4_BIND: 1238 case BPF_CGROUP_INET6_BIND: 1239 case BPF_CGROUP_INET4_CONNECT: 1240 case BPF_CGROUP_INET6_CONNECT: 1241 return 0; 1242 default: 1243 return -EINVAL; 1244 } 1245 default: 1246 return 0; 1247 } 1248 } 1249 1250 /* last field in 'union bpf_attr' used by this command */ 1251 #define BPF_PROG_LOAD_LAST_FIELD expected_attach_type 1252 1253 static int bpf_prog_load(union bpf_attr *attr) 1254 { 1255 enum bpf_prog_type type = attr->prog_type; 1256 struct bpf_prog *prog; 1257 int err; 1258 char license[128]; 1259 bool is_gpl; 1260 1261 if (CHECK_ATTR(BPF_PROG_LOAD)) 1262 return -EINVAL; 1263 1264 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT) 1265 return -EINVAL; 1266 1267 /* copy eBPF program license from user space */ 1268 if (strncpy_from_user(license, u64_to_user_ptr(attr->license), 1269 sizeof(license) - 1) < 0) 1270 return -EFAULT; 1271 license[sizeof(license) - 1] = 0; 1272 1273 /* eBPF programs must be GPL compatible to use GPL-ed functions */ 1274 is_gpl = license_is_gpl_compatible(license); 1275 1276 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS) 1277 return -E2BIG; 1278 1279 if (type == BPF_PROG_TYPE_KPROBE && 1280 attr->kern_version != LINUX_VERSION_CODE) 1281 return -EINVAL; 1282 1283 if (type != BPF_PROG_TYPE_SOCKET_FILTER && 1284 type != BPF_PROG_TYPE_CGROUP_SKB && 1285 !capable(CAP_SYS_ADMIN)) 1286 return -EPERM; 1287 1288 bpf_prog_load_fixup_attach_type(attr); 1289 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type)) 1290 return -EINVAL; 1291 1292 /* plain bpf_prog allocation */ 1293 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); 1294 if (!prog) 1295 return -ENOMEM; 1296 1297 prog->expected_attach_type = attr->expected_attach_type; 1298 1299 prog->aux->offload_requested = !!attr->prog_ifindex; 1300 1301 err = security_bpf_prog_alloc(prog->aux); 1302 if (err) 1303 goto free_prog_nouncharge; 1304 1305 err = bpf_prog_charge_memlock(prog); 1306 if (err) 1307 goto free_prog_sec; 1308 1309 prog->len = attr->insn_cnt; 1310 1311 err = -EFAULT; 1312 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), 1313 bpf_prog_insn_size(prog)) != 0) 1314 goto free_prog; 1315 1316 prog->orig_prog = NULL; 1317 prog->jited = 0; 1318 1319 atomic_set(&prog->aux->refcnt, 1); 1320 prog->gpl_compatible = is_gpl ? 1 : 0; 1321 1322 if (bpf_prog_is_dev_bound(prog->aux)) { 1323 err = bpf_prog_offload_init(prog, attr); 1324 if (err) 1325 goto free_prog; 1326 } 1327 1328 /* find program type: socket_filter vs tracing_filter */ 1329 err = find_prog_type(type, prog); 1330 if (err < 0) 1331 goto free_prog; 1332 1333 prog->aux->load_time = ktime_get_boot_ns(); 1334 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name); 1335 if (err) 1336 goto free_prog; 1337 1338 /* run eBPF verifier */ 1339 err = bpf_check(&prog, attr); 1340 if (err < 0) 1341 goto free_used_maps; 1342 1343 /* eBPF program is ready to be JITed */ 1344 if (!prog->bpf_func) 1345 prog = bpf_prog_select_runtime(prog, &err); 1346 if (err < 0) 1347 goto free_used_maps; 1348 1349 err = bpf_prog_alloc_id(prog); 1350 if (err) 1351 goto free_used_maps; 1352 1353 err = bpf_prog_new_fd(prog); 1354 if (err < 0) { 1355 /* failed to allocate fd. 1356 * bpf_prog_put() is needed because the above 1357 * bpf_prog_alloc_id() has published the prog 1358 * to the userspace and the userspace may 1359 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID. 1360 */ 1361 bpf_prog_put(prog); 1362 return err; 1363 } 1364 1365 bpf_prog_kallsyms_add(prog); 1366 return err; 1367 1368 free_used_maps: 1369 free_used_maps(prog->aux); 1370 free_prog: 1371 bpf_prog_uncharge_memlock(prog); 1372 free_prog_sec: 1373 security_bpf_prog_free(prog->aux); 1374 free_prog_nouncharge: 1375 bpf_prog_free(prog); 1376 return err; 1377 } 1378 1379 #define BPF_OBJ_LAST_FIELD file_flags 1380 1381 static int bpf_obj_pin(const union bpf_attr *attr) 1382 { 1383 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) 1384 return -EINVAL; 1385 1386 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); 1387 } 1388 1389 static int bpf_obj_get(const union bpf_attr *attr) 1390 { 1391 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || 1392 attr->file_flags & ~BPF_OBJ_FLAG_MASK) 1393 return -EINVAL; 1394 1395 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), 1396 attr->file_flags); 1397 } 1398 1399 struct bpf_raw_tracepoint { 1400 struct bpf_raw_event_map *btp; 1401 struct bpf_prog *prog; 1402 }; 1403 1404 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp) 1405 { 1406 struct bpf_raw_tracepoint *raw_tp = filp->private_data; 1407 1408 if (raw_tp->prog) { 1409 bpf_probe_unregister(raw_tp->btp, raw_tp->prog); 1410 bpf_prog_put(raw_tp->prog); 1411 } 1412 kfree(raw_tp); 1413 return 0; 1414 } 1415 1416 static const struct file_operations bpf_raw_tp_fops = { 1417 .release = bpf_raw_tracepoint_release, 1418 .read = bpf_dummy_read, 1419 .write = bpf_dummy_write, 1420 }; 1421 1422 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd 1423 1424 static int bpf_raw_tracepoint_open(const union bpf_attr *attr) 1425 { 1426 struct bpf_raw_tracepoint *raw_tp; 1427 struct bpf_raw_event_map *btp; 1428 struct bpf_prog *prog; 1429 char tp_name[128]; 1430 int tp_fd, err; 1431 1432 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name), 1433 sizeof(tp_name) - 1) < 0) 1434 return -EFAULT; 1435 tp_name[sizeof(tp_name) - 1] = 0; 1436 1437 btp = bpf_find_raw_tracepoint(tp_name); 1438 if (!btp) 1439 return -ENOENT; 1440 1441 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); 1442 if (!raw_tp) 1443 return -ENOMEM; 1444 raw_tp->btp = btp; 1445 1446 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd, 1447 BPF_PROG_TYPE_RAW_TRACEPOINT); 1448 if (IS_ERR(prog)) { 1449 err = PTR_ERR(prog); 1450 goto out_free_tp; 1451 } 1452 1453 err = bpf_probe_register(raw_tp->btp, prog); 1454 if (err) 1455 goto out_put_prog; 1456 1457 raw_tp->prog = prog; 1458 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, 1459 O_CLOEXEC); 1460 if (tp_fd < 0) { 1461 bpf_probe_unregister(raw_tp->btp, prog); 1462 err = tp_fd; 1463 goto out_put_prog; 1464 } 1465 return tp_fd; 1466 1467 out_put_prog: 1468 bpf_prog_put(prog); 1469 out_free_tp: 1470 kfree(raw_tp); 1471 return err; 1472 } 1473 1474 #ifdef CONFIG_CGROUP_BPF 1475 1476 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, 1477 enum bpf_attach_type attach_type) 1478 { 1479 switch (prog->type) { 1480 case BPF_PROG_TYPE_CGROUP_SOCK: 1481 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 1482 return attach_type == prog->expected_attach_type ? 0 : -EINVAL; 1483 default: 1484 return 0; 1485 } 1486 } 1487 1488 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags 1489 1490 static int sockmap_get_from_fd(const union bpf_attr *attr, 1491 int type, bool attach) 1492 { 1493 struct bpf_prog *prog = NULL; 1494 int ufd = attr->target_fd; 1495 struct bpf_map *map; 1496 struct fd f; 1497 int err; 1498 1499 f = fdget(ufd); 1500 map = __bpf_map_get(f); 1501 if (IS_ERR(map)) 1502 return PTR_ERR(map); 1503 1504 if (attach) { 1505 prog = bpf_prog_get_type(attr->attach_bpf_fd, type); 1506 if (IS_ERR(prog)) { 1507 fdput(f); 1508 return PTR_ERR(prog); 1509 } 1510 } 1511 1512 err = sock_map_prog(map, prog, attr->attach_type); 1513 if (err) { 1514 fdput(f); 1515 if (prog) 1516 bpf_prog_put(prog); 1517 return err; 1518 } 1519 1520 fdput(f); 1521 return 0; 1522 } 1523 1524 #define BPF_F_ATTACH_MASK \ 1525 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI) 1526 1527 static int bpf_prog_attach(const union bpf_attr *attr) 1528 { 1529 enum bpf_prog_type ptype; 1530 struct bpf_prog *prog; 1531 struct cgroup *cgrp; 1532 int ret; 1533 1534 if (!capable(CAP_NET_ADMIN)) 1535 return -EPERM; 1536 1537 if (CHECK_ATTR(BPF_PROG_ATTACH)) 1538 return -EINVAL; 1539 1540 if (attr->attach_flags & ~BPF_F_ATTACH_MASK) 1541 return -EINVAL; 1542 1543 switch (attr->attach_type) { 1544 case BPF_CGROUP_INET_INGRESS: 1545 case BPF_CGROUP_INET_EGRESS: 1546 ptype = BPF_PROG_TYPE_CGROUP_SKB; 1547 break; 1548 case BPF_CGROUP_INET_SOCK_CREATE: 1549 case BPF_CGROUP_INET4_POST_BIND: 1550 case BPF_CGROUP_INET6_POST_BIND: 1551 ptype = BPF_PROG_TYPE_CGROUP_SOCK; 1552 break; 1553 case BPF_CGROUP_INET4_BIND: 1554 case BPF_CGROUP_INET6_BIND: 1555 case BPF_CGROUP_INET4_CONNECT: 1556 case BPF_CGROUP_INET6_CONNECT: 1557 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 1558 break; 1559 case BPF_CGROUP_SOCK_OPS: 1560 ptype = BPF_PROG_TYPE_SOCK_OPS; 1561 break; 1562 case BPF_CGROUP_DEVICE: 1563 ptype = BPF_PROG_TYPE_CGROUP_DEVICE; 1564 break; 1565 case BPF_SK_MSG_VERDICT: 1566 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true); 1567 case BPF_SK_SKB_STREAM_PARSER: 1568 case BPF_SK_SKB_STREAM_VERDICT: 1569 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true); 1570 default: 1571 return -EINVAL; 1572 } 1573 1574 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 1575 if (IS_ERR(prog)) 1576 return PTR_ERR(prog); 1577 1578 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { 1579 bpf_prog_put(prog); 1580 return -EINVAL; 1581 } 1582 1583 cgrp = cgroup_get_from_fd(attr->target_fd); 1584 if (IS_ERR(cgrp)) { 1585 bpf_prog_put(prog); 1586 return PTR_ERR(cgrp); 1587 } 1588 1589 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type, 1590 attr->attach_flags); 1591 if (ret) 1592 bpf_prog_put(prog); 1593 cgroup_put(cgrp); 1594 1595 return ret; 1596 } 1597 1598 #define BPF_PROG_DETACH_LAST_FIELD attach_type 1599 1600 static int bpf_prog_detach(const union bpf_attr *attr) 1601 { 1602 enum bpf_prog_type ptype; 1603 struct bpf_prog *prog; 1604 struct cgroup *cgrp; 1605 int ret; 1606 1607 if (!capable(CAP_NET_ADMIN)) 1608 return -EPERM; 1609 1610 if (CHECK_ATTR(BPF_PROG_DETACH)) 1611 return -EINVAL; 1612 1613 switch (attr->attach_type) { 1614 case BPF_CGROUP_INET_INGRESS: 1615 case BPF_CGROUP_INET_EGRESS: 1616 ptype = BPF_PROG_TYPE_CGROUP_SKB; 1617 break; 1618 case BPF_CGROUP_INET_SOCK_CREATE: 1619 case BPF_CGROUP_INET4_POST_BIND: 1620 case BPF_CGROUP_INET6_POST_BIND: 1621 ptype = BPF_PROG_TYPE_CGROUP_SOCK; 1622 break; 1623 case BPF_CGROUP_INET4_BIND: 1624 case BPF_CGROUP_INET6_BIND: 1625 case BPF_CGROUP_INET4_CONNECT: 1626 case BPF_CGROUP_INET6_CONNECT: 1627 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 1628 break; 1629 case BPF_CGROUP_SOCK_OPS: 1630 ptype = BPF_PROG_TYPE_SOCK_OPS; 1631 break; 1632 case BPF_CGROUP_DEVICE: 1633 ptype = BPF_PROG_TYPE_CGROUP_DEVICE; 1634 break; 1635 case BPF_SK_MSG_VERDICT: 1636 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false); 1637 case BPF_SK_SKB_STREAM_PARSER: 1638 case BPF_SK_SKB_STREAM_VERDICT: 1639 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false); 1640 default: 1641 return -EINVAL; 1642 } 1643 1644 cgrp = cgroup_get_from_fd(attr->target_fd); 1645 if (IS_ERR(cgrp)) 1646 return PTR_ERR(cgrp); 1647 1648 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 1649 if (IS_ERR(prog)) 1650 prog = NULL; 1651 1652 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0); 1653 if (prog) 1654 bpf_prog_put(prog); 1655 cgroup_put(cgrp); 1656 return ret; 1657 } 1658 1659 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt 1660 1661 static int bpf_prog_query(const union bpf_attr *attr, 1662 union bpf_attr __user *uattr) 1663 { 1664 struct cgroup *cgrp; 1665 int ret; 1666 1667 if (!capable(CAP_NET_ADMIN)) 1668 return -EPERM; 1669 if (CHECK_ATTR(BPF_PROG_QUERY)) 1670 return -EINVAL; 1671 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) 1672 return -EINVAL; 1673 1674 switch (attr->query.attach_type) { 1675 case BPF_CGROUP_INET_INGRESS: 1676 case BPF_CGROUP_INET_EGRESS: 1677 case BPF_CGROUP_INET_SOCK_CREATE: 1678 case BPF_CGROUP_INET4_BIND: 1679 case BPF_CGROUP_INET6_BIND: 1680 case BPF_CGROUP_INET4_POST_BIND: 1681 case BPF_CGROUP_INET6_POST_BIND: 1682 case BPF_CGROUP_INET4_CONNECT: 1683 case BPF_CGROUP_INET6_CONNECT: 1684 case BPF_CGROUP_SOCK_OPS: 1685 case BPF_CGROUP_DEVICE: 1686 break; 1687 default: 1688 return -EINVAL; 1689 } 1690 cgrp = cgroup_get_from_fd(attr->query.target_fd); 1691 if (IS_ERR(cgrp)) 1692 return PTR_ERR(cgrp); 1693 ret = cgroup_bpf_query(cgrp, attr, uattr); 1694 cgroup_put(cgrp); 1695 return ret; 1696 } 1697 #endif /* CONFIG_CGROUP_BPF */ 1698 1699 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration 1700 1701 static int bpf_prog_test_run(const union bpf_attr *attr, 1702 union bpf_attr __user *uattr) 1703 { 1704 struct bpf_prog *prog; 1705 int ret = -ENOTSUPP; 1706 1707 if (!capable(CAP_SYS_ADMIN)) 1708 return -EPERM; 1709 if (CHECK_ATTR(BPF_PROG_TEST_RUN)) 1710 return -EINVAL; 1711 1712 prog = bpf_prog_get(attr->test.prog_fd); 1713 if (IS_ERR(prog)) 1714 return PTR_ERR(prog); 1715 1716 if (prog->aux->ops->test_run) 1717 ret = prog->aux->ops->test_run(prog, attr, uattr); 1718 1719 bpf_prog_put(prog); 1720 return ret; 1721 } 1722 1723 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id 1724 1725 static int bpf_obj_get_next_id(const union bpf_attr *attr, 1726 union bpf_attr __user *uattr, 1727 struct idr *idr, 1728 spinlock_t *lock) 1729 { 1730 u32 next_id = attr->start_id; 1731 int err = 0; 1732 1733 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) 1734 return -EINVAL; 1735 1736 if (!capable(CAP_SYS_ADMIN)) 1737 return -EPERM; 1738 1739 next_id++; 1740 spin_lock_bh(lock); 1741 if (!idr_get_next(idr, &next_id)) 1742 err = -ENOENT; 1743 spin_unlock_bh(lock); 1744 1745 if (!err) 1746 err = put_user(next_id, &uattr->next_id); 1747 1748 return err; 1749 } 1750 1751 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id 1752 1753 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) 1754 { 1755 struct bpf_prog *prog; 1756 u32 id = attr->prog_id; 1757 int fd; 1758 1759 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) 1760 return -EINVAL; 1761 1762 if (!capable(CAP_SYS_ADMIN)) 1763 return -EPERM; 1764 1765 spin_lock_bh(&prog_idr_lock); 1766 prog = idr_find(&prog_idr, id); 1767 if (prog) 1768 prog = bpf_prog_inc_not_zero(prog); 1769 else 1770 prog = ERR_PTR(-ENOENT); 1771 spin_unlock_bh(&prog_idr_lock); 1772 1773 if (IS_ERR(prog)) 1774 return PTR_ERR(prog); 1775 1776 fd = bpf_prog_new_fd(prog); 1777 if (fd < 0) 1778 bpf_prog_put(prog); 1779 1780 return fd; 1781 } 1782 1783 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags 1784 1785 static int bpf_map_get_fd_by_id(const union bpf_attr *attr) 1786 { 1787 struct bpf_map *map; 1788 u32 id = attr->map_id; 1789 int f_flags; 1790 int fd; 1791 1792 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || 1793 attr->open_flags & ~BPF_OBJ_FLAG_MASK) 1794 return -EINVAL; 1795 1796 if (!capable(CAP_SYS_ADMIN)) 1797 return -EPERM; 1798 1799 f_flags = bpf_get_file_flag(attr->open_flags); 1800 if (f_flags < 0) 1801 return f_flags; 1802 1803 spin_lock_bh(&map_idr_lock); 1804 map = idr_find(&map_idr, id); 1805 if (map) 1806 map = bpf_map_inc_not_zero(map, true); 1807 else 1808 map = ERR_PTR(-ENOENT); 1809 spin_unlock_bh(&map_idr_lock); 1810 1811 if (IS_ERR(map)) 1812 return PTR_ERR(map); 1813 1814 fd = bpf_map_new_fd(map, f_flags); 1815 if (fd < 0) 1816 bpf_map_put(map); 1817 1818 return fd; 1819 } 1820 1821 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, 1822 unsigned long addr) 1823 { 1824 int i; 1825 1826 for (i = 0; i < prog->aux->used_map_cnt; i++) 1827 if (prog->aux->used_maps[i] == (void *)addr) 1828 return prog->aux->used_maps[i]; 1829 return NULL; 1830 } 1831 1832 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog) 1833 { 1834 const struct bpf_map *map; 1835 struct bpf_insn *insns; 1836 u64 imm; 1837 int i; 1838 1839 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), 1840 GFP_USER); 1841 if (!insns) 1842 return insns; 1843 1844 for (i = 0; i < prog->len; i++) { 1845 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) { 1846 insns[i].code = BPF_JMP | BPF_CALL; 1847 insns[i].imm = BPF_FUNC_tail_call; 1848 /* fall-through */ 1849 } 1850 if (insns[i].code == (BPF_JMP | BPF_CALL) || 1851 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) { 1852 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) 1853 insns[i].code = BPF_JMP | BPF_CALL; 1854 if (!bpf_dump_raw_ok()) 1855 insns[i].imm = 0; 1856 continue; 1857 } 1858 1859 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW)) 1860 continue; 1861 1862 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; 1863 map = bpf_map_from_imm(prog, imm); 1864 if (map) { 1865 insns[i].src_reg = BPF_PSEUDO_MAP_FD; 1866 insns[i].imm = map->id; 1867 insns[i + 1].imm = 0; 1868 continue; 1869 } 1870 1871 if (!bpf_dump_raw_ok() && 1872 imm == (unsigned long)prog->aux) { 1873 insns[i].imm = 0; 1874 insns[i + 1].imm = 0; 1875 continue; 1876 } 1877 } 1878 1879 return insns; 1880 } 1881 1882 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, 1883 const union bpf_attr *attr, 1884 union bpf_attr __user *uattr) 1885 { 1886 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); 1887 struct bpf_prog_info info = {}; 1888 u32 info_len = attr->info.info_len; 1889 char __user *uinsns; 1890 u32 ulen; 1891 int err; 1892 1893 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); 1894 if (err) 1895 return err; 1896 info_len = min_t(u32, sizeof(info), info_len); 1897 1898 if (copy_from_user(&info, uinfo, info_len)) 1899 return -EFAULT; 1900 1901 info.type = prog->type; 1902 info.id = prog->aux->id; 1903 info.load_time = prog->aux->load_time; 1904 info.created_by_uid = from_kuid_munged(current_user_ns(), 1905 prog->aux->user->uid); 1906 info.gpl_compatible = prog->gpl_compatible; 1907 1908 memcpy(info.tag, prog->tag, sizeof(prog->tag)); 1909 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); 1910 1911 ulen = info.nr_map_ids; 1912 info.nr_map_ids = prog->aux->used_map_cnt; 1913 ulen = min_t(u32, info.nr_map_ids, ulen); 1914 if (ulen) { 1915 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); 1916 u32 i; 1917 1918 for (i = 0; i < ulen; i++) 1919 if (put_user(prog->aux->used_maps[i]->id, 1920 &user_map_ids[i])) 1921 return -EFAULT; 1922 } 1923 1924 if (!capable(CAP_SYS_ADMIN)) { 1925 info.jited_prog_len = 0; 1926 info.xlated_prog_len = 0; 1927 goto done; 1928 } 1929 1930 ulen = info.xlated_prog_len; 1931 info.xlated_prog_len = bpf_prog_insn_size(prog); 1932 if (info.xlated_prog_len && ulen) { 1933 struct bpf_insn *insns_sanitized; 1934 bool fault; 1935 1936 if (prog->blinded && !bpf_dump_raw_ok()) { 1937 info.xlated_prog_insns = 0; 1938 goto done; 1939 } 1940 insns_sanitized = bpf_insn_prepare_dump(prog); 1941 if (!insns_sanitized) 1942 return -ENOMEM; 1943 uinsns = u64_to_user_ptr(info.xlated_prog_insns); 1944 ulen = min_t(u32, info.xlated_prog_len, ulen); 1945 fault = copy_to_user(uinsns, insns_sanitized, ulen); 1946 kfree(insns_sanitized); 1947 if (fault) 1948 return -EFAULT; 1949 } 1950 1951 if (bpf_prog_is_dev_bound(prog->aux)) { 1952 err = bpf_prog_offload_info_fill(&info, prog); 1953 if (err) 1954 return err; 1955 goto done; 1956 } 1957 1958 /* NOTE: the following code is supposed to be skipped for offload. 1959 * bpf_prog_offload_info_fill() is the place to fill similar fields 1960 * for offload. 1961 */ 1962 ulen = info.jited_prog_len; 1963 info.jited_prog_len = prog->jited_len; 1964 if (info.jited_prog_len && ulen) { 1965 if (bpf_dump_raw_ok()) { 1966 uinsns = u64_to_user_ptr(info.jited_prog_insns); 1967 ulen = min_t(u32, info.jited_prog_len, ulen); 1968 if (copy_to_user(uinsns, prog->bpf_func, ulen)) 1969 return -EFAULT; 1970 } else { 1971 info.jited_prog_insns = 0; 1972 } 1973 } 1974 1975 done: 1976 if (copy_to_user(uinfo, &info, info_len) || 1977 put_user(info_len, &uattr->info.info_len)) 1978 return -EFAULT; 1979 1980 return 0; 1981 } 1982 1983 static int bpf_map_get_info_by_fd(struct bpf_map *map, 1984 const union bpf_attr *attr, 1985 union bpf_attr __user *uattr) 1986 { 1987 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); 1988 struct bpf_map_info info = {}; 1989 u32 info_len = attr->info.info_len; 1990 int err; 1991 1992 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); 1993 if (err) 1994 return err; 1995 info_len = min_t(u32, sizeof(info), info_len); 1996 1997 info.type = map->map_type; 1998 info.id = map->id; 1999 info.key_size = map->key_size; 2000 info.value_size = map->value_size; 2001 info.max_entries = map->max_entries; 2002 info.map_flags = map->map_flags; 2003 memcpy(info.name, map->name, sizeof(map->name)); 2004 2005 if (bpf_map_is_dev_bound(map)) { 2006 err = bpf_map_offload_info_fill(&info, map); 2007 if (err) 2008 return err; 2009 } 2010 2011 if (copy_to_user(uinfo, &info, info_len) || 2012 put_user(info_len, &uattr->info.info_len)) 2013 return -EFAULT; 2014 2015 return 0; 2016 } 2017 2018 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info 2019 2020 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, 2021 union bpf_attr __user *uattr) 2022 { 2023 int ufd = attr->info.bpf_fd; 2024 struct fd f; 2025 int err; 2026 2027 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) 2028 return -EINVAL; 2029 2030 f = fdget(ufd); 2031 if (!f.file) 2032 return -EBADFD; 2033 2034 if (f.file->f_op == &bpf_prog_fops) 2035 err = bpf_prog_get_info_by_fd(f.file->private_data, attr, 2036 uattr); 2037 else if (f.file->f_op == &bpf_map_fops) 2038 err = bpf_map_get_info_by_fd(f.file->private_data, attr, 2039 uattr); 2040 else if (f.file->f_op == &btf_fops) 2041 err = btf_get_info_by_fd(f.file->private_data, attr, uattr); 2042 else 2043 err = -EINVAL; 2044 2045 fdput(f); 2046 return err; 2047 } 2048 2049 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level 2050 2051 static int bpf_btf_load(const union bpf_attr *attr) 2052 { 2053 if (CHECK_ATTR(BPF_BTF_LOAD)) 2054 return -EINVAL; 2055 2056 if (!capable(CAP_SYS_ADMIN)) 2057 return -EPERM; 2058 2059 return btf_new_fd(attr); 2060 } 2061 2062 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) 2063 { 2064 union bpf_attr attr = {}; 2065 int err; 2066 2067 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) 2068 return -EPERM; 2069 2070 err = check_uarg_tail_zero(uattr, sizeof(attr), size); 2071 if (err) 2072 return err; 2073 size = min_t(u32, size, sizeof(attr)); 2074 2075 /* copy attributes from user space, may be less than sizeof(bpf_attr) */ 2076 if (copy_from_user(&attr, uattr, size) != 0) 2077 return -EFAULT; 2078 2079 err = security_bpf(cmd, &attr, size); 2080 if (err < 0) 2081 return err; 2082 2083 switch (cmd) { 2084 case BPF_MAP_CREATE: 2085 err = map_create(&attr); 2086 break; 2087 case BPF_MAP_LOOKUP_ELEM: 2088 err = map_lookup_elem(&attr); 2089 break; 2090 case BPF_MAP_UPDATE_ELEM: 2091 err = map_update_elem(&attr); 2092 break; 2093 case BPF_MAP_DELETE_ELEM: 2094 err = map_delete_elem(&attr); 2095 break; 2096 case BPF_MAP_GET_NEXT_KEY: 2097 err = map_get_next_key(&attr); 2098 break; 2099 case BPF_PROG_LOAD: 2100 err = bpf_prog_load(&attr); 2101 break; 2102 case BPF_OBJ_PIN: 2103 err = bpf_obj_pin(&attr); 2104 break; 2105 case BPF_OBJ_GET: 2106 err = bpf_obj_get(&attr); 2107 break; 2108 #ifdef CONFIG_CGROUP_BPF 2109 case BPF_PROG_ATTACH: 2110 err = bpf_prog_attach(&attr); 2111 break; 2112 case BPF_PROG_DETACH: 2113 err = bpf_prog_detach(&attr); 2114 break; 2115 case BPF_PROG_QUERY: 2116 err = bpf_prog_query(&attr, uattr); 2117 break; 2118 #endif 2119 case BPF_PROG_TEST_RUN: 2120 err = bpf_prog_test_run(&attr, uattr); 2121 break; 2122 case BPF_PROG_GET_NEXT_ID: 2123 err = bpf_obj_get_next_id(&attr, uattr, 2124 &prog_idr, &prog_idr_lock); 2125 break; 2126 case BPF_MAP_GET_NEXT_ID: 2127 err = bpf_obj_get_next_id(&attr, uattr, 2128 &map_idr, &map_idr_lock); 2129 break; 2130 case BPF_PROG_GET_FD_BY_ID: 2131 err = bpf_prog_get_fd_by_id(&attr); 2132 break; 2133 case BPF_MAP_GET_FD_BY_ID: 2134 err = bpf_map_get_fd_by_id(&attr); 2135 break; 2136 case BPF_OBJ_GET_INFO_BY_FD: 2137 err = bpf_obj_get_info_by_fd(&attr, uattr); 2138 break; 2139 case BPF_RAW_TRACEPOINT_OPEN: 2140 err = bpf_raw_tracepoint_open(&attr); 2141 break; 2142 case BPF_BTF_LOAD: 2143 err = bpf_btf_load(&attr); 2144 break; 2145 default: 2146 err = -EINVAL; 2147 break; 2148 } 2149 2150 return err; 2151 } 2152