1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com 3 */ 4 #include <crypto/sha2.h> 5 #include <linux/bpf.h> 6 #include <linux/bpf-cgroup.h> 7 #include <linux/bpf_trace.h> 8 #include <linux/bpf_lirc.h> 9 #include <linux/bpf_verifier.h> 10 #include <linux/bsearch.h> 11 #include <linux/btf.h> 12 #include <linux/hex.h> 13 #include <linux/syscalls.h> 14 #include <linux/slab.h> 15 #include <linux/sched/signal.h> 16 #include <linux/vmalloc.h> 17 #include <linux/mmzone.h> 18 #include <linux/anon_inodes.h> 19 #include <linux/fdtable.h> 20 #include <linux/file.h> 21 #include <linux/fs.h> 22 #include <linux/license.h> 23 #include <linux/filter.h> 24 #include <linux/kernel.h> 25 #include <linux/idr.h> 26 #include <linux/cred.h> 27 #include <linux/timekeeping.h> 28 #include <linux/ctype.h> 29 #include <linux/nospec.h> 30 #include <linux/audit.h> 31 #include <uapi/linux/btf.h> 32 #include <linux/pgtable.h> 33 #include <linux/bpf_lsm.h> 34 #include <linux/poll.h> 35 #include <linux/sort.h> 36 #include <linux/bpf-netns.h> 37 #include <linux/rcupdate_trace.h> 38 #include <linux/memcontrol.h> 39 #include <linux/trace_events.h> 40 #include <linux/tracepoint.h> 41 #include <linux/overflow.h> 42 #include <linux/cookie.h> 43 #include <linux/btf_ids.h> 44 45 #include <net/netfilter/nf_bpf_link.h> 46 #include <net/netkit.h> 47 #include <net/tcx.h> 48 49 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ 50 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ 51 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) 52 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY) 53 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) 54 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \ 55 IS_FD_HASH(map)) 56 57 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) 58 59 DEFINE_PER_CPU(int, bpf_prog_active); 60 DEFINE_COOKIE(bpf_map_cookie); 61 static DEFINE_IDR(prog_idr); 62 static DEFINE_SPINLOCK(prog_idr_lock); 63 static DEFINE_IDR(map_idr); 64 static DEFINE_SPINLOCK(map_idr_lock); 65 static DEFINE_IDR(link_idr); 66 static DEFINE_SPINLOCK(link_idr_lock); 67 68 int sysctl_unprivileged_bpf_disabled __read_mostly = 69 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0; 70 71 static const struct bpf_map_ops * const bpf_map_types[] = { 72 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) 73 #define BPF_MAP_TYPE(_id, _ops) \ 74 [_id] = &_ops, 75 #define BPF_LINK_TYPE(_id, _name) 76 #include <linux/bpf_types.h> 77 #undef BPF_PROG_TYPE 78 #undef BPF_MAP_TYPE 79 #undef BPF_LINK_TYPE 80 }; 81 82 /* 83 * If we're handed a bigger struct than we know of, ensure all the unknown bits 84 * are 0 - i.e. new user-space does not rely on any kernel feature extensions 85 * we don't know about yet. 86 * 87 * There is a ToCToU between this function call and the following 88 * copy_from_user() call. However, this is not a concern since this function is 89 * meant to be a future-proofing of bits. 90 */ 91 int bpf_check_uarg_tail_zero(bpfptr_t uaddr, 92 size_t expected_size, 93 size_t actual_size) 94 { 95 int res; 96 97 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ 98 return -E2BIG; 99 100 if (actual_size <= expected_size) 101 return 0; 102 103 if (uaddr.is_kernel) 104 res = memchr_inv(uaddr.kernel + expected_size, 0, 105 actual_size - expected_size) == NULL; 106 else 107 res = check_zeroed_user(uaddr.user + expected_size, 108 actual_size - expected_size); 109 if (res < 0) 110 return res; 111 return res ? 0 : -E2BIG; 112 } 113 114 const struct bpf_map_ops bpf_map_offload_ops = { 115 .map_meta_equal = bpf_map_meta_equal, 116 .map_alloc = bpf_map_offload_map_alloc, 117 .map_free = bpf_map_offload_map_free, 118 .map_check_btf = map_check_no_btf, 119 .map_mem_usage = bpf_map_offload_map_mem_usage, 120 }; 121 122 static void bpf_map_write_active_inc(struct bpf_map *map) 123 { 124 atomic64_inc(&map->writecnt); 125 } 126 127 static void bpf_map_write_active_dec(struct bpf_map *map) 128 { 129 atomic64_dec(&map->writecnt); 130 } 131 132 bool bpf_map_write_active(const struct bpf_map *map) 133 { 134 return atomic64_read(&map->writecnt) != 0; 135 } 136 137 static u32 bpf_map_value_size(const struct bpf_map *map, u64 flags) 138 { 139 if (flags & (BPF_F_CPU | BPF_F_ALL_CPUS)) 140 return map->value_size; 141 else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 142 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || 143 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || 144 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) 145 return round_up(map->value_size, 8) * num_possible_cpus(); 146 else if (IS_FD_MAP(map)) 147 return sizeof(u32); 148 else 149 return map->value_size; 150 } 151 152 static void maybe_wait_bpf_programs(struct bpf_map *map) 153 { 154 /* Wait for any running non-sleepable BPF programs to complete so that 155 * userspace, when we return to it, knows that all non-sleepable 156 * programs that could be running use the new map value. For sleepable 157 * BPF programs, synchronize_rcu_tasks_trace() should be used to wait 158 * for the completions of these programs, but considering the waiting 159 * time can be very long and userspace may think it will hang forever, 160 * so don't handle sleepable BPF programs now. 161 */ 162 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || 163 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) 164 synchronize_rcu_expedited(); 165 } 166 167 static void unpin_uptr_kaddr(void *kaddr) 168 { 169 if (kaddr) 170 unpin_user_page(virt_to_page(kaddr)); 171 } 172 173 static void __bpf_obj_unpin_uptrs(struct btf_record *rec, u32 cnt, void *obj) 174 { 175 const struct btf_field *field; 176 void **uptr_addr; 177 int i; 178 179 for (i = 0, field = rec->fields; i < cnt; i++, field++) { 180 if (field->type != BPF_UPTR) 181 continue; 182 183 uptr_addr = obj + field->offset; 184 unpin_uptr_kaddr(*uptr_addr); 185 } 186 } 187 188 static void bpf_obj_unpin_uptrs(struct btf_record *rec, void *obj) 189 { 190 if (!btf_record_has_field(rec, BPF_UPTR)) 191 return; 192 193 __bpf_obj_unpin_uptrs(rec, rec->cnt, obj); 194 } 195 196 static int bpf_obj_pin_uptrs(struct btf_record *rec, void *obj) 197 { 198 const struct btf_field *field; 199 const struct btf_type *t; 200 unsigned long start, end; 201 struct page *page; 202 void **uptr_addr; 203 int i, err; 204 205 if (!btf_record_has_field(rec, BPF_UPTR)) 206 return 0; 207 208 for (i = 0, field = rec->fields; i < rec->cnt; i++, field++) { 209 if (field->type != BPF_UPTR) 210 continue; 211 212 uptr_addr = obj + field->offset; 213 start = *(unsigned long *)uptr_addr; 214 if (!start) 215 continue; 216 217 t = btf_type_by_id(field->kptr.btf, field->kptr.btf_id); 218 /* t->size was checked for zero before */ 219 if (check_add_overflow(start, t->size - 1, &end)) { 220 err = -EFAULT; 221 goto unpin_all; 222 } 223 224 /* The uptr's struct cannot span across two pages */ 225 if ((start & PAGE_MASK) != (end & PAGE_MASK)) { 226 err = -EOPNOTSUPP; 227 goto unpin_all; 228 } 229 230 err = pin_user_pages_fast(start, 1, FOLL_LONGTERM | FOLL_WRITE, &page); 231 if (err != 1) 232 goto unpin_all; 233 234 if (PageHighMem(page)) { 235 err = -EOPNOTSUPP; 236 unpin_user_page(page); 237 goto unpin_all; 238 } 239 240 *uptr_addr = page_address(page) + offset_in_page(start); 241 } 242 243 return 0; 244 245 unpin_all: 246 __bpf_obj_unpin_uptrs(rec, i, obj); 247 return err; 248 } 249 250 static int bpf_map_update_value(struct bpf_map *map, struct file *map_file, 251 void *key, void *value, __u64 flags) 252 { 253 int err; 254 255 /* Need to create a kthread, thus must support schedule */ 256 if (bpf_map_is_offloaded(map)) { 257 return bpf_map_offload_update_elem(map, key, value, flags); 258 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP || 259 map->map_type == BPF_MAP_TYPE_ARENA || 260 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 261 return map->ops->map_update_elem(map, key, value, flags); 262 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH || 263 map->map_type == BPF_MAP_TYPE_SOCKMAP) { 264 return sock_map_update_elem_sys(map, key, value, flags); 265 } else if (IS_FD_PROG_ARRAY(map)) { 266 return bpf_fd_array_map_update_elem(map, map_file, key, value, 267 flags); 268 } 269 270 bpf_disable_instrumentation(); 271 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 272 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 273 err = bpf_percpu_hash_update(map, key, value, flags); 274 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 275 err = bpf_percpu_array_update(map, key, value, flags); 276 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { 277 err = bpf_percpu_cgroup_storage_update(map, key, value, 278 flags); 279 } else if (IS_FD_ARRAY(map)) { 280 err = bpf_fd_array_map_update_elem(map, map_file, key, value, 281 flags); 282 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { 283 err = bpf_fd_htab_map_update_elem(map, map_file, key, value, 284 flags); 285 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { 286 /* rcu_read_lock() is not needed */ 287 err = bpf_fd_reuseport_array_update_elem(map, key, value, 288 flags); 289 } else if (map->map_type == BPF_MAP_TYPE_QUEUE || 290 map->map_type == BPF_MAP_TYPE_STACK || 291 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) { 292 err = map->ops->map_push_elem(map, value, flags); 293 } else { 294 err = bpf_obj_pin_uptrs(map->record, value); 295 if (!err) { 296 rcu_read_lock(); 297 err = map->ops->map_update_elem(map, key, value, flags); 298 rcu_read_unlock(); 299 if (err) 300 bpf_obj_unpin_uptrs(map->record, value); 301 } 302 } 303 bpf_enable_instrumentation(); 304 305 return err; 306 } 307 308 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, 309 __u64 flags) 310 { 311 void *ptr; 312 int err; 313 314 if (bpf_map_is_offloaded(map)) 315 return bpf_map_offload_lookup_elem(map, key, value); 316 317 bpf_disable_instrumentation(); 318 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 319 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 320 err = bpf_percpu_hash_copy(map, key, value, flags); 321 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 322 err = bpf_percpu_array_copy(map, key, value, flags); 323 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { 324 err = bpf_percpu_cgroup_storage_copy(map, key, value, flags); 325 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { 326 err = bpf_stackmap_extract(map, key, value, false); 327 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) { 328 err = bpf_fd_array_map_lookup_elem(map, key, value); 329 } else if (IS_FD_HASH(map)) { 330 err = bpf_fd_htab_map_lookup_elem(map, key, value); 331 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { 332 err = bpf_fd_reuseport_array_lookup_elem(map, key, value); 333 } else if (map->map_type == BPF_MAP_TYPE_QUEUE || 334 map->map_type == BPF_MAP_TYPE_STACK || 335 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) { 336 err = map->ops->map_peek_elem(map, value); 337 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 338 /* struct_ops map requires directly updating "value" */ 339 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value); 340 } else { 341 rcu_read_lock(); 342 if (map->ops->map_lookup_elem_sys_only) 343 ptr = map->ops->map_lookup_elem_sys_only(map, key); 344 else 345 ptr = map->ops->map_lookup_elem(map, key); 346 if (IS_ERR(ptr)) { 347 err = PTR_ERR(ptr); 348 } else if (!ptr) { 349 err = -ENOENT; 350 } else { 351 err = 0; 352 if (flags & BPF_F_LOCK) 353 /* lock 'ptr' and copy everything but lock */ 354 copy_map_value_locked(map, value, ptr, true); 355 else 356 copy_map_value(map, value, ptr); 357 /* mask lock and timer, since value wasn't zero inited */ 358 check_and_init_map_value(map, value); 359 } 360 rcu_read_unlock(); 361 } 362 363 bpf_enable_instrumentation(); 364 365 return err; 366 } 367 368 /* Please, do not use this function outside from the map creation path 369 * (e.g. in map update path) without taking care of setting the active 370 * memory cgroup (see at bpf_map_kmalloc_node() for example). 371 */ 372 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable) 373 { 374 /* We really just want to fail instead of triggering OOM killer 375 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc, 376 * which is used for lower order allocation requests. 377 * 378 * It has been observed that higher order allocation requests done by 379 * vmalloc with __GFP_NORETRY being set might fail due to not trying 380 * to reclaim memory from the page cache, thus we set 381 * __GFP_RETRY_MAYFAIL to avoid such situations. 382 */ 383 384 gfp_t gfp = bpf_memcg_flags(__GFP_NOWARN | __GFP_ZERO); 385 unsigned int flags = 0; 386 unsigned long align = 1; 387 void *area; 388 389 if (size >= SIZE_MAX) 390 return NULL; 391 392 /* kmalloc()'ed memory can't be mmap()'ed */ 393 if (mmapable) { 394 BUG_ON(!PAGE_ALIGNED(size)); 395 align = SHMLBA; 396 flags = VM_USERMAP; 397 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { 398 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY, 399 numa_node); 400 if (area != NULL) 401 return area; 402 } 403 404 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, 405 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL, 406 flags, numa_node, __builtin_return_address(0)); 407 } 408 409 void *bpf_map_area_alloc(u64 size, int numa_node) 410 { 411 return __bpf_map_area_alloc(size, numa_node, false); 412 } 413 414 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node) 415 { 416 return __bpf_map_area_alloc(size, numa_node, true); 417 } 418 419 void bpf_map_area_free(void *area) 420 { 421 kvfree(area); 422 } 423 424 static u32 bpf_map_flags_retain_permanent(u32 flags) 425 { 426 /* Some map creation flags are not tied to the map object but 427 * rather to the map fd instead, so they have no meaning upon 428 * map object inspection since multiple file descriptors with 429 * different (access) properties can exist here. Thus, given 430 * this has zero meaning for the map itself, lets clear these 431 * from here. 432 */ 433 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY); 434 } 435 436 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) 437 { 438 map->map_type = attr->map_type; 439 map->key_size = attr->key_size; 440 map->value_size = attr->value_size; 441 map->max_entries = attr->max_entries; 442 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags); 443 map->numa_node = bpf_map_attr_numa_node(attr); 444 map->map_extra = attr->map_extra; 445 } 446 447 static int bpf_map_alloc_id(struct bpf_map *map) 448 { 449 int id; 450 451 idr_preload(GFP_KERNEL); 452 spin_lock_bh(&map_idr_lock); 453 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); 454 if (id > 0) 455 map->id = id; 456 spin_unlock_bh(&map_idr_lock); 457 idr_preload_end(); 458 459 if (WARN_ON_ONCE(!id)) 460 return -ENOSPC; 461 462 return id > 0 ? 0 : id; 463 } 464 465 void bpf_map_free_id(struct bpf_map *map) 466 { 467 unsigned long flags; 468 469 /* Offloaded maps are removed from the IDR store when their device 470 * disappears - even if someone holds an fd to them they are unusable, 471 * the memory is gone, all ops will fail; they are simply waiting for 472 * refcnt to drop to be freed. 473 */ 474 if (!map->id) 475 return; 476 477 spin_lock_irqsave(&map_idr_lock, flags); 478 479 idr_remove(&map_idr, map->id); 480 map->id = 0; 481 482 spin_unlock_irqrestore(&map_idr_lock, flags); 483 } 484 485 #ifdef CONFIG_MEMCG 486 static void bpf_map_save_memcg(struct bpf_map *map) 487 { 488 /* Currently if a map is created by a process belonging to the root 489 * memory cgroup, get_obj_cgroup_from_current() will return NULL. 490 * So we have to check map->objcg for being NULL each time it's 491 * being used. 492 */ 493 if (memcg_bpf_enabled()) 494 map->objcg = get_obj_cgroup_from_current(); 495 } 496 497 static void bpf_map_release_memcg(struct bpf_map *map) 498 { 499 if (map->objcg) 500 obj_cgroup_put(map->objcg); 501 } 502 503 static struct mem_cgroup *bpf_map_get_memcg(const struct bpf_map *map) 504 { 505 if (map->objcg) 506 return get_mem_cgroup_from_objcg(map->objcg); 507 508 return root_mem_cgroup; 509 } 510 511 void bpf_map_memcg_enter(const struct bpf_map *map, struct mem_cgroup **old_memcg, 512 struct mem_cgroup **new_memcg) 513 { 514 *new_memcg = bpf_map_get_memcg(map); 515 *old_memcg = set_active_memcg(*new_memcg); 516 } 517 518 void bpf_map_memcg_exit(struct mem_cgroup *old_memcg, 519 struct mem_cgroup *new_memcg) 520 { 521 set_active_memcg(old_memcg); 522 mem_cgroup_put(new_memcg); 523 } 524 525 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags, 526 int node) 527 { 528 struct mem_cgroup *memcg, *old_memcg; 529 void *ptr; 530 531 bpf_map_memcg_enter(map, &old_memcg, &memcg); 532 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node); 533 bpf_map_memcg_exit(old_memcg, memcg); 534 535 return ptr; 536 } 537 538 void *bpf_map_kmalloc_nolock(const struct bpf_map *map, size_t size, gfp_t flags, 539 int node) 540 { 541 struct mem_cgroup *memcg, *old_memcg; 542 void *ptr; 543 544 bpf_map_memcg_enter(map, &old_memcg, &memcg); 545 ptr = kmalloc_nolock(size, flags | __GFP_ACCOUNT, node); 546 bpf_map_memcg_exit(old_memcg, memcg); 547 548 return ptr; 549 } 550 551 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags) 552 { 553 struct mem_cgroup *memcg, *old_memcg; 554 void *ptr; 555 556 bpf_map_memcg_enter(map, &old_memcg, &memcg); 557 ptr = kzalloc(size, flags | __GFP_ACCOUNT); 558 bpf_map_memcg_exit(old_memcg, memcg); 559 560 return ptr; 561 } 562 563 void *bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size, 564 gfp_t flags) 565 { 566 struct mem_cgroup *memcg, *old_memcg; 567 void *ptr; 568 569 bpf_map_memcg_enter(map, &old_memcg, &memcg); 570 ptr = kvcalloc(n, size, flags | __GFP_ACCOUNT); 571 bpf_map_memcg_exit(old_memcg, memcg); 572 573 return ptr; 574 } 575 576 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, 577 size_t align, gfp_t flags) 578 { 579 struct mem_cgroup *memcg, *old_memcg; 580 void __percpu *ptr; 581 582 bpf_map_memcg_enter(map, &old_memcg, &memcg); 583 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT); 584 bpf_map_memcg_exit(old_memcg, memcg); 585 586 return ptr; 587 } 588 589 #else 590 static void bpf_map_save_memcg(struct bpf_map *map) 591 { 592 } 593 594 static void bpf_map_release_memcg(struct bpf_map *map) 595 { 596 } 597 #endif 598 599 static bool can_alloc_pages(void) 600 { 601 return preempt_count() == 0 && !irqs_disabled() && 602 !IS_ENABLED(CONFIG_PREEMPT_RT); 603 } 604 605 static struct page *__bpf_alloc_page(int nid) 606 { 607 if (!can_alloc_pages()) 608 return alloc_pages_nolock(__GFP_ACCOUNT, nid, 0); 609 610 return alloc_pages_node(nid, 611 GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT 612 | __GFP_NOWARN, 613 0); 614 } 615 616 int bpf_map_alloc_pages(const struct bpf_map *map, int nid, 617 unsigned long nr_pages, struct page **pages) 618 { 619 unsigned long i, j; 620 struct page *pg; 621 int ret = 0; 622 623 for (i = 0; i < nr_pages; i++) { 624 pg = __bpf_alloc_page(nid); 625 626 if (pg) { 627 pages[i] = pg; 628 continue; 629 } 630 for (j = 0; j < i; j++) 631 free_pages_nolock(pages[j], 0); 632 ret = -ENOMEM; 633 break; 634 } 635 636 return ret; 637 } 638 639 640 static int btf_field_cmp(const void *a, const void *b) 641 { 642 const struct btf_field *f1 = a, *f2 = b; 643 644 if (f1->offset < f2->offset) 645 return -1; 646 else if (f1->offset > f2->offset) 647 return 1; 648 return 0; 649 } 650 651 struct btf_field *btf_record_find(const struct btf_record *rec, u32 offset, 652 u32 field_mask) 653 { 654 struct btf_field *field; 655 656 if (IS_ERR_OR_NULL(rec) || !(rec->field_mask & field_mask)) 657 return NULL; 658 field = bsearch(&offset, rec->fields, rec->cnt, sizeof(rec->fields[0]), btf_field_cmp); 659 if (!field || !(field->type & field_mask)) 660 return NULL; 661 return field; 662 } 663 664 void btf_record_free(struct btf_record *rec) 665 { 666 int i; 667 668 if (IS_ERR_OR_NULL(rec)) 669 return; 670 for (i = 0; i < rec->cnt; i++) { 671 switch (rec->fields[i].type) { 672 case BPF_KPTR_UNREF: 673 case BPF_KPTR_REF: 674 case BPF_KPTR_PERCPU: 675 case BPF_UPTR: 676 if (rec->fields[i].kptr.module) 677 module_put(rec->fields[i].kptr.module); 678 if (btf_is_kernel(rec->fields[i].kptr.btf)) 679 btf_put(rec->fields[i].kptr.btf); 680 break; 681 case BPF_LIST_HEAD: 682 case BPF_LIST_NODE: 683 case BPF_RB_ROOT: 684 case BPF_RB_NODE: 685 case BPF_SPIN_LOCK: 686 case BPF_RES_SPIN_LOCK: 687 case BPF_TIMER: 688 case BPF_REFCOUNT: 689 case BPF_WORKQUEUE: 690 case BPF_TASK_WORK: 691 /* Nothing to release */ 692 break; 693 default: 694 WARN_ON_ONCE(1); 695 continue; 696 } 697 } 698 kfree(rec); 699 } 700 701 void bpf_map_free_record(struct bpf_map *map) 702 { 703 btf_record_free(map->record); 704 map->record = NULL; 705 } 706 707 struct btf_record *btf_record_dup(const struct btf_record *rec) 708 { 709 const struct btf_field *fields; 710 struct btf_record *new_rec; 711 int ret, size, i; 712 713 if (IS_ERR_OR_NULL(rec)) 714 return NULL; 715 size = struct_size(rec, fields, rec->cnt); 716 new_rec = kmemdup(rec, size, GFP_KERNEL | __GFP_NOWARN); 717 if (!new_rec) 718 return ERR_PTR(-ENOMEM); 719 /* Do a deep copy of the btf_record */ 720 fields = rec->fields; 721 new_rec->cnt = 0; 722 for (i = 0; i < rec->cnt; i++) { 723 switch (fields[i].type) { 724 case BPF_KPTR_UNREF: 725 case BPF_KPTR_REF: 726 case BPF_KPTR_PERCPU: 727 case BPF_UPTR: 728 if (btf_is_kernel(fields[i].kptr.btf)) 729 btf_get(fields[i].kptr.btf); 730 if (fields[i].kptr.module && !try_module_get(fields[i].kptr.module)) { 731 ret = -ENXIO; 732 goto free; 733 } 734 break; 735 case BPF_LIST_HEAD: 736 case BPF_LIST_NODE: 737 case BPF_RB_ROOT: 738 case BPF_RB_NODE: 739 case BPF_SPIN_LOCK: 740 case BPF_RES_SPIN_LOCK: 741 case BPF_TIMER: 742 case BPF_REFCOUNT: 743 case BPF_WORKQUEUE: 744 case BPF_TASK_WORK: 745 /* Nothing to acquire */ 746 break; 747 default: 748 ret = -EFAULT; 749 WARN_ON_ONCE(1); 750 goto free; 751 } 752 new_rec->cnt++; 753 } 754 return new_rec; 755 free: 756 btf_record_free(new_rec); 757 return ERR_PTR(ret); 758 } 759 760 bool btf_record_equal(const struct btf_record *rec_a, const struct btf_record *rec_b) 761 { 762 bool a_has_fields = !IS_ERR_OR_NULL(rec_a), b_has_fields = !IS_ERR_OR_NULL(rec_b); 763 int size; 764 765 if (!a_has_fields && !b_has_fields) 766 return true; 767 if (a_has_fields != b_has_fields) 768 return false; 769 if (rec_a->cnt != rec_b->cnt) 770 return false; 771 size = struct_size(rec_a, fields, rec_a->cnt); 772 /* btf_parse_fields uses kzalloc to allocate a btf_record, so unused 773 * members are zeroed out. So memcmp is safe to do without worrying 774 * about padding/unused fields. 775 * 776 * While spin_lock, timer, and kptr have no relation to map BTF, 777 * list_head metadata is specific to map BTF, the btf and value_rec 778 * members in particular. btf is the map BTF, while value_rec points to 779 * btf_record in that map BTF. 780 * 781 * So while by default, we don't rely on the map BTF (which the records 782 * were parsed from) matching for both records, which is not backwards 783 * compatible, in case list_head is part of it, we implicitly rely on 784 * that by way of depending on memcmp succeeding for it. 785 */ 786 return !memcmp(rec_a, rec_b, size); 787 } 788 789 void bpf_obj_free_timer(const struct btf_record *rec, void *obj) 790 { 791 if (WARN_ON_ONCE(!btf_record_has_field(rec, BPF_TIMER))) 792 return; 793 bpf_timer_cancel_and_free(obj + rec->timer_off); 794 } 795 796 void bpf_obj_free_workqueue(const struct btf_record *rec, void *obj) 797 { 798 if (WARN_ON_ONCE(!btf_record_has_field(rec, BPF_WORKQUEUE))) 799 return; 800 bpf_wq_cancel_and_free(obj + rec->wq_off); 801 } 802 803 void bpf_obj_free_task_work(const struct btf_record *rec, void *obj) 804 { 805 if (WARN_ON_ONCE(!btf_record_has_field(rec, BPF_TASK_WORK))) 806 return; 807 bpf_task_work_cancel_and_free(obj + rec->task_work_off); 808 } 809 810 void bpf_obj_cancel_fields(struct bpf_map *map, void *obj) 811 { 812 bpf_map_free_internal_structs(map, obj); 813 } 814 815 void bpf_obj_free_fields(const struct btf_record *rec, void *obj) 816 { 817 const struct btf_field *fields; 818 int i; 819 820 if (IS_ERR_OR_NULL(rec)) 821 return; 822 fields = rec->fields; 823 for (i = 0; i < rec->cnt; i++) { 824 struct btf_struct_meta *pointee_struct_meta; 825 const struct btf_field *field = &fields[i]; 826 void *field_ptr = obj + field->offset; 827 void *xchgd_field; 828 829 switch (fields[i].type) { 830 case BPF_SPIN_LOCK: 831 case BPF_RES_SPIN_LOCK: 832 break; 833 case BPF_TIMER: 834 bpf_timer_cancel_and_free(field_ptr); 835 break; 836 case BPF_WORKQUEUE: 837 bpf_wq_cancel_and_free(field_ptr); 838 break; 839 case BPF_TASK_WORK: 840 bpf_task_work_cancel_and_free(field_ptr); 841 break; 842 case BPF_KPTR_UNREF: 843 WRITE_ONCE(*(u64 *)field_ptr, 0); 844 break; 845 case BPF_KPTR_REF: 846 case BPF_KPTR_PERCPU: 847 xchgd_field = (void *)xchg((unsigned long *)field_ptr, 0); 848 if (!xchgd_field) 849 break; 850 851 if (!btf_is_kernel(field->kptr.btf)) { 852 pointee_struct_meta = btf_find_struct_meta(field->kptr.btf, 853 field->kptr.btf_id); 854 __bpf_obj_drop_impl(xchgd_field, pointee_struct_meta ? 855 pointee_struct_meta->record : NULL, 856 fields[i].type == BPF_KPTR_PERCPU); 857 } else { 858 field->kptr.dtor(xchgd_field); 859 } 860 break; 861 case BPF_UPTR: 862 /* The caller ensured that no one is using the uptr */ 863 unpin_uptr_kaddr(*(void **)field_ptr); 864 break; 865 case BPF_LIST_HEAD: 866 if (WARN_ON_ONCE(rec->spin_lock_off < 0)) 867 continue; 868 bpf_list_head_free(field, field_ptr, obj + rec->spin_lock_off); 869 break; 870 case BPF_RB_ROOT: 871 if (WARN_ON_ONCE(rec->spin_lock_off < 0)) 872 continue; 873 bpf_rb_root_free(field, field_ptr, obj + rec->spin_lock_off); 874 break; 875 case BPF_LIST_NODE: 876 case BPF_RB_NODE: 877 case BPF_REFCOUNT: 878 break; 879 default: 880 WARN_ON_ONCE(1); 881 continue; 882 } 883 } 884 } 885 886 static void bpf_map_free(struct bpf_map *map) 887 { 888 struct btf_record *rec = map->record; 889 struct btf *btf = map->btf; 890 891 /* implementation dependent freeing. Disabling migration to simplify 892 * the free of values or special fields allocated from bpf memory 893 * allocator. 894 */ 895 kfree(map->excl_prog_sha); 896 migrate_disable(); 897 map->ops->map_free(map); 898 migrate_enable(); 899 900 /* Delay freeing of btf_record for maps, as map_free 901 * callback usually needs access to them. It is better to do it here 902 * than require each callback to do the free itself manually. 903 * 904 * Note that the btf_record stashed in map->inner_map_meta->record was 905 * already freed using the map_free callback for map in map case which 906 * eventually calls bpf_map_free_meta, since inner_map_meta is only a 907 * template bpf_map struct used during verification. 908 */ 909 btf_record_free(rec); 910 /* Delay freeing of btf for maps, as map_free callback may need 911 * struct_meta info which will be freed with btf_put(). 912 */ 913 btf_put(btf); 914 } 915 916 /* called from workqueue */ 917 static void bpf_map_free_deferred(struct work_struct *work) 918 { 919 struct bpf_map *map = container_of(work, struct bpf_map, work); 920 921 security_bpf_map_free(map); 922 bpf_map_release_memcg(map); 923 bpf_map_owner_free(map); 924 bpf_map_free(map); 925 } 926 927 static void bpf_map_put_uref(struct bpf_map *map) 928 { 929 if (atomic64_dec_and_test(&map->usercnt)) { 930 if (map->ops->map_release_uref) 931 map->ops->map_release_uref(map); 932 } 933 } 934 935 static void bpf_map_free_in_work(struct bpf_map *map) 936 { 937 INIT_WORK(&map->work, bpf_map_free_deferred); 938 /* Avoid spawning kworkers, since they all might contend 939 * for the same mutex like slab_mutex. 940 */ 941 queue_work(system_dfl_wq, &map->work); 942 } 943 944 static void bpf_map_free_rcu_gp(struct rcu_head *rcu) 945 { 946 bpf_map_free_in_work(container_of(rcu, struct bpf_map, rcu)); 947 } 948 949 /* decrement map refcnt and schedule it for freeing via workqueue 950 * (underlying map implementation ops->map_free() might sleep) 951 */ 952 void bpf_map_put(struct bpf_map *map) 953 { 954 if (atomic64_dec_and_test(&map->refcnt)) { 955 /* bpf_map_free_id() must be called first */ 956 bpf_map_free_id(map); 957 958 WARN_ON_ONCE(atomic64_read(&map->sleepable_refcnt)); 959 /* RCU tasks trace grace period implies RCU grace period. */ 960 if (READ_ONCE(map->free_after_mult_rcu_gp)) 961 call_rcu_tasks_trace(&map->rcu, bpf_map_free_rcu_gp); 962 else if (READ_ONCE(map->free_after_rcu_gp)) 963 call_rcu(&map->rcu, bpf_map_free_rcu_gp); 964 else 965 bpf_map_free_in_work(map); 966 } 967 } 968 EXPORT_SYMBOL_GPL(bpf_map_put); 969 970 void bpf_map_put_with_uref(struct bpf_map *map) 971 { 972 bpf_map_put_uref(map); 973 bpf_map_put(map); 974 } 975 976 static int bpf_map_release(struct inode *inode, struct file *filp) 977 { 978 struct bpf_map *map = filp->private_data; 979 980 if (map->ops->map_release) 981 map->ops->map_release(map, filp); 982 983 bpf_map_put_with_uref(map); 984 return 0; 985 } 986 987 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) 988 { 989 fmode_t mode = fd_file(f)->f_mode; 990 991 /* Our file permissions may have been overridden by global 992 * map permissions facing syscall side. 993 */ 994 if (READ_ONCE(map->frozen)) 995 mode &= ~FMODE_CAN_WRITE; 996 return mode; 997 } 998 999 #ifdef CONFIG_PROC_FS 1000 /* Show the memory usage of a bpf map */ 1001 static u64 bpf_map_memory_usage(const struct bpf_map *map) 1002 { 1003 return map->ops->map_mem_usage(map); 1004 } 1005 1006 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) 1007 { 1008 struct bpf_map *map = filp->private_data; 1009 u32 type = 0, jited = 0; 1010 1011 spin_lock(&map->owner_lock); 1012 if (map->owner) { 1013 type = map->owner->type; 1014 jited = map->owner->jited; 1015 } 1016 spin_unlock(&map->owner_lock); 1017 1018 seq_printf(m, 1019 "map_type:\t%u\n" 1020 "key_size:\t%u\n" 1021 "value_size:\t%u\n" 1022 "max_entries:\t%u\n" 1023 "map_flags:\t%#x\n" 1024 "map_extra:\t%#llx\n" 1025 "memlock:\t%llu\n" 1026 "map_id:\t%u\n" 1027 "frozen:\t%u\n", 1028 map->map_type, 1029 map->key_size, 1030 map->value_size, 1031 map->max_entries, 1032 map->map_flags, 1033 (unsigned long long)map->map_extra, 1034 bpf_map_memory_usage(map), 1035 map->id, 1036 READ_ONCE(map->frozen)); 1037 if (type) { 1038 seq_printf(m, "owner_prog_type:\t%u\n", type); 1039 seq_printf(m, "owner_jited:\t%u\n", jited); 1040 } 1041 } 1042 #endif 1043 1044 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, 1045 loff_t *ppos) 1046 { 1047 /* We need this handler such that alloc_file() enables 1048 * f_mode with FMODE_CAN_READ. 1049 */ 1050 return -EINVAL; 1051 } 1052 1053 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, 1054 size_t siz, loff_t *ppos) 1055 { 1056 /* We need this handler such that alloc_file() enables 1057 * f_mode with FMODE_CAN_WRITE. 1058 */ 1059 return -EINVAL; 1060 } 1061 1062 /* called for any extra memory-mapped regions (except initial) */ 1063 static void bpf_map_mmap_open(struct vm_area_struct *vma) 1064 { 1065 struct bpf_map *map = vma->vm_file->private_data; 1066 1067 if (vma->vm_flags & VM_MAYWRITE) 1068 bpf_map_write_active_inc(map); 1069 } 1070 1071 /* called for all unmapped memory region (including initial) */ 1072 static void bpf_map_mmap_close(struct vm_area_struct *vma) 1073 { 1074 struct bpf_map *map = vma->vm_file->private_data; 1075 1076 if (vma->vm_flags & VM_MAYWRITE) 1077 bpf_map_write_active_dec(map); 1078 } 1079 1080 static const struct vm_operations_struct bpf_map_default_vmops = { 1081 .open = bpf_map_mmap_open, 1082 .close = bpf_map_mmap_close, 1083 }; 1084 1085 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma) 1086 { 1087 struct bpf_map *map = filp->private_data; 1088 int err = 0; 1089 1090 if (!map->ops->map_mmap || !IS_ERR_OR_NULL(map->record)) 1091 return -ENOTSUPP; 1092 1093 if (!(vma->vm_flags & VM_SHARED)) 1094 return -EINVAL; 1095 1096 mutex_lock(&map->freeze_mutex); 1097 1098 if (vma->vm_flags & VM_WRITE) { 1099 if (map->frozen) { 1100 err = -EPERM; 1101 goto out; 1102 } 1103 /* map is meant to be read-only, so do not allow mapping as 1104 * writable, because it's possible to leak a writable page 1105 * reference and allows user-space to still modify it after 1106 * freezing, while verifier will assume contents do not change 1107 */ 1108 if (map->map_flags & BPF_F_RDONLY_PROG) { 1109 err = -EACCES; 1110 goto out; 1111 } 1112 bpf_map_write_active_inc(map); 1113 } 1114 out: 1115 mutex_unlock(&map->freeze_mutex); 1116 if (err) 1117 return err; 1118 1119 /* set default open/close callbacks */ 1120 vma->vm_ops = &bpf_map_default_vmops; 1121 vma->vm_private_data = map; 1122 vm_flags_clear(vma, VM_MAYEXEC); 1123 /* If mapping is read-only, then disallow potentially re-mapping with 1124 * PROT_WRITE by dropping VM_MAYWRITE flag. This VM_MAYWRITE clearing 1125 * means that as far as BPF map's memory-mapped VMAs are concerned, 1126 * VM_WRITE and VM_MAYWRITE and equivalent, if one of them is set, 1127 * both should be set, so we can forget about VM_MAYWRITE and always 1128 * check just VM_WRITE 1129 */ 1130 if (!(vma->vm_flags & VM_WRITE)) 1131 vm_flags_clear(vma, VM_MAYWRITE); 1132 1133 err = map->ops->map_mmap(map, vma); 1134 if (err) { 1135 if (vma->vm_flags & VM_WRITE) 1136 bpf_map_write_active_dec(map); 1137 } 1138 1139 return err; 1140 } 1141 1142 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts) 1143 { 1144 struct bpf_map *map = filp->private_data; 1145 1146 if (map->ops->map_poll) 1147 return map->ops->map_poll(map, filp, pts); 1148 1149 return EPOLLERR; 1150 } 1151 1152 static unsigned long bpf_get_unmapped_area(struct file *filp, unsigned long addr, 1153 unsigned long len, unsigned long pgoff, 1154 unsigned long flags) 1155 { 1156 struct bpf_map *map = filp->private_data; 1157 1158 if (map->ops->map_get_unmapped_area) 1159 return map->ops->map_get_unmapped_area(filp, addr, len, pgoff, flags); 1160 #ifdef CONFIG_MMU 1161 return mm_get_unmapped_area(filp, addr, len, pgoff, flags); 1162 #else 1163 return addr; 1164 #endif 1165 } 1166 1167 const struct file_operations bpf_map_fops = { 1168 #ifdef CONFIG_PROC_FS 1169 .show_fdinfo = bpf_map_show_fdinfo, 1170 #endif 1171 .release = bpf_map_release, 1172 .read = bpf_dummy_read, 1173 .write = bpf_dummy_write, 1174 .mmap = bpf_map_mmap, 1175 .poll = bpf_map_poll, 1176 .get_unmapped_area = bpf_get_unmapped_area, 1177 }; 1178 1179 int bpf_map_new_fd(struct bpf_map *map, int flags) 1180 { 1181 int ret; 1182 1183 ret = security_bpf_map(map, OPEN_FMODE(flags)); 1184 if (ret < 0) 1185 return ret; 1186 1187 return anon_inode_getfd("bpf-map", &bpf_map_fops, map, 1188 flags | O_CLOEXEC); 1189 } 1190 1191 int bpf_get_file_flag(int flags) 1192 { 1193 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) 1194 return -EINVAL; 1195 if (flags & BPF_F_RDONLY) 1196 return O_RDONLY; 1197 if (flags & BPF_F_WRONLY) 1198 return O_WRONLY; 1199 return O_RDWR; 1200 } 1201 1202 /* helper macro to check that unused fields 'union bpf_attr' are zero */ 1203 #define CHECK_ATTR(CMD) \ 1204 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ 1205 sizeof(attr->CMD##_LAST_FIELD), 0, \ 1206 sizeof(*attr) - \ 1207 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ 1208 sizeof(attr->CMD##_LAST_FIELD)) != NULL 1209 1210 /* dst and src must have at least "size" number of bytes. 1211 * Return strlen on success and < 0 on error. 1212 */ 1213 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size) 1214 { 1215 const char *end = src + size; 1216 const char *orig_src = src; 1217 1218 memset(dst, 0, size); 1219 /* Copy all isalnum(), '_' and '.' chars. */ 1220 while (src < end && *src) { 1221 if (!isalnum(*src) && 1222 *src != '_' && *src != '.') 1223 return -EINVAL; 1224 *dst++ = *src++; 1225 } 1226 1227 /* No '\0' found in "size" number of bytes */ 1228 if (src == end) 1229 return -EINVAL; 1230 1231 return src - orig_src; 1232 } 1233 EXPORT_SYMBOL_GPL(bpf_obj_name_cpy); 1234 1235 int map_check_no_btf(struct bpf_map *map, 1236 const struct btf *btf, 1237 const struct btf_type *key_type, 1238 const struct btf_type *value_type) 1239 { 1240 return -ENOTSUPP; 1241 } 1242 1243 static int map_check_btf(struct bpf_map *map, struct bpf_token *token, 1244 const struct btf *btf, u32 btf_key_id, u32 btf_value_id) 1245 { 1246 const struct btf_type *key_type, *value_type; 1247 u32 key_size, value_size; 1248 int ret = 0; 1249 1250 /* Some maps allow key to be unspecified. */ 1251 if (btf_key_id) { 1252 key_type = btf_type_id_size(btf, &btf_key_id, &key_size); 1253 if (!key_type || key_size != map->key_size) 1254 return -EINVAL; 1255 } else { 1256 key_type = btf_type_by_id(btf, 0); 1257 if (!map->ops->map_check_btf) 1258 return -EINVAL; 1259 } 1260 1261 value_type = btf_type_id_size(btf, &btf_value_id, &value_size); 1262 if (!value_type || value_size != map->value_size) 1263 return -EINVAL; 1264 1265 map->record = btf_parse_fields(btf, value_type, 1266 BPF_SPIN_LOCK | BPF_RES_SPIN_LOCK | BPF_TIMER | BPF_KPTR | BPF_LIST_HEAD | 1267 BPF_RB_ROOT | BPF_REFCOUNT | BPF_WORKQUEUE | BPF_UPTR | 1268 BPF_TASK_WORK, 1269 map->value_size); 1270 if (!IS_ERR_OR_NULL(map->record)) { 1271 int i; 1272 1273 if (!bpf_token_capable(token, CAP_BPF)) { 1274 ret = -EPERM; 1275 goto free_map_tab; 1276 } 1277 if (map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) { 1278 ret = -EACCES; 1279 goto free_map_tab; 1280 } 1281 for (i = 0; i < sizeof(map->record->field_mask) * 8; i++) { 1282 switch (map->record->field_mask & (1 << i)) { 1283 case 0: 1284 continue; 1285 case BPF_SPIN_LOCK: 1286 case BPF_RES_SPIN_LOCK: 1287 if (map->map_type != BPF_MAP_TYPE_HASH && 1288 map->map_type != BPF_MAP_TYPE_RHASH && 1289 map->map_type != BPF_MAP_TYPE_ARRAY && 1290 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && 1291 map->map_type != BPF_MAP_TYPE_SK_STORAGE && 1292 map->map_type != BPF_MAP_TYPE_INODE_STORAGE && 1293 map->map_type != BPF_MAP_TYPE_TASK_STORAGE && 1294 map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) { 1295 ret = -EOPNOTSUPP; 1296 goto free_map_tab; 1297 } 1298 break; 1299 case BPF_TIMER: 1300 case BPF_WORKQUEUE: 1301 case BPF_TASK_WORK: 1302 if (map->map_type != BPF_MAP_TYPE_HASH && 1303 map->map_type != BPF_MAP_TYPE_RHASH && 1304 map->map_type != BPF_MAP_TYPE_LRU_HASH && 1305 map->map_type != BPF_MAP_TYPE_ARRAY) { 1306 ret = -EOPNOTSUPP; 1307 goto free_map_tab; 1308 } 1309 break; 1310 case BPF_KPTR_UNREF: 1311 case BPF_KPTR_REF: 1312 case BPF_KPTR_PERCPU: 1313 case BPF_REFCOUNT: 1314 if (map->map_type != BPF_MAP_TYPE_HASH && 1315 map->map_type != BPF_MAP_TYPE_RHASH && 1316 map->map_type != BPF_MAP_TYPE_PERCPU_HASH && 1317 map->map_type != BPF_MAP_TYPE_LRU_HASH && 1318 map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH && 1319 map->map_type != BPF_MAP_TYPE_ARRAY && 1320 map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY && 1321 map->map_type != BPF_MAP_TYPE_SK_STORAGE && 1322 map->map_type != BPF_MAP_TYPE_INODE_STORAGE && 1323 map->map_type != BPF_MAP_TYPE_TASK_STORAGE && 1324 map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) { 1325 ret = -EOPNOTSUPP; 1326 goto free_map_tab; 1327 } 1328 break; 1329 case BPF_UPTR: 1330 if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) { 1331 ret = -EOPNOTSUPP; 1332 goto free_map_tab; 1333 } 1334 break; 1335 case BPF_LIST_HEAD: 1336 case BPF_RB_ROOT: 1337 if (map->map_type != BPF_MAP_TYPE_HASH && 1338 map->map_type != BPF_MAP_TYPE_LRU_HASH && 1339 map->map_type != BPF_MAP_TYPE_ARRAY) { 1340 ret = -EOPNOTSUPP; 1341 goto free_map_tab; 1342 } 1343 break; 1344 default: 1345 /* Fail if map_type checks are missing for a field type */ 1346 ret = -EOPNOTSUPP; 1347 goto free_map_tab; 1348 } 1349 } 1350 } 1351 1352 ret = btf_check_and_fixup_fields(btf, map->record); 1353 if (ret < 0) 1354 goto free_map_tab; 1355 1356 if (map->ops->map_check_btf) { 1357 ret = map->ops->map_check_btf(map, btf, key_type, value_type); 1358 if (ret < 0) 1359 goto free_map_tab; 1360 } 1361 1362 return ret; 1363 free_map_tab: 1364 bpf_map_free_record(map); 1365 return ret; 1366 } 1367 1368 #define BPF_MAP_CREATE_LAST_FIELD excl_prog_hash_size 1369 /* called via syscall */ 1370 static int map_create_alloc(union bpf_attr *attr, bpfptr_t uattr, struct bpf_verifier_log *log, 1371 struct bpf_map **mapp, struct bpf_token **tokenp) 1372 { 1373 const struct bpf_map_ops *ops; 1374 struct bpf_token *token = NULL; 1375 int numa_node = bpf_map_attr_numa_node(attr); 1376 u32 map_type = attr->map_type; 1377 struct bpf_map *map; 1378 bool token_flag; 1379 int err; 1380 1381 err = CHECK_ATTR(BPF_MAP_CREATE); 1382 if (err) { 1383 bpf_log(log, "Invalid attr.\n"); 1384 return -EINVAL; 1385 } 1386 1387 /* check BPF_F_TOKEN_FD flag, remember if it's set, and then clear it 1388 * to avoid per-map type checks tripping on unknown flag 1389 */ 1390 token_flag = attr->map_flags & BPF_F_TOKEN_FD; 1391 attr->map_flags &= ~BPF_F_TOKEN_FD; 1392 1393 if (attr->btf_vmlinux_value_type_id) { 1394 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS) { 1395 bpf_log(log, "btf_vmlinux_value_type_id can only be used with struct_ops maps.\n"); 1396 return -EINVAL; 1397 } 1398 if (attr->btf_key_type_id || attr->btf_value_type_id) { 1399 bpf_log(log, "btf_vmlinux_value_type_id is mutually exclusive with btf_key_type_id and btf_value_type_id.\n"); 1400 return -EINVAL; 1401 } 1402 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) { 1403 bpf_log(log, "Invalid btf_value_type_id.\n"); 1404 return -EINVAL; 1405 } 1406 1407 if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER && 1408 attr->map_type != BPF_MAP_TYPE_ARENA && 1409 attr->map_type != BPF_MAP_TYPE_RHASH && 1410 attr->map_extra != 0) { 1411 bpf_log(log, "Invalid map_extra.\n"); 1412 return -EINVAL; 1413 } 1414 1415 if (numa_node != NUMA_NO_NODE && 1416 ((unsigned int)numa_node >= nr_node_ids || 1417 !node_online(numa_node))) { 1418 bpf_log(log, "Invalid numa_node.\n"); 1419 return -EINVAL; 1420 } 1421 1422 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ 1423 map_type = attr->map_type; 1424 if (map_type >= ARRAY_SIZE(bpf_map_types)) { 1425 bpf_log(log, "Invalid map_type.\n"); 1426 return -EINVAL; 1427 } 1428 map_type = array_index_nospec(map_type, ARRAY_SIZE(bpf_map_types)); 1429 ops = bpf_map_types[map_type]; 1430 if (!ops) 1431 return -EINVAL; 1432 1433 if (ops->map_alloc_check) { 1434 err = ops->map_alloc_check(attr); 1435 if (err) 1436 return err; 1437 } 1438 if (attr->map_ifindex) 1439 ops = &bpf_map_offload_ops; 1440 if (!ops->map_mem_usage) 1441 return -EINVAL; 1442 1443 if (token_flag) { 1444 token = bpf_token_get_from_fd(attr->map_token_fd); 1445 if (IS_ERR(token)) { 1446 bpf_log(log, "Invalid map_token_fd.\n"); 1447 return PTR_ERR(token); 1448 } 1449 1450 /* if current token doesn't grant map creation permissions, 1451 * then we can't use this token, so ignore it and rely on 1452 * system-wide capabilities checks 1453 */ 1454 if (!bpf_token_allow_cmd(token, BPF_MAP_CREATE) || 1455 !bpf_token_allow_map_type(token, attr->map_type)) { 1456 bpf_token_put(token); 1457 token = NULL; 1458 } 1459 } 1460 1461 err = -EPERM; 1462 1463 /* Intent here is for unprivileged_bpf_disabled to block BPF map 1464 * creation for unprivileged users; other actions depend 1465 * on fd availability and access to bpffs, so are dependent on 1466 * object creation success. Even with unprivileged BPF disabled, 1467 * capability checks are still carried out. 1468 */ 1469 if (sysctl_unprivileged_bpf_disabled && !bpf_token_capable(token, CAP_BPF)) 1470 goto put_token; 1471 1472 /* check privileged map type permissions */ 1473 switch (map_type) { 1474 case BPF_MAP_TYPE_ARRAY: 1475 case BPF_MAP_TYPE_PERCPU_ARRAY: 1476 case BPF_MAP_TYPE_PROG_ARRAY: 1477 case BPF_MAP_TYPE_PERF_EVENT_ARRAY: 1478 case BPF_MAP_TYPE_CGROUP_ARRAY: 1479 case BPF_MAP_TYPE_ARRAY_OF_MAPS: 1480 case BPF_MAP_TYPE_HASH: 1481 case BPF_MAP_TYPE_RHASH: 1482 case BPF_MAP_TYPE_PERCPU_HASH: 1483 case BPF_MAP_TYPE_HASH_OF_MAPS: 1484 case BPF_MAP_TYPE_RINGBUF: 1485 case BPF_MAP_TYPE_USER_RINGBUF: 1486 case BPF_MAP_TYPE_CGROUP_STORAGE: 1487 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: 1488 /* unprivileged */ 1489 break; 1490 case BPF_MAP_TYPE_SK_STORAGE: 1491 case BPF_MAP_TYPE_INODE_STORAGE: 1492 case BPF_MAP_TYPE_TASK_STORAGE: 1493 case BPF_MAP_TYPE_CGRP_STORAGE: 1494 case BPF_MAP_TYPE_BLOOM_FILTER: 1495 case BPF_MAP_TYPE_LPM_TRIE: 1496 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: 1497 case BPF_MAP_TYPE_STACK_TRACE: 1498 case BPF_MAP_TYPE_QUEUE: 1499 case BPF_MAP_TYPE_STACK: 1500 case BPF_MAP_TYPE_LRU_HASH: 1501 case BPF_MAP_TYPE_LRU_PERCPU_HASH: 1502 case BPF_MAP_TYPE_STRUCT_OPS: 1503 case BPF_MAP_TYPE_CPUMAP: 1504 case BPF_MAP_TYPE_ARENA: 1505 case BPF_MAP_TYPE_INSN_ARRAY: 1506 if (!bpf_token_capable(token, CAP_BPF)) 1507 goto put_token; 1508 break; 1509 case BPF_MAP_TYPE_SOCKMAP: 1510 case BPF_MAP_TYPE_SOCKHASH: 1511 case BPF_MAP_TYPE_DEVMAP: 1512 case BPF_MAP_TYPE_DEVMAP_HASH: 1513 case BPF_MAP_TYPE_XSKMAP: 1514 if (!bpf_token_capable(token, CAP_NET_ADMIN)) 1515 goto put_token; 1516 break; 1517 default: 1518 WARN(1, "unsupported map type %d", map_type); 1519 goto put_token; 1520 } 1521 1522 map = ops->map_alloc(attr); 1523 if (IS_ERR(map)) { 1524 err = PTR_ERR(map); 1525 goto put_token; 1526 } 1527 map->ops = ops; 1528 map->map_type = map_type; 1529 1530 err = bpf_obj_name_cpy(map->name, attr->map_name, 1531 sizeof(attr->map_name)); 1532 if (err < 0) { 1533 bpf_log(log, "Invalid map_name.\n"); 1534 goto free_map; 1535 } 1536 1537 preempt_disable(); 1538 map->cookie = gen_cookie_next(&bpf_map_cookie); 1539 preempt_enable(); 1540 1541 atomic64_set(&map->refcnt, 1); 1542 atomic64_set(&map->usercnt, 1); 1543 mutex_init(&map->freeze_mutex); 1544 spin_lock_init(&map->owner_lock); 1545 1546 if (attr->btf_key_type_id || attr->btf_value_type_id || 1547 /* Even the map's value is a kernel's struct, 1548 * the bpf_prog.o must have BTF to begin with 1549 * to figure out the corresponding kernel's 1550 * counter part. Thus, attr->btf_fd has 1551 * to be valid also. 1552 */ 1553 attr->btf_vmlinux_value_type_id) { 1554 struct btf *btf; 1555 1556 btf = btf_get_by_fd(attr->btf_fd); 1557 if (IS_ERR(btf)) { 1558 bpf_log(log, "Invalid btf_fd.\n"); 1559 err = PTR_ERR(btf); 1560 goto free_map; 1561 } 1562 if (btf_is_kernel(btf)) { 1563 btf_put(btf); 1564 err = -EACCES; 1565 goto free_map; 1566 } 1567 map->btf = btf; 1568 1569 if (attr->btf_value_type_id) { 1570 err = map_check_btf(map, token, btf, attr->btf_key_type_id, 1571 attr->btf_value_type_id); 1572 if (err) 1573 goto free_map; 1574 } 1575 1576 map->btf_key_type_id = attr->btf_key_type_id; 1577 map->btf_value_type_id = attr->btf_value_type_id; 1578 map->btf_vmlinux_value_type_id = 1579 attr->btf_vmlinux_value_type_id; 1580 } 1581 1582 if (attr->excl_prog_hash) { 1583 bpfptr_t uprog_hash = make_bpfptr(attr->excl_prog_hash, uattr.is_kernel); 1584 1585 if (attr->excl_prog_hash_size != SHA256_DIGEST_SIZE) { 1586 bpf_log(log, "Invalid excl_prog_hash_size.\n"); 1587 err = -EINVAL; 1588 goto free_map; 1589 } 1590 1591 map->excl_prog_sha = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL); 1592 if (!map->excl_prog_sha) { 1593 err = -ENOMEM; 1594 goto free_map; 1595 } 1596 1597 if (copy_from_bpfptr(map->excl_prog_sha, uprog_hash, SHA256_DIGEST_SIZE)) { 1598 err = -EFAULT; 1599 goto free_map; 1600 } 1601 } else if (attr->excl_prog_hash_size) { 1602 bpf_log(log, "Invalid excl_prog_hash_size.\n"); 1603 err = -EINVAL; 1604 goto free_map; 1605 } 1606 1607 *mapp = map; 1608 *tokenp = token; 1609 return 0; 1610 1611 free_map: 1612 bpf_map_free(map); 1613 put_token: 1614 bpf_token_put(token); 1615 return err; 1616 } 1617 1618 static int map_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_attr *attr_common, 1619 bpfptr_t uattr_common, u32 size_common) 1620 { 1621 struct bpf_token *token = NULL; 1622 struct bpf_verifier_log *log; 1623 struct bpf_log_attr attr_log; 1624 struct bpf_map *map = NULL; 1625 int err, ret; 1626 int f_flags; 1627 1628 log = bpf_log_attr_create_vlog(&attr_log, attr_common, uattr_common, size_common); 1629 if (IS_ERR(log)) 1630 return PTR_ERR(log); 1631 1632 err = map_create_alloc(attr, uattr, log, &map, &token); 1633 1634 /* preserve original error even if log finalization is successful */ 1635 ret = bpf_log_attr_finalize(&attr_log, log); 1636 if (ret) 1637 err = ret; 1638 1639 kfree(log); 1640 1641 if (err) 1642 goto free_map; 1643 1644 f_flags = bpf_get_file_flag(attr->map_flags); 1645 if (f_flags < 0) { 1646 err = f_flags; 1647 goto free_map; 1648 } 1649 1650 err = security_bpf_map_create(map, attr, token, uattr.is_kernel); 1651 if (err) 1652 goto free_map; 1653 1654 err = bpf_map_alloc_id(map); 1655 if (err) 1656 goto free_map_sec; 1657 1658 bpf_map_save_memcg(map); 1659 bpf_token_put(token); 1660 1661 err = bpf_map_new_fd(map, f_flags); 1662 if (err < 0) { 1663 /* failed to allocate fd. 1664 * bpf_map_put_with_uref() is needed because the above 1665 * bpf_map_alloc_id() has published the map 1666 * to the userspace and the userspace may 1667 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. 1668 */ 1669 bpf_map_put_with_uref(map); 1670 return err; 1671 } 1672 1673 return err; 1674 1675 free_map_sec: 1676 security_bpf_map_free(map); 1677 free_map: 1678 if (map) 1679 bpf_map_free(map); 1680 bpf_token_put(token); 1681 return err; 1682 } 1683 1684 void bpf_map_inc(struct bpf_map *map) 1685 { 1686 atomic64_inc(&map->refcnt); 1687 } 1688 EXPORT_SYMBOL_GPL(bpf_map_inc); 1689 1690 void bpf_map_inc_with_uref(struct bpf_map *map) 1691 { 1692 atomic64_inc(&map->refcnt); 1693 atomic64_inc(&map->usercnt); 1694 } 1695 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref); 1696 1697 struct bpf_map *bpf_map_get(u32 ufd) 1698 { 1699 CLASS(fd, f)(ufd); 1700 struct bpf_map *map = __bpf_map_get(f); 1701 1702 if (!IS_ERR(map)) 1703 bpf_map_inc(map); 1704 1705 return map; 1706 } 1707 EXPORT_SYMBOL_NS(bpf_map_get, "BPF_INTERNAL"); 1708 1709 struct bpf_map *bpf_map_get_with_uref(u32 ufd) 1710 { 1711 CLASS(fd, f)(ufd); 1712 struct bpf_map *map = __bpf_map_get(f); 1713 1714 if (!IS_ERR(map)) 1715 bpf_map_inc_with_uref(map); 1716 1717 return map; 1718 } 1719 1720 /* map_idr_lock should have been held or the map should have been 1721 * protected by rcu read lock. 1722 */ 1723 struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref) 1724 { 1725 int refold; 1726 1727 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0); 1728 if (!refold) 1729 return ERR_PTR(-ENOENT); 1730 if (uref) 1731 atomic64_inc(&map->usercnt); 1732 1733 return map; 1734 } 1735 1736 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map) 1737 { 1738 lockdep_assert(rcu_read_lock_held()); 1739 return __bpf_map_inc_not_zero(map, false); 1740 } 1741 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero); 1742 1743 int __weak bpf_stackmap_extract(struct bpf_map *map, void *key, void *value, 1744 bool delete) 1745 { 1746 return -ENOTSUPP; 1747 } 1748 1749 static void *__bpf_copy_key(void __user *ukey, u64 key_size) 1750 { 1751 if (key_size) 1752 return vmemdup_user(ukey, key_size); 1753 1754 if (ukey) 1755 return ERR_PTR(-EINVAL); 1756 1757 return NULL; 1758 } 1759 1760 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size) 1761 { 1762 if (key_size) 1763 return kvmemdup_bpfptr(ukey, key_size); 1764 1765 if (!bpfptr_is_null(ukey)) 1766 return ERR_PTR(-EINVAL); 1767 1768 return NULL; 1769 } 1770 1771 /* last field in 'union bpf_attr' used by this command */ 1772 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags 1773 1774 static int map_lookup_elem(union bpf_attr *attr) 1775 { 1776 void __user *ukey = u64_to_user_ptr(attr->key); 1777 void __user *uvalue = u64_to_user_ptr(attr->value); 1778 struct bpf_map *map; 1779 void *key, *value; 1780 u32 value_size; 1781 int err; 1782 1783 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) 1784 return -EINVAL; 1785 1786 CLASS(fd, f)(attr->map_fd); 1787 map = __bpf_map_get(f); 1788 if (IS_ERR(map)) 1789 return PTR_ERR(map); 1790 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) 1791 return -EPERM; 1792 1793 err = bpf_map_check_op_flags(map, attr->flags, BPF_F_LOCK | BPF_F_CPU); 1794 if (err) 1795 return err; 1796 1797 key = __bpf_copy_key(ukey, map->key_size); 1798 if (IS_ERR(key)) 1799 return PTR_ERR(key); 1800 1801 value_size = bpf_map_value_size(map, attr->flags); 1802 1803 err = -ENOMEM; 1804 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); 1805 if (!value) 1806 goto free_key; 1807 1808 if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) { 1809 if (copy_from_user(value, uvalue, value_size)) 1810 err = -EFAULT; 1811 else 1812 err = bpf_map_copy_value(map, key, value, attr->flags); 1813 goto free_value; 1814 } 1815 1816 err = bpf_map_copy_value(map, key, value, attr->flags); 1817 if (err) 1818 goto free_value; 1819 1820 err = -EFAULT; 1821 if (copy_to_user(uvalue, value, value_size) != 0) 1822 goto free_value; 1823 1824 err = 0; 1825 1826 free_value: 1827 kvfree(value); 1828 free_key: 1829 kvfree(key); 1830 return err; 1831 } 1832 1833 1834 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags 1835 1836 static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr) 1837 { 1838 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel); 1839 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel); 1840 struct bpf_map *map; 1841 void *key, *value; 1842 u32 value_size; 1843 int err; 1844 1845 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) 1846 return -EINVAL; 1847 1848 CLASS(fd, f)(attr->map_fd); 1849 map = __bpf_map_get(f); 1850 if (IS_ERR(map)) 1851 return PTR_ERR(map); 1852 bpf_map_write_active_inc(map); 1853 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 1854 err = -EPERM; 1855 goto err_put; 1856 } 1857 1858 err = bpf_map_check_op_flags(map, attr->flags, ~0); 1859 if (err) 1860 goto err_put; 1861 1862 key = ___bpf_copy_key(ukey, map->key_size); 1863 if (IS_ERR(key)) { 1864 err = PTR_ERR(key); 1865 goto err_put; 1866 } 1867 1868 value_size = bpf_map_value_size(map, attr->flags); 1869 value = kvmemdup_bpfptr(uvalue, value_size); 1870 if (IS_ERR(value)) { 1871 err = PTR_ERR(value); 1872 goto free_key; 1873 } 1874 1875 err = bpf_map_update_value(map, fd_file(f), key, value, attr->flags); 1876 if (!err) 1877 maybe_wait_bpf_programs(map); 1878 1879 kvfree(value); 1880 free_key: 1881 kvfree(key); 1882 err_put: 1883 bpf_map_write_active_dec(map); 1884 return err; 1885 } 1886 1887 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key 1888 1889 static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr) 1890 { 1891 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel); 1892 struct bpf_map *map; 1893 void *key; 1894 int err; 1895 1896 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) 1897 return -EINVAL; 1898 1899 CLASS(fd, f)(attr->map_fd); 1900 map = __bpf_map_get(f); 1901 if (IS_ERR(map)) 1902 return PTR_ERR(map); 1903 bpf_map_write_active_inc(map); 1904 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 1905 err = -EPERM; 1906 goto err_put; 1907 } 1908 1909 key = ___bpf_copy_key(ukey, map->key_size); 1910 if (IS_ERR(key)) { 1911 err = PTR_ERR(key); 1912 goto err_put; 1913 } 1914 1915 if (bpf_map_is_offloaded(map)) { 1916 err = bpf_map_offload_delete_elem(map, key); 1917 goto out; 1918 } else if (IS_FD_PROG_ARRAY(map) || 1919 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 1920 /* These maps require sleepable context */ 1921 err = map->ops->map_delete_elem(map, key); 1922 goto out; 1923 } 1924 1925 bpf_disable_instrumentation(); 1926 rcu_read_lock(); 1927 err = map->ops->map_delete_elem(map, key); 1928 rcu_read_unlock(); 1929 bpf_enable_instrumentation(); 1930 if (!err) 1931 maybe_wait_bpf_programs(map); 1932 out: 1933 kvfree(key); 1934 err_put: 1935 bpf_map_write_active_dec(map); 1936 return err; 1937 } 1938 1939 /* last field in 'union bpf_attr' used by this command */ 1940 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key 1941 1942 static int map_get_next_key(union bpf_attr *attr) 1943 { 1944 void __user *ukey = u64_to_user_ptr(attr->key); 1945 void __user *unext_key = u64_to_user_ptr(attr->next_key); 1946 struct bpf_map *map; 1947 void *key, *next_key; 1948 int err; 1949 1950 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) 1951 return -EINVAL; 1952 1953 CLASS(fd, f)(attr->map_fd); 1954 map = __bpf_map_get(f); 1955 if (IS_ERR(map)) 1956 return PTR_ERR(map); 1957 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) 1958 return -EPERM; 1959 1960 if (ukey) { 1961 key = __bpf_copy_key(ukey, map->key_size); 1962 if (IS_ERR(key)) 1963 return PTR_ERR(key); 1964 } else { 1965 key = NULL; 1966 } 1967 1968 err = -ENOMEM; 1969 next_key = kvmalloc(map->key_size, GFP_USER); 1970 if (!next_key) 1971 goto free_key; 1972 1973 if (bpf_map_is_offloaded(map)) { 1974 err = bpf_map_offload_get_next_key(map, key, next_key); 1975 goto out; 1976 } 1977 1978 rcu_read_lock(); 1979 err = map->ops->map_get_next_key(map, key, next_key); 1980 rcu_read_unlock(); 1981 out: 1982 if (err) 1983 goto free_next_key; 1984 1985 err = -EFAULT; 1986 if (copy_to_user(unext_key, next_key, map->key_size) != 0) 1987 goto free_next_key; 1988 1989 err = 0; 1990 1991 free_next_key: 1992 kvfree(next_key); 1993 free_key: 1994 kvfree(key); 1995 return err; 1996 } 1997 1998 int generic_map_delete_batch(struct bpf_map *map, 1999 const union bpf_attr *attr, 2000 union bpf_attr __user *uattr) 2001 { 2002 void __user *keys = u64_to_user_ptr(attr->batch.keys); 2003 u32 cp, max_count; 2004 int err = 0; 2005 void *key; 2006 2007 if (attr->batch.elem_flags & ~BPF_F_LOCK) 2008 return -EINVAL; 2009 2010 if ((attr->batch.elem_flags & BPF_F_LOCK) && 2011 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) { 2012 return -EINVAL; 2013 } 2014 2015 max_count = attr->batch.count; 2016 if (!max_count) 2017 return 0; 2018 2019 if (put_user(0, &uattr->batch.count)) 2020 return -EFAULT; 2021 2022 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); 2023 if (!key) 2024 return -ENOMEM; 2025 2026 for (cp = 0; cp < max_count; cp++) { 2027 err = -EFAULT; 2028 if (copy_from_user(key, keys + cp * map->key_size, 2029 map->key_size)) 2030 break; 2031 2032 if (bpf_map_is_offloaded(map)) { 2033 err = bpf_map_offload_delete_elem(map, key); 2034 break; 2035 } 2036 2037 bpf_disable_instrumentation(); 2038 rcu_read_lock(); 2039 err = map->ops->map_delete_elem(map, key); 2040 rcu_read_unlock(); 2041 bpf_enable_instrumentation(); 2042 if (err) 2043 break; 2044 cond_resched(); 2045 } 2046 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) 2047 err = -EFAULT; 2048 2049 kvfree(key); 2050 2051 return err; 2052 } 2053 2054 int generic_map_update_batch(struct bpf_map *map, struct file *map_file, 2055 const union bpf_attr *attr, 2056 union bpf_attr __user *uattr) 2057 { 2058 void __user *values = u64_to_user_ptr(attr->batch.values); 2059 void __user *keys = u64_to_user_ptr(attr->batch.keys); 2060 u32 value_size, cp, max_count; 2061 void *key, *value; 2062 int err = 0; 2063 2064 err = bpf_map_check_op_flags(map, attr->batch.elem_flags, 2065 BPF_F_LOCK | BPF_F_CPU | BPF_F_ALL_CPUS); 2066 if (err) 2067 return err; 2068 2069 value_size = bpf_map_value_size(map, attr->batch.elem_flags); 2070 2071 max_count = attr->batch.count; 2072 if (!max_count) 2073 return 0; 2074 2075 if (put_user(0, &uattr->batch.count)) 2076 return -EFAULT; 2077 2078 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); 2079 if (!key) 2080 return -ENOMEM; 2081 2082 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); 2083 if (!value) { 2084 kvfree(key); 2085 return -ENOMEM; 2086 } 2087 2088 for (cp = 0; cp < max_count; cp++) { 2089 err = -EFAULT; 2090 if (copy_from_user(key, keys + cp * map->key_size, 2091 map->key_size) || 2092 copy_from_user(value, values + cp * value_size, value_size)) 2093 break; 2094 2095 err = bpf_map_update_value(map, map_file, key, value, 2096 attr->batch.elem_flags); 2097 2098 if (err) 2099 break; 2100 cond_resched(); 2101 } 2102 2103 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) 2104 err = -EFAULT; 2105 2106 kvfree(value); 2107 kvfree(key); 2108 2109 return err; 2110 } 2111 2112 int generic_map_lookup_batch(struct bpf_map *map, 2113 const union bpf_attr *attr, 2114 union bpf_attr __user *uattr) 2115 { 2116 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch); 2117 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch); 2118 void __user *values = u64_to_user_ptr(attr->batch.values); 2119 void __user *keys = u64_to_user_ptr(attr->batch.keys); 2120 void *buf, *buf_prevkey, *prev_key, *key, *value; 2121 u32 value_size, cp, max_count; 2122 int err; 2123 2124 err = bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK | BPF_F_CPU); 2125 if (err) 2126 return err; 2127 2128 value_size = bpf_map_value_size(map, attr->batch.elem_flags); 2129 2130 max_count = attr->batch.count; 2131 if (!max_count) 2132 return 0; 2133 2134 if (put_user(0, &uattr->batch.count)) 2135 return -EFAULT; 2136 2137 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); 2138 if (!buf_prevkey) 2139 return -ENOMEM; 2140 2141 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN); 2142 if (!buf) { 2143 kvfree(buf_prevkey); 2144 return -ENOMEM; 2145 } 2146 2147 err = -EFAULT; 2148 prev_key = NULL; 2149 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size)) 2150 goto free_buf; 2151 key = buf; 2152 value = key + map->key_size; 2153 if (ubatch) 2154 prev_key = buf_prevkey; 2155 2156 for (cp = 0; cp < max_count;) { 2157 rcu_read_lock(); 2158 err = map->ops->map_get_next_key(map, prev_key, key); 2159 rcu_read_unlock(); 2160 if (err) 2161 break; 2162 err = bpf_map_copy_value(map, key, value, 2163 attr->batch.elem_flags); 2164 2165 if (err == -ENOENT) 2166 goto next_key; 2167 2168 if (err) 2169 goto free_buf; 2170 2171 if (copy_to_user(keys + cp * map->key_size, key, 2172 map->key_size)) { 2173 err = -EFAULT; 2174 goto free_buf; 2175 } 2176 if (copy_to_user(values + cp * value_size, value, value_size)) { 2177 err = -EFAULT; 2178 goto free_buf; 2179 } 2180 2181 cp++; 2182 next_key: 2183 if (!prev_key) 2184 prev_key = buf_prevkey; 2185 2186 swap(prev_key, key); 2187 cond_resched(); 2188 } 2189 2190 if (err == -EFAULT) 2191 goto free_buf; 2192 2193 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) || 2194 (cp && copy_to_user(uobatch, prev_key, map->key_size)))) 2195 err = -EFAULT; 2196 2197 free_buf: 2198 kvfree(buf_prevkey); 2199 kvfree(buf); 2200 return err; 2201 } 2202 2203 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags 2204 2205 static int map_lookup_and_delete_elem(union bpf_attr *attr) 2206 { 2207 void __user *ukey = u64_to_user_ptr(attr->key); 2208 void __user *uvalue = u64_to_user_ptr(attr->value); 2209 struct bpf_map *map; 2210 void *key, *value; 2211 u32 value_size; 2212 int err; 2213 2214 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM)) 2215 return -EINVAL; 2216 2217 if (attr->flags & ~BPF_F_LOCK) 2218 return -EINVAL; 2219 2220 CLASS(fd, f)(attr->map_fd); 2221 map = __bpf_map_get(f); 2222 if (IS_ERR(map)) 2223 return PTR_ERR(map); 2224 bpf_map_write_active_inc(map); 2225 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) || 2226 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 2227 err = -EPERM; 2228 goto err_put; 2229 } 2230 2231 if (attr->flags && 2232 (map->map_type == BPF_MAP_TYPE_QUEUE || 2233 map->map_type == BPF_MAP_TYPE_STACK)) { 2234 err = -EINVAL; 2235 goto err_put; 2236 } 2237 2238 if ((attr->flags & BPF_F_LOCK) && 2239 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) { 2240 err = -EINVAL; 2241 goto err_put; 2242 } 2243 2244 key = __bpf_copy_key(ukey, map->key_size); 2245 if (IS_ERR(key)) { 2246 err = PTR_ERR(key); 2247 goto err_put; 2248 } 2249 2250 value_size = bpf_map_value_size(map, 0); 2251 2252 err = -ENOMEM; 2253 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); 2254 if (!value) 2255 goto free_key; 2256 2257 err = -ENOTSUPP; 2258 if (map->map_type == BPF_MAP_TYPE_QUEUE || 2259 map->map_type == BPF_MAP_TYPE_STACK) { 2260 err = map->ops->map_pop_elem(map, value); 2261 } else if (map->map_type == BPF_MAP_TYPE_HASH || 2262 map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 2263 map->map_type == BPF_MAP_TYPE_LRU_HASH || 2264 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || 2265 map->map_type == BPF_MAP_TYPE_RHASH || 2266 map->map_type == BPF_MAP_TYPE_STACK_TRACE) { 2267 if (!bpf_map_is_offloaded(map)) { 2268 bpf_disable_instrumentation(); 2269 rcu_read_lock(); 2270 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags); 2271 rcu_read_unlock(); 2272 bpf_enable_instrumentation(); 2273 } 2274 } 2275 2276 if (err) 2277 goto free_value; 2278 2279 if (copy_to_user(uvalue, value, value_size) != 0) { 2280 err = -EFAULT; 2281 goto free_value; 2282 } 2283 2284 err = 0; 2285 2286 free_value: 2287 kvfree(value); 2288 free_key: 2289 kvfree(key); 2290 err_put: 2291 bpf_map_write_active_dec(map); 2292 return err; 2293 } 2294 2295 #define BPF_MAP_FREEZE_LAST_FIELD map_fd 2296 2297 static int map_freeze(const union bpf_attr *attr) 2298 { 2299 int err = 0; 2300 struct bpf_map *map; 2301 2302 if (CHECK_ATTR(BPF_MAP_FREEZE)) 2303 return -EINVAL; 2304 2305 CLASS(fd, f)(attr->map_fd); 2306 map = __bpf_map_get(f); 2307 if (IS_ERR(map)) 2308 return PTR_ERR(map); 2309 2310 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS || !IS_ERR_OR_NULL(map->record)) 2311 return -ENOTSUPP; 2312 2313 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) 2314 return -EPERM; 2315 2316 mutex_lock(&map->freeze_mutex); 2317 if (bpf_map_write_active(map)) { 2318 err = -EBUSY; 2319 goto err_put; 2320 } 2321 if (READ_ONCE(map->frozen)) { 2322 err = -EBUSY; 2323 goto err_put; 2324 } 2325 2326 WRITE_ONCE(map->frozen, true); 2327 err_put: 2328 mutex_unlock(&map->freeze_mutex); 2329 return err; 2330 } 2331 2332 static const struct bpf_prog_ops * const bpf_prog_types[] = { 2333 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 2334 [_id] = & _name ## _prog_ops, 2335 #define BPF_MAP_TYPE(_id, _ops) 2336 #define BPF_LINK_TYPE(_id, _name) 2337 #include <linux/bpf_types.h> 2338 #undef BPF_PROG_TYPE 2339 #undef BPF_MAP_TYPE 2340 #undef BPF_LINK_TYPE 2341 }; 2342 2343 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) 2344 { 2345 const struct bpf_prog_ops *ops; 2346 2347 if (type >= ARRAY_SIZE(bpf_prog_types)) 2348 return -EINVAL; 2349 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); 2350 ops = bpf_prog_types[type]; 2351 if (!ops) 2352 return -EINVAL; 2353 2354 if (!bpf_prog_is_offloaded(prog->aux)) 2355 prog->aux->ops = ops; 2356 else 2357 prog->aux->ops = &bpf_offload_prog_ops; 2358 prog->type = type; 2359 return 0; 2360 } 2361 2362 enum bpf_audit { 2363 BPF_AUDIT_LOAD, 2364 BPF_AUDIT_UNLOAD, 2365 BPF_AUDIT_MAX, 2366 }; 2367 2368 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = { 2369 [BPF_AUDIT_LOAD] = "LOAD", 2370 [BPF_AUDIT_UNLOAD] = "UNLOAD", 2371 }; 2372 2373 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op) 2374 { 2375 struct audit_context *ctx = NULL; 2376 struct audit_buffer *ab; 2377 2378 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX)) 2379 return; 2380 if (audit_enabled == AUDIT_OFF) 2381 return; 2382 if (!in_hardirq() && !irqs_disabled()) 2383 ctx = audit_context(); 2384 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF); 2385 if (unlikely(!ab)) 2386 return; 2387 audit_log_format(ab, "prog-id=%u op=%s", 2388 prog->aux->id, bpf_audit_str[op]); 2389 audit_log_end(ab); 2390 } 2391 2392 static int bpf_prog_alloc_id(struct bpf_prog *prog) 2393 { 2394 int id; 2395 2396 idr_preload(GFP_KERNEL); 2397 spin_lock_bh(&prog_idr_lock); 2398 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); 2399 if (id > 0) 2400 prog->aux->id = id; 2401 spin_unlock_bh(&prog_idr_lock); 2402 idr_preload_end(); 2403 2404 /* id is in [1, INT_MAX) */ 2405 if (WARN_ON_ONCE(!id)) 2406 return -ENOSPC; 2407 2408 return id > 0 ? 0 : id; 2409 } 2410 2411 void bpf_prog_free_id(struct bpf_prog *prog) 2412 { 2413 unsigned long flags; 2414 2415 /* cBPF to eBPF migrations are currently not in the idr store. 2416 * Offloaded programs are removed from the store when their device 2417 * disappears - even if someone grabs an fd to them they are unusable, 2418 * simply waiting for refcnt to drop to be freed. 2419 */ 2420 if (!prog->aux->id) 2421 return; 2422 2423 spin_lock_irqsave(&prog_idr_lock, flags); 2424 idr_remove(&prog_idr, prog->aux->id); 2425 prog->aux->id = 0; 2426 spin_unlock_irqrestore(&prog_idr_lock, flags); 2427 } 2428 2429 static void __bpf_prog_put_rcu(struct rcu_head *rcu) 2430 { 2431 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); 2432 2433 kvfree(aux->func_info); 2434 kfree(aux->func_info_aux); 2435 free_uid(aux->user); 2436 security_bpf_prog_free(aux->prog); 2437 bpf_prog_free(aux->prog); 2438 } 2439 2440 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) 2441 { 2442 bpf_prog_kallsyms_del_all(prog); 2443 btf_put(prog->aux->btf); 2444 module_put(prog->aux->mod); 2445 kvfree(prog->aux->jited_linfo); 2446 kvfree(prog->aux->linfo); 2447 kfree(prog->aux->kfunc_tab); 2448 kfree(prog->aux->ctx_arg_info); 2449 if (prog->aux->attach_btf) 2450 btf_put(prog->aux->attach_btf); 2451 2452 if (deferred) { 2453 if (prog->sleepable) 2454 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu); 2455 else 2456 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); 2457 } else { 2458 __bpf_prog_put_rcu(&prog->aux->rcu); 2459 } 2460 } 2461 2462 static void bpf_prog_put_deferred(struct work_struct *work) 2463 { 2464 struct bpf_prog_aux *aux; 2465 struct bpf_prog *prog; 2466 2467 aux = container_of(work, struct bpf_prog_aux, work); 2468 prog = aux->prog; 2469 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0); 2470 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD); 2471 bpf_prog_free_id(prog); 2472 __bpf_prog_put_noref(prog, true); 2473 } 2474 2475 static void __bpf_prog_put(struct bpf_prog *prog) 2476 { 2477 struct bpf_prog_aux *aux = prog->aux; 2478 2479 if (atomic64_dec_and_test(&aux->refcnt)) { 2480 if (in_hardirq() || irqs_disabled()) { 2481 INIT_WORK(&aux->work, bpf_prog_put_deferred); 2482 schedule_work(&aux->work); 2483 } else { 2484 bpf_prog_put_deferred(&aux->work); 2485 } 2486 } 2487 } 2488 2489 void bpf_prog_put(struct bpf_prog *prog) 2490 { 2491 __bpf_prog_put(prog); 2492 } 2493 EXPORT_SYMBOL_GPL(bpf_prog_put); 2494 2495 static int bpf_prog_release(struct inode *inode, struct file *filp) 2496 { 2497 struct bpf_prog *prog = filp->private_data; 2498 2499 bpf_prog_put(prog); 2500 return 0; 2501 } 2502 2503 struct bpf_prog_kstats { 2504 u64 nsecs; 2505 u64 cnt; 2506 u64 misses; 2507 }; 2508 2509 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog) 2510 { 2511 struct bpf_prog_stats *stats; 2512 unsigned int flags; 2513 2514 if (unlikely(!prog->stats)) 2515 return; 2516 2517 stats = this_cpu_ptr(prog->stats); 2518 flags = u64_stats_update_begin_irqsave(&stats->syncp); 2519 u64_stats_inc(&stats->misses); 2520 u64_stats_update_end_irqrestore(&stats->syncp, flags); 2521 } 2522 2523 static void bpf_prog_get_stats(const struct bpf_prog *prog, 2524 struct bpf_prog_kstats *stats) 2525 { 2526 u64 nsecs = 0, cnt = 0, misses = 0; 2527 int cpu; 2528 2529 for_each_possible_cpu(cpu) { 2530 const struct bpf_prog_stats *st; 2531 unsigned int start; 2532 u64 tnsecs, tcnt, tmisses; 2533 2534 st = per_cpu_ptr(prog->stats, cpu); 2535 do { 2536 start = u64_stats_fetch_begin(&st->syncp); 2537 tnsecs = u64_stats_read(&st->nsecs); 2538 tcnt = u64_stats_read(&st->cnt); 2539 tmisses = u64_stats_read(&st->misses); 2540 } while (u64_stats_fetch_retry(&st->syncp, start)); 2541 nsecs += tnsecs; 2542 cnt += tcnt; 2543 misses += tmisses; 2544 } 2545 stats->nsecs = nsecs; 2546 stats->cnt = cnt; 2547 stats->misses = misses; 2548 } 2549 2550 #ifdef CONFIG_PROC_FS 2551 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) 2552 { 2553 const struct bpf_prog *prog = filp->private_data; 2554 char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 2555 struct bpf_prog_kstats stats; 2556 2557 bpf_prog_get_stats(prog, &stats); 2558 bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 2559 seq_printf(m, 2560 "prog_type:\t%u\n" 2561 "prog_jited:\t%u\n" 2562 "prog_tag:\t%s\n" 2563 "memlock:\t%llu\n" 2564 "prog_id:\t%u\n" 2565 "run_time_ns:\t%llu\n" 2566 "run_cnt:\t%llu\n" 2567 "recursion_misses:\t%llu\n" 2568 "verified_insns:\t%u\n", 2569 prog->type, 2570 prog->jited, 2571 prog_tag, 2572 prog->pages * 1ULL << PAGE_SHIFT, 2573 prog->aux->id, 2574 stats.nsecs, 2575 stats.cnt, 2576 stats.misses, 2577 prog->aux->verified_insns); 2578 } 2579 #endif 2580 2581 const struct file_operations bpf_prog_fops = { 2582 #ifdef CONFIG_PROC_FS 2583 .show_fdinfo = bpf_prog_show_fdinfo, 2584 #endif 2585 .release = bpf_prog_release, 2586 .read = bpf_dummy_read, 2587 .write = bpf_dummy_write, 2588 }; 2589 2590 int bpf_prog_new_fd(struct bpf_prog *prog) 2591 { 2592 int ret; 2593 2594 ret = security_bpf_prog(prog); 2595 if (ret < 0) 2596 return ret; 2597 2598 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, 2599 O_RDWR | O_CLOEXEC); 2600 } 2601 2602 void bpf_prog_add(struct bpf_prog *prog, int i) 2603 { 2604 atomic64_add(i, &prog->aux->refcnt); 2605 } 2606 EXPORT_SYMBOL_GPL(bpf_prog_add); 2607 2608 void bpf_prog_sub(struct bpf_prog *prog, int i) 2609 { 2610 /* Only to be used for undoing previous bpf_prog_add() in some 2611 * error path. We still know that another entity in our call 2612 * path holds a reference to the program, thus atomic_sub() can 2613 * be safely used in such cases! 2614 */ 2615 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0); 2616 } 2617 EXPORT_SYMBOL_GPL(bpf_prog_sub); 2618 2619 void bpf_prog_inc(struct bpf_prog *prog) 2620 { 2621 atomic64_inc(&prog->aux->refcnt); 2622 } 2623 EXPORT_SYMBOL_GPL(bpf_prog_inc); 2624 2625 /* prog_idr_lock should have been held */ 2626 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) 2627 { 2628 int refold; 2629 2630 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0); 2631 2632 if (!refold) 2633 return ERR_PTR(-ENOENT); 2634 2635 return prog; 2636 } 2637 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); 2638 2639 bool bpf_prog_get_ok(struct bpf_prog *prog, 2640 enum bpf_prog_type *attach_type, bool attach_drv) 2641 { 2642 /* not an attachment, just a refcount inc, always allow */ 2643 if (!attach_type) 2644 return true; 2645 2646 if (prog->type != *attach_type) 2647 return false; 2648 if (bpf_prog_is_offloaded(prog->aux) && !attach_drv) 2649 return false; 2650 2651 return true; 2652 } 2653 2654 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, 2655 bool attach_drv) 2656 { 2657 CLASS(fd, f)(ufd); 2658 struct bpf_prog *prog; 2659 2660 if (fd_empty(f)) 2661 return ERR_PTR(-EBADF); 2662 if (fd_file(f)->f_op != &bpf_prog_fops) 2663 return ERR_PTR(-EINVAL); 2664 2665 prog = fd_file(f)->private_data; 2666 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) 2667 return ERR_PTR(-EINVAL); 2668 2669 bpf_prog_inc(prog); 2670 return prog; 2671 } 2672 2673 struct bpf_prog *bpf_prog_get(u32 ufd) 2674 { 2675 return __bpf_prog_get(ufd, NULL, false); 2676 } 2677 2678 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, 2679 bool attach_drv) 2680 { 2681 return __bpf_prog_get(ufd, &type, attach_drv); 2682 } 2683 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); 2684 2685 /* Initially all BPF programs could be loaded w/o specifying 2686 * expected_attach_type. Later for some of them specifying expected_attach_type 2687 * at load time became required so that program could be validated properly. 2688 * Programs of types that are allowed to be loaded both w/ and w/o (for 2689 * backward compatibility) expected_attach_type, should have the default attach 2690 * type assigned to expected_attach_type for the latter case, so that it can be 2691 * validated later at attach time. 2692 * 2693 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if 2694 * prog type requires it but has some attach types that have to be backward 2695 * compatible. 2696 */ 2697 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) 2698 { 2699 switch (attr->prog_type) { 2700 case BPF_PROG_TYPE_CGROUP_SOCK: 2701 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't 2702 * exist so checking for non-zero is the way to go here. 2703 */ 2704 if (!attr->expected_attach_type) 2705 attr->expected_attach_type = 2706 BPF_CGROUP_INET_SOCK_CREATE; 2707 break; 2708 case BPF_PROG_TYPE_SK_REUSEPORT: 2709 if (!attr->expected_attach_type) 2710 attr->expected_attach_type = 2711 BPF_SK_REUSEPORT_SELECT; 2712 break; 2713 } 2714 } 2715 2716 static int 2717 bpf_prog_load_check_attach(enum bpf_prog_type prog_type, 2718 enum bpf_attach_type expected_attach_type, 2719 struct btf *attach_btf, u32 btf_id, 2720 struct bpf_prog *dst_prog, 2721 bool multi_func) 2722 { 2723 if (btf_id) { 2724 if (btf_id > BTF_MAX_TYPE) 2725 return -EINVAL; 2726 2727 if (!attach_btf && !dst_prog) 2728 return -EINVAL; 2729 2730 switch (prog_type) { 2731 case BPF_PROG_TYPE_TRACING: 2732 case BPF_PROG_TYPE_LSM: 2733 case BPF_PROG_TYPE_STRUCT_OPS: 2734 case BPF_PROG_TYPE_EXT: 2735 break; 2736 default: 2737 return -EINVAL; 2738 } 2739 } 2740 2741 if (multi_func) { 2742 if (prog_type != BPF_PROG_TYPE_TRACING) 2743 return -EINVAL; 2744 if (!attach_btf || btf_id) 2745 return -EINVAL; 2746 return 0; 2747 } 2748 2749 if (attach_btf && (!btf_id || dst_prog)) 2750 return -EINVAL; 2751 2752 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING && 2753 prog_type != BPF_PROG_TYPE_EXT) 2754 return -EINVAL; 2755 2756 switch (prog_type) { 2757 case BPF_PROG_TYPE_CGROUP_SOCK: 2758 switch (expected_attach_type) { 2759 case BPF_CGROUP_INET_SOCK_CREATE: 2760 case BPF_CGROUP_INET_SOCK_RELEASE: 2761 case BPF_CGROUP_INET4_POST_BIND: 2762 case BPF_CGROUP_INET6_POST_BIND: 2763 return 0; 2764 default: 2765 return -EINVAL; 2766 } 2767 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2768 switch (expected_attach_type) { 2769 case BPF_CGROUP_INET4_BIND: 2770 case BPF_CGROUP_INET6_BIND: 2771 case BPF_CGROUP_INET4_CONNECT: 2772 case BPF_CGROUP_INET6_CONNECT: 2773 case BPF_CGROUP_UNIX_CONNECT: 2774 case BPF_CGROUP_INET4_GETPEERNAME: 2775 case BPF_CGROUP_INET6_GETPEERNAME: 2776 case BPF_CGROUP_UNIX_GETPEERNAME: 2777 case BPF_CGROUP_INET4_GETSOCKNAME: 2778 case BPF_CGROUP_INET6_GETSOCKNAME: 2779 case BPF_CGROUP_UNIX_GETSOCKNAME: 2780 case BPF_CGROUP_UDP4_SENDMSG: 2781 case BPF_CGROUP_UDP6_SENDMSG: 2782 case BPF_CGROUP_UNIX_SENDMSG: 2783 case BPF_CGROUP_UDP4_RECVMSG: 2784 case BPF_CGROUP_UDP6_RECVMSG: 2785 case BPF_CGROUP_UNIX_RECVMSG: 2786 return 0; 2787 default: 2788 return -EINVAL; 2789 } 2790 case BPF_PROG_TYPE_CGROUP_SKB: 2791 switch (expected_attach_type) { 2792 case BPF_CGROUP_INET_INGRESS: 2793 case BPF_CGROUP_INET_EGRESS: 2794 return 0; 2795 default: 2796 return -EINVAL; 2797 } 2798 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2799 switch (expected_attach_type) { 2800 case BPF_CGROUP_SETSOCKOPT: 2801 case BPF_CGROUP_GETSOCKOPT: 2802 return 0; 2803 default: 2804 return -EINVAL; 2805 } 2806 case BPF_PROG_TYPE_SK_LOOKUP: 2807 if (expected_attach_type == BPF_SK_LOOKUP) 2808 return 0; 2809 return -EINVAL; 2810 case BPF_PROG_TYPE_SK_REUSEPORT: 2811 switch (expected_attach_type) { 2812 case BPF_SK_REUSEPORT_SELECT: 2813 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE: 2814 return 0; 2815 default: 2816 return -EINVAL; 2817 } 2818 case BPF_PROG_TYPE_NETFILTER: 2819 if (expected_attach_type == BPF_NETFILTER) 2820 return 0; 2821 return -EINVAL; 2822 case BPF_PROG_TYPE_SYSCALL: 2823 case BPF_PROG_TYPE_EXT: 2824 if (expected_attach_type) 2825 return -EINVAL; 2826 fallthrough; 2827 default: 2828 return 0; 2829 } 2830 } 2831 2832 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type) 2833 { 2834 switch (prog_type) { 2835 case BPF_PROG_TYPE_SCHED_CLS: 2836 case BPF_PROG_TYPE_SCHED_ACT: 2837 case BPF_PROG_TYPE_XDP: 2838 case BPF_PROG_TYPE_LWT_IN: 2839 case BPF_PROG_TYPE_LWT_OUT: 2840 case BPF_PROG_TYPE_LWT_XMIT: 2841 case BPF_PROG_TYPE_LWT_SEG6LOCAL: 2842 case BPF_PROG_TYPE_SK_SKB: 2843 case BPF_PROG_TYPE_SK_MSG: 2844 case BPF_PROG_TYPE_FLOW_DISSECTOR: 2845 case BPF_PROG_TYPE_CGROUP_DEVICE: 2846 case BPF_PROG_TYPE_CGROUP_SOCK: 2847 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2848 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2849 case BPF_PROG_TYPE_CGROUP_SYSCTL: 2850 case BPF_PROG_TYPE_SOCK_OPS: 2851 case BPF_PROG_TYPE_EXT: /* extends any prog */ 2852 case BPF_PROG_TYPE_NETFILTER: 2853 return true; 2854 case BPF_PROG_TYPE_CGROUP_SKB: 2855 /* always unpriv */ 2856 case BPF_PROG_TYPE_SK_REUSEPORT: 2857 /* equivalent to SOCKET_FILTER. need CAP_BPF only */ 2858 default: 2859 return false; 2860 } 2861 } 2862 2863 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) 2864 { 2865 switch (prog_type) { 2866 case BPF_PROG_TYPE_KPROBE: 2867 case BPF_PROG_TYPE_TRACEPOINT: 2868 case BPF_PROG_TYPE_PERF_EVENT: 2869 case BPF_PROG_TYPE_RAW_TRACEPOINT: 2870 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 2871 case BPF_PROG_TYPE_TRACING: 2872 case BPF_PROG_TYPE_LSM: 2873 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */ 2874 case BPF_PROG_TYPE_EXT: /* extends any prog */ 2875 return true; 2876 default: 2877 return false; 2878 } 2879 } 2880 2881 static int bpf_prog_mark_insn_arrays_ready(struct bpf_prog *prog) 2882 { 2883 int err; 2884 int i; 2885 2886 for (i = 0; i < prog->aux->used_map_cnt; i++) { 2887 if (prog->aux->used_maps[i]->map_type != BPF_MAP_TYPE_INSN_ARRAY) 2888 continue; 2889 2890 err = bpf_insn_array_ready(prog->aux->used_maps[i]); 2891 if (err) 2892 return err; 2893 } 2894 2895 return 0; 2896 } 2897 2898 extern int bpf_multi_func(void); 2899 int __init __used bpf_multi_func(void) { return 0; } 2900 2901 BTF_ID_LIST_GLOBAL_SINGLE(bpf_multi_func_btf_id, func, bpf_multi_func) 2902 2903 /* last field in 'union bpf_attr' used by this command */ 2904 #define BPF_PROG_LOAD_LAST_FIELD keyring_id 2905 2906 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log) 2907 { 2908 enum bpf_prog_type type = attr->prog_type; 2909 struct bpf_prog *prog, *dst_prog = NULL; 2910 struct btf *attach_btf = NULL; 2911 struct bpf_token *token = NULL; 2912 bool bpf_cap; 2913 int err; 2914 char license[128]; 2915 bool multi_func; 2916 2917 if (CHECK_ATTR(BPF_PROG_LOAD)) 2918 return -EINVAL; 2919 2920 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | 2921 BPF_F_ANY_ALIGNMENT | 2922 BPF_F_TEST_STATE_FREQ | 2923 BPF_F_SLEEPABLE | 2924 BPF_F_TEST_RND_HI32 | 2925 BPF_F_XDP_HAS_FRAGS | 2926 BPF_F_XDP_DEV_BOUND_ONLY | 2927 BPF_F_TEST_REG_INVARIANTS | 2928 BPF_F_TOKEN_FD)) 2929 return -EINVAL; 2930 2931 bpf_prog_load_fixup_attach_type(attr); 2932 2933 if (attr->prog_flags & BPF_F_TOKEN_FD) { 2934 token = bpf_token_get_from_fd(attr->prog_token_fd); 2935 if (IS_ERR(token)) 2936 return PTR_ERR(token); 2937 /* if current token doesn't grant prog loading permissions, 2938 * then we can't use this token, so ignore it and rely on 2939 * system-wide capabilities checks 2940 */ 2941 if (!bpf_token_allow_cmd(token, BPF_PROG_LOAD) || 2942 !bpf_token_allow_prog_type(token, attr->prog_type, 2943 attr->expected_attach_type)) { 2944 bpf_token_put(token); 2945 token = NULL; 2946 } 2947 } 2948 2949 bpf_cap = bpf_token_capable(token, CAP_BPF); 2950 err = -EPERM; 2951 2952 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && 2953 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && 2954 !bpf_cap) 2955 goto put_token; 2956 2957 /* Intent here is for unprivileged_bpf_disabled to block BPF program 2958 * creation for unprivileged users; other actions depend 2959 * on fd availability and access to bpffs, so are dependent on 2960 * object creation success. Even with unprivileged BPF disabled, 2961 * capability checks are still carried out for these 2962 * and other operations. 2963 */ 2964 if (sysctl_unprivileged_bpf_disabled && !bpf_cap) 2965 goto put_token; 2966 2967 if (attr->insn_cnt == 0 || 2968 attr->insn_cnt > (bpf_cap ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) { 2969 err = -E2BIG; 2970 goto put_token; 2971 } 2972 if (type != BPF_PROG_TYPE_SOCKET_FILTER && 2973 type != BPF_PROG_TYPE_CGROUP_SKB && 2974 !bpf_cap) 2975 goto put_token; 2976 2977 if (is_net_admin_prog_type(type) && !bpf_token_capable(token, CAP_NET_ADMIN)) 2978 goto put_token; 2979 if (is_perfmon_prog_type(type) && !bpf_token_capable(token, CAP_PERFMON)) 2980 goto put_token; 2981 2982 multi_func = is_tracing_multi(attr->expected_attach_type); 2983 2984 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog 2985 * or btf, we need to check which one it is 2986 */ 2987 if (attr->attach_prog_fd) { 2988 dst_prog = bpf_prog_get(attr->attach_prog_fd); 2989 if (IS_ERR(dst_prog)) { 2990 dst_prog = NULL; 2991 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd); 2992 if (IS_ERR(attach_btf)) { 2993 err = -EINVAL; 2994 goto put_token; 2995 } 2996 if (!btf_is_kernel(attach_btf)) { 2997 /* attaching through specifying bpf_prog's BTF 2998 * objects directly might be supported eventually 2999 */ 3000 btf_put(attach_btf); 3001 err = -ENOTSUPP; 3002 goto put_token; 3003 } 3004 } 3005 } else if (attr->attach_btf_id || multi_func) { 3006 /* fall back to vmlinux BTF, if BTF type ID is specified */ 3007 attach_btf = bpf_get_btf_vmlinux(); 3008 if (IS_ERR(attach_btf)) { 3009 err = PTR_ERR(attach_btf); 3010 goto put_token; 3011 } 3012 if (!attach_btf) { 3013 err = -EINVAL; 3014 goto put_token; 3015 } 3016 btf_get(attach_btf); 3017 } 3018 3019 if (bpf_prog_load_check_attach(type, attr->expected_attach_type, 3020 attach_btf, attr->attach_btf_id, 3021 dst_prog, multi_func)) { 3022 if (dst_prog) 3023 bpf_prog_put(dst_prog); 3024 if (attach_btf) 3025 btf_put(attach_btf); 3026 err = -EINVAL; 3027 goto put_token; 3028 } 3029 3030 /* plain bpf_prog allocation */ 3031 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); 3032 if (!prog) { 3033 if (dst_prog) 3034 bpf_prog_put(dst_prog); 3035 if (attach_btf) 3036 btf_put(attach_btf); 3037 err = -EINVAL; 3038 goto put_token; 3039 } 3040 3041 prog->expected_attach_type = attr->expected_attach_type; 3042 prog->sleepable = !!(attr->prog_flags & BPF_F_SLEEPABLE); 3043 prog->aux->attach_btf = attach_btf; 3044 prog->aux->attach_btf_id = multi_func ? bpf_multi_func_btf_id[0] : attr->attach_btf_id; 3045 prog->aux->dst_prog = dst_prog; 3046 prog->aux->dev_bound = !!attr->prog_ifindex; 3047 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS; 3048 3049 /* move token into prog->aux, reuse taken refcnt */ 3050 prog->aux->token = token; 3051 token = NULL; 3052 3053 prog->aux->user = get_current_user(); 3054 prog->len = attr->insn_cnt; 3055 3056 err = -EFAULT; 3057 if (copy_from_bpfptr(prog->insns, 3058 make_bpfptr(attr->insns, uattr.is_kernel), 3059 bpf_prog_insn_size(prog)) != 0) 3060 goto free_prog; 3061 /* copy eBPF program license from user space */ 3062 if (strncpy_from_bpfptr(license, 3063 make_bpfptr(attr->license, uattr.is_kernel), 3064 sizeof(license) - 1) < 0) 3065 goto free_prog; 3066 license[sizeof(license) - 1] = 0; 3067 3068 /* eBPF programs must be GPL compatible to use GPL-ed functions */ 3069 prog->gpl_compatible = license_is_gpl_compatible(license) ? 1 : 0; 3070 prog->aux->sig.keyring_type = BPF_SIG_KEYRING_NONE; 3071 prog->aux->sig.verdict = BPF_SIG_UNSIGNED; 3072 prog->orig_prog = NULL; 3073 prog->jited = 0; 3074 3075 atomic64_set(&prog->aux->refcnt, 1); 3076 3077 if (bpf_prog_is_dev_bound(prog->aux)) { 3078 err = bpf_prog_dev_bound_init(prog, attr); 3079 if (err) 3080 goto free_prog; 3081 } 3082 3083 if (type == BPF_PROG_TYPE_EXT && dst_prog && 3084 bpf_prog_is_dev_bound(dst_prog->aux)) { 3085 err = bpf_prog_dev_bound_inherit(prog, dst_prog); 3086 if (err) 3087 goto free_prog; 3088 } 3089 3090 /* 3091 * Bookkeeping for managing the program attachment chain. 3092 * 3093 * It might be tempting to set attach_tracing_prog flag at the attachment 3094 * time, but this will not prevent from loading bunch of tracing prog 3095 * first, then attach them one to another. 3096 * 3097 * The flag attach_tracing_prog is set for the whole program lifecycle, and 3098 * doesn't have to be cleared in bpf_tracing_link_release, since tracing 3099 * programs cannot change attachment target. 3100 */ 3101 if (type == BPF_PROG_TYPE_TRACING && dst_prog && 3102 dst_prog->type == BPF_PROG_TYPE_TRACING) { 3103 prog->aux->attach_tracing_prog = true; 3104 } 3105 3106 /* find program type: socket_filter vs tracing_filter */ 3107 err = find_prog_type(type, prog); 3108 if (err < 0) 3109 goto free_prog; 3110 3111 prog->aux->load_time = ktime_get_boottime_ns(); 3112 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name, 3113 sizeof(attr->prog_name)); 3114 if (err < 0) 3115 goto free_prog; 3116 3117 /* run eBPF verifier */ 3118 err = bpf_check(&prog, attr, uattr, attr_log); 3119 if (err < 0) 3120 goto free_used_maps; 3121 3122 err = bpf_prog_mark_insn_arrays_ready(prog); 3123 if (err < 0) 3124 goto free_used_maps; 3125 3126 err = bpf_prog_alloc_id(prog); 3127 if (err) 3128 goto free_used_maps; 3129 3130 /* Upon success of bpf_prog_alloc_id(), the BPF prog is 3131 * effectively publicly exposed. However, retrieving via 3132 * bpf_prog_get_fd_by_id() will take another reference, 3133 * therefore it cannot be gone underneath us. 3134 * 3135 * Only for the time /after/ successful bpf_prog_new_fd() 3136 * and before returning to userspace, we might just hold 3137 * one reference and any parallel close on that fd could 3138 * rip everything out. Hence, below notifications must 3139 * happen before bpf_prog_new_fd(). 3140 * 3141 * Also, any failure handling from this point onwards must 3142 * be using bpf_prog_put() given the program is exposed. 3143 */ 3144 bpf_prog_kallsyms_add(prog); 3145 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); 3146 bpf_audit_prog(prog, BPF_AUDIT_LOAD); 3147 3148 err = bpf_prog_new_fd(prog); 3149 if (err < 0) 3150 bpf_prog_put(prog); 3151 return err; 3152 3153 free_used_maps: 3154 /* In case we have subprogs, we need to wait for a grace 3155 * period before we can tear down JIT memory since symbols 3156 * are already exposed under kallsyms. 3157 */ 3158 __bpf_prog_put_noref(prog, prog->aux->real_func_cnt); 3159 return err; 3160 3161 free_prog: 3162 free_uid(prog->aux->user); 3163 if (prog->aux->attach_btf) 3164 btf_put(prog->aux->attach_btf); 3165 bpf_prog_free(prog); 3166 put_token: 3167 bpf_token_put(token); 3168 return err; 3169 } 3170 3171 #define BPF_OBJ_LAST_FIELD path_fd 3172 3173 static int bpf_obj_pin(const union bpf_attr *attr) 3174 { 3175 int path_fd; 3176 3177 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags & ~BPF_F_PATH_FD) 3178 return -EINVAL; 3179 3180 /* path_fd has to be accompanied by BPF_F_PATH_FD flag */ 3181 if (!(attr->file_flags & BPF_F_PATH_FD) && attr->path_fd) 3182 return -EINVAL; 3183 3184 path_fd = attr->file_flags & BPF_F_PATH_FD ? attr->path_fd : AT_FDCWD; 3185 return bpf_obj_pin_user(attr->bpf_fd, path_fd, 3186 u64_to_user_ptr(attr->pathname)); 3187 } 3188 3189 static int bpf_obj_get(const union bpf_attr *attr) 3190 { 3191 int path_fd; 3192 3193 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || 3194 attr->file_flags & ~(BPF_OBJ_FLAG_MASK | BPF_F_PATH_FD)) 3195 return -EINVAL; 3196 3197 /* path_fd has to be accompanied by BPF_F_PATH_FD flag */ 3198 if (!(attr->file_flags & BPF_F_PATH_FD) && attr->path_fd) 3199 return -EINVAL; 3200 3201 path_fd = attr->file_flags & BPF_F_PATH_FD ? attr->path_fd : AT_FDCWD; 3202 return bpf_obj_get_user(path_fd, u64_to_user_ptr(attr->pathname), 3203 attr->file_flags); 3204 } 3205 3206 /* bpf_link_init_sleepable() allows to specify whether BPF link itself has 3207 * "sleepable" semantics, which normally would mean that BPF link's attach 3208 * hook can dereference link or link's underlying program for some time after 3209 * detachment due to RCU Tasks Trace-based lifetime protection scheme. 3210 * BPF program itself can be non-sleepable, yet, because it's transitively 3211 * reachable through BPF link, its freeing has to be delayed until after RCU 3212 * Tasks Trace GP. 3213 */ 3214 void bpf_link_init_sleepable(struct bpf_link *link, enum bpf_link_type type, 3215 const struct bpf_link_ops *ops, struct bpf_prog *prog, 3216 enum bpf_attach_type attach_type, bool sleepable) 3217 { 3218 WARN_ON(ops->dealloc && ops->dealloc_deferred); 3219 atomic64_set(&link->refcnt, 1); 3220 link->type = type; 3221 link->sleepable = sleepable; 3222 link->id = 0; 3223 link->ops = ops; 3224 link->prog = prog; 3225 link->attach_type = attach_type; 3226 } 3227 3228 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, 3229 const struct bpf_link_ops *ops, struct bpf_prog *prog, 3230 enum bpf_attach_type attach_type) 3231 { 3232 bpf_link_init_sleepable(link, type, ops, prog, attach_type, false); 3233 } 3234 3235 void bpf_tramp_link_init(struct bpf_tramp_link *link, enum bpf_link_type type, 3236 const struct bpf_link_ops *ops, struct bpf_prog *prog, 3237 enum bpf_attach_type attach_type, u64 cookie) 3238 { 3239 bpf_link_init(&link->link, type, ops, prog, attach_type); 3240 link->node.link = &link->link; 3241 link->node.cookie = cookie; 3242 } 3243 3244 static void bpf_link_free_id(int id) 3245 { 3246 if (!id) 3247 return; 3248 3249 spin_lock_bh(&link_idr_lock); 3250 idr_remove(&link_idr, id); 3251 spin_unlock_bh(&link_idr_lock); 3252 } 3253 3254 /* Clean up bpf_link and corresponding anon_inode file and FD. After 3255 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred 3256 * anon_inode's release() call. This helper marks bpf_link as 3257 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt 3258 * is not decremented, it's the responsibility of a calling code that failed 3259 * to complete bpf_link initialization. 3260 * This helper eventually calls link's dealloc callback, but does not call 3261 * link's release callback. 3262 */ 3263 void bpf_link_cleanup(struct bpf_link_primer *primer) 3264 { 3265 primer->link->prog = NULL; 3266 bpf_link_free_id(primer->id); 3267 fput(primer->file); 3268 put_unused_fd(primer->fd); 3269 } 3270 3271 void bpf_link_inc(struct bpf_link *link) 3272 { 3273 atomic64_inc(&link->refcnt); 3274 } 3275 3276 static void bpf_link_dealloc(struct bpf_link *link) 3277 { 3278 /* now that we know that bpf_link itself can't be reached, put underlying BPF program */ 3279 if (link->prog) 3280 bpf_prog_put(link->prog); 3281 3282 /* free bpf_link and its containing memory */ 3283 if (link->ops->dealloc_deferred) 3284 link->ops->dealloc_deferred(link); 3285 else 3286 link->ops->dealloc(link); 3287 } 3288 3289 static void bpf_link_defer_dealloc_rcu_gp(struct rcu_head *rcu) 3290 { 3291 struct bpf_link *link = container_of(rcu, struct bpf_link, rcu); 3292 3293 bpf_link_dealloc(link); 3294 } 3295 3296 static bool bpf_link_is_tracepoint(struct bpf_link *link) 3297 { 3298 /* 3299 * Only these combinations support a tracepoint bpf_link. 3300 * BPF_LINK_TYPE_TRACING raw_tp progs are hardcoded to use 3301 * bpf_raw_tp_link_lops and thus dealloc_deferred(), see 3302 * bpf_raw_tp_link_attach(). 3303 */ 3304 return link->type == BPF_LINK_TYPE_RAW_TRACEPOINT || 3305 (link->type == BPF_LINK_TYPE_TRACING && link->attach_type == BPF_TRACE_RAW_TP); 3306 } 3307 3308 /* bpf_link_free is guaranteed to be called from process context */ 3309 static void bpf_link_free(struct bpf_link *link) 3310 { 3311 const struct bpf_link_ops *ops = link->ops; 3312 3313 bpf_link_free_id(link->id); 3314 /* detach BPF program, clean up used resources */ 3315 if (link->prog) 3316 ops->release(link); 3317 if (ops->dealloc_deferred) { 3318 /* 3319 * Schedule BPF link deallocation, which will only then 3320 * trigger putting BPF program refcount. 3321 * If underlying BPF program is sleepable or BPF link's target 3322 * attach hookpoint is sleepable or otherwise requires RCU GPs 3323 * to ensure link and its underlying BPF program is not 3324 * reachable anymore, we need to first wait for RCU tasks 3325 * trace sync, and then go through "classic" RCU grace period. 3326 * 3327 * For tracepoint BPF links, we need to go through SRCU grace 3328 * period wait instead when non-faultable tracepoint is used. We 3329 * don't need to chain SRCU grace period waits, however, for the 3330 * faultable case, since it exclusively uses RCU Tasks Trace. 3331 */ 3332 if (link->sleepable || (link->prog && link->prog->sleepable)) 3333 /* RCU Tasks Trace grace period implies RCU grace period. */ 3334 call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_rcu_gp); 3335 /* We need to do a SRCU grace period wait for non-faultable tracepoint BPF links. */ 3336 else if (bpf_link_is_tracepoint(link)) 3337 call_tracepoint_unregister_atomic(&link->rcu, bpf_link_defer_dealloc_rcu_gp); 3338 else 3339 call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp); 3340 } else if (ops->dealloc) { 3341 bpf_link_dealloc(link); 3342 } 3343 } 3344 3345 static void bpf_link_put_deferred(struct work_struct *work) 3346 { 3347 struct bpf_link *link = container_of(work, struct bpf_link, work); 3348 3349 bpf_link_free(link); 3350 } 3351 3352 /* bpf_link_put might be called from atomic context. It needs to be called 3353 * from sleepable context in order to acquire sleeping locks during the process. 3354 */ 3355 void bpf_link_put(struct bpf_link *link) 3356 { 3357 if (!atomic64_dec_and_test(&link->refcnt)) 3358 return; 3359 3360 INIT_WORK(&link->work, bpf_link_put_deferred); 3361 schedule_work(&link->work); 3362 } 3363 EXPORT_SYMBOL(bpf_link_put); 3364 3365 static void bpf_link_put_direct(struct bpf_link *link) 3366 { 3367 if (!atomic64_dec_and_test(&link->refcnt)) 3368 return; 3369 bpf_link_free(link); 3370 } 3371 3372 static int bpf_link_release(struct inode *inode, struct file *filp) 3373 { 3374 struct bpf_link *link = filp->private_data; 3375 3376 bpf_link_put_direct(link); 3377 return 0; 3378 } 3379 3380 #ifdef CONFIG_PROC_FS 3381 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) 3382 #define BPF_MAP_TYPE(_id, _ops) 3383 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name, 3384 static const char *bpf_link_type_strs[] = { 3385 [BPF_LINK_TYPE_UNSPEC] = "<invalid>", 3386 #include <linux/bpf_types.h> 3387 }; 3388 #undef BPF_PROG_TYPE 3389 #undef BPF_MAP_TYPE 3390 #undef BPF_LINK_TYPE 3391 3392 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp) 3393 { 3394 const struct bpf_link *link = filp->private_data; 3395 const struct bpf_prog *prog = link->prog; 3396 enum bpf_link_type type = link->type; 3397 char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 3398 3399 if (type < ARRAY_SIZE(bpf_link_type_strs) && bpf_link_type_strs[type]) { 3400 if (link->type == BPF_LINK_TYPE_KPROBE_MULTI) 3401 seq_printf(m, "link_type:\t%s\n", link->flags == BPF_F_KPROBE_MULTI_RETURN ? 3402 "kretprobe_multi" : "kprobe_multi"); 3403 else if (link->type == BPF_LINK_TYPE_UPROBE_MULTI) 3404 seq_printf(m, "link_type:\t%s\n", link->flags & BPF_F_UPROBE_MULTI_RETURN ? 3405 "uretprobe_multi" : "uprobe_multi"); 3406 else 3407 seq_printf(m, "link_type:\t%s\n", bpf_link_type_strs[type]); 3408 } else { 3409 WARN_ONCE(1, "missing BPF_LINK_TYPE(...) for link type %u\n", type); 3410 seq_printf(m, "link_type:\t<%u>\n", type); 3411 } 3412 seq_printf(m, "link_id:\t%u\n", link->id); 3413 3414 if (prog) { 3415 bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 3416 seq_printf(m, 3417 "prog_tag:\t%s\n" 3418 "prog_id:\t%u\n", 3419 prog_tag, 3420 prog->aux->id); 3421 } 3422 if (link->ops->show_fdinfo) 3423 link->ops->show_fdinfo(link, m); 3424 } 3425 #endif 3426 3427 static __poll_t bpf_link_poll(struct file *file, struct poll_table_struct *pts) 3428 { 3429 struct bpf_link *link = file->private_data; 3430 3431 return link->ops->poll(file, pts); 3432 } 3433 3434 static const struct file_operations bpf_link_fops = { 3435 #ifdef CONFIG_PROC_FS 3436 .show_fdinfo = bpf_link_show_fdinfo, 3437 #endif 3438 .release = bpf_link_release, 3439 .read = bpf_dummy_read, 3440 .write = bpf_dummy_write, 3441 }; 3442 3443 static const struct file_operations bpf_link_fops_poll = { 3444 #ifdef CONFIG_PROC_FS 3445 .show_fdinfo = bpf_link_show_fdinfo, 3446 #endif 3447 .release = bpf_link_release, 3448 .read = bpf_dummy_read, 3449 .write = bpf_dummy_write, 3450 .poll = bpf_link_poll, 3451 }; 3452 3453 static int bpf_link_alloc_id(struct bpf_link *link) 3454 { 3455 int id; 3456 3457 idr_preload(GFP_KERNEL); 3458 spin_lock_bh(&link_idr_lock); 3459 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC); 3460 spin_unlock_bh(&link_idr_lock); 3461 idr_preload_end(); 3462 3463 return id; 3464 } 3465 3466 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file, 3467 * reserving unused FD and allocating ID from link_idr. This is to be paired 3468 * with bpf_link_settle() to install FD and ID and expose bpf_link to 3469 * user-space, if bpf_link is successfully attached. If not, bpf_link and 3470 * pre-allocated resources are to be freed with bpf_cleanup() call. All the 3471 * transient state is passed around in struct bpf_link_primer. 3472 * This is preferred way to create and initialize bpf_link, especially when 3473 * there are complicated and expensive operations in between creating bpf_link 3474 * itself and attaching it to BPF hook. By using bpf_link_prime() and 3475 * bpf_link_settle() kernel code using bpf_link doesn't have to perform 3476 * expensive (and potentially failing) roll back operations in a rare case 3477 * that file, FD, or ID can't be allocated. 3478 */ 3479 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer) 3480 { 3481 struct file *file; 3482 int fd, id; 3483 3484 fd = get_unused_fd_flags(O_CLOEXEC); 3485 if (fd < 0) 3486 return fd; 3487 3488 3489 id = bpf_link_alloc_id(link); 3490 if (id < 0) { 3491 put_unused_fd(fd); 3492 return id; 3493 } 3494 3495 file = anon_inode_getfile("bpf_link", 3496 link->ops->poll ? &bpf_link_fops_poll : &bpf_link_fops, 3497 link, O_CLOEXEC); 3498 if (IS_ERR(file)) { 3499 bpf_link_free_id(id); 3500 put_unused_fd(fd); 3501 return PTR_ERR(file); 3502 } 3503 3504 primer->link = link; 3505 primer->file = file; 3506 primer->fd = fd; 3507 primer->id = id; 3508 return 0; 3509 } 3510 3511 int bpf_link_settle(struct bpf_link_primer *primer) 3512 { 3513 /* make bpf_link fetchable by ID */ 3514 spin_lock_bh(&link_idr_lock); 3515 primer->link->id = primer->id; 3516 spin_unlock_bh(&link_idr_lock); 3517 /* make bpf_link fetchable by FD */ 3518 fd_install(primer->fd, primer->file); 3519 /* pass through installed FD */ 3520 return primer->fd; 3521 } 3522 3523 int bpf_link_new_fd(struct bpf_link *link) 3524 { 3525 return anon_inode_getfd("bpf-link", 3526 link->ops->poll ? &bpf_link_fops_poll : &bpf_link_fops, 3527 link, O_CLOEXEC); 3528 } 3529 3530 struct bpf_link *bpf_link_get_from_fd(u32 ufd) 3531 { 3532 CLASS(fd, f)(ufd); 3533 struct bpf_link *link; 3534 3535 if (fd_empty(f)) 3536 return ERR_PTR(-EBADF); 3537 if (fd_file(f)->f_op != &bpf_link_fops && fd_file(f)->f_op != &bpf_link_fops_poll) 3538 return ERR_PTR(-EINVAL); 3539 3540 link = fd_file(f)->private_data; 3541 bpf_link_inc(link); 3542 return link; 3543 } 3544 EXPORT_SYMBOL_NS(bpf_link_get_from_fd, "BPF_INTERNAL"); 3545 3546 static void bpf_tracing_link_release(struct bpf_link *link) 3547 { 3548 struct bpf_tracing_link *tr_link = 3549 container_of(link, struct bpf_tracing_link, link.link); 3550 3551 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link.node, 3552 tr_link->trampoline, 3553 tr_link->tgt_prog)); 3554 3555 bpf_trampoline_put(tr_link->trampoline); 3556 3557 /* tgt_prog is NULL if target is a kernel function */ 3558 if (tr_link->tgt_prog) 3559 bpf_prog_put(tr_link->tgt_prog); 3560 } 3561 3562 static void bpf_tracing_link_dealloc(struct bpf_link *link) 3563 { 3564 struct bpf_tracing_link *tr_link = container_of(link, struct bpf_tracing_link, link.link); 3565 3566 kfree(tr_link); 3567 } 3568 3569 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link, 3570 struct seq_file *seq) 3571 { 3572 struct bpf_tracing_link *tr_link = container_of(link, struct bpf_tracing_link, link.link); 3573 3574 u32 target_btf_id, target_obj_id; 3575 3576 bpf_trampoline_unpack_key(tr_link->trampoline->key, 3577 &target_obj_id, &target_btf_id); 3578 seq_printf(seq, 3579 "attach_type:\t%d\n" 3580 "target_obj_id:\t%u\n" 3581 "target_btf_id:\t%u\n" 3582 "cookie:\t%llu\n", 3583 link->attach_type, 3584 target_obj_id, 3585 target_btf_id, 3586 tr_link->link.node.cookie); 3587 } 3588 3589 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, 3590 struct bpf_link_info *info) 3591 { 3592 struct bpf_tracing_link *tr_link = container_of(link, struct bpf_tracing_link, link.link); 3593 3594 info->tracing.attach_type = link->attach_type; 3595 info->tracing.cookie = tr_link->link.node.cookie; 3596 bpf_trampoline_unpack_key(tr_link->trampoline->key, 3597 &info->tracing.target_obj_id, 3598 &info->tracing.target_btf_id); 3599 3600 return 0; 3601 } 3602 3603 static const struct bpf_link_ops bpf_tracing_link_lops = { 3604 .release = bpf_tracing_link_release, 3605 .dealloc = bpf_tracing_link_dealloc, 3606 .show_fdinfo = bpf_tracing_link_show_fdinfo, 3607 .fill_link_info = bpf_tracing_link_fill_link_info, 3608 }; 3609 3610 static int bpf_tracing_prog_attach(struct bpf_prog *prog, 3611 int tgt_prog_fd, 3612 u32 btf_id, 3613 u64 bpf_cookie, 3614 enum bpf_attach_type attach_type) 3615 { 3616 struct bpf_link_primer link_primer; 3617 struct bpf_prog *tgt_prog = NULL; 3618 struct bpf_trampoline *tr = NULL; 3619 struct bpf_tracing_link *link; 3620 u64 key = 0; 3621 int err; 3622 3623 switch (prog->type) { 3624 case BPF_PROG_TYPE_TRACING: 3625 if (prog->expected_attach_type != BPF_TRACE_FENTRY && 3626 prog->expected_attach_type != BPF_TRACE_FEXIT && 3627 prog->expected_attach_type != BPF_TRACE_FSESSION && 3628 prog->expected_attach_type != BPF_MODIFY_RETURN) { 3629 err = -EINVAL; 3630 goto out_put_prog; 3631 } 3632 break; 3633 case BPF_PROG_TYPE_EXT: 3634 if (prog->expected_attach_type != 0) { 3635 err = -EINVAL; 3636 goto out_put_prog; 3637 } 3638 break; 3639 case BPF_PROG_TYPE_LSM: 3640 if (prog->expected_attach_type != BPF_LSM_MAC) { 3641 err = -EINVAL; 3642 goto out_put_prog; 3643 } 3644 break; 3645 default: 3646 err = -EINVAL; 3647 goto out_put_prog; 3648 } 3649 3650 if (!!tgt_prog_fd != !!btf_id) { 3651 err = -EINVAL; 3652 goto out_put_prog; 3653 } 3654 3655 if (tgt_prog_fd) { 3656 /* 3657 * For now we only allow new targets for BPF_PROG_TYPE_EXT. If this 3658 * part would be changed to implement the same for 3659 * BPF_PROG_TYPE_TRACING, do not forget to update the way how 3660 * attach_tracing_prog flag is set. 3661 */ 3662 if (prog->type != BPF_PROG_TYPE_EXT) { 3663 err = -EINVAL; 3664 goto out_put_prog; 3665 } 3666 3667 tgt_prog = bpf_prog_get(tgt_prog_fd); 3668 if (IS_ERR(tgt_prog)) { 3669 err = PTR_ERR(tgt_prog); 3670 tgt_prog = NULL; 3671 goto out_put_prog; 3672 } 3673 3674 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id); 3675 } 3676 3677 link = kzalloc_obj(*link, GFP_USER); 3678 if (!link) { 3679 err = -ENOMEM; 3680 goto out_put_prog; 3681 } 3682 bpf_tramp_link_init(&link->link, BPF_LINK_TYPE_TRACING, 3683 &bpf_tracing_link_lops, prog, attach_type, bpf_cookie); 3684 3685 if (prog->expected_attach_type == BPF_TRACE_FSESSION) { 3686 link->fexit.link = &link->link.link; 3687 link->fexit.cookie = bpf_cookie; 3688 } 3689 3690 mutex_lock(&prog->aux->dst_mutex); 3691 3692 /* There are a few possible cases here: 3693 * 3694 * - if prog->aux->dst_trampoline is set, the program was just loaded 3695 * and not yet attached to anything, so we can use the values stored 3696 * in prog->aux 3697 * 3698 * - if prog->aux->dst_trampoline is NULL, the program has already been 3699 * attached to a target and its initial target was cleared (below) 3700 * 3701 * - if tgt_prog != NULL, the caller specified tgt_prog_fd + 3702 * target_btf_id using the link_create API. 3703 * 3704 * - if tgt_prog == NULL when this function was called using the old 3705 * raw_tracepoint_open API, and we need a target from prog->aux 3706 * 3707 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program 3708 * was detached and is going for re-attachment. 3709 * 3710 * - if prog->aux->dst_trampoline is NULL and tgt_prog and prog->aux->attach_btf 3711 * are NULL, then program was already attached and user did not provide 3712 * tgt_prog_fd so we have no way to find out or create trampoline 3713 */ 3714 if (!prog->aux->dst_trampoline && !tgt_prog) { 3715 /* 3716 * Allow re-attach for TRACING and LSM programs. If it's 3717 * currently linked, bpf_trampoline_link_prog will fail. 3718 * EXT programs need to specify tgt_prog_fd, so they 3719 * re-attach in separate code path. 3720 */ 3721 if (prog->type != BPF_PROG_TYPE_TRACING && 3722 prog->type != BPF_PROG_TYPE_LSM) { 3723 err = -EINVAL; 3724 goto out_unlock; 3725 } 3726 /* We can allow re-attach only if we have valid attach_btf. */ 3727 if (!prog->aux->attach_btf) { 3728 err = -EINVAL; 3729 goto out_unlock; 3730 } 3731 btf_id = prog->aux->attach_btf_id; 3732 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id); 3733 } 3734 3735 if (!prog->aux->dst_trampoline || 3736 (key && key != prog->aux->dst_trampoline->key)) { 3737 /* If there is no saved target, or the specified target is 3738 * different from the destination specified at load time, we 3739 * need a new trampoline and a check for compatibility 3740 */ 3741 struct bpf_attach_target_info tgt_info = {}; 3742 3743 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id, 3744 &tgt_info); 3745 if (err) 3746 goto out_unlock; 3747 3748 if (tgt_info.tgt_mod) { 3749 module_put(prog->aux->mod); 3750 prog->aux->mod = tgt_info.tgt_mod; 3751 } 3752 3753 tr = bpf_trampoline_get(key, &tgt_info); 3754 if (!tr) { 3755 err = -ENOMEM; 3756 goto out_unlock; 3757 } 3758 } else { 3759 /* The caller didn't specify a target, or the target was the 3760 * same as the destination supplied during program load. This 3761 * means we can reuse the trampoline and reference from program 3762 * load time, and there is no need to allocate a new one. This 3763 * can only happen once for any program, as the saved values in 3764 * prog->aux are cleared below. 3765 */ 3766 tr = prog->aux->dst_trampoline; 3767 tgt_prog = prog->aux->dst_prog; 3768 } 3769 /* 3770 * It is to prevent modifying struct pt_regs via kprobe_write_ctx=true 3771 * freplace prog. Without this check, kprobe_write_ctx=true freplace 3772 * prog is allowed to attach to kprobe_write_ctx=false kprobe prog, and 3773 * then modify the registers of the kprobe prog's target kernel 3774 * function. 3775 * 3776 * This also blocks the combination of uprobe+freplace, because it is 3777 * unable to recognize the use of the tgt_prog as an uprobe or a kprobe 3778 * by tgt_prog itself. At attach time, uprobe/kprobe is recognized by 3779 * the target perf event flags in __perf_event_set_bpf_prog(). 3780 */ 3781 if (prog->type == BPF_PROG_TYPE_EXT && 3782 prog->aux->kprobe_write_ctx != tgt_prog->aux->kprobe_write_ctx) { 3783 err = -EINVAL; 3784 goto out_unlock; 3785 } 3786 3787 err = bpf_link_prime(&link->link.link, &link_primer); 3788 if (err) 3789 goto out_unlock; 3790 3791 err = bpf_trampoline_link_prog(&link->link.node, tr, tgt_prog); 3792 if (err) { 3793 bpf_link_cleanup(&link_primer); 3794 link = NULL; 3795 goto out_unlock; 3796 } 3797 3798 link->tgt_prog = tgt_prog; 3799 link->trampoline = tr; 3800 3801 /* Always clear the trampoline and target prog from prog->aux to make 3802 * sure the original attach destination is not kept alive after a 3803 * program is (re-)attached to another target. 3804 */ 3805 if (prog->aux->dst_prog && 3806 (tgt_prog_fd || tr != prog->aux->dst_trampoline)) 3807 /* got extra prog ref from syscall, or attaching to different prog */ 3808 bpf_prog_put(prog->aux->dst_prog); 3809 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline) 3810 /* we allocated a new trampoline, so free the old one */ 3811 bpf_trampoline_put(prog->aux->dst_trampoline); 3812 3813 prog->aux->dst_prog = NULL; 3814 prog->aux->dst_trampoline = NULL; 3815 mutex_unlock(&prog->aux->dst_mutex); 3816 3817 return bpf_link_settle(&link_primer); 3818 out_unlock: 3819 if (tr && tr != prog->aux->dst_trampoline) 3820 bpf_trampoline_put(tr); 3821 mutex_unlock(&prog->aux->dst_mutex); 3822 kfree(link); 3823 out_put_prog: 3824 if (tgt_prog_fd && tgt_prog) 3825 bpf_prog_put(tgt_prog); 3826 return err; 3827 } 3828 3829 static void bpf_raw_tp_link_release(struct bpf_link *link) 3830 { 3831 struct bpf_raw_tp_link *raw_tp = 3832 container_of(link, struct bpf_raw_tp_link, link); 3833 3834 bpf_probe_unregister(raw_tp->btp, raw_tp); 3835 bpf_put_raw_tracepoint(raw_tp->btp); 3836 } 3837 3838 static void bpf_raw_tp_link_dealloc(struct bpf_link *link) 3839 { 3840 struct bpf_raw_tp_link *raw_tp = 3841 container_of(link, struct bpf_raw_tp_link, link); 3842 3843 kfree(raw_tp); 3844 } 3845 3846 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link, 3847 struct seq_file *seq) 3848 { 3849 struct bpf_raw_tp_link *raw_tp_link = 3850 container_of(link, struct bpf_raw_tp_link, link); 3851 3852 seq_printf(seq, 3853 "tp_name:\t%s\n" 3854 "cookie:\t%llu\n", 3855 raw_tp_link->btp->tp->name, 3856 raw_tp_link->cookie); 3857 } 3858 3859 static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen, 3860 u32 len) 3861 { 3862 if (ulen >= len + 1) { 3863 if (copy_to_user(ubuf, buf, len + 1)) 3864 return -EFAULT; 3865 } else { 3866 char zero = '\0'; 3867 3868 if (copy_to_user(ubuf, buf, ulen - 1)) 3869 return -EFAULT; 3870 if (put_user(zero, ubuf + ulen - 1)) 3871 return -EFAULT; 3872 return -ENOSPC; 3873 } 3874 3875 return 0; 3876 } 3877 3878 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, 3879 struct bpf_link_info *info) 3880 { 3881 struct bpf_raw_tp_link *raw_tp_link = 3882 container_of(link, struct bpf_raw_tp_link, link); 3883 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name); 3884 const char *tp_name = raw_tp_link->btp->tp->name; 3885 u32 ulen = info->raw_tracepoint.tp_name_len; 3886 size_t tp_len = strlen(tp_name); 3887 3888 if (!ulen ^ !ubuf) 3889 return -EINVAL; 3890 3891 info->raw_tracepoint.tp_name_len = tp_len + 1; 3892 info->raw_tracepoint.cookie = raw_tp_link->cookie; 3893 3894 if (!ubuf) 3895 return 0; 3896 3897 return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len); 3898 } 3899 3900 static const struct bpf_link_ops bpf_raw_tp_link_lops = { 3901 .release = bpf_raw_tp_link_release, 3902 .dealloc_deferred = bpf_raw_tp_link_dealloc, 3903 .show_fdinfo = bpf_raw_tp_link_show_fdinfo, 3904 .fill_link_info = bpf_raw_tp_link_fill_link_info, 3905 }; 3906 3907 #ifdef CONFIG_PERF_EVENTS 3908 struct bpf_perf_link { 3909 struct bpf_link link; 3910 struct file *perf_file; 3911 }; 3912 3913 static void bpf_perf_link_release(struct bpf_link *link) 3914 { 3915 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); 3916 struct perf_event *event = perf_link->perf_file->private_data; 3917 3918 perf_event_free_bpf_prog(event); 3919 fput(perf_link->perf_file); 3920 } 3921 3922 static void bpf_perf_link_dealloc(struct bpf_link *link) 3923 { 3924 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); 3925 3926 kfree(perf_link); 3927 } 3928 3929 static int bpf_perf_link_fill_common(const struct perf_event *event, 3930 char __user *uname, u32 *ulenp, 3931 u64 *probe_offset, u64 *probe_addr, 3932 u32 *fd_type, unsigned long *missed) 3933 { 3934 const char *buf; 3935 u32 prog_id, ulen; 3936 size_t len; 3937 int err; 3938 3939 ulen = *ulenp; 3940 if (!ulen ^ !uname) 3941 return -EINVAL; 3942 3943 err = bpf_get_perf_event_info(event, &prog_id, fd_type, &buf, 3944 probe_offset, probe_addr, missed); 3945 if (err) 3946 return err; 3947 3948 if (buf) { 3949 len = strlen(buf); 3950 *ulenp = len + 1; 3951 } else { 3952 *ulenp = 1; 3953 } 3954 if (!uname) 3955 return 0; 3956 3957 if (buf) { 3958 err = bpf_copy_to_user(uname, buf, ulen, len); 3959 if (err) 3960 return err; 3961 } else { 3962 char zero = '\0'; 3963 3964 if (put_user(zero, uname)) 3965 return -EFAULT; 3966 } 3967 return 0; 3968 } 3969 3970 #ifdef CONFIG_KPROBE_EVENTS 3971 static int bpf_perf_link_fill_kprobe(const struct perf_event *event, 3972 struct bpf_link_info *info) 3973 { 3974 unsigned long missed; 3975 char __user *uname; 3976 u64 addr, offset; 3977 u32 ulen, type; 3978 int err; 3979 3980 uname = u64_to_user_ptr(info->perf_event.kprobe.func_name); 3981 ulen = info->perf_event.kprobe.name_len; 3982 err = bpf_perf_link_fill_common(event, uname, &ulen, &offset, &addr, 3983 &type, &missed); 3984 if (err) 3985 return err; 3986 if (type == BPF_FD_TYPE_KRETPROBE) 3987 info->perf_event.type = BPF_PERF_EVENT_KRETPROBE; 3988 else 3989 info->perf_event.type = BPF_PERF_EVENT_KPROBE; 3990 info->perf_event.kprobe.name_len = ulen; 3991 info->perf_event.kprobe.offset = offset; 3992 info->perf_event.kprobe.missed = missed; 3993 if (!kallsyms_show_value(current_cred())) 3994 addr = 0; 3995 info->perf_event.kprobe.addr = addr; 3996 info->perf_event.kprobe.cookie = event->bpf_cookie; 3997 return 0; 3998 } 3999 4000 static void bpf_perf_link_fdinfo_kprobe(const struct perf_event *event, 4001 struct seq_file *seq) 4002 { 4003 const char *name; 4004 int err; 4005 u32 prog_id, type; 4006 u64 offset, addr; 4007 unsigned long missed; 4008 4009 err = bpf_get_perf_event_info(event, &prog_id, &type, &name, 4010 &offset, &addr, &missed); 4011 if (err) 4012 return; 4013 4014 seq_printf(seq, 4015 "name:\t%s\n" 4016 "offset:\t%#llx\n" 4017 "missed:\t%lu\n" 4018 "addr:\t%#llx\n" 4019 "event_type:\t%s\n" 4020 "cookie:\t%llu\n", 4021 name, offset, missed, addr, 4022 type == BPF_FD_TYPE_KRETPROBE ? "kretprobe" : "kprobe", 4023 event->bpf_cookie); 4024 } 4025 #endif 4026 4027 #ifdef CONFIG_UPROBE_EVENTS 4028 static int bpf_perf_link_fill_uprobe(const struct perf_event *event, 4029 struct bpf_link_info *info) 4030 { 4031 u64 ref_ctr_offset, offset; 4032 char __user *uname; 4033 u32 ulen, type; 4034 int err; 4035 4036 uname = u64_to_user_ptr(info->perf_event.uprobe.file_name); 4037 ulen = info->perf_event.uprobe.name_len; 4038 err = bpf_perf_link_fill_common(event, uname, &ulen, &offset, &ref_ctr_offset, 4039 &type, NULL); 4040 if (err) 4041 return err; 4042 4043 if (type == BPF_FD_TYPE_URETPROBE) 4044 info->perf_event.type = BPF_PERF_EVENT_URETPROBE; 4045 else 4046 info->perf_event.type = BPF_PERF_EVENT_UPROBE; 4047 info->perf_event.uprobe.name_len = ulen; 4048 info->perf_event.uprobe.offset = offset; 4049 info->perf_event.uprobe.cookie = event->bpf_cookie; 4050 info->perf_event.uprobe.ref_ctr_offset = ref_ctr_offset; 4051 return 0; 4052 } 4053 4054 static void bpf_perf_link_fdinfo_uprobe(const struct perf_event *event, 4055 struct seq_file *seq) 4056 { 4057 const char *name; 4058 int err; 4059 u32 prog_id, type; 4060 u64 offset, ref_ctr_offset; 4061 unsigned long missed; 4062 4063 err = bpf_get_perf_event_info(event, &prog_id, &type, &name, 4064 &offset, &ref_ctr_offset, &missed); 4065 if (err) 4066 return; 4067 4068 seq_printf(seq, 4069 "name:\t%s\n" 4070 "offset:\t%#llx\n" 4071 "ref_ctr_offset:\t%#llx\n" 4072 "event_type:\t%s\n" 4073 "cookie:\t%llu\n", 4074 name, offset, ref_ctr_offset, 4075 type == BPF_FD_TYPE_URETPROBE ? "uretprobe" : "uprobe", 4076 event->bpf_cookie); 4077 } 4078 #endif 4079 4080 static int bpf_perf_link_fill_probe(const struct perf_event *event, 4081 struct bpf_link_info *info) 4082 { 4083 #ifdef CONFIG_KPROBE_EVENTS 4084 if (event->tp_event->flags & TRACE_EVENT_FL_KPROBE) 4085 return bpf_perf_link_fill_kprobe(event, info); 4086 #endif 4087 #ifdef CONFIG_UPROBE_EVENTS 4088 if (event->tp_event->flags & TRACE_EVENT_FL_UPROBE) 4089 return bpf_perf_link_fill_uprobe(event, info); 4090 #endif 4091 return -EOPNOTSUPP; 4092 } 4093 4094 static int bpf_perf_link_fill_tracepoint(const struct perf_event *event, 4095 struct bpf_link_info *info) 4096 { 4097 char __user *uname; 4098 u32 ulen; 4099 int err; 4100 4101 uname = u64_to_user_ptr(info->perf_event.tracepoint.tp_name); 4102 ulen = info->perf_event.tracepoint.name_len; 4103 err = bpf_perf_link_fill_common(event, uname, &ulen, NULL, NULL, NULL, NULL); 4104 if (err) 4105 return err; 4106 4107 info->perf_event.type = BPF_PERF_EVENT_TRACEPOINT; 4108 info->perf_event.tracepoint.name_len = ulen; 4109 info->perf_event.tracepoint.cookie = event->bpf_cookie; 4110 return 0; 4111 } 4112 4113 static int bpf_perf_link_fill_perf_event(const struct perf_event *event, 4114 struct bpf_link_info *info) 4115 { 4116 info->perf_event.event.type = event->attr.type; 4117 info->perf_event.event.config = event->attr.config; 4118 info->perf_event.event.cookie = event->bpf_cookie; 4119 info->perf_event.type = BPF_PERF_EVENT_EVENT; 4120 return 0; 4121 } 4122 4123 static int bpf_perf_link_fill_link_info(const struct bpf_link *link, 4124 struct bpf_link_info *info) 4125 { 4126 struct bpf_perf_link *perf_link; 4127 const struct perf_event *event; 4128 4129 perf_link = container_of(link, struct bpf_perf_link, link); 4130 event = perf_get_event(perf_link->perf_file); 4131 if (IS_ERR(event)) 4132 return PTR_ERR(event); 4133 4134 switch (event->prog->type) { 4135 case BPF_PROG_TYPE_PERF_EVENT: 4136 return bpf_perf_link_fill_perf_event(event, info); 4137 case BPF_PROG_TYPE_TRACEPOINT: 4138 return bpf_perf_link_fill_tracepoint(event, info); 4139 case BPF_PROG_TYPE_KPROBE: 4140 return bpf_perf_link_fill_probe(event, info); 4141 default: 4142 return -EOPNOTSUPP; 4143 } 4144 } 4145 4146 static void bpf_perf_event_link_show_fdinfo(const struct perf_event *event, 4147 struct seq_file *seq) 4148 { 4149 seq_printf(seq, 4150 "type:\t%u\n" 4151 "config:\t%llu\n" 4152 "event_type:\t%s\n" 4153 "cookie:\t%llu\n", 4154 event->attr.type, event->attr.config, 4155 "event", event->bpf_cookie); 4156 } 4157 4158 static void bpf_tracepoint_link_show_fdinfo(const struct perf_event *event, 4159 struct seq_file *seq) 4160 { 4161 int err; 4162 const char *name; 4163 u32 prog_id; 4164 4165 err = bpf_get_perf_event_info(event, &prog_id, NULL, &name, NULL, 4166 NULL, NULL); 4167 if (err) 4168 return; 4169 4170 seq_printf(seq, 4171 "tp_name:\t%s\n" 4172 "event_type:\t%s\n" 4173 "cookie:\t%llu\n", 4174 name, "tracepoint", event->bpf_cookie); 4175 } 4176 4177 static void bpf_probe_link_show_fdinfo(const struct perf_event *event, 4178 struct seq_file *seq) 4179 { 4180 #ifdef CONFIG_KPROBE_EVENTS 4181 if (event->tp_event->flags & TRACE_EVENT_FL_KPROBE) 4182 return bpf_perf_link_fdinfo_kprobe(event, seq); 4183 #endif 4184 4185 #ifdef CONFIG_UPROBE_EVENTS 4186 if (event->tp_event->flags & TRACE_EVENT_FL_UPROBE) 4187 return bpf_perf_link_fdinfo_uprobe(event, seq); 4188 #endif 4189 } 4190 4191 static void bpf_perf_link_show_fdinfo(const struct bpf_link *link, 4192 struct seq_file *seq) 4193 { 4194 struct bpf_perf_link *perf_link; 4195 const struct perf_event *event; 4196 4197 perf_link = container_of(link, struct bpf_perf_link, link); 4198 event = perf_get_event(perf_link->perf_file); 4199 if (IS_ERR(event)) 4200 return; 4201 4202 switch (event->prog->type) { 4203 case BPF_PROG_TYPE_PERF_EVENT: 4204 return bpf_perf_event_link_show_fdinfo(event, seq); 4205 case BPF_PROG_TYPE_TRACEPOINT: 4206 return bpf_tracepoint_link_show_fdinfo(event, seq); 4207 case BPF_PROG_TYPE_KPROBE: 4208 return bpf_probe_link_show_fdinfo(event, seq); 4209 default: 4210 return; 4211 } 4212 } 4213 4214 static const struct bpf_link_ops bpf_perf_link_lops = { 4215 .release = bpf_perf_link_release, 4216 .dealloc = bpf_perf_link_dealloc, 4217 .fill_link_info = bpf_perf_link_fill_link_info, 4218 .show_fdinfo = bpf_perf_link_show_fdinfo, 4219 }; 4220 4221 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 4222 { 4223 struct bpf_link_primer link_primer; 4224 struct bpf_perf_link *link; 4225 struct perf_event *event; 4226 struct file *perf_file; 4227 int err; 4228 4229 if (attr->link_create.flags) 4230 return -EINVAL; 4231 4232 perf_file = perf_event_get(attr->link_create.target_fd); 4233 if (IS_ERR(perf_file)) 4234 return PTR_ERR(perf_file); 4235 4236 link = kzalloc_obj(*link, GFP_USER); 4237 if (!link) { 4238 err = -ENOMEM; 4239 goto out_put_file; 4240 } 4241 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog, 4242 attr->link_create.attach_type); 4243 link->perf_file = perf_file; 4244 4245 err = bpf_link_prime(&link->link, &link_primer); 4246 if (err) { 4247 kfree(link); 4248 goto out_put_file; 4249 } 4250 4251 event = perf_file->private_data; 4252 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie); 4253 if (err) { 4254 bpf_link_cleanup(&link_primer); 4255 goto out_put_file; 4256 } 4257 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */ 4258 bpf_prog_inc(prog); 4259 4260 return bpf_link_settle(&link_primer); 4261 4262 out_put_file: 4263 fput(perf_file); 4264 return err; 4265 } 4266 #else 4267 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 4268 { 4269 return -EOPNOTSUPP; 4270 } 4271 #endif /* CONFIG_PERF_EVENTS */ 4272 4273 static int bpf_raw_tp_link_attach(struct bpf_prog *prog, 4274 const char __user *user_tp_name, u64 cookie, 4275 enum bpf_attach_type attach_type) 4276 { 4277 struct bpf_link_primer link_primer; 4278 struct bpf_raw_tp_link *link; 4279 struct bpf_raw_event_map *btp; 4280 const char *tp_name; 4281 char buf[128]; 4282 int err; 4283 4284 switch (prog->type) { 4285 case BPF_PROG_TYPE_TRACING: 4286 case BPF_PROG_TYPE_EXT: 4287 case BPF_PROG_TYPE_LSM: 4288 if (user_tp_name) 4289 /* The attach point for this category of programs 4290 * should be specified via btf_id during program load. 4291 */ 4292 return -EINVAL; 4293 if (prog->type == BPF_PROG_TYPE_TRACING && 4294 prog->expected_attach_type == BPF_TRACE_RAW_TP) { 4295 tp_name = prog->aux->attach_func_name; 4296 break; 4297 } 4298 return bpf_tracing_prog_attach(prog, 0, 0, 0, attach_type); 4299 case BPF_PROG_TYPE_RAW_TRACEPOINT: 4300 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 4301 if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0) 4302 return -EFAULT; 4303 buf[sizeof(buf) - 1] = 0; 4304 tp_name = buf; 4305 break; 4306 default: 4307 return -EINVAL; 4308 } 4309 4310 btp = bpf_get_raw_tracepoint(tp_name); 4311 if (!btp) 4312 return -ENOENT; 4313 4314 if (prog->sleepable && !tracepoint_is_faultable(btp->tp)) { 4315 bpf_put_raw_tracepoint(btp); 4316 return -EINVAL; 4317 } 4318 4319 link = kzalloc_obj(*link, GFP_USER); 4320 if (!link) { 4321 err = -ENOMEM; 4322 goto out_put_btp; 4323 } 4324 bpf_link_init_sleepable(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT, 4325 &bpf_raw_tp_link_lops, prog, attach_type, 4326 tracepoint_is_faultable(btp->tp)); 4327 link->btp = btp; 4328 link->cookie = cookie; 4329 4330 err = bpf_link_prime(&link->link, &link_primer); 4331 if (err) { 4332 kfree(link); 4333 goto out_put_btp; 4334 } 4335 4336 err = bpf_probe_register(link->btp, link); 4337 if (err) { 4338 bpf_link_cleanup(&link_primer); 4339 goto out_put_btp; 4340 } 4341 4342 return bpf_link_settle(&link_primer); 4343 4344 out_put_btp: 4345 bpf_put_raw_tracepoint(btp); 4346 return err; 4347 } 4348 4349 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.cookie 4350 4351 static int bpf_raw_tracepoint_open(const union bpf_attr *attr) 4352 { 4353 struct bpf_prog *prog; 4354 void __user *tp_name; 4355 __u64 cookie; 4356 int fd; 4357 4358 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN)) 4359 return -EINVAL; 4360 4361 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); 4362 if (IS_ERR(prog)) 4363 return PTR_ERR(prog); 4364 4365 tp_name = u64_to_user_ptr(attr->raw_tracepoint.name); 4366 cookie = attr->raw_tracepoint.cookie; 4367 fd = bpf_raw_tp_link_attach(prog, tp_name, cookie, prog->expected_attach_type); 4368 if (fd < 0) 4369 bpf_prog_put(prog); 4370 return fd; 4371 } 4372 4373 static enum bpf_prog_type 4374 attach_type_to_prog_type(enum bpf_attach_type attach_type) 4375 { 4376 switch (attach_type) { 4377 case BPF_CGROUP_INET_INGRESS: 4378 case BPF_CGROUP_INET_EGRESS: 4379 return BPF_PROG_TYPE_CGROUP_SKB; 4380 case BPF_CGROUP_INET_SOCK_CREATE: 4381 case BPF_CGROUP_INET_SOCK_RELEASE: 4382 case BPF_CGROUP_INET4_POST_BIND: 4383 case BPF_CGROUP_INET6_POST_BIND: 4384 return BPF_PROG_TYPE_CGROUP_SOCK; 4385 case BPF_CGROUP_INET4_BIND: 4386 case BPF_CGROUP_INET6_BIND: 4387 case BPF_CGROUP_INET4_CONNECT: 4388 case BPF_CGROUP_INET6_CONNECT: 4389 case BPF_CGROUP_UNIX_CONNECT: 4390 case BPF_CGROUP_INET4_GETPEERNAME: 4391 case BPF_CGROUP_INET6_GETPEERNAME: 4392 case BPF_CGROUP_UNIX_GETPEERNAME: 4393 case BPF_CGROUP_INET4_GETSOCKNAME: 4394 case BPF_CGROUP_INET6_GETSOCKNAME: 4395 case BPF_CGROUP_UNIX_GETSOCKNAME: 4396 case BPF_CGROUP_UDP4_SENDMSG: 4397 case BPF_CGROUP_UDP6_SENDMSG: 4398 case BPF_CGROUP_UNIX_SENDMSG: 4399 case BPF_CGROUP_UDP4_RECVMSG: 4400 case BPF_CGROUP_UDP6_RECVMSG: 4401 case BPF_CGROUP_UNIX_RECVMSG: 4402 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 4403 case BPF_CGROUP_SOCK_OPS: 4404 return BPF_PROG_TYPE_SOCK_OPS; 4405 case BPF_CGROUP_DEVICE: 4406 return BPF_PROG_TYPE_CGROUP_DEVICE; 4407 case BPF_SK_MSG_VERDICT: 4408 return BPF_PROG_TYPE_SK_MSG; 4409 case BPF_SK_SKB_STREAM_PARSER: 4410 case BPF_SK_SKB_STREAM_VERDICT: 4411 case BPF_SK_SKB_VERDICT: 4412 return BPF_PROG_TYPE_SK_SKB; 4413 case BPF_LIRC_MODE2: 4414 return BPF_PROG_TYPE_LIRC_MODE2; 4415 case BPF_FLOW_DISSECTOR: 4416 return BPF_PROG_TYPE_FLOW_DISSECTOR; 4417 case BPF_CGROUP_SYSCTL: 4418 return BPF_PROG_TYPE_CGROUP_SYSCTL; 4419 case BPF_CGROUP_GETSOCKOPT: 4420 case BPF_CGROUP_SETSOCKOPT: 4421 return BPF_PROG_TYPE_CGROUP_SOCKOPT; 4422 case BPF_TRACE_ITER: 4423 case BPF_TRACE_RAW_TP: 4424 case BPF_TRACE_FENTRY: 4425 case BPF_TRACE_FEXIT: 4426 case BPF_TRACE_FSESSION: 4427 case BPF_TRACE_FSESSION_MULTI: 4428 case BPF_TRACE_FENTRY_MULTI: 4429 case BPF_TRACE_FEXIT_MULTI: 4430 case BPF_MODIFY_RETURN: 4431 return BPF_PROG_TYPE_TRACING; 4432 case BPF_LSM_MAC: 4433 return BPF_PROG_TYPE_LSM; 4434 case BPF_SK_LOOKUP: 4435 return BPF_PROG_TYPE_SK_LOOKUP; 4436 case BPF_XDP: 4437 return BPF_PROG_TYPE_XDP; 4438 case BPF_LSM_CGROUP: 4439 return BPF_PROG_TYPE_LSM; 4440 case BPF_TCX_INGRESS: 4441 case BPF_TCX_EGRESS: 4442 case BPF_NETKIT_PRIMARY: 4443 case BPF_NETKIT_PEER: 4444 return BPF_PROG_TYPE_SCHED_CLS; 4445 default: 4446 return BPF_PROG_TYPE_UNSPEC; 4447 } 4448 } 4449 4450 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, 4451 enum bpf_attach_type attach_type) 4452 { 4453 enum bpf_prog_type ptype; 4454 4455 switch (prog->type) { 4456 case BPF_PROG_TYPE_CGROUP_SOCK: 4457 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 4458 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 4459 case BPF_PROG_TYPE_SK_LOOKUP: 4460 return attach_type == prog->expected_attach_type ? 0 : -EINVAL; 4461 case BPF_PROG_TYPE_CGROUP_SKB: 4462 if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN)) 4463 /* cg-skb progs can be loaded by unpriv user. 4464 * check permissions at attach time. 4465 */ 4466 return -EPERM; 4467 4468 ptype = attach_type_to_prog_type(attach_type); 4469 if (prog->type != ptype) 4470 return -EINVAL; 4471 4472 return prog->enforce_expected_attach_type && 4473 prog->expected_attach_type != attach_type ? 4474 -EINVAL : 0; 4475 case BPF_PROG_TYPE_EXT: 4476 return 0; 4477 case BPF_PROG_TYPE_NETFILTER: 4478 if (attach_type != BPF_NETFILTER) 4479 return -EINVAL; 4480 return 0; 4481 case BPF_PROG_TYPE_PERF_EVENT: 4482 case BPF_PROG_TYPE_TRACEPOINT: 4483 if (attach_type != BPF_PERF_EVENT) 4484 return -EINVAL; 4485 return 0; 4486 case BPF_PROG_TYPE_KPROBE: 4487 if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI && 4488 attach_type != BPF_TRACE_KPROBE_MULTI) 4489 return -EINVAL; 4490 if (prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION && 4491 attach_type != BPF_TRACE_KPROBE_SESSION) 4492 return -EINVAL; 4493 if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI && 4494 attach_type != BPF_TRACE_UPROBE_MULTI) 4495 return -EINVAL; 4496 if (prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION && 4497 attach_type != BPF_TRACE_UPROBE_SESSION) 4498 return -EINVAL; 4499 if (attach_type != BPF_PERF_EVENT && 4500 attach_type != BPF_TRACE_KPROBE_MULTI && 4501 attach_type != BPF_TRACE_KPROBE_SESSION && 4502 attach_type != BPF_TRACE_UPROBE_MULTI && 4503 attach_type != BPF_TRACE_UPROBE_SESSION) 4504 return -EINVAL; 4505 return 0; 4506 case BPF_PROG_TYPE_SCHED_CLS: 4507 if (attach_type != BPF_TCX_INGRESS && 4508 attach_type != BPF_TCX_EGRESS && 4509 attach_type != BPF_NETKIT_PRIMARY && 4510 attach_type != BPF_NETKIT_PEER) 4511 return -EINVAL; 4512 return 0; 4513 default: 4514 ptype = attach_type_to_prog_type(attach_type); 4515 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) 4516 return -EINVAL; 4517 return 0; 4518 } 4519 } 4520 4521 static bool is_cgroup_prog_type(enum bpf_prog_type ptype, enum bpf_attach_type atype, 4522 bool check_atype) 4523 { 4524 switch (ptype) { 4525 case BPF_PROG_TYPE_CGROUP_DEVICE: 4526 case BPF_PROG_TYPE_CGROUP_SKB: 4527 case BPF_PROG_TYPE_CGROUP_SOCK: 4528 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 4529 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 4530 case BPF_PROG_TYPE_CGROUP_SYSCTL: 4531 case BPF_PROG_TYPE_SOCK_OPS: 4532 return true; 4533 case BPF_PROG_TYPE_LSM: 4534 return check_atype ? atype == BPF_LSM_CGROUP : true; 4535 default: 4536 return false; 4537 } 4538 } 4539 4540 #define BPF_PROG_ATTACH_LAST_FIELD expected_revision 4541 4542 #define BPF_F_ATTACH_MASK_BASE \ 4543 (BPF_F_ALLOW_OVERRIDE | \ 4544 BPF_F_ALLOW_MULTI | \ 4545 BPF_F_REPLACE | \ 4546 BPF_F_PREORDER) 4547 4548 #define BPF_F_ATTACH_MASK_MPROG \ 4549 (BPF_F_REPLACE | \ 4550 BPF_F_BEFORE | \ 4551 BPF_F_AFTER | \ 4552 BPF_F_ID | \ 4553 BPF_F_LINK) 4554 4555 static int bpf_prog_attach(const union bpf_attr *attr) 4556 { 4557 enum bpf_prog_type ptype; 4558 struct bpf_prog *prog; 4559 int ret; 4560 4561 if (CHECK_ATTR(BPF_PROG_ATTACH)) 4562 return -EINVAL; 4563 4564 ptype = attach_type_to_prog_type(attr->attach_type); 4565 if (ptype == BPF_PROG_TYPE_UNSPEC) 4566 return -EINVAL; 4567 if (bpf_mprog_supported(ptype)) { 4568 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG) 4569 return -EINVAL; 4570 } else if (is_cgroup_prog_type(ptype, 0, false)) { 4571 if (attr->attach_flags & ~(BPF_F_ATTACH_MASK_BASE | BPF_F_ATTACH_MASK_MPROG)) 4572 return -EINVAL; 4573 } else { 4574 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_BASE) 4575 return -EINVAL; 4576 if (attr->relative_fd || 4577 attr->expected_revision) 4578 return -EINVAL; 4579 } 4580 4581 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 4582 if (IS_ERR(prog)) 4583 return PTR_ERR(prog); 4584 4585 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { 4586 bpf_prog_put(prog); 4587 return -EINVAL; 4588 } 4589 4590 if (is_cgroup_prog_type(ptype, prog->expected_attach_type, true)) { 4591 ret = cgroup_bpf_prog_attach(attr, ptype, prog); 4592 goto out; 4593 } 4594 4595 switch (ptype) { 4596 case BPF_PROG_TYPE_SK_SKB: 4597 case BPF_PROG_TYPE_SK_MSG: 4598 ret = sock_map_get_from_fd(attr, prog); 4599 break; 4600 case BPF_PROG_TYPE_LIRC_MODE2: 4601 ret = lirc_prog_attach(attr, prog); 4602 break; 4603 case BPF_PROG_TYPE_FLOW_DISSECTOR: 4604 ret = netns_bpf_prog_attach(attr, prog); 4605 break; 4606 case BPF_PROG_TYPE_SCHED_CLS: 4607 if (attr->attach_type == BPF_TCX_INGRESS || 4608 attr->attach_type == BPF_TCX_EGRESS) 4609 ret = tcx_prog_attach(attr, prog); 4610 else 4611 ret = netkit_prog_attach(attr, prog); 4612 break; 4613 default: 4614 ret = -EINVAL; 4615 } 4616 out: 4617 if (ret) 4618 bpf_prog_put(prog); 4619 return ret; 4620 } 4621 4622 #define BPF_PROG_DETACH_LAST_FIELD expected_revision 4623 4624 static int bpf_prog_detach(const union bpf_attr *attr) 4625 { 4626 struct bpf_prog *prog = NULL; 4627 enum bpf_prog_type ptype; 4628 int ret; 4629 4630 if (CHECK_ATTR(BPF_PROG_DETACH)) 4631 return -EINVAL; 4632 4633 ptype = attach_type_to_prog_type(attr->attach_type); 4634 if (bpf_mprog_supported(ptype)) { 4635 if (ptype == BPF_PROG_TYPE_UNSPEC) 4636 return -EINVAL; 4637 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG) 4638 return -EINVAL; 4639 if (attr->attach_bpf_fd) { 4640 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 4641 if (IS_ERR(prog)) 4642 return PTR_ERR(prog); 4643 } else if (!bpf_mprog_detach_empty(ptype)) { 4644 return -EPERM; 4645 } 4646 } else if (is_cgroup_prog_type(ptype, 0, false)) { 4647 if (attr->attach_flags || attr->relative_fd) 4648 return -EINVAL; 4649 } else if (attr->attach_flags || 4650 attr->relative_fd || 4651 attr->expected_revision) { 4652 return -EINVAL; 4653 } 4654 4655 switch (ptype) { 4656 case BPF_PROG_TYPE_SK_MSG: 4657 case BPF_PROG_TYPE_SK_SKB: 4658 ret = sock_map_prog_detach(attr, ptype); 4659 break; 4660 case BPF_PROG_TYPE_LIRC_MODE2: 4661 ret = lirc_prog_detach(attr); 4662 break; 4663 case BPF_PROG_TYPE_FLOW_DISSECTOR: 4664 ret = netns_bpf_prog_detach(attr, ptype); 4665 break; 4666 case BPF_PROG_TYPE_CGROUP_DEVICE: 4667 case BPF_PROG_TYPE_CGROUP_SKB: 4668 case BPF_PROG_TYPE_CGROUP_SOCK: 4669 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 4670 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 4671 case BPF_PROG_TYPE_CGROUP_SYSCTL: 4672 case BPF_PROG_TYPE_SOCK_OPS: 4673 case BPF_PROG_TYPE_LSM: 4674 ret = cgroup_bpf_prog_detach(attr, ptype); 4675 break; 4676 case BPF_PROG_TYPE_SCHED_CLS: 4677 if (attr->attach_type == BPF_TCX_INGRESS || 4678 attr->attach_type == BPF_TCX_EGRESS) 4679 ret = tcx_prog_detach(attr, prog); 4680 else 4681 ret = netkit_prog_detach(attr, prog); 4682 break; 4683 default: 4684 ret = -EINVAL; 4685 } 4686 4687 if (prog) 4688 bpf_prog_put(prog); 4689 return ret; 4690 } 4691 4692 #define BPF_PROG_QUERY_LAST_FIELD query.revision 4693 4694 static int bpf_prog_query(const union bpf_attr *attr, 4695 union bpf_attr __user *uattr, u32 uattr_size) 4696 { 4697 if (!bpf_net_capable()) 4698 return -EPERM; 4699 if (CHECK_ATTR(BPF_PROG_QUERY)) 4700 return -EINVAL; 4701 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) 4702 return -EINVAL; 4703 4704 switch (attr->query.attach_type) { 4705 case BPF_CGROUP_INET_INGRESS: 4706 case BPF_CGROUP_INET_EGRESS: 4707 case BPF_CGROUP_INET_SOCK_CREATE: 4708 case BPF_CGROUP_INET_SOCK_RELEASE: 4709 case BPF_CGROUP_INET4_BIND: 4710 case BPF_CGROUP_INET6_BIND: 4711 case BPF_CGROUP_INET4_POST_BIND: 4712 case BPF_CGROUP_INET6_POST_BIND: 4713 case BPF_CGROUP_INET4_CONNECT: 4714 case BPF_CGROUP_INET6_CONNECT: 4715 case BPF_CGROUP_UNIX_CONNECT: 4716 case BPF_CGROUP_INET4_GETPEERNAME: 4717 case BPF_CGROUP_INET6_GETPEERNAME: 4718 case BPF_CGROUP_UNIX_GETPEERNAME: 4719 case BPF_CGROUP_INET4_GETSOCKNAME: 4720 case BPF_CGROUP_INET6_GETSOCKNAME: 4721 case BPF_CGROUP_UNIX_GETSOCKNAME: 4722 case BPF_CGROUP_UDP4_SENDMSG: 4723 case BPF_CGROUP_UDP6_SENDMSG: 4724 case BPF_CGROUP_UNIX_SENDMSG: 4725 case BPF_CGROUP_UDP4_RECVMSG: 4726 case BPF_CGROUP_UDP6_RECVMSG: 4727 case BPF_CGROUP_UNIX_RECVMSG: 4728 case BPF_CGROUP_SOCK_OPS: 4729 case BPF_CGROUP_DEVICE: 4730 case BPF_CGROUP_SYSCTL: 4731 case BPF_CGROUP_GETSOCKOPT: 4732 case BPF_CGROUP_SETSOCKOPT: 4733 case BPF_LSM_CGROUP: 4734 return cgroup_bpf_prog_query(attr, uattr, uattr_size); 4735 case BPF_LIRC_MODE2: 4736 return lirc_prog_query(attr, uattr); 4737 case BPF_FLOW_DISSECTOR: 4738 case BPF_SK_LOOKUP: 4739 return netns_bpf_prog_query(attr, uattr); 4740 case BPF_SK_SKB_STREAM_PARSER: 4741 case BPF_SK_SKB_STREAM_VERDICT: 4742 case BPF_SK_MSG_VERDICT: 4743 case BPF_SK_SKB_VERDICT: 4744 return sock_map_bpf_prog_query(attr, uattr); 4745 case BPF_TCX_INGRESS: 4746 case BPF_TCX_EGRESS: 4747 return tcx_prog_query(attr, uattr); 4748 case BPF_NETKIT_PRIMARY: 4749 case BPF_NETKIT_PEER: 4750 return netkit_prog_query(attr, uattr); 4751 default: 4752 return -EINVAL; 4753 } 4754 } 4755 4756 #define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size 4757 4758 static int bpf_prog_test_run(const union bpf_attr *attr, 4759 union bpf_attr __user *uattr) 4760 { 4761 struct bpf_prog *prog; 4762 int ret = -ENOTSUPP; 4763 4764 if (CHECK_ATTR(BPF_PROG_TEST_RUN)) 4765 return -EINVAL; 4766 4767 if ((attr->test.ctx_size_in && !attr->test.ctx_in) || 4768 (!attr->test.ctx_size_in && attr->test.ctx_in)) 4769 return -EINVAL; 4770 4771 if ((attr->test.ctx_size_out && !attr->test.ctx_out) || 4772 (!attr->test.ctx_size_out && attr->test.ctx_out)) 4773 return -EINVAL; 4774 4775 prog = bpf_prog_get(attr->test.prog_fd); 4776 if (IS_ERR(prog)) 4777 return PTR_ERR(prog); 4778 4779 if (prog->aux->ops->test_run) 4780 ret = prog->aux->ops->test_run(prog, attr, uattr); 4781 4782 bpf_prog_put(prog); 4783 return ret; 4784 } 4785 4786 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id 4787 4788 static int bpf_obj_get_next_id(const union bpf_attr *attr, 4789 union bpf_attr __user *uattr, 4790 struct idr *idr, 4791 spinlock_t *lock) 4792 { 4793 u32 next_id = attr->start_id; 4794 int err = 0; 4795 4796 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) 4797 return -EINVAL; 4798 4799 if (!capable(CAP_SYS_ADMIN)) 4800 return -EPERM; 4801 4802 next_id++; 4803 spin_lock_bh(lock); 4804 if (!idr_get_next(idr, &next_id)) 4805 err = -ENOENT; 4806 spin_unlock_bh(lock); 4807 4808 if (!err) 4809 err = put_user(next_id, &uattr->next_id); 4810 4811 return err; 4812 } 4813 4814 struct bpf_map *bpf_map_get_curr_or_next(u32 *id) 4815 { 4816 struct bpf_map *map; 4817 4818 spin_lock_bh(&map_idr_lock); 4819 again: 4820 map = idr_get_next(&map_idr, id); 4821 if (map) { 4822 map = __bpf_map_inc_not_zero(map, false); 4823 if (IS_ERR(map)) { 4824 (*id)++; 4825 goto again; 4826 } 4827 } 4828 spin_unlock_bh(&map_idr_lock); 4829 4830 return map; 4831 } 4832 4833 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id) 4834 { 4835 struct bpf_prog *prog; 4836 4837 spin_lock_bh(&prog_idr_lock); 4838 again: 4839 prog = idr_get_next(&prog_idr, id); 4840 if (prog) { 4841 prog = bpf_prog_inc_not_zero(prog); 4842 if (IS_ERR(prog)) { 4843 (*id)++; 4844 goto again; 4845 } 4846 } 4847 spin_unlock_bh(&prog_idr_lock); 4848 4849 return prog; 4850 } 4851 4852 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id 4853 4854 struct bpf_prog *bpf_prog_by_id(u32 id) 4855 { 4856 struct bpf_prog *prog; 4857 4858 if (!id) 4859 return ERR_PTR(-ENOENT); 4860 4861 spin_lock_bh(&prog_idr_lock); 4862 prog = idr_find(&prog_idr, id); 4863 if (prog) 4864 prog = bpf_prog_inc_not_zero(prog); 4865 else 4866 prog = ERR_PTR(-ENOENT); 4867 spin_unlock_bh(&prog_idr_lock); 4868 return prog; 4869 } 4870 4871 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) 4872 { 4873 struct bpf_prog *prog; 4874 u32 id = attr->prog_id; 4875 int fd; 4876 4877 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) 4878 return -EINVAL; 4879 4880 if (!capable(CAP_SYS_ADMIN)) 4881 return -EPERM; 4882 4883 prog = bpf_prog_by_id(id); 4884 if (IS_ERR(prog)) 4885 return PTR_ERR(prog); 4886 4887 fd = bpf_prog_new_fd(prog); 4888 if (fd < 0) 4889 bpf_prog_put(prog); 4890 4891 return fd; 4892 } 4893 4894 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags 4895 4896 static int bpf_map_get_fd_by_id(const union bpf_attr *attr) 4897 { 4898 struct bpf_map *map; 4899 u32 id = attr->map_id; 4900 int f_flags; 4901 int fd; 4902 4903 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || 4904 attr->open_flags & ~BPF_OBJ_FLAG_MASK) 4905 return -EINVAL; 4906 4907 if (!capable(CAP_SYS_ADMIN)) 4908 return -EPERM; 4909 4910 f_flags = bpf_get_file_flag(attr->open_flags); 4911 if (f_flags < 0) 4912 return f_flags; 4913 4914 spin_lock_bh(&map_idr_lock); 4915 map = idr_find(&map_idr, id); 4916 if (map) 4917 map = __bpf_map_inc_not_zero(map, true); 4918 else 4919 map = ERR_PTR(-ENOENT); 4920 spin_unlock_bh(&map_idr_lock); 4921 4922 if (IS_ERR(map)) 4923 return PTR_ERR(map); 4924 4925 fd = bpf_map_new_fd(map, f_flags); 4926 if (fd < 0) 4927 bpf_map_put_with_uref(map); 4928 4929 return fd; 4930 } 4931 4932 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, 4933 unsigned long addr, u32 *off, 4934 u32 *type) 4935 { 4936 const struct bpf_map *map; 4937 int i; 4938 4939 mutex_lock(&prog->aux->used_maps_mutex); 4940 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { 4941 map = prog->aux->used_maps[i]; 4942 if (map == (void *)addr) { 4943 *type = BPF_PSEUDO_MAP_FD; 4944 goto out; 4945 } 4946 if (!map->ops->map_direct_value_meta) 4947 continue; 4948 if (!map->ops->map_direct_value_meta(map, addr, off)) { 4949 *type = BPF_PSEUDO_MAP_VALUE; 4950 goto out; 4951 } 4952 } 4953 map = NULL; 4954 4955 out: 4956 mutex_unlock(&prog->aux->used_maps_mutex); 4957 return map; 4958 } 4959 4960 static void prepare_dump_pseudo_call(struct bpf_insn *insn) 4961 { 4962 s32 call_off = insn->imm; 4963 4964 /* 4965 * BPF_CALL_ARGS only exists for interpreter fallback. 4966 * 1. For interpreter (BPF_CALL_ARGS): insn->off is the index of 4967 * interpreters_args array, so here using bpf_call_args_imm() 4968 * to get the real address offset. 4969 * 2. For JIT (BPF_CALL): insn->off is the subprog id. 4970 */ 4971 if (insn->code == (BPF_JMP | BPF_CALL_ARGS)) 4972 insn->imm = bpf_call_args_imm(insn->off); 4973 else 4974 insn->imm = insn->off; 4975 4976 /* Avoid dumping a truncated and misleading pc-relative offset. */ 4977 if (call_off > S16_MAX || call_off < S16_MIN) 4978 insn->off = 0; 4979 else 4980 insn->off = call_off; 4981 } 4982 4983 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog, 4984 const struct cred *f_cred) 4985 { 4986 const struct bpf_map *map; 4987 struct bpf_insn *insns; 4988 u32 off, type; 4989 u64 imm; 4990 u8 code; 4991 int i; 4992 4993 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), 4994 GFP_USER); 4995 if (!insns) 4996 return insns; 4997 4998 for (i = 0; i < prog->len; i++) { 4999 code = insns[i].code; 5000 5001 if (code == (BPF_JMP | BPF_TAIL_CALL)) { 5002 insns[i].code = BPF_JMP | BPF_CALL; 5003 insns[i].imm = BPF_FUNC_tail_call; 5004 /* fall-through */ 5005 } 5006 if (code == (BPF_JMP | BPF_CALL) || 5007 code == (BPF_JMP | BPF_CALL_ARGS)) { 5008 /* Restore the legacy xlated dump layout. */ 5009 if (insns[i].src_reg == BPF_PSEUDO_CALL) 5010 prepare_dump_pseudo_call(&insns[i]); 5011 if (code == (BPF_JMP | BPF_CALL_ARGS)) 5012 insns[i].code = BPF_JMP | BPF_CALL; 5013 if (!bpf_dump_raw_ok(f_cred)) 5014 insns[i].imm = 0; 5015 continue; 5016 } 5017 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) { 5018 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM; 5019 continue; 5020 } 5021 5022 if ((BPF_CLASS(code) == BPF_LDX || BPF_CLASS(code) == BPF_STX || 5023 BPF_CLASS(code) == BPF_ST) && BPF_MODE(code) == BPF_PROBE_MEM32) { 5024 insns[i].code = BPF_CLASS(code) | BPF_SIZE(code) | BPF_MEM; 5025 continue; 5026 } 5027 5028 if (code != (BPF_LD | BPF_IMM | BPF_DW)) 5029 continue; 5030 5031 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; 5032 map = bpf_map_from_imm(prog, imm, &off, &type); 5033 if (map) { 5034 insns[i].src_reg = type; 5035 insns[i].imm = map->id; 5036 insns[i + 1].imm = off; 5037 continue; 5038 } 5039 } 5040 5041 return insns; 5042 } 5043 5044 static int set_info_rec_size(struct bpf_prog_info *info) 5045 { 5046 /* 5047 * Ensure info.*_rec_size is the same as kernel expected size 5048 * 5049 * or 5050 * 5051 * Only allow zero *_rec_size if both _rec_size and _cnt are 5052 * zero. In this case, the kernel will set the expected 5053 * _rec_size back to the info. 5054 */ 5055 5056 if ((info->nr_func_info || info->func_info_rec_size) && 5057 info->func_info_rec_size != sizeof(struct bpf_func_info)) 5058 return -EINVAL; 5059 5060 if ((info->nr_line_info || info->line_info_rec_size) && 5061 info->line_info_rec_size != sizeof(struct bpf_line_info)) 5062 return -EINVAL; 5063 5064 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && 5065 info->jited_line_info_rec_size != sizeof(__u64)) 5066 return -EINVAL; 5067 5068 info->func_info_rec_size = sizeof(struct bpf_func_info); 5069 info->line_info_rec_size = sizeof(struct bpf_line_info); 5070 info->jited_line_info_rec_size = sizeof(__u64); 5071 5072 return 0; 5073 } 5074 5075 static int bpf_prog_get_info_by_fd(struct file *file, 5076 struct bpf_prog *prog, 5077 const union bpf_attr *attr, 5078 union bpf_attr __user *uattr) 5079 { 5080 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5081 struct btf *attach_btf = bpf_prog_get_target_btf(prog); 5082 struct bpf_prog_info info; 5083 u32 info_len = attr->info.info_len; 5084 struct bpf_prog_kstats stats; 5085 char __user *uinsns; 5086 u32 ulen, len; 5087 int err; 5088 5089 len = offsetofend(struct bpf_prog_info, attach_btf_id); 5090 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), len, info_len); 5091 if (err) 5092 return err; 5093 info_len = min_t(u32, sizeof(info), info_len); 5094 5095 memset(&info, 0, sizeof(info)); 5096 if (copy_from_user(&info, uinfo, info_len)) 5097 return -EFAULT; 5098 5099 info.type = prog->type; 5100 info.id = prog->aux->id; 5101 info.load_time = prog->aux->load_time; 5102 info.created_by_uid = from_kuid_munged(current_user_ns(), 5103 prog->aux->user->uid); 5104 info.gpl_compatible = prog->gpl_compatible; 5105 5106 memcpy(info.tag, prog->tag, sizeof(prog->tag)); 5107 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); 5108 5109 mutex_lock(&prog->aux->used_maps_mutex); 5110 ulen = info.nr_map_ids; 5111 info.nr_map_ids = prog->aux->used_map_cnt; 5112 ulen = min_t(u32, info.nr_map_ids, ulen); 5113 if (ulen) { 5114 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); 5115 u32 i; 5116 5117 for (i = 0; i < ulen; i++) 5118 if (put_user(prog->aux->used_maps[i]->id, 5119 &user_map_ids[i])) { 5120 mutex_unlock(&prog->aux->used_maps_mutex); 5121 return -EFAULT; 5122 } 5123 } 5124 mutex_unlock(&prog->aux->used_maps_mutex); 5125 5126 err = set_info_rec_size(&info); 5127 if (err) 5128 return err; 5129 5130 bpf_prog_get_stats(prog, &stats); 5131 info.run_time_ns = stats.nsecs; 5132 info.run_cnt = stats.cnt; 5133 info.recursion_misses = stats.misses; 5134 5135 info.verified_insns = prog->aux->verified_insns; 5136 if (prog->aux->btf) 5137 info.btf_id = btf_obj_id(prog->aux->btf); 5138 5139 if (!bpf_capable()) { 5140 info.jited_prog_len = 0; 5141 info.xlated_prog_len = 0; 5142 info.nr_jited_ksyms = 0; 5143 info.nr_jited_func_lens = 0; 5144 info.nr_func_info = 0; 5145 info.nr_line_info = 0; 5146 info.nr_jited_line_info = 0; 5147 goto done; 5148 } 5149 5150 ulen = info.xlated_prog_len; 5151 info.xlated_prog_len = bpf_prog_insn_size(prog); 5152 if (info.xlated_prog_len && ulen) { 5153 struct bpf_insn *insns_sanitized; 5154 bool fault; 5155 5156 if (!prog->blinded || bpf_dump_raw_ok(file->f_cred)) { 5157 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred); 5158 if (!insns_sanitized) 5159 return -ENOMEM; 5160 uinsns = u64_to_user_ptr(info.xlated_prog_insns); 5161 ulen = min_t(u32, info.xlated_prog_len, ulen); 5162 fault = copy_to_user(uinsns, insns_sanitized, ulen); 5163 kfree(insns_sanitized); 5164 if (fault) 5165 return -EFAULT; 5166 } else { 5167 info.xlated_prog_insns = 0; 5168 } 5169 } 5170 5171 if (bpf_prog_is_offloaded(prog->aux)) { 5172 err = bpf_prog_offload_info_fill(&info, prog); 5173 if (err) 5174 return err; 5175 goto done; 5176 } 5177 5178 /* NOTE: the following code is supposed to be skipped for offload. 5179 * bpf_prog_offload_info_fill() is the place to fill similar fields 5180 * for offload. 5181 */ 5182 ulen = info.jited_prog_len; 5183 if (prog->aux->func_cnt) { 5184 u32 i; 5185 5186 info.jited_prog_len = 0; 5187 for (i = 0; i < prog->aux->func_cnt; i++) 5188 info.jited_prog_len += prog->aux->func[i]->jited_len; 5189 } else { 5190 info.jited_prog_len = prog->jited_len; 5191 } 5192 5193 if (info.jited_prog_len && ulen) { 5194 if (bpf_dump_raw_ok(file->f_cred)) { 5195 uinsns = u64_to_user_ptr(info.jited_prog_insns); 5196 ulen = min_t(u32, info.jited_prog_len, ulen); 5197 5198 /* for multi-function programs, copy the JITed 5199 * instructions for all the functions 5200 */ 5201 if (prog->aux->func_cnt) { 5202 u32 len, free, i; 5203 u8 *img; 5204 5205 free = ulen; 5206 for (i = 0; i < prog->aux->func_cnt; i++) { 5207 len = prog->aux->func[i]->jited_len; 5208 len = min_t(u32, len, free); 5209 img = (u8 *) prog->aux->func[i]->bpf_func; 5210 if (copy_to_user(uinsns, img, len)) 5211 return -EFAULT; 5212 uinsns += len; 5213 free -= len; 5214 if (!free) 5215 break; 5216 } 5217 } else { 5218 if (copy_to_user(uinsns, prog->bpf_func, ulen)) 5219 return -EFAULT; 5220 } 5221 } else { 5222 info.jited_prog_insns = 0; 5223 } 5224 } 5225 5226 ulen = info.nr_jited_ksyms; 5227 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; 5228 if (ulen) { 5229 if (bpf_dump_raw_ok(file->f_cred)) { 5230 unsigned long ksym_addr; 5231 u64 __user *user_ksyms; 5232 u32 i; 5233 5234 /* copy the address of the kernel symbol 5235 * corresponding to each function 5236 */ 5237 ulen = min_t(u32, info.nr_jited_ksyms, ulen); 5238 user_ksyms = u64_to_user_ptr(info.jited_ksyms); 5239 if (prog->aux->func_cnt) { 5240 for (i = 0; i < ulen; i++) { 5241 ksym_addr = (unsigned long) 5242 prog->aux->func[i]->bpf_func; 5243 if (put_user((u64) ksym_addr, 5244 &user_ksyms[i])) 5245 return -EFAULT; 5246 } 5247 } else { 5248 ksym_addr = (unsigned long) prog->bpf_func; 5249 if (put_user((u64) ksym_addr, &user_ksyms[0])) 5250 return -EFAULT; 5251 } 5252 } else { 5253 info.jited_ksyms = 0; 5254 } 5255 } 5256 5257 ulen = info.nr_jited_func_lens; 5258 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; 5259 if (ulen) { 5260 if (bpf_dump_raw_ok(file->f_cred)) { 5261 u32 __user *user_lens; 5262 u32 func_len, i; 5263 5264 /* copy the JITed image lengths for each function */ 5265 ulen = min_t(u32, info.nr_jited_func_lens, ulen); 5266 user_lens = u64_to_user_ptr(info.jited_func_lens); 5267 if (prog->aux->func_cnt) { 5268 for (i = 0; i < ulen; i++) { 5269 func_len = 5270 prog->aux->func[i]->jited_len; 5271 if (put_user(func_len, &user_lens[i])) 5272 return -EFAULT; 5273 } 5274 } else { 5275 func_len = prog->jited_len; 5276 if (put_user(func_len, &user_lens[0])) 5277 return -EFAULT; 5278 } 5279 } else { 5280 info.jited_func_lens = 0; 5281 } 5282 } 5283 5284 info.attach_btf_id = prog->aux->attach_btf_id; 5285 if (attach_btf) 5286 info.attach_btf_obj_id = btf_obj_id(attach_btf); 5287 5288 ulen = info.nr_func_info; 5289 info.nr_func_info = prog->aux->func_info_cnt; 5290 if (info.nr_func_info && ulen) { 5291 char __user *user_finfo; 5292 5293 user_finfo = u64_to_user_ptr(info.func_info); 5294 ulen = min_t(u32, info.nr_func_info, ulen); 5295 if (copy_to_user(user_finfo, prog->aux->func_info, 5296 info.func_info_rec_size * ulen)) 5297 return -EFAULT; 5298 } 5299 5300 ulen = info.nr_line_info; 5301 info.nr_line_info = prog->aux->nr_linfo; 5302 if (info.nr_line_info && ulen) { 5303 __u8 __user *user_linfo; 5304 5305 user_linfo = u64_to_user_ptr(info.line_info); 5306 ulen = min_t(u32, info.nr_line_info, ulen); 5307 if (copy_to_user(user_linfo, prog->aux->linfo, 5308 info.line_info_rec_size * ulen)) 5309 return -EFAULT; 5310 } 5311 5312 ulen = info.nr_jited_line_info; 5313 if (prog->aux->jited_linfo) 5314 info.nr_jited_line_info = prog->aux->nr_linfo; 5315 else 5316 info.nr_jited_line_info = 0; 5317 if (info.nr_jited_line_info && ulen) { 5318 if (bpf_dump_raw_ok(file->f_cred)) { 5319 unsigned long line_addr; 5320 __u64 __user *user_linfo; 5321 u32 i; 5322 5323 user_linfo = u64_to_user_ptr(info.jited_line_info); 5324 ulen = min_t(u32, info.nr_jited_line_info, ulen); 5325 for (i = 0; i < ulen; i++) { 5326 line_addr = (unsigned long)prog->aux->jited_linfo[i]; 5327 if (put_user((__u64)line_addr, &user_linfo[i])) 5328 return -EFAULT; 5329 } 5330 } else { 5331 info.jited_line_info = 0; 5332 } 5333 } 5334 5335 ulen = info.nr_prog_tags; 5336 info.nr_prog_tags = prog->aux->func_cnt ? : 1; 5337 if (ulen) { 5338 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; 5339 u32 i; 5340 5341 user_prog_tags = u64_to_user_ptr(info.prog_tags); 5342 ulen = min_t(u32, info.nr_prog_tags, ulen); 5343 if (prog->aux->func_cnt) { 5344 for (i = 0; i < ulen; i++) { 5345 if (copy_to_user(user_prog_tags[i], 5346 prog->aux->func[i]->tag, 5347 BPF_TAG_SIZE)) 5348 return -EFAULT; 5349 } 5350 } else { 5351 if (copy_to_user(user_prog_tags[0], 5352 prog->tag, BPF_TAG_SIZE)) 5353 return -EFAULT; 5354 } 5355 } 5356 5357 done: 5358 if (copy_to_user(uinfo, &info, info_len) || 5359 put_user(info_len, &uattr->info.info_len)) 5360 return -EFAULT; 5361 5362 return 0; 5363 } 5364 5365 static int bpf_map_get_info_by_fd(struct file *file, 5366 struct bpf_map *map, 5367 const union bpf_attr *attr, 5368 union bpf_attr __user *uattr) 5369 { 5370 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5371 struct bpf_map_info info; 5372 u32 info_len = attr->info.info_len, len; 5373 int err; 5374 5375 len = offsetofend(struct bpf_map_info, hash_size); 5376 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), len, info_len); 5377 if (err) 5378 return err; 5379 info_len = min_t(u32, sizeof(info), info_len); 5380 5381 memset(&info, 0, sizeof(info)); 5382 if (copy_from_user(&info, uinfo, info_len)) 5383 return -EFAULT; 5384 5385 info.type = map->map_type; 5386 info.id = map->id; 5387 info.key_size = map->key_size; 5388 info.value_size = map->value_size; 5389 info.max_entries = map->max_entries; 5390 info.map_flags = map->map_flags; 5391 info.map_extra = map->map_extra; 5392 memcpy(info.name, map->name, sizeof(map->name)); 5393 5394 if (map->btf) { 5395 info.btf_id = btf_obj_id(map->btf); 5396 info.btf_key_type_id = map->btf_key_type_id; 5397 info.btf_value_type_id = map->btf_value_type_id; 5398 } 5399 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; 5400 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) 5401 bpf_map_struct_ops_info_fill(&info, map); 5402 5403 if (bpf_map_is_offloaded(map)) { 5404 err = bpf_map_offload_info_fill(&info, map); 5405 if (err) 5406 return err; 5407 } 5408 5409 if (info.hash) { 5410 char __user *uhash = u64_to_user_ptr(info.hash); 5411 5412 if (!map->ops->map_get_hash) 5413 return -EINVAL; 5414 if (info.hash_size != sizeof(map->sha)) 5415 return -EINVAL; 5416 if (!READ_ONCE(map->frozen)) 5417 return -EPERM; 5418 5419 err = map->ops->map_get_hash(map); 5420 if (err != 0) 5421 return err; 5422 5423 if (copy_to_user(uhash, map->sha, sizeof(map->sha)) != 0) 5424 return -EFAULT; 5425 } else if (info.hash_size) { 5426 return -EINVAL; 5427 } 5428 5429 if (copy_to_user(uinfo, &info, info_len) || 5430 put_user(info_len, &uattr->info.info_len)) 5431 return -EFAULT; 5432 5433 return 0; 5434 } 5435 5436 static int bpf_btf_get_info_by_fd(struct file *file, 5437 struct btf *btf, 5438 const union bpf_attr *attr, 5439 union bpf_attr __user *uattr) 5440 { 5441 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5442 u32 info_len = attr->info.info_len; 5443 int err; 5444 5445 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len); 5446 if (err) 5447 return err; 5448 5449 return btf_get_info_by_fd(btf, attr, uattr); 5450 } 5451 5452 static int bpf_link_get_info_by_fd(struct file *file, 5453 struct bpf_link *link, 5454 const union bpf_attr *attr, 5455 union bpf_attr __user *uattr) 5456 { 5457 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5458 struct bpf_link_info info; 5459 u32 info_len = attr->info.info_len; 5460 int err; 5461 5462 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); 5463 if (err) 5464 return err; 5465 info_len = min_t(u32, sizeof(info), info_len); 5466 5467 memset(&info, 0, sizeof(info)); 5468 if (copy_from_user(&info, uinfo, info_len)) 5469 return -EFAULT; 5470 5471 info.type = link->type; 5472 info.id = link->id; 5473 if (link->prog) 5474 info.prog_id = link->prog->aux->id; 5475 5476 if (link->ops->fill_link_info) { 5477 err = link->ops->fill_link_info(link, &info); 5478 if (err) 5479 return err; 5480 } 5481 5482 if (copy_to_user(uinfo, &info, info_len) || 5483 put_user(info_len, &uattr->info.info_len)) 5484 return -EFAULT; 5485 5486 return 0; 5487 } 5488 5489 5490 static int token_get_info_by_fd(struct file *file, 5491 struct bpf_token *token, 5492 const union bpf_attr *attr, 5493 union bpf_attr __user *uattr) 5494 { 5495 struct bpf_token_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5496 u32 info_len = attr->info.info_len; 5497 int err; 5498 5499 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len); 5500 if (err) 5501 return err; 5502 return bpf_token_get_info_by_fd(token, attr, uattr); 5503 } 5504 5505 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info 5506 5507 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, 5508 union bpf_attr __user *uattr) 5509 { 5510 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) 5511 return -EINVAL; 5512 5513 CLASS(fd, f)(attr->info.bpf_fd); 5514 if (fd_empty(f)) 5515 return -EBADFD; 5516 5517 if (fd_file(f)->f_op == &bpf_prog_fops) 5518 return bpf_prog_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, 5519 uattr); 5520 else if (fd_file(f)->f_op == &bpf_map_fops) 5521 return bpf_map_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, 5522 uattr); 5523 else if (fd_file(f)->f_op == &btf_fops) 5524 return bpf_btf_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, uattr); 5525 else if (fd_file(f)->f_op == &bpf_link_fops || fd_file(f)->f_op == &bpf_link_fops_poll) 5526 return bpf_link_get_info_by_fd(fd_file(f), fd_file(f)->private_data, 5527 attr, uattr); 5528 else if (fd_file(f)->f_op == &bpf_token_fops) 5529 return token_get_info_by_fd(fd_file(f), fd_file(f)->private_data, 5530 attr, uattr); 5531 return -EINVAL; 5532 } 5533 5534 #define BPF_BTF_LOAD_LAST_FIELD btf_token_fd 5535 5536 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log) 5537 { 5538 struct bpf_token *token = NULL; 5539 5540 if (CHECK_ATTR(BPF_BTF_LOAD)) 5541 return -EINVAL; 5542 5543 if (attr->btf_flags & ~BPF_F_TOKEN_FD) 5544 return -EINVAL; 5545 5546 if (attr->btf_flags & BPF_F_TOKEN_FD) { 5547 token = bpf_token_get_from_fd(attr->btf_token_fd); 5548 if (IS_ERR(token)) 5549 return PTR_ERR(token); 5550 if (!bpf_token_allow_cmd(token, BPF_BTF_LOAD)) { 5551 bpf_token_put(token); 5552 token = NULL; 5553 } 5554 } 5555 5556 if (!bpf_token_capable(token, CAP_BPF)) { 5557 bpf_token_put(token); 5558 return -EPERM; 5559 } 5560 5561 bpf_token_put(token); 5562 5563 return btf_new_fd(attr, uattr, attr_log); 5564 } 5565 5566 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD fd_by_id_token_fd 5567 5568 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) 5569 { 5570 struct bpf_token *token = NULL; 5571 5572 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) 5573 return -EINVAL; 5574 5575 if (attr->open_flags & ~BPF_F_TOKEN_FD) 5576 return -EINVAL; 5577 5578 if (attr->open_flags & BPF_F_TOKEN_FD) { 5579 token = bpf_token_get_from_fd(attr->fd_by_id_token_fd); 5580 if (IS_ERR(token)) 5581 return PTR_ERR(token); 5582 if (!bpf_token_allow_cmd(token, BPF_BTF_GET_FD_BY_ID)) { 5583 bpf_token_put(token); 5584 token = NULL; 5585 } 5586 } 5587 5588 if (!bpf_token_capable(token, CAP_SYS_ADMIN)) { 5589 bpf_token_put(token); 5590 return -EPERM; 5591 } 5592 5593 bpf_token_put(token); 5594 5595 return btf_get_fd_by_id(attr->btf_id); 5596 } 5597 5598 static int bpf_task_fd_query_copy(const union bpf_attr *attr, 5599 union bpf_attr __user *uattr, 5600 u32 prog_id, u32 fd_type, 5601 const char *buf, u64 probe_offset, 5602 u64 probe_addr) 5603 { 5604 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); 5605 u32 len = buf ? strlen(buf) : 0, input_len; 5606 int err = 0; 5607 5608 if (put_user(len, &uattr->task_fd_query.buf_len)) 5609 return -EFAULT; 5610 input_len = attr->task_fd_query.buf_len; 5611 if (input_len && ubuf) { 5612 if (!len) { 5613 /* nothing to copy, just make ubuf NULL terminated */ 5614 char zero = '\0'; 5615 5616 if (put_user(zero, ubuf)) 5617 return -EFAULT; 5618 } else { 5619 err = bpf_copy_to_user(ubuf, buf, input_len, len); 5620 if (err == -EFAULT) 5621 return err; 5622 } 5623 } 5624 5625 if (put_user(prog_id, &uattr->task_fd_query.prog_id) || 5626 put_user(fd_type, &uattr->task_fd_query.fd_type) || 5627 put_user(probe_offset, &uattr->task_fd_query.probe_offset) || 5628 put_user(probe_addr, &uattr->task_fd_query.probe_addr)) 5629 return -EFAULT; 5630 5631 return err; 5632 } 5633 5634 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr 5635 5636 static int bpf_task_fd_query(const union bpf_attr *attr, 5637 union bpf_attr __user *uattr) 5638 { 5639 pid_t pid = attr->task_fd_query.pid; 5640 u32 fd = attr->task_fd_query.fd; 5641 const struct perf_event *event; 5642 struct task_struct *task; 5643 struct file *file; 5644 int err; 5645 5646 if (CHECK_ATTR(BPF_TASK_FD_QUERY)) 5647 return -EINVAL; 5648 5649 if (!capable(CAP_SYS_ADMIN)) 5650 return -EPERM; 5651 5652 if (attr->task_fd_query.flags != 0) 5653 return -EINVAL; 5654 5655 rcu_read_lock(); 5656 task = get_pid_task(find_vpid(pid), PIDTYPE_PID); 5657 rcu_read_unlock(); 5658 if (!task) 5659 return -ENOENT; 5660 5661 err = 0; 5662 file = fget_task(task, fd); 5663 put_task_struct(task); 5664 if (!file) 5665 return -EBADF; 5666 5667 if (file->f_op == &bpf_link_fops || file->f_op == &bpf_link_fops_poll) { 5668 struct bpf_link *link = file->private_data; 5669 5670 if (link->ops == &bpf_raw_tp_link_lops) { 5671 struct bpf_raw_tp_link *raw_tp = 5672 container_of(link, struct bpf_raw_tp_link, link); 5673 struct bpf_raw_event_map *btp = raw_tp->btp; 5674 5675 err = bpf_task_fd_query_copy(attr, uattr, 5676 raw_tp->link.prog->aux->id, 5677 BPF_FD_TYPE_RAW_TRACEPOINT, 5678 btp->tp->name, 0, 0); 5679 goto put_file; 5680 } 5681 goto out_not_supp; 5682 } 5683 5684 event = perf_get_event(file); 5685 if (!IS_ERR(event)) { 5686 u64 probe_offset, probe_addr; 5687 u32 prog_id, fd_type; 5688 const char *buf; 5689 5690 err = bpf_get_perf_event_info(event, &prog_id, &fd_type, 5691 &buf, &probe_offset, 5692 &probe_addr, NULL); 5693 if (!err) 5694 err = bpf_task_fd_query_copy(attr, uattr, prog_id, 5695 fd_type, buf, 5696 probe_offset, 5697 probe_addr); 5698 goto put_file; 5699 } 5700 5701 out_not_supp: 5702 err = -ENOTSUPP; 5703 put_file: 5704 fput(file); 5705 return err; 5706 } 5707 5708 #define BPF_MAP_BATCH_LAST_FIELD batch.flags 5709 5710 #define BPF_DO_BATCH(fn, ...) \ 5711 do { \ 5712 if (!fn) { \ 5713 err = -ENOTSUPP; \ 5714 goto err_put; \ 5715 } \ 5716 err = fn(__VA_ARGS__); \ 5717 } while (0) 5718 5719 static int bpf_map_do_batch(const union bpf_attr *attr, 5720 union bpf_attr __user *uattr, 5721 int cmd) 5722 { 5723 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH || 5724 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH; 5725 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH; 5726 struct bpf_map *map; 5727 int err; 5728 5729 if (CHECK_ATTR(BPF_MAP_BATCH)) 5730 return -EINVAL; 5731 5732 CLASS(fd, f)(attr->batch.map_fd); 5733 5734 map = __bpf_map_get(f); 5735 if (IS_ERR(map)) 5736 return PTR_ERR(map); 5737 if (has_write) 5738 bpf_map_write_active_inc(map); 5739 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { 5740 err = -EPERM; 5741 goto err_put; 5742 } 5743 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 5744 err = -EPERM; 5745 goto err_put; 5746 } 5747 5748 if (cmd == BPF_MAP_LOOKUP_BATCH) 5749 BPF_DO_BATCH(map->ops->map_lookup_batch, map, attr, uattr); 5750 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) 5751 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch, map, attr, uattr); 5752 else if (cmd == BPF_MAP_UPDATE_BATCH) 5753 BPF_DO_BATCH(map->ops->map_update_batch, map, fd_file(f), attr, uattr); 5754 else 5755 BPF_DO_BATCH(map->ops->map_delete_batch, map, attr, uattr); 5756 err_put: 5757 if (has_write) { 5758 maybe_wait_bpf_programs(map); 5759 bpf_map_write_active_dec(map); 5760 } 5761 return err; 5762 } 5763 5764 #define BPF_LINK_CREATE_LAST_FIELD link_create.uprobe_multi.path_fd 5765 static int link_create(union bpf_attr *attr, bpfptr_t uattr) 5766 { 5767 struct bpf_prog *prog; 5768 int ret; 5769 5770 if (CHECK_ATTR(BPF_LINK_CREATE)) 5771 return -EINVAL; 5772 5773 if (attr->link_create.attach_type == BPF_STRUCT_OPS) 5774 return bpf_struct_ops_link_create(attr); 5775 5776 prog = bpf_prog_get(attr->link_create.prog_fd); 5777 if (IS_ERR(prog)) 5778 return PTR_ERR(prog); 5779 5780 ret = bpf_prog_attach_check_attach_type(prog, 5781 attr->link_create.attach_type); 5782 if (ret) 5783 goto out; 5784 5785 switch (prog->type) { 5786 case BPF_PROG_TYPE_CGROUP_SKB: 5787 case BPF_PROG_TYPE_CGROUP_SOCK: 5788 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 5789 case BPF_PROG_TYPE_SOCK_OPS: 5790 case BPF_PROG_TYPE_CGROUP_DEVICE: 5791 case BPF_PROG_TYPE_CGROUP_SYSCTL: 5792 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 5793 ret = cgroup_bpf_link_attach(attr, prog); 5794 break; 5795 case BPF_PROG_TYPE_EXT: 5796 ret = bpf_tracing_prog_attach(prog, 5797 attr->link_create.target_fd, 5798 attr->link_create.target_btf_id, 5799 attr->link_create.tracing.cookie, 5800 attr->link_create.attach_type); 5801 break; 5802 case BPF_PROG_TYPE_LSM: 5803 case BPF_PROG_TYPE_TRACING: 5804 if (attr->link_create.attach_type != prog->expected_attach_type) { 5805 ret = -EINVAL; 5806 goto out; 5807 } 5808 if (prog->expected_attach_type == BPF_TRACE_RAW_TP) 5809 ret = bpf_raw_tp_link_attach(prog, NULL, attr->link_create.tracing.cookie, 5810 attr->link_create.attach_type); 5811 else if (prog->expected_attach_type == BPF_TRACE_ITER) 5812 ret = bpf_iter_link_attach(attr, uattr, prog); 5813 else if (prog->expected_attach_type == BPF_LSM_CGROUP) 5814 ret = cgroup_bpf_link_attach(attr, prog); 5815 else if (is_tracing_multi(prog->expected_attach_type)) 5816 ret = bpf_tracing_multi_attach(prog, attr); 5817 else 5818 ret = bpf_tracing_prog_attach(prog, 5819 attr->link_create.target_fd, 5820 attr->link_create.target_btf_id, 5821 attr->link_create.tracing.cookie, 5822 attr->link_create.attach_type); 5823 break; 5824 case BPF_PROG_TYPE_FLOW_DISSECTOR: 5825 case BPF_PROG_TYPE_SK_LOOKUP: 5826 ret = netns_bpf_link_create(attr, prog); 5827 break; 5828 case BPF_PROG_TYPE_SK_MSG: 5829 case BPF_PROG_TYPE_SK_SKB: 5830 ret = sock_map_link_create(attr, prog); 5831 break; 5832 #ifdef CONFIG_NET 5833 case BPF_PROG_TYPE_XDP: 5834 ret = bpf_xdp_link_attach(attr, prog); 5835 break; 5836 case BPF_PROG_TYPE_SCHED_CLS: 5837 if (attr->link_create.attach_type == BPF_TCX_INGRESS || 5838 attr->link_create.attach_type == BPF_TCX_EGRESS) 5839 ret = tcx_link_attach(attr, prog); 5840 else 5841 ret = netkit_link_attach(attr, prog); 5842 break; 5843 case BPF_PROG_TYPE_NETFILTER: 5844 ret = bpf_nf_link_attach(attr, prog); 5845 break; 5846 #endif 5847 case BPF_PROG_TYPE_PERF_EVENT: 5848 case BPF_PROG_TYPE_TRACEPOINT: 5849 ret = bpf_perf_link_attach(attr, prog); 5850 break; 5851 case BPF_PROG_TYPE_KPROBE: 5852 if (attr->link_create.attach_type == BPF_PERF_EVENT) 5853 ret = bpf_perf_link_attach(attr, prog); 5854 else if (attr->link_create.attach_type == BPF_TRACE_KPROBE_MULTI || 5855 attr->link_create.attach_type == BPF_TRACE_KPROBE_SESSION) 5856 ret = bpf_kprobe_multi_link_attach(attr, prog); 5857 else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI || 5858 attr->link_create.attach_type == BPF_TRACE_UPROBE_SESSION) 5859 ret = bpf_uprobe_multi_link_attach(attr, prog); 5860 break; 5861 default: 5862 ret = -EINVAL; 5863 } 5864 5865 out: 5866 if (ret < 0) 5867 bpf_prog_put(prog); 5868 return ret; 5869 } 5870 5871 static int link_update_map(struct bpf_link *link, union bpf_attr *attr) 5872 { 5873 struct bpf_map *new_map, *old_map = NULL; 5874 int ret; 5875 5876 new_map = bpf_map_get(attr->link_update.new_map_fd); 5877 if (IS_ERR(new_map)) 5878 return PTR_ERR(new_map); 5879 5880 if (attr->link_update.flags & BPF_F_REPLACE) { 5881 old_map = bpf_map_get(attr->link_update.old_map_fd); 5882 if (IS_ERR(old_map)) { 5883 ret = PTR_ERR(old_map); 5884 goto out_put; 5885 } 5886 } else if (attr->link_update.old_map_fd) { 5887 ret = -EINVAL; 5888 goto out_put; 5889 } 5890 5891 ret = link->ops->update_map(link, new_map, old_map); 5892 5893 if (old_map) 5894 bpf_map_put(old_map); 5895 out_put: 5896 bpf_map_put(new_map); 5897 return ret; 5898 } 5899 5900 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd 5901 5902 static int link_update(union bpf_attr *attr) 5903 { 5904 struct bpf_prog *old_prog = NULL, *new_prog; 5905 struct bpf_link *link; 5906 u32 flags; 5907 int ret; 5908 5909 if (CHECK_ATTR(BPF_LINK_UPDATE)) 5910 return -EINVAL; 5911 5912 flags = attr->link_update.flags; 5913 if (flags & ~BPF_F_REPLACE) 5914 return -EINVAL; 5915 5916 link = bpf_link_get_from_fd(attr->link_update.link_fd); 5917 if (IS_ERR(link)) 5918 return PTR_ERR(link); 5919 5920 if (link->ops->update_map) { 5921 ret = link_update_map(link, attr); 5922 goto out_put_link; 5923 } 5924 5925 new_prog = bpf_prog_get(attr->link_update.new_prog_fd); 5926 if (IS_ERR(new_prog)) { 5927 ret = PTR_ERR(new_prog); 5928 goto out_put_link; 5929 } 5930 5931 if (flags & BPF_F_REPLACE) { 5932 old_prog = bpf_prog_get(attr->link_update.old_prog_fd); 5933 if (IS_ERR(old_prog)) { 5934 ret = PTR_ERR(old_prog); 5935 old_prog = NULL; 5936 goto out_put_progs; 5937 } 5938 } else if (attr->link_update.old_prog_fd) { 5939 ret = -EINVAL; 5940 goto out_put_progs; 5941 } 5942 5943 if (link->ops->update_prog) 5944 ret = link->ops->update_prog(link, new_prog, old_prog); 5945 else 5946 ret = -EINVAL; 5947 5948 out_put_progs: 5949 if (old_prog) 5950 bpf_prog_put(old_prog); 5951 if (ret) 5952 bpf_prog_put(new_prog); 5953 out_put_link: 5954 bpf_link_put_direct(link); 5955 return ret; 5956 } 5957 5958 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd 5959 5960 static int link_detach(union bpf_attr *attr) 5961 { 5962 struct bpf_link *link; 5963 int ret; 5964 5965 if (CHECK_ATTR(BPF_LINK_DETACH)) 5966 return -EINVAL; 5967 5968 link = bpf_link_get_from_fd(attr->link_detach.link_fd); 5969 if (IS_ERR(link)) 5970 return PTR_ERR(link); 5971 5972 if (link->ops->detach) 5973 ret = link->ops->detach(link); 5974 else 5975 ret = -EOPNOTSUPP; 5976 5977 bpf_link_put_direct(link); 5978 return ret; 5979 } 5980 5981 struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link) 5982 { 5983 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT); 5984 } 5985 EXPORT_SYMBOL(bpf_link_inc_not_zero); 5986 5987 struct bpf_link *bpf_link_by_id(u32 id) 5988 { 5989 struct bpf_link *link; 5990 5991 if (!id) 5992 return ERR_PTR(-ENOENT); 5993 5994 spin_lock_bh(&link_idr_lock); 5995 /* before link is "settled", ID is 0, pretend it doesn't exist yet */ 5996 link = idr_find(&link_idr, id); 5997 if (link) { 5998 if (link->id) 5999 link = bpf_link_inc_not_zero(link); 6000 else 6001 link = ERR_PTR(-EAGAIN); 6002 } else { 6003 link = ERR_PTR(-ENOENT); 6004 } 6005 spin_unlock_bh(&link_idr_lock); 6006 return link; 6007 } 6008 6009 struct bpf_link *bpf_link_get_curr_or_next(u32 *id) 6010 { 6011 struct bpf_link *link; 6012 6013 spin_lock_bh(&link_idr_lock); 6014 again: 6015 link = idr_get_next(&link_idr, id); 6016 if (link) { 6017 link = bpf_link_inc_not_zero(link); 6018 if (IS_ERR(link)) { 6019 (*id)++; 6020 goto again; 6021 } 6022 } 6023 spin_unlock_bh(&link_idr_lock); 6024 6025 return link; 6026 } 6027 6028 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id 6029 6030 static int bpf_link_get_fd_by_id(const union bpf_attr *attr) 6031 { 6032 struct bpf_link *link; 6033 u32 id = attr->link_id; 6034 int fd; 6035 6036 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID)) 6037 return -EINVAL; 6038 6039 if (!capable(CAP_SYS_ADMIN)) 6040 return -EPERM; 6041 6042 link = bpf_link_by_id(id); 6043 if (IS_ERR(link)) 6044 return PTR_ERR(link); 6045 6046 fd = bpf_link_new_fd(link); 6047 if (fd < 0) 6048 bpf_link_put_direct(link); 6049 6050 return fd; 6051 } 6052 6053 DEFINE_MUTEX(bpf_stats_enabled_mutex); 6054 6055 static int bpf_stats_release(struct inode *inode, struct file *file) 6056 { 6057 mutex_lock(&bpf_stats_enabled_mutex); 6058 static_key_slow_dec(&bpf_stats_enabled_key.key); 6059 mutex_unlock(&bpf_stats_enabled_mutex); 6060 return 0; 6061 } 6062 6063 static const struct file_operations bpf_stats_fops = { 6064 .release = bpf_stats_release, 6065 }; 6066 6067 static int bpf_enable_runtime_stats(void) 6068 { 6069 int fd; 6070 6071 mutex_lock(&bpf_stats_enabled_mutex); 6072 6073 /* Set a very high limit to avoid overflow */ 6074 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) { 6075 mutex_unlock(&bpf_stats_enabled_mutex); 6076 return -EBUSY; 6077 } 6078 6079 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC); 6080 if (fd >= 0) 6081 static_key_slow_inc(&bpf_stats_enabled_key.key); 6082 6083 mutex_unlock(&bpf_stats_enabled_mutex); 6084 return fd; 6085 } 6086 6087 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type 6088 6089 static int bpf_enable_stats(union bpf_attr *attr) 6090 { 6091 6092 if (CHECK_ATTR(BPF_ENABLE_STATS)) 6093 return -EINVAL; 6094 6095 if (!capable(CAP_SYS_ADMIN)) 6096 return -EPERM; 6097 6098 switch (attr->enable_stats.type) { 6099 case BPF_STATS_RUN_TIME: 6100 return bpf_enable_runtime_stats(); 6101 default: 6102 break; 6103 } 6104 return -EINVAL; 6105 } 6106 6107 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags 6108 6109 static int bpf_iter_create(union bpf_attr *attr) 6110 { 6111 struct bpf_link *link; 6112 int err; 6113 6114 if (CHECK_ATTR(BPF_ITER_CREATE)) 6115 return -EINVAL; 6116 6117 if (attr->iter_create.flags) 6118 return -EINVAL; 6119 6120 link = bpf_link_get_from_fd(attr->iter_create.link_fd); 6121 if (IS_ERR(link)) 6122 return PTR_ERR(link); 6123 6124 err = bpf_iter_new_fd(link); 6125 bpf_link_put_direct(link); 6126 6127 return err; 6128 } 6129 6130 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags 6131 6132 static int bpf_prog_bind_map(union bpf_attr *attr) 6133 { 6134 struct bpf_prog *prog; 6135 struct bpf_map *map; 6136 struct bpf_map **used_maps_old, **used_maps_new; 6137 int i, ret = 0; 6138 6139 if (CHECK_ATTR(BPF_PROG_BIND_MAP)) 6140 return -EINVAL; 6141 6142 if (attr->prog_bind_map.flags) 6143 return -EINVAL; 6144 6145 prog = bpf_prog_get(attr->prog_bind_map.prog_fd); 6146 if (IS_ERR(prog)) 6147 return PTR_ERR(prog); 6148 6149 map = bpf_map_get(attr->prog_bind_map.map_fd); 6150 if (IS_ERR(map)) { 6151 ret = PTR_ERR(map); 6152 goto out_prog_put; 6153 } 6154 6155 mutex_lock(&prog->aux->used_maps_mutex); 6156 6157 used_maps_old = prog->aux->used_maps; 6158 6159 for (i = 0; i < prog->aux->used_map_cnt; i++) 6160 if (used_maps_old[i] == map) { 6161 bpf_map_put(map); 6162 goto out_unlock; 6163 } 6164 6165 used_maps_new = kmalloc_objs(used_maps_new[0], 6166 prog->aux->used_map_cnt + 1); 6167 if (!used_maps_new) { 6168 ret = -ENOMEM; 6169 goto out_unlock; 6170 } 6171 6172 /* The bpf program will not access the bpf map, but for the sake of 6173 * simplicity, increase sleepable_refcnt for sleepable program as well. 6174 */ 6175 if (prog->sleepable) 6176 atomic64_inc(&map->sleepable_refcnt); 6177 memcpy(used_maps_new, used_maps_old, 6178 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt); 6179 used_maps_new[prog->aux->used_map_cnt] = map; 6180 6181 prog->aux->used_map_cnt++; 6182 prog->aux->used_maps = used_maps_new; 6183 6184 kfree(used_maps_old); 6185 6186 out_unlock: 6187 mutex_unlock(&prog->aux->used_maps_mutex); 6188 6189 if (ret) 6190 bpf_map_put(map); 6191 out_prog_put: 6192 bpf_prog_put(prog); 6193 return ret; 6194 } 6195 6196 #define BPF_TOKEN_CREATE_LAST_FIELD token_create.bpffs_fd 6197 6198 static int token_create(union bpf_attr *attr) 6199 { 6200 if (CHECK_ATTR(BPF_TOKEN_CREATE)) 6201 return -EINVAL; 6202 6203 /* no flags are supported yet */ 6204 if (attr->token_create.flags) 6205 return -EINVAL; 6206 6207 return bpf_token_create(attr); 6208 } 6209 6210 #define BPF_PROG_STREAM_READ_BY_FD_LAST_FIELD prog_stream_read.prog_fd 6211 6212 static int prog_stream_read(union bpf_attr *attr) 6213 { 6214 char __user *buf = u64_to_user_ptr(attr->prog_stream_read.stream_buf); 6215 u32 len = attr->prog_stream_read.stream_buf_len; 6216 struct bpf_prog *prog; 6217 int ret; 6218 6219 if (CHECK_ATTR(BPF_PROG_STREAM_READ_BY_FD)) 6220 return -EINVAL; 6221 6222 prog = bpf_prog_get(attr->prog_stream_read.prog_fd); 6223 if (IS_ERR(prog)) 6224 return PTR_ERR(prog); 6225 6226 ret = bpf_prog_stream_read(prog, attr->prog_stream_read.stream_id, buf, len); 6227 bpf_prog_put(prog); 6228 6229 return ret; 6230 } 6231 6232 #define BPF_PROG_ASSOC_STRUCT_OPS_LAST_FIELD prog_assoc_struct_ops.flags 6233 6234 static int prog_assoc_struct_ops(union bpf_attr *attr) 6235 { 6236 struct bpf_prog *prog; 6237 struct bpf_map *map; 6238 int ret; 6239 6240 if (CHECK_ATTR(BPF_PROG_ASSOC_STRUCT_OPS)) 6241 return -EINVAL; 6242 6243 if (attr->prog_assoc_struct_ops.flags) 6244 return -EINVAL; 6245 6246 prog = bpf_prog_get(attr->prog_assoc_struct_ops.prog_fd); 6247 if (IS_ERR(prog)) 6248 return PTR_ERR(prog); 6249 6250 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) { 6251 ret = -EINVAL; 6252 goto put_prog; 6253 } 6254 6255 map = bpf_map_get(attr->prog_assoc_struct_ops.map_fd); 6256 if (IS_ERR(map)) { 6257 ret = PTR_ERR(map); 6258 goto put_prog; 6259 } 6260 6261 if (map->map_type != BPF_MAP_TYPE_STRUCT_OPS) { 6262 ret = -EINVAL; 6263 goto put_map; 6264 } 6265 6266 ret = bpf_prog_assoc_struct_ops(prog, map); 6267 6268 put_map: 6269 bpf_map_put(map); 6270 put_prog: 6271 bpf_prog_put(prog); 6272 return ret; 6273 } 6274 6275 static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, 6276 bpfptr_t uattr_common, unsigned int size_common) 6277 { 6278 struct bpf_common_attr attr_common; 6279 u32 offsetof_log_true_size = 0; 6280 struct bpf_log_attr attr_log; 6281 union bpf_attr attr; 6282 int err; 6283 6284 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); 6285 if (err) 6286 return err; 6287 size = min_t(u32, size, sizeof(attr)); 6288 6289 /* copy attributes from user space, may be less than sizeof(bpf_attr) */ 6290 memset(&attr, 0, sizeof(attr)); 6291 if (copy_from_bpfptr(&attr, uattr, size) != 0) 6292 return -EFAULT; 6293 6294 memset(&attr_common, 0, sizeof(attr_common)); 6295 if (cmd & BPF_COMMON_ATTRS) { 6296 err = bpf_check_uarg_tail_zero(uattr_common, 6297 offsetofend(struct bpf_common_attr, log_true_size), 6298 size_common); 6299 if (err) 6300 return err; 6301 6302 cmd &= ~BPF_COMMON_ATTRS; 6303 size_common = min_t(u32, size_common, sizeof(attr_common)); 6304 if (copy_from_bpfptr(&attr_common, uattr_common, size_common) != 0) 6305 return -EFAULT; 6306 } else { 6307 size_common = 0; 6308 } 6309 6310 err = security_bpf(cmd, &attr, size, uattr.is_kernel); 6311 if (err < 0) 6312 return err; 6313 6314 switch (cmd) { 6315 case BPF_MAP_CREATE: 6316 err = map_create(&attr, uattr, &attr_common, uattr_common, size_common); 6317 break; 6318 case BPF_MAP_LOOKUP_ELEM: 6319 err = map_lookup_elem(&attr); 6320 break; 6321 case BPF_MAP_UPDATE_ELEM: 6322 err = map_update_elem(&attr, uattr); 6323 break; 6324 case BPF_MAP_DELETE_ELEM: 6325 err = map_delete_elem(&attr, uattr); 6326 break; 6327 case BPF_MAP_GET_NEXT_KEY: 6328 err = map_get_next_key(&attr); 6329 break; 6330 case BPF_MAP_FREEZE: 6331 err = map_freeze(&attr); 6332 break; 6333 case BPF_PROG_LOAD: 6334 if (size >= offsetofend(union bpf_attr, log_true_size)) 6335 offsetof_log_true_size = offsetof(union bpf_attr, log_true_size); 6336 err = bpf_log_attr_init(&attr_log, attr.log_buf, attr.log_size, attr.log_level, 6337 offsetof_log_true_size, uattr, &attr_common, uattr_common, 6338 size_common); 6339 err = err ?: bpf_prog_load(&attr, uattr, &attr_log); 6340 break; 6341 case BPF_OBJ_PIN: 6342 err = bpf_obj_pin(&attr); 6343 break; 6344 case BPF_OBJ_GET: 6345 err = bpf_obj_get(&attr); 6346 break; 6347 case BPF_PROG_ATTACH: 6348 err = bpf_prog_attach(&attr); 6349 break; 6350 case BPF_PROG_DETACH: 6351 err = bpf_prog_detach(&attr); 6352 break; 6353 case BPF_PROG_QUERY: 6354 err = bpf_prog_query(&attr, uattr.user, size); 6355 break; 6356 case BPF_PROG_TEST_RUN: 6357 err = bpf_prog_test_run(&attr, uattr.user); 6358 break; 6359 case BPF_PROG_GET_NEXT_ID: 6360 err = bpf_obj_get_next_id(&attr, uattr.user, 6361 &prog_idr, &prog_idr_lock); 6362 break; 6363 case BPF_MAP_GET_NEXT_ID: 6364 err = bpf_obj_get_next_id(&attr, uattr.user, 6365 &map_idr, &map_idr_lock); 6366 break; 6367 case BPF_BTF_GET_NEXT_ID: 6368 err = bpf_obj_get_next_id(&attr, uattr.user, 6369 &btf_idr, &btf_idr_lock); 6370 break; 6371 case BPF_PROG_GET_FD_BY_ID: 6372 err = bpf_prog_get_fd_by_id(&attr); 6373 break; 6374 case BPF_MAP_GET_FD_BY_ID: 6375 err = bpf_map_get_fd_by_id(&attr); 6376 break; 6377 case BPF_OBJ_GET_INFO_BY_FD: 6378 err = bpf_obj_get_info_by_fd(&attr, uattr.user); 6379 break; 6380 case BPF_RAW_TRACEPOINT_OPEN: 6381 err = bpf_raw_tracepoint_open(&attr); 6382 break; 6383 case BPF_BTF_LOAD: 6384 if (size >= offsetofend(union bpf_attr, btf_log_true_size)) 6385 offsetof_log_true_size = offsetof(union bpf_attr, btf_log_true_size); 6386 err = bpf_log_attr_init(&attr_log, attr.btf_log_buf, attr.btf_log_size, 6387 attr.btf_log_level, offsetof_log_true_size, uattr, 6388 &attr_common, uattr_common, size_common); 6389 err = err ?: bpf_btf_load(&attr, uattr, &attr_log); 6390 break; 6391 case BPF_BTF_GET_FD_BY_ID: 6392 err = bpf_btf_get_fd_by_id(&attr); 6393 break; 6394 case BPF_TASK_FD_QUERY: 6395 err = bpf_task_fd_query(&attr, uattr.user); 6396 break; 6397 case BPF_MAP_LOOKUP_AND_DELETE_ELEM: 6398 err = map_lookup_and_delete_elem(&attr); 6399 break; 6400 case BPF_MAP_LOOKUP_BATCH: 6401 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH); 6402 break; 6403 case BPF_MAP_LOOKUP_AND_DELETE_BATCH: 6404 err = bpf_map_do_batch(&attr, uattr.user, 6405 BPF_MAP_LOOKUP_AND_DELETE_BATCH); 6406 break; 6407 case BPF_MAP_UPDATE_BATCH: 6408 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH); 6409 break; 6410 case BPF_MAP_DELETE_BATCH: 6411 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH); 6412 break; 6413 case BPF_LINK_CREATE: 6414 err = link_create(&attr, uattr); 6415 break; 6416 case BPF_LINK_UPDATE: 6417 err = link_update(&attr); 6418 break; 6419 case BPF_LINK_GET_FD_BY_ID: 6420 err = bpf_link_get_fd_by_id(&attr); 6421 break; 6422 case BPF_LINK_GET_NEXT_ID: 6423 err = bpf_obj_get_next_id(&attr, uattr.user, 6424 &link_idr, &link_idr_lock); 6425 break; 6426 case BPF_ENABLE_STATS: 6427 err = bpf_enable_stats(&attr); 6428 break; 6429 case BPF_ITER_CREATE: 6430 err = bpf_iter_create(&attr); 6431 break; 6432 case BPF_LINK_DETACH: 6433 err = link_detach(&attr); 6434 break; 6435 case BPF_PROG_BIND_MAP: 6436 err = bpf_prog_bind_map(&attr); 6437 break; 6438 case BPF_TOKEN_CREATE: 6439 err = token_create(&attr); 6440 break; 6441 case BPF_PROG_STREAM_READ_BY_FD: 6442 err = prog_stream_read(&attr); 6443 break; 6444 case BPF_PROG_ASSOC_STRUCT_OPS: 6445 err = prog_assoc_struct_ops(&attr); 6446 break; 6447 default: 6448 err = -EINVAL; 6449 break; 6450 } 6451 6452 return err; 6453 } 6454 6455 SYSCALL_DEFINE5(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size, 6456 struct bpf_common_attr __user *, uattr_common, unsigned int, size_common) 6457 { 6458 return __sys_bpf(cmd, USER_BPFPTR(uattr), size, USER_BPFPTR(uattr_common), size_common); 6459 } 6460 6461 static bool syscall_prog_is_valid_access(int off, int size, 6462 enum bpf_access_type type, 6463 const struct bpf_prog *prog, 6464 struct bpf_insn_access_aux *info) 6465 { 6466 if (off < 0 || off >= U16_MAX) 6467 return false; 6468 /* No alignment requirements for syscall ctx accesses. */ 6469 return true; 6470 } 6471 6472 BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size) 6473 { 6474 switch (cmd) { 6475 case BPF_MAP_CREATE: 6476 case BPF_MAP_DELETE_ELEM: 6477 case BPF_MAP_UPDATE_ELEM: 6478 case BPF_MAP_FREEZE: 6479 case BPF_MAP_GET_FD_BY_ID: 6480 case BPF_PROG_LOAD: 6481 case BPF_BTF_LOAD: 6482 case BPF_LINK_CREATE: 6483 case BPF_RAW_TRACEPOINT_OPEN: 6484 break; 6485 default: 6486 return -EINVAL; 6487 } 6488 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size, KERNEL_BPFPTR(NULL), 0); 6489 } 6490 6491 6492 /* To shut up -Wmissing-prototypes. 6493 * This function is used by the kernel light skeleton 6494 * to load bpf programs when modules are loaded or during kernel boot. 6495 * See tools/lib/bpf/skel_internal.h 6496 */ 6497 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size); 6498 6499 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size) 6500 { 6501 struct bpf_prog * __maybe_unused prog; 6502 struct bpf_tramp_run_ctx __maybe_unused run_ctx; 6503 6504 switch (cmd) { 6505 #ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */ 6506 case BPF_PROG_TEST_RUN: 6507 if (attr->test.data_in || attr->test.data_out || 6508 attr->test.ctx_out || attr->test.duration || 6509 attr->test.repeat || attr->test.flags) 6510 return -EINVAL; 6511 6512 prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL); 6513 if (IS_ERR(prog)) 6514 return PTR_ERR(prog); 6515 6516 if (attr->test.ctx_size_in < prog->aux->max_ctx_offset || 6517 attr->test.ctx_size_in > U16_MAX) { 6518 bpf_prog_put(prog); 6519 return -EINVAL; 6520 } 6521 6522 run_ctx.bpf_cookie = 0; 6523 if (!__bpf_prog_enter_sleepable_recur(prog, &run_ctx)) { 6524 /* recursion detected */ 6525 __bpf_prog_exit_sleepable_recur(prog, 0, &run_ctx); 6526 bpf_prog_put(prog); 6527 return -EBUSY; 6528 } 6529 attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in); 6530 __bpf_prog_exit_sleepable_recur(prog, 0 /* bpf_prog_run does runtime stats */, 6531 &run_ctx); 6532 bpf_prog_put(prog); 6533 return 0; 6534 #endif 6535 default: 6536 return ____bpf_sys_bpf(cmd, attr, size); 6537 } 6538 } 6539 EXPORT_SYMBOL_NS(kern_sys_bpf, "BPF_INTERNAL"); 6540 6541 static const struct bpf_func_proto bpf_sys_bpf_proto = { 6542 .func = bpf_sys_bpf, 6543 .gpl_only = false, 6544 .ret_type = RET_INTEGER, 6545 .arg1_type = ARG_ANYTHING, 6546 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6547 .arg3_type = ARG_CONST_SIZE, 6548 }; 6549 6550 const struct bpf_func_proto * __weak 6551 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6552 { 6553 return bpf_base_func_proto(func_id, prog); 6554 } 6555 6556 BPF_CALL_1(bpf_sys_close, u32, fd) 6557 { 6558 /* When bpf program calls this helper there should not be 6559 * an fdget() without matching completed fdput(). 6560 * This helper is allowed in the following callchain only: 6561 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close 6562 */ 6563 return close_fd(fd); 6564 } 6565 6566 static const struct bpf_func_proto bpf_sys_close_proto = { 6567 .func = bpf_sys_close, 6568 .gpl_only = false, 6569 .ret_type = RET_INTEGER, 6570 .arg1_type = ARG_ANYTHING, 6571 }; 6572 6573 BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res) 6574 { 6575 *res = 0; 6576 if (flags) 6577 return -EINVAL; 6578 6579 if (name_sz <= 1 || name[name_sz - 1]) 6580 return -EINVAL; 6581 6582 if (!bpf_dump_raw_ok(current_cred())) 6583 return -EPERM; 6584 6585 *res = kallsyms_lookup_name(name); 6586 return *res ? 0 : -ENOENT; 6587 } 6588 6589 static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { 6590 .func = bpf_kallsyms_lookup_name, 6591 .gpl_only = false, 6592 .ret_type = RET_INTEGER, 6593 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6594 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 6595 .arg3_type = ARG_ANYTHING, 6596 .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED, 6597 .arg4_size = sizeof(u64), 6598 }; 6599 6600 static const struct bpf_func_proto * 6601 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6602 { 6603 switch (func_id) { 6604 case BPF_FUNC_sys_bpf: 6605 return !bpf_token_capable(prog->aux->token, CAP_PERFMON) 6606 ? NULL : &bpf_sys_bpf_proto; 6607 case BPF_FUNC_btf_find_by_name_kind: 6608 return &bpf_btf_find_by_name_kind_proto; 6609 case BPF_FUNC_sys_close: 6610 return &bpf_sys_close_proto; 6611 case BPF_FUNC_kallsyms_lookup_name: 6612 return &bpf_kallsyms_lookup_name_proto; 6613 default: 6614 return tracing_prog_func_proto(func_id, prog); 6615 } 6616 } 6617 6618 const struct bpf_verifier_ops bpf_syscall_verifier_ops = { 6619 .get_func_proto = syscall_prog_func_proto, 6620 .is_valid_access = syscall_prog_is_valid_access, 6621 }; 6622 6623 const struct bpf_prog_ops bpf_syscall_prog_ops = { 6624 .test_run = bpf_prog_test_run_syscall, 6625 }; 6626 6627 #ifdef CONFIG_SYSCTL 6628 static int bpf_stats_handler(const struct ctl_table *table, int write, 6629 void *buffer, size_t *lenp, loff_t *ppos) 6630 { 6631 struct static_key *key = (struct static_key *)table->data; 6632 static int saved_val; 6633 int val, ret; 6634 struct ctl_table tmp = { 6635 .data = &val, 6636 .maxlen = sizeof(val), 6637 .mode = table->mode, 6638 .extra1 = SYSCTL_ZERO, 6639 .extra2 = SYSCTL_ONE, 6640 }; 6641 6642 if (write && !capable(CAP_SYS_ADMIN)) 6643 return -EPERM; 6644 6645 mutex_lock(&bpf_stats_enabled_mutex); 6646 val = saved_val; 6647 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 6648 if (write && !ret && val != saved_val) { 6649 if (val) 6650 static_key_slow_inc(key); 6651 else 6652 static_key_slow_dec(key); 6653 saved_val = val; 6654 } 6655 mutex_unlock(&bpf_stats_enabled_mutex); 6656 return ret; 6657 } 6658 6659 void __weak unpriv_ebpf_notify(int new_state) 6660 { 6661 } 6662 6663 static int bpf_unpriv_handler(const struct ctl_table *table, int write, 6664 void *buffer, size_t *lenp, loff_t *ppos) 6665 { 6666 int ret, unpriv_enable = *(int *)table->data; 6667 bool locked_state = unpriv_enable == 1; 6668 struct ctl_table tmp = *table; 6669 6670 if (write && !capable(CAP_SYS_ADMIN)) 6671 return -EPERM; 6672 6673 tmp.data = &unpriv_enable; 6674 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 6675 if (write && !ret) { 6676 if (locked_state && unpriv_enable != 1) 6677 return -EPERM; 6678 *(int *)table->data = unpriv_enable; 6679 } 6680 6681 if (write) 6682 unpriv_ebpf_notify(unpriv_enable); 6683 6684 return ret; 6685 } 6686 6687 static const struct ctl_table bpf_syscall_table[] = { 6688 { 6689 .procname = "unprivileged_bpf_disabled", 6690 .data = &sysctl_unprivileged_bpf_disabled, 6691 .maxlen = sizeof(sysctl_unprivileged_bpf_disabled), 6692 .mode = 0644, 6693 .proc_handler = bpf_unpriv_handler, 6694 .extra1 = SYSCTL_ZERO, 6695 .extra2 = SYSCTL_TWO, 6696 }, 6697 { 6698 .procname = "bpf_stats_enabled", 6699 .data = &bpf_stats_enabled_key.key, 6700 .mode = 0644, 6701 .proc_handler = bpf_stats_handler, 6702 }, 6703 }; 6704 6705 static int __init bpf_syscall_sysctl_init(void) 6706 { 6707 register_sysctl_init("kernel", bpf_syscall_table); 6708 return 0; 6709 } 6710 late_initcall(bpf_syscall_sysctl_init); 6711 #endif /* CONFIG_SYSCTL */ 6712