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 INIT_WORK(&link->work, bpf_link_put_deferred); 2349 schedule_work(&link->work); 2350 } 2351 2352 static int bpf_link_release(struct inode *inode, struct file *filp) 2353 { 2354 struct bpf_link *link = filp->private_data; 2355 2356 bpf_link_put(link); 2357 return 0; 2358 } 2359 2360 #ifdef CONFIG_PROC_FS 2361 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) 2362 #define BPF_MAP_TYPE(_id, _ops) 2363 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name, 2364 static const char *bpf_link_type_strs[] = { 2365 [BPF_LINK_TYPE_UNSPEC] = "<invalid>", 2366 #include <linux/bpf_types.h> 2367 }; 2368 #undef BPF_PROG_TYPE 2369 #undef BPF_MAP_TYPE 2370 #undef BPF_LINK_TYPE 2371 2372 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp) 2373 { 2374 const struct bpf_link *link = filp->private_data; 2375 const struct bpf_prog *prog = link->prog; 2376 char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 2377 2378 bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 2379 seq_printf(m, 2380 "link_type:\t%s\n" 2381 "link_id:\t%u\n" 2382 "prog_tag:\t%s\n" 2383 "prog_id:\t%u\n", 2384 bpf_link_type_strs[link->type], 2385 link->id, 2386 prog_tag, 2387 prog->aux->id); 2388 if (link->ops->show_fdinfo) 2389 link->ops->show_fdinfo(link, m); 2390 } 2391 #endif 2392 2393 static const struct file_operations bpf_link_fops = { 2394 #ifdef CONFIG_PROC_FS 2395 .show_fdinfo = bpf_link_show_fdinfo, 2396 #endif 2397 .release = bpf_link_release, 2398 .read = bpf_dummy_read, 2399 .write = bpf_dummy_write, 2400 }; 2401 2402 static int bpf_link_alloc_id(struct bpf_link *link) 2403 { 2404 int id; 2405 2406 idr_preload(GFP_KERNEL); 2407 spin_lock_bh(&link_idr_lock); 2408 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC); 2409 spin_unlock_bh(&link_idr_lock); 2410 idr_preload_end(); 2411 2412 return id; 2413 } 2414 2415 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file, 2416 * reserving unused FD and allocating ID from link_idr. This is to be paired 2417 * with bpf_link_settle() to install FD and ID and expose bpf_link to 2418 * user-space, if bpf_link is successfully attached. If not, bpf_link and 2419 * pre-allocated resources are to be freed with bpf_cleanup() call. All the 2420 * transient state is passed around in struct bpf_link_primer. 2421 * This is preferred way to create and initialize bpf_link, especially when 2422 * there are complicated and expensive operations inbetween creating bpf_link 2423 * itself and attaching it to BPF hook. By using bpf_link_prime() and 2424 * bpf_link_settle() kernel code using bpf_link doesn't have to perform 2425 * expensive (and potentially failing) roll back operations in a rare case 2426 * that file, FD, or ID can't be allocated. 2427 */ 2428 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer) 2429 { 2430 struct file *file; 2431 int fd, id; 2432 2433 fd = get_unused_fd_flags(O_CLOEXEC); 2434 if (fd < 0) 2435 return fd; 2436 2437 2438 id = bpf_link_alloc_id(link); 2439 if (id < 0) { 2440 put_unused_fd(fd); 2441 return id; 2442 } 2443 2444 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC); 2445 if (IS_ERR(file)) { 2446 bpf_link_free_id(id); 2447 put_unused_fd(fd); 2448 return PTR_ERR(file); 2449 } 2450 2451 primer->link = link; 2452 primer->file = file; 2453 primer->fd = fd; 2454 primer->id = id; 2455 return 0; 2456 } 2457 2458 int bpf_link_settle(struct bpf_link_primer *primer) 2459 { 2460 /* make bpf_link fetchable by ID */ 2461 spin_lock_bh(&link_idr_lock); 2462 primer->link->id = primer->id; 2463 spin_unlock_bh(&link_idr_lock); 2464 /* make bpf_link fetchable by FD */ 2465 fd_install(primer->fd, primer->file); 2466 /* pass through installed FD */ 2467 return primer->fd; 2468 } 2469 2470 int bpf_link_new_fd(struct bpf_link *link) 2471 { 2472 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC); 2473 } 2474 2475 struct bpf_link *bpf_link_get_from_fd(u32 ufd) 2476 { 2477 struct fd f = fdget(ufd); 2478 struct bpf_link *link; 2479 2480 if (!f.file) 2481 return ERR_PTR(-EBADF); 2482 if (f.file->f_op != &bpf_link_fops) { 2483 fdput(f); 2484 return ERR_PTR(-EINVAL); 2485 } 2486 2487 link = f.file->private_data; 2488 bpf_link_inc(link); 2489 fdput(f); 2490 2491 return link; 2492 } 2493 2494 struct bpf_tracing_link { 2495 struct bpf_link link; 2496 enum bpf_attach_type attach_type; 2497 }; 2498 2499 static void bpf_tracing_link_release(struct bpf_link *link) 2500 { 2501 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog)); 2502 } 2503 2504 static void bpf_tracing_link_dealloc(struct bpf_link *link) 2505 { 2506 struct bpf_tracing_link *tr_link = 2507 container_of(link, struct bpf_tracing_link, link); 2508 2509 kfree(tr_link); 2510 } 2511 2512 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link, 2513 struct seq_file *seq) 2514 { 2515 struct bpf_tracing_link *tr_link = 2516 container_of(link, struct bpf_tracing_link, link); 2517 2518 seq_printf(seq, 2519 "attach_type:\t%d\n", 2520 tr_link->attach_type); 2521 } 2522 2523 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, 2524 struct bpf_link_info *info) 2525 { 2526 struct bpf_tracing_link *tr_link = 2527 container_of(link, struct bpf_tracing_link, link); 2528 2529 info->tracing.attach_type = tr_link->attach_type; 2530 2531 return 0; 2532 } 2533 2534 static const struct bpf_link_ops bpf_tracing_link_lops = { 2535 .release = bpf_tracing_link_release, 2536 .dealloc = bpf_tracing_link_dealloc, 2537 .show_fdinfo = bpf_tracing_link_show_fdinfo, 2538 .fill_link_info = bpf_tracing_link_fill_link_info, 2539 }; 2540 2541 static int bpf_tracing_prog_attach(struct bpf_prog *prog) 2542 { 2543 struct bpf_link_primer link_primer; 2544 struct bpf_tracing_link *link; 2545 int err; 2546 2547 switch (prog->type) { 2548 case BPF_PROG_TYPE_TRACING: 2549 if (prog->expected_attach_type != BPF_TRACE_FENTRY && 2550 prog->expected_attach_type != BPF_TRACE_FEXIT && 2551 prog->expected_attach_type != BPF_MODIFY_RETURN) { 2552 err = -EINVAL; 2553 goto out_put_prog; 2554 } 2555 break; 2556 case BPF_PROG_TYPE_EXT: 2557 if (prog->expected_attach_type != 0) { 2558 err = -EINVAL; 2559 goto out_put_prog; 2560 } 2561 break; 2562 case BPF_PROG_TYPE_LSM: 2563 if (prog->expected_attach_type != BPF_LSM_MAC) { 2564 err = -EINVAL; 2565 goto out_put_prog; 2566 } 2567 break; 2568 default: 2569 err = -EINVAL; 2570 goto out_put_prog; 2571 } 2572 2573 link = kzalloc(sizeof(*link), GFP_USER); 2574 if (!link) { 2575 err = -ENOMEM; 2576 goto out_put_prog; 2577 } 2578 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING, 2579 &bpf_tracing_link_lops, prog); 2580 link->attach_type = prog->expected_attach_type; 2581 2582 err = bpf_link_prime(&link->link, &link_primer); 2583 if (err) { 2584 kfree(link); 2585 goto out_put_prog; 2586 } 2587 2588 err = bpf_trampoline_link_prog(prog); 2589 if (err) { 2590 bpf_link_cleanup(&link_primer); 2591 goto out_put_prog; 2592 } 2593 2594 return bpf_link_settle(&link_primer); 2595 out_put_prog: 2596 bpf_prog_put(prog); 2597 return err; 2598 } 2599 2600 struct bpf_raw_tp_link { 2601 struct bpf_link link; 2602 struct bpf_raw_event_map *btp; 2603 }; 2604 2605 static void bpf_raw_tp_link_release(struct bpf_link *link) 2606 { 2607 struct bpf_raw_tp_link *raw_tp = 2608 container_of(link, struct bpf_raw_tp_link, link); 2609 2610 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog); 2611 bpf_put_raw_tracepoint(raw_tp->btp); 2612 } 2613 2614 static void bpf_raw_tp_link_dealloc(struct bpf_link *link) 2615 { 2616 struct bpf_raw_tp_link *raw_tp = 2617 container_of(link, struct bpf_raw_tp_link, link); 2618 2619 kfree(raw_tp); 2620 } 2621 2622 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link, 2623 struct seq_file *seq) 2624 { 2625 struct bpf_raw_tp_link *raw_tp_link = 2626 container_of(link, struct bpf_raw_tp_link, link); 2627 2628 seq_printf(seq, 2629 "tp_name:\t%s\n", 2630 raw_tp_link->btp->tp->name); 2631 } 2632 2633 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, 2634 struct bpf_link_info *info) 2635 { 2636 struct bpf_raw_tp_link *raw_tp_link = 2637 container_of(link, struct bpf_raw_tp_link, link); 2638 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name); 2639 const char *tp_name = raw_tp_link->btp->tp->name; 2640 u32 ulen = info->raw_tracepoint.tp_name_len; 2641 size_t tp_len = strlen(tp_name); 2642 2643 if (!ulen ^ !ubuf) 2644 return -EINVAL; 2645 2646 info->raw_tracepoint.tp_name_len = tp_len + 1; 2647 2648 if (!ubuf) 2649 return 0; 2650 2651 if (ulen >= tp_len + 1) { 2652 if (copy_to_user(ubuf, tp_name, tp_len + 1)) 2653 return -EFAULT; 2654 } else { 2655 char zero = '\0'; 2656 2657 if (copy_to_user(ubuf, tp_name, ulen - 1)) 2658 return -EFAULT; 2659 if (put_user(zero, ubuf + ulen - 1)) 2660 return -EFAULT; 2661 return -ENOSPC; 2662 } 2663 2664 return 0; 2665 } 2666 2667 static const struct bpf_link_ops bpf_raw_tp_link_lops = { 2668 .release = bpf_raw_tp_link_release, 2669 .dealloc = bpf_raw_tp_link_dealloc, 2670 .show_fdinfo = bpf_raw_tp_link_show_fdinfo, 2671 .fill_link_info = bpf_raw_tp_link_fill_link_info, 2672 }; 2673 2674 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd 2675 2676 static int bpf_raw_tracepoint_open(const union bpf_attr *attr) 2677 { 2678 struct bpf_link_primer link_primer; 2679 struct bpf_raw_tp_link *link; 2680 struct bpf_raw_event_map *btp; 2681 struct bpf_prog *prog; 2682 const char *tp_name; 2683 char buf[128]; 2684 int err; 2685 2686 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN)) 2687 return -EINVAL; 2688 2689 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); 2690 if (IS_ERR(prog)) 2691 return PTR_ERR(prog); 2692 2693 switch (prog->type) { 2694 case BPF_PROG_TYPE_TRACING: 2695 case BPF_PROG_TYPE_EXT: 2696 case BPF_PROG_TYPE_LSM: 2697 if (attr->raw_tracepoint.name) { 2698 /* The attach point for this category of programs 2699 * should be specified via btf_id during program load. 2700 */ 2701 err = -EINVAL; 2702 goto out_put_prog; 2703 } 2704 if (prog->type == BPF_PROG_TYPE_TRACING && 2705 prog->expected_attach_type == BPF_TRACE_RAW_TP) { 2706 tp_name = prog->aux->attach_func_name; 2707 break; 2708 } 2709 return bpf_tracing_prog_attach(prog); 2710 case BPF_PROG_TYPE_RAW_TRACEPOINT: 2711 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 2712 if (strncpy_from_user(buf, 2713 u64_to_user_ptr(attr->raw_tracepoint.name), 2714 sizeof(buf) - 1) < 0) { 2715 err = -EFAULT; 2716 goto out_put_prog; 2717 } 2718 buf[sizeof(buf) - 1] = 0; 2719 tp_name = buf; 2720 break; 2721 default: 2722 err = -EINVAL; 2723 goto out_put_prog; 2724 } 2725 2726 btp = bpf_get_raw_tracepoint(tp_name); 2727 if (!btp) { 2728 err = -ENOENT; 2729 goto out_put_prog; 2730 } 2731 2732 link = kzalloc(sizeof(*link), GFP_USER); 2733 if (!link) { 2734 err = -ENOMEM; 2735 goto out_put_btp; 2736 } 2737 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT, 2738 &bpf_raw_tp_link_lops, prog); 2739 link->btp = btp; 2740 2741 err = bpf_link_prime(&link->link, &link_primer); 2742 if (err) { 2743 kfree(link); 2744 goto out_put_btp; 2745 } 2746 2747 err = bpf_probe_register(link->btp, prog); 2748 if (err) { 2749 bpf_link_cleanup(&link_primer); 2750 goto out_put_btp; 2751 } 2752 2753 return bpf_link_settle(&link_primer); 2754 2755 out_put_btp: 2756 bpf_put_raw_tracepoint(btp); 2757 out_put_prog: 2758 bpf_prog_put(prog); 2759 return err; 2760 } 2761 2762 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, 2763 enum bpf_attach_type attach_type) 2764 { 2765 switch (prog->type) { 2766 case BPF_PROG_TYPE_CGROUP_SOCK: 2767 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2768 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2769 case BPF_PROG_TYPE_SK_LOOKUP: 2770 return attach_type == prog->expected_attach_type ? 0 : -EINVAL; 2771 case BPF_PROG_TYPE_CGROUP_SKB: 2772 if (!capable(CAP_NET_ADMIN)) 2773 /* cg-skb progs can be loaded by unpriv user. 2774 * check permissions at attach time. 2775 */ 2776 return -EPERM; 2777 return prog->enforce_expected_attach_type && 2778 prog->expected_attach_type != attach_type ? 2779 -EINVAL : 0; 2780 default: 2781 return 0; 2782 } 2783 } 2784 2785 static enum bpf_prog_type 2786 attach_type_to_prog_type(enum bpf_attach_type attach_type) 2787 { 2788 switch (attach_type) { 2789 case BPF_CGROUP_INET_INGRESS: 2790 case BPF_CGROUP_INET_EGRESS: 2791 return BPF_PROG_TYPE_CGROUP_SKB; 2792 break; 2793 case BPF_CGROUP_INET_SOCK_CREATE: 2794 case BPF_CGROUP_INET_SOCK_RELEASE: 2795 case BPF_CGROUP_INET4_POST_BIND: 2796 case BPF_CGROUP_INET6_POST_BIND: 2797 return BPF_PROG_TYPE_CGROUP_SOCK; 2798 case BPF_CGROUP_INET4_BIND: 2799 case BPF_CGROUP_INET6_BIND: 2800 case BPF_CGROUP_INET4_CONNECT: 2801 case BPF_CGROUP_INET6_CONNECT: 2802 case BPF_CGROUP_INET4_GETPEERNAME: 2803 case BPF_CGROUP_INET6_GETPEERNAME: 2804 case BPF_CGROUP_INET4_GETSOCKNAME: 2805 case BPF_CGROUP_INET6_GETSOCKNAME: 2806 case BPF_CGROUP_UDP4_SENDMSG: 2807 case BPF_CGROUP_UDP6_SENDMSG: 2808 case BPF_CGROUP_UDP4_RECVMSG: 2809 case BPF_CGROUP_UDP6_RECVMSG: 2810 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 2811 case BPF_CGROUP_SOCK_OPS: 2812 return BPF_PROG_TYPE_SOCK_OPS; 2813 case BPF_CGROUP_DEVICE: 2814 return BPF_PROG_TYPE_CGROUP_DEVICE; 2815 case BPF_SK_MSG_VERDICT: 2816 return BPF_PROG_TYPE_SK_MSG; 2817 case BPF_SK_SKB_STREAM_PARSER: 2818 case BPF_SK_SKB_STREAM_VERDICT: 2819 return BPF_PROG_TYPE_SK_SKB; 2820 case BPF_LIRC_MODE2: 2821 return BPF_PROG_TYPE_LIRC_MODE2; 2822 case BPF_FLOW_DISSECTOR: 2823 return BPF_PROG_TYPE_FLOW_DISSECTOR; 2824 case BPF_CGROUP_SYSCTL: 2825 return BPF_PROG_TYPE_CGROUP_SYSCTL; 2826 case BPF_CGROUP_GETSOCKOPT: 2827 case BPF_CGROUP_SETSOCKOPT: 2828 return BPF_PROG_TYPE_CGROUP_SOCKOPT; 2829 case BPF_TRACE_ITER: 2830 return BPF_PROG_TYPE_TRACING; 2831 case BPF_SK_LOOKUP: 2832 return BPF_PROG_TYPE_SK_LOOKUP; 2833 case BPF_XDP: 2834 return BPF_PROG_TYPE_XDP; 2835 default: 2836 return BPF_PROG_TYPE_UNSPEC; 2837 } 2838 } 2839 2840 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd 2841 2842 #define BPF_F_ATTACH_MASK \ 2843 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE) 2844 2845 static int bpf_prog_attach(const union bpf_attr *attr) 2846 { 2847 enum bpf_prog_type ptype; 2848 struct bpf_prog *prog; 2849 int ret; 2850 2851 if (CHECK_ATTR(BPF_PROG_ATTACH)) 2852 return -EINVAL; 2853 2854 if (attr->attach_flags & ~BPF_F_ATTACH_MASK) 2855 return -EINVAL; 2856 2857 ptype = attach_type_to_prog_type(attr->attach_type); 2858 if (ptype == BPF_PROG_TYPE_UNSPEC) 2859 return -EINVAL; 2860 2861 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 2862 if (IS_ERR(prog)) 2863 return PTR_ERR(prog); 2864 2865 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { 2866 bpf_prog_put(prog); 2867 return -EINVAL; 2868 } 2869 2870 switch (ptype) { 2871 case BPF_PROG_TYPE_SK_SKB: 2872 case BPF_PROG_TYPE_SK_MSG: 2873 ret = sock_map_get_from_fd(attr, prog); 2874 break; 2875 case BPF_PROG_TYPE_LIRC_MODE2: 2876 ret = lirc_prog_attach(attr, prog); 2877 break; 2878 case BPF_PROG_TYPE_FLOW_DISSECTOR: 2879 ret = netns_bpf_prog_attach(attr, prog); 2880 break; 2881 case BPF_PROG_TYPE_CGROUP_DEVICE: 2882 case BPF_PROG_TYPE_CGROUP_SKB: 2883 case BPF_PROG_TYPE_CGROUP_SOCK: 2884 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2885 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2886 case BPF_PROG_TYPE_CGROUP_SYSCTL: 2887 case BPF_PROG_TYPE_SOCK_OPS: 2888 ret = cgroup_bpf_prog_attach(attr, ptype, prog); 2889 break; 2890 default: 2891 ret = -EINVAL; 2892 } 2893 2894 if (ret) 2895 bpf_prog_put(prog); 2896 return ret; 2897 } 2898 2899 #define BPF_PROG_DETACH_LAST_FIELD attach_type 2900 2901 static int bpf_prog_detach(const union bpf_attr *attr) 2902 { 2903 enum bpf_prog_type ptype; 2904 2905 if (CHECK_ATTR(BPF_PROG_DETACH)) 2906 return -EINVAL; 2907 2908 ptype = attach_type_to_prog_type(attr->attach_type); 2909 2910 switch (ptype) { 2911 case BPF_PROG_TYPE_SK_MSG: 2912 case BPF_PROG_TYPE_SK_SKB: 2913 return sock_map_prog_detach(attr, ptype); 2914 case BPF_PROG_TYPE_LIRC_MODE2: 2915 return lirc_prog_detach(attr); 2916 case BPF_PROG_TYPE_FLOW_DISSECTOR: 2917 return netns_bpf_prog_detach(attr, ptype); 2918 case BPF_PROG_TYPE_CGROUP_DEVICE: 2919 case BPF_PROG_TYPE_CGROUP_SKB: 2920 case BPF_PROG_TYPE_CGROUP_SOCK: 2921 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2922 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2923 case BPF_PROG_TYPE_CGROUP_SYSCTL: 2924 case BPF_PROG_TYPE_SOCK_OPS: 2925 return cgroup_bpf_prog_detach(attr, ptype); 2926 default: 2927 return -EINVAL; 2928 } 2929 } 2930 2931 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt 2932 2933 static int bpf_prog_query(const union bpf_attr *attr, 2934 union bpf_attr __user *uattr) 2935 { 2936 if (!capable(CAP_NET_ADMIN)) 2937 return -EPERM; 2938 if (CHECK_ATTR(BPF_PROG_QUERY)) 2939 return -EINVAL; 2940 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) 2941 return -EINVAL; 2942 2943 switch (attr->query.attach_type) { 2944 case BPF_CGROUP_INET_INGRESS: 2945 case BPF_CGROUP_INET_EGRESS: 2946 case BPF_CGROUP_INET_SOCK_CREATE: 2947 case BPF_CGROUP_INET_SOCK_RELEASE: 2948 case BPF_CGROUP_INET4_BIND: 2949 case BPF_CGROUP_INET6_BIND: 2950 case BPF_CGROUP_INET4_POST_BIND: 2951 case BPF_CGROUP_INET6_POST_BIND: 2952 case BPF_CGROUP_INET4_CONNECT: 2953 case BPF_CGROUP_INET6_CONNECT: 2954 case BPF_CGROUP_INET4_GETPEERNAME: 2955 case BPF_CGROUP_INET6_GETPEERNAME: 2956 case BPF_CGROUP_INET4_GETSOCKNAME: 2957 case BPF_CGROUP_INET6_GETSOCKNAME: 2958 case BPF_CGROUP_UDP4_SENDMSG: 2959 case BPF_CGROUP_UDP6_SENDMSG: 2960 case BPF_CGROUP_UDP4_RECVMSG: 2961 case BPF_CGROUP_UDP6_RECVMSG: 2962 case BPF_CGROUP_SOCK_OPS: 2963 case BPF_CGROUP_DEVICE: 2964 case BPF_CGROUP_SYSCTL: 2965 case BPF_CGROUP_GETSOCKOPT: 2966 case BPF_CGROUP_SETSOCKOPT: 2967 return cgroup_bpf_prog_query(attr, uattr); 2968 case BPF_LIRC_MODE2: 2969 return lirc_prog_query(attr, uattr); 2970 case BPF_FLOW_DISSECTOR: 2971 case BPF_SK_LOOKUP: 2972 return netns_bpf_prog_query(attr, uattr); 2973 default: 2974 return -EINVAL; 2975 } 2976 } 2977 2978 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out 2979 2980 static int bpf_prog_test_run(const union bpf_attr *attr, 2981 union bpf_attr __user *uattr) 2982 { 2983 struct bpf_prog *prog; 2984 int ret = -ENOTSUPP; 2985 2986 if (CHECK_ATTR(BPF_PROG_TEST_RUN)) 2987 return -EINVAL; 2988 2989 if ((attr->test.ctx_size_in && !attr->test.ctx_in) || 2990 (!attr->test.ctx_size_in && attr->test.ctx_in)) 2991 return -EINVAL; 2992 2993 if ((attr->test.ctx_size_out && !attr->test.ctx_out) || 2994 (!attr->test.ctx_size_out && attr->test.ctx_out)) 2995 return -EINVAL; 2996 2997 prog = bpf_prog_get(attr->test.prog_fd); 2998 if (IS_ERR(prog)) 2999 return PTR_ERR(prog); 3000 3001 if (prog->aux->ops->test_run) 3002 ret = prog->aux->ops->test_run(prog, attr, uattr); 3003 3004 bpf_prog_put(prog); 3005 return ret; 3006 } 3007 3008 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id 3009 3010 static int bpf_obj_get_next_id(const union bpf_attr *attr, 3011 union bpf_attr __user *uattr, 3012 struct idr *idr, 3013 spinlock_t *lock) 3014 { 3015 u32 next_id = attr->start_id; 3016 int err = 0; 3017 3018 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) 3019 return -EINVAL; 3020 3021 if (!capable(CAP_SYS_ADMIN)) 3022 return -EPERM; 3023 3024 next_id++; 3025 spin_lock_bh(lock); 3026 if (!idr_get_next(idr, &next_id)) 3027 err = -ENOENT; 3028 spin_unlock_bh(lock); 3029 3030 if (!err) 3031 err = put_user(next_id, &uattr->next_id); 3032 3033 return err; 3034 } 3035 3036 struct bpf_map *bpf_map_get_curr_or_next(u32 *id) 3037 { 3038 struct bpf_map *map; 3039 3040 spin_lock_bh(&map_idr_lock); 3041 again: 3042 map = idr_get_next(&map_idr, id); 3043 if (map) { 3044 map = __bpf_map_inc_not_zero(map, false); 3045 if (IS_ERR(map)) { 3046 (*id)++; 3047 goto again; 3048 } 3049 } 3050 spin_unlock_bh(&map_idr_lock); 3051 3052 return map; 3053 } 3054 3055 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id) 3056 { 3057 struct bpf_prog *prog; 3058 3059 spin_lock_bh(&prog_idr_lock); 3060 again: 3061 prog = idr_get_next(&prog_idr, id); 3062 if (prog) { 3063 prog = bpf_prog_inc_not_zero(prog); 3064 if (IS_ERR(prog)) { 3065 (*id)++; 3066 goto again; 3067 } 3068 } 3069 spin_unlock_bh(&prog_idr_lock); 3070 3071 return prog; 3072 } 3073 3074 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id 3075 3076 struct bpf_prog *bpf_prog_by_id(u32 id) 3077 { 3078 struct bpf_prog *prog; 3079 3080 if (!id) 3081 return ERR_PTR(-ENOENT); 3082 3083 spin_lock_bh(&prog_idr_lock); 3084 prog = idr_find(&prog_idr, id); 3085 if (prog) 3086 prog = bpf_prog_inc_not_zero(prog); 3087 else 3088 prog = ERR_PTR(-ENOENT); 3089 spin_unlock_bh(&prog_idr_lock); 3090 return prog; 3091 } 3092 3093 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) 3094 { 3095 struct bpf_prog *prog; 3096 u32 id = attr->prog_id; 3097 int fd; 3098 3099 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) 3100 return -EINVAL; 3101 3102 if (!capable(CAP_SYS_ADMIN)) 3103 return -EPERM; 3104 3105 prog = bpf_prog_by_id(id); 3106 if (IS_ERR(prog)) 3107 return PTR_ERR(prog); 3108 3109 fd = bpf_prog_new_fd(prog); 3110 if (fd < 0) 3111 bpf_prog_put(prog); 3112 3113 return fd; 3114 } 3115 3116 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags 3117 3118 static int bpf_map_get_fd_by_id(const union bpf_attr *attr) 3119 { 3120 struct bpf_map *map; 3121 u32 id = attr->map_id; 3122 int f_flags; 3123 int fd; 3124 3125 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || 3126 attr->open_flags & ~BPF_OBJ_FLAG_MASK) 3127 return -EINVAL; 3128 3129 if (!capable(CAP_SYS_ADMIN)) 3130 return -EPERM; 3131 3132 f_flags = bpf_get_file_flag(attr->open_flags); 3133 if (f_flags < 0) 3134 return f_flags; 3135 3136 spin_lock_bh(&map_idr_lock); 3137 map = idr_find(&map_idr, id); 3138 if (map) 3139 map = __bpf_map_inc_not_zero(map, true); 3140 else 3141 map = ERR_PTR(-ENOENT); 3142 spin_unlock_bh(&map_idr_lock); 3143 3144 if (IS_ERR(map)) 3145 return PTR_ERR(map); 3146 3147 fd = bpf_map_new_fd(map, f_flags); 3148 if (fd < 0) 3149 bpf_map_put_with_uref(map); 3150 3151 return fd; 3152 } 3153 3154 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, 3155 unsigned long addr, u32 *off, 3156 u32 *type) 3157 { 3158 const struct bpf_map *map; 3159 int i; 3160 3161 mutex_lock(&prog->aux->used_maps_mutex); 3162 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { 3163 map = prog->aux->used_maps[i]; 3164 if (map == (void *)addr) { 3165 *type = BPF_PSEUDO_MAP_FD; 3166 goto out; 3167 } 3168 if (!map->ops->map_direct_value_meta) 3169 continue; 3170 if (!map->ops->map_direct_value_meta(map, addr, off)) { 3171 *type = BPF_PSEUDO_MAP_VALUE; 3172 goto out; 3173 } 3174 } 3175 map = NULL; 3176 3177 out: 3178 mutex_unlock(&prog->aux->used_maps_mutex); 3179 return map; 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 mutex_lock(&prog->aux->used_maps_mutex); 3298 ulen = info.nr_map_ids; 3299 info.nr_map_ids = prog->aux->used_map_cnt; 3300 ulen = min_t(u32, info.nr_map_ids, ulen); 3301 if (ulen) { 3302 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); 3303 u32 i; 3304 3305 for (i = 0; i < ulen; i++) 3306 if (put_user(prog->aux->used_maps[i]->id, 3307 &user_map_ids[i])) { 3308 mutex_unlock(&prog->aux->used_maps_mutex); 3309 return -EFAULT; 3310 } 3311 } 3312 mutex_unlock(&prog->aux->used_maps_mutex); 3313 3314 err = set_info_rec_size(&info); 3315 if (err) 3316 return err; 3317 3318 bpf_prog_get_stats(prog, &stats); 3319 info.run_time_ns = stats.nsecs; 3320 info.run_cnt = stats.cnt; 3321 3322 if (!bpf_capable()) { 3323 info.jited_prog_len = 0; 3324 info.xlated_prog_len = 0; 3325 info.nr_jited_ksyms = 0; 3326 info.nr_jited_func_lens = 0; 3327 info.nr_func_info = 0; 3328 info.nr_line_info = 0; 3329 info.nr_jited_line_info = 0; 3330 goto done; 3331 } 3332 3333 ulen = info.xlated_prog_len; 3334 info.xlated_prog_len = bpf_prog_insn_size(prog); 3335 if (info.xlated_prog_len && ulen) { 3336 struct bpf_insn *insns_sanitized; 3337 bool fault; 3338 3339 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) { 3340 info.xlated_prog_insns = 0; 3341 goto done; 3342 } 3343 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred); 3344 if (!insns_sanitized) 3345 return -ENOMEM; 3346 uinsns = u64_to_user_ptr(info.xlated_prog_insns); 3347 ulen = min_t(u32, info.xlated_prog_len, ulen); 3348 fault = copy_to_user(uinsns, insns_sanitized, ulen); 3349 kfree(insns_sanitized); 3350 if (fault) 3351 return -EFAULT; 3352 } 3353 3354 if (bpf_prog_is_dev_bound(prog->aux)) { 3355 err = bpf_prog_offload_info_fill(&info, prog); 3356 if (err) 3357 return err; 3358 goto done; 3359 } 3360 3361 /* NOTE: the following code is supposed to be skipped for offload. 3362 * bpf_prog_offload_info_fill() is the place to fill similar fields 3363 * for offload. 3364 */ 3365 ulen = info.jited_prog_len; 3366 if (prog->aux->func_cnt) { 3367 u32 i; 3368 3369 info.jited_prog_len = 0; 3370 for (i = 0; i < prog->aux->func_cnt; i++) 3371 info.jited_prog_len += prog->aux->func[i]->jited_len; 3372 } else { 3373 info.jited_prog_len = prog->jited_len; 3374 } 3375 3376 if (info.jited_prog_len && ulen) { 3377 if (bpf_dump_raw_ok(file->f_cred)) { 3378 uinsns = u64_to_user_ptr(info.jited_prog_insns); 3379 ulen = min_t(u32, info.jited_prog_len, ulen); 3380 3381 /* for multi-function programs, copy the JITed 3382 * instructions for all the functions 3383 */ 3384 if (prog->aux->func_cnt) { 3385 u32 len, free, i; 3386 u8 *img; 3387 3388 free = ulen; 3389 for (i = 0; i < prog->aux->func_cnt; i++) { 3390 len = prog->aux->func[i]->jited_len; 3391 len = min_t(u32, len, free); 3392 img = (u8 *) prog->aux->func[i]->bpf_func; 3393 if (copy_to_user(uinsns, img, len)) 3394 return -EFAULT; 3395 uinsns += len; 3396 free -= len; 3397 if (!free) 3398 break; 3399 } 3400 } else { 3401 if (copy_to_user(uinsns, prog->bpf_func, ulen)) 3402 return -EFAULT; 3403 } 3404 } else { 3405 info.jited_prog_insns = 0; 3406 } 3407 } 3408 3409 ulen = info.nr_jited_ksyms; 3410 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; 3411 if (ulen) { 3412 if (bpf_dump_raw_ok(file->f_cred)) { 3413 unsigned long ksym_addr; 3414 u64 __user *user_ksyms; 3415 u32 i; 3416 3417 /* copy the address of the kernel symbol 3418 * corresponding to each function 3419 */ 3420 ulen = min_t(u32, info.nr_jited_ksyms, ulen); 3421 user_ksyms = u64_to_user_ptr(info.jited_ksyms); 3422 if (prog->aux->func_cnt) { 3423 for (i = 0; i < ulen; i++) { 3424 ksym_addr = (unsigned long) 3425 prog->aux->func[i]->bpf_func; 3426 if (put_user((u64) ksym_addr, 3427 &user_ksyms[i])) 3428 return -EFAULT; 3429 } 3430 } else { 3431 ksym_addr = (unsigned long) prog->bpf_func; 3432 if (put_user((u64) ksym_addr, &user_ksyms[0])) 3433 return -EFAULT; 3434 } 3435 } else { 3436 info.jited_ksyms = 0; 3437 } 3438 } 3439 3440 ulen = info.nr_jited_func_lens; 3441 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; 3442 if (ulen) { 3443 if (bpf_dump_raw_ok(file->f_cred)) { 3444 u32 __user *user_lens; 3445 u32 func_len, i; 3446 3447 /* copy the JITed image lengths for each function */ 3448 ulen = min_t(u32, info.nr_jited_func_lens, ulen); 3449 user_lens = u64_to_user_ptr(info.jited_func_lens); 3450 if (prog->aux->func_cnt) { 3451 for (i = 0; i < ulen; i++) { 3452 func_len = 3453 prog->aux->func[i]->jited_len; 3454 if (put_user(func_len, &user_lens[i])) 3455 return -EFAULT; 3456 } 3457 } else { 3458 func_len = prog->jited_len; 3459 if (put_user(func_len, &user_lens[0])) 3460 return -EFAULT; 3461 } 3462 } else { 3463 info.jited_func_lens = 0; 3464 } 3465 } 3466 3467 if (prog->aux->btf) 3468 info.btf_id = btf_id(prog->aux->btf); 3469 3470 ulen = info.nr_func_info; 3471 info.nr_func_info = prog->aux->func_info_cnt; 3472 if (info.nr_func_info && ulen) { 3473 char __user *user_finfo; 3474 3475 user_finfo = u64_to_user_ptr(info.func_info); 3476 ulen = min_t(u32, info.nr_func_info, ulen); 3477 if (copy_to_user(user_finfo, prog->aux->func_info, 3478 info.func_info_rec_size * ulen)) 3479 return -EFAULT; 3480 } 3481 3482 ulen = info.nr_line_info; 3483 info.nr_line_info = prog->aux->nr_linfo; 3484 if (info.nr_line_info && ulen) { 3485 __u8 __user *user_linfo; 3486 3487 user_linfo = u64_to_user_ptr(info.line_info); 3488 ulen = min_t(u32, info.nr_line_info, ulen); 3489 if (copy_to_user(user_linfo, prog->aux->linfo, 3490 info.line_info_rec_size * ulen)) 3491 return -EFAULT; 3492 } 3493 3494 ulen = info.nr_jited_line_info; 3495 if (prog->aux->jited_linfo) 3496 info.nr_jited_line_info = prog->aux->nr_linfo; 3497 else 3498 info.nr_jited_line_info = 0; 3499 if (info.nr_jited_line_info && ulen) { 3500 if (bpf_dump_raw_ok(file->f_cred)) { 3501 __u64 __user *user_linfo; 3502 u32 i; 3503 3504 user_linfo = u64_to_user_ptr(info.jited_line_info); 3505 ulen = min_t(u32, info.nr_jited_line_info, ulen); 3506 for (i = 0; i < ulen; i++) { 3507 if (put_user((__u64)(long)prog->aux->jited_linfo[i], 3508 &user_linfo[i])) 3509 return -EFAULT; 3510 } 3511 } else { 3512 info.jited_line_info = 0; 3513 } 3514 } 3515 3516 ulen = info.nr_prog_tags; 3517 info.nr_prog_tags = prog->aux->func_cnt ? : 1; 3518 if (ulen) { 3519 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; 3520 u32 i; 3521 3522 user_prog_tags = u64_to_user_ptr(info.prog_tags); 3523 ulen = min_t(u32, info.nr_prog_tags, ulen); 3524 if (prog->aux->func_cnt) { 3525 for (i = 0; i < ulen; i++) { 3526 if (copy_to_user(user_prog_tags[i], 3527 prog->aux->func[i]->tag, 3528 BPF_TAG_SIZE)) 3529 return -EFAULT; 3530 } 3531 } else { 3532 if (copy_to_user(user_prog_tags[0], 3533 prog->tag, BPF_TAG_SIZE)) 3534 return -EFAULT; 3535 } 3536 } 3537 3538 done: 3539 if (copy_to_user(uinfo, &info, info_len) || 3540 put_user(info_len, &uattr->info.info_len)) 3541 return -EFAULT; 3542 3543 return 0; 3544 } 3545 3546 static int bpf_map_get_info_by_fd(struct file *file, 3547 struct bpf_map *map, 3548 const union bpf_attr *attr, 3549 union bpf_attr __user *uattr) 3550 { 3551 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); 3552 struct bpf_map_info info; 3553 u32 info_len = attr->info.info_len; 3554 int err; 3555 3556 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); 3557 if (err) 3558 return err; 3559 info_len = min_t(u32, sizeof(info), info_len); 3560 3561 memset(&info, 0, sizeof(info)); 3562 info.type = map->map_type; 3563 info.id = map->id; 3564 info.key_size = map->key_size; 3565 info.value_size = map->value_size; 3566 info.max_entries = map->max_entries; 3567 info.map_flags = map->map_flags; 3568 memcpy(info.name, map->name, sizeof(map->name)); 3569 3570 if (map->btf) { 3571 info.btf_id = btf_id(map->btf); 3572 info.btf_key_type_id = map->btf_key_type_id; 3573 info.btf_value_type_id = map->btf_value_type_id; 3574 } 3575 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; 3576 3577 if (bpf_map_is_dev_bound(map)) { 3578 err = bpf_map_offload_info_fill(&info, map); 3579 if (err) 3580 return err; 3581 } 3582 3583 if (copy_to_user(uinfo, &info, info_len) || 3584 put_user(info_len, &uattr->info.info_len)) 3585 return -EFAULT; 3586 3587 return 0; 3588 } 3589 3590 static int bpf_btf_get_info_by_fd(struct file *file, 3591 struct btf *btf, 3592 const union bpf_attr *attr, 3593 union bpf_attr __user *uattr) 3594 { 3595 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); 3596 u32 info_len = attr->info.info_len; 3597 int err; 3598 3599 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len); 3600 if (err) 3601 return err; 3602 3603 return btf_get_info_by_fd(btf, attr, uattr); 3604 } 3605 3606 static int bpf_link_get_info_by_fd(struct file *file, 3607 struct bpf_link *link, 3608 const union bpf_attr *attr, 3609 union bpf_attr __user *uattr) 3610 { 3611 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info); 3612 struct bpf_link_info info; 3613 u32 info_len = attr->info.info_len; 3614 int err; 3615 3616 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); 3617 if (err) 3618 return err; 3619 info_len = min_t(u32, sizeof(info), info_len); 3620 3621 memset(&info, 0, sizeof(info)); 3622 if (copy_from_user(&info, uinfo, info_len)) 3623 return -EFAULT; 3624 3625 info.type = link->type; 3626 info.id = link->id; 3627 info.prog_id = link->prog->aux->id; 3628 3629 if (link->ops->fill_link_info) { 3630 err = link->ops->fill_link_info(link, &info); 3631 if (err) 3632 return err; 3633 } 3634 3635 if (copy_to_user(uinfo, &info, info_len) || 3636 put_user(info_len, &uattr->info.info_len)) 3637 return -EFAULT; 3638 3639 return 0; 3640 } 3641 3642 3643 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info 3644 3645 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, 3646 union bpf_attr __user *uattr) 3647 { 3648 int ufd = attr->info.bpf_fd; 3649 struct fd f; 3650 int err; 3651 3652 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) 3653 return -EINVAL; 3654 3655 f = fdget(ufd); 3656 if (!f.file) 3657 return -EBADFD; 3658 3659 if (f.file->f_op == &bpf_prog_fops) 3660 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr, 3661 uattr); 3662 else if (f.file->f_op == &bpf_map_fops) 3663 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr, 3664 uattr); 3665 else if (f.file->f_op == &btf_fops) 3666 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr); 3667 else if (f.file->f_op == &bpf_link_fops) 3668 err = bpf_link_get_info_by_fd(f.file, f.file->private_data, 3669 attr, uattr); 3670 else 3671 err = -EINVAL; 3672 3673 fdput(f); 3674 return err; 3675 } 3676 3677 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level 3678 3679 static int bpf_btf_load(const union bpf_attr *attr) 3680 { 3681 if (CHECK_ATTR(BPF_BTF_LOAD)) 3682 return -EINVAL; 3683 3684 if (!bpf_capable()) 3685 return -EPERM; 3686 3687 return btf_new_fd(attr); 3688 } 3689 3690 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id 3691 3692 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) 3693 { 3694 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) 3695 return -EINVAL; 3696 3697 if (!capable(CAP_SYS_ADMIN)) 3698 return -EPERM; 3699 3700 return btf_get_fd_by_id(attr->btf_id); 3701 } 3702 3703 static int bpf_task_fd_query_copy(const union bpf_attr *attr, 3704 union bpf_attr __user *uattr, 3705 u32 prog_id, u32 fd_type, 3706 const char *buf, u64 probe_offset, 3707 u64 probe_addr) 3708 { 3709 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); 3710 u32 len = buf ? strlen(buf) : 0, input_len; 3711 int err = 0; 3712 3713 if (put_user(len, &uattr->task_fd_query.buf_len)) 3714 return -EFAULT; 3715 input_len = attr->task_fd_query.buf_len; 3716 if (input_len && ubuf) { 3717 if (!len) { 3718 /* nothing to copy, just make ubuf NULL terminated */ 3719 char zero = '\0'; 3720 3721 if (put_user(zero, ubuf)) 3722 return -EFAULT; 3723 } else if (input_len >= len + 1) { 3724 /* ubuf can hold the string with NULL terminator */ 3725 if (copy_to_user(ubuf, buf, len + 1)) 3726 return -EFAULT; 3727 } else { 3728 /* ubuf cannot hold the string with NULL terminator, 3729 * do a partial copy with NULL terminator. 3730 */ 3731 char zero = '\0'; 3732 3733 err = -ENOSPC; 3734 if (copy_to_user(ubuf, buf, input_len - 1)) 3735 return -EFAULT; 3736 if (put_user(zero, ubuf + input_len - 1)) 3737 return -EFAULT; 3738 } 3739 } 3740 3741 if (put_user(prog_id, &uattr->task_fd_query.prog_id) || 3742 put_user(fd_type, &uattr->task_fd_query.fd_type) || 3743 put_user(probe_offset, &uattr->task_fd_query.probe_offset) || 3744 put_user(probe_addr, &uattr->task_fd_query.probe_addr)) 3745 return -EFAULT; 3746 3747 return err; 3748 } 3749 3750 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr 3751 3752 static int bpf_task_fd_query(const union bpf_attr *attr, 3753 union bpf_attr __user *uattr) 3754 { 3755 pid_t pid = attr->task_fd_query.pid; 3756 u32 fd = attr->task_fd_query.fd; 3757 const struct perf_event *event; 3758 struct files_struct *files; 3759 struct task_struct *task; 3760 struct file *file; 3761 int err; 3762 3763 if (CHECK_ATTR(BPF_TASK_FD_QUERY)) 3764 return -EINVAL; 3765 3766 if (!capable(CAP_SYS_ADMIN)) 3767 return -EPERM; 3768 3769 if (attr->task_fd_query.flags != 0) 3770 return -EINVAL; 3771 3772 task = get_pid_task(find_vpid(pid), PIDTYPE_PID); 3773 if (!task) 3774 return -ENOENT; 3775 3776 files = get_files_struct(task); 3777 put_task_struct(task); 3778 if (!files) 3779 return -ENOENT; 3780 3781 err = 0; 3782 spin_lock(&files->file_lock); 3783 file = fcheck_files(files, fd); 3784 if (!file) 3785 err = -EBADF; 3786 else 3787 get_file(file); 3788 spin_unlock(&files->file_lock); 3789 put_files_struct(files); 3790 3791 if (err) 3792 goto out; 3793 3794 if (file->f_op == &bpf_link_fops) { 3795 struct bpf_link *link = file->private_data; 3796 3797 if (link->ops == &bpf_raw_tp_link_lops) { 3798 struct bpf_raw_tp_link *raw_tp = 3799 container_of(link, struct bpf_raw_tp_link, link); 3800 struct bpf_raw_event_map *btp = raw_tp->btp; 3801 3802 err = bpf_task_fd_query_copy(attr, uattr, 3803 raw_tp->link.prog->aux->id, 3804 BPF_FD_TYPE_RAW_TRACEPOINT, 3805 btp->tp->name, 0, 0); 3806 goto put_file; 3807 } 3808 goto out_not_supp; 3809 } 3810 3811 event = perf_get_event(file); 3812 if (!IS_ERR(event)) { 3813 u64 probe_offset, probe_addr; 3814 u32 prog_id, fd_type; 3815 const char *buf; 3816 3817 err = bpf_get_perf_event_info(event, &prog_id, &fd_type, 3818 &buf, &probe_offset, 3819 &probe_addr); 3820 if (!err) 3821 err = bpf_task_fd_query_copy(attr, uattr, prog_id, 3822 fd_type, buf, 3823 probe_offset, 3824 probe_addr); 3825 goto put_file; 3826 } 3827 3828 out_not_supp: 3829 err = -ENOTSUPP; 3830 put_file: 3831 fput(file); 3832 out: 3833 return err; 3834 } 3835 3836 #define BPF_MAP_BATCH_LAST_FIELD batch.flags 3837 3838 #define BPF_DO_BATCH(fn) \ 3839 do { \ 3840 if (!fn) { \ 3841 err = -ENOTSUPP; \ 3842 goto err_put; \ 3843 } \ 3844 err = fn(map, attr, uattr); \ 3845 } while (0) 3846 3847 static int bpf_map_do_batch(const union bpf_attr *attr, 3848 union bpf_attr __user *uattr, 3849 int cmd) 3850 { 3851 struct bpf_map *map; 3852 int err, ufd; 3853 struct fd f; 3854 3855 if (CHECK_ATTR(BPF_MAP_BATCH)) 3856 return -EINVAL; 3857 3858 ufd = attr->batch.map_fd; 3859 f = fdget(ufd); 3860 map = __bpf_map_get(f); 3861 if (IS_ERR(map)) 3862 return PTR_ERR(map); 3863 3864 if ((cmd == BPF_MAP_LOOKUP_BATCH || 3865 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) && 3866 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { 3867 err = -EPERM; 3868 goto err_put; 3869 } 3870 3871 if (cmd != BPF_MAP_LOOKUP_BATCH && 3872 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 3873 err = -EPERM; 3874 goto err_put; 3875 } 3876 3877 if (cmd == BPF_MAP_LOOKUP_BATCH) 3878 BPF_DO_BATCH(map->ops->map_lookup_batch); 3879 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) 3880 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch); 3881 else if (cmd == BPF_MAP_UPDATE_BATCH) 3882 BPF_DO_BATCH(map->ops->map_update_batch); 3883 else 3884 BPF_DO_BATCH(map->ops->map_delete_batch); 3885 3886 err_put: 3887 fdput(f); 3888 return err; 3889 } 3890 3891 static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 3892 { 3893 if (attr->link_create.attach_type == BPF_TRACE_ITER && 3894 prog->expected_attach_type == BPF_TRACE_ITER) 3895 return bpf_iter_link_attach(attr, prog); 3896 3897 return -EINVAL; 3898 } 3899 3900 #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len 3901 static int link_create(union bpf_attr *attr) 3902 { 3903 enum bpf_prog_type ptype; 3904 struct bpf_prog *prog; 3905 int ret; 3906 3907 if (CHECK_ATTR(BPF_LINK_CREATE)) 3908 return -EINVAL; 3909 3910 ptype = attach_type_to_prog_type(attr->link_create.attach_type); 3911 if (ptype == BPF_PROG_TYPE_UNSPEC) 3912 return -EINVAL; 3913 3914 prog = bpf_prog_get_type(attr->link_create.prog_fd, ptype); 3915 if (IS_ERR(prog)) 3916 return PTR_ERR(prog); 3917 3918 ret = bpf_prog_attach_check_attach_type(prog, 3919 attr->link_create.attach_type); 3920 if (ret) 3921 goto err_out; 3922 3923 switch (ptype) { 3924 case BPF_PROG_TYPE_CGROUP_SKB: 3925 case BPF_PROG_TYPE_CGROUP_SOCK: 3926 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 3927 case BPF_PROG_TYPE_SOCK_OPS: 3928 case BPF_PROG_TYPE_CGROUP_DEVICE: 3929 case BPF_PROG_TYPE_CGROUP_SYSCTL: 3930 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 3931 ret = cgroup_bpf_link_attach(attr, prog); 3932 break; 3933 case BPF_PROG_TYPE_TRACING: 3934 ret = tracing_bpf_link_attach(attr, prog); 3935 break; 3936 case BPF_PROG_TYPE_FLOW_DISSECTOR: 3937 case BPF_PROG_TYPE_SK_LOOKUP: 3938 ret = netns_bpf_link_create(attr, prog); 3939 break; 3940 #ifdef CONFIG_NET 3941 case BPF_PROG_TYPE_XDP: 3942 ret = bpf_xdp_link_attach(attr, prog); 3943 break; 3944 #endif 3945 default: 3946 ret = -EINVAL; 3947 } 3948 3949 err_out: 3950 if (ret < 0) 3951 bpf_prog_put(prog); 3952 return ret; 3953 } 3954 3955 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd 3956 3957 static int link_update(union bpf_attr *attr) 3958 { 3959 struct bpf_prog *old_prog = NULL, *new_prog; 3960 struct bpf_link *link; 3961 u32 flags; 3962 int ret; 3963 3964 if (CHECK_ATTR(BPF_LINK_UPDATE)) 3965 return -EINVAL; 3966 3967 flags = attr->link_update.flags; 3968 if (flags & ~BPF_F_REPLACE) 3969 return -EINVAL; 3970 3971 link = bpf_link_get_from_fd(attr->link_update.link_fd); 3972 if (IS_ERR(link)) 3973 return PTR_ERR(link); 3974 3975 new_prog = bpf_prog_get(attr->link_update.new_prog_fd); 3976 if (IS_ERR(new_prog)) { 3977 ret = PTR_ERR(new_prog); 3978 goto out_put_link; 3979 } 3980 3981 if (flags & BPF_F_REPLACE) { 3982 old_prog = bpf_prog_get(attr->link_update.old_prog_fd); 3983 if (IS_ERR(old_prog)) { 3984 ret = PTR_ERR(old_prog); 3985 old_prog = NULL; 3986 goto out_put_progs; 3987 } 3988 } else if (attr->link_update.old_prog_fd) { 3989 ret = -EINVAL; 3990 goto out_put_progs; 3991 } 3992 3993 if (link->ops->update_prog) 3994 ret = link->ops->update_prog(link, new_prog, old_prog); 3995 else 3996 ret = -EINVAL; 3997 3998 out_put_progs: 3999 if (old_prog) 4000 bpf_prog_put(old_prog); 4001 if (ret) 4002 bpf_prog_put(new_prog); 4003 out_put_link: 4004 bpf_link_put(link); 4005 return ret; 4006 } 4007 4008 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd 4009 4010 static int link_detach(union bpf_attr *attr) 4011 { 4012 struct bpf_link *link; 4013 int ret; 4014 4015 if (CHECK_ATTR(BPF_LINK_DETACH)) 4016 return -EINVAL; 4017 4018 link = bpf_link_get_from_fd(attr->link_detach.link_fd); 4019 if (IS_ERR(link)) 4020 return PTR_ERR(link); 4021 4022 if (link->ops->detach) 4023 ret = link->ops->detach(link); 4024 else 4025 ret = -EOPNOTSUPP; 4026 4027 bpf_link_put(link); 4028 return ret; 4029 } 4030 4031 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link) 4032 { 4033 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT); 4034 } 4035 4036 struct bpf_link *bpf_link_by_id(u32 id) 4037 { 4038 struct bpf_link *link; 4039 4040 if (!id) 4041 return ERR_PTR(-ENOENT); 4042 4043 spin_lock_bh(&link_idr_lock); 4044 /* before link is "settled", ID is 0, pretend it doesn't exist yet */ 4045 link = idr_find(&link_idr, id); 4046 if (link) { 4047 if (link->id) 4048 link = bpf_link_inc_not_zero(link); 4049 else 4050 link = ERR_PTR(-EAGAIN); 4051 } else { 4052 link = ERR_PTR(-ENOENT); 4053 } 4054 spin_unlock_bh(&link_idr_lock); 4055 return link; 4056 } 4057 4058 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id 4059 4060 static int bpf_link_get_fd_by_id(const union bpf_attr *attr) 4061 { 4062 struct bpf_link *link; 4063 u32 id = attr->link_id; 4064 int fd; 4065 4066 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID)) 4067 return -EINVAL; 4068 4069 if (!capable(CAP_SYS_ADMIN)) 4070 return -EPERM; 4071 4072 link = bpf_link_by_id(id); 4073 if (IS_ERR(link)) 4074 return PTR_ERR(link); 4075 4076 fd = bpf_link_new_fd(link); 4077 if (fd < 0) 4078 bpf_link_put(link); 4079 4080 return fd; 4081 } 4082 4083 DEFINE_MUTEX(bpf_stats_enabled_mutex); 4084 4085 static int bpf_stats_release(struct inode *inode, struct file *file) 4086 { 4087 mutex_lock(&bpf_stats_enabled_mutex); 4088 static_key_slow_dec(&bpf_stats_enabled_key.key); 4089 mutex_unlock(&bpf_stats_enabled_mutex); 4090 return 0; 4091 } 4092 4093 static const struct file_operations bpf_stats_fops = { 4094 .release = bpf_stats_release, 4095 }; 4096 4097 static int bpf_enable_runtime_stats(void) 4098 { 4099 int fd; 4100 4101 mutex_lock(&bpf_stats_enabled_mutex); 4102 4103 /* Set a very high limit to avoid overflow */ 4104 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) { 4105 mutex_unlock(&bpf_stats_enabled_mutex); 4106 return -EBUSY; 4107 } 4108 4109 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC); 4110 if (fd >= 0) 4111 static_key_slow_inc(&bpf_stats_enabled_key.key); 4112 4113 mutex_unlock(&bpf_stats_enabled_mutex); 4114 return fd; 4115 } 4116 4117 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type 4118 4119 static int bpf_enable_stats(union bpf_attr *attr) 4120 { 4121 4122 if (CHECK_ATTR(BPF_ENABLE_STATS)) 4123 return -EINVAL; 4124 4125 if (!capable(CAP_SYS_ADMIN)) 4126 return -EPERM; 4127 4128 switch (attr->enable_stats.type) { 4129 case BPF_STATS_RUN_TIME: 4130 return bpf_enable_runtime_stats(); 4131 default: 4132 break; 4133 } 4134 return -EINVAL; 4135 } 4136 4137 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags 4138 4139 static int bpf_iter_create(union bpf_attr *attr) 4140 { 4141 struct bpf_link *link; 4142 int err; 4143 4144 if (CHECK_ATTR(BPF_ITER_CREATE)) 4145 return -EINVAL; 4146 4147 if (attr->iter_create.flags) 4148 return -EINVAL; 4149 4150 link = bpf_link_get_from_fd(attr->iter_create.link_fd); 4151 if (IS_ERR(link)) 4152 return PTR_ERR(link); 4153 4154 err = bpf_iter_new_fd(link); 4155 bpf_link_put(link); 4156 4157 return err; 4158 } 4159 4160 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags 4161 4162 static int bpf_prog_bind_map(union bpf_attr *attr) 4163 { 4164 struct bpf_prog *prog; 4165 struct bpf_map *map; 4166 struct bpf_map **used_maps_old, **used_maps_new; 4167 int i, ret = 0; 4168 4169 if (CHECK_ATTR(BPF_PROG_BIND_MAP)) 4170 return -EINVAL; 4171 4172 if (attr->prog_bind_map.flags) 4173 return -EINVAL; 4174 4175 prog = bpf_prog_get(attr->prog_bind_map.prog_fd); 4176 if (IS_ERR(prog)) 4177 return PTR_ERR(prog); 4178 4179 map = bpf_map_get(attr->prog_bind_map.map_fd); 4180 if (IS_ERR(map)) { 4181 ret = PTR_ERR(map); 4182 goto out_prog_put; 4183 } 4184 4185 mutex_lock(&prog->aux->used_maps_mutex); 4186 4187 used_maps_old = prog->aux->used_maps; 4188 4189 for (i = 0; i < prog->aux->used_map_cnt; i++) 4190 if (used_maps_old[i] == map) 4191 goto out_unlock; 4192 4193 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1, 4194 sizeof(used_maps_new[0]), 4195 GFP_KERNEL); 4196 if (!used_maps_new) { 4197 ret = -ENOMEM; 4198 goto out_unlock; 4199 } 4200 4201 memcpy(used_maps_new, used_maps_old, 4202 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt); 4203 used_maps_new[prog->aux->used_map_cnt] = map; 4204 4205 prog->aux->used_map_cnt++; 4206 prog->aux->used_maps = used_maps_new; 4207 4208 kfree(used_maps_old); 4209 4210 out_unlock: 4211 mutex_unlock(&prog->aux->used_maps_mutex); 4212 4213 if (ret) 4214 bpf_map_put(map); 4215 out_prog_put: 4216 bpf_prog_put(prog); 4217 return ret; 4218 } 4219 4220 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) 4221 { 4222 union bpf_attr attr; 4223 int err; 4224 4225 if (sysctl_unprivileged_bpf_disabled && !bpf_capable()) 4226 return -EPERM; 4227 4228 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); 4229 if (err) 4230 return err; 4231 size = min_t(u32, size, sizeof(attr)); 4232 4233 /* copy attributes from user space, may be less than sizeof(bpf_attr) */ 4234 memset(&attr, 0, sizeof(attr)); 4235 if (copy_from_user(&attr, uattr, size) != 0) 4236 return -EFAULT; 4237 4238 err = security_bpf(cmd, &attr, size); 4239 if (err < 0) 4240 return err; 4241 4242 switch (cmd) { 4243 case BPF_MAP_CREATE: 4244 err = map_create(&attr); 4245 break; 4246 case BPF_MAP_LOOKUP_ELEM: 4247 err = map_lookup_elem(&attr); 4248 break; 4249 case BPF_MAP_UPDATE_ELEM: 4250 err = map_update_elem(&attr); 4251 break; 4252 case BPF_MAP_DELETE_ELEM: 4253 err = map_delete_elem(&attr); 4254 break; 4255 case BPF_MAP_GET_NEXT_KEY: 4256 err = map_get_next_key(&attr); 4257 break; 4258 case BPF_MAP_FREEZE: 4259 err = map_freeze(&attr); 4260 break; 4261 case BPF_PROG_LOAD: 4262 err = bpf_prog_load(&attr, uattr); 4263 break; 4264 case BPF_OBJ_PIN: 4265 err = bpf_obj_pin(&attr); 4266 break; 4267 case BPF_OBJ_GET: 4268 err = bpf_obj_get(&attr); 4269 break; 4270 case BPF_PROG_ATTACH: 4271 err = bpf_prog_attach(&attr); 4272 break; 4273 case BPF_PROG_DETACH: 4274 err = bpf_prog_detach(&attr); 4275 break; 4276 case BPF_PROG_QUERY: 4277 err = bpf_prog_query(&attr, uattr); 4278 break; 4279 case BPF_PROG_TEST_RUN: 4280 err = bpf_prog_test_run(&attr, uattr); 4281 break; 4282 case BPF_PROG_GET_NEXT_ID: 4283 err = bpf_obj_get_next_id(&attr, uattr, 4284 &prog_idr, &prog_idr_lock); 4285 break; 4286 case BPF_MAP_GET_NEXT_ID: 4287 err = bpf_obj_get_next_id(&attr, uattr, 4288 &map_idr, &map_idr_lock); 4289 break; 4290 case BPF_BTF_GET_NEXT_ID: 4291 err = bpf_obj_get_next_id(&attr, uattr, 4292 &btf_idr, &btf_idr_lock); 4293 break; 4294 case BPF_PROG_GET_FD_BY_ID: 4295 err = bpf_prog_get_fd_by_id(&attr); 4296 break; 4297 case BPF_MAP_GET_FD_BY_ID: 4298 err = bpf_map_get_fd_by_id(&attr); 4299 break; 4300 case BPF_OBJ_GET_INFO_BY_FD: 4301 err = bpf_obj_get_info_by_fd(&attr, uattr); 4302 break; 4303 case BPF_RAW_TRACEPOINT_OPEN: 4304 err = bpf_raw_tracepoint_open(&attr); 4305 break; 4306 case BPF_BTF_LOAD: 4307 err = bpf_btf_load(&attr); 4308 break; 4309 case BPF_BTF_GET_FD_BY_ID: 4310 err = bpf_btf_get_fd_by_id(&attr); 4311 break; 4312 case BPF_TASK_FD_QUERY: 4313 err = bpf_task_fd_query(&attr, uattr); 4314 break; 4315 case BPF_MAP_LOOKUP_AND_DELETE_ELEM: 4316 err = map_lookup_and_delete_elem(&attr); 4317 break; 4318 case BPF_MAP_LOOKUP_BATCH: 4319 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH); 4320 break; 4321 case BPF_MAP_LOOKUP_AND_DELETE_BATCH: 4322 err = bpf_map_do_batch(&attr, uattr, 4323 BPF_MAP_LOOKUP_AND_DELETE_BATCH); 4324 break; 4325 case BPF_MAP_UPDATE_BATCH: 4326 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH); 4327 break; 4328 case BPF_MAP_DELETE_BATCH: 4329 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH); 4330 break; 4331 case BPF_LINK_CREATE: 4332 err = link_create(&attr); 4333 break; 4334 case BPF_LINK_UPDATE: 4335 err = link_update(&attr); 4336 break; 4337 case BPF_LINK_GET_FD_BY_ID: 4338 err = bpf_link_get_fd_by_id(&attr); 4339 break; 4340 case BPF_LINK_GET_NEXT_ID: 4341 err = bpf_obj_get_next_id(&attr, uattr, 4342 &link_idr, &link_idr_lock); 4343 break; 4344 case BPF_ENABLE_STATS: 4345 err = bpf_enable_stats(&attr); 4346 break; 4347 case BPF_ITER_CREATE: 4348 err = bpf_iter_create(&attr); 4349 break; 4350 case BPF_LINK_DETACH: 4351 err = link_detach(&attr); 4352 break; 4353 case BPF_PROG_BIND_MAP: 4354 err = bpf_prog_bind_map(&attr); 4355 break; 4356 default: 4357 err = -EINVAL; 4358 break; 4359 } 4360 4361 return err; 4362 } 4363