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