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