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