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