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