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