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