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