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