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