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