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