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