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