1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com 3 */ 4 #include <linux/bpf.h> 5 #include <linux/bpf_trace.h> 6 #include <linux/bpf_lirc.h> 7 #include <linux/btf.h> 8 #include <linux/syscalls.h> 9 #include <linux/slab.h> 10 #include <linux/sched/signal.h> 11 #include <linux/vmalloc.h> 12 #include <linux/mmzone.h> 13 #include <linux/anon_inodes.h> 14 #include <linux/fdtable.h> 15 #include <linux/file.h> 16 #include <linux/fs.h> 17 #include <linux/license.h> 18 #include <linux/filter.h> 19 #include <linux/version.h> 20 #include <linux/kernel.h> 21 #include <linux/idr.h> 22 #include <linux/cred.h> 23 #include <linux/timekeeping.h> 24 #include <linux/ctype.h> 25 #include <linux/nospec.h> 26 #include <linux/audit.h> 27 #include <uapi/linux/btf.h> 28 #include <linux/pgtable.h> 29 #include <linux/bpf_lsm.h> 30 #include <linux/poll.h> 31 #include <linux/bpf-netns.h> 32 33 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ 34 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ 35 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) 36 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY) 37 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) 38 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \ 39 IS_FD_HASH(map)) 40 41 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) 42 43 DEFINE_PER_CPU(int, bpf_prog_active); 44 static DEFINE_IDR(prog_idr); 45 static DEFINE_SPINLOCK(prog_idr_lock); 46 static DEFINE_IDR(map_idr); 47 static DEFINE_SPINLOCK(map_idr_lock); 48 static DEFINE_IDR(link_idr); 49 static DEFINE_SPINLOCK(link_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, _name, prog_ctx_type, kern_ctx_type) 55 #define BPF_MAP_TYPE(_id, _ops) \ 56 [_id] = &_ops, 57 #define BPF_LINK_TYPE(_id, _name) 58 #include <linux/bpf_types.h> 59 #undef BPF_PROG_TYPE 60 #undef BPF_MAP_TYPE 61 #undef BPF_LINK_TYPE 62 }; 63 64 /* 65 * If we're handed a bigger struct than we know of, ensure all the unknown bits 66 * are 0 - i.e. new user-space does not rely on any kernel feature extensions 67 * we don't know about yet. 68 * 69 * There is a ToCToU between this function call and the following 70 * copy_from_user() call. However, this is not a concern since this function is 71 * meant to be a future-proofing of bits. 72 */ 73 int bpf_check_uarg_tail_zero(void __user *uaddr, 74 size_t expected_size, 75 size_t actual_size) 76 { 77 unsigned char __user *addr = uaddr + expected_size; 78 int res; 79 80 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ 81 return -E2BIG; 82 83 if (actual_size <= expected_size) 84 return 0; 85 86 res = check_zeroed_user(addr, actual_size - expected_size); 87 if (res < 0) 88 return res; 89 return res ? 0 : -E2BIG; 90 } 91 92 const struct bpf_map_ops bpf_map_offload_ops = { 93 .map_alloc = bpf_map_offload_map_alloc, 94 .map_free = bpf_map_offload_map_free, 95 .map_check_btf = map_check_no_btf, 96 }; 97 98 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) 99 { 100 const struct bpf_map_ops *ops; 101 u32 type = attr->map_type; 102 struct bpf_map *map; 103 int err; 104 105 if (type >= ARRAY_SIZE(bpf_map_types)) 106 return ERR_PTR(-EINVAL); 107 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); 108 ops = bpf_map_types[type]; 109 if (!ops) 110 return ERR_PTR(-EINVAL); 111 112 if (ops->map_alloc_check) { 113 err = ops->map_alloc_check(attr); 114 if (err) 115 return ERR_PTR(err); 116 } 117 if (attr->map_ifindex) 118 ops = &bpf_map_offload_ops; 119 map = ops->map_alloc(attr); 120 if (IS_ERR(map)) 121 return map; 122 map->ops = ops; 123 map->map_type = type; 124 return map; 125 } 126 127 static u32 bpf_map_value_size(struct bpf_map *map) 128 { 129 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 130 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || 131 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || 132 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) 133 return round_up(map->value_size, 8) * num_possible_cpus(); 134 else if (IS_FD_MAP(map)) 135 return sizeof(u32); 136 else 137 return map->value_size; 138 } 139 140 static void maybe_wait_bpf_programs(struct bpf_map *map) 141 { 142 /* Wait for any running BPF programs to complete so that 143 * userspace, when we return to it, knows that all programs 144 * that could be running use the new map value. 145 */ 146 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || 147 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) 148 synchronize_rcu(); 149 } 150 151 static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key, 152 void *value, __u64 flags) 153 { 154 int err; 155 156 /* Need to create a kthread, thus must support schedule */ 157 if (bpf_map_is_dev_bound(map)) { 158 return bpf_map_offload_update_elem(map, key, value, flags); 159 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP || 160 map->map_type == BPF_MAP_TYPE_SOCKHASH || 161 map->map_type == BPF_MAP_TYPE_SOCKMAP || 162 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 163 return map->ops->map_update_elem(map, key, value, flags); 164 } else if (IS_FD_PROG_ARRAY(map)) { 165 return bpf_fd_array_map_update_elem(map, f.file, key, value, 166 flags); 167 } 168 169 bpf_disable_instrumentation(); 170 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 171 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 172 err = bpf_percpu_hash_update(map, key, value, flags); 173 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 174 err = bpf_percpu_array_update(map, key, value, flags); 175 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { 176 err = bpf_percpu_cgroup_storage_update(map, key, value, 177 flags); 178 } else if (IS_FD_ARRAY(map)) { 179 rcu_read_lock(); 180 err = bpf_fd_array_map_update_elem(map, f.file, key, value, 181 flags); 182 rcu_read_unlock(); 183 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { 184 rcu_read_lock(); 185 err = bpf_fd_htab_map_update_elem(map, f.file, key, value, 186 flags); 187 rcu_read_unlock(); 188 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { 189 /* rcu_read_lock() is not needed */ 190 err = bpf_fd_reuseport_array_update_elem(map, key, value, 191 flags); 192 } else if (map->map_type == BPF_MAP_TYPE_QUEUE || 193 map->map_type == BPF_MAP_TYPE_STACK) { 194 err = map->ops->map_push_elem(map, value, flags); 195 } else { 196 rcu_read_lock(); 197 err = map->ops->map_update_elem(map, key, value, flags); 198 rcu_read_unlock(); 199 } 200 bpf_enable_instrumentation(); 201 maybe_wait_bpf_programs(map); 202 203 return err; 204 } 205 206 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, 207 __u64 flags) 208 { 209 void *ptr; 210 int err; 211 212 if (bpf_map_is_dev_bound(map)) 213 return bpf_map_offload_lookup_elem(map, key, value); 214 215 bpf_disable_instrumentation(); 216 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 217 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 218 err = bpf_percpu_hash_copy(map, key, value); 219 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 220 err = bpf_percpu_array_copy(map, key, value); 221 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { 222 err = bpf_percpu_cgroup_storage_copy(map, key, value); 223 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { 224 err = bpf_stackmap_copy(map, key, value); 225 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) { 226 err = bpf_fd_array_map_lookup_elem(map, key, value); 227 } else if (IS_FD_HASH(map)) { 228 err = bpf_fd_htab_map_lookup_elem(map, key, value); 229 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { 230 err = bpf_fd_reuseport_array_lookup_elem(map, key, value); 231 } else if (map->map_type == BPF_MAP_TYPE_QUEUE || 232 map->map_type == BPF_MAP_TYPE_STACK) { 233 err = map->ops->map_peek_elem(map, value); 234 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 235 /* struct_ops map requires directly updating "value" */ 236 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value); 237 } else { 238 rcu_read_lock(); 239 if (map->ops->map_lookup_elem_sys_only) 240 ptr = map->ops->map_lookup_elem_sys_only(map, key); 241 else 242 ptr = map->ops->map_lookup_elem(map, key); 243 if (IS_ERR(ptr)) { 244 err = PTR_ERR(ptr); 245 } else if (!ptr) { 246 err = -ENOENT; 247 } else { 248 err = 0; 249 if (flags & BPF_F_LOCK) 250 /* lock 'ptr' and copy everything but lock */ 251 copy_map_value_locked(map, value, ptr, true); 252 else 253 copy_map_value(map, value, ptr); 254 /* mask lock, since value wasn't zero inited */ 255 check_and_init_map_lock(map, value); 256 } 257 rcu_read_unlock(); 258 } 259 260 bpf_enable_instrumentation(); 261 maybe_wait_bpf_programs(map); 262 263 return err; 264 } 265 266 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable) 267 { 268 /* We really just want to fail instead of triggering OOM killer 269 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc, 270 * which is used for lower order allocation requests. 271 * 272 * It has been observed that higher order allocation requests done by 273 * vmalloc with __GFP_NORETRY being set might fail due to not trying 274 * to reclaim memory from the page cache, thus we set 275 * __GFP_RETRY_MAYFAIL to avoid such situations. 276 */ 277 278 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO; 279 unsigned int flags = 0; 280 unsigned long align = 1; 281 void *area; 282 283 if (size >= SIZE_MAX) 284 return NULL; 285 286 /* kmalloc()'ed memory can't be mmap()'ed */ 287 if (mmapable) { 288 BUG_ON(!PAGE_ALIGNED(size)); 289 align = SHMLBA; 290 flags = VM_USERMAP; 291 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { 292 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY, 293 numa_node); 294 if (area != NULL) 295 return area; 296 } 297 298 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, 299 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL, 300 flags, numa_node, __builtin_return_address(0)); 301 } 302 303 void *bpf_map_area_alloc(u64 size, int numa_node) 304 { 305 return __bpf_map_area_alloc(size, numa_node, false); 306 } 307 308 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node) 309 { 310 return __bpf_map_area_alloc(size, numa_node, true); 311 } 312 313 void bpf_map_area_free(void *area) 314 { 315 kvfree(area); 316 } 317 318 static u32 bpf_map_flags_retain_permanent(u32 flags) 319 { 320 /* Some map creation flags are not tied to the map object but 321 * rather to the map fd instead, so they have no meaning upon 322 * map object inspection since multiple file descriptors with 323 * different (access) properties can exist here. Thus, given 324 * this has zero meaning for the map itself, lets clear these 325 * from here. 326 */ 327 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY); 328 } 329 330 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) 331 { 332 map->map_type = attr->map_type; 333 map->key_size = attr->key_size; 334 map->value_size = attr->value_size; 335 map->max_entries = attr->max_entries; 336 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags); 337 map->numa_node = bpf_map_attr_numa_node(attr); 338 } 339 340 static int bpf_charge_memlock(struct user_struct *user, u32 pages) 341 { 342 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 343 344 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) { 345 atomic_long_sub(pages, &user->locked_vm); 346 return -EPERM; 347 } 348 return 0; 349 } 350 351 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages) 352 { 353 if (user) 354 atomic_long_sub(pages, &user->locked_vm); 355 } 356 357 int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size) 358 { 359 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT; 360 struct user_struct *user; 361 int ret; 362 363 if (size >= U32_MAX - PAGE_SIZE) 364 return -E2BIG; 365 366 user = get_current_user(); 367 ret = bpf_charge_memlock(user, pages); 368 if (ret) { 369 free_uid(user); 370 return ret; 371 } 372 373 mem->pages = pages; 374 mem->user = user; 375 376 return 0; 377 } 378 379 void bpf_map_charge_finish(struct bpf_map_memory *mem) 380 { 381 bpf_uncharge_memlock(mem->user, mem->pages); 382 free_uid(mem->user); 383 } 384 385 void bpf_map_charge_move(struct bpf_map_memory *dst, 386 struct bpf_map_memory *src) 387 { 388 *dst = *src; 389 390 /* Make sure src will not be used for the redundant uncharging. */ 391 memset(src, 0, sizeof(struct bpf_map_memory)); 392 } 393 394 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages) 395 { 396 int ret; 397 398 ret = bpf_charge_memlock(map->memory.user, pages); 399 if (ret) 400 return ret; 401 map->memory.pages += pages; 402 return ret; 403 } 404 405 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages) 406 { 407 bpf_uncharge_memlock(map->memory.user, pages); 408 map->memory.pages -= pages; 409 } 410 411 static int bpf_map_alloc_id(struct bpf_map *map) 412 { 413 int id; 414 415 idr_preload(GFP_KERNEL); 416 spin_lock_bh(&map_idr_lock); 417 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); 418 if (id > 0) 419 map->id = id; 420 spin_unlock_bh(&map_idr_lock); 421 idr_preload_end(); 422 423 if (WARN_ON_ONCE(!id)) 424 return -ENOSPC; 425 426 return id > 0 ? 0 : id; 427 } 428 429 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) 430 { 431 unsigned long flags; 432 433 /* Offloaded maps are removed from the IDR store when their device 434 * disappears - even if someone holds an fd to them they are unusable, 435 * the memory is gone, all ops will fail; they are simply waiting for 436 * refcnt to drop to be freed. 437 */ 438 if (!map->id) 439 return; 440 441 if (do_idr_lock) 442 spin_lock_irqsave(&map_idr_lock, flags); 443 else 444 __acquire(&map_idr_lock); 445 446 idr_remove(&map_idr, map->id); 447 map->id = 0; 448 449 if (do_idr_lock) 450 spin_unlock_irqrestore(&map_idr_lock, flags); 451 else 452 __release(&map_idr_lock); 453 } 454 455 /* called from workqueue */ 456 static void bpf_map_free_deferred(struct work_struct *work) 457 { 458 struct bpf_map *map = container_of(work, struct bpf_map, work); 459 struct bpf_map_memory mem; 460 461 bpf_map_charge_move(&mem, &map->memory); 462 security_bpf_map_free(map); 463 /* implementation dependent freeing */ 464 map->ops->map_free(map); 465 bpf_map_charge_finish(&mem); 466 } 467 468 static void bpf_map_put_uref(struct bpf_map *map) 469 { 470 if (atomic64_dec_and_test(&map->usercnt)) { 471 if (map->ops->map_release_uref) 472 map->ops->map_release_uref(map); 473 } 474 } 475 476 /* decrement map refcnt and schedule it for freeing via workqueue 477 * (unrelying map implementation ops->map_free() might sleep) 478 */ 479 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) 480 { 481 if (atomic64_dec_and_test(&map->refcnt)) { 482 /* bpf_map_free_id() must be called first */ 483 bpf_map_free_id(map, do_idr_lock); 484 btf_put(map->btf); 485 INIT_WORK(&map->work, bpf_map_free_deferred); 486 schedule_work(&map->work); 487 } 488 } 489 490 void bpf_map_put(struct bpf_map *map) 491 { 492 __bpf_map_put(map, true); 493 } 494 EXPORT_SYMBOL_GPL(bpf_map_put); 495 496 void bpf_map_put_with_uref(struct bpf_map *map) 497 { 498 bpf_map_put_uref(map); 499 bpf_map_put(map); 500 } 501 502 static int bpf_map_release(struct inode *inode, struct file *filp) 503 { 504 struct bpf_map *map = filp->private_data; 505 506 if (map->ops->map_release) 507 map->ops->map_release(map, filp); 508 509 bpf_map_put_with_uref(map); 510 return 0; 511 } 512 513 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) 514 { 515 fmode_t mode = f.file->f_mode; 516 517 /* Our file permissions may have been overridden by global 518 * map permissions facing syscall side. 519 */ 520 if (READ_ONCE(map->frozen)) 521 mode &= ~FMODE_CAN_WRITE; 522 return mode; 523 } 524 525 #ifdef CONFIG_PROC_FS 526 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) 527 { 528 const struct bpf_map *map = filp->private_data; 529 const struct bpf_array *array; 530 u32 type = 0, jited = 0; 531 532 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { 533 array = container_of(map, struct bpf_array, map); 534 type = array->aux->type; 535 jited = array->aux->jited; 536 } 537 538 seq_printf(m, 539 "map_type:\t%u\n" 540 "key_size:\t%u\n" 541 "value_size:\t%u\n" 542 "max_entries:\t%u\n" 543 "map_flags:\t%#x\n" 544 "memlock:\t%llu\n" 545 "map_id:\t%u\n" 546 "frozen:\t%u\n", 547 map->map_type, 548 map->key_size, 549 map->value_size, 550 map->max_entries, 551 map->map_flags, 552 map->memory.pages * 1ULL << PAGE_SHIFT, 553 map->id, 554 READ_ONCE(map->frozen)); 555 if (type) { 556 seq_printf(m, "owner_prog_type:\t%u\n", type); 557 seq_printf(m, "owner_jited:\t%u\n", jited); 558 } 559 } 560 #endif 561 562 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, 563 loff_t *ppos) 564 { 565 /* We need this handler such that alloc_file() enables 566 * f_mode with FMODE_CAN_READ. 567 */ 568 return -EINVAL; 569 } 570 571 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, 572 size_t siz, loff_t *ppos) 573 { 574 /* We need this handler such that alloc_file() enables 575 * f_mode with FMODE_CAN_WRITE. 576 */ 577 return -EINVAL; 578 } 579 580 /* called for any extra memory-mapped regions (except initial) */ 581 static void bpf_map_mmap_open(struct vm_area_struct *vma) 582 { 583 struct bpf_map *map = vma->vm_file->private_data; 584 585 if (vma->vm_flags & VM_MAYWRITE) { 586 mutex_lock(&map->freeze_mutex); 587 map->writecnt++; 588 mutex_unlock(&map->freeze_mutex); 589 } 590 } 591 592 /* called for all unmapped memory region (including initial) */ 593 static void bpf_map_mmap_close(struct vm_area_struct *vma) 594 { 595 struct bpf_map *map = vma->vm_file->private_data; 596 597 if (vma->vm_flags & VM_MAYWRITE) { 598 mutex_lock(&map->freeze_mutex); 599 map->writecnt--; 600 mutex_unlock(&map->freeze_mutex); 601 } 602 } 603 604 static const struct vm_operations_struct bpf_map_default_vmops = { 605 .open = bpf_map_mmap_open, 606 .close = bpf_map_mmap_close, 607 }; 608 609 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma) 610 { 611 struct bpf_map *map = filp->private_data; 612 int err; 613 614 if (!map->ops->map_mmap || map_value_has_spin_lock(map)) 615 return -ENOTSUPP; 616 617 if (!(vma->vm_flags & VM_SHARED)) 618 return -EINVAL; 619 620 mutex_lock(&map->freeze_mutex); 621 622 if (vma->vm_flags & VM_WRITE) { 623 if (map->frozen) { 624 err = -EPERM; 625 goto out; 626 } 627 /* map is meant to be read-only, so do not allow mapping as 628 * writable, because it's possible to leak a writable page 629 * reference and allows user-space to still modify it after 630 * freezing, while verifier will assume contents do not change 631 */ 632 if (map->map_flags & BPF_F_RDONLY_PROG) { 633 err = -EACCES; 634 goto out; 635 } 636 } 637 638 /* set default open/close callbacks */ 639 vma->vm_ops = &bpf_map_default_vmops; 640 vma->vm_private_data = map; 641 vma->vm_flags &= ~VM_MAYEXEC; 642 if (!(vma->vm_flags & VM_WRITE)) 643 /* disallow re-mapping with PROT_WRITE */ 644 vma->vm_flags &= ~VM_MAYWRITE; 645 646 err = map->ops->map_mmap(map, vma); 647 if (err) 648 goto out; 649 650 if (vma->vm_flags & VM_MAYWRITE) 651 map->writecnt++; 652 out: 653 mutex_unlock(&map->freeze_mutex); 654 return err; 655 } 656 657 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts) 658 { 659 struct bpf_map *map = filp->private_data; 660 661 if (map->ops->map_poll) 662 return map->ops->map_poll(map, filp, pts); 663 664 return EPOLLERR; 665 } 666 667 const struct file_operations bpf_map_fops = { 668 #ifdef CONFIG_PROC_FS 669 .show_fdinfo = bpf_map_show_fdinfo, 670 #endif 671 .release = bpf_map_release, 672 .read = bpf_dummy_read, 673 .write = bpf_dummy_write, 674 .mmap = bpf_map_mmap, 675 .poll = bpf_map_poll, 676 }; 677 678 int bpf_map_new_fd(struct bpf_map *map, int flags) 679 { 680 int ret; 681 682 ret = security_bpf_map(map, OPEN_FMODE(flags)); 683 if (ret < 0) 684 return ret; 685 686 return anon_inode_getfd("bpf-map", &bpf_map_fops, map, 687 flags | O_CLOEXEC); 688 } 689 690 int bpf_get_file_flag(int flags) 691 { 692 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) 693 return -EINVAL; 694 if (flags & BPF_F_RDONLY) 695 return O_RDONLY; 696 if (flags & BPF_F_WRONLY) 697 return O_WRONLY; 698 return O_RDWR; 699 } 700 701 /* helper macro to check that unused fields 'union bpf_attr' are zero */ 702 #define CHECK_ATTR(CMD) \ 703 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ 704 sizeof(attr->CMD##_LAST_FIELD), 0, \ 705 sizeof(*attr) - \ 706 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ 707 sizeof(attr->CMD##_LAST_FIELD)) != NULL 708 709 /* dst and src must have at least "size" number of bytes. 710 * Return strlen on success and < 0 on error. 711 */ 712 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size) 713 { 714 const char *end = src + size; 715 const char *orig_src = src; 716 717 memset(dst, 0, size); 718 /* Copy all isalnum(), '_' and '.' chars. */ 719 while (src < end && *src) { 720 if (!isalnum(*src) && 721 *src != '_' && *src != '.') 722 return -EINVAL; 723 *dst++ = *src++; 724 } 725 726 /* No '\0' found in "size" number of bytes */ 727 if (src == end) 728 return -EINVAL; 729 730 return src - orig_src; 731 } 732 733 int map_check_no_btf(const struct bpf_map *map, 734 const struct btf *btf, 735 const struct btf_type *key_type, 736 const struct btf_type *value_type) 737 { 738 return -ENOTSUPP; 739 } 740 741 static int map_check_btf(struct bpf_map *map, const struct btf *btf, 742 u32 btf_key_id, u32 btf_value_id) 743 { 744 const struct btf_type *key_type, *value_type; 745 u32 key_size, value_size; 746 int ret = 0; 747 748 /* Some maps allow key to be unspecified. */ 749 if (btf_key_id) { 750 key_type = btf_type_id_size(btf, &btf_key_id, &key_size); 751 if (!key_type || key_size != map->key_size) 752 return -EINVAL; 753 } else { 754 key_type = btf_type_by_id(btf, 0); 755 if (!map->ops->map_check_btf) 756 return -EINVAL; 757 } 758 759 value_type = btf_type_id_size(btf, &btf_value_id, &value_size); 760 if (!value_type || value_size != map->value_size) 761 return -EINVAL; 762 763 map->spin_lock_off = btf_find_spin_lock(btf, value_type); 764 765 if (map_value_has_spin_lock(map)) { 766 if (map->map_flags & BPF_F_RDONLY_PROG) 767 return -EACCES; 768 if (map->map_type != BPF_MAP_TYPE_HASH && 769 map->map_type != BPF_MAP_TYPE_ARRAY && 770 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && 771 map->map_type != BPF_MAP_TYPE_SK_STORAGE) 772 return -ENOTSUPP; 773 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) > 774 map->value_size) { 775 WARN_ONCE(1, 776 "verifier bug spin_lock_off %d value_size %d\n", 777 map->spin_lock_off, map->value_size); 778 return -EFAULT; 779 } 780 } 781 782 if (map->ops->map_check_btf) 783 ret = map->ops->map_check_btf(map, btf, key_type, value_type); 784 785 return ret; 786 } 787 788 #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id 789 /* called via syscall */ 790 static int map_create(union bpf_attr *attr) 791 { 792 int numa_node = bpf_map_attr_numa_node(attr); 793 struct bpf_map_memory mem; 794 struct bpf_map *map; 795 int f_flags; 796 int err; 797 798 err = CHECK_ATTR(BPF_MAP_CREATE); 799 if (err) 800 return -EINVAL; 801 802 if (attr->btf_vmlinux_value_type_id) { 803 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS || 804 attr->btf_key_type_id || attr->btf_value_type_id) 805 return -EINVAL; 806 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) { 807 return -EINVAL; 808 } 809 810 f_flags = bpf_get_file_flag(attr->map_flags); 811 if (f_flags < 0) 812 return f_flags; 813 814 if (numa_node != NUMA_NO_NODE && 815 ((unsigned int)numa_node >= nr_node_ids || 816 !node_online(numa_node))) 817 return -EINVAL; 818 819 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ 820 map = find_and_alloc_map(attr); 821 if (IS_ERR(map)) 822 return PTR_ERR(map); 823 824 err = bpf_obj_name_cpy(map->name, attr->map_name, 825 sizeof(attr->map_name)); 826 if (err < 0) 827 goto free_map; 828 829 atomic64_set(&map->refcnt, 1); 830 atomic64_set(&map->usercnt, 1); 831 mutex_init(&map->freeze_mutex); 832 833 map->spin_lock_off = -EINVAL; 834 if (attr->btf_key_type_id || attr->btf_value_type_id || 835 /* Even the map's value is a kernel's struct, 836 * the bpf_prog.o must have BTF to begin with 837 * to figure out the corresponding kernel's 838 * counter part. Thus, attr->btf_fd has 839 * to be valid also. 840 */ 841 attr->btf_vmlinux_value_type_id) { 842 struct btf *btf; 843 844 btf = btf_get_by_fd(attr->btf_fd); 845 if (IS_ERR(btf)) { 846 err = PTR_ERR(btf); 847 goto free_map; 848 } 849 map->btf = btf; 850 851 if (attr->btf_value_type_id) { 852 err = map_check_btf(map, btf, attr->btf_key_type_id, 853 attr->btf_value_type_id); 854 if (err) 855 goto free_map; 856 } 857 858 map->btf_key_type_id = attr->btf_key_type_id; 859 map->btf_value_type_id = attr->btf_value_type_id; 860 map->btf_vmlinux_value_type_id = 861 attr->btf_vmlinux_value_type_id; 862 } 863 864 err = security_bpf_map_alloc(map); 865 if (err) 866 goto free_map; 867 868 err = bpf_map_alloc_id(map); 869 if (err) 870 goto free_map_sec; 871 872 err = bpf_map_new_fd(map, f_flags); 873 if (err < 0) { 874 /* failed to allocate fd. 875 * bpf_map_put_with_uref() is needed because the above 876 * bpf_map_alloc_id() has published the map 877 * to the userspace and the userspace may 878 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. 879 */ 880 bpf_map_put_with_uref(map); 881 return err; 882 } 883 884 return err; 885 886 free_map_sec: 887 security_bpf_map_free(map); 888 free_map: 889 btf_put(map->btf); 890 bpf_map_charge_move(&mem, &map->memory); 891 map->ops->map_free(map); 892 bpf_map_charge_finish(&mem); 893 return err; 894 } 895 896 /* if error is returned, fd is released. 897 * On success caller should complete fd access with matching fdput() 898 */ 899 struct bpf_map *__bpf_map_get(struct fd f) 900 { 901 if (!f.file) 902 return ERR_PTR(-EBADF); 903 if (f.file->f_op != &bpf_map_fops) { 904 fdput(f); 905 return ERR_PTR(-EINVAL); 906 } 907 908 return f.file->private_data; 909 } 910 911 void bpf_map_inc(struct bpf_map *map) 912 { 913 atomic64_inc(&map->refcnt); 914 } 915 EXPORT_SYMBOL_GPL(bpf_map_inc); 916 917 void bpf_map_inc_with_uref(struct bpf_map *map) 918 { 919 atomic64_inc(&map->refcnt); 920 atomic64_inc(&map->usercnt); 921 } 922 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref); 923 924 struct bpf_map *bpf_map_get(u32 ufd) 925 { 926 struct fd f = fdget(ufd); 927 struct bpf_map *map; 928 929 map = __bpf_map_get(f); 930 if (IS_ERR(map)) 931 return map; 932 933 bpf_map_inc(map); 934 fdput(f); 935 936 return map; 937 } 938 939 struct bpf_map *bpf_map_get_with_uref(u32 ufd) 940 { 941 struct fd f = fdget(ufd); 942 struct bpf_map *map; 943 944 map = __bpf_map_get(f); 945 if (IS_ERR(map)) 946 return map; 947 948 bpf_map_inc_with_uref(map); 949 fdput(f); 950 951 return map; 952 } 953 954 /* map_idr_lock should have been held */ 955 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref) 956 { 957 int refold; 958 959 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0); 960 if (!refold) 961 return ERR_PTR(-ENOENT); 962 if (uref) 963 atomic64_inc(&map->usercnt); 964 965 return map; 966 } 967 968 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map) 969 { 970 spin_lock_bh(&map_idr_lock); 971 map = __bpf_map_inc_not_zero(map, false); 972 spin_unlock_bh(&map_idr_lock); 973 974 return map; 975 } 976 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero); 977 978 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) 979 { 980 return -ENOTSUPP; 981 } 982 983 static void *__bpf_copy_key(void __user *ukey, u64 key_size) 984 { 985 if (key_size) 986 return memdup_user(ukey, key_size); 987 988 if (ukey) 989 return ERR_PTR(-EINVAL); 990 991 return NULL; 992 } 993 994 /* last field in 'union bpf_attr' used by this command */ 995 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags 996 997 static int map_lookup_elem(union bpf_attr *attr) 998 { 999 void __user *ukey = u64_to_user_ptr(attr->key); 1000 void __user *uvalue = u64_to_user_ptr(attr->value); 1001 int ufd = attr->map_fd; 1002 struct bpf_map *map; 1003 void *key, *value; 1004 u32 value_size; 1005 struct fd f; 1006 int err; 1007 1008 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) 1009 return -EINVAL; 1010 1011 if (attr->flags & ~BPF_F_LOCK) 1012 return -EINVAL; 1013 1014 f = fdget(ufd); 1015 map = __bpf_map_get(f); 1016 if (IS_ERR(map)) 1017 return PTR_ERR(map); 1018 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { 1019 err = -EPERM; 1020 goto err_put; 1021 } 1022 1023 if ((attr->flags & BPF_F_LOCK) && 1024 !map_value_has_spin_lock(map)) { 1025 err = -EINVAL; 1026 goto err_put; 1027 } 1028 1029 key = __bpf_copy_key(ukey, map->key_size); 1030 if (IS_ERR(key)) { 1031 err = PTR_ERR(key); 1032 goto err_put; 1033 } 1034 1035 value_size = bpf_map_value_size(map); 1036 1037 err = -ENOMEM; 1038 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 1039 if (!value) 1040 goto free_key; 1041 1042 err = bpf_map_copy_value(map, key, value, attr->flags); 1043 if (err) 1044 goto free_value; 1045 1046 err = -EFAULT; 1047 if (copy_to_user(uvalue, value, value_size) != 0) 1048 goto free_value; 1049 1050 err = 0; 1051 1052 free_value: 1053 kfree(value); 1054 free_key: 1055 kfree(key); 1056 err_put: 1057 fdput(f); 1058 return err; 1059 } 1060 1061 1062 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags 1063 1064 static int map_update_elem(union bpf_attr *attr) 1065 { 1066 void __user *ukey = u64_to_user_ptr(attr->key); 1067 void __user *uvalue = u64_to_user_ptr(attr->value); 1068 int ufd = attr->map_fd; 1069 struct bpf_map *map; 1070 void *key, *value; 1071 u32 value_size; 1072 struct fd f; 1073 int err; 1074 1075 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) 1076 return -EINVAL; 1077 1078 f = fdget(ufd); 1079 map = __bpf_map_get(f); 1080 if (IS_ERR(map)) 1081 return PTR_ERR(map); 1082 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 1083 err = -EPERM; 1084 goto err_put; 1085 } 1086 1087 if ((attr->flags & BPF_F_LOCK) && 1088 !map_value_has_spin_lock(map)) { 1089 err = -EINVAL; 1090 goto err_put; 1091 } 1092 1093 key = __bpf_copy_key(ukey, map->key_size); 1094 if (IS_ERR(key)) { 1095 err = PTR_ERR(key); 1096 goto err_put; 1097 } 1098 1099 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 1100 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || 1101 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || 1102 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) 1103 value_size = round_up(map->value_size, 8) * num_possible_cpus(); 1104 else 1105 value_size = map->value_size; 1106 1107 err = -ENOMEM; 1108 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 1109 if (!value) 1110 goto free_key; 1111 1112 err = -EFAULT; 1113 if (copy_from_user(value, uvalue, value_size) != 0) 1114 goto free_value; 1115 1116 err = bpf_map_update_value(map, f, key, value, attr->flags); 1117 1118 free_value: 1119 kfree(value); 1120 free_key: 1121 kfree(key); 1122 err_put: 1123 fdput(f); 1124 return err; 1125 } 1126 1127 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key 1128 1129 static int map_delete_elem(union bpf_attr *attr) 1130 { 1131 void __user *ukey = u64_to_user_ptr(attr->key); 1132 int ufd = attr->map_fd; 1133 struct bpf_map *map; 1134 struct fd f; 1135 void *key; 1136 int err; 1137 1138 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) 1139 return -EINVAL; 1140 1141 f = fdget(ufd); 1142 map = __bpf_map_get(f); 1143 if (IS_ERR(map)) 1144 return PTR_ERR(map); 1145 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 1146 err = -EPERM; 1147 goto err_put; 1148 } 1149 1150 key = __bpf_copy_key(ukey, map->key_size); 1151 if (IS_ERR(key)) { 1152 err = PTR_ERR(key); 1153 goto err_put; 1154 } 1155 1156 if (bpf_map_is_dev_bound(map)) { 1157 err = bpf_map_offload_delete_elem(map, key); 1158 goto out; 1159 } else if (IS_FD_PROG_ARRAY(map) || 1160 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 1161 /* These maps require sleepable context */ 1162 err = map->ops->map_delete_elem(map, key); 1163 goto out; 1164 } 1165 1166 bpf_disable_instrumentation(); 1167 rcu_read_lock(); 1168 err = map->ops->map_delete_elem(map, key); 1169 rcu_read_unlock(); 1170 bpf_enable_instrumentation(); 1171 maybe_wait_bpf_programs(map); 1172 out: 1173 kfree(key); 1174 err_put: 1175 fdput(f); 1176 return err; 1177 } 1178 1179 /* last field in 'union bpf_attr' used by this command */ 1180 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key 1181 1182 static int map_get_next_key(union bpf_attr *attr) 1183 { 1184 void __user *ukey = u64_to_user_ptr(attr->key); 1185 void __user *unext_key = u64_to_user_ptr(attr->next_key); 1186 int ufd = attr->map_fd; 1187 struct bpf_map *map; 1188 void *key, *next_key; 1189 struct fd f; 1190 int err; 1191 1192 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) 1193 return -EINVAL; 1194 1195 f = fdget(ufd); 1196 map = __bpf_map_get(f); 1197 if (IS_ERR(map)) 1198 return PTR_ERR(map); 1199 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { 1200 err = -EPERM; 1201 goto err_put; 1202 } 1203 1204 if (ukey) { 1205 key = __bpf_copy_key(ukey, map->key_size); 1206 if (IS_ERR(key)) { 1207 err = PTR_ERR(key); 1208 goto err_put; 1209 } 1210 } else { 1211 key = NULL; 1212 } 1213 1214 err = -ENOMEM; 1215 next_key = kmalloc(map->key_size, GFP_USER); 1216 if (!next_key) 1217 goto free_key; 1218 1219 if (bpf_map_is_dev_bound(map)) { 1220 err = bpf_map_offload_get_next_key(map, key, next_key); 1221 goto out; 1222 } 1223 1224 rcu_read_lock(); 1225 err = map->ops->map_get_next_key(map, key, next_key); 1226 rcu_read_unlock(); 1227 out: 1228 if (err) 1229 goto free_next_key; 1230 1231 err = -EFAULT; 1232 if (copy_to_user(unext_key, next_key, map->key_size) != 0) 1233 goto free_next_key; 1234 1235 err = 0; 1236 1237 free_next_key: 1238 kfree(next_key); 1239 free_key: 1240 kfree(key); 1241 err_put: 1242 fdput(f); 1243 return err; 1244 } 1245 1246 int generic_map_delete_batch(struct bpf_map *map, 1247 const union bpf_attr *attr, 1248 union bpf_attr __user *uattr) 1249 { 1250 void __user *keys = u64_to_user_ptr(attr->batch.keys); 1251 u32 cp, max_count; 1252 int err = 0; 1253 void *key; 1254 1255 if (attr->batch.elem_flags & ~BPF_F_LOCK) 1256 return -EINVAL; 1257 1258 if ((attr->batch.elem_flags & BPF_F_LOCK) && 1259 !map_value_has_spin_lock(map)) { 1260 return -EINVAL; 1261 } 1262 1263 max_count = attr->batch.count; 1264 if (!max_count) 1265 return 0; 1266 1267 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN); 1268 if (!key) 1269 return -ENOMEM; 1270 1271 for (cp = 0; cp < max_count; cp++) { 1272 err = -EFAULT; 1273 if (copy_from_user(key, keys + cp * map->key_size, 1274 map->key_size)) 1275 break; 1276 1277 if (bpf_map_is_dev_bound(map)) { 1278 err = bpf_map_offload_delete_elem(map, key); 1279 break; 1280 } 1281 1282 bpf_disable_instrumentation(); 1283 rcu_read_lock(); 1284 err = map->ops->map_delete_elem(map, key); 1285 rcu_read_unlock(); 1286 bpf_enable_instrumentation(); 1287 maybe_wait_bpf_programs(map); 1288 if (err) 1289 break; 1290 } 1291 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) 1292 err = -EFAULT; 1293 1294 kfree(key); 1295 return err; 1296 } 1297 1298 int generic_map_update_batch(struct bpf_map *map, 1299 const union bpf_attr *attr, 1300 union bpf_attr __user *uattr) 1301 { 1302 void __user *values = u64_to_user_ptr(attr->batch.values); 1303 void __user *keys = u64_to_user_ptr(attr->batch.keys); 1304 u32 value_size, cp, max_count; 1305 int ufd = attr->map_fd; 1306 void *key, *value; 1307 struct fd f; 1308 int err = 0; 1309 1310 f = fdget(ufd); 1311 if (attr->batch.elem_flags & ~BPF_F_LOCK) 1312 return -EINVAL; 1313 1314 if ((attr->batch.elem_flags & BPF_F_LOCK) && 1315 !map_value_has_spin_lock(map)) { 1316 return -EINVAL; 1317 } 1318 1319 value_size = bpf_map_value_size(map); 1320 1321 max_count = attr->batch.count; 1322 if (!max_count) 1323 return 0; 1324 1325 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN); 1326 if (!key) 1327 return -ENOMEM; 1328 1329 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 1330 if (!value) { 1331 kfree(key); 1332 return -ENOMEM; 1333 } 1334 1335 for (cp = 0; cp < max_count; cp++) { 1336 err = -EFAULT; 1337 if (copy_from_user(key, keys + cp * map->key_size, 1338 map->key_size) || 1339 copy_from_user(value, values + cp * value_size, value_size)) 1340 break; 1341 1342 err = bpf_map_update_value(map, f, key, value, 1343 attr->batch.elem_flags); 1344 1345 if (err) 1346 break; 1347 } 1348 1349 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) 1350 err = -EFAULT; 1351 1352 kfree(value); 1353 kfree(key); 1354 return err; 1355 } 1356 1357 #define MAP_LOOKUP_RETRIES 3 1358 1359 int generic_map_lookup_batch(struct bpf_map *map, 1360 const union bpf_attr *attr, 1361 union bpf_attr __user *uattr) 1362 { 1363 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch); 1364 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch); 1365 void __user *values = u64_to_user_ptr(attr->batch.values); 1366 void __user *keys = u64_to_user_ptr(attr->batch.keys); 1367 void *buf, *buf_prevkey, *prev_key, *key, *value; 1368 int err, retry = MAP_LOOKUP_RETRIES; 1369 u32 value_size, cp, max_count; 1370 1371 if (attr->batch.elem_flags & ~BPF_F_LOCK) 1372 return -EINVAL; 1373 1374 if ((attr->batch.elem_flags & BPF_F_LOCK) && 1375 !map_value_has_spin_lock(map)) 1376 return -EINVAL; 1377 1378 value_size = bpf_map_value_size(map); 1379 1380 max_count = attr->batch.count; 1381 if (!max_count) 1382 return 0; 1383 1384 if (put_user(0, &uattr->batch.count)) 1385 return -EFAULT; 1386 1387 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN); 1388 if (!buf_prevkey) 1389 return -ENOMEM; 1390 1391 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN); 1392 if (!buf) { 1393 kfree(buf_prevkey); 1394 return -ENOMEM; 1395 } 1396 1397 err = -EFAULT; 1398 prev_key = NULL; 1399 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size)) 1400 goto free_buf; 1401 key = buf; 1402 value = key + map->key_size; 1403 if (ubatch) 1404 prev_key = buf_prevkey; 1405 1406 for (cp = 0; cp < max_count;) { 1407 rcu_read_lock(); 1408 err = map->ops->map_get_next_key(map, prev_key, key); 1409 rcu_read_unlock(); 1410 if (err) 1411 break; 1412 err = bpf_map_copy_value(map, key, value, 1413 attr->batch.elem_flags); 1414 1415 if (err == -ENOENT) { 1416 if (retry) { 1417 retry--; 1418 continue; 1419 } 1420 err = -EINTR; 1421 break; 1422 } 1423 1424 if (err) 1425 goto free_buf; 1426 1427 if (copy_to_user(keys + cp * map->key_size, key, 1428 map->key_size)) { 1429 err = -EFAULT; 1430 goto free_buf; 1431 } 1432 if (copy_to_user(values + cp * value_size, value, value_size)) { 1433 err = -EFAULT; 1434 goto free_buf; 1435 } 1436 1437 if (!prev_key) 1438 prev_key = buf_prevkey; 1439 1440 swap(prev_key, key); 1441 retry = MAP_LOOKUP_RETRIES; 1442 cp++; 1443 } 1444 1445 if (err == -EFAULT) 1446 goto free_buf; 1447 1448 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) || 1449 (cp && copy_to_user(uobatch, prev_key, map->key_size)))) 1450 err = -EFAULT; 1451 1452 free_buf: 1453 kfree(buf_prevkey); 1454 kfree(buf); 1455 return err; 1456 } 1457 1458 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value 1459 1460 static int map_lookup_and_delete_elem(union bpf_attr *attr) 1461 { 1462 void __user *ukey = u64_to_user_ptr(attr->key); 1463 void __user *uvalue = u64_to_user_ptr(attr->value); 1464 int ufd = attr->map_fd; 1465 struct bpf_map *map; 1466 void *key, *value; 1467 u32 value_size; 1468 struct fd f; 1469 int err; 1470 1471 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM)) 1472 return -EINVAL; 1473 1474 f = fdget(ufd); 1475 map = __bpf_map_get(f); 1476 if (IS_ERR(map)) 1477 return PTR_ERR(map); 1478 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) || 1479 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 1480 err = -EPERM; 1481 goto err_put; 1482 } 1483 1484 key = __bpf_copy_key(ukey, map->key_size); 1485 if (IS_ERR(key)) { 1486 err = PTR_ERR(key); 1487 goto err_put; 1488 } 1489 1490 value_size = map->value_size; 1491 1492 err = -ENOMEM; 1493 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 1494 if (!value) 1495 goto free_key; 1496 1497 if (map->map_type == BPF_MAP_TYPE_QUEUE || 1498 map->map_type == BPF_MAP_TYPE_STACK) { 1499 err = map->ops->map_pop_elem(map, value); 1500 } else { 1501 err = -ENOTSUPP; 1502 } 1503 1504 if (err) 1505 goto free_value; 1506 1507 if (copy_to_user(uvalue, value, value_size) != 0) { 1508 err = -EFAULT; 1509 goto free_value; 1510 } 1511 1512 err = 0; 1513 1514 free_value: 1515 kfree(value); 1516 free_key: 1517 kfree(key); 1518 err_put: 1519 fdput(f); 1520 return err; 1521 } 1522 1523 #define BPF_MAP_FREEZE_LAST_FIELD map_fd 1524 1525 static int map_freeze(const union bpf_attr *attr) 1526 { 1527 int err = 0, ufd = attr->map_fd; 1528 struct bpf_map *map; 1529 struct fd f; 1530 1531 if (CHECK_ATTR(BPF_MAP_FREEZE)) 1532 return -EINVAL; 1533 1534 f = fdget(ufd); 1535 map = __bpf_map_get(f); 1536 if (IS_ERR(map)) 1537 return PTR_ERR(map); 1538 1539 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 1540 fdput(f); 1541 return -ENOTSUPP; 1542 } 1543 1544 mutex_lock(&map->freeze_mutex); 1545 1546 if (map->writecnt) { 1547 err = -EBUSY; 1548 goto err_put; 1549 } 1550 if (READ_ONCE(map->frozen)) { 1551 err = -EBUSY; 1552 goto err_put; 1553 } 1554 if (!bpf_capable()) { 1555 err = -EPERM; 1556 goto err_put; 1557 } 1558 1559 WRITE_ONCE(map->frozen, true); 1560 err_put: 1561 mutex_unlock(&map->freeze_mutex); 1562 fdput(f); 1563 return err; 1564 } 1565 1566 static const struct bpf_prog_ops * const bpf_prog_types[] = { 1567 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 1568 [_id] = & _name ## _prog_ops, 1569 #define BPF_MAP_TYPE(_id, _ops) 1570 #define BPF_LINK_TYPE(_id, _name) 1571 #include <linux/bpf_types.h> 1572 #undef BPF_PROG_TYPE 1573 #undef BPF_MAP_TYPE 1574 #undef BPF_LINK_TYPE 1575 }; 1576 1577 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) 1578 { 1579 const struct bpf_prog_ops *ops; 1580 1581 if (type >= ARRAY_SIZE(bpf_prog_types)) 1582 return -EINVAL; 1583 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); 1584 ops = bpf_prog_types[type]; 1585 if (!ops) 1586 return -EINVAL; 1587 1588 if (!bpf_prog_is_dev_bound(prog->aux)) 1589 prog->aux->ops = ops; 1590 else 1591 prog->aux->ops = &bpf_offload_prog_ops; 1592 prog->type = type; 1593 return 0; 1594 } 1595 1596 enum bpf_audit { 1597 BPF_AUDIT_LOAD, 1598 BPF_AUDIT_UNLOAD, 1599 BPF_AUDIT_MAX, 1600 }; 1601 1602 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = { 1603 [BPF_AUDIT_LOAD] = "LOAD", 1604 [BPF_AUDIT_UNLOAD] = "UNLOAD", 1605 }; 1606 1607 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op) 1608 { 1609 struct audit_context *ctx = NULL; 1610 struct audit_buffer *ab; 1611 1612 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX)) 1613 return; 1614 if (audit_enabled == AUDIT_OFF) 1615 return; 1616 if (op == BPF_AUDIT_LOAD) 1617 ctx = audit_context(); 1618 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF); 1619 if (unlikely(!ab)) 1620 return; 1621 audit_log_format(ab, "prog-id=%u op=%s", 1622 prog->aux->id, bpf_audit_str[op]); 1623 audit_log_end(ab); 1624 } 1625 1626 int __bpf_prog_charge(struct user_struct *user, u32 pages) 1627 { 1628 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 1629 unsigned long user_bufs; 1630 1631 if (user) { 1632 user_bufs = atomic_long_add_return(pages, &user->locked_vm); 1633 if (user_bufs > memlock_limit) { 1634 atomic_long_sub(pages, &user->locked_vm); 1635 return -EPERM; 1636 } 1637 } 1638 1639 return 0; 1640 } 1641 1642 void __bpf_prog_uncharge(struct user_struct *user, u32 pages) 1643 { 1644 if (user) 1645 atomic_long_sub(pages, &user->locked_vm); 1646 } 1647 1648 static int bpf_prog_charge_memlock(struct bpf_prog *prog) 1649 { 1650 struct user_struct *user = get_current_user(); 1651 int ret; 1652 1653 ret = __bpf_prog_charge(user, prog->pages); 1654 if (ret) { 1655 free_uid(user); 1656 return ret; 1657 } 1658 1659 prog->aux->user = user; 1660 return 0; 1661 } 1662 1663 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) 1664 { 1665 struct user_struct *user = prog->aux->user; 1666 1667 __bpf_prog_uncharge(user, prog->pages); 1668 free_uid(user); 1669 } 1670 1671 static int bpf_prog_alloc_id(struct bpf_prog *prog) 1672 { 1673 int id; 1674 1675 idr_preload(GFP_KERNEL); 1676 spin_lock_bh(&prog_idr_lock); 1677 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); 1678 if (id > 0) 1679 prog->aux->id = id; 1680 spin_unlock_bh(&prog_idr_lock); 1681 idr_preload_end(); 1682 1683 /* id is in [1, INT_MAX) */ 1684 if (WARN_ON_ONCE(!id)) 1685 return -ENOSPC; 1686 1687 return id > 0 ? 0 : id; 1688 } 1689 1690 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock) 1691 { 1692 /* cBPF to eBPF migrations are currently not in the idr store. 1693 * Offloaded programs are removed from the store when their device 1694 * disappears - even if someone grabs an fd to them they are unusable, 1695 * simply waiting for refcnt to drop to be freed. 1696 */ 1697 if (!prog->aux->id) 1698 return; 1699 1700 if (do_idr_lock) 1701 spin_lock_bh(&prog_idr_lock); 1702 else 1703 __acquire(&prog_idr_lock); 1704 1705 idr_remove(&prog_idr, prog->aux->id); 1706 prog->aux->id = 0; 1707 1708 if (do_idr_lock) 1709 spin_unlock_bh(&prog_idr_lock); 1710 else 1711 __release(&prog_idr_lock); 1712 } 1713 1714 static void __bpf_prog_put_rcu(struct rcu_head *rcu) 1715 { 1716 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); 1717 1718 kvfree(aux->func_info); 1719 kfree(aux->func_info_aux); 1720 bpf_prog_uncharge_memlock(aux->prog); 1721 security_bpf_prog_free(aux); 1722 bpf_prog_free(aux->prog); 1723 } 1724 1725 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) 1726 { 1727 bpf_prog_kallsyms_del_all(prog); 1728 btf_put(prog->aux->btf); 1729 bpf_prog_free_linfo(prog); 1730 1731 if (deferred) 1732 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); 1733 else 1734 __bpf_prog_put_rcu(&prog->aux->rcu); 1735 } 1736 1737 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) 1738 { 1739 if (atomic64_dec_and_test(&prog->aux->refcnt)) { 1740 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0); 1741 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD); 1742 /* bpf_prog_free_id() must be called first */ 1743 bpf_prog_free_id(prog, do_idr_lock); 1744 __bpf_prog_put_noref(prog, true); 1745 } 1746 } 1747 1748 void bpf_prog_put(struct bpf_prog *prog) 1749 { 1750 __bpf_prog_put(prog, true); 1751 } 1752 EXPORT_SYMBOL_GPL(bpf_prog_put); 1753 1754 static int bpf_prog_release(struct inode *inode, struct file *filp) 1755 { 1756 struct bpf_prog *prog = filp->private_data; 1757 1758 bpf_prog_put(prog); 1759 return 0; 1760 } 1761 1762 static void bpf_prog_get_stats(const struct bpf_prog *prog, 1763 struct bpf_prog_stats *stats) 1764 { 1765 u64 nsecs = 0, cnt = 0; 1766 int cpu; 1767 1768 for_each_possible_cpu(cpu) { 1769 const struct bpf_prog_stats *st; 1770 unsigned int start; 1771 u64 tnsecs, tcnt; 1772 1773 st = per_cpu_ptr(prog->aux->stats, cpu); 1774 do { 1775 start = u64_stats_fetch_begin_irq(&st->syncp); 1776 tnsecs = st->nsecs; 1777 tcnt = st->cnt; 1778 } while (u64_stats_fetch_retry_irq(&st->syncp, start)); 1779 nsecs += tnsecs; 1780 cnt += tcnt; 1781 } 1782 stats->nsecs = nsecs; 1783 stats->cnt = cnt; 1784 } 1785 1786 #ifdef CONFIG_PROC_FS 1787 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) 1788 { 1789 const struct bpf_prog *prog = filp->private_data; 1790 char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 1791 struct bpf_prog_stats stats; 1792 1793 bpf_prog_get_stats(prog, &stats); 1794 bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 1795 seq_printf(m, 1796 "prog_type:\t%u\n" 1797 "prog_jited:\t%u\n" 1798 "prog_tag:\t%s\n" 1799 "memlock:\t%llu\n" 1800 "prog_id:\t%u\n" 1801 "run_time_ns:\t%llu\n" 1802 "run_cnt:\t%llu\n", 1803 prog->type, 1804 prog->jited, 1805 prog_tag, 1806 prog->pages * 1ULL << PAGE_SHIFT, 1807 prog->aux->id, 1808 stats.nsecs, 1809 stats.cnt); 1810 } 1811 #endif 1812 1813 const struct file_operations bpf_prog_fops = { 1814 #ifdef CONFIG_PROC_FS 1815 .show_fdinfo = bpf_prog_show_fdinfo, 1816 #endif 1817 .release = bpf_prog_release, 1818 .read = bpf_dummy_read, 1819 .write = bpf_dummy_write, 1820 }; 1821 1822 int bpf_prog_new_fd(struct bpf_prog *prog) 1823 { 1824 int ret; 1825 1826 ret = security_bpf_prog(prog); 1827 if (ret < 0) 1828 return ret; 1829 1830 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, 1831 O_RDWR | O_CLOEXEC); 1832 } 1833 1834 static struct bpf_prog *____bpf_prog_get(struct fd f) 1835 { 1836 if (!f.file) 1837 return ERR_PTR(-EBADF); 1838 if (f.file->f_op != &bpf_prog_fops) { 1839 fdput(f); 1840 return ERR_PTR(-EINVAL); 1841 } 1842 1843 return f.file->private_data; 1844 } 1845 1846 void bpf_prog_add(struct bpf_prog *prog, int i) 1847 { 1848 atomic64_add(i, &prog->aux->refcnt); 1849 } 1850 EXPORT_SYMBOL_GPL(bpf_prog_add); 1851 1852 void bpf_prog_sub(struct bpf_prog *prog, int i) 1853 { 1854 /* Only to be used for undoing previous bpf_prog_add() in some 1855 * error path. We still know that another entity in our call 1856 * path holds a reference to the program, thus atomic_sub() can 1857 * be safely used in such cases! 1858 */ 1859 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0); 1860 } 1861 EXPORT_SYMBOL_GPL(bpf_prog_sub); 1862 1863 void bpf_prog_inc(struct bpf_prog *prog) 1864 { 1865 atomic64_inc(&prog->aux->refcnt); 1866 } 1867 EXPORT_SYMBOL_GPL(bpf_prog_inc); 1868 1869 /* prog_idr_lock should have been held */ 1870 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) 1871 { 1872 int refold; 1873 1874 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0); 1875 1876 if (!refold) 1877 return ERR_PTR(-ENOENT); 1878 1879 return prog; 1880 } 1881 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); 1882 1883 bool bpf_prog_get_ok(struct bpf_prog *prog, 1884 enum bpf_prog_type *attach_type, bool attach_drv) 1885 { 1886 /* not an attachment, just a refcount inc, always allow */ 1887 if (!attach_type) 1888 return true; 1889 1890 if (prog->type != *attach_type) 1891 return false; 1892 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) 1893 return false; 1894 1895 return true; 1896 } 1897 1898 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, 1899 bool attach_drv) 1900 { 1901 struct fd f = fdget(ufd); 1902 struct bpf_prog *prog; 1903 1904 prog = ____bpf_prog_get(f); 1905 if (IS_ERR(prog)) 1906 return prog; 1907 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { 1908 prog = ERR_PTR(-EINVAL); 1909 goto out; 1910 } 1911 1912 bpf_prog_inc(prog); 1913 out: 1914 fdput(f); 1915 return prog; 1916 } 1917 1918 struct bpf_prog *bpf_prog_get(u32 ufd) 1919 { 1920 return __bpf_prog_get(ufd, NULL, false); 1921 } 1922 1923 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, 1924 bool attach_drv) 1925 { 1926 return __bpf_prog_get(ufd, &type, attach_drv); 1927 } 1928 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); 1929 1930 /* Initially all BPF programs could be loaded w/o specifying 1931 * expected_attach_type. Later for some of them specifying expected_attach_type 1932 * at load time became required so that program could be validated properly. 1933 * Programs of types that are allowed to be loaded both w/ and w/o (for 1934 * backward compatibility) expected_attach_type, should have the default attach 1935 * type assigned to expected_attach_type for the latter case, so that it can be 1936 * validated later at attach time. 1937 * 1938 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if 1939 * prog type requires it but has some attach types that have to be backward 1940 * compatible. 1941 */ 1942 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) 1943 { 1944 switch (attr->prog_type) { 1945 case BPF_PROG_TYPE_CGROUP_SOCK: 1946 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't 1947 * exist so checking for non-zero is the way to go here. 1948 */ 1949 if (!attr->expected_attach_type) 1950 attr->expected_attach_type = 1951 BPF_CGROUP_INET_SOCK_CREATE; 1952 break; 1953 } 1954 } 1955 1956 static int 1957 bpf_prog_load_check_attach(enum bpf_prog_type prog_type, 1958 enum bpf_attach_type expected_attach_type, 1959 u32 btf_id, u32 prog_fd) 1960 { 1961 if (btf_id) { 1962 if (btf_id > BTF_MAX_TYPE) 1963 return -EINVAL; 1964 1965 switch (prog_type) { 1966 case BPF_PROG_TYPE_TRACING: 1967 case BPF_PROG_TYPE_LSM: 1968 case BPF_PROG_TYPE_STRUCT_OPS: 1969 case BPF_PROG_TYPE_EXT: 1970 break; 1971 default: 1972 return -EINVAL; 1973 } 1974 } 1975 1976 if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING && 1977 prog_type != BPF_PROG_TYPE_EXT) 1978 return -EINVAL; 1979 1980 switch (prog_type) { 1981 case BPF_PROG_TYPE_CGROUP_SOCK: 1982 switch (expected_attach_type) { 1983 case BPF_CGROUP_INET_SOCK_CREATE: 1984 case BPF_CGROUP_INET_SOCK_RELEASE: 1985 case BPF_CGROUP_INET4_POST_BIND: 1986 case BPF_CGROUP_INET6_POST_BIND: 1987 return 0; 1988 default: 1989 return -EINVAL; 1990 } 1991 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 1992 switch (expected_attach_type) { 1993 case BPF_CGROUP_INET4_BIND: 1994 case BPF_CGROUP_INET6_BIND: 1995 case BPF_CGROUP_INET4_CONNECT: 1996 case BPF_CGROUP_INET6_CONNECT: 1997 case BPF_CGROUP_INET4_GETPEERNAME: 1998 case BPF_CGROUP_INET6_GETPEERNAME: 1999 case BPF_CGROUP_INET4_GETSOCKNAME: 2000 case BPF_CGROUP_INET6_GETSOCKNAME: 2001 case BPF_CGROUP_UDP4_SENDMSG: 2002 case BPF_CGROUP_UDP6_SENDMSG: 2003 case BPF_CGROUP_UDP4_RECVMSG: 2004 case BPF_CGROUP_UDP6_RECVMSG: 2005 return 0; 2006 default: 2007 return -EINVAL; 2008 } 2009 case BPF_PROG_TYPE_CGROUP_SKB: 2010 switch (expected_attach_type) { 2011 case BPF_CGROUP_INET_INGRESS: 2012 case BPF_CGROUP_INET_EGRESS: 2013 return 0; 2014 default: 2015 return -EINVAL; 2016 } 2017 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2018 switch (expected_attach_type) { 2019 case BPF_CGROUP_SETSOCKOPT: 2020 case BPF_CGROUP_GETSOCKOPT: 2021 return 0; 2022 default: 2023 return -EINVAL; 2024 } 2025 case BPF_PROG_TYPE_EXT: 2026 if (expected_attach_type) 2027 return -EINVAL; 2028 /* fallthrough */ 2029 default: 2030 return 0; 2031 } 2032 } 2033 2034 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type) 2035 { 2036 switch (prog_type) { 2037 case BPF_PROG_TYPE_SCHED_CLS: 2038 case BPF_PROG_TYPE_SCHED_ACT: 2039 case BPF_PROG_TYPE_XDP: 2040 case BPF_PROG_TYPE_LWT_IN: 2041 case BPF_PROG_TYPE_LWT_OUT: 2042 case BPF_PROG_TYPE_LWT_XMIT: 2043 case BPF_PROG_TYPE_LWT_SEG6LOCAL: 2044 case BPF_PROG_TYPE_SK_SKB: 2045 case BPF_PROG_TYPE_SK_MSG: 2046 case BPF_PROG_TYPE_LIRC_MODE2: 2047 case BPF_PROG_TYPE_FLOW_DISSECTOR: 2048 case BPF_PROG_TYPE_CGROUP_DEVICE: 2049 case BPF_PROG_TYPE_CGROUP_SOCK: 2050 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2051 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2052 case BPF_PROG_TYPE_CGROUP_SYSCTL: 2053 case BPF_PROG_TYPE_SOCK_OPS: 2054 case BPF_PROG_TYPE_EXT: /* extends any prog */ 2055 return true; 2056 case BPF_PROG_TYPE_CGROUP_SKB: 2057 /* always unpriv */ 2058 case BPF_PROG_TYPE_SK_REUSEPORT: 2059 /* equivalent to SOCKET_FILTER. need CAP_BPF only */ 2060 default: 2061 return false; 2062 } 2063 } 2064 2065 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) 2066 { 2067 switch (prog_type) { 2068 case BPF_PROG_TYPE_KPROBE: 2069 case BPF_PROG_TYPE_TRACEPOINT: 2070 case BPF_PROG_TYPE_PERF_EVENT: 2071 case BPF_PROG_TYPE_RAW_TRACEPOINT: 2072 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 2073 case BPF_PROG_TYPE_TRACING: 2074 case BPF_PROG_TYPE_LSM: 2075 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */ 2076 case BPF_PROG_TYPE_EXT: /* extends any prog */ 2077 return true; 2078 default: 2079 return false; 2080 } 2081 } 2082 2083 /* last field in 'union bpf_attr' used by this command */ 2084 #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd 2085 2086 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) 2087 { 2088 enum bpf_prog_type type = attr->prog_type; 2089 struct bpf_prog *prog; 2090 int err; 2091 char license[128]; 2092 bool is_gpl; 2093 2094 if (CHECK_ATTR(BPF_PROG_LOAD)) 2095 return -EINVAL; 2096 2097 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | 2098 BPF_F_ANY_ALIGNMENT | 2099 BPF_F_TEST_STATE_FREQ | 2100 BPF_F_TEST_RND_HI32)) 2101 return -EINVAL; 2102 2103 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && 2104 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && 2105 !bpf_capable()) 2106 return -EPERM; 2107 2108 /* copy eBPF program license from user space */ 2109 if (strncpy_from_user(license, u64_to_user_ptr(attr->license), 2110 sizeof(license) - 1) < 0) 2111 return -EFAULT; 2112 license[sizeof(license) - 1] = 0; 2113 2114 /* eBPF programs must be GPL compatible to use GPL-ed functions */ 2115 is_gpl = license_is_gpl_compatible(license); 2116 2117 if (attr->insn_cnt == 0 || 2118 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) 2119 return -E2BIG; 2120 if (type != BPF_PROG_TYPE_SOCKET_FILTER && 2121 type != BPF_PROG_TYPE_CGROUP_SKB && 2122 !bpf_capable()) 2123 return -EPERM; 2124 2125 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN)) 2126 return -EPERM; 2127 if (is_perfmon_prog_type(type) && !perfmon_capable()) 2128 return -EPERM; 2129 2130 bpf_prog_load_fixup_attach_type(attr); 2131 if (bpf_prog_load_check_attach(type, attr->expected_attach_type, 2132 attr->attach_btf_id, 2133 attr->attach_prog_fd)) 2134 return -EINVAL; 2135 2136 /* plain bpf_prog allocation */ 2137 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); 2138 if (!prog) 2139 return -ENOMEM; 2140 2141 prog->expected_attach_type = attr->expected_attach_type; 2142 prog->aux->attach_btf_id = attr->attach_btf_id; 2143 if (attr->attach_prog_fd) { 2144 struct bpf_prog *tgt_prog; 2145 2146 tgt_prog = bpf_prog_get(attr->attach_prog_fd); 2147 if (IS_ERR(tgt_prog)) { 2148 err = PTR_ERR(tgt_prog); 2149 goto free_prog_nouncharge; 2150 } 2151 prog->aux->linked_prog = tgt_prog; 2152 } 2153 2154 prog->aux->offload_requested = !!attr->prog_ifindex; 2155 2156 err = security_bpf_prog_alloc(prog->aux); 2157 if (err) 2158 goto free_prog_nouncharge; 2159 2160 err = bpf_prog_charge_memlock(prog); 2161 if (err) 2162 goto free_prog_sec; 2163 2164 prog->len = attr->insn_cnt; 2165 2166 err = -EFAULT; 2167 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), 2168 bpf_prog_insn_size(prog)) != 0) 2169 goto free_prog; 2170 2171 prog->orig_prog = NULL; 2172 prog->jited = 0; 2173 2174 atomic64_set(&prog->aux->refcnt, 1); 2175 prog->gpl_compatible = is_gpl ? 1 : 0; 2176 2177 if (bpf_prog_is_dev_bound(prog->aux)) { 2178 err = bpf_prog_offload_init(prog, attr); 2179 if (err) 2180 goto free_prog; 2181 } 2182 2183 /* find program type: socket_filter vs tracing_filter */ 2184 err = find_prog_type(type, prog); 2185 if (err < 0) 2186 goto free_prog; 2187 2188 prog->aux->load_time = ktime_get_boottime_ns(); 2189 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name, 2190 sizeof(attr->prog_name)); 2191 if (err < 0) 2192 goto free_prog; 2193 2194 /* run eBPF verifier */ 2195 err = bpf_check(&prog, attr, uattr); 2196 if (err < 0) 2197 goto free_used_maps; 2198 2199 prog = bpf_prog_select_runtime(prog, &err); 2200 if (err < 0) 2201 goto free_used_maps; 2202 2203 err = bpf_prog_alloc_id(prog); 2204 if (err) 2205 goto free_used_maps; 2206 2207 /* Upon success of bpf_prog_alloc_id(), the BPF prog is 2208 * effectively publicly exposed. However, retrieving via 2209 * bpf_prog_get_fd_by_id() will take another reference, 2210 * therefore it cannot be gone underneath us. 2211 * 2212 * Only for the time /after/ successful bpf_prog_new_fd() 2213 * and before returning to userspace, we might just hold 2214 * one reference and any parallel close on that fd could 2215 * rip everything out. Hence, below notifications must 2216 * happen before bpf_prog_new_fd(). 2217 * 2218 * Also, any failure handling from this point onwards must 2219 * be using bpf_prog_put() given the program is exposed. 2220 */ 2221 bpf_prog_kallsyms_add(prog); 2222 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); 2223 bpf_audit_prog(prog, BPF_AUDIT_LOAD); 2224 2225 err = bpf_prog_new_fd(prog); 2226 if (err < 0) 2227 bpf_prog_put(prog); 2228 return err; 2229 2230 free_used_maps: 2231 /* In case we have subprogs, we need to wait for a grace 2232 * period before we can tear down JIT memory since symbols 2233 * are already exposed under kallsyms. 2234 */ 2235 __bpf_prog_put_noref(prog, prog->aux->func_cnt); 2236 return err; 2237 free_prog: 2238 bpf_prog_uncharge_memlock(prog); 2239 free_prog_sec: 2240 security_bpf_prog_free(prog->aux); 2241 free_prog_nouncharge: 2242 bpf_prog_free(prog); 2243 return err; 2244 } 2245 2246 #define BPF_OBJ_LAST_FIELD file_flags 2247 2248 static int bpf_obj_pin(const union bpf_attr *attr) 2249 { 2250 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) 2251 return -EINVAL; 2252 2253 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); 2254 } 2255 2256 static int bpf_obj_get(const union bpf_attr *attr) 2257 { 2258 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || 2259 attr->file_flags & ~BPF_OBJ_FLAG_MASK) 2260 return -EINVAL; 2261 2262 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), 2263 attr->file_flags); 2264 } 2265 2266 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, 2267 const struct bpf_link_ops *ops, struct bpf_prog *prog) 2268 { 2269 atomic64_set(&link->refcnt, 1); 2270 link->type = type; 2271 link->id = 0; 2272 link->ops = ops; 2273 link->prog = prog; 2274 } 2275 2276 static void bpf_link_free_id(int id) 2277 { 2278 if (!id) 2279 return; 2280 2281 spin_lock_bh(&link_idr_lock); 2282 idr_remove(&link_idr, id); 2283 spin_unlock_bh(&link_idr_lock); 2284 } 2285 2286 /* Clean up bpf_link and corresponding anon_inode file and FD. After 2287 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred 2288 * anon_inode's release() call. This helper marksbpf_link as 2289 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt 2290 * is not decremented, it's the responsibility of a calling code that failed 2291 * to complete bpf_link initialization. 2292 */ 2293 void bpf_link_cleanup(struct bpf_link_primer *primer) 2294 { 2295 primer->link->prog = NULL; 2296 bpf_link_free_id(primer->id); 2297 fput(primer->file); 2298 put_unused_fd(primer->fd); 2299 } 2300 2301 void bpf_link_inc(struct bpf_link *link) 2302 { 2303 atomic64_inc(&link->refcnt); 2304 } 2305 2306 /* bpf_link_free is guaranteed to be called from process context */ 2307 static void bpf_link_free(struct bpf_link *link) 2308 { 2309 bpf_link_free_id(link->id); 2310 if (link->prog) { 2311 /* detach BPF program, clean up used resources */ 2312 link->ops->release(link); 2313 bpf_prog_put(link->prog); 2314 } 2315 /* free bpf_link and its containing memory */ 2316 link->ops->dealloc(link); 2317 } 2318 2319 static void bpf_link_put_deferred(struct work_struct *work) 2320 { 2321 struct bpf_link *link = container_of(work, struct bpf_link, work); 2322 2323 bpf_link_free(link); 2324 } 2325 2326 /* bpf_link_put can be called from atomic context, but ensures that resources 2327 * are freed from process context 2328 */ 2329 void bpf_link_put(struct bpf_link *link) 2330 { 2331 if (!atomic64_dec_and_test(&link->refcnt)) 2332 return; 2333 2334 if (in_atomic()) { 2335 INIT_WORK(&link->work, bpf_link_put_deferred); 2336 schedule_work(&link->work); 2337 } else { 2338 bpf_link_free(link); 2339 } 2340 } 2341 2342 static int bpf_link_release(struct inode *inode, struct file *filp) 2343 { 2344 struct bpf_link *link = filp->private_data; 2345 2346 bpf_link_put(link); 2347 return 0; 2348 } 2349 2350 #ifdef CONFIG_PROC_FS 2351 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) 2352 #define BPF_MAP_TYPE(_id, _ops) 2353 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name, 2354 static const char *bpf_link_type_strs[] = { 2355 [BPF_LINK_TYPE_UNSPEC] = "<invalid>", 2356 #include <linux/bpf_types.h> 2357 }; 2358 #undef BPF_PROG_TYPE 2359 #undef BPF_MAP_TYPE 2360 #undef BPF_LINK_TYPE 2361 2362 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp) 2363 { 2364 const struct bpf_link *link = filp->private_data; 2365 const struct bpf_prog *prog = link->prog; 2366 char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 2367 2368 bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 2369 seq_printf(m, 2370 "link_type:\t%s\n" 2371 "link_id:\t%u\n" 2372 "prog_tag:\t%s\n" 2373 "prog_id:\t%u\n", 2374 bpf_link_type_strs[link->type], 2375 link->id, 2376 prog_tag, 2377 prog->aux->id); 2378 if (link->ops->show_fdinfo) 2379 link->ops->show_fdinfo(link, m); 2380 } 2381 #endif 2382 2383 static const struct file_operations bpf_link_fops = { 2384 #ifdef CONFIG_PROC_FS 2385 .show_fdinfo = bpf_link_show_fdinfo, 2386 #endif 2387 .release = bpf_link_release, 2388 .read = bpf_dummy_read, 2389 .write = bpf_dummy_write, 2390 }; 2391 2392 static int bpf_link_alloc_id(struct bpf_link *link) 2393 { 2394 int id; 2395 2396 idr_preload(GFP_KERNEL); 2397 spin_lock_bh(&link_idr_lock); 2398 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC); 2399 spin_unlock_bh(&link_idr_lock); 2400 idr_preload_end(); 2401 2402 return id; 2403 } 2404 2405 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file, 2406 * reserving unused FD and allocating ID from link_idr. This is to be paired 2407 * with bpf_link_settle() to install FD and ID and expose bpf_link to 2408 * user-space, if bpf_link is successfully attached. If not, bpf_link and 2409 * pre-allocated resources are to be freed with bpf_cleanup() call. All the 2410 * transient state is passed around in struct bpf_link_primer. 2411 * This is preferred way to create and initialize bpf_link, especially when 2412 * there are complicated and expensive operations inbetween creating bpf_link 2413 * itself and attaching it to BPF hook. By using bpf_link_prime() and 2414 * bpf_link_settle() kernel code using bpf_link doesn't have to perform 2415 * expensive (and potentially failing) roll back operations in a rare case 2416 * that file, FD, or ID can't be allocated. 2417 */ 2418 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer) 2419 { 2420 struct file *file; 2421 int fd, id; 2422 2423 fd = get_unused_fd_flags(O_CLOEXEC); 2424 if (fd < 0) 2425 return fd; 2426 2427 2428 id = bpf_link_alloc_id(link); 2429 if (id < 0) { 2430 put_unused_fd(fd); 2431 return id; 2432 } 2433 2434 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC); 2435 if (IS_ERR(file)) { 2436 bpf_link_free_id(id); 2437 put_unused_fd(fd); 2438 return PTR_ERR(file); 2439 } 2440 2441 primer->link = link; 2442 primer->file = file; 2443 primer->fd = fd; 2444 primer->id = id; 2445 return 0; 2446 } 2447 2448 int bpf_link_settle(struct bpf_link_primer *primer) 2449 { 2450 /* make bpf_link fetchable by ID */ 2451 spin_lock_bh(&link_idr_lock); 2452 primer->link->id = primer->id; 2453 spin_unlock_bh(&link_idr_lock); 2454 /* make bpf_link fetchable by FD */ 2455 fd_install(primer->fd, primer->file); 2456 /* pass through installed FD */ 2457 return primer->fd; 2458 } 2459 2460 int bpf_link_new_fd(struct bpf_link *link) 2461 { 2462 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC); 2463 } 2464 2465 struct bpf_link *bpf_link_get_from_fd(u32 ufd) 2466 { 2467 struct fd f = fdget(ufd); 2468 struct bpf_link *link; 2469 2470 if (!f.file) 2471 return ERR_PTR(-EBADF); 2472 if (f.file->f_op != &bpf_link_fops) { 2473 fdput(f); 2474 return ERR_PTR(-EINVAL); 2475 } 2476 2477 link = f.file->private_data; 2478 bpf_link_inc(link); 2479 fdput(f); 2480 2481 return link; 2482 } 2483 2484 struct bpf_tracing_link { 2485 struct bpf_link link; 2486 enum bpf_attach_type attach_type; 2487 }; 2488 2489 static void bpf_tracing_link_release(struct bpf_link *link) 2490 { 2491 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog)); 2492 } 2493 2494 static void bpf_tracing_link_dealloc(struct bpf_link *link) 2495 { 2496 struct bpf_tracing_link *tr_link = 2497 container_of(link, struct bpf_tracing_link, link); 2498 2499 kfree(tr_link); 2500 } 2501 2502 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link, 2503 struct seq_file *seq) 2504 { 2505 struct bpf_tracing_link *tr_link = 2506 container_of(link, struct bpf_tracing_link, link); 2507 2508 seq_printf(seq, 2509 "attach_type:\t%d\n", 2510 tr_link->attach_type); 2511 } 2512 2513 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, 2514 struct bpf_link_info *info) 2515 { 2516 struct bpf_tracing_link *tr_link = 2517 container_of(link, struct bpf_tracing_link, link); 2518 2519 info->tracing.attach_type = tr_link->attach_type; 2520 2521 return 0; 2522 } 2523 2524 static const struct bpf_link_ops bpf_tracing_link_lops = { 2525 .release = bpf_tracing_link_release, 2526 .dealloc = bpf_tracing_link_dealloc, 2527 .show_fdinfo = bpf_tracing_link_show_fdinfo, 2528 .fill_link_info = bpf_tracing_link_fill_link_info, 2529 }; 2530 2531 static int bpf_tracing_prog_attach(struct bpf_prog *prog) 2532 { 2533 struct bpf_link_primer link_primer; 2534 struct bpf_tracing_link *link; 2535 int err; 2536 2537 switch (prog->type) { 2538 case BPF_PROG_TYPE_TRACING: 2539 if (prog->expected_attach_type != BPF_TRACE_FENTRY && 2540 prog->expected_attach_type != BPF_TRACE_FEXIT && 2541 prog->expected_attach_type != BPF_MODIFY_RETURN) { 2542 err = -EINVAL; 2543 goto out_put_prog; 2544 } 2545 break; 2546 case BPF_PROG_TYPE_EXT: 2547 if (prog->expected_attach_type != 0) { 2548 err = -EINVAL; 2549 goto out_put_prog; 2550 } 2551 break; 2552 case BPF_PROG_TYPE_LSM: 2553 if (prog->expected_attach_type != BPF_LSM_MAC) { 2554 err = -EINVAL; 2555 goto out_put_prog; 2556 } 2557 break; 2558 default: 2559 err = -EINVAL; 2560 goto out_put_prog; 2561 } 2562 2563 link = kzalloc(sizeof(*link), GFP_USER); 2564 if (!link) { 2565 err = -ENOMEM; 2566 goto out_put_prog; 2567 } 2568 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING, 2569 &bpf_tracing_link_lops, prog); 2570 link->attach_type = prog->expected_attach_type; 2571 2572 err = bpf_link_prime(&link->link, &link_primer); 2573 if (err) { 2574 kfree(link); 2575 goto out_put_prog; 2576 } 2577 2578 err = bpf_trampoline_link_prog(prog); 2579 if (err) { 2580 bpf_link_cleanup(&link_primer); 2581 goto out_put_prog; 2582 } 2583 2584 return bpf_link_settle(&link_primer); 2585 out_put_prog: 2586 bpf_prog_put(prog); 2587 return err; 2588 } 2589 2590 struct bpf_raw_tp_link { 2591 struct bpf_link link; 2592 struct bpf_raw_event_map *btp; 2593 }; 2594 2595 static void bpf_raw_tp_link_release(struct bpf_link *link) 2596 { 2597 struct bpf_raw_tp_link *raw_tp = 2598 container_of(link, struct bpf_raw_tp_link, link); 2599 2600 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog); 2601 bpf_put_raw_tracepoint(raw_tp->btp); 2602 } 2603 2604 static void bpf_raw_tp_link_dealloc(struct bpf_link *link) 2605 { 2606 struct bpf_raw_tp_link *raw_tp = 2607 container_of(link, struct bpf_raw_tp_link, link); 2608 2609 kfree(raw_tp); 2610 } 2611 2612 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link, 2613 struct seq_file *seq) 2614 { 2615 struct bpf_raw_tp_link *raw_tp_link = 2616 container_of(link, struct bpf_raw_tp_link, link); 2617 2618 seq_printf(seq, 2619 "tp_name:\t%s\n", 2620 raw_tp_link->btp->tp->name); 2621 } 2622 2623 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, 2624 struct bpf_link_info *info) 2625 { 2626 struct bpf_raw_tp_link *raw_tp_link = 2627 container_of(link, struct bpf_raw_tp_link, link); 2628 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name); 2629 const char *tp_name = raw_tp_link->btp->tp->name; 2630 u32 ulen = info->raw_tracepoint.tp_name_len; 2631 size_t tp_len = strlen(tp_name); 2632 2633 if (ulen && !ubuf) 2634 return -EINVAL; 2635 2636 info->raw_tracepoint.tp_name_len = tp_len + 1; 2637 2638 if (!ubuf) 2639 return 0; 2640 2641 if (ulen >= tp_len + 1) { 2642 if (copy_to_user(ubuf, tp_name, tp_len + 1)) 2643 return -EFAULT; 2644 } else { 2645 char zero = '\0'; 2646 2647 if (copy_to_user(ubuf, tp_name, ulen - 1)) 2648 return -EFAULT; 2649 if (put_user(zero, ubuf + ulen - 1)) 2650 return -EFAULT; 2651 return -ENOSPC; 2652 } 2653 2654 return 0; 2655 } 2656 2657 static const struct bpf_link_ops bpf_raw_tp_link_lops = { 2658 .release = bpf_raw_tp_link_release, 2659 .dealloc = bpf_raw_tp_link_dealloc, 2660 .show_fdinfo = bpf_raw_tp_link_show_fdinfo, 2661 .fill_link_info = bpf_raw_tp_link_fill_link_info, 2662 }; 2663 2664 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd 2665 2666 static int bpf_raw_tracepoint_open(const union bpf_attr *attr) 2667 { 2668 struct bpf_link_primer link_primer; 2669 struct bpf_raw_tp_link *link; 2670 struct bpf_raw_event_map *btp; 2671 struct bpf_prog *prog; 2672 const char *tp_name; 2673 char buf[128]; 2674 int err; 2675 2676 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN)) 2677 return -EINVAL; 2678 2679 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); 2680 if (IS_ERR(prog)) 2681 return PTR_ERR(prog); 2682 2683 switch (prog->type) { 2684 case BPF_PROG_TYPE_TRACING: 2685 case BPF_PROG_TYPE_EXT: 2686 case BPF_PROG_TYPE_LSM: 2687 if (attr->raw_tracepoint.name) { 2688 /* The attach point for this category of programs 2689 * should be specified via btf_id during program load. 2690 */ 2691 err = -EINVAL; 2692 goto out_put_prog; 2693 } 2694 if (prog->type == BPF_PROG_TYPE_TRACING && 2695 prog->expected_attach_type == BPF_TRACE_RAW_TP) { 2696 tp_name = prog->aux->attach_func_name; 2697 break; 2698 } 2699 return bpf_tracing_prog_attach(prog); 2700 case BPF_PROG_TYPE_RAW_TRACEPOINT: 2701 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 2702 if (strncpy_from_user(buf, 2703 u64_to_user_ptr(attr->raw_tracepoint.name), 2704 sizeof(buf) - 1) < 0) { 2705 err = -EFAULT; 2706 goto out_put_prog; 2707 } 2708 buf[sizeof(buf) - 1] = 0; 2709 tp_name = buf; 2710 break; 2711 default: 2712 err = -EINVAL; 2713 goto out_put_prog; 2714 } 2715 2716 btp = bpf_get_raw_tracepoint(tp_name); 2717 if (!btp) { 2718 err = -ENOENT; 2719 goto out_put_prog; 2720 } 2721 2722 link = kzalloc(sizeof(*link), GFP_USER); 2723 if (!link) { 2724 err = -ENOMEM; 2725 goto out_put_btp; 2726 } 2727 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT, 2728 &bpf_raw_tp_link_lops, prog); 2729 link->btp = btp; 2730 2731 err = bpf_link_prime(&link->link, &link_primer); 2732 if (err) { 2733 kfree(link); 2734 goto out_put_btp; 2735 } 2736 2737 err = bpf_probe_register(link->btp, prog); 2738 if (err) { 2739 bpf_link_cleanup(&link_primer); 2740 goto out_put_btp; 2741 } 2742 2743 return bpf_link_settle(&link_primer); 2744 2745 out_put_btp: 2746 bpf_put_raw_tracepoint(btp); 2747 out_put_prog: 2748 bpf_prog_put(prog); 2749 return err; 2750 } 2751 2752 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, 2753 enum bpf_attach_type attach_type) 2754 { 2755 switch (prog->type) { 2756 case BPF_PROG_TYPE_CGROUP_SOCK: 2757 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2758 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2759 return attach_type == prog->expected_attach_type ? 0 : -EINVAL; 2760 case BPF_PROG_TYPE_CGROUP_SKB: 2761 if (!capable(CAP_NET_ADMIN)) 2762 /* cg-skb progs can be loaded by unpriv user. 2763 * check permissions at attach time. 2764 */ 2765 return -EPERM; 2766 return prog->enforce_expected_attach_type && 2767 prog->expected_attach_type != attach_type ? 2768 -EINVAL : 0; 2769 default: 2770 return 0; 2771 } 2772 } 2773 2774 static enum bpf_prog_type 2775 attach_type_to_prog_type(enum bpf_attach_type attach_type) 2776 { 2777 switch (attach_type) { 2778 case BPF_CGROUP_INET_INGRESS: 2779 case BPF_CGROUP_INET_EGRESS: 2780 return BPF_PROG_TYPE_CGROUP_SKB; 2781 break; 2782 case BPF_CGROUP_INET_SOCK_CREATE: 2783 case BPF_CGROUP_INET_SOCK_RELEASE: 2784 case BPF_CGROUP_INET4_POST_BIND: 2785 case BPF_CGROUP_INET6_POST_BIND: 2786 return BPF_PROG_TYPE_CGROUP_SOCK; 2787 case BPF_CGROUP_INET4_BIND: 2788 case BPF_CGROUP_INET6_BIND: 2789 case BPF_CGROUP_INET4_CONNECT: 2790 case BPF_CGROUP_INET6_CONNECT: 2791 case BPF_CGROUP_INET4_GETPEERNAME: 2792 case BPF_CGROUP_INET6_GETPEERNAME: 2793 case BPF_CGROUP_INET4_GETSOCKNAME: 2794 case BPF_CGROUP_INET6_GETSOCKNAME: 2795 case BPF_CGROUP_UDP4_SENDMSG: 2796 case BPF_CGROUP_UDP6_SENDMSG: 2797 case BPF_CGROUP_UDP4_RECVMSG: 2798 case BPF_CGROUP_UDP6_RECVMSG: 2799 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 2800 case BPF_CGROUP_SOCK_OPS: 2801 return BPF_PROG_TYPE_SOCK_OPS; 2802 case BPF_CGROUP_DEVICE: 2803 return BPF_PROG_TYPE_CGROUP_DEVICE; 2804 case BPF_SK_MSG_VERDICT: 2805 return BPF_PROG_TYPE_SK_MSG; 2806 case BPF_SK_SKB_STREAM_PARSER: 2807 case BPF_SK_SKB_STREAM_VERDICT: 2808 return BPF_PROG_TYPE_SK_SKB; 2809 case BPF_LIRC_MODE2: 2810 return BPF_PROG_TYPE_LIRC_MODE2; 2811 case BPF_FLOW_DISSECTOR: 2812 return BPF_PROG_TYPE_FLOW_DISSECTOR; 2813 case BPF_CGROUP_SYSCTL: 2814 return BPF_PROG_TYPE_CGROUP_SYSCTL; 2815 case BPF_CGROUP_GETSOCKOPT: 2816 case BPF_CGROUP_SETSOCKOPT: 2817 return BPF_PROG_TYPE_CGROUP_SOCKOPT; 2818 case BPF_TRACE_ITER: 2819 return BPF_PROG_TYPE_TRACING; 2820 default: 2821 return BPF_PROG_TYPE_UNSPEC; 2822 } 2823 } 2824 2825 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd 2826 2827 #define BPF_F_ATTACH_MASK \ 2828 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE) 2829 2830 static int bpf_prog_attach(const union bpf_attr *attr) 2831 { 2832 enum bpf_prog_type ptype; 2833 struct bpf_prog *prog; 2834 int ret; 2835 2836 if (CHECK_ATTR(BPF_PROG_ATTACH)) 2837 return -EINVAL; 2838 2839 if (attr->attach_flags & ~BPF_F_ATTACH_MASK) 2840 return -EINVAL; 2841 2842 ptype = attach_type_to_prog_type(attr->attach_type); 2843 if (ptype == BPF_PROG_TYPE_UNSPEC) 2844 return -EINVAL; 2845 2846 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 2847 if (IS_ERR(prog)) 2848 return PTR_ERR(prog); 2849 2850 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { 2851 bpf_prog_put(prog); 2852 return -EINVAL; 2853 } 2854 2855 switch (ptype) { 2856 case BPF_PROG_TYPE_SK_SKB: 2857 case BPF_PROG_TYPE_SK_MSG: 2858 ret = sock_map_get_from_fd(attr, prog); 2859 break; 2860 case BPF_PROG_TYPE_LIRC_MODE2: 2861 ret = lirc_prog_attach(attr, prog); 2862 break; 2863 case BPF_PROG_TYPE_FLOW_DISSECTOR: 2864 ret = netns_bpf_prog_attach(attr, prog); 2865 break; 2866 case BPF_PROG_TYPE_CGROUP_DEVICE: 2867 case BPF_PROG_TYPE_CGROUP_SKB: 2868 case BPF_PROG_TYPE_CGROUP_SOCK: 2869 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2870 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2871 case BPF_PROG_TYPE_CGROUP_SYSCTL: 2872 case BPF_PROG_TYPE_SOCK_OPS: 2873 ret = cgroup_bpf_prog_attach(attr, ptype, prog); 2874 break; 2875 default: 2876 ret = -EINVAL; 2877 } 2878 2879 if (ret) 2880 bpf_prog_put(prog); 2881 return ret; 2882 } 2883 2884 #define BPF_PROG_DETACH_LAST_FIELD attach_type 2885 2886 static int bpf_prog_detach(const union bpf_attr *attr) 2887 { 2888 enum bpf_prog_type ptype; 2889 2890 if (CHECK_ATTR(BPF_PROG_DETACH)) 2891 return -EINVAL; 2892 2893 ptype = attach_type_to_prog_type(attr->attach_type); 2894 2895 switch (ptype) { 2896 case BPF_PROG_TYPE_SK_MSG: 2897 case BPF_PROG_TYPE_SK_SKB: 2898 return sock_map_prog_detach(attr, ptype); 2899 case BPF_PROG_TYPE_LIRC_MODE2: 2900 return lirc_prog_detach(attr); 2901 case BPF_PROG_TYPE_FLOW_DISSECTOR: 2902 return netns_bpf_prog_detach(attr, ptype); 2903 case BPF_PROG_TYPE_CGROUP_DEVICE: 2904 case BPF_PROG_TYPE_CGROUP_SKB: 2905 case BPF_PROG_TYPE_CGROUP_SOCK: 2906 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2907 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2908 case BPF_PROG_TYPE_CGROUP_SYSCTL: 2909 case BPF_PROG_TYPE_SOCK_OPS: 2910 return cgroup_bpf_prog_detach(attr, ptype); 2911 default: 2912 return -EINVAL; 2913 } 2914 } 2915 2916 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt 2917 2918 static int bpf_prog_query(const union bpf_attr *attr, 2919 union bpf_attr __user *uattr) 2920 { 2921 if (!capable(CAP_NET_ADMIN)) 2922 return -EPERM; 2923 if (CHECK_ATTR(BPF_PROG_QUERY)) 2924 return -EINVAL; 2925 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) 2926 return -EINVAL; 2927 2928 switch (attr->query.attach_type) { 2929 case BPF_CGROUP_INET_INGRESS: 2930 case BPF_CGROUP_INET_EGRESS: 2931 case BPF_CGROUP_INET_SOCK_CREATE: 2932 case BPF_CGROUP_INET_SOCK_RELEASE: 2933 case BPF_CGROUP_INET4_BIND: 2934 case BPF_CGROUP_INET6_BIND: 2935 case BPF_CGROUP_INET4_POST_BIND: 2936 case BPF_CGROUP_INET6_POST_BIND: 2937 case BPF_CGROUP_INET4_CONNECT: 2938 case BPF_CGROUP_INET6_CONNECT: 2939 case BPF_CGROUP_INET4_GETPEERNAME: 2940 case BPF_CGROUP_INET6_GETPEERNAME: 2941 case BPF_CGROUP_INET4_GETSOCKNAME: 2942 case BPF_CGROUP_INET6_GETSOCKNAME: 2943 case BPF_CGROUP_UDP4_SENDMSG: 2944 case BPF_CGROUP_UDP6_SENDMSG: 2945 case BPF_CGROUP_UDP4_RECVMSG: 2946 case BPF_CGROUP_UDP6_RECVMSG: 2947 case BPF_CGROUP_SOCK_OPS: 2948 case BPF_CGROUP_DEVICE: 2949 case BPF_CGROUP_SYSCTL: 2950 case BPF_CGROUP_GETSOCKOPT: 2951 case BPF_CGROUP_SETSOCKOPT: 2952 return cgroup_bpf_prog_query(attr, uattr); 2953 case BPF_LIRC_MODE2: 2954 return lirc_prog_query(attr, uattr); 2955 case BPF_FLOW_DISSECTOR: 2956 return netns_bpf_prog_query(attr, uattr); 2957 default: 2958 return -EINVAL; 2959 } 2960 } 2961 2962 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out 2963 2964 static int bpf_prog_test_run(const union bpf_attr *attr, 2965 union bpf_attr __user *uattr) 2966 { 2967 struct bpf_prog *prog; 2968 int ret = -ENOTSUPP; 2969 2970 if (CHECK_ATTR(BPF_PROG_TEST_RUN)) 2971 return -EINVAL; 2972 2973 if ((attr->test.ctx_size_in && !attr->test.ctx_in) || 2974 (!attr->test.ctx_size_in && attr->test.ctx_in)) 2975 return -EINVAL; 2976 2977 if ((attr->test.ctx_size_out && !attr->test.ctx_out) || 2978 (!attr->test.ctx_size_out && attr->test.ctx_out)) 2979 return -EINVAL; 2980 2981 prog = bpf_prog_get(attr->test.prog_fd); 2982 if (IS_ERR(prog)) 2983 return PTR_ERR(prog); 2984 2985 if (prog->aux->ops->test_run) 2986 ret = prog->aux->ops->test_run(prog, attr, uattr); 2987 2988 bpf_prog_put(prog); 2989 return ret; 2990 } 2991 2992 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id 2993 2994 static int bpf_obj_get_next_id(const union bpf_attr *attr, 2995 union bpf_attr __user *uattr, 2996 struct idr *idr, 2997 spinlock_t *lock) 2998 { 2999 u32 next_id = attr->start_id; 3000 int err = 0; 3001 3002 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) 3003 return -EINVAL; 3004 3005 if (!capable(CAP_SYS_ADMIN)) 3006 return -EPERM; 3007 3008 next_id++; 3009 spin_lock_bh(lock); 3010 if (!idr_get_next(idr, &next_id)) 3011 err = -ENOENT; 3012 spin_unlock_bh(lock); 3013 3014 if (!err) 3015 err = put_user(next_id, &uattr->next_id); 3016 3017 return err; 3018 } 3019 3020 struct bpf_map *bpf_map_get_curr_or_next(u32 *id) 3021 { 3022 struct bpf_map *map; 3023 3024 spin_lock_bh(&map_idr_lock); 3025 again: 3026 map = idr_get_next(&map_idr, id); 3027 if (map) { 3028 map = __bpf_map_inc_not_zero(map, false); 3029 if (IS_ERR(map)) { 3030 (*id)++; 3031 goto again; 3032 } 3033 } 3034 spin_unlock_bh(&map_idr_lock); 3035 3036 return map; 3037 } 3038 3039 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id 3040 3041 struct bpf_prog *bpf_prog_by_id(u32 id) 3042 { 3043 struct bpf_prog *prog; 3044 3045 if (!id) 3046 return ERR_PTR(-ENOENT); 3047 3048 spin_lock_bh(&prog_idr_lock); 3049 prog = idr_find(&prog_idr, id); 3050 if (prog) 3051 prog = bpf_prog_inc_not_zero(prog); 3052 else 3053 prog = ERR_PTR(-ENOENT); 3054 spin_unlock_bh(&prog_idr_lock); 3055 return prog; 3056 } 3057 3058 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) 3059 { 3060 struct bpf_prog *prog; 3061 u32 id = attr->prog_id; 3062 int fd; 3063 3064 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) 3065 return -EINVAL; 3066 3067 if (!capable(CAP_SYS_ADMIN)) 3068 return -EPERM; 3069 3070 prog = bpf_prog_by_id(id); 3071 if (IS_ERR(prog)) 3072 return PTR_ERR(prog); 3073 3074 fd = bpf_prog_new_fd(prog); 3075 if (fd < 0) 3076 bpf_prog_put(prog); 3077 3078 return fd; 3079 } 3080 3081 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags 3082 3083 static int bpf_map_get_fd_by_id(const union bpf_attr *attr) 3084 { 3085 struct bpf_map *map; 3086 u32 id = attr->map_id; 3087 int f_flags; 3088 int fd; 3089 3090 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || 3091 attr->open_flags & ~BPF_OBJ_FLAG_MASK) 3092 return -EINVAL; 3093 3094 if (!capable(CAP_SYS_ADMIN)) 3095 return -EPERM; 3096 3097 f_flags = bpf_get_file_flag(attr->open_flags); 3098 if (f_flags < 0) 3099 return f_flags; 3100 3101 spin_lock_bh(&map_idr_lock); 3102 map = idr_find(&map_idr, id); 3103 if (map) 3104 map = __bpf_map_inc_not_zero(map, true); 3105 else 3106 map = ERR_PTR(-ENOENT); 3107 spin_unlock_bh(&map_idr_lock); 3108 3109 if (IS_ERR(map)) 3110 return PTR_ERR(map); 3111 3112 fd = bpf_map_new_fd(map, f_flags); 3113 if (fd < 0) 3114 bpf_map_put_with_uref(map); 3115 3116 return fd; 3117 } 3118 3119 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, 3120 unsigned long addr, u32 *off, 3121 u32 *type) 3122 { 3123 const struct bpf_map *map; 3124 int i; 3125 3126 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { 3127 map = prog->aux->used_maps[i]; 3128 if (map == (void *)addr) { 3129 *type = BPF_PSEUDO_MAP_FD; 3130 return map; 3131 } 3132 if (!map->ops->map_direct_value_meta) 3133 continue; 3134 if (!map->ops->map_direct_value_meta(map, addr, off)) { 3135 *type = BPF_PSEUDO_MAP_VALUE; 3136 return map; 3137 } 3138 } 3139 3140 return NULL; 3141 } 3142 3143 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog, 3144 const struct cred *f_cred) 3145 { 3146 const struct bpf_map *map; 3147 struct bpf_insn *insns; 3148 u32 off, type; 3149 u64 imm; 3150 u8 code; 3151 int i; 3152 3153 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), 3154 GFP_USER); 3155 if (!insns) 3156 return insns; 3157 3158 for (i = 0; i < prog->len; i++) { 3159 code = insns[i].code; 3160 3161 if (code == (BPF_JMP | BPF_TAIL_CALL)) { 3162 insns[i].code = BPF_JMP | BPF_CALL; 3163 insns[i].imm = BPF_FUNC_tail_call; 3164 /* fall-through */ 3165 } 3166 if (code == (BPF_JMP | BPF_CALL) || 3167 code == (BPF_JMP | BPF_CALL_ARGS)) { 3168 if (code == (BPF_JMP | BPF_CALL_ARGS)) 3169 insns[i].code = BPF_JMP | BPF_CALL; 3170 if (!bpf_dump_raw_ok(f_cred)) 3171 insns[i].imm = 0; 3172 continue; 3173 } 3174 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) { 3175 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM; 3176 continue; 3177 } 3178 3179 if (code != (BPF_LD | BPF_IMM | BPF_DW)) 3180 continue; 3181 3182 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; 3183 map = bpf_map_from_imm(prog, imm, &off, &type); 3184 if (map) { 3185 insns[i].src_reg = type; 3186 insns[i].imm = map->id; 3187 insns[i + 1].imm = off; 3188 continue; 3189 } 3190 } 3191 3192 return insns; 3193 } 3194 3195 static int set_info_rec_size(struct bpf_prog_info *info) 3196 { 3197 /* 3198 * Ensure info.*_rec_size is the same as kernel expected size 3199 * 3200 * or 3201 * 3202 * Only allow zero *_rec_size if both _rec_size and _cnt are 3203 * zero. In this case, the kernel will set the expected 3204 * _rec_size back to the info. 3205 */ 3206 3207 if ((info->nr_func_info || info->func_info_rec_size) && 3208 info->func_info_rec_size != sizeof(struct bpf_func_info)) 3209 return -EINVAL; 3210 3211 if ((info->nr_line_info || info->line_info_rec_size) && 3212 info->line_info_rec_size != sizeof(struct bpf_line_info)) 3213 return -EINVAL; 3214 3215 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && 3216 info->jited_line_info_rec_size != sizeof(__u64)) 3217 return -EINVAL; 3218 3219 info->func_info_rec_size = sizeof(struct bpf_func_info); 3220 info->line_info_rec_size = sizeof(struct bpf_line_info); 3221 info->jited_line_info_rec_size = sizeof(__u64); 3222 3223 return 0; 3224 } 3225 3226 static int bpf_prog_get_info_by_fd(struct file *file, 3227 struct bpf_prog *prog, 3228 const union bpf_attr *attr, 3229 union bpf_attr __user *uattr) 3230 { 3231 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); 3232 struct bpf_prog_info info; 3233 u32 info_len = attr->info.info_len; 3234 struct bpf_prog_stats stats; 3235 char __user *uinsns; 3236 u32 ulen; 3237 int err; 3238 3239 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); 3240 if (err) 3241 return err; 3242 info_len = min_t(u32, sizeof(info), info_len); 3243 3244 memset(&info, 0, sizeof(info)); 3245 if (copy_from_user(&info, uinfo, info_len)) 3246 return -EFAULT; 3247 3248 info.type = prog->type; 3249 info.id = prog->aux->id; 3250 info.load_time = prog->aux->load_time; 3251 info.created_by_uid = from_kuid_munged(current_user_ns(), 3252 prog->aux->user->uid); 3253 info.gpl_compatible = prog->gpl_compatible; 3254 3255 memcpy(info.tag, prog->tag, sizeof(prog->tag)); 3256 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); 3257 3258 ulen = info.nr_map_ids; 3259 info.nr_map_ids = prog->aux->used_map_cnt; 3260 ulen = min_t(u32, info.nr_map_ids, ulen); 3261 if (ulen) { 3262 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); 3263 u32 i; 3264 3265 for (i = 0; i < ulen; i++) 3266 if (put_user(prog->aux->used_maps[i]->id, 3267 &user_map_ids[i])) 3268 return -EFAULT; 3269 } 3270 3271 err = set_info_rec_size(&info); 3272 if (err) 3273 return err; 3274 3275 bpf_prog_get_stats(prog, &stats); 3276 info.run_time_ns = stats.nsecs; 3277 info.run_cnt = stats.cnt; 3278 3279 if (!bpf_capable()) { 3280 info.jited_prog_len = 0; 3281 info.xlated_prog_len = 0; 3282 info.nr_jited_ksyms = 0; 3283 info.nr_jited_func_lens = 0; 3284 info.nr_func_info = 0; 3285 info.nr_line_info = 0; 3286 info.nr_jited_line_info = 0; 3287 goto done; 3288 } 3289 3290 ulen = info.xlated_prog_len; 3291 info.xlated_prog_len = bpf_prog_insn_size(prog); 3292 if (info.xlated_prog_len && ulen) { 3293 struct bpf_insn *insns_sanitized; 3294 bool fault; 3295 3296 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) { 3297 info.xlated_prog_insns = 0; 3298 goto done; 3299 } 3300 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred); 3301 if (!insns_sanitized) 3302 return -ENOMEM; 3303 uinsns = u64_to_user_ptr(info.xlated_prog_insns); 3304 ulen = min_t(u32, info.xlated_prog_len, ulen); 3305 fault = copy_to_user(uinsns, insns_sanitized, ulen); 3306 kfree(insns_sanitized); 3307 if (fault) 3308 return -EFAULT; 3309 } 3310 3311 if (bpf_prog_is_dev_bound(prog->aux)) { 3312 err = bpf_prog_offload_info_fill(&info, prog); 3313 if (err) 3314 return err; 3315 goto done; 3316 } 3317 3318 /* NOTE: the following code is supposed to be skipped for offload. 3319 * bpf_prog_offload_info_fill() is the place to fill similar fields 3320 * for offload. 3321 */ 3322 ulen = info.jited_prog_len; 3323 if (prog->aux->func_cnt) { 3324 u32 i; 3325 3326 info.jited_prog_len = 0; 3327 for (i = 0; i < prog->aux->func_cnt; i++) 3328 info.jited_prog_len += prog->aux->func[i]->jited_len; 3329 } else { 3330 info.jited_prog_len = prog->jited_len; 3331 } 3332 3333 if (info.jited_prog_len && ulen) { 3334 if (bpf_dump_raw_ok(file->f_cred)) { 3335 uinsns = u64_to_user_ptr(info.jited_prog_insns); 3336 ulen = min_t(u32, info.jited_prog_len, ulen); 3337 3338 /* for multi-function programs, copy the JITed 3339 * instructions for all the functions 3340 */ 3341 if (prog->aux->func_cnt) { 3342 u32 len, free, i; 3343 u8 *img; 3344 3345 free = ulen; 3346 for (i = 0; i < prog->aux->func_cnt; i++) { 3347 len = prog->aux->func[i]->jited_len; 3348 len = min_t(u32, len, free); 3349 img = (u8 *) prog->aux->func[i]->bpf_func; 3350 if (copy_to_user(uinsns, img, len)) 3351 return -EFAULT; 3352 uinsns += len; 3353 free -= len; 3354 if (!free) 3355 break; 3356 } 3357 } else { 3358 if (copy_to_user(uinsns, prog->bpf_func, ulen)) 3359 return -EFAULT; 3360 } 3361 } else { 3362 info.jited_prog_insns = 0; 3363 } 3364 } 3365 3366 ulen = info.nr_jited_ksyms; 3367 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; 3368 if (ulen) { 3369 if (bpf_dump_raw_ok(file->f_cred)) { 3370 unsigned long ksym_addr; 3371 u64 __user *user_ksyms; 3372 u32 i; 3373 3374 /* copy the address of the kernel symbol 3375 * corresponding to each function 3376 */ 3377 ulen = min_t(u32, info.nr_jited_ksyms, ulen); 3378 user_ksyms = u64_to_user_ptr(info.jited_ksyms); 3379 if (prog->aux->func_cnt) { 3380 for (i = 0; i < ulen; i++) { 3381 ksym_addr = (unsigned long) 3382 prog->aux->func[i]->bpf_func; 3383 if (put_user((u64) ksym_addr, 3384 &user_ksyms[i])) 3385 return -EFAULT; 3386 } 3387 } else { 3388 ksym_addr = (unsigned long) prog->bpf_func; 3389 if (put_user((u64) ksym_addr, &user_ksyms[0])) 3390 return -EFAULT; 3391 } 3392 } else { 3393 info.jited_ksyms = 0; 3394 } 3395 } 3396 3397 ulen = info.nr_jited_func_lens; 3398 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; 3399 if (ulen) { 3400 if (bpf_dump_raw_ok(file->f_cred)) { 3401 u32 __user *user_lens; 3402 u32 func_len, i; 3403 3404 /* copy the JITed image lengths for each function */ 3405 ulen = min_t(u32, info.nr_jited_func_lens, ulen); 3406 user_lens = u64_to_user_ptr(info.jited_func_lens); 3407 if (prog->aux->func_cnt) { 3408 for (i = 0; i < ulen; i++) { 3409 func_len = 3410 prog->aux->func[i]->jited_len; 3411 if (put_user(func_len, &user_lens[i])) 3412 return -EFAULT; 3413 } 3414 } else { 3415 func_len = prog->jited_len; 3416 if (put_user(func_len, &user_lens[0])) 3417 return -EFAULT; 3418 } 3419 } else { 3420 info.jited_func_lens = 0; 3421 } 3422 } 3423 3424 if (prog->aux->btf) 3425 info.btf_id = btf_id(prog->aux->btf); 3426 3427 ulen = info.nr_func_info; 3428 info.nr_func_info = prog->aux->func_info_cnt; 3429 if (info.nr_func_info && ulen) { 3430 char __user *user_finfo; 3431 3432 user_finfo = u64_to_user_ptr(info.func_info); 3433 ulen = min_t(u32, info.nr_func_info, ulen); 3434 if (copy_to_user(user_finfo, prog->aux->func_info, 3435 info.func_info_rec_size * ulen)) 3436 return -EFAULT; 3437 } 3438 3439 ulen = info.nr_line_info; 3440 info.nr_line_info = prog->aux->nr_linfo; 3441 if (info.nr_line_info && ulen) { 3442 __u8 __user *user_linfo; 3443 3444 user_linfo = u64_to_user_ptr(info.line_info); 3445 ulen = min_t(u32, info.nr_line_info, ulen); 3446 if (copy_to_user(user_linfo, prog->aux->linfo, 3447 info.line_info_rec_size * ulen)) 3448 return -EFAULT; 3449 } 3450 3451 ulen = info.nr_jited_line_info; 3452 if (prog->aux->jited_linfo) 3453 info.nr_jited_line_info = prog->aux->nr_linfo; 3454 else 3455 info.nr_jited_line_info = 0; 3456 if (info.nr_jited_line_info && ulen) { 3457 if (bpf_dump_raw_ok(file->f_cred)) { 3458 __u64 __user *user_linfo; 3459 u32 i; 3460 3461 user_linfo = u64_to_user_ptr(info.jited_line_info); 3462 ulen = min_t(u32, info.nr_jited_line_info, ulen); 3463 for (i = 0; i < ulen; i++) { 3464 if (put_user((__u64)(long)prog->aux->jited_linfo[i], 3465 &user_linfo[i])) 3466 return -EFAULT; 3467 } 3468 } else { 3469 info.jited_line_info = 0; 3470 } 3471 } 3472 3473 ulen = info.nr_prog_tags; 3474 info.nr_prog_tags = prog->aux->func_cnt ? : 1; 3475 if (ulen) { 3476 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; 3477 u32 i; 3478 3479 user_prog_tags = u64_to_user_ptr(info.prog_tags); 3480 ulen = min_t(u32, info.nr_prog_tags, ulen); 3481 if (prog->aux->func_cnt) { 3482 for (i = 0; i < ulen; i++) { 3483 if (copy_to_user(user_prog_tags[i], 3484 prog->aux->func[i]->tag, 3485 BPF_TAG_SIZE)) 3486 return -EFAULT; 3487 } 3488 } else { 3489 if (copy_to_user(user_prog_tags[0], 3490 prog->tag, BPF_TAG_SIZE)) 3491 return -EFAULT; 3492 } 3493 } 3494 3495 done: 3496 if (copy_to_user(uinfo, &info, info_len) || 3497 put_user(info_len, &uattr->info.info_len)) 3498 return -EFAULT; 3499 3500 return 0; 3501 } 3502 3503 static int bpf_map_get_info_by_fd(struct file *file, 3504 struct bpf_map *map, 3505 const union bpf_attr *attr, 3506 union bpf_attr __user *uattr) 3507 { 3508 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); 3509 struct bpf_map_info info; 3510 u32 info_len = attr->info.info_len; 3511 int err; 3512 3513 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); 3514 if (err) 3515 return err; 3516 info_len = min_t(u32, sizeof(info), info_len); 3517 3518 memset(&info, 0, sizeof(info)); 3519 info.type = map->map_type; 3520 info.id = map->id; 3521 info.key_size = map->key_size; 3522 info.value_size = map->value_size; 3523 info.max_entries = map->max_entries; 3524 info.map_flags = map->map_flags; 3525 memcpy(info.name, map->name, sizeof(map->name)); 3526 3527 if (map->btf) { 3528 info.btf_id = btf_id(map->btf); 3529 info.btf_key_type_id = map->btf_key_type_id; 3530 info.btf_value_type_id = map->btf_value_type_id; 3531 } 3532 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; 3533 3534 if (bpf_map_is_dev_bound(map)) { 3535 err = bpf_map_offload_info_fill(&info, map); 3536 if (err) 3537 return err; 3538 } 3539 3540 if (copy_to_user(uinfo, &info, info_len) || 3541 put_user(info_len, &uattr->info.info_len)) 3542 return -EFAULT; 3543 3544 return 0; 3545 } 3546 3547 static int bpf_btf_get_info_by_fd(struct file *file, 3548 struct btf *btf, 3549 const union bpf_attr *attr, 3550 union bpf_attr __user *uattr) 3551 { 3552 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); 3553 u32 info_len = attr->info.info_len; 3554 int err; 3555 3556 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len); 3557 if (err) 3558 return err; 3559 3560 return btf_get_info_by_fd(btf, attr, uattr); 3561 } 3562 3563 static int bpf_link_get_info_by_fd(struct file *file, 3564 struct bpf_link *link, 3565 const union bpf_attr *attr, 3566 union bpf_attr __user *uattr) 3567 { 3568 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info); 3569 struct bpf_link_info info; 3570 u32 info_len = attr->info.info_len; 3571 int err; 3572 3573 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); 3574 if (err) 3575 return err; 3576 info_len = min_t(u32, sizeof(info), info_len); 3577 3578 memset(&info, 0, sizeof(info)); 3579 if (copy_from_user(&info, uinfo, info_len)) 3580 return -EFAULT; 3581 3582 info.type = link->type; 3583 info.id = link->id; 3584 info.prog_id = link->prog->aux->id; 3585 3586 if (link->ops->fill_link_info) { 3587 err = link->ops->fill_link_info(link, &info); 3588 if (err) 3589 return err; 3590 } 3591 3592 if (copy_to_user(uinfo, &info, info_len) || 3593 put_user(info_len, &uattr->info.info_len)) 3594 return -EFAULT; 3595 3596 return 0; 3597 } 3598 3599 3600 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info 3601 3602 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, 3603 union bpf_attr __user *uattr) 3604 { 3605 int ufd = attr->info.bpf_fd; 3606 struct fd f; 3607 int err; 3608 3609 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) 3610 return -EINVAL; 3611 3612 f = fdget(ufd); 3613 if (!f.file) 3614 return -EBADFD; 3615 3616 if (f.file->f_op == &bpf_prog_fops) 3617 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr, 3618 uattr); 3619 else if (f.file->f_op == &bpf_map_fops) 3620 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr, 3621 uattr); 3622 else if (f.file->f_op == &btf_fops) 3623 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr); 3624 else if (f.file->f_op == &bpf_link_fops) 3625 err = bpf_link_get_info_by_fd(f.file, f.file->private_data, 3626 attr, uattr); 3627 else 3628 err = -EINVAL; 3629 3630 fdput(f); 3631 return err; 3632 } 3633 3634 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level 3635 3636 static int bpf_btf_load(const union bpf_attr *attr) 3637 { 3638 if (CHECK_ATTR(BPF_BTF_LOAD)) 3639 return -EINVAL; 3640 3641 if (!bpf_capable()) 3642 return -EPERM; 3643 3644 return btf_new_fd(attr); 3645 } 3646 3647 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id 3648 3649 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) 3650 { 3651 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) 3652 return -EINVAL; 3653 3654 if (!capable(CAP_SYS_ADMIN)) 3655 return -EPERM; 3656 3657 return btf_get_fd_by_id(attr->btf_id); 3658 } 3659 3660 static int bpf_task_fd_query_copy(const union bpf_attr *attr, 3661 union bpf_attr __user *uattr, 3662 u32 prog_id, u32 fd_type, 3663 const char *buf, u64 probe_offset, 3664 u64 probe_addr) 3665 { 3666 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); 3667 u32 len = buf ? strlen(buf) : 0, input_len; 3668 int err = 0; 3669 3670 if (put_user(len, &uattr->task_fd_query.buf_len)) 3671 return -EFAULT; 3672 input_len = attr->task_fd_query.buf_len; 3673 if (input_len && ubuf) { 3674 if (!len) { 3675 /* nothing to copy, just make ubuf NULL terminated */ 3676 char zero = '\0'; 3677 3678 if (put_user(zero, ubuf)) 3679 return -EFAULT; 3680 } else if (input_len >= len + 1) { 3681 /* ubuf can hold the string with NULL terminator */ 3682 if (copy_to_user(ubuf, buf, len + 1)) 3683 return -EFAULT; 3684 } else { 3685 /* ubuf cannot hold the string with NULL terminator, 3686 * do a partial copy with NULL terminator. 3687 */ 3688 char zero = '\0'; 3689 3690 err = -ENOSPC; 3691 if (copy_to_user(ubuf, buf, input_len - 1)) 3692 return -EFAULT; 3693 if (put_user(zero, ubuf + input_len - 1)) 3694 return -EFAULT; 3695 } 3696 } 3697 3698 if (put_user(prog_id, &uattr->task_fd_query.prog_id) || 3699 put_user(fd_type, &uattr->task_fd_query.fd_type) || 3700 put_user(probe_offset, &uattr->task_fd_query.probe_offset) || 3701 put_user(probe_addr, &uattr->task_fd_query.probe_addr)) 3702 return -EFAULT; 3703 3704 return err; 3705 } 3706 3707 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr 3708 3709 static int bpf_task_fd_query(const union bpf_attr *attr, 3710 union bpf_attr __user *uattr) 3711 { 3712 pid_t pid = attr->task_fd_query.pid; 3713 u32 fd = attr->task_fd_query.fd; 3714 const struct perf_event *event; 3715 struct files_struct *files; 3716 struct task_struct *task; 3717 struct file *file; 3718 int err; 3719 3720 if (CHECK_ATTR(BPF_TASK_FD_QUERY)) 3721 return -EINVAL; 3722 3723 if (!capable(CAP_SYS_ADMIN)) 3724 return -EPERM; 3725 3726 if (attr->task_fd_query.flags != 0) 3727 return -EINVAL; 3728 3729 task = get_pid_task(find_vpid(pid), PIDTYPE_PID); 3730 if (!task) 3731 return -ENOENT; 3732 3733 files = get_files_struct(task); 3734 put_task_struct(task); 3735 if (!files) 3736 return -ENOENT; 3737 3738 err = 0; 3739 spin_lock(&files->file_lock); 3740 file = fcheck_files(files, fd); 3741 if (!file) 3742 err = -EBADF; 3743 else 3744 get_file(file); 3745 spin_unlock(&files->file_lock); 3746 put_files_struct(files); 3747 3748 if (err) 3749 goto out; 3750 3751 if (file->f_op == &bpf_link_fops) { 3752 struct bpf_link *link = file->private_data; 3753 3754 if (link->ops == &bpf_raw_tp_link_lops) { 3755 struct bpf_raw_tp_link *raw_tp = 3756 container_of(link, struct bpf_raw_tp_link, link); 3757 struct bpf_raw_event_map *btp = raw_tp->btp; 3758 3759 err = bpf_task_fd_query_copy(attr, uattr, 3760 raw_tp->link.prog->aux->id, 3761 BPF_FD_TYPE_RAW_TRACEPOINT, 3762 btp->tp->name, 0, 0); 3763 goto put_file; 3764 } 3765 goto out_not_supp; 3766 } 3767 3768 event = perf_get_event(file); 3769 if (!IS_ERR(event)) { 3770 u64 probe_offset, probe_addr; 3771 u32 prog_id, fd_type; 3772 const char *buf; 3773 3774 err = bpf_get_perf_event_info(event, &prog_id, &fd_type, 3775 &buf, &probe_offset, 3776 &probe_addr); 3777 if (!err) 3778 err = bpf_task_fd_query_copy(attr, uattr, prog_id, 3779 fd_type, buf, 3780 probe_offset, 3781 probe_addr); 3782 goto put_file; 3783 } 3784 3785 out_not_supp: 3786 err = -ENOTSUPP; 3787 put_file: 3788 fput(file); 3789 out: 3790 return err; 3791 } 3792 3793 #define BPF_MAP_BATCH_LAST_FIELD batch.flags 3794 3795 #define BPF_DO_BATCH(fn) \ 3796 do { \ 3797 if (!fn) { \ 3798 err = -ENOTSUPP; \ 3799 goto err_put; \ 3800 } \ 3801 err = fn(map, attr, uattr); \ 3802 } while (0) 3803 3804 static int bpf_map_do_batch(const union bpf_attr *attr, 3805 union bpf_attr __user *uattr, 3806 int cmd) 3807 { 3808 struct bpf_map *map; 3809 int err, ufd; 3810 struct fd f; 3811 3812 if (CHECK_ATTR(BPF_MAP_BATCH)) 3813 return -EINVAL; 3814 3815 ufd = attr->batch.map_fd; 3816 f = fdget(ufd); 3817 map = __bpf_map_get(f); 3818 if (IS_ERR(map)) 3819 return PTR_ERR(map); 3820 3821 if ((cmd == BPF_MAP_LOOKUP_BATCH || 3822 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) && 3823 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { 3824 err = -EPERM; 3825 goto err_put; 3826 } 3827 3828 if (cmd != BPF_MAP_LOOKUP_BATCH && 3829 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 3830 err = -EPERM; 3831 goto err_put; 3832 } 3833 3834 if (cmd == BPF_MAP_LOOKUP_BATCH) 3835 BPF_DO_BATCH(map->ops->map_lookup_batch); 3836 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) 3837 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch); 3838 else if (cmd == BPF_MAP_UPDATE_BATCH) 3839 BPF_DO_BATCH(map->ops->map_update_batch); 3840 else 3841 BPF_DO_BATCH(map->ops->map_delete_batch); 3842 3843 err_put: 3844 fdput(f); 3845 return err; 3846 } 3847 3848 static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 3849 { 3850 if (attr->link_create.attach_type == BPF_TRACE_ITER && 3851 prog->expected_attach_type == BPF_TRACE_ITER) 3852 return bpf_iter_link_attach(attr, prog); 3853 3854 return -EINVAL; 3855 } 3856 3857 #define BPF_LINK_CREATE_LAST_FIELD link_create.flags 3858 static int link_create(union bpf_attr *attr) 3859 { 3860 enum bpf_prog_type ptype; 3861 struct bpf_prog *prog; 3862 int ret; 3863 3864 if (CHECK_ATTR(BPF_LINK_CREATE)) 3865 return -EINVAL; 3866 3867 ptype = attach_type_to_prog_type(attr->link_create.attach_type); 3868 if (ptype == BPF_PROG_TYPE_UNSPEC) 3869 return -EINVAL; 3870 3871 prog = bpf_prog_get_type(attr->link_create.prog_fd, ptype); 3872 if (IS_ERR(prog)) 3873 return PTR_ERR(prog); 3874 3875 ret = bpf_prog_attach_check_attach_type(prog, 3876 attr->link_create.attach_type); 3877 if (ret) 3878 goto err_out; 3879 3880 switch (ptype) { 3881 case BPF_PROG_TYPE_CGROUP_SKB: 3882 case BPF_PROG_TYPE_CGROUP_SOCK: 3883 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 3884 case BPF_PROG_TYPE_SOCK_OPS: 3885 case BPF_PROG_TYPE_CGROUP_DEVICE: 3886 case BPF_PROG_TYPE_CGROUP_SYSCTL: 3887 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 3888 ret = cgroup_bpf_link_attach(attr, prog); 3889 break; 3890 case BPF_PROG_TYPE_TRACING: 3891 ret = tracing_bpf_link_attach(attr, prog); 3892 break; 3893 case BPF_PROG_TYPE_FLOW_DISSECTOR: 3894 ret = netns_bpf_link_create(attr, prog); 3895 break; 3896 default: 3897 ret = -EINVAL; 3898 } 3899 3900 err_out: 3901 if (ret < 0) 3902 bpf_prog_put(prog); 3903 return ret; 3904 } 3905 3906 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd 3907 3908 static int link_update(union bpf_attr *attr) 3909 { 3910 struct bpf_prog *old_prog = NULL, *new_prog; 3911 struct bpf_link *link; 3912 u32 flags; 3913 int ret; 3914 3915 if (CHECK_ATTR(BPF_LINK_UPDATE)) 3916 return -EINVAL; 3917 3918 flags = attr->link_update.flags; 3919 if (flags & ~BPF_F_REPLACE) 3920 return -EINVAL; 3921 3922 link = bpf_link_get_from_fd(attr->link_update.link_fd); 3923 if (IS_ERR(link)) 3924 return PTR_ERR(link); 3925 3926 new_prog = bpf_prog_get(attr->link_update.new_prog_fd); 3927 if (IS_ERR(new_prog)) { 3928 ret = PTR_ERR(new_prog); 3929 goto out_put_link; 3930 } 3931 3932 if (flags & BPF_F_REPLACE) { 3933 old_prog = bpf_prog_get(attr->link_update.old_prog_fd); 3934 if (IS_ERR(old_prog)) { 3935 ret = PTR_ERR(old_prog); 3936 old_prog = NULL; 3937 goto out_put_progs; 3938 } 3939 } else if (attr->link_update.old_prog_fd) { 3940 ret = -EINVAL; 3941 goto out_put_progs; 3942 } 3943 3944 if (link->ops->update_prog) 3945 ret = link->ops->update_prog(link, new_prog, old_prog); 3946 else 3947 ret = -EINVAL; 3948 3949 out_put_progs: 3950 if (old_prog) 3951 bpf_prog_put(old_prog); 3952 if (ret) 3953 bpf_prog_put(new_prog); 3954 out_put_link: 3955 bpf_link_put(link); 3956 return ret; 3957 } 3958 3959 static int bpf_link_inc_not_zero(struct bpf_link *link) 3960 { 3961 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? 0 : -ENOENT; 3962 } 3963 3964 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id 3965 3966 static int bpf_link_get_fd_by_id(const union bpf_attr *attr) 3967 { 3968 struct bpf_link *link; 3969 u32 id = attr->link_id; 3970 int fd, err; 3971 3972 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID)) 3973 return -EINVAL; 3974 3975 if (!capable(CAP_SYS_ADMIN)) 3976 return -EPERM; 3977 3978 spin_lock_bh(&link_idr_lock); 3979 link = idr_find(&link_idr, id); 3980 /* before link is "settled", ID is 0, pretend it doesn't exist yet */ 3981 if (link) { 3982 if (link->id) 3983 err = bpf_link_inc_not_zero(link); 3984 else 3985 err = -EAGAIN; 3986 } else { 3987 err = -ENOENT; 3988 } 3989 spin_unlock_bh(&link_idr_lock); 3990 3991 if (err) 3992 return err; 3993 3994 fd = bpf_link_new_fd(link); 3995 if (fd < 0) 3996 bpf_link_put(link); 3997 3998 return fd; 3999 } 4000 4001 DEFINE_MUTEX(bpf_stats_enabled_mutex); 4002 4003 static int bpf_stats_release(struct inode *inode, struct file *file) 4004 { 4005 mutex_lock(&bpf_stats_enabled_mutex); 4006 static_key_slow_dec(&bpf_stats_enabled_key.key); 4007 mutex_unlock(&bpf_stats_enabled_mutex); 4008 return 0; 4009 } 4010 4011 static const struct file_operations bpf_stats_fops = { 4012 .release = bpf_stats_release, 4013 }; 4014 4015 static int bpf_enable_runtime_stats(void) 4016 { 4017 int fd; 4018 4019 mutex_lock(&bpf_stats_enabled_mutex); 4020 4021 /* Set a very high limit to avoid overflow */ 4022 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) { 4023 mutex_unlock(&bpf_stats_enabled_mutex); 4024 return -EBUSY; 4025 } 4026 4027 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC); 4028 if (fd >= 0) 4029 static_key_slow_inc(&bpf_stats_enabled_key.key); 4030 4031 mutex_unlock(&bpf_stats_enabled_mutex); 4032 return fd; 4033 } 4034 4035 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type 4036 4037 static int bpf_enable_stats(union bpf_attr *attr) 4038 { 4039 4040 if (CHECK_ATTR(BPF_ENABLE_STATS)) 4041 return -EINVAL; 4042 4043 if (!capable(CAP_SYS_ADMIN)) 4044 return -EPERM; 4045 4046 switch (attr->enable_stats.type) { 4047 case BPF_STATS_RUN_TIME: 4048 return bpf_enable_runtime_stats(); 4049 default: 4050 break; 4051 } 4052 return -EINVAL; 4053 } 4054 4055 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags 4056 4057 static int bpf_iter_create(union bpf_attr *attr) 4058 { 4059 struct bpf_link *link; 4060 int err; 4061 4062 if (CHECK_ATTR(BPF_ITER_CREATE)) 4063 return -EINVAL; 4064 4065 if (attr->iter_create.flags) 4066 return -EINVAL; 4067 4068 link = bpf_link_get_from_fd(attr->iter_create.link_fd); 4069 if (IS_ERR(link)) 4070 return PTR_ERR(link); 4071 4072 err = bpf_iter_new_fd(link); 4073 bpf_link_put(link); 4074 4075 return err; 4076 } 4077 4078 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) 4079 { 4080 union bpf_attr attr; 4081 int err; 4082 4083 if (sysctl_unprivileged_bpf_disabled && !bpf_capable()) 4084 return -EPERM; 4085 4086 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); 4087 if (err) 4088 return err; 4089 size = min_t(u32, size, sizeof(attr)); 4090 4091 /* copy attributes from user space, may be less than sizeof(bpf_attr) */ 4092 memset(&attr, 0, sizeof(attr)); 4093 if (copy_from_user(&attr, uattr, size) != 0) 4094 return -EFAULT; 4095 4096 err = security_bpf(cmd, &attr, size); 4097 if (err < 0) 4098 return err; 4099 4100 switch (cmd) { 4101 case BPF_MAP_CREATE: 4102 err = map_create(&attr); 4103 break; 4104 case BPF_MAP_LOOKUP_ELEM: 4105 err = map_lookup_elem(&attr); 4106 break; 4107 case BPF_MAP_UPDATE_ELEM: 4108 err = map_update_elem(&attr); 4109 break; 4110 case BPF_MAP_DELETE_ELEM: 4111 err = map_delete_elem(&attr); 4112 break; 4113 case BPF_MAP_GET_NEXT_KEY: 4114 err = map_get_next_key(&attr); 4115 break; 4116 case BPF_MAP_FREEZE: 4117 err = map_freeze(&attr); 4118 break; 4119 case BPF_PROG_LOAD: 4120 err = bpf_prog_load(&attr, uattr); 4121 break; 4122 case BPF_OBJ_PIN: 4123 err = bpf_obj_pin(&attr); 4124 break; 4125 case BPF_OBJ_GET: 4126 err = bpf_obj_get(&attr); 4127 break; 4128 case BPF_PROG_ATTACH: 4129 err = bpf_prog_attach(&attr); 4130 break; 4131 case BPF_PROG_DETACH: 4132 err = bpf_prog_detach(&attr); 4133 break; 4134 case BPF_PROG_QUERY: 4135 err = bpf_prog_query(&attr, uattr); 4136 break; 4137 case BPF_PROG_TEST_RUN: 4138 err = bpf_prog_test_run(&attr, uattr); 4139 break; 4140 case BPF_PROG_GET_NEXT_ID: 4141 err = bpf_obj_get_next_id(&attr, uattr, 4142 &prog_idr, &prog_idr_lock); 4143 break; 4144 case BPF_MAP_GET_NEXT_ID: 4145 err = bpf_obj_get_next_id(&attr, uattr, 4146 &map_idr, &map_idr_lock); 4147 break; 4148 case BPF_BTF_GET_NEXT_ID: 4149 err = bpf_obj_get_next_id(&attr, uattr, 4150 &btf_idr, &btf_idr_lock); 4151 break; 4152 case BPF_PROG_GET_FD_BY_ID: 4153 err = bpf_prog_get_fd_by_id(&attr); 4154 break; 4155 case BPF_MAP_GET_FD_BY_ID: 4156 err = bpf_map_get_fd_by_id(&attr); 4157 break; 4158 case BPF_OBJ_GET_INFO_BY_FD: 4159 err = bpf_obj_get_info_by_fd(&attr, uattr); 4160 break; 4161 case BPF_RAW_TRACEPOINT_OPEN: 4162 err = bpf_raw_tracepoint_open(&attr); 4163 break; 4164 case BPF_BTF_LOAD: 4165 err = bpf_btf_load(&attr); 4166 break; 4167 case BPF_BTF_GET_FD_BY_ID: 4168 err = bpf_btf_get_fd_by_id(&attr); 4169 break; 4170 case BPF_TASK_FD_QUERY: 4171 err = bpf_task_fd_query(&attr, uattr); 4172 break; 4173 case BPF_MAP_LOOKUP_AND_DELETE_ELEM: 4174 err = map_lookup_and_delete_elem(&attr); 4175 break; 4176 case BPF_MAP_LOOKUP_BATCH: 4177 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH); 4178 break; 4179 case BPF_MAP_LOOKUP_AND_DELETE_BATCH: 4180 err = bpf_map_do_batch(&attr, uattr, 4181 BPF_MAP_LOOKUP_AND_DELETE_BATCH); 4182 break; 4183 case BPF_MAP_UPDATE_BATCH: 4184 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH); 4185 break; 4186 case BPF_MAP_DELETE_BATCH: 4187 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH); 4188 break; 4189 case BPF_LINK_CREATE: 4190 err = link_create(&attr); 4191 break; 4192 case BPF_LINK_UPDATE: 4193 err = link_update(&attr); 4194 break; 4195 case BPF_LINK_GET_FD_BY_ID: 4196 err = bpf_link_get_fd_by_id(&attr); 4197 break; 4198 case BPF_LINK_GET_NEXT_ID: 4199 err = bpf_obj_get_next_id(&attr, uattr, 4200 &link_idr, &link_idr_lock); 4201 break; 4202 case BPF_ENABLE_STATS: 4203 err = bpf_enable_stats(&attr); 4204 break; 4205 case BPF_ITER_CREATE: 4206 err = bpf_iter_create(&attr); 4207 break; 4208 default: 4209 err = -EINVAL; 4210 break; 4211 } 4212 4213 return err; 4214 } 4215