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