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