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