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