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