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