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