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