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 if (dst_prog) { 3047 prog->aux->saved_dst_prog_type = dst_prog->type; 3048 prog->aux->saved_dst_attach_type = dst_prog->expected_attach_type; 3049 } 3050 prog->aux->dev_bound = !!attr->prog_ifindex; 3051 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS; 3052 3053 /* move token into prog->aux, reuse taken refcnt */ 3054 prog->aux->token = token; 3055 token = NULL; 3056 3057 prog->aux->user = get_current_user(); 3058 prog->len = attr->insn_cnt; 3059 3060 err = -EFAULT; 3061 if (copy_from_bpfptr(prog->insns, 3062 make_bpfptr(attr->insns, uattr.is_kernel), 3063 bpf_prog_insn_size(prog)) != 0) 3064 goto free_prog; 3065 /* copy eBPF program license from user space */ 3066 if (strncpy_from_bpfptr(license, 3067 make_bpfptr(attr->license, uattr.is_kernel), 3068 sizeof(license) - 1) < 0) 3069 goto free_prog; 3070 license[sizeof(license) - 1] = 0; 3071 3072 /* eBPF programs must be GPL compatible to use GPL-ed functions */ 3073 prog->gpl_compatible = license_is_gpl_compatible(license) ? 1 : 0; 3074 prog->aux->sig.keyring_type = BPF_SIG_KEYRING_NONE; 3075 prog->aux->sig.verdict = BPF_SIG_UNSIGNED; 3076 prog->orig_prog = NULL; 3077 prog->jited = 0; 3078 3079 atomic64_set(&prog->aux->refcnt, 1); 3080 3081 if (bpf_prog_is_dev_bound(prog->aux)) { 3082 err = bpf_prog_dev_bound_init(prog, attr); 3083 if (err) 3084 goto free_prog; 3085 } 3086 3087 if (type == BPF_PROG_TYPE_EXT && dst_prog && 3088 bpf_prog_is_dev_bound(dst_prog->aux)) { 3089 err = bpf_prog_dev_bound_inherit(prog, dst_prog); 3090 if (err) 3091 goto free_prog; 3092 } 3093 3094 /* 3095 * Bookkeeping for managing the program attachment chain. 3096 * 3097 * It might be tempting to set attach_tracing_prog flag at the attachment 3098 * time, but this will not prevent from loading bunch of tracing prog 3099 * first, then attach them one to another. 3100 * 3101 * The flag attach_tracing_prog is set for the whole program lifecycle, and 3102 * doesn't have to be cleared in bpf_tracing_link_release, since tracing 3103 * programs cannot change attachment target. 3104 */ 3105 if (type == BPF_PROG_TYPE_TRACING && dst_prog && 3106 dst_prog->type == BPF_PROG_TYPE_TRACING) { 3107 prog->aux->attach_tracing_prog = true; 3108 } 3109 3110 /* find program type: socket_filter vs tracing_filter */ 3111 err = find_prog_type(type, prog); 3112 if (err < 0) 3113 goto free_prog; 3114 3115 prog->aux->load_time = ktime_get_boottime_ns(); 3116 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name, 3117 sizeof(attr->prog_name)); 3118 if (err < 0) 3119 goto free_prog; 3120 3121 /* run eBPF verifier */ 3122 err = bpf_check(&prog, attr, uattr, attr_log); 3123 if (err < 0) 3124 goto free_used_maps; 3125 3126 err = bpf_prog_mark_insn_arrays_ready(prog); 3127 if (err < 0) 3128 goto free_used_maps; 3129 3130 err = bpf_prog_alloc_id(prog); 3131 if (err) 3132 goto free_used_maps; 3133 3134 /* Upon success of bpf_prog_alloc_id(), the BPF prog is 3135 * effectively publicly exposed. However, retrieving via 3136 * bpf_prog_get_fd_by_id() will take another reference, 3137 * therefore it cannot be gone underneath us. 3138 * 3139 * Only for the time /after/ successful bpf_prog_new_fd() 3140 * and before returning to userspace, we might just hold 3141 * one reference and any parallel close on that fd could 3142 * rip everything out. Hence, below notifications must 3143 * happen before bpf_prog_new_fd(). 3144 * 3145 * Also, any failure handling from this point onwards must 3146 * be using bpf_prog_put() given the program is exposed. 3147 */ 3148 bpf_prog_kallsyms_add(prog); 3149 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); 3150 bpf_audit_prog(prog, BPF_AUDIT_LOAD); 3151 3152 err = bpf_prog_new_fd(prog); 3153 if (err < 0) 3154 bpf_prog_put(prog); 3155 return err; 3156 3157 free_used_maps: 3158 /* In case we have subprogs, we need to wait for a grace 3159 * period before we can tear down JIT memory since symbols 3160 * are already exposed under kallsyms. 3161 */ 3162 __bpf_prog_put_noref(prog, prog->aux->real_func_cnt); 3163 return err; 3164 3165 free_prog: 3166 free_uid(prog->aux->user); 3167 if (prog->aux->attach_btf) 3168 btf_put(prog->aux->attach_btf); 3169 bpf_prog_free(prog); 3170 put_token: 3171 bpf_token_put(token); 3172 return err; 3173 } 3174 3175 #define BPF_OBJ_LAST_FIELD path_fd 3176 3177 static int bpf_obj_pin(const union bpf_attr *attr) 3178 { 3179 int path_fd; 3180 3181 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags & ~BPF_F_PATH_FD) 3182 return -EINVAL; 3183 3184 /* path_fd has to be accompanied by BPF_F_PATH_FD flag */ 3185 if (!(attr->file_flags & BPF_F_PATH_FD) && attr->path_fd) 3186 return -EINVAL; 3187 3188 path_fd = attr->file_flags & BPF_F_PATH_FD ? attr->path_fd : AT_FDCWD; 3189 return bpf_obj_pin_user(attr->bpf_fd, path_fd, 3190 u64_to_user_ptr(attr->pathname)); 3191 } 3192 3193 static int bpf_obj_get(const union bpf_attr *attr) 3194 { 3195 int path_fd; 3196 3197 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || 3198 attr->file_flags & ~(BPF_OBJ_FLAG_MASK | BPF_F_PATH_FD)) 3199 return -EINVAL; 3200 3201 /* path_fd has to be accompanied by BPF_F_PATH_FD flag */ 3202 if (!(attr->file_flags & BPF_F_PATH_FD) && attr->path_fd) 3203 return -EINVAL; 3204 3205 path_fd = attr->file_flags & BPF_F_PATH_FD ? attr->path_fd : AT_FDCWD; 3206 return bpf_obj_get_user(path_fd, u64_to_user_ptr(attr->pathname), 3207 attr->file_flags); 3208 } 3209 3210 /* bpf_link_init_sleepable() allows to specify whether BPF link itself has 3211 * "sleepable" semantics, which normally would mean that BPF link's attach 3212 * hook can dereference link or link's underlying program for some time after 3213 * detachment due to RCU Tasks Trace-based lifetime protection scheme. 3214 * BPF program itself can be non-sleepable, yet, because it's transitively 3215 * reachable through BPF link, its freeing has to be delayed until after RCU 3216 * Tasks Trace GP. 3217 */ 3218 void bpf_link_init_sleepable(struct bpf_link *link, enum bpf_link_type type, 3219 const struct bpf_link_ops *ops, struct bpf_prog *prog, 3220 enum bpf_attach_type attach_type, bool sleepable) 3221 { 3222 WARN_ON(ops->dealloc && ops->dealloc_deferred); 3223 atomic64_set(&link->refcnt, 1); 3224 link->type = type; 3225 link->sleepable = sleepable; 3226 link->id = 0; 3227 link->ops = ops; 3228 link->prog = prog; 3229 link->attach_type = attach_type; 3230 } 3231 3232 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, 3233 const struct bpf_link_ops *ops, struct bpf_prog *prog, 3234 enum bpf_attach_type attach_type) 3235 { 3236 bpf_link_init_sleepable(link, type, ops, prog, attach_type, false); 3237 } 3238 3239 void bpf_tramp_link_init(struct bpf_tramp_link *link, enum bpf_link_type type, 3240 const struct bpf_link_ops *ops, struct bpf_prog *prog, 3241 enum bpf_attach_type attach_type, u64 cookie) 3242 { 3243 bpf_link_init(&link->link, type, ops, prog, attach_type); 3244 link->node.link = &link->link; 3245 link->node.cookie = cookie; 3246 } 3247 3248 static void bpf_link_free_id(int id) 3249 { 3250 if (!id) 3251 return; 3252 3253 spin_lock_bh(&link_idr_lock); 3254 idr_remove(&link_idr, id); 3255 spin_unlock_bh(&link_idr_lock); 3256 } 3257 3258 /* Clean up bpf_link and corresponding anon_inode file and FD. After 3259 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred 3260 * anon_inode's release() call. This helper marks bpf_link as 3261 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt 3262 * is not decremented, it's the responsibility of a calling code that failed 3263 * to complete bpf_link initialization. 3264 * This helper eventually calls link's dealloc callback, but does not call 3265 * link's release callback. 3266 */ 3267 void bpf_link_cleanup(struct bpf_link_primer *primer) 3268 { 3269 primer->link->prog = NULL; 3270 bpf_link_free_id(primer->id); 3271 fput(primer->file); 3272 put_unused_fd(primer->fd); 3273 } 3274 3275 void bpf_link_inc(struct bpf_link *link) 3276 { 3277 atomic64_inc(&link->refcnt); 3278 } 3279 3280 static void bpf_link_dealloc(struct bpf_link *link) 3281 { 3282 /* now that we know that bpf_link itself can't be reached, put underlying BPF program */ 3283 if (link->prog) 3284 bpf_prog_put(link->prog); 3285 3286 /* free bpf_link and its containing memory */ 3287 if (link->ops->dealloc_deferred) 3288 link->ops->dealloc_deferred(link); 3289 else 3290 link->ops->dealloc(link); 3291 } 3292 3293 static void bpf_link_defer_dealloc_rcu_gp(struct rcu_head *rcu) 3294 { 3295 struct bpf_link *link = container_of(rcu, struct bpf_link, rcu); 3296 3297 bpf_link_dealloc(link); 3298 } 3299 3300 static bool bpf_link_is_tracepoint(struct bpf_link *link) 3301 { 3302 /* 3303 * Only these combinations support a tracepoint bpf_link. 3304 * BPF_LINK_TYPE_TRACING raw_tp progs are hardcoded to use 3305 * bpf_raw_tp_link_lops and thus dealloc_deferred(), see 3306 * bpf_raw_tp_link_attach(). 3307 */ 3308 return link->type == BPF_LINK_TYPE_RAW_TRACEPOINT || 3309 (link->type == BPF_LINK_TYPE_TRACING && link->attach_type == BPF_TRACE_RAW_TP); 3310 } 3311 3312 /* bpf_link_free is guaranteed to be called from process context */ 3313 static void bpf_link_free(struct bpf_link *link) 3314 { 3315 const struct bpf_link_ops *ops = link->ops; 3316 3317 bpf_link_free_id(link->id); 3318 /* detach BPF program, clean up used resources */ 3319 if (link->prog) 3320 ops->release(link); 3321 if (ops->dealloc_deferred) { 3322 /* 3323 * Schedule BPF link deallocation, which will only then 3324 * trigger putting BPF program refcount. 3325 * If underlying BPF program is sleepable or BPF link's target 3326 * attach hookpoint is sleepable or otherwise requires RCU GPs 3327 * to ensure link and its underlying BPF program is not 3328 * reachable anymore, we need to first wait for RCU tasks 3329 * trace sync, and then go through "classic" RCU grace period. 3330 * 3331 * For tracepoint BPF links, we need to go through SRCU grace 3332 * period wait instead when non-faultable tracepoint is used. We 3333 * don't need to chain SRCU grace period waits, however, for the 3334 * faultable case, since it exclusively uses RCU Tasks Trace. 3335 */ 3336 if (link->sleepable || (link->prog && link->prog->sleepable)) 3337 /* RCU Tasks Trace grace period implies RCU grace period. */ 3338 call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_rcu_gp); 3339 /* We need to do a SRCU grace period wait for non-faultable tracepoint BPF links. */ 3340 else if (bpf_link_is_tracepoint(link)) 3341 call_tracepoint_unregister_atomic(&link->rcu, bpf_link_defer_dealloc_rcu_gp); 3342 else 3343 call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp); 3344 } else if (ops->dealloc) { 3345 bpf_link_dealloc(link); 3346 } 3347 } 3348 3349 static void bpf_link_put_deferred(struct work_struct *work) 3350 { 3351 struct bpf_link *link = container_of(work, struct bpf_link, work); 3352 3353 bpf_link_free(link); 3354 } 3355 3356 /* bpf_link_put might be called from atomic context. It needs to be called 3357 * from sleepable context in order to acquire sleeping locks during the process. 3358 */ 3359 void bpf_link_put(struct bpf_link *link) 3360 { 3361 if (!atomic64_dec_and_test(&link->refcnt)) 3362 return; 3363 3364 INIT_WORK(&link->work, bpf_link_put_deferred); 3365 schedule_work(&link->work); 3366 } 3367 EXPORT_SYMBOL(bpf_link_put); 3368 3369 static void bpf_link_put_direct(struct bpf_link *link) 3370 { 3371 if (!atomic64_dec_and_test(&link->refcnt)) 3372 return; 3373 bpf_link_free(link); 3374 } 3375 3376 static int bpf_link_release(struct inode *inode, struct file *filp) 3377 { 3378 struct bpf_link *link = filp->private_data; 3379 3380 bpf_link_put_direct(link); 3381 return 0; 3382 } 3383 3384 #ifdef CONFIG_PROC_FS 3385 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) 3386 #define BPF_MAP_TYPE(_id, _ops) 3387 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name, 3388 static const char *bpf_link_type_strs[] = { 3389 [BPF_LINK_TYPE_UNSPEC] = "<invalid>", 3390 #include <linux/bpf_types.h> 3391 }; 3392 #undef BPF_PROG_TYPE 3393 #undef BPF_MAP_TYPE 3394 #undef BPF_LINK_TYPE 3395 3396 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp) 3397 { 3398 const struct bpf_link *link = filp->private_data; 3399 const struct bpf_prog *prog; 3400 enum bpf_link_type type = link->type; 3401 char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 3402 u32 prog_id = 0; 3403 3404 if (type < ARRAY_SIZE(bpf_link_type_strs) && bpf_link_type_strs[type]) { 3405 if (link->type == BPF_LINK_TYPE_KPROBE_MULTI) 3406 seq_printf(m, "link_type:\t%s\n", link->flags == BPF_F_KPROBE_MULTI_RETURN ? 3407 "kretprobe_multi" : "kprobe_multi"); 3408 else if (link->type == BPF_LINK_TYPE_UPROBE_MULTI) 3409 seq_printf(m, "link_type:\t%s\n", link->flags & BPF_F_UPROBE_MULTI_RETURN ? 3410 "uretprobe_multi" : "uprobe_multi"); 3411 else 3412 seq_printf(m, "link_type:\t%s\n", bpf_link_type_strs[type]); 3413 } else { 3414 WARN_ONCE(1, "missing BPF_LINK_TYPE(...) for link type %u\n", type); 3415 seq_printf(m, "link_type:\t<%u>\n", type); 3416 } 3417 seq_printf(m, "link_id:\t%u\n", link->id); 3418 3419 rcu_read_lock(); 3420 prog = READ_ONCE(link->prog); 3421 if (prog) { 3422 bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 3423 prog_id = prog->aux->id; 3424 } 3425 rcu_read_unlock(); 3426 3427 if (prog) { 3428 seq_printf(m, 3429 "prog_tag:\t%s\n" 3430 "prog_id:\t%u\n", 3431 prog_tag, 3432 prog_id); 3433 } 3434 if (link->ops->show_fdinfo) 3435 link->ops->show_fdinfo(link, m); 3436 } 3437 #endif 3438 3439 static __poll_t bpf_link_poll(struct file *file, struct poll_table_struct *pts) 3440 { 3441 struct bpf_link *link = file->private_data; 3442 3443 return link->ops->poll(file, pts); 3444 } 3445 3446 static const struct file_operations bpf_link_fops = { 3447 #ifdef CONFIG_PROC_FS 3448 .show_fdinfo = bpf_link_show_fdinfo, 3449 #endif 3450 .release = bpf_link_release, 3451 .read = bpf_dummy_read, 3452 .write = bpf_dummy_write, 3453 }; 3454 3455 static const struct file_operations bpf_link_fops_poll = { 3456 #ifdef CONFIG_PROC_FS 3457 .show_fdinfo = bpf_link_show_fdinfo, 3458 #endif 3459 .release = bpf_link_release, 3460 .read = bpf_dummy_read, 3461 .write = bpf_dummy_write, 3462 .poll = bpf_link_poll, 3463 }; 3464 3465 static int bpf_link_alloc_id(struct bpf_link *link) 3466 { 3467 int id; 3468 3469 idr_preload(GFP_KERNEL); 3470 spin_lock_bh(&link_idr_lock); 3471 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC); 3472 spin_unlock_bh(&link_idr_lock); 3473 idr_preload_end(); 3474 3475 return id; 3476 } 3477 3478 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file, 3479 * reserving unused FD and allocating ID from link_idr. This is to be paired 3480 * with bpf_link_settle() to install FD and ID and expose bpf_link to 3481 * user-space, if bpf_link is successfully attached. If not, bpf_link and 3482 * pre-allocated resources are to be freed with bpf_cleanup() call. All the 3483 * transient state is passed around in struct bpf_link_primer. 3484 * This is preferred way to create and initialize bpf_link, especially when 3485 * there are complicated and expensive operations in between creating bpf_link 3486 * itself and attaching it to BPF hook. By using bpf_link_prime() and 3487 * bpf_link_settle() kernel code using bpf_link doesn't have to perform 3488 * expensive (and potentially failing) roll back operations in a rare case 3489 * that file, FD, or ID can't be allocated. 3490 */ 3491 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer) 3492 { 3493 struct file *file; 3494 int fd, id; 3495 3496 fd = get_unused_fd_flags(O_CLOEXEC); 3497 if (fd < 0) 3498 return fd; 3499 3500 3501 id = bpf_link_alloc_id(link); 3502 if (id < 0) { 3503 put_unused_fd(fd); 3504 return id; 3505 } 3506 3507 file = anon_inode_getfile("bpf_link", 3508 link->ops->poll ? &bpf_link_fops_poll : &bpf_link_fops, 3509 link, O_CLOEXEC); 3510 if (IS_ERR(file)) { 3511 bpf_link_free_id(id); 3512 put_unused_fd(fd); 3513 return PTR_ERR(file); 3514 } 3515 3516 primer->link = link; 3517 primer->file = file; 3518 primer->fd = fd; 3519 primer->id = id; 3520 return 0; 3521 } 3522 3523 int bpf_link_settle(struct bpf_link_primer *primer) 3524 { 3525 /* make bpf_link fetchable by ID */ 3526 spin_lock_bh(&link_idr_lock); 3527 primer->link->id = primer->id; 3528 spin_unlock_bh(&link_idr_lock); 3529 /* make bpf_link fetchable by FD */ 3530 fd_install(primer->fd, primer->file); 3531 /* pass through installed FD */ 3532 return primer->fd; 3533 } 3534 3535 int bpf_link_new_fd(struct bpf_link *link) 3536 { 3537 return anon_inode_getfd("bpf-link", 3538 link->ops->poll ? &bpf_link_fops_poll : &bpf_link_fops, 3539 link, O_CLOEXEC); 3540 } 3541 3542 struct bpf_link *bpf_link_get_from_fd(u32 ufd) 3543 { 3544 CLASS(fd, f)(ufd); 3545 struct bpf_link *link; 3546 3547 if (fd_empty(f)) 3548 return ERR_PTR(-EBADF); 3549 if (fd_file(f)->f_op != &bpf_link_fops && fd_file(f)->f_op != &bpf_link_fops_poll) 3550 return ERR_PTR(-EINVAL); 3551 3552 link = fd_file(f)->private_data; 3553 bpf_link_inc(link); 3554 return link; 3555 } 3556 EXPORT_SYMBOL_NS(bpf_link_get_from_fd, "BPF_INTERNAL"); 3557 3558 static void bpf_tracing_link_release(struct bpf_link *link) 3559 { 3560 struct bpf_tracing_link *tr_link = 3561 container_of(link, struct bpf_tracing_link, link.link); 3562 int err; 3563 3564 err = bpf_trampoline_unlink_prog(&tr_link->link.node, 3565 tr_link->trampoline, 3566 tr_link->tgt_prog); 3567 WARN_ONCE(err, "bpf_trampoline_unlink_prog failed: %d\n", err); 3568 3569 bpf_trampoline_put(tr_link->trampoline); 3570 3571 /* tgt_prog is NULL if target is a kernel function */ 3572 if (tr_link->tgt_prog) 3573 bpf_prog_put(tr_link->tgt_prog); 3574 } 3575 3576 static void bpf_tracing_link_dealloc(struct bpf_link *link) 3577 { 3578 struct bpf_tracing_link *tr_link = container_of(link, struct bpf_tracing_link, link.link); 3579 3580 kfree(tr_link); 3581 } 3582 3583 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link, 3584 struct seq_file *seq) 3585 { 3586 struct bpf_tracing_link *tr_link = container_of(link, struct bpf_tracing_link, link.link); 3587 3588 u32 target_btf_id, target_obj_id; 3589 3590 bpf_trampoline_unpack_key(tr_link->trampoline->key, 3591 &target_obj_id, &target_btf_id); 3592 seq_printf(seq, 3593 "attach_type:\t%d\n" 3594 "target_obj_id:\t%u\n" 3595 "target_btf_id:\t%u\n" 3596 "cookie:\t%llu\n", 3597 link->attach_type, 3598 target_obj_id, 3599 target_btf_id, 3600 tr_link->link.node.cookie); 3601 } 3602 3603 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, 3604 struct bpf_link_info *info) 3605 { 3606 struct bpf_tracing_link *tr_link = container_of(link, struct bpf_tracing_link, link.link); 3607 3608 info->tracing.attach_type = link->attach_type; 3609 info->tracing.cookie = tr_link->link.node.cookie; 3610 bpf_trampoline_unpack_key(tr_link->trampoline->key, 3611 &info->tracing.target_obj_id, 3612 &info->tracing.target_btf_id); 3613 3614 return 0; 3615 } 3616 3617 static const struct bpf_link_ops bpf_tracing_link_lops = { 3618 .release = bpf_tracing_link_release, 3619 .dealloc = bpf_tracing_link_dealloc, 3620 .show_fdinfo = bpf_tracing_link_show_fdinfo, 3621 .fill_link_info = bpf_tracing_link_fill_link_info, 3622 }; 3623 3624 static int bpf_tracing_prog_attach(struct bpf_prog *prog, 3625 int tgt_prog_fd, 3626 u32 btf_id, 3627 u64 bpf_cookie, 3628 enum bpf_attach_type attach_type) 3629 { 3630 struct bpf_link_primer link_primer; 3631 struct bpf_prog *tgt_prog = NULL; 3632 struct bpf_trampoline *tr = NULL; 3633 struct bpf_tracing_link *link; 3634 u64 key = 0; 3635 int err; 3636 3637 switch (prog->type) { 3638 case BPF_PROG_TYPE_TRACING: 3639 if (prog->expected_attach_type != BPF_TRACE_FENTRY && 3640 prog->expected_attach_type != BPF_TRACE_FEXIT && 3641 prog->expected_attach_type != BPF_TRACE_FSESSION && 3642 prog->expected_attach_type != BPF_MODIFY_RETURN) { 3643 err = -EINVAL; 3644 goto out_put_prog; 3645 } 3646 break; 3647 case BPF_PROG_TYPE_EXT: 3648 if (prog->expected_attach_type != 0) { 3649 err = -EINVAL; 3650 goto out_put_prog; 3651 } 3652 break; 3653 case BPF_PROG_TYPE_LSM: 3654 if (prog->expected_attach_type != BPF_LSM_MAC) { 3655 err = -EINVAL; 3656 goto out_put_prog; 3657 } 3658 break; 3659 default: 3660 err = -EINVAL; 3661 goto out_put_prog; 3662 } 3663 3664 if (!!tgt_prog_fd != !!btf_id) { 3665 err = -EINVAL; 3666 goto out_put_prog; 3667 } 3668 3669 if (tgt_prog_fd) { 3670 /* 3671 * For now we only allow new targets for BPF_PROG_TYPE_EXT. If this 3672 * part would be changed to implement the same for 3673 * BPF_PROG_TYPE_TRACING, do not forget to update the way how 3674 * attach_tracing_prog flag is set. 3675 */ 3676 if (prog->type != BPF_PROG_TYPE_EXT) { 3677 err = -EINVAL; 3678 goto out_put_prog; 3679 } 3680 3681 tgt_prog = bpf_prog_get(tgt_prog_fd); 3682 if (IS_ERR(tgt_prog)) { 3683 err = PTR_ERR(tgt_prog); 3684 tgt_prog = NULL; 3685 goto out_put_prog; 3686 } 3687 3688 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id); 3689 } 3690 3691 link = kzalloc_obj(*link, GFP_USER); 3692 if (!link) { 3693 err = -ENOMEM; 3694 goto out_put_prog; 3695 } 3696 bpf_tramp_link_init(&link->link, BPF_LINK_TYPE_TRACING, 3697 &bpf_tracing_link_lops, prog, attach_type, bpf_cookie); 3698 3699 if (prog->expected_attach_type == BPF_TRACE_FSESSION) { 3700 link->fexit.link = &link->link.link; 3701 link->fexit.cookie = bpf_cookie; 3702 } 3703 3704 mutex_lock(&prog->aux->dst_mutex); 3705 3706 /* There are a few possible cases here: 3707 * 3708 * - if prog->aux->dst_trampoline is set, the program was just loaded 3709 * and not yet attached to anything, so we can use the values stored 3710 * in prog->aux 3711 * 3712 * - if prog->aux->dst_trampoline is NULL, the program has already been 3713 * attached to a target and its initial target was cleared (below) 3714 * 3715 * - if tgt_prog != NULL, the caller specified tgt_prog_fd + 3716 * target_btf_id using the link_create API. 3717 * 3718 * - if tgt_prog == NULL when this function was called using the old 3719 * raw_tracepoint_open API, and we need a target from prog->aux 3720 * 3721 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program 3722 * was detached and is going for re-attachment. 3723 * 3724 * - if prog->aux->dst_trampoline is NULL and tgt_prog and prog->aux->attach_btf 3725 * are NULL, then program was already attached and user did not provide 3726 * tgt_prog_fd so we have no way to find out or create trampoline 3727 */ 3728 if (!prog->aux->dst_trampoline && !tgt_prog) { 3729 /* 3730 * Allow re-attach for TRACING and LSM programs. If it's 3731 * currently linked, bpf_trampoline_link_prog will fail. 3732 * EXT programs need to specify tgt_prog_fd, so they 3733 * re-attach in separate code path. 3734 */ 3735 if (prog->type != BPF_PROG_TYPE_TRACING && 3736 prog->type != BPF_PROG_TYPE_LSM) { 3737 err = -EINVAL; 3738 goto out_unlock; 3739 } 3740 /* We can allow re-attach only if we have valid attach_btf. */ 3741 if (!prog->aux->attach_btf) { 3742 err = -EINVAL; 3743 goto out_unlock; 3744 } 3745 btf_id = prog->aux->attach_btf_id; 3746 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id); 3747 } 3748 3749 if (!prog->aux->dst_trampoline || 3750 (key && key != prog->aux->dst_trampoline->key)) { 3751 /* If there is no saved target, or the specified target is 3752 * different from the destination specified at load time, we 3753 * need a new trampoline and a check for compatibility 3754 */ 3755 struct bpf_attach_target_info tgt_info = {}; 3756 3757 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id, 3758 &tgt_info); 3759 if (err) 3760 goto out_unlock; 3761 3762 if (tgt_info.tgt_mod) { 3763 module_put(prog->aux->mod); 3764 prog->aux->mod = tgt_info.tgt_mod; 3765 } 3766 3767 tr = bpf_trampoline_get(key, &tgt_info); 3768 if (!tr) { 3769 err = -ENOMEM; 3770 goto out_unlock; 3771 } 3772 } else { 3773 /* The caller didn't specify a target, or the target was the 3774 * same as the destination supplied during program load. This 3775 * means we can reuse the trampoline and reference from program 3776 * load time, and there is no need to allocate a new one. This 3777 * can only happen once for any program, as the saved values in 3778 * prog->aux are cleared below. 3779 */ 3780 tr = prog->aux->dst_trampoline; 3781 tgt_prog = prog->aux->dst_prog; 3782 } 3783 /* 3784 * It is to prevent modifying struct pt_regs via kprobe_write_ctx=true 3785 * freplace prog. Without this check, kprobe_write_ctx=true freplace 3786 * prog is allowed to attach to kprobe_write_ctx=false kprobe prog, and 3787 * then modify the registers of the kprobe prog's target kernel 3788 * function. 3789 * 3790 * This also blocks the combination of uprobe+freplace, because it is 3791 * unable to recognize the use of the tgt_prog as an uprobe or a kprobe 3792 * by tgt_prog itself. At attach time, uprobe/kprobe is recognized by 3793 * the target perf event flags in __perf_event_set_bpf_prog(). 3794 */ 3795 if (prog->type == BPF_PROG_TYPE_EXT && 3796 prog->aux->kprobe_write_ctx != tgt_prog->aux->kprobe_write_ctx) { 3797 err = -EINVAL; 3798 goto out_unlock; 3799 } 3800 3801 err = bpf_link_prime(&link->link.link, &link_primer); 3802 if (err) 3803 goto out_unlock; 3804 3805 err = bpf_trampoline_link_prog(&link->link.node, tr, tgt_prog); 3806 if (err) { 3807 bpf_link_cleanup(&link_primer); 3808 link = NULL; 3809 goto out_unlock; 3810 } 3811 3812 link->tgt_prog = tgt_prog; 3813 link->trampoline = tr; 3814 3815 /* Always clear the trampoline and target prog from prog->aux to make 3816 * sure the original attach destination is not kept alive after a 3817 * program is (re-)attached to another target. 3818 */ 3819 if (prog->aux->dst_prog && 3820 (tgt_prog_fd || tr != prog->aux->dst_trampoline)) 3821 /* got extra prog ref from syscall, or attaching to different prog */ 3822 bpf_prog_put(prog->aux->dst_prog); 3823 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline) 3824 /* we allocated a new trampoline, so free the old one */ 3825 bpf_trampoline_put(prog->aux->dst_trampoline); 3826 3827 prog->aux->dst_prog = NULL; 3828 prog->aux->dst_trampoline = NULL; 3829 mutex_unlock(&prog->aux->dst_mutex); 3830 3831 return bpf_link_settle(&link_primer); 3832 out_unlock: 3833 if (tr && tr != prog->aux->dst_trampoline) 3834 bpf_trampoline_put(tr); 3835 mutex_unlock(&prog->aux->dst_mutex); 3836 kfree(link); 3837 out_put_prog: 3838 if (tgt_prog_fd && tgt_prog) 3839 bpf_prog_put(tgt_prog); 3840 return err; 3841 } 3842 3843 static void bpf_raw_tp_link_release(struct bpf_link *link) 3844 { 3845 struct bpf_raw_tp_link *raw_tp = 3846 container_of(link, struct bpf_raw_tp_link, link); 3847 3848 bpf_probe_unregister(raw_tp->btp, raw_tp); 3849 bpf_put_raw_tracepoint(raw_tp->btp); 3850 } 3851 3852 static void bpf_raw_tp_link_dealloc(struct bpf_link *link) 3853 { 3854 struct bpf_raw_tp_link *raw_tp = 3855 container_of(link, struct bpf_raw_tp_link, link); 3856 3857 kfree(raw_tp); 3858 } 3859 3860 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link, 3861 struct seq_file *seq) 3862 { 3863 struct bpf_raw_tp_link *raw_tp_link = 3864 container_of(link, struct bpf_raw_tp_link, link); 3865 3866 seq_printf(seq, 3867 "tp_name:\t%s\n" 3868 "cookie:\t%llu\n", 3869 raw_tp_link->btp->tp->name, 3870 raw_tp_link->cookie); 3871 } 3872 3873 static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen, 3874 u32 len) 3875 { 3876 if (ulen >= len + 1) { 3877 if (copy_to_user(ubuf, buf, len + 1)) 3878 return -EFAULT; 3879 } else { 3880 char zero = '\0'; 3881 3882 if (copy_to_user(ubuf, buf, ulen - 1)) 3883 return -EFAULT; 3884 if (put_user(zero, ubuf + ulen - 1)) 3885 return -EFAULT; 3886 return -ENOSPC; 3887 } 3888 3889 return 0; 3890 } 3891 3892 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, 3893 struct bpf_link_info *info) 3894 { 3895 struct bpf_raw_tp_link *raw_tp_link = 3896 container_of(link, struct bpf_raw_tp_link, link); 3897 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name); 3898 const char *tp_name = raw_tp_link->btp->tp->name; 3899 u32 ulen = info->raw_tracepoint.tp_name_len; 3900 size_t tp_len = strlen(tp_name); 3901 3902 if (!ulen ^ !ubuf) 3903 return -EINVAL; 3904 3905 info->raw_tracepoint.tp_name_len = tp_len + 1; 3906 info->raw_tracepoint.cookie = raw_tp_link->cookie; 3907 3908 if (!ubuf) 3909 return 0; 3910 3911 return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len); 3912 } 3913 3914 static const struct bpf_link_ops bpf_raw_tp_link_lops = { 3915 .release = bpf_raw_tp_link_release, 3916 .dealloc_deferred = bpf_raw_tp_link_dealloc, 3917 .show_fdinfo = bpf_raw_tp_link_show_fdinfo, 3918 .fill_link_info = bpf_raw_tp_link_fill_link_info, 3919 }; 3920 3921 #ifdef CONFIG_PERF_EVENTS 3922 struct bpf_perf_link { 3923 struct bpf_link link; 3924 struct file *perf_file; 3925 }; 3926 3927 static void bpf_perf_link_release(struct bpf_link *link) 3928 { 3929 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); 3930 struct perf_event *event = perf_link->perf_file->private_data; 3931 3932 perf_event_free_bpf_prog(event); 3933 fput(perf_link->perf_file); 3934 } 3935 3936 static void bpf_perf_link_dealloc(struct bpf_link *link) 3937 { 3938 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); 3939 3940 kfree(perf_link); 3941 } 3942 3943 static int bpf_perf_link_fill_common(const struct perf_event *event, 3944 char __user *uname, u32 *ulenp, 3945 u64 *probe_offset, u64 *probe_addr, 3946 u32 *fd_type, unsigned long *missed) 3947 { 3948 const char *buf; 3949 u32 prog_id, ulen; 3950 size_t len; 3951 int err; 3952 3953 ulen = *ulenp; 3954 if (!ulen ^ !uname) 3955 return -EINVAL; 3956 3957 err = bpf_get_perf_event_info(event, &prog_id, fd_type, &buf, 3958 probe_offset, probe_addr, missed); 3959 if (err) 3960 return err; 3961 3962 if (buf) { 3963 len = strlen(buf); 3964 *ulenp = len + 1; 3965 } else { 3966 *ulenp = 1; 3967 } 3968 if (!uname) 3969 return 0; 3970 3971 if (buf) { 3972 err = bpf_copy_to_user(uname, buf, ulen, len); 3973 if (err) 3974 return err; 3975 } else { 3976 char zero = '\0'; 3977 3978 if (put_user(zero, uname)) 3979 return -EFAULT; 3980 } 3981 return 0; 3982 } 3983 3984 #ifdef CONFIG_KPROBE_EVENTS 3985 static int bpf_perf_link_fill_kprobe(const struct perf_event *event, 3986 struct bpf_link_info *info) 3987 { 3988 unsigned long missed; 3989 char __user *uname; 3990 u64 addr, offset; 3991 u32 ulen, type; 3992 int err; 3993 3994 uname = u64_to_user_ptr(info->perf_event.kprobe.func_name); 3995 ulen = info->perf_event.kprobe.name_len; 3996 err = bpf_perf_link_fill_common(event, uname, &ulen, &offset, &addr, 3997 &type, &missed); 3998 if (err) 3999 return err; 4000 if (type == BPF_FD_TYPE_KRETPROBE) 4001 info->perf_event.type = BPF_PERF_EVENT_KRETPROBE; 4002 else 4003 info->perf_event.type = BPF_PERF_EVENT_KPROBE; 4004 info->perf_event.kprobe.name_len = ulen; 4005 info->perf_event.kprobe.offset = offset; 4006 info->perf_event.kprobe.missed = missed; 4007 if (!kallsyms_show_value(current_cred())) 4008 addr = 0; 4009 info->perf_event.kprobe.addr = addr; 4010 info->perf_event.kprobe.cookie = event->bpf_cookie; 4011 return 0; 4012 } 4013 4014 static void bpf_perf_link_fdinfo_kprobe(const struct perf_event *event, 4015 struct seq_file *seq) 4016 { 4017 const char *name; 4018 int err; 4019 u32 prog_id, type; 4020 u64 offset, addr; 4021 unsigned long missed; 4022 4023 err = bpf_get_perf_event_info(event, &prog_id, &type, &name, 4024 &offset, &addr, &missed); 4025 if (err) 4026 return; 4027 4028 seq_printf(seq, 4029 "name:\t%s\n" 4030 "offset:\t%#llx\n" 4031 "missed:\t%lu\n" 4032 "addr:\t%#llx\n" 4033 "event_type:\t%s\n" 4034 "cookie:\t%llu\n", 4035 name, offset, missed, addr, 4036 type == BPF_FD_TYPE_KRETPROBE ? "kretprobe" : "kprobe", 4037 event->bpf_cookie); 4038 } 4039 #endif 4040 4041 #ifdef CONFIG_UPROBE_EVENTS 4042 static int bpf_perf_link_fill_uprobe(const struct perf_event *event, 4043 struct bpf_link_info *info) 4044 { 4045 u64 ref_ctr_offset, offset; 4046 char __user *uname; 4047 u32 ulen, type; 4048 int err; 4049 4050 uname = u64_to_user_ptr(info->perf_event.uprobe.file_name); 4051 ulen = info->perf_event.uprobe.name_len; 4052 err = bpf_perf_link_fill_common(event, uname, &ulen, &offset, &ref_ctr_offset, 4053 &type, NULL); 4054 if (err) 4055 return err; 4056 4057 if (type == BPF_FD_TYPE_URETPROBE) 4058 info->perf_event.type = BPF_PERF_EVENT_URETPROBE; 4059 else 4060 info->perf_event.type = BPF_PERF_EVENT_UPROBE; 4061 info->perf_event.uprobe.name_len = ulen; 4062 info->perf_event.uprobe.offset = offset; 4063 info->perf_event.uprobe.cookie = event->bpf_cookie; 4064 info->perf_event.uprobe.ref_ctr_offset = ref_ctr_offset; 4065 return 0; 4066 } 4067 4068 static void bpf_perf_link_fdinfo_uprobe(const struct perf_event *event, 4069 struct seq_file *seq) 4070 { 4071 const char *name; 4072 int err; 4073 u32 prog_id, type; 4074 u64 offset, ref_ctr_offset; 4075 unsigned long missed; 4076 4077 err = bpf_get_perf_event_info(event, &prog_id, &type, &name, 4078 &offset, &ref_ctr_offset, &missed); 4079 if (err) 4080 return; 4081 4082 seq_printf(seq, 4083 "name:\t%s\n" 4084 "offset:\t%#llx\n" 4085 "ref_ctr_offset:\t%#llx\n" 4086 "event_type:\t%s\n" 4087 "cookie:\t%llu\n", 4088 name, offset, ref_ctr_offset, 4089 type == BPF_FD_TYPE_URETPROBE ? "uretprobe" : "uprobe", 4090 event->bpf_cookie); 4091 } 4092 #endif 4093 4094 static int bpf_perf_link_fill_probe(const struct perf_event *event, 4095 struct bpf_link_info *info) 4096 { 4097 #ifdef CONFIG_KPROBE_EVENTS 4098 if (event->tp_event->flags & TRACE_EVENT_FL_KPROBE) 4099 return bpf_perf_link_fill_kprobe(event, info); 4100 #endif 4101 #ifdef CONFIG_UPROBE_EVENTS 4102 if (event->tp_event->flags & TRACE_EVENT_FL_UPROBE) 4103 return bpf_perf_link_fill_uprobe(event, info); 4104 #endif 4105 return -EOPNOTSUPP; 4106 } 4107 4108 static int bpf_perf_link_fill_tracepoint(const struct perf_event *event, 4109 struct bpf_link_info *info) 4110 { 4111 char __user *uname; 4112 u32 ulen; 4113 int err; 4114 4115 uname = u64_to_user_ptr(info->perf_event.tracepoint.tp_name); 4116 ulen = info->perf_event.tracepoint.name_len; 4117 err = bpf_perf_link_fill_common(event, uname, &ulen, NULL, NULL, NULL, NULL); 4118 if (err) 4119 return err; 4120 4121 info->perf_event.type = BPF_PERF_EVENT_TRACEPOINT; 4122 info->perf_event.tracepoint.name_len = ulen; 4123 info->perf_event.tracepoint.cookie = event->bpf_cookie; 4124 return 0; 4125 } 4126 4127 static int bpf_perf_link_fill_perf_event(const struct perf_event *event, 4128 struct bpf_link_info *info) 4129 { 4130 info->perf_event.event.type = event->attr.type; 4131 info->perf_event.event.config = event->attr.config; 4132 info->perf_event.event.cookie = event->bpf_cookie; 4133 info->perf_event.type = BPF_PERF_EVENT_EVENT; 4134 return 0; 4135 } 4136 4137 static int bpf_perf_link_fill_link_info(const struct bpf_link *link, 4138 struct bpf_link_info *info) 4139 { 4140 struct bpf_perf_link *perf_link; 4141 const struct perf_event *event; 4142 4143 perf_link = container_of(link, struct bpf_perf_link, link); 4144 event = perf_get_event(perf_link->perf_file); 4145 if (IS_ERR(event)) 4146 return PTR_ERR(event); 4147 4148 switch (event->prog->type) { 4149 case BPF_PROG_TYPE_PERF_EVENT: 4150 return bpf_perf_link_fill_perf_event(event, info); 4151 case BPF_PROG_TYPE_TRACEPOINT: 4152 return bpf_perf_link_fill_tracepoint(event, info); 4153 case BPF_PROG_TYPE_KPROBE: 4154 return bpf_perf_link_fill_probe(event, info); 4155 default: 4156 return -EOPNOTSUPP; 4157 } 4158 } 4159 4160 static void bpf_perf_event_link_show_fdinfo(const struct perf_event *event, 4161 struct seq_file *seq) 4162 { 4163 seq_printf(seq, 4164 "type:\t%u\n" 4165 "config:\t%llu\n" 4166 "event_type:\t%s\n" 4167 "cookie:\t%llu\n", 4168 event->attr.type, event->attr.config, 4169 "event", event->bpf_cookie); 4170 } 4171 4172 static void bpf_tracepoint_link_show_fdinfo(const struct perf_event *event, 4173 struct seq_file *seq) 4174 { 4175 int err; 4176 const char *name; 4177 u32 prog_id; 4178 4179 err = bpf_get_perf_event_info(event, &prog_id, NULL, &name, NULL, 4180 NULL, NULL); 4181 if (err) 4182 return; 4183 4184 seq_printf(seq, 4185 "tp_name:\t%s\n" 4186 "event_type:\t%s\n" 4187 "cookie:\t%llu\n", 4188 name, "tracepoint", event->bpf_cookie); 4189 } 4190 4191 static void bpf_probe_link_show_fdinfo(const struct perf_event *event, 4192 struct seq_file *seq) 4193 { 4194 #ifdef CONFIG_KPROBE_EVENTS 4195 if (event->tp_event->flags & TRACE_EVENT_FL_KPROBE) 4196 return bpf_perf_link_fdinfo_kprobe(event, seq); 4197 #endif 4198 4199 #ifdef CONFIG_UPROBE_EVENTS 4200 if (event->tp_event->flags & TRACE_EVENT_FL_UPROBE) 4201 return bpf_perf_link_fdinfo_uprobe(event, seq); 4202 #endif 4203 } 4204 4205 static void bpf_perf_link_show_fdinfo(const struct bpf_link *link, 4206 struct seq_file *seq) 4207 { 4208 struct bpf_perf_link *perf_link; 4209 const struct perf_event *event; 4210 4211 perf_link = container_of(link, struct bpf_perf_link, link); 4212 event = perf_get_event(perf_link->perf_file); 4213 if (IS_ERR(event)) 4214 return; 4215 4216 switch (event->prog->type) { 4217 case BPF_PROG_TYPE_PERF_EVENT: 4218 return bpf_perf_event_link_show_fdinfo(event, seq); 4219 case BPF_PROG_TYPE_TRACEPOINT: 4220 return bpf_tracepoint_link_show_fdinfo(event, seq); 4221 case BPF_PROG_TYPE_KPROBE: 4222 return bpf_probe_link_show_fdinfo(event, seq); 4223 default: 4224 return; 4225 } 4226 } 4227 4228 static const struct bpf_link_ops bpf_perf_link_lops = { 4229 .release = bpf_perf_link_release, 4230 .dealloc = bpf_perf_link_dealloc, 4231 .fill_link_info = bpf_perf_link_fill_link_info, 4232 .show_fdinfo = bpf_perf_link_show_fdinfo, 4233 }; 4234 4235 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 4236 { 4237 struct bpf_link_primer link_primer; 4238 struct bpf_perf_link *link; 4239 struct perf_event *event; 4240 struct file *perf_file; 4241 int err; 4242 4243 if (attr->link_create.flags) 4244 return -EINVAL; 4245 4246 perf_file = perf_event_get(attr->link_create.target_fd); 4247 if (IS_ERR(perf_file)) 4248 return PTR_ERR(perf_file); 4249 4250 link = kzalloc_obj(*link, GFP_USER); 4251 if (!link) { 4252 err = -ENOMEM; 4253 goto out_put_file; 4254 } 4255 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog, 4256 attr->link_create.attach_type); 4257 link->perf_file = perf_file; 4258 4259 err = bpf_link_prime(&link->link, &link_primer); 4260 if (err) { 4261 kfree(link); 4262 goto out_put_file; 4263 } 4264 4265 event = perf_file->private_data; 4266 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie); 4267 if (err) { 4268 bpf_link_cleanup(&link_primer); 4269 goto out_put_file; 4270 } 4271 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */ 4272 bpf_prog_inc(prog); 4273 4274 return bpf_link_settle(&link_primer); 4275 4276 out_put_file: 4277 fput(perf_file); 4278 return err; 4279 } 4280 #else 4281 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 4282 { 4283 return -EOPNOTSUPP; 4284 } 4285 #endif /* CONFIG_PERF_EVENTS */ 4286 4287 static int bpf_raw_tp_link_attach(struct bpf_prog *prog, 4288 const char __user *user_tp_name, u64 cookie, 4289 enum bpf_attach_type attach_type) 4290 { 4291 struct bpf_link_primer link_primer; 4292 struct bpf_raw_tp_link *link; 4293 struct bpf_raw_event_map *btp; 4294 const char *tp_name; 4295 char buf[128]; 4296 int err; 4297 4298 switch (prog->type) { 4299 case BPF_PROG_TYPE_TRACING: 4300 case BPF_PROG_TYPE_EXT: 4301 case BPF_PROG_TYPE_LSM: 4302 if (user_tp_name) 4303 /* The attach point for this category of programs 4304 * should be specified via btf_id during program load. 4305 */ 4306 return -EINVAL; 4307 if (prog->type == BPF_PROG_TYPE_TRACING && 4308 prog->expected_attach_type == BPF_TRACE_RAW_TP) { 4309 tp_name = prog->aux->attach_func_name; 4310 break; 4311 } 4312 return bpf_tracing_prog_attach(prog, 0, 0, 0, attach_type); 4313 case BPF_PROG_TYPE_RAW_TRACEPOINT: 4314 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 4315 if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0) 4316 return -EFAULT; 4317 buf[sizeof(buf) - 1] = 0; 4318 tp_name = buf; 4319 break; 4320 default: 4321 return -EINVAL; 4322 } 4323 4324 btp = bpf_get_raw_tracepoint(tp_name); 4325 if (!btp) 4326 return -ENOENT; 4327 4328 if (prog->sleepable && !tracepoint_is_faultable(btp->tp)) { 4329 bpf_put_raw_tracepoint(btp); 4330 return -EINVAL; 4331 } 4332 4333 link = kzalloc_obj(*link, GFP_USER); 4334 if (!link) { 4335 err = -ENOMEM; 4336 goto out_put_btp; 4337 } 4338 bpf_link_init_sleepable(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT, 4339 &bpf_raw_tp_link_lops, prog, attach_type, 4340 tracepoint_is_faultable(btp->tp)); 4341 link->btp = btp; 4342 link->cookie = cookie; 4343 4344 err = bpf_link_prime(&link->link, &link_primer); 4345 if (err) { 4346 kfree(link); 4347 goto out_put_btp; 4348 } 4349 4350 err = bpf_probe_register(link->btp, link); 4351 if (err) { 4352 bpf_link_cleanup(&link_primer); 4353 goto out_put_btp; 4354 } 4355 4356 return bpf_link_settle(&link_primer); 4357 4358 out_put_btp: 4359 bpf_put_raw_tracepoint(btp); 4360 return err; 4361 } 4362 4363 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.cookie 4364 4365 static int bpf_raw_tracepoint_open(const union bpf_attr *attr) 4366 { 4367 struct bpf_prog *prog; 4368 void __user *tp_name; 4369 __u64 cookie; 4370 int fd; 4371 4372 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN)) 4373 return -EINVAL; 4374 4375 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); 4376 if (IS_ERR(prog)) 4377 return PTR_ERR(prog); 4378 4379 tp_name = u64_to_user_ptr(attr->raw_tracepoint.name); 4380 cookie = attr->raw_tracepoint.cookie; 4381 fd = bpf_raw_tp_link_attach(prog, tp_name, cookie, prog->expected_attach_type); 4382 if (fd < 0) 4383 bpf_prog_put(prog); 4384 return fd; 4385 } 4386 4387 static enum bpf_prog_type 4388 attach_type_to_prog_type(enum bpf_attach_type attach_type) 4389 { 4390 switch (attach_type) { 4391 case BPF_CGROUP_INET_INGRESS: 4392 case BPF_CGROUP_INET_EGRESS: 4393 return BPF_PROG_TYPE_CGROUP_SKB; 4394 case BPF_CGROUP_INET_SOCK_CREATE: 4395 case BPF_CGROUP_INET_SOCK_RELEASE: 4396 case BPF_CGROUP_INET4_POST_BIND: 4397 case BPF_CGROUP_INET6_POST_BIND: 4398 return BPF_PROG_TYPE_CGROUP_SOCK; 4399 case BPF_CGROUP_INET4_BIND: 4400 case BPF_CGROUP_INET6_BIND: 4401 case BPF_CGROUP_INET4_CONNECT: 4402 case BPF_CGROUP_INET6_CONNECT: 4403 case BPF_CGROUP_UNIX_CONNECT: 4404 case BPF_CGROUP_INET4_GETPEERNAME: 4405 case BPF_CGROUP_INET6_GETPEERNAME: 4406 case BPF_CGROUP_UNIX_GETPEERNAME: 4407 case BPF_CGROUP_INET4_GETSOCKNAME: 4408 case BPF_CGROUP_INET6_GETSOCKNAME: 4409 case BPF_CGROUP_UNIX_GETSOCKNAME: 4410 case BPF_CGROUP_UDP4_SENDMSG: 4411 case BPF_CGROUP_UDP6_SENDMSG: 4412 case BPF_CGROUP_UNIX_SENDMSG: 4413 case BPF_CGROUP_UDP4_RECVMSG: 4414 case BPF_CGROUP_UDP6_RECVMSG: 4415 case BPF_CGROUP_UNIX_RECVMSG: 4416 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 4417 case BPF_CGROUP_SOCK_OPS: 4418 return BPF_PROG_TYPE_SOCK_OPS; 4419 case BPF_CGROUP_DEVICE: 4420 return BPF_PROG_TYPE_CGROUP_DEVICE; 4421 case BPF_SK_MSG_VERDICT: 4422 return BPF_PROG_TYPE_SK_MSG; 4423 case BPF_SK_SKB_STREAM_PARSER: 4424 case BPF_SK_SKB_STREAM_VERDICT: 4425 case BPF_SK_SKB_VERDICT: 4426 return BPF_PROG_TYPE_SK_SKB; 4427 case BPF_LIRC_MODE2: 4428 return BPF_PROG_TYPE_LIRC_MODE2; 4429 case BPF_FLOW_DISSECTOR: 4430 return BPF_PROG_TYPE_FLOW_DISSECTOR; 4431 case BPF_CGROUP_SYSCTL: 4432 return BPF_PROG_TYPE_CGROUP_SYSCTL; 4433 case BPF_CGROUP_GETSOCKOPT: 4434 case BPF_CGROUP_SETSOCKOPT: 4435 return BPF_PROG_TYPE_CGROUP_SOCKOPT; 4436 case BPF_TRACE_ITER: 4437 case BPF_TRACE_RAW_TP: 4438 case BPF_TRACE_FENTRY: 4439 case BPF_TRACE_FEXIT: 4440 case BPF_TRACE_FSESSION: 4441 case BPF_TRACE_FSESSION_MULTI: 4442 case BPF_TRACE_FENTRY_MULTI: 4443 case BPF_TRACE_FEXIT_MULTI: 4444 case BPF_MODIFY_RETURN: 4445 return BPF_PROG_TYPE_TRACING; 4446 case BPF_LSM_MAC: 4447 return BPF_PROG_TYPE_LSM; 4448 case BPF_SK_LOOKUP: 4449 return BPF_PROG_TYPE_SK_LOOKUP; 4450 case BPF_XDP: 4451 return BPF_PROG_TYPE_XDP; 4452 case BPF_LSM_CGROUP: 4453 return BPF_PROG_TYPE_LSM; 4454 case BPF_TCX_INGRESS: 4455 case BPF_TCX_EGRESS: 4456 case BPF_NETKIT_PRIMARY: 4457 case BPF_NETKIT_PEER: 4458 return BPF_PROG_TYPE_SCHED_CLS; 4459 default: 4460 return BPF_PROG_TYPE_UNSPEC; 4461 } 4462 } 4463 4464 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, 4465 enum bpf_attach_type attach_type) 4466 { 4467 enum bpf_prog_type ptype; 4468 4469 switch (prog->type) { 4470 case BPF_PROG_TYPE_CGROUP_SOCK: 4471 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 4472 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 4473 case BPF_PROG_TYPE_SK_LOOKUP: 4474 return attach_type == prog->expected_attach_type ? 0 : -EINVAL; 4475 case BPF_PROG_TYPE_CGROUP_SKB: 4476 if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN)) 4477 /* cg-skb progs can be loaded by unpriv user. 4478 * check permissions at attach time. 4479 */ 4480 return -EPERM; 4481 4482 ptype = attach_type_to_prog_type(attach_type); 4483 if (prog->type != ptype) 4484 return -EINVAL; 4485 4486 return prog->enforce_expected_attach_type && 4487 prog->expected_attach_type != attach_type ? 4488 -EINVAL : 0; 4489 case BPF_PROG_TYPE_EXT: 4490 return 0; 4491 case BPF_PROG_TYPE_NETFILTER: 4492 if (attach_type != BPF_NETFILTER) 4493 return -EINVAL; 4494 return 0; 4495 case BPF_PROG_TYPE_PERF_EVENT: 4496 case BPF_PROG_TYPE_TRACEPOINT: 4497 if (attach_type != BPF_PERF_EVENT) 4498 return -EINVAL; 4499 return 0; 4500 case BPF_PROG_TYPE_KPROBE: 4501 if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI && 4502 attach_type != BPF_TRACE_KPROBE_MULTI) 4503 return -EINVAL; 4504 if (prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION && 4505 attach_type != BPF_TRACE_KPROBE_SESSION) 4506 return -EINVAL; 4507 if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI && 4508 attach_type != BPF_TRACE_UPROBE_MULTI) 4509 return -EINVAL; 4510 if (prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION && 4511 attach_type != BPF_TRACE_UPROBE_SESSION) 4512 return -EINVAL; 4513 if (attach_type != BPF_PERF_EVENT && 4514 attach_type != BPF_TRACE_KPROBE_MULTI && 4515 attach_type != BPF_TRACE_KPROBE_SESSION && 4516 attach_type != BPF_TRACE_UPROBE_MULTI && 4517 attach_type != BPF_TRACE_UPROBE_SESSION) 4518 return -EINVAL; 4519 return 0; 4520 case BPF_PROG_TYPE_SCHED_CLS: 4521 if (attach_type != BPF_TCX_INGRESS && 4522 attach_type != BPF_TCX_EGRESS && 4523 attach_type != BPF_NETKIT_PRIMARY && 4524 attach_type != BPF_NETKIT_PEER) 4525 return -EINVAL; 4526 return 0; 4527 default: 4528 ptype = attach_type_to_prog_type(attach_type); 4529 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) 4530 return -EINVAL; 4531 return 0; 4532 } 4533 } 4534 4535 static bool is_cgroup_prog_type(enum bpf_prog_type ptype, enum bpf_attach_type atype, 4536 bool check_atype) 4537 { 4538 switch (ptype) { 4539 case BPF_PROG_TYPE_CGROUP_DEVICE: 4540 case BPF_PROG_TYPE_CGROUP_SKB: 4541 case BPF_PROG_TYPE_CGROUP_SOCK: 4542 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 4543 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 4544 case BPF_PROG_TYPE_CGROUP_SYSCTL: 4545 case BPF_PROG_TYPE_SOCK_OPS: 4546 return true; 4547 case BPF_PROG_TYPE_LSM: 4548 return check_atype ? atype == BPF_LSM_CGROUP : true; 4549 default: 4550 return false; 4551 } 4552 } 4553 4554 #define BPF_PROG_ATTACH_LAST_FIELD expected_revision 4555 4556 #define BPF_F_ATTACH_MASK_BASE \ 4557 (BPF_F_ALLOW_OVERRIDE | \ 4558 BPF_F_ALLOW_MULTI | \ 4559 BPF_F_REPLACE | \ 4560 BPF_F_PREORDER) 4561 4562 #define BPF_F_ATTACH_MASK_MPROG \ 4563 (BPF_F_REPLACE | \ 4564 BPF_F_BEFORE | \ 4565 BPF_F_AFTER | \ 4566 BPF_F_ID | \ 4567 BPF_F_LINK) 4568 4569 static int bpf_prog_attach(const union bpf_attr *attr) 4570 { 4571 enum bpf_prog_type ptype; 4572 struct bpf_prog *prog; 4573 int ret; 4574 4575 if (CHECK_ATTR(BPF_PROG_ATTACH)) 4576 return -EINVAL; 4577 4578 ptype = attach_type_to_prog_type(attr->attach_type); 4579 if (ptype == BPF_PROG_TYPE_UNSPEC) 4580 return -EINVAL; 4581 if (bpf_mprog_supported(ptype)) { 4582 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG) 4583 return -EINVAL; 4584 } else if (is_cgroup_prog_type(ptype, 0, false)) { 4585 if (attr->attach_flags & ~(BPF_F_ATTACH_MASK_BASE | BPF_F_ATTACH_MASK_MPROG)) 4586 return -EINVAL; 4587 } else { 4588 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_BASE) 4589 return -EINVAL; 4590 if (attr->relative_fd || 4591 attr->expected_revision) 4592 return -EINVAL; 4593 } 4594 4595 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 4596 if (IS_ERR(prog)) 4597 return PTR_ERR(prog); 4598 4599 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { 4600 bpf_prog_put(prog); 4601 return -EINVAL; 4602 } 4603 4604 if (is_cgroup_prog_type(ptype, prog->expected_attach_type, true)) { 4605 ret = cgroup_bpf_prog_attach(attr, ptype, prog); 4606 goto out; 4607 } 4608 4609 switch (ptype) { 4610 case BPF_PROG_TYPE_SK_SKB: 4611 case BPF_PROG_TYPE_SK_MSG: 4612 ret = sock_map_get_from_fd(attr, prog); 4613 break; 4614 case BPF_PROG_TYPE_LIRC_MODE2: 4615 ret = lirc_prog_attach(attr, prog); 4616 break; 4617 case BPF_PROG_TYPE_FLOW_DISSECTOR: 4618 ret = netns_bpf_prog_attach(attr, prog); 4619 break; 4620 case BPF_PROG_TYPE_SCHED_CLS: 4621 if (attr->attach_type == BPF_TCX_INGRESS || 4622 attr->attach_type == BPF_TCX_EGRESS) 4623 ret = tcx_prog_attach(attr, prog); 4624 else 4625 ret = netkit_prog_attach(attr, prog); 4626 break; 4627 default: 4628 ret = -EINVAL; 4629 } 4630 out: 4631 if (ret) 4632 bpf_prog_put(prog); 4633 return ret; 4634 } 4635 4636 #define BPF_PROG_DETACH_LAST_FIELD expected_revision 4637 4638 static int bpf_prog_detach(const union bpf_attr *attr) 4639 { 4640 struct bpf_prog *prog = NULL; 4641 enum bpf_prog_type ptype; 4642 int ret; 4643 4644 if (CHECK_ATTR(BPF_PROG_DETACH)) 4645 return -EINVAL; 4646 4647 ptype = attach_type_to_prog_type(attr->attach_type); 4648 if (bpf_mprog_supported(ptype)) { 4649 if (ptype == BPF_PROG_TYPE_UNSPEC) 4650 return -EINVAL; 4651 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG) 4652 return -EINVAL; 4653 if (attr->attach_bpf_fd) { 4654 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 4655 if (IS_ERR(prog)) 4656 return PTR_ERR(prog); 4657 } else if (!bpf_mprog_detach_empty(ptype)) { 4658 return -EPERM; 4659 } 4660 } else if (is_cgroup_prog_type(ptype, 0, false)) { 4661 if (attr->attach_flags || attr->relative_fd) 4662 return -EINVAL; 4663 } else if (attr->attach_flags || 4664 attr->relative_fd || 4665 attr->expected_revision) { 4666 return -EINVAL; 4667 } 4668 4669 switch (ptype) { 4670 case BPF_PROG_TYPE_SK_MSG: 4671 case BPF_PROG_TYPE_SK_SKB: 4672 ret = sock_map_prog_detach(attr, ptype); 4673 break; 4674 case BPF_PROG_TYPE_LIRC_MODE2: 4675 ret = lirc_prog_detach(attr); 4676 break; 4677 case BPF_PROG_TYPE_FLOW_DISSECTOR: 4678 ret = netns_bpf_prog_detach(attr, ptype); 4679 break; 4680 case BPF_PROG_TYPE_CGROUP_DEVICE: 4681 case BPF_PROG_TYPE_CGROUP_SKB: 4682 case BPF_PROG_TYPE_CGROUP_SOCK: 4683 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 4684 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 4685 case BPF_PROG_TYPE_CGROUP_SYSCTL: 4686 case BPF_PROG_TYPE_SOCK_OPS: 4687 case BPF_PROG_TYPE_LSM: 4688 ret = cgroup_bpf_prog_detach(attr, ptype); 4689 break; 4690 case BPF_PROG_TYPE_SCHED_CLS: 4691 if (attr->attach_type == BPF_TCX_INGRESS || 4692 attr->attach_type == BPF_TCX_EGRESS) 4693 ret = tcx_prog_detach(attr, prog); 4694 else 4695 ret = netkit_prog_detach(attr, prog); 4696 break; 4697 default: 4698 ret = -EINVAL; 4699 } 4700 4701 if (prog) 4702 bpf_prog_put(prog); 4703 return ret; 4704 } 4705 4706 #define BPF_PROG_QUERY_LAST_FIELD query.revision 4707 4708 static int bpf_prog_query(const union bpf_attr *attr, 4709 union bpf_attr __user *uattr, u32 uattr_size) 4710 { 4711 if (!bpf_net_capable()) 4712 return -EPERM; 4713 if (CHECK_ATTR(BPF_PROG_QUERY)) 4714 return -EINVAL; 4715 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) 4716 return -EINVAL; 4717 4718 switch (attr->query.attach_type) { 4719 case BPF_CGROUP_INET_INGRESS: 4720 case BPF_CGROUP_INET_EGRESS: 4721 case BPF_CGROUP_INET_SOCK_CREATE: 4722 case BPF_CGROUP_INET_SOCK_RELEASE: 4723 case BPF_CGROUP_INET4_BIND: 4724 case BPF_CGROUP_INET6_BIND: 4725 case BPF_CGROUP_INET4_POST_BIND: 4726 case BPF_CGROUP_INET6_POST_BIND: 4727 case BPF_CGROUP_INET4_CONNECT: 4728 case BPF_CGROUP_INET6_CONNECT: 4729 case BPF_CGROUP_UNIX_CONNECT: 4730 case BPF_CGROUP_INET4_GETPEERNAME: 4731 case BPF_CGROUP_INET6_GETPEERNAME: 4732 case BPF_CGROUP_UNIX_GETPEERNAME: 4733 case BPF_CGROUP_INET4_GETSOCKNAME: 4734 case BPF_CGROUP_INET6_GETSOCKNAME: 4735 case BPF_CGROUP_UNIX_GETSOCKNAME: 4736 case BPF_CGROUP_UDP4_SENDMSG: 4737 case BPF_CGROUP_UDP6_SENDMSG: 4738 case BPF_CGROUP_UNIX_SENDMSG: 4739 case BPF_CGROUP_UDP4_RECVMSG: 4740 case BPF_CGROUP_UDP6_RECVMSG: 4741 case BPF_CGROUP_UNIX_RECVMSG: 4742 case BPF_CGROUP_SOCK_OPS: 4743 case BPF_CGROUP_DEVICE: 4744 case BPF_CGROUP_SYSCTL: 4745 case BPF_CGROUP_GETSOCKOPT: 4746 case BPF_CGROUP_SETSOCKOPT: 4747 case BPF_LSM_CGROUP: 4748 return cgroup_bpf_prog_query(attr, uattr, uattr_size); 4749 case BPF_LIRC_MODE2: 4750 return lirc_prog_query(attr, uattr); 4751 case BPF_FLOW_DISSECTOR: 4752 case BPF_SK_LOOKUP: 4753 return netns_bpf_prog_query(attr, uattr); 4754 case BPF_SK_SKB_STREAM_PARSER: 4755 case BPF_SK_SKB_STREAM_VERDICT: 4756 case BPF_SK_MSG_VERDICT: 4757 case BPF_SK_SKB_VERDICT: 4758 return sock_map_bpf_prog_query(attr, uattr); 4759 case BPF_TCX_INGRESS: 4760 case BPF_TCX_EGRESS: 4761 return tcx_prog_query(attr, uattr); 4762 case BPF_NETKIT_PRIMARY: 4763 case BPF_NETKIT_PEER: 4764 return netkit_prog_query(attr, uattr); 4765 default: 4766 return -EINVAL; 4767 } 4768 } 4769 4770 #define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size 4771 4772 static int bpf_prog_test_run(const union bpf_attr *attr, 4773 union bpf_attr __user *uattr) 4774 { 4775 struct bpf_prog *prog; 4776 int ret = -ENOTSUPP; 4777 4778 if (CHECK_ATTR(BPF_PROG_TEST_RUN)) 4779 return -EINVAL; 4780 4781 if ((attr->test.ctx_size_in && !attr->test.ctx_in) || 4782 (!attr->test.ctx_size_in && attr->test.ctx_in)) 4783 return -EINVAL; 4784 4785 if ((attr->test.ctx_size_out && !attr->test.ctx_out) || 4786 (!attr->test.ctx_size_out && attr->test.ctx_out)) 4787 return -EINVAL; 4788 4789 prog = bpf_prog_get(attr->test.prog_fd); 4790 if (IS_ERR(prog)) 4791 return PTR_ERR(prog); 4792 4793 if (prog->aux->ops->test_run) 4794 ret = prog->aux->ops->test_run(prog, attr, uattr); 4795 4796 bpf_prog_put(prog); 4797 return ret; 4798 } 4799 4800 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id 4801 4802 static int bpf_obj_get_next_id(const union bpf_attr *attr, 4803 union bpf_attr __user *uattr, 4804 struct idr *idr, 4805 spinlock_t *lock) 4806 { 4807 u32 next_id = attr->start_id; 4808 int err = 0; 4809 4810 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) 4811 return -EINVAL; 4812 4813 if (!capable(CAP_SYS_ADMIN)) 4814 return -EPERM; 4815 4816 next_id++; 4817 spin_lock_bh(lock); 4818 if (!idr_get_next(idr, &next_id)) 4819 err = -ENOENT; 4820 spin_unlock_bh(lock); 4821 4822 if (!err) 4823 err = put_user(next_id, &uattr->next_id); 4824 4825 return err; 4826 } 4827 4828 struct bpf_map *bpf_map_get_curr_or_next(u32 *id) 4829 { 4830 struct bpf_map *map; 4831 4832 spin_lock_bh(&map_idr_lock); 4833 again: 4834 map = idr_get_next(&map_idr, id); 4835 if (map) { 4836 map = __bpf_map_inc_not_zero(map, false); 4837 if (IS_ERR(map)) { 4838 (*id)++; 4839 goto again; 4840 } 4841 } 4842 spin_unlock_bh(&map_idr_lock); 4843 4844 return map; 4845 } 4846 4847 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id) 4848 { 4849 struct bpf_prog *prog; 4850 4851 spin_lock_bh(&prog_idr_lock); 4852 again: 4853 prog = idr_get_next(&prog_idr, id); 4854 if (prog) { 4855 prog = bpf_prog_inc_not_zero(prog); 4856 if (IS_ERR(prog)) { 4857 (*id)++; 4858 goto again; 4859 } 4860 } 4861 spin_unlock_bh(&prog_idr_lock); 4862 4863 return prog; 4864 } 4865 4866 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id 4867 4868 struct bpf_prog *bpf_prog_by_id(u32 id) 4869 { 4870 struct bpf_prog *prog; 4871 4872 if (!id) 4873 return ERR_PTR(-ENOENT); 4874 4875 spin_lock_bh(&prog_idr_lock); 4876 prog = idr_find(&prog_idr, id); 4877 if (prog) 4878 prog = bpf_prog_inc_not_zero(prog); 4879 else 4880 prog = ERR_PTR(-ENOENT); 4881 spin_unlock_bh(&prog_idr_lock); 4882 return prog; 4883 } 4884 4885 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) 4886 { 4887 struct bpf_prog *prog; 4888 u32 id = attr->prog_id; 4889 int fd; 4890 4891 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) 4892 return -EINVAL; 4893 4894 if (!capable(CAP_SYS_ADMIN)) 4895 return -EPERM; 4896 4897 prog = bpf_prog_by_id(id); 4898 if (IS_ERR(prog)) 4899 return PTR_ERR(prog); 4900 4901 fd = bpf_prog_new_fd(prog); 4902 if (fd < 0) 4903 bpf_prog_put(prog); 4904 4905 return fd; 4906 } 4907 4908 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags 4909 4910 static int bpf_map_get_fd_by_id(const union bpf_attr *attr) 4911 { 4912 struct bpf_map *map; 4913 u32 id = attr->map_id; 4914 int f_flags; 4915 int fd; 4916 4917 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || 4918 attr->open_flags & ~BPF_OBJ_FLAG_MASK) 4919 return -EINVAL; 4920 4921 if (!capable(CAP_SYS_ADMIN)) 4922 return -EPERM; 4923 4924 f_flags = bpf_get_file_flag(attr->open_flags); 4925 if (f_flags < 0) 4926 return f_flags; 4927 4928 spin_lock_bh(&map_idr_lock); 4929 map = idr_find(&map_idr, id); 4930 if (map) 4931 map = __bpf_map_inc_not_zero(map, true); 4932 else 4933 map = ERR_PTR(-ENOENT); 4934 spin_unlock_bh(&map_idr_lock); 4935 4936 if (IS_ERR(map)) 4937 return PTR_ERR(map); 4938 4939 fd = bpf_map_new_fd(map, f_flags); 4940 if (fd < 0) 4941 bpf_map_put_with_uref(map); 4942 4943 return fd; 4944 } 4945 4946 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, 4947 unsigned long addr, u32 *off, 4948 u32 *type) 4949 { 4950 const struct bpf_map *map; 4951 int i; 4952 4953 mutex_lock(&prog->aux->used_maps_mutex); 4954 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { 4955 map = prog->aux->used_maps[i]; 4956 if (map == (void *)addr) { 4957 *type = BPF_PSEUDO_MAP_FD; 4958 goto out; 4959 } 4960 if (!map->ops->map_direct_value_meta) 4961 continue; 4962 if (!map->ops->map_direct_value_meta(map, addr, off)) { 4963 *type = BPF_PSEUDO_MAP_VALUE; 4964 goto out; 4965 } 4966 } 4967 map = NULL; 4968 4969 out: 4970 mutex_unlock(&prog->aux->used_maps_mutex); 4971 return map; 4972 } 4973 4974 static void prepare_dump_pseudo_call(struct bpf_insn *insn) 4975 { 4976 s32 call_off = insn->imm; 4977 4978 /* 4979 * BPF_CALL_ARGS only exists for interpreter fallback. 4980 * 1. For interpreter (BPF_CALL_ARGS): insn->off is the index of 4981 * interpreters_args array, so here using bpf_call_args_imm() 4982 * to get the real address offset. 4983 * 2. For JIT (BPF_CALL): insn->off is the subprog id. 4984 */ 4985 if (insn->code == (BPF_JMP | BPF_CALL_ARGS)) 4986 insn->imm = bpf_call_args_imm(insn->off); 4987 else 4988 insn->imm = insn->off; 4989 4990 /* Avoid dumping a truncated and misleading pc-relative offset. */ 4991 if (call_off > S16_MAX || call_off < S16_MIN) 4992 insn->off = 0; 4993 else 4994 insn->off = call_off; 4995 } 4996 4997 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog, 4998 const struct cred *f_cred) 4999 { 5000 const struct bpf_map *map; 5001 struct bpf_insn *insns; 5002 u32 off, type; 5003 u64 imm; 5004 u8 code; 5005 int i; 5006 5007 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), 5008 GFP_USER); 5009 if (!insns) 5010 return insns; 5011 5012 for (i = 0; i < prog->len; i++) { 5013 code = insns[i].code; 5014 5015 if (code == (BPF_JMP | BPF_TAIL_CALL)) { 5016 insns[i].code = BPF_JMP | BPF_CALL; 5017 insns[i].imm = BPF_FUNC_tail_call; 5018 /* fall-through */ 5019 } 5020 if (code == (BPF_JMP | BPF_CALL) || 5021 code == (BPF_JMP | BPF_CALL_ARGS)) { 5022 /* Restore the legacy xlated dump layout. */ 5023 if (insns[i].src_reg == BPF_PSEUDO_CALL) 5024 prepare_dump_pseudo_call(&insns[i]); 5025 if (code == (BPF_JMP | BPF_CALL_ARGS)) 5026 insns[i].code = BPF_JMP | BPF_CALL; 5027 if (!bpf_dump_raw_ok(f_cred)) 5028 insns[i].imm = 0; 5029 continue; 5030 } 5031 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) { 5032 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM; 5033 continue; 5034 } 5035 5036 if ((BPF_CLASS(code) == BPF_LDX || BPF_CLASS(code) == BPF_STX || 5037 BPF_CLASS(code) == BPF_ST) && BPF_MODE(code) == BPF_PROBE_MEM32) { 5038 insns[i].code = BPF_CLASS(code) | BPF_SIZE(code) | BPF_MEM; 5039 continue; 5040 } 5041 5042 if (code != (BPF_LD | BPF_IMM | BPF_DW)) 5043 continue; 5044 5045 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; 5046 map = bpf_map_from_imm(prog, imm, &off, &type); 5047 if (map) { 5048 insns[i].src_reg = type; 5049 insns[i].imm = map->id; 5050 insns[i + 1].imm = off; 5051 continue; 5052 } 5053 } 5054 5055 return insns; 5056 } 5057 5058 static int set_info_rec_size(struct bpf_prog_info *info) 5059 { 5060 /* 5061 * Ensure info.*_rec_size is the same as kernel expected size 5062 * 5063 * or 5064 * 5065 * Only allow zero *_rec_size if both _rec_size and _cnt are 5066 * zero. In this case, the kernel will set the expected 5067 * _rec_size back to the info. 5068 */ 5069 5070 if ((info->nr_func_info || info->func_info_rec_size) && 5071 info->func_info_rec_size != sizeof(struct bpf_func_info)) 5072 return -EINVAL; 5073 5074 if ((info->nr_line_info || info->line_info_rec_size) && 5075 info->line_info_rec_size != sizeof(struct bpf_line_info)) 5076 return -EINVAL; 5077 5078 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && 5079 info->jited_line_info_rec_size != sizeof(__u64)) 5080 return -EINVAL; 5081 5082 info->func_info_rec_size = sizeof(struct bpf_func_info); 5083 info->line_info_rec_size = sizeof(struct bpf_line_info); 5084 info->jited_line_info_rec_size = sizeof(__u64); 5085 5086 return 0; 5087 } 5088 5089 static int bpf_prog_get_info_by_fd(struct file *file, 5090 struct bpf_prog *prog, 5091 const union bpf_attr *attr, 5092 union bpf_attr __user *uattr) 5093 { 5094 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5095 struct btf *attach_btf = bpf_prog_get_target_btf(prog); 5096 struct bpf_prog_info info; 5097 u32 info_len = attr->info.info_len; 5098 struct bpf_prog_kstats stats; 5099 char __user *uinsns; 5100 u32 ulen, len; 5101 int err; 5102 5103 len = offsetofend(struct bpf_prog_info, attach_btf_id); 5104 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), len, info_len); 5105 if (err) 5106 return err; 5107 info_len = min_t(u32, sizeof(info), info_len); 5108 5109 memset(&info, 0, sizeof(info)); 5110 if (copy_from_user(&info, uinfo, info_len)) 5111 return -EFAULT; 5112 5113 info.type = prog->type; 5114 info.id = prog->aux->id; 5115 info.load_time = prog->aux->load_time; 5116 info.created_by_uid = from_kuid_munged(current_user_ns(), 5117 prog->aux->user->uid); 5118 info.gpl_compatible = prog->gpl_compatible; 5119 5120 memcpy(info.tag, prog->tag, sizeof(prog->tag)); 5121 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); 5122 5123 mutex_lock(&prog->aux->used_maps_mutex); 5124 ulen = info.nr_map_ids; 5125 info.nr_map_ids = prog->aux->used_map_cnt; 5126 ulen = min_t(u32, info.nr_map_ids, ulen); 5127 if (ulen) { 5128 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); 5129 u32 i; 5130 5131 for (i = 0; i < ulen; i++) 5132 if (put_user(prog->aux->used_maps[i]->id, 5133 &user_map_ids[i])) { 5134 mutex_unlock(&prog->aux->used_maps_mutex); 5135 return -EFAULT; 5136 } 5137 } 5138 mutex_unlock(&prog->aux->used_maps_mutex); 5139 5140 err = set_info_rec_size(&info); 5141 if (err) 5142 return err; 5143 5144 bpf_prog_get_stats(prog, &stats); 5145 info.run_time_ns = stats.nsecs; 5146 info.run_cnt = stats.cnt; 5147 info.recursion_misses = stats.misses; 5148 5149 info.verified_insns = prog->aux->verified_insns; 5150 if (prog->aux->btf) 5151 info.btf_id = btf_obj_id(prog->aux->btf); 5152 5153 if (!bpf_capable()) { 5154 info.jited_prog_len = 0; 5155 info.xlated_prog_len = 0; 5156 info.nr_jited_ksyms = 0; 5157 info.nr_jited_func_lens = 0; 5158 info.nr_func_info = 0; 5159 info.nr_line_info = 0; 5160 info.nr_jited_line_info = 0; 5161 goto done; 5162 } 5163 5164 ulen = info.xlated_prog_len; 5165 info.xlated_prog_len = bpf_prog_insn_size(prog); 5166 if (info.xlated_prog_len && ulen) { 5167 struct bpf_insn *insns_sanitized; 5168 bool fault; 5169 5170 if (!prog->blinded || bpf_dump_raw_ok(file->f_cred)) { 5171 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred); 5172 if (!insns_sanitized) 5173 return -ENOMEM; 5174 uinsns = u64_to_user_ptr(info.xlated_prog_insns); 5175 ulen = min_t(u32, info.xlated_prog_len, ulen); 5176 fault = copy_to_user(uinsns, insns_sanitized, ulen); 5177 kfree(insns_sanitized); 5178 if (fault) 5179 return -EFAULT; 5180 } else { 5181 info.xlated_prog_insns = 0; 5182 } 5183 } 5184 5185 if (bpf_prog_is_offloaded(prog->aux)) { 5186 err = bpf_prog_offload_info_fill(&info, prog); 5187 if (err) 5188 return err; 5189 goto done; 5190 } 5191 5192 /* NOTE: the following code is supposed to be skipped for offload. 5193 * bpf_prog_offload_info_fill() is the place to fill similar fields 5194 * for offload. 5195 */ 5196 ulen = info.jited_prog_len; 5197 if (prog->aux->func_cnt) { 5198 u32 i; 5199 5200 info.jited_prog_len = 0; 5201 for (i = 0; i < prog->aux->func_cnt; i++) 5202 info.jited_prog_len += prog->aux->func[i]->jited_len; 5203 } else { 5204 info.jited_prog_len = prog->jited_len; 5205 } 5206 5207 if (info.jited_prog_len && ulen) { 5208 if (bpf_dump_raw_ok(file->f_cred)) { 5209 uinsns = u64_to_user_ptr(info.jited_prog_insns); 5210 ulen = min_t(u32, info.jited_prog_len, ulen); 5211 5212 /* for multi-function programs, copy the JITed 5213 * instructions for all the functions 5214 */ 5215 if (prog->aux->func_cnt) { 5216 u32 len, free, i; 5217 u8 *img; 5218 5219 free = ulen; 5220 for (i = 0; i < prog->aux->func_cnt; i++) { 5221 len = prog->aux->func[i]->jited_len; 5222 len = min_t(u32, len, free); 5223 img = (u8 *) prog->aux->func[i]->bpf_func; 5224 if (copy_to_user(uinsns, img, len)) 5225 return -EFAULT; 5226 uinsns += len; 5227 free -= len; 5228 if (!free) 5229 break; 5230 } 5231 } else { 5232 if (copy_to_user(uinsns, prog->bpf_func, ulen)) 5233 return -EFAULT; 5234 } 5235 } else { 5236 info.jited_prog_insns = 0; 5237 } 5238 } 5239 5240 ulen = info.nr_jited_ksyms; 5241 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; 5242 if (ulen) { 5243 if (bpf_dump_raw_ok(file->f_cred)) { 5244 unsigned long ksym_addr; 5245 u64 __user *user_ksyms; 5246 u32 i; 5247 5248 /* copy the address of the kernel symbol 5249 * corresponding to each function 5250 */ 5251 ulen = min_t(u32, info.nr_jited_ksyms, ulen); 5252 user_ksyms = u64_to_user_ptr(info.jited_ksyms); 5253 if (prog->aux->func_cnt) { 5254 for (i = 0; i < ulen; i++) { 5255 ksym_addr = (unsigned long) 5256 prog->aux->func[i]->bpf_func; 5257 if (put_user((u64) ksym_addr, 5258 &user_ksyms[i])) 5259 return -EFAULT; 5260 } 5261 } else { 5262 ksym_addr = (unsigned long) prog->bpf_func; 5263 if (put_user((u64) ksym_addr, &user_ksyms[0])) 5264 return -EFAULT; 5265 } 5266 } else { 5267 info.jited_ksyms = 0; 5268 } 5269 } 5270 5271 ulen = info.nr_jited_func_lens; 5272 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; 5273 if (ulen) { 5274 if (bpf_dump_raw_ok(file->f_cred)) { 5275 u32 __user *user_lens; 5276 u32 func_len, i; 5277 5278 /* copy the JITed image lengths for each function */ 5279 ulen = min_t(u32, info.nr_jited_func_lens, ulen); 5280 user_lens = u64_to_user_ptr(info.jited_func_lens); 5281 if (prog->aux->func_cnt) { 5282 for (i = 0; i < ulen; i++) { 5283 func_len = 5284 prog->aux->func[i]->jited_len; 5285 if (put_user(func_len, &user_lens[i])) 5286 return -EFAULT; 5287 } 5288 } else { 5289 func_len = prog->jited_len; 5290 if (put_user(func_len, &user_lens[0])) 5291 return -EFAULT; 5292 } 5293 } else { 5294 info.jited_func_lens = 0; 5295 } 5296 } 5297 5298 info.attach_btf_id = prog->aux->attach_btf_id; 5299 if (attach_btf) 5300 info.attach_btf_obj_id = btf_obj_id(attach_btf); 5301 5302 ulen = info.nr_func_info; 5303 info.nr_func_info = prog->aux->func_info_cnt; 5304 if (info.nr_func_info && ulen) { 5305 char __user *user_finfo; 5306 5307 user_finfo = u64_to_user_ptr(info.func_info); 5308 ulen = min_t(u32, info.nr_func_info, ulen); 5309 if (copy_to_user(user_finfo, prog->aux->func_info, 5310 info.func_info_rec_size * ulen)) 5311 return -EFAULT; 5312 } 5313 5314 ulen = info.nr_line_info; 5315 info.nr_line_info = prog->aux->nr_linfo; 5316 if (info.nr_line_info && ulen) { 5317 __u8 __user *user_linfo; 5318 5319 user_linfo = u64_to_user_ptr(info.line_info); 5320 ulen = min_t(u32, info.nr_line_info, ulen); 5321 if (copy_to_user(user_linfo, prog->aux->linfo, 5322 info.line_info_rec_size * ulen)) 5323 return -EFAULT; 5324 } 5325 5326 ulen = info.nr_jited_line_info; 5327 if (prog->aux->jited_linfo) 5328 info.nr_jited_line_info = prog->aux->nr_linfo; 5329 else 5330 info.nr_jited_line_info = 0; 5331 if (info.nr_jited_line_info && ulen) { 5332 if (bpf_dump_raw_ok(file->f_cred)) { 5333 unsigned long line_addr; 5334 __u64 __user *user_linfo; 5335 u32 i; 5336 5337 user_linfo = u64_to_user_ptr(info.jited_line_info); 5338 ulen = min_t(u32, info.nr_jited_line_info, ulen); 5339 for (i = 0; i < ulen; i++) { 5340 line_addr = (unsigned long)prog->aux->jited_linfo[i]; 5341 if (put_user((__u64)line_addr, &user_linfo[i])) 5342 return -EFAULT; 5343 } 5344 } else { 5345 info.jited_line_info = 0; 5346 } 5347 } 5348 5349 ulen = info.nr_prog_tags; 5350 info.nr_prog_tags = prog->aux->func_cnt ? : 1; 5351 if (ulen) { 5352 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; 5353 u32 i; 5354 5355 user_prog_tags = u64_to_user_ptr(info.prog_tags); 5356 ulen = min_t(u32, info.nr_prog_tags, ulen); 5357 if (prog->aux->func_cnt) { 5358 for (i = 0; i < ulen; i++) { 5359 if (copy_to_user(user_prog_tags[i], 5360 prog->aux->func[i]->tag, 5361 BPF_TAG_SIZE)) 5362 return -EFAULT; 5363 } 5364 } else { 5365 if (copy_to_user(user_prog_tags[0], 5366 prog->tag, BPF_TAG_SIZE)) 5367 return -EFAULT; 5368 } 5369 } 5370 5371 done: 5372 if (copy_to_user(uinfo, &info, info_len) || 5373 put_user(info_len, &uattr->info.info_len)) 5374 return -EFAULT; 5375 5376 return 0; 5377 } 5378 5379 static int bpf_map_get_info_by_fd(struct file *file, 5380 struct bpf_map *map, 5381 const union bpf_attr *attr, 5382 union bpf_attr __user *uattr) 5383 { 5384 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5385 struct bpf_map_info info; 5386 u32 info_len = attr->info.info_len, len; 5387 int err; 5388 5389 len = offsetofend(struct bpf_map_info, hash_size); 5390 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), len, info_len); 5391 if (err) 5392 return err; 5393 info_len = min_t(u32, sizeof(info), info_len); 5394 5395 memset(&info, 0, sizeof(info)); 5396 if (copy_from_user(&info, uinfo, info_len)) 5397 return -EFAULT; 5398 5399 info.type = map->map_type; 5400 info.id = map->id; 5401 info.key_size = map->key_size; 5402 info.value_size = map->value_size; 5403 info.max_entries = map->max_entries; 5404 info.map_flags = map->map_flags; 5405 info.map_extra = map->map_extra; 5406 memcpy(info.name, map->name, sizeof(map->name)); 5407 5408 if (map->btf) { 5409 info.btf_id = btf_obj_id(map->btf); 5410 info.btf_key_type_id = map->btf_key_type_id; 5411 info.btf_value_type_id = map->btf_value_type_id; 5412 } 5413 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; 5414 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) 5415 bpf_map_struct_ops_info_fill(&info, map); 5416 5417 if (bpf_map_is_offloaded(map)) { 5418 err = bpf_map_offload_info_fill(&info, map); 5419 if (err) 5420 return err; 5421 } 5422 5423 if (info.hash) { 5424 char __user *uhash = u64_to_user_ptr(info.hash); 5425 5426 if (!map->ops->map_get_hash) 5427 return -EINVAL; 5428 if (info.hash_size != sizeof(map->sha)) 5429 return -EINVAL; 5430 if (!READ_ONCE(map->frozen)) 5431 return -EPERM; 5432 5433 err = map->ops->map_get_hash(map); 5434 if (err != 0) 5435 return err; 5436 5437 if (copy_to_user(uhash, map->sha, sizeof(map->sha)) != 0) 5438 return -EFAULT; 5439 } else if (info.hash_size) { 5440 return -EINVAL; 5441 } 5442 5443 if (copy_to_user(uinfo, &info, info_len) || 5444 put_user(info_len, &uattr->info.info_len)) 5445 return -EFAULT; 5446 5447 return 0; 5448 } 5449 5450 static int bpf_btf_get_info_by_fd(struct file *file, 5451 struct btf *btf, 5452 const union bpf_attr *attr, 5453 union bpf_attr __user *uattr) 5454 { 5455 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5456 u32 info_len = attr->info.info_len; 5457 int err; 5458 5459 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len); 5460 if (err) 5461 return err; 5462 5463 return btf_get_info_by_fd(btf, attr, uattr); 5464 } 5465 5466 static int bpf_link_get_info_by_fd(struct file *file, 5467 struct bpf_link *link, 5468 const union bpf_attr *attr, 5469 union bpf_attr __user *uattr) 5470 { 5471 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5472 struct bpf_link_info info; 5473 const struct bpf_prog *prog; 5474 u32 info_len = attr->info.info_len; 5475 int err; 5476 5477 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); 5478 if (err) 5479 return err; 5480 info_len = min_t(u32, sizeof(info), info_len); 5481 5482 memset(&info, 0, sizeof(info)); 5483 if (copy_from_user(&info, uinfo, info_len)) 5484 return -EFAULT; 5485 5486 info.type = link->type; 5487 info.id = link->id; 5488 5489 rcu_read_lock(); 5490 prog = READ_ONCE(link->prog); 5491 if (prog) 5492 info.prog_id = prog->aux->id; 5493 rcu_read_unlock(); 5494 5495 if (link->ops->fill_link_info) { 5496 err = link->ops->fill_link_info(link, &info); 5497 if (err) 5498 return err; 5499 } 5500 5501 if (copy_to_user(uinfo, &info, info_len) || 5502 put_user(info_len, &uattr->info.info_len)) 5503 return -EFAULT; 5504 5505 return 0; 5506 } 5507 5508 5509 static int token_get_info_by_fd(struct file *file, 5510 struct bpf_token *token, 5511 const union bpf_attr *attr, 5512 union bpf_attr __user *uattr) 5513 { 5514 struct bpf_token_info __user *uinfo = u64_to_user_ptr(attr->info.info); 5515 u32 info_len = attr->info.info_len; 5516 int err; 5517 5518 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len); 5519 if (err) 5520 return err; 5521 return bpf_token_get_info_by_fd(token, attr, uattr); 5522 } 5523 5524 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info 5525 5526 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, 5527 union bpf_attr __user *uattr) 5528 { 5529 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) 5530 return -EINVAL; 5531 5532 CLASS(fd, f)(attr->info.bpf_fd); 5533 if (fd_empty(f)) 5534 return -EBADFD; 5535 5536 if (fd_file(f)->f_op == &bpf_prog_fops) 5537 return bpf_prog_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, 5538 uattr); 5539 else if (fd_file(f)->f_op == &bpf_map_fops) 5540 return bpf_map_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, 5541 uattr); 5542 else if (fd_file(f)->f_op == &btf_fops) 5543 return bpf_btf_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, uattr); 5544 else if (fd_file(f)->f_op == &bpf_link_fops || fd_file(f)->f_op == &bpf_link_fops_poll) 5545 return bpf_link_get_info_by_fd(fd_file(f), fd_file(f)->private_data, 5546 attr, uattr); 5547 else if (fd_file(f)->f_op == &bpf_token_fops) 5548 return token_get_info_by_fd(fd_file(f), fd_file(f)->private_data, 5549 attr, uattr); 5550 return -EINVAL; 5551 } 5552 5553 #define BPF_BTF_LOAD_LAST_FIELD btf_token_fd 5554 5555 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log) 5556 { 5557 struct bpf_token *token = NULL; 5558 5559 if (CHECK_ATTR(BPF_BTF_LOAD)) 5560 return -EINVAL; 5561 5562 if (attr->btf_flags & ~BPF_F_TOKEN_FD) 5563 return -EINVAL; 5564 5565 if (attr->btf_flags & BPF_F_TOKEN_FD) { 5566 token = bpf_token_get_from_fd(attr->btf_token_fd); 5567 if (IS_ERR(token)) 5568 return PTR_ERR(token); 5569 if (!bpf_token_allow_cmd(token, BPF_BTF_LOAD)) { 5570 bpf_token_put(token); 5571 token = NULL; 5572 } 5573 } 5574 5575 if (!bpf_token_capable(token, CAP_BPF)) { 5576 bpf_token_put(token); 5577 return -EPERM; 5578 } 5579 5580 bpf_token_put(token); 5581 5582 return btf_new_fd(attr, uattr, attr_log); 5583 } 5584 5585 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD fd_by_id_token_fd 5586 5587 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) 5588 { 5589 struct bpf_token *token = NULL; 5590 5591 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) 5592 return -EINVAL; 5593 5594 if (attr->open_flags & ~BPF_F_TOKEN_FD) 5595 return -EINVAL; 5596 5597 if (attr->open_flags & BPF_F_TOKEN_FD) { 5598 token = bpf_token_get_from_fd(attr->fd_by_id_token_fd); 5599 if (IS_ERR(token)) 5600 return PTR_ERR(token); 5601 if (!bpf_token_allow_cmd(token, BPF_BTF_GET_FD_BY_ID)) { 5602 bpf_token_put(token); 5603 token = NULL; 5604 } 5605 } 5606 5607 if (!bpf_token_capable(token, CAP_SYS_ADMIN)) { 5608 bpf_token_put(token); 5609 return -EPERM; 5610 } 5611 5612 bpf_token_put(token); 5613 5614 return btf_get_fd_by_id(attr->btf_id); 5615 } 5616 5617 static int bpf_task_fd_query_copy(const union bpf_attr *attr, 5618 union bpf_attr __user *uattr, 5619 u32 prog_id, u32 fd_type, 5620 const char *buf, u64 probe_offset, 5621 u64 probe_addr) 5622 { 5623 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); 5624 u32 len = buf ? strlen(buf) : 0, input_len; 5625 int err = 0; 5626 5627 if (put_user(len, &uattr->task_fd_query.buf_len)) 5628 return -EFAULT; 5629 input_len = attr->task_fd_query.buf_len; 5630 if (input_len && ubuf) { 5631 if (!len) { 5632 /* nothing to copy, just make ubuf NULL terminated */ 5633 char zero = '\0'; 5634 5635 if (put_user(zero, ubuf)) 5636 return -EFAULT; 5637 } else { 5638 err = bpf_copy_to_user(ubuf, buf, input_len, len); 5639 if (err == -EFAULT) 5640 return err; 5641 } 5642 } 5643 5644 if (put_user(prog_id, &uattr->task_fd_query.prog_id) || 5645 put_user(fd_type, &uattr->task_fd_query.fd_type) || 5646 put_user(probe_offset, &uattr->task_fd_query.probe_offset) || 5647 put_user(probe_addr, &uattr->task_fd_query.probe_addr)) 5648 return -EFAULT; 5649 5650 return err; 5651 } 5652 5653 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr 5654 5655 static int bpf_task_fd_query(const union bpf_attr *attr, 5656 union bpf_attr __user *uattr) 5657 { 5658 pid_t pid = attr->task_fd_query.pid; 5659 u32 fd = attr->task_fd_query.fd; 5660 const struct perf_event *event; 5661 struct task_struct *task; 5662 struct file *file; 5663 int err; 5664 5665 if (CHECK_ATTR(BPF_TASK_FD_QUERY)) 5666 return -EINVAL; 5667 5668 if (!capable(CAP_SYS_ADMIN)) 5669 return -EPERM; 5670 5671 if (attr->task_fd_query.flags != 0) 5672 return -EINVAL; 5673 5674 rcu_read_lock(); 5675 task = get_pid_task(find_vpid(pid), PIDTYPE_PID); 5676 rcu_read_unlock(); 5677 if (!task) 5678 return -ENOENT; 5679 5680 err = 0; 5681 file = fget_task(task, fd); 5682 put_task_struct(task); 5683 if (!file) 5684 return -EBADF; 5685 5686 if (file->f_op == &bpf_link_fops || file->f_op == &bpf_link_fops_poll) { 5687 struct bpf_link *link = file->private_data; 5688 5689 if (link->ops == &bpf_raw_tp_link_lops) { 5690 struct bpf_raw_tp_link *raw_tp = 5691 container_of(link, struct bpf_raw_tp_link, link); 5692 struct bpf_raw_event_map *btp = raw_tp->btp; 5693 5694 err = bpf_task_fd_query_copy(attr, uattr, 5695 raw_tp->link.prog->aux->id, 5696 BPF_FD_TYPE_RAW_TRACEPOINT, 5697 btp->tp->name, 0, 0); 5698 goto put_file; 5699 } 5700 goto out_not_supp; 5701 } 5702 5703 event = perf_get_event(file); 5704 if (!IS_ERR(event)) { 5705 u64 probe_offset, probe_addr; 5706 u32 prog_id, fd_type; 5707 const char *buf; 5708 5709 err = bpf_get_perf_event_info(event, &prog_id, &fd_type, 5710 &buf, &probe_offset, 5711 &probe_addr, NULL); 5712 if (!err) 5713 err = bpf_task_fd_query_copy(attr, uattr, prog_id, 5714 fd_type, buf, 5715 probe_offset, 5716 probe_addr); 5717 goto put_file; 5718 } 5719 5720 out_not_supp: 5721 err = -ENOTSUPP; 5722 put_file: 5723 fput(file); 5724 return err; 5725 } 5726 5727 #define BPF_MAP_BATCH_LAST_FIELD batch.flags 5728 5729 #define BPF_DO_BATCH(fn, ...) \ 5730 do { \ 5731 if (!fn) { \ 5732 err = -ENOTSUPP; \ 5733 goto err_put; \ 5734 } \ 5735 err = fn(__VA_ARGS__); \ 5736 } while (0) 5737 5738 static int bpf_map_do_batch(const union bpf_attr *attr, 5739 union bpf_attr __user *uattr, 5740 int cmd) 5741 { 5742 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH || 5743 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH; 5744 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH; 5745 struct bpf_map *map; 5746 int err; 5747 5748 if (CHECK_ATTR(BPF_MAP_BATCH)) 5749 return -EINVAL; 5750 5751 CLASS(fd, f)(attr->batch.map_fd); 5752 5753 map = __bpf_map_get(f); 5754 if (IS_ERR(map)) 5755 return PTR_ERR(map); 5756 if (has_write) 5757 bpf_map_write_active_inc(map); 5758 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { 5759 err = -EPERM; 5760 goto err_put; 5761 } 5762 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 5763 err = -EPERM; 5764 goto err_put; 5765 } 5766 5767 if (cmd == BPF_MAP_LOOKUP_BATCH) 5768 BPF_DO_BATCH(map->ops->map_lookup_batch, map, attr, uattr); 5769 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) 5770 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch, map, attr, uattr); 5771 else if (cmd == BPF_MAP_UPDATE_BATCH) 5772 BPF_DO_BATCH(map->ops->map_update_batch, map, fd_file(f), attr, uattr); 5773 else 5774 BPF_DO_BATCH(map->ops->map_delete_batch, map, attr, uattr); 5775 err_put: 5776 if (has_write) { 5777 maybe_wait_bpf_programs(map); 5778 bpf_map_write_active_dec(map); 5779 } 5780 return err; 5781 } 5782 5783 #define BPF_LINK_CREATE_LAST_FIELD link_create.uprobe_multi.path_fd 5784 static int link_create(union bpf_attr *attr, bpfptr_t uattr) 5785 { 5786 struct bpf_prog *prog; 5787 int ret; 5788 5789 if (CHECK_ATTR(BPF_LINK_CREATE)) 5790 return -EINVAL; 5791 5792 if (attr->link_create.attach_type == BPF_STRUCT_OPS) 5793 return bpf_struct_ops_link_create(attr); 5794 5795 prog = bpf_prog_get(attr->link_create.prog_fd); 5796 if (IS_ERR(prog)) 5797 return PTR_ERR(prog); 5798 5799 ret = bpf_prog_attach_check_attach_type(prog, 5800 attr->link_create.attach_type); 5801 if (ret) 5802 goto out; 5803 5804 switch (prog->type) { 5805 case BPF_PROG_TYPE_CGROUP_SKB: 5806 case BPF_PROG_TYPE_CGROUP_SOCK: 5807 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 5808 case BPF_PROG_TYPE_SOCK_OPS: 5809 case BPF_PROG_TYPE_CGROUP_DEVICE: 5810 case BPF_PROG_TYPE_CGROUP_SYSCTL: 5811 case BPF_PROG_TYPE_CGROUP_SOCKOPT: 5812 ret = cgroup_bpf_link_attach(attr, prog); 5813 break; 5814 case BPF_PROG_TYPE_EXT: 5815 ret = bpf_tracing_prog_attach(prog, 5816 attr->link_create.target_fd, 5817 attr->link_create.target_btf_id, 5818 attr->link_create.tracing.cookie, 5819 attr->link_create.attach_type); 5820 break; 5821 case BPF_PROG_TYPE_LSM: 5822 case BPF_PROG_TYPE_TRACING: 5823 if (attr->link_create.attach_type != prog->expected_attach_type) { 5824 ret = -EINVAL; 5825 goto out; 5826 } 5827 if (prog->expected_attach_type == BPF_TRACE_RAW_TP) 5828 ret = bpf_raw_tp_link_attach(prog, NULL, attr->link_create.tracing.cookie, 5829 attr->link_create.attach_type); 5830 else if (prog->expected_attach_type == BPF_TRACE_ITER) 5831 ret = bpf_iter_link_attach(attr, uattr, prog); 5832 else if (prog->expected_attach_type == BPF_LSM_CGROUP) 5833 ret = cgroup_bpf_link_attach(attr, prog); 5834 else if (is_tracing_multi(prog->expected_attach_type)) 5835 ret = bpf_tracing_multi_attach(prog, attr); 5836 else 5837 ret = bpf_tracing_prog_attach(prog, 5838 attr->link_create.target_fd, 5839 attr->link_create.target_btf_id, 5840 attr->link_create.tracing.cookie, 5841 attr->link_create.attach_type); 5842 break; 5843 case BPF_PROG_TYPE_FLOW_DISSECTOR: 5844 case BPF_PROG_TYPE_SK_LOOKUP: 5845 ret = netns_bpf_link_create(attr, prog); 5846 break; 5847 case BPF_PROG_TYPE_SK_MSG: 5848 case BPF_PROG_TYPE_SK_SKB: 5849 ret = sock_map_link_create(attr, prog); 5850 break; 5851 #ifdef CONFIG_NET 5852 case BPF_PROG_TYPE_XDP: 5853 ret = bpf_xdp_link_attach(attr, prog); 5854 break; 5855 case BPF_PROG_TYPE_SCHED_CLS: 5856 if (attr->link_create.attach_type == BPF_TCX_INGRESS || 5857 attr->link_create.attach_type == BPF_TCX_EGRESS) 5858 ret = tcx_link_attach(attr, prog); 5859 else 5860 ret = netkit_link_attach(attr, prog); 5861 break; 5862 case BPF_PROG_TYPE_NETFILTER: 5863 ret = bpf_nf_link_attach(attr, prog); 5864 break; 5865 #endif 5866 case BPF_PROG_TYPE_PERF_EVENT: 5867 case BPF_PROG_TYPE_TRACEPOINT: 5868 ret = bpf_perf_link_attach(attr, prog); 5869 break; 5870 case BPF_PROG_TYPE_KPROBE: 5871 if (attr->link_create.attach_type == BPF_PERF_EVENT) 5872 ret = bpf_perf_link_attach(attr, prog); 5873 else if (attr->link_create.attach_type == BPF_TRACE_KPROBE_MULTI || 5874 attr->link_create.attach_type == BPF_TRACE_KPROBE_SESSION) 5875 ret = bpf_kprobe_multi_link_attach(attr, prog); 5876 else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI || 5877 attr->link_create.attach_type == BPF_TRACE_UPROBE_SESSION) 5878 ret = bpf_uprobe_multi_link_attach(attr, prog); 5879 break; 5880 default: 5881 ret = -EINVAL; 5882 } 5883 5884 out: 5885 if (ret < 0) 5886 bpf_prog_put(prog); 5887 return ret; 5888 } 5889 5890 static int link_update_map(struct bpf_link *link, union bpf_attr *attr) 5891 { 5892 struct bpf_map *new_map, *old_map = NULL; 5893 int ret; 5894 5895 new_map = bpf_map_get(attr->link_update.new_map_fd); 5896 if (IS_ERR(new_map)) 5897 return PTR_ERR(new_map); 5898 5899 if (attr->link_update.flags & BPF_F_REPLACE) { 5900 old_map = bpf_map_get(attr->link_update.old_map_fd); 5901 if (IS_ERR(old_map)) { 5902 ret = PTR_ERR(old_map); 5903 goto out_put; 5904 } 5905 } else if (attr->link_update.old_map_fd) { 5906 ret = -EINVAL; 5907 goto out_put; 5908 } 5909 5910 ret = link->ops->update_map(link, new_map, old_map); 5911 5912 if (old_map) 5913 bpf_map_put(old_map); 5914 out_put: 5915 bpf_map_put(new_map); 5916 return ret; 5917 } 5918 5919 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd 5920 5921 static int link_update(union bpf_attr *attr) 5922 { 5923 struct bpf_prog *old_prog = NULL, *new_prog; 5924 struct bpf_link *link; 5925 u32 flags; 5926 int ret; 5927 5928 if (CHECK_ATTR(BPF_LINK_UPDATE)) 5929 return -EINVAL; 5930 5931 flags = attr->link_update.flags; 5932 if (flags & ~BPF_F_REPLACE) 5933 return -EINVAL; 5934 5935 link = bpf_link_get_from_fd(attr->link_update.link_fd); 5936 if (IS_ERR(link)) 5937 return PTR_ERR(link); 5938 5939 if (link->ops->update_map) { 5940 ret = link_update_map(link, attr); 5941 goto out_put_link; 5942 } 5943 5944 new_prog = bpf_prog_get(attr->link_update.new_prog_fd); 5945 if (IS_ERR(new_prog)) { 5946 ret = PTR_ERR(new_prog); 5947 goto out_put_link; 5948 } 5949 5950 if (flags & BPF_F_REPLACE) { 5951 old_prog = bpf_prog_get(attr->link_update.old_prog_fd); 5952 if (IS_ERR(old_prog)) { 5953 ret = PTR_ERR(old_prog); 5954 old_prog = NULL; 5955 goto out_put_progs; 5956 } 5957 } else if (attr->link_update.old_prog_fd) { 5958 ret = -EINVAL; 5959 goto out_put_progs; 5960 } 5961 5962 if (link->ops->update_prog) 5963 ret = link->ops->update_prog(link, new_prog, old_prog); 5964 else 5965 ret = -EINVAL; 5966 5967 out_put_progs: 5968 if (old_prog) 5969 bpf_prog_put(old_prog); 5970 if (ret) 5971 bpf_prog_put(new_prog); 5972 out_put_link: 5973 bpf_link_put_direct(link); 5974 return ret; 5975 } 5976 5977 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd 5978 5979 static int link_detach(union bpf_attr *attr) 5980 { 5981 struct bpf_link *link; 5982 int ret; 5983 5984 if (CHECK_ATTR(BPF_LINK_DETACH)) 5985 return -EINVAL; 5986 5987 link = bpf_link_get_from_fd(attr->link_detach.link_fd); 5988 if (IS_ERR(link)) 5989 return PTR_ERR(link); 5990 5991 if (link->ops->detach) 5992 ret = link->ops->detach(link); 5993 else 5994 ret = -EOPNOTSUPP; 5995 5996 bpf_link_put_direct(link); 5997 return ret; 5998 } 5999 6000 struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link) 6001 { 6002 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT); 6003 } 6004 EXPORT_SYMBOL(bpf_link_inc_not_zero); 6005 6006 struct bpf_link *bpf_link_by_id(u32 id) 6007 { 6008 struct bpf_link *link; 6009 6010 if (!id) 6011 return ERR_PTR(-ENOENT); 6012 6013 spin_lock_bh(&link_idr_lock); 6014 /* before link is "settled", ID is 0, pretend it doesn't exist yet */ 6015 link = idr_find(&link_idr, id); 6016 if (link) { 6017 if (link->id) 6018 link = bpf_link_inc_not_zero(link); 6019 else 6020 link = ERR_PTR(-EAGAIN); 6021 } else { 6022 link = ERR_PTR(-ENOENT); 6023 } 6024 spin_unlock_bh(&link_idr_lock); 6025 return link; 6026 } 6027 6028 struct bpf_link *bpf_link_get_curr_or_next(u32 *id) 6029 { 6030 struct bpf_link *link; 6031 6032 spin_lock_bh(&link_idr_lock); 6033 again: 6034 link = idr_get_next(&link_idr, id); 6035 if (link) { 6036 link = bpf_link_inc_not_zero(link); 6037 if (IS_ERR(link)) { 6038 (*id)++; 6039 goto again; 6040 } 6041 } 6042 spin_unlock_bh(&link_idr_lock); 6043 6044 return link; 6045 } 6046 6047 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id 6048 6049 static int bpf_link_get_fd_by_id(const union bpf_attr *attr) 6050 { 6051 struct bpf_link *link; 6052 u32 id = attr->link_id; 6053 int fd; 6054 6055 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID)) 6056 return -EINVAL; 6057 6058 if (!capable(CAP_SYS_ADMIN)) 6059 return -EPERM; 6060 6061 link = bpf_link_by_id(id); 6062 if (IS_ERR(link)) 6063 return PTR_ERR(link); 6064 6065 fd = bpf_link_new_fd(link); 6066 if (fd < 0) 6067 bpf_link_put_direct(link); 6068 6069 return fd; 6070 } 6071 6072 DEFINE_MUTEX(bpf_stats_enabled_mutex); 6073 6074 static int bpf_stats_release(struct inode *inode, struct file *file) 6075 { 6076 mutex_lock(&bpf_stats_enabled_mutex); 6077 static_key_slow_dec(&bpf_stats_enabled_key.key); 6078 mutex_unlock(&bpf_stats_enabled_mutex); 6079 return 0; 6080 } 6081 6082 static const struct file_operations bpf_stats_fops = { 6083 .release = bpf_stats_release, 6084 }; 6085 6086 static int bpf_enable_runtime_stats(void) 6087 { 6088 int fd; 6089 6090 mutex_lock(&bpf_stats_enabled_mutex); 6091 6092 /* Set a very high limit to avoid overflow */ 6093 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) { 6094 mutex_unlock(&bpf_stats_enabled_mutex); 6095 return -EBUSY; 6096 } 6097 6098 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC); 6099 if (fd >= 0) 6100 static_key_slow_inc(&bpf_stats_enabled_key.key); 6101 6102 mutex_unlock(&bpf_stats_enabled_mutex); 6103 return fd; 6104 } 6105 6106 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type 6107 6108 static int bpf_enable_stats(union bpf_attr *attr) 6109 { 6110 6111 if (CHECK_ATTR(BPF_ENABLE_STATS)) 6112 return -EINVAL; 6113 6114 if (!capable(CAP_SYS_ADMIN)) 6115 return -EPERM; 6116 6117 switch (attr->enable_stats.type) { 6118 case BPF_STATS_RUN_TIME: 6119 return bpf_enable_runtime_stats(); 6120 default: 6121 break; 6122 } 6123 return -EINVAL; 6124 } 6125 6126 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags 6127 6128 static int bpf_iter_create(union bpf_attr *attr) 6129 { 6130 struct bpf_link *link; 6131 int err; 6132 6133 if (CHECK_ATTR(BPF_ITER_CREATE)) 6134 return -EINVAL; 6135 6136 if (attr->iter_create.flags) 6137 return -EINVAL; 6138 6139 link = bpf_link_get_from_fd(attr->iter_create.link_fd); 6140 if (IS_ERR(link)) 6141 return PTR_ERR(link); 6142 6143 err = bpf_iter_new_fd(link); 6144 bpf_link_put_direct(link); 6145 6146 return err; 6147 } 6148 6149 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags 6150 6151 static int bpf_prog_bind_map(union bpf_attr *attr) 6152 { 6153 struct bpf_prog *prog; 6154 struct bpf_map *map; 6155 struct bpf_map **used_maps_old, **used_maps_new; 6156 int i, ret = 0; 6157 6158 if (CHECK_ATTR(BPF_PROG_BIND_MAP)) 6159 return -EINVAL; 6160 6161 if (attr->prog_bind_map.flags) 6162 return -EINVAL; 6163 6164 prog = bpf_prog_get(attr->prog_bind_map.prog_fd); 6165 if (IS_ERR(prog)) 6166 return PTR_ERR(prog); 6167 6168 map = bpf_map_get(attr->prog_bind_map.map_fd); 6169 if (IS_ERR(map)) { 6170 ret = PTR_ERR(map); 6171 goto out_prog_put; 6172 } 6173 6174 mutex_lock(&prog->aux->used_maps_mutex); 6175 6176 used_maps_old = prog->aux->used_maps; 6177 6178 for (i = 0; i < prog->aux->used_map_cnt; i++) 6179 if (used_maps_old[i] == map) { 6180 bpf_map_put(map); 6181 goto out_unlock; 6182 } 6183 6184 used_maps_new = kmalloc_objs(used_maps_new[0], 6185 prog->aux->used_map_cnt + 1); 6186 if (!used_maps_new) { 6187 ret = -ENOMEM; 6188 goto out_unlock; 6189 } 6190 6191 /* The bpf program will not access the bpf map, but for the sake of 6192 * simplicity, increase sleepable_refcnt for sleepable program as well. 6193 */ 6194 if (prog->sleepable) 6195 atomic64_inc(&map->sleepable_refcnt); 6196 memcpy(used_maps_new, used_maps_old, 6197 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt); 6198 used_maps_new[prog->aux->used_map_cnt] = map; 6199 6200 prog->aux->used_map_cnt++; 6201 prog->aux->used_maps = used_maps_new; 6202 6203 kfree(used_maps_old); 6204 6205 out_unlock: 6206 mutex_unlock(&prog->aux->used_maps_mutex); 6207 6208 if (ret) 6209 bpf_map_put(map); 6210 out_prog_put: 6211 bpf_prog_put(prog); 6212 return ret; 6213 } 6214 6215 #define BPF_TOKEN_CREATE_LAST_FIELD token_create.bpffs_fd 6216 6217 static int token_create(union bpf_attr *attr) 6218 { 6219 if (CHECK_ATTR(BPF_TOKEN_CREATE)) 6220 return -EINVAL; 6221 6222 /* no flags are supported yet */ 6223 if (attr->token_create.flags) 6224 return -EINVAL; 6225 6226 return bpf_token_create(attr); 6227 } 6228 6229 #define BPF_PROG_STREAM_READ_BY_FD_LAST_FIELD prog_stream_read.prog_fd 6230 6231 static int prog_stream_read(union bpf_attr *attr) 6232 { 6233 char __user *buf = u64_to_user_ptr(attr->prog_stream_read.stream_buf); 6234 u32 len = attr->prog_stream_read.stream_buf_len; 6235 struct bpf_prog *prog; 6236 int ret; 6237 6238 if (CHECK_ATTR(BPF_PROG_STREAM_READ_BY_FD)) 6239 return -EINVAL; 6240 6241 prog = bpf_prog_get(attr->prog_stream_read.prog_fd); 6242 if (IS_ERR(prog)) 6243 return PTR_ERR(prog); 6244 6245 ret = bpf_prog_stream_read(prog, attr->prog_stream_read.stream_id, buf, len); 6246 bpf_prog_put(prog); 6247 6248 return ret; 6249 } 6250 6251 #define BPF_PROG_ASSOC_STRUCT_OPS_LAST_FIELD prog_assoc_struct_ops.flags 6252 6253 static int prog_assoc_struct_ops(union bpf_attr *attr) 6254 { 6255 struct bpf_prog *prog; 6256 struct bpf_map *map; 6257 int ret; 6258 6259 if (CHECK_ATTR(BPF_PROG_ASSOC_STRUCT_OPS)) 6260 return -EINVAL; 6261 6262 if (attr->prog_assoc_struct_ops.flags) 6263 return -EINVAL; 6264 6265 prog = bpf_prog_get(attr->prog_assoc_struct_ops.prog_fd); 6266 if (IS_ERR(prog)) 6267 return PTR_ERR(prog); 6268 6269 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) { 6270 ret = -EINVAL; 6271 goto put_prog; 6272 } 6273 6274 map = bpf_map_get(attr->prog_assoc_struct_ops.map_fd); 6275 if (IS_ERR(map)) { 6276 ret = PTR_ERR(map); 6277 goto put_prog; 6278 } 6279 6280 if (map->map_type != BPF_MAP_TYPE_STRUCT_OPS) { 6281 ret = -EINVAL; 6282 goto put_map; 6283 } 6284 6285 ret = bpf_prog_assoc_struct_ops(prog, map); 6286 6287 put_map: 6288 bpf_map_put(map); 6289 put_prog: 6290 bpf_prog_put(prog); 6291 return ret; 6292 } 6293 6294 static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, 6295 bpfptr_t uattr_common, unsigned int size_common) 6296 { 6297 struct bpf_common_attr attr_common; 6298 u32 offsetof_log_true_size = 0; 6299 struct bpf_log_attr attr_log; 6300 union bpf_attr attr; 6301 int err; 6302 6303 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); 6304 if (err) 6305 return err; 6306 size = min_t(u32, size, sizeof(attr)); 6307 6308 /* copy attributes from user space, may be less than sizeof(bpf_attr) */ 6309 memset(&attr, 0, sizeof(attr)); 6310 if (copy_from_bpfptr(&attr, uattr, size) != 0) 6311 return -EFAULT; 6312 6313 memset(&attr_common, 0, sizeof(attr_common)); 6314 if (cmd & BPF_COMMON_ATTRS) { 6315 err = bpf_check_uarg_tail_zero(uattr_common, 6316 offsetofend(struct bpf_common_attr, log_true_size), 6317 size_common); 6318 if (err) 6319 return err; 6320 6321 cmd &= ~BPF_COMMON_ATTRS; 6322 size_common = min_t(u32, size_common, sizeof(attr_common)); 6323 if (copy_from_bpfptr(&attr_common, uattr_common, size_common) != 0) 6324 return -EFAULT; 6325 } else { 6326 size_common = 0; 6327 } 6328 6329 err = security_bpf(cmd, &attr, size, uattr.is_kernel); 6330 if (err < 0) 6331 return err; 6332 6333 switch (cmd) { 6334 case BPF_MAP_CREATE: 6335 err = map_create(&attr, uattr, &attr_common, uattr_common, size_common); 6336 break; 6337 case BPF_MAP_LOOKUP_ELEM: 6338 err = map_lookup_elem(&attr); 6339 break; 6340 case BPF_MAP_UPDATE_ELEM: 6341 err = map_update_elem(&attr, uattr); 6342 break; 6343 case BPF_MAP_DELETE_ELEM: 6344 err = map_delete_elem(&attr, uattr); 6345 break; 6346 case BPF_MAP_GET_NEXT_KEY: 6347 err = map_get_next_key(&attr); 6348 break; 6349 case BPF_MAP_FREEZE: 6350 err = map_freeze(&attr); 6351 break; 6352 case BPF_PROG_LOAD: 6353 if (size >= offsetofend(union bpf_attr, log_true_size)) 6354 offsetof_log_true_size = offsetof(union bpf_attr, log_true_size); 6355 err = bpf_log_attr_init(&attr_log, attr.log_buf, attr.log_size, attr.log_level, 6356 offsetof_log_true_size, uattr, &attr_common, uattr_common, 6357 size_common); 6358 err = err ?: bpf_prog_load(&attr, uattr, &attr_log); 6359 break; 6360 case BPF_OBJ_PIN: 6361 err = bpf_obj_pin(&attr); 6362 break; 6363 case BPF_OBJ_GET: 6364 err = bpf_obj_get(&attr); 6365 break; 6366 case BPF_PROG_ATTACH: 6367 err = bpf_prog_attach(&attr); 6368 break; 6369 case BPF_PROG_DETACH: 6370 err = bpf_prog_detach(&attr); 6371 break; 6372 case BPF_PROG_QUERY: 6373 err = bpf_prog_query(&attr, uattr.user, size); 6374 break; 6375 case BPF_PROG_TEST_RUN: 6376 err = bpf_prog_test_run(&attr, uattr.user); 6377 break; 6378 case BPF_PROG_GET_NEXT_ID: 6379 err = bpf_obj_get_next_id(&attr, uattr.user, 6380 &prog_idr, &prog_idr_lock); 6381 break; 6382 case BPF_MAP_GET_NEXT_ID: 6383 err = bpf_obj_get_next_id(&attr, uattr.user, 6384 &map_idr, &map_idr_lock); 6385 break; 6386 case BPF_BTF_GET_NEXT_ID: 6387 err = bpf_obj_get_next_id(&attr, uattr.user, 6388 &btf_idr, &btf_idr_lock); 6389 break; 6390 case BPF_PROG_GET_FD_BY_ID: 6391 err = bpf_prog_get_fd_by_id(&attr); 6392 break; 6393 case BPF_MAP_GET_FD_BY_ID: 6394 err = bpf_map_get_fd_by_id(&attr); 6395 break; 6396 case BPF_OBJ_GET_INFO_BY_FD: 6397 err = bpf_obj_get_info_by_fd(&attr, uattr.user); 6398 break; 6399 case BPF_RAW_TRACEPOINT_OPEN: 6400 err = bpf_raw_tracepoint_open(&attr); 6401 break; 6402 case BPF_BTF_LOAD: 6403 if (size >= offsetofend(union bpf_attr, btf_log_true_size)) 6404 offsetof_log_true_size = offsetof(union bpf_attr, btf_log_true_size); 6405 err = bpf_log_attr_init(&attr_log, attr.btf_log_buf, attr.btf_log_size, 6406 attr.btf_log_level, offsetof_log_true_size, uattr, 6407 &attr_common, uattr_common, size_common); 6408 err = err ?: bpf_btf_load(&attr, uattr, &attr_log); 6409 break; 6410 case BPF_BTF_GET_FD_BY_ID: 6411 err = bpf_btf_get_fd_by_id(&attr); 6412 break; 6413 case BPF_TASK_FD_QUERY: 6414 err = bpf_task_fd_query(&attr, uattr.user); 6415 break; 6416 case BPF_MAP_LOOKUP_AND_DELETE_ELEM: 6417 err = map_lookup_and_delete_elem(&attr); 6418 break; 6419 case BPF_MAP_LOOKUP_BATCH: 6420 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH); 6421 break; 6422 case BPF_MAP_LOOKUP_AND_DELETE_BATCH: 6423 err = bpf_map_do_batch(&attr, uattr.user, 6424 BPF_MAP_LOOKUP_AND_DELETE_BATCH); 6425 break; 6426 case BPF_MAP_UPDATE_BATCH: 6427 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH); 6428 break; 6429 case BPF_MAP_DELETE_BATCH: 6430 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH); 6431 break; 6432 case BPF_LINK_CREATE: 6433 err = link_create(&attr, uattr); 6434 break; 6435 case BPF_LINK_UPDATE: 6436 err = link_update(&attr); 6437 break; 6438 case BPF_LINK_GET_FD_BY_ID: 6439 err = bpf_link_get_fd_by_id(&attr); 6440 break; 6441 case BPF_LINK_GET_NEXT_ID: 6442 err = bpf_obj_get_next_id(&attr, uattr.user, 6443 &link_idr, &link_idr_lock); 6444 break; 6445 case BPF_ENABLE_STATS: 6446 err = bpf_enable_stats(&attr); 6447 break; 6448 case BPF_ITER_CREATE: 6449 err = bpf_iter_create(&attr); 6450 break; 6451 case BPF_LINK_DETACH: 6452 err = link_detach(&attr); 6453 break; 6454 case BPF_PROG_BIND_MAP: 6455 err = bpf_prog_bind_map(&attr); 6456 break; 6457 case BPF_TOKEN_CREATE: 6458 err = token_create(&attr); 6459 break; 6460 case BPF_PROG_STREAM_READ_BY_FD: 6461 err = prog_stream_read(&attr); 6462 break; 6463 case BPF_PROG_ASSOC_STRUCT_OPS: 6464 err = prog_assoc_struct_ops(&attr); 6465 break; 6466 default: 6467 err = -EINVAL; 6468 break; 6469 } 6470 6471 return err; 6472 } 6473 6474 SYSCALL_DEFINE5(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size, 6475 struct bpf_common_attr __user *, uattr_common, unsigned int, size_common) 6476 { 6477 return __sys_bpf(cmd, USER_BPFPTR(uattr), size, USER_BPFPTR(uattr_common), size_common); 6478 } 6479 6480 static bool syscall_prog_is_valid_access(int off, int size, 6481 enum bpf_access_type type, 6482 const struct bpf_prog *prog, 6483 struct bpf_insn_access_aux *info) 6484 { 6485 if (off < 0 || off >= U16_MAX) 6486 return false; 6487 /* No alignment requirements for syscall ctx accesses. */ 6488 return true; 6489 } 6490 6491 BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size) 6492 { 6493 switch (cmd) { 6494 case BPF_MAP_CREATE: 6495 case BPF_MAP_DELETE_ELEM: 6496 case BPF_MAP_UPDATE_ELEM: 6497 case BPF_MAP_FREEZE: 6498 case BPF_MAP_GET_FD_BY_ID: 6499 case BPF_PROG_LOAD: 6500 case BPF_BTF_LOAD: 6501 case BPF_LINK_CREATE: 6502 case BPF_RAW_TRACEPOINT_OPEN: 6503 break; 6504 default: 6505 return -EINVAL; 6506 } 6507 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size, KERNEL_BPFPTR(NULL), 0); 6508 } 6509 6510 6511 /* To shut up -Wmissing-prototypes. 6512 * This function is used by the kernel light skeleton 6513 * to load bpf programs when modules are loaded or during kernel boot. 6514 * See tools/lib/bpf/skel_internal.h 6515 */ 6516 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size); 6517 6518 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size) 6519 { 6520 struct bpf_prog * __maybe_unused prog; 6521 struct bpf_tramp_run_ctx __maybe_unused run_ctx; 6522 6523 switch (cmd) { 6524 #ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */ 6525 case BPF_PROG_TEST_RUN: 6526 if (attr->test.data_in || attr->test.data_out || 6527 attr->test.ctx_out || attr->test.duration || 6528 attr->test.repeat || attr->test.flags) 6529 return -EINVAL; 6530 6531 prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL); 6532 if (IS_ERR(prog)) 6533 return PTR_ERR(prog); 6534 6535 if (attr->test.ctx_size_in < prog->aux->max_ctx_offset || 6536 attr->test.ctx_size_in > U16_MAX) { 6537 bpf_prog_put(prog); 6538 return -EINVAL; 6539 } 6540 6541 run_ctx.bpf_cookie = 0; 6542 if (!__bpf_prog_enter_sleepable_recur(prog, &run_ctx)) { 6543 /* recursion detected */ 6544 __bpf_prog_exit_sleepable_recur(prog, 0, &run_ctx); 6545 bpf_prog_put(prog); 6546 return -EBUSY; 6547 } 6548 attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in); 6549 __bpf_prog_exit_sleepable_recur(prog, 0 /* bpf_prog_run does runtime stats */, 6550 &run_ctx); 6551 bpf_prog_put(prog); 6552 return 0; 6553 #endif 6554 default: 6555 return ____bpf_sys_bpf(cmd, attr, size); 6556 } 6557 } 6558 EXPORT_SYMBOL_NS(kern_sys_bpf, "BPF_INTERNAL"); 6559 6560 static const struct bpf_func_proto bpf_sys_bpf_proto = { 6561 .func = bpf_sys_bpf, 6562 .gpl_only = false, 6563 .ret_type = RET_INTEGER, 6564 .arg1_type = ARG_ANYTHING, 6565 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6566 .arg3_type = ARG_MEM_SIZE, 6567 }; 6568 6569 const struct bpf_func_proto * __weak 6570 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6571 { 6572 return bpf_base_func_proto(func_id, prog); 6573 } 6574 6575 BPF_CALL_1(bpf_sys_close, u32, fd) 6576 { 6577 /* When bpf program calls this helper there should not be 6578 * an fdget() without matching completed fdput(). 6579 * This helper is allowed in the following callchain only: 6580 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close 6581 */ 6582 return close_fd(fd); 6583 } 6584 6585 static const struct bpf_func_proto bpf_sys_close_proto = { 6586 .func = bpf_sys_close, 6587 .gpl_only = false, 6588 .ret_type = RET_INTEGER, 6589 .arg1_type = ARG_ANYTHING, 6590 }; 6591 6592 BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res) 6593 { 6594 *res = 0; 6595 if (flags) 6596 return -EINVAL; 6597 6598 if (name_sz <= 1 || name[name_sz - 1]) 6599 return -EINVAL; 6600 6601 if (!bpf_dump_raw_ok(current_cred())) 6602 return -EPERM; 6603 6604 *res = kallsyms_lookup_name(name); 6605 return *res ? 0 : -ENOENT; 6606 } 6607 6608 static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { 6609 .func = bpf_kallsyms_lookup_name, 6610 .gpl_only = false, 6611 .ret_type = RET_INTEGER, 6612 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6613 .arg2_type = ARG_MEM_SIZE_OR_ZERO, 6614 .arg3_type = ARG_ANYTHING, 6615 .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED, 6616 .arg4_size = sizeof(u64), 6617 }; 6618 6619 static const struct bpf_func_proto * 6620 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6621 { 6622 switch (func_id) { 6623 case BPF_FUNC_sys_bpf: 6624 return !bpf_token_capable(prog->aux->token, CAP_PERFMON) 6625 ? NULL : &bpf_sys_bpf_proto; 6626 case BPF_FUNC_btf_find_by_name_kind: 6627 return &bpf_btf_find_by_name_kind_proto; 6628 case BPF_FUNC_sys_close: 6629 return &bpf_sys_close_proto; 6630 case BPF_FUNC_kallsyms_lookup_name: 6631 return &bpf_kallsyms_lookup_name_proto; 6632 default: 6633 return tracing_prog_func_proto(func_id, prog); 6634 } 6635 } 6636 6637 const struct bpf_verifier_ops bpf_syscall_verifier_ops = { 6638 .get_func_proto = syscall_prog_func_proto, 6639 .is_valid_access = syscall_prog_is_valid_access, 6640 }; 6641 6642 const struct bpf_prog_ops bpf_syscall_prog_ops = { 6643 .test_run = bpf_prog_test_run_syscall, 6644 }; 6645 6646 #ifdef CONFIG_SYSCTL 6647 static int bpf_stats_handler(const struct ctl_table *table, int write, 6648 void *buffer, size_t *lenp, loff_t *ppos) 6649 { 6650 struct static_key *key = (struct static_key *)table->data; 6651 static int saved_val; 6652 int val, ret; 6653 struct ctl_table tmp = { 6654 .data = &val, 6655 .maxlen = sizeof(val), 6656 .mode = table->mode, 6657 .extra1 = SYSCTL_ZERO, 6658 .extra2 = SYSCTL_ONE, 6659 }; 6660 6661 if (write && !capable(CAP_SYS_ADMIN)) 6662 return -EPERM; 6663 6664 mutex_lock(&bpf_stats_enabled_mutex); 6665 val = saved_val; 6666 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 6667 if (write && !ret && val != saved_val) { 6668 if (val) 6669 static_key_slow_inc(key); 6670 else 6671 static_key_slow_dec(key); 6672 saved_val = val; 6673 } 6674 mutex_unlock(&bpf_stats_enabled_mutex); 6675 return ret; 6676 } 6677 6678 void __weak unpriv_ebpf_notify(int new_state) 6679 { 6680 } 6681 6682 static int bpf_unpriv_handler(const struct ctl_table *table, int write, 6683 void *buffer, size_t *lenp, loff_t *ppos) 6684 { 6685 int ret, unpriv_enable = *(int *)table->data; 6686 bool locked_state = unpriv_enable == 1; 6687 struct ctl_table tmp = *table; 6688 6689 if (write && !capable(CAP_SYS_ADMIN)) 6690 return -EPERM; 6691 6692 tmp.data = &unpriv_enable; 6693 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 6694 if (write && !ret) { 6695 if (locked_state && unpriv_enable != 1) 6696 return -EPERM; 6697 *(int *)table->data = unpriv_enable; 6698 } 6699 6700 if (write) 6701 unpriv_ebpf_notify(unpriv_enable); 6702 6703 return ret; 6704 } 6705 6706 static const struct ctl_table bpf_syscall_table[] = { 6707 { 6708 .procname = "unprivileged_bpf_disabled", 6709 .data = &sysctl_unprivileged_bpf_disabled, 6710 .maxlen = sizeof(sysctl_unprivileged_bpf_disabled), 6711 .mode = 0644, 6712 .proc_handler = bpf_unpriv_handler, 6713 .extra1 = SYSCTL_ZERO, 6714 .extra2 = SYSCTL_TWO, 6715 }, 6716 { 6717 .procname = "bpf_stats_enabled", 6718 .data = &bpf_stats_enabled_key.key, 6719 .mode = 0644, 6720 .proc_handler = bpf_stats_handler, 6721 }, 6722 }; 6723 6724 static int __init bpf_syscall_sysctl_init(void) 6725 { 6726 register_sysctl_init("kernel", bpf_syscall_table); 6727 return 0; 6728 } 6729 late_initcall(bpf_syscall_sysctl_init); 6730 #endif /* CONFIG_SYSCTL */ 6731