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