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/btf.h> 6 #include <linux/bpf-cgroup.h> 7 #include <linux/cgroup.h> 8 #include <linux/rcupdate.h> 9 #include <linux/random.h> 10 #include <linux/smp.h> 11 #include <linux/topology.h> 12 #include <linux/ktime.h> 13 #include <linux/sched.h> 14 #include <linux/uidgid.h> 15 #include <linux/filter.h> 16 #include <linux/ctype.h> 17 #include <linux/jiffies.h> 18 #include <linux/pid_namespace.h> 19 #include <linux/poison.h> 20 #include <linux/proc_ns.h> 21 #include <linux/sched/task.h> 22 #include <linux/security.h> 23 #include <linux/btf_ids.h> 24 #include <linux/bpf_mem_alloc.h> 25 #include <linux/kasan.h> 26 #include <linux/bpf_verifier.h> 27 #include <linux/uaccess.h> 28 29 #include "../../lib/kstrtox.h" 30 31 /* If kernel subsystem is allowing eBPF programs to call this function, 32 * inside its own verifier_ops->get_func_proto() callback it should return 33 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments 34 * 35 * Different map implementations will rely on rcu in map methods 36 * lookup/update/delete, therefore eBPF programs must run under rcu lock 37 * if program is allowed to access maps, so check rcu_read_lock_held() or 38 * rcu_read_lock_trace_held() in all three functions. 39 */ 40 BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key) 41 { 42 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() && 43 !rcu_read_lock_bh_held()); 44 return (unsigned long) map->ops->map_lookup_elem(map, key); 45 } 46 47 const struct bpf_func_proto bpf_map_lookup_elem_proto = { 48 .func = bpf_map_lookup_elem, 49 .gpl_only = false, 50 .pkt_access = true, 51 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, 52 .arg1_type = ARG_CONST_MAP_PTR, 53 .arg2_type = ARG_PTR_TO_MAP_KEY, 54 }; 55 56 BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key, 57 void *, value, u64, flags) 58 { 59 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() && 60 !rcu_read_lock_bh_held()); 61 return map->ops->map_update_elem(map, key, value, flags); 62 } 63 64 const struct bpf_func_proto bpf_map_update_elem_proto = { 65 .func = bpf_map_update_elem, 66 .gpl_only = false, 67 .pkt_access = true, 68 .ret_type = RET_INTEGER, 69 .arg1_type = ARG_CONST_MAP_PTR, 70 .arg2_type = ARG_PTR_TO_MAP_KEY, 71 .arg3_type = ARG_PTR_TO_MAP_VALUE, 72 .arg4_type = ARG_ANYTHING, 73 }; 74 75 BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key) 76 { 77 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() && 78 !rcu_read_lock_bh_held()); 79 return map->ops->map_delete_elem(map, key); 80 } 81 82 const struct bpf_func_proto bpf_map_delete_elem_proto = { 83 .func = bpf_map_delete_elem, 84 .gpl_only = false, 85 .pkt_access = true, 86 .ret_type = RET_INTEGER, 87 .arg1_type = ARG_CONST_MAP_PTR, 88 .arg2_type = ARG_PTR_TO_MAP_KEY, 89 }; 90 91 BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags) 92 { 93 return map->ops->map_push_elem(map, value, flags); 94 } 95 96 const struct bpf_func_proto bpf_map_push_elem_proto = { 97 .func = bpf_map_push_elem, 98 .gpl_only = false, 99 .pkt_access = true, 100 .ret_type = RET_INTEGER, 101 .arg1_type = ARG_CONST_MAP_PTR, 102 .arg2_type = ARG_PTR_TO_MAP_VALUE, 103 .arg3_type = ARG_ANYTHING, 104 }; 105 106 BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value) 107 { 108 return map->ops->map_pop_elem(map, value); 109 } 110 111 const struct bpf_func_proto bpf_map_pop_elem_proto = { 112 .func = bpf_map_pop_elem, 113 .gpl_only = false, 114 .ret_type = RET_INTEGER, 115 .arg1_type = ARG_CONST_MAP_PTR, 116 .arg2_type = ARG_PTR_TO_MAP_VALUE | MEM_UNINIT | MEM_WRITE, 117 }; 118 119 BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value) 120 { 121 return map->ops->map_peek_elem(map, value); 122 } 123 124 const struct bpf_func_proto bpf_map_peek_elem_proto = { 125 .func = bpf_map_peek_elem, 126 .gpl_only = false, 127 .ret_type = RET_INTEGER, 128 .arg1_type = ARG_CONST_MAP_PTR, 129 .arg2_type = ARG_PTR_TO_MAP_VALUE | MEM_UNINIT | MEM_WRITE, 130 }; 131 132 BPF_CALL_3(bpf_map_lookup_percpu_elem, struct bpf_map *, map, void *, key, u32, cpu) 133 { 134 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() && 135 !rcu_read_lock_bh_held()); 136 return (unsigned long) map->ops->map_lookup_percpu_elem(map, key, cpu); 137 } 138 139 const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto = { 140 .func = bpf_map_lookup_percpu_elem, 141 .gpl_only = false, 142 .pkt_access = true, 143 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, 144 .arg1_type = ARG_CONST_MAP_PTR, 145 .arg2_type = ARG_PTR_TO_MAP_KEY, 146 .arg3_type = ARG_ANYTHING, 147 }; 148 149 const struct bpf_func_proto bpf_get_prandom_u32_proto = { 150 .func = bpf_user_rnd_u32, 151 .gpl_only = false, 152 .ret_type = RET_INTEGER, 153 }; 154 155 BPF_CALL_0(bpf_get_smp_processor_id) 156 { 157 return smp_processor_id(); 158 } 159 160 const struct bpf_func_proto bpf_get_smp_processor_id_proto = { 161 .func = bpf_get_smp_processor_id, 162 .gpl_only = false, 163 .ret_type = RET_INTEGER, 164 .allow_fastcall = true, 165 }; 166 167 BPF_CALL_0(bpf_get_numa_node_id) 168 { 169 return numa_node_id(); 170 } 171 172 const struct bpf_func_proto bpf_get_numa_node_id_proto = { 173 .func = bpf_get_numa_node_id, 174 .gpl_only = false, 175 .ret_type = RET_INTEGER, 176 }; 177 178 BPF_CALL_0(bpf_ktime_get_ns) 179 { 180 /* NMI safe access to clock monotonic */ 181 return ktime_get_mono_fast_ns(); 182 } 183 184 const struct bpf_func_proto bpf_ktime_get_ns_proto = { 185 .func = bpf_ktime_get_ns, 186 .gpl_only = false, 187 .ret_type = RET_INTEGER, 188 }; 189 190 BPF_CALL_0(bpf_ktime_get_boot_ns) 191 { 192 /* NMI safe access to clock boottime */ 193 return ktime_get_boot_fast_ns(); 194 } 195 196 const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = { 197 .func = bpf_ktime_get_boot_ns, 198 .gpl_only = false, 199 .ret_type = RET_INTEGER, 200 }; 201 202 BPF_CALL_0(bpf_ktime_get_coarse_ns) 203 { 204 return ktime_get_coarse_ns(); 205 } 206 207 const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto = { 208 .func = bpf_ktime_get_coarse_ns, 209 .gpl_only = false, 210 .ret_type = RET_INTEGER, 211 }; 212 213 BPF_CALL_0(bpf_ktime_get_tai_ns) 214 { 215 /* NMI safe access to clock tai */ 216 return ktime_get_tai_fast_ns(); 217 } 218 219 const struct bpf_func_proto bpf_ktime_get_tai_ns_proto = { 220 .func = bpf_ktime_get_tai_ns, 221 .gpl_only = false, 222 .ret_type = RET_INTEGER, 223 }; 224 225 BPF_CALL_0(bpf_get_current_pid_tgid) 226 { 227 struct task_struct *task = current; 228 229 if (unlikely(!task)) 230 return -EINVAL; 231 232 return (u64) task->tgid << 32 | task->pid; 233 } 234 235 const struct bpf_func_proto bpf_get_current_pid_tgid_proto = { 236 .func = bpf_get_current_pid_tgid, 237 .gpl_only = false, 238 .ret_type = RET_INTEGER, 239 }; 240 241 BPF_CALL_0(bpf_get_current_uid_gid) 242 { 243 struct task_struct *task = current; 244 kuid_t uid; 245 kgid_t gid; 246 247 if (unlikely(!task)) 248 return -EINVAL; 249 250 current_uid_gid(&uid, &gid); 251 return (u64) from_kgid(&init_user_ns, gid) << 32 | 252 from_kuid(&init_user_ns, uid); 253 } 254 255 const struct bpf_func_proto bpf_get_current_uid_gid_proto = { 256 .func = bpf_get_current_uid_gid, 257 .gpl_only = false, 258 .ret_type = RET_INTEGER, 259 }; 260 261 BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size) 262 { 263 struct task_struct *task = current; 264 265 if (unlikely(!task)) 266 goto err_clear; 267 268 /* Verifier guarantees that size > 0 */ 269 strscpy_pad(buf, task->comm, size); 270 return 0; 271 err_clear: 272 memset(buf, 0, size); 273 return -EINVAL; 274 } 275 276 const struct bpf_func_proto bpf_get_current_comm_proto = { 277 .func = bpf_get_current_comm, 278 .gpl_only = false, 279 .ret_type = RET_INTEGER, 280 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 281 .arg2_type = ARG_CONST_SIZE, 282 }; 283 284 #if defined(CONFIG_QUEUED_SPINLOCKS) || defined(CONFIG_BPF_ARCH_SPINLOCK) 285 286 static inline void __bpf_spin_lock(struct bpf_spin_lock *lock) 287 { 288 arch_spinlock_t *l = (void *)lock; 289 union { 290 __u32 val; 291 arch_spinlock_t lock; 292 } u = { .lock = __ARCH_SPIN_LOCK_UNLOCKED }; 293 294 compiletime_assert(u.val == 0, "__ARCH_SPIN_LOCK_UNLOCKED not 0"); 295 BUILD_BUG_ON(sizeof(*l) != sizeof(__u32)); 296 BUILD_BUG_ON(sizeof(*lock) != sizeof(__u32)); 297 preempt_disable(); 298 arch_spin_lock(l); 299 } 300 301 static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock) 302 { 303 arch_spinlock_t *l = (void *)lock; 304 305 arch_spin_unlock(l); 306 preempt_enable(); 307 } 308 309 #else 310 311 static inline void __bpf_spin_lock(struct bpf_spin_lock *lock) 312 { 313 atomic_t *l = (void *)lock; 314 315 BUILD_BUG_ON(sizeof(*l) != sizeof(*lock)); 316 do { 317 atomic_cond_read_relaxed(l, !VAL); 318 } while (atomic_xchg(l, 1)); 319 } 320 321 static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock) 322 { 323 atomic_t *l = (void *)lock; 324 325 atomic_set_release(l, 0); 326 } 327 328 #endif 329 330 static DEFINE_PER_CPU(unsigned long, irqsave_flags); 331 332 static inline void __bpf_spin_lock_irqsave(struct bpf_spin_lock *lock) 333 { 334 unsigned long flags; 335 336 local_irq_save(flags); 337 __bpf_spin_lock(lock); 338 __this_cpu_write(irqsave_flags, flags); 339 } 340 341 NOTRACE_BPF_CALL_1(bpf_spin_lock, struct bpf_spin_lock *, lock) 342 { 343 __bpf_spin_lock_irqsave(lock); 344 return 0; 345 } 346 347 const struct bpf_func_proto bpf_spin_lock_proto = { 348 .func = bpf_spin_lock, 349 .gpl_only = false, 350 .ret_type = RET_VOID, 351 .arg1_type = ARG_PTR_TO_SPIN_LOCK, 352 .arg1_btf_id = BPF_PTR_POISON, 353 }; 354 355 static inline void __bpf_spin_unlock_irqrestore(struct bpf_spin_lock *lock) 356 { 357 unsigned long flags; 358 359 flags = __this_cpu_read(irqsave_flags); 360 __bpf_spin_unlock(lock); 361 local_irq_restore(flags); 362 } 363 364 NOTRACE_BPF_CALL_1(bpf_spin_unlock, struct bpf_spin_lock *, lock) 365 { 366 __bpf_spin_unlock_irqrestore(lock); 367 return 0; 368 } 369 370 const struct bpf_func_proto bpf_spin_unlock_proto = { 371 .func = bpf_spin_unlock, 372 .gpl_only = false, 373 .ret_type = RET_VOID, 374 .arg1_type = ARG_PTR_TO_SPIN_LOCK, 375 .arg1_btf_id = BPF_PTR_POISON, 376 }; 377 378 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src, 379 bool lock_src) 380 { 381 struct bpf_spin_lock *lock; 382 383 if (lock_src) 384 lock = src + map->record->spin_lock_off; 385 else 386 lock = dst + map->record->spin_lock_off; 387 preempt_disable(); 388 __bpf_spin_lock_irqsave(lock); 389 copy_map_value(map, dst, src); 390 __bpf_spin_unlock_irqrestore(lock); 391 preempt_enable(); 392 } 393 394 BPF_CALL_0(bpf_jiffies64) 395 { 396 return get_jiffies_64(); 397 } 398 399 const struct bpf_func_proto bpf_jiffies64_proto = { 400 .func = bpf_jiffies64, 401 .gpl_only = false, 402 .ret_type = RET_INTEGER, 403 }; 404 405 #ifdef CONFIG_CGROUPS 406 BPF_CALL_0(bpf_get_current_cgroup_id) 407 { 408 struct cgroup *cgrp; 409 u64 cgrp_id; 410 411 rcu_read_lock(); 412 cgrp = task_dfl_cgroup(current); 413 cgrp_id = cgroup_id(cgrp); 414 rcu_read_unlock(); 415 416 return cgrp_id; 417 } 418 419 const struct bpf_func_proto bpf_get_current_cgroup_id_proto = { 420 .func = bpf_get_current_cgroup_id, 421 .gpl_only = false, 422 .ret_type = RET_INTEGER, 423 }; 424 425 BPF_CALL_1(bpf_get_current_ancestor_cgroup_id, int, ancestor_level) 426 { 427 struct cgroup *cgrp; 428 struct cgroup *ancestor; 429 u64 cgrp_id; 430 431 rcu_read_lock(); 432 cgrp = task_dfl_cgroup(current); 433 ancestor = cgroup_ancestor(cgrp, ancestor_level); 434 cgrp_id = ancestor ? cgroup_id(ancestor) : 0; 435 rcu_read_unlock(); 436 437 return cgrp_id; 438 } 439 440 const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto = { 441 .func = bpf_get_current_ancestor_cgroup_id, 442 .gpl_only = false, 443 .ret_type = RET_INTEGER, 444 .arg1_type = ARG_ANYTHING, 445 }; 446 #endif /* CONFIG_CGROUPS */ 447 448 #define BPF_STRTOX_BASE_MASK 0x1F 449 450 static int __bpf_strtoull(const char *buf, size_t buf_len, u64 flags, 451 unsigned long long *res, bool *is_negative) 452 { 453 unsigned int base = flags & BPF_STRTOX_BASE_MASK; 454 const char *cur_buf = buf; 455 size_t cur_len = buf_len; 456 unsigned int consumed; 457 size_t val_len; 458 char str[64]; 459 460 if (!buf || !buf_len || !res || !is_negative) 461 return -EINVAL; 462 463 if (base != 0 && base != 8 && base != 10 && base != 16) 464 return -EINVAL; 465 466 if (flags & ~BPF_STRTOX_BASE_MASK) 467 return -EINVAL; 468 469 while (cur_buf < buf + buf_len && isspace(*cur_buf)) 470 ++cur_buf; 471 472 *is_negative = (cur_buf < buf + buf_len && *cur_buf == '-'); 473 if (*is_negative) 474 ++cur_buf; 475 476 consumed = cur_buf - buf; 477 cur_len -= consumed; 478 if (!cur_len) 479 return -EINVAL; 480 481 cur_len = min(cur_len, sizeof(str) - 1); 482 memcpy(str, cur_buf, cur_len); 483 str[cur_len] = '\0'; 484 cur_buf = str; 485 486 cur_buf = _parse_integer_fixup_radix(cur_buf, &base); 487 val_len = _parse_integer(cur_buf, base, res); 488 489 if (val_len & KSTRTOX_OVERFLOW) 490 return -ERANGE; 491 492 if (val_len == 0) 493 return -EINVAL; 494 495 cur_buf += val_len; 496 consumed += cur_buf - str; 497 498 return consumed; 499 } 500 501 static int __bpf_strtoll(const char *buf, size_t buf_len, u64 flags, 502 long long *res) 503 { 504 unsigned long long _res; 505 bool is_negative; 506 int err; 507 508 err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative); 509 if (err < 0) 510 return err; 511 if (is_negative) { 512 if ((long long)-_res > 0) 513 return -ERANGE; 514 *res = -_res; 515 } else { 516 if ((long long)_res < 0) 517 return -ERANGE; 518 *res = _res; 519 } 520 return err; 521 } 522 523 BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags, 524 s64 *, res) 525 { 526 long long _res; 527 int err; 528 529 *res = 0; 530 err = __bpf_strtoll(buf, buf_len, flags, &_res); 531 if (err < 0) 532 return err; 533 *res = _res; 534 return err; 535 } 536 537 const struct bpf_func_proto bpf_strtol_proto = { 538 .func = bpf_strtol, 539 .gpl_only = false, 540 .ret_type = RET_INTEGER, 541 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, 542 .arg2_type = ARG_CONST_SIZE, 543 .arg3_type = ARG_ANYTHING, 544 .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED, 545 .arg4_size = sizeof(s64), 546 }; 547 548 BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags, 549 u64 *, res) 550 { 551 unsigned long long _res; 552 bool is_negative; 553 int err; 554 555 *res = 0; 556 err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative); 557 if (err < 0) 558 return err; 559 if (is_negative) 560 return -EINVAL; 561 *res = _res; 562 return err; 563 } 564 565 const struct bpf_func_proto bpf_strtoul_proto = { 566 .func = bpf_strtoul, 567 .gpl_only = false, 568 .ret_type = RET_INTEGER, 569 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, 570 .arg2_type = ARG_CONST_SIZE, 571 .arg3_type = ARG_ANYTHING, 572 .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED, 573 .arg4_size = sizeof(u64), 574 }; 575 576 BPF_CALL_3(bpf_strncmp, const char *, s1, u32, s1_sz, const char *, s2) 577 { 578 return strncmp(s1, s2, s1_sz); 579 } 580 581 static const struct bpf_func_proto bpf_strncmp_proto = { 582 .func = bpf_strncmp, 583 .gpl_only = false, 584 .ret_type = RET_INTEGER, 585 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, 586 .arg2_type = ARG_CONST_SIZE, 587 .arg3_type = ARG_PTR_TO_CONST_STR, 588 }; 589 590 BPF_CALL_4(bpf_get_ns_current_pid_tgid, u64, dev, u64, ino, 591 struct bpf_pidns_info *, nsdata, u32, size) 592 { 593 struct task_struct *task = current; 594 struct pid_namespace *pidns; 595 int err = -EINVAL; 596 597 if (unlikely(size != sizeof(struct bpf_pidns_info))) 598 goto clear; 599 600 if (unlikely((u64)(dev_t)dev != dev)) 601 goto clear; 602 603 if (unlikely(!task)) 604 goto clear; 605 606 pidns = task_active_pid_ns(task); 607 if (unlikely(!pidns)) { 608 err = -ENOENT; 609 goto clear; 610 } 611 612 if (!ns_match(&pidns->ns, (dev_t)dev, ino)) 613 goto clear; 614 615 nsdata->pid = task_pid_nr_ns(task, pidns); 616 nsdata->tgid = task_tgid_nr_ns(task, pidns); 617 return 0; 618 clear: 619 memset((void *)nsdata, 0, (size_t) size); 620 return err; 621 } 622 623 const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = { 624 .func = bpf_get_ns_current_pid_tgid, 625 .gpl_only = false, 626 .ret_type = RET_INTEGER, 627 .arg1_type = ARG_ANYTHING, 628 .arg2_type = ARG_ANYTHING, 629 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 630 .arg4_type = ARG_CONST_SIZE, 631 }; 632 633 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = { 634 .func = bpf_get_raw_cpu_id, 635 .gpl_only = false, 636 .ret_type = RET_INTEGER, 637 }; 638 639 BPF_CALL_5(bpf_event_output_data, void *, ctx, struct bpf_map *, map, 640 u64, flags, void *, data, u64, size) 641 { 642 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) 643 return -EINVAL; 644 645 return bpf_event_output(map, flags, data, size, NULL, 0, NULL); 646 } 647 648 const struct bpf_func_proto bpf_event_output_data_proto = { 649 .func = bpf_event_output_data, 650 .gpl_only = true, 651 .ret_type = RET_INTEGER, 652 .arg1_type = ARG_PTR_TO_CTX, 653 .arg2_type = ARG_CONST_MAP_PTR, 654 .arg3_type = ARG_ANYTHING, 655 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 656 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 657 }; 658 659 BPF_CALL_3(bpf_copy_from_user, void *, dst, u32, size, 660 const void __user *, user_ptr) 661 { 662 int ret = copy_from_user(dst, user_ptr, size); 663 664 if (unlikely(ret)) { 665 memset(dst, 0, size); 666 ret = -EFAULT; 667 } 668 669 return ret; 670 } 671 672 const struct bpf_func_proto bpf_copy_from_user_proto = { 673 .func = bpf_copy_from_user, 674 .gpl_only = false, 675 .might_sleep = true, 676 .ret_type = RET_INTEGER, 677 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 678 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 679 .arg3_type = ARG_ANYTHING, 680 }; 681 682 BPF_CALL_5(bpf_copy_from_user_task, void *, dst, u32, size, 683 const void __user *, user_ptr, struct task_struct *, tsk, u64, flags) 684 { 685 int ret; 686 687 /* flags is not used yet */ 688 if (unlikely(flags)) 689 return -EINVAL; 690 691 if (unlikely(!size)) 692 return 0; 693 694 ret = access_process_vm(tsk, (unsigned long)user_ptr, dst, size, 0); 695 if (ret == size) 696 return 0; 697 698 memset(dst, 0, size); 699 /* Return -EFAULT for partial read */ 700 return ret < 0 ? ret : -EFAULT; 701 } 702 703 const struct bpf_func_proto bpf_copy_from_user_task_proto = { 704 .func = bpf_copy_from_user_task, 705 .gpl_only = true, 706 .might_sleep = true, 707 .ret_type = RET_INTEGER, 708 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 709 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 710 .arg3_type = ARG_ANYTHING, 711 .arg4_type = ARG_PTR_TO_BTF_ID, 712 .arg4_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK], 713 .arg5_type = ARG_ANYTHING 714 }; 715 716 BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu) 717 { 718 if (cpu >= nr_cpu_ids) 719 return (unsigned long)NULL; 720 721 return (unsigned long)per_cpu_ptr((const void __percpu *)(const uintptr_t)ptr, cpu); 722 } 723 724 const struct bpf_func_proto bpf_per_cpu_ptr_proto = { 725 .func = bpf_per_cpu_ptr, 726 .gpl_only = false, 727 .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | PTR_MAYBE_NULL | MEM_RDONLY, 728 .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID, 729 .arg2_type = ARG_ANYTHING, 730 }; 731 732 BPF_CALL_1(bpf_this_cpu_ptr, const void *, percpu_ptr) 733 { 734 return (unsigned long)this_cpu_ptr((const void __percpu *)(const uintptr_t)percpu_ptr); 735 } 736 737 const struct bpf_func_proto bpf_this_cpu_ptr_proto = { 738 .func = bpf_this_cpu_ptr, 739 .gpl_only = false, 740 .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | MEM_RDONLY, 741 .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID, 742 }; 743 744 static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype, 745 size_t bufsz) 746 { 747 void __user *user_ptr = (__force void __user *)unsafe_ptr; 748 749 buf[0] = 0; 750 751 switch (fmt_ptype) { 752 case 's': 753 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE 754 if ((unsigned long)unsafe_ptr < TASK_SIZE) 755 return strncpy_from_user_nofault(buf, user_ptr, bufsz); 756 fallthrough; 757 #endif 758 case 'k': 759 return strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz); 760 case 'u': 761 return strncpy_from_user_nofault(buf, user_ptr, bufsz); 762 } 763 764 return -EINVAL; 765 } 766 767 /* Support executing three nested bprintf helper calls on a given CPU */ 768 #define MAX_BPRINTF_NEST_LEVEL 3 769 770 static DEFINE_PER_CPU(struct bpf_bprintf_buffers[MAX_BPRINTF_NEST_LEVEL], bpf_bprintf_bufs); 771 static DEFINE_PER_CPU(int, bpf_bprintf_nest_level); 772 773 int bpf_try_get_buffers(struct bpf_bprintf_buffers **bufs) 774 { 775 int nest_level; 776 777 preempt_disable(); 778 nest_level = this_cpu_inc_return(bpf_bprintf_nest_level); 779 if (WARN_ON_ONCE(nest_level > MAX_BPRINTF_NEST_LEVEL)) { 780 this_cpu_dec(bpf_bprintf_nest_level); 781 preempt_enable(); 782 return -EBUSY; 783 } 784 *bufs = this_cpu_ptr(&bpf_bprintf_bufs[nest_level - 1]); 785 786 return 0; 787 } 788 789 void bpf_put_buffers(void) 790 { 791 if (WARN_ON_ONCE(this_cpu_read(bpf_bprintf_nest_level) == 0)) 792 return; 793 this_cpu_dec(bpf_bprintf_nest_level); 794 preempt_enable(); 795 } 796 797 void bpf_bprintf_cleanup(struct bpf_bprintf_data *data) 798 { 799 if (!data->bin_args && !data->buf) 800 return; 801 bpf_put_buffers(); 802 } 803 804 /* 805 * bpf_bprintf_prepare - Generic pass on format strings for bprintf-like helpers 806 * 807 * Returns a negative value if fmt is an invalid format string or 0 otherwise. 808 * 809 * This can be used in two ways: 810 * - Format string verification only: when data->get_bin_args is false 811 * - Arguments preparation: in addition to the above verification, it writes in 812 * data->bin_args a binary representation of arguments usable by bstr_printf 813 * where pointers from BPF have been sanitized. 814 * 815 * In argument preparation mode, if 0 is returned, safe temporary buffers are 816 * allocated and bpf_bprintf_cleanup should be called to free them after use. 817 */ 818 int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args, 819 u32 num_args, struct bpf_bprintf_data *data) 820 { 821 bool get_buffers = (data->get_bin_args && num_args) || data->get_buf; 822 char *unsafe_ptr = NULL, *tmp_buf = NULL, *tmp_buf_end, *fmt_end; 823 struct bpf_bprintf_buffers *buffers = NULL; 824 size_t sizeof_cur_arg, sizeof_cur_ip; 825 int err, i, num_spec = 0; 826 u64 cur_arg; 827 char fmt_ptype, cur_ip[16], ip_spec[] = "%pXX"; 828 829 fmt_end = strnchr(fmt, fmt_size, 0); 830 if (!fmt_end) 831 return -EINVAL; 832 fmt_size = fmt_end - fmt; 833 834 if (get_buffers && bpf_try_get_buffers(&buffers)) 835 return -EBUSY; 836 837 if (data->get_bin_args) { 838 if (num_args) 839 tmp_buf = buffers->bin_args; 840 tmp_buf_end = tmp_buf + MAX_BPRINTF_BIN_ARGS; 841 data->bin_args = (u32 *)tmp_buf; 842 } 843 844 if (data->get_buf) 845 data->buf = buffers->buf; 846 847 for (i = 0; i < fmt_size; i++) { 848 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) { 849 err = -EINVAL; 850 goto out; 851 } 852 853 if (fmt[i] != '%') 854 continue; 855 856 if (fmt[i + 1] == '%') { 857 i++; 858 continue; 859 } 860 861 if (num_spec >= num_args) { 862 err = -EINVAL; 863 goto out; 864 } 865 866 /* The string is zero-terminated so if fmt[i] != 0, we can 867 * always access fmt[i + 1], in the worst case it will be a 0 868 */ 869 i++; 870 871 /* skip optional "[0 +-][num]" width formatting field */ 872 while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' || 873 fmt[i] == ' ') 874 i++; 875 if (fmt[i] >= '1' && fmt[i] <= '9') { 876 i++; 877 while (fmt[i] >= '0' && fmt[i] <= '9') 878 i++; 879 } 880 881 if (fmt[i] == 'p') { 882 sizeof_cur_arg = sizeof(long); 883 884 if (fmt[i + 1] == 0 || isspace(fmt[i + 1]) || 885 ispunct(fmt[i + 1])) { 886 if (tmp_buf) 887 cur_arg = raw_args[num_spec]; 888 goto nocopy_fmt; 889 } 890 891 if ((fmt[i + 1] == 'k' || fmt[i + 1] == 'u') && 892 fmt[i + 2] == 's') { 893 fmt_ptype = fmt[i + 1]; 894 i += 2; 895 goto fmt_str; 896 } 897 898 if (fmt[i + 1] == 'K' || 899 fmt[i + 1] == 'x' || fmt[i + 1] == 's' || 900 fmt[i + 1] == 'S') { 901 if (tmp_buf) 902 cur_arg = raw_args[num_spec]; 903 i++; 904 goto nocopy_fmt; 905 } 906 907 if (fmt[i + 1] == 'B') { 908 if (tmp_buf) { 909 err = snprintf(tmp_buf, 910 (tmp_buf_end - tmp_buf), 911 "%pB", 912 (void *)(long)raw_args[num_spec]); 913 tmp_buf += (err + 1); 914 } 915 916 i++; 917 num_spec++; 918 continue; 919 } 920 921 /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */ 922 if ((fmt[i + 1] != 'i' && fmt[i + 1] != 'I') || 923 (fmt[i + 2] != '4' && fmt[i + 2] != '6')) { 924 err = -EINVAL; 925 goto out; 926 } 927 928 i += 2; 929 if (!tmp_buf) 930 goto nocopy_fmt; 931 932 sizeof_cur_ip = (fmt[i] == '4') ? 4 : 16; 933 if (tmp_buf_end - tmp_buf < sizeof_cur_ip) { 934 err = -ENOSPC; 935 goto out; 936 } 937 938 unsafe_ptr = (char *)(long)raw_args[num_spec]; 939 err = copy_from_kernel_nofault(cur_ip, unsafe_ptr, 940 sizeof_cur_ip); 941 if (err < 0) 942 memset(cur_ip, 0, sizeof_cur_ip); 943 944 /* hack: bstr_printf expects IP addresses to be 945 * pre-formatted as strings, ironically, the easiest way 946 * to do that is to call snprintf. 947 */ 948 ip_spec[2] = fmt[i - 1]; 949 ip_spec[3] = fmt[i]; 950 err = snprintf(tmp_buf, tmp_buf_end - tmp_buf, 951 ip_spec, &cur_ip); 952 953 tmp_buf += err + 1; 954 num_spec++; 955 956 continue; 957 } else if (fmt[i] == 's') { 958 fmt_ptype = fmt[i]; 959 fmt_str: 960 if (fmt[i + 1] != 0 && 961 !isspace(fmt[i + 1]) && 962 !ispunct(fmt[i + 1])) { 963 err = -EINVAL; 964 goto out; 965 } 966 967 if (!tmp_buf) 968 goto nocopy_fmt; 969 970 if (tmp_buf_end == tmp_buf) { 971 err = -ENOSPC; 972 goto out; 973 } 974 975 unsafe_ptr = (char *)(long)raw_args[num_spec]; 976 err = bpf_trace_copy_string(tmp_buf, unsafe_ptr, 977 fmt_ptype, 978 tmp_buf_end - tmp_buf); 979 if (err < 0) { 980 tmp_buf[0] = '\0'; 981 err = 1; 982 } 983 984 tmp_buf += err; 985 num_spec++; 986 987 continue; 988 } else if (fmt[i] == 'c') { 989 if (!tmp_buf) 990 goto nocopy_fmt; 991 992 if (tmp_buf_end == tmp_buf) { 993 err = -ENOSPC; 994 goto out; 995 } 996 997 *tmp_buf = raw_args[num_spec]; 998 tmp_buf++; 999 num_spec++; 1000 1001 continue; 1002 } 1003 1004 sizeof_cur_arg = sizeof(int); 1005 1006 if (fmt[i] == 'l') { 1007 sizeof_cur_arg = sizeof(long); 1008 i++; 1009 } 1010 if (fmt[i] == 'l') { 1011 sizeof_cur_arg = sizeof(long long); 1012 i++; 1013 } 1014 1015 if (fmt[i] != 'i' && fmt[i] != 'd' && fmt[i] != 'u' && 1016 fmt[i] != 'x' && fmt[i] != 'X') { 1017 err = -EINVAL; 1018 goto out; 1019 } 1020 1021 if (tmp_buf) 1022 cur_arg = raw_args[num_spec]; 1023 nocopy_fmt: 1024 if (tmp_buf) { 1025 tmp_buf = PTR_ALIGN(tmp_buf, sizeof(u32)); 1026 if (tmp_buf_end - tmp_buf < sizeof_cur_arg) { 1027 err = -ENOSPC; 1028 goto out; 1029 } 1030 1031 if (sizeof_cur_arg == 8) { 1032 *(u32 *)tmp_buf = *(u32 *)&cur_arg; 1033 *(u32 *)(tmp_buf + 4) = *((u32 *)&cur_arg + 1); 1034 } else { 1035 *(u32 *)tmp_buf = (u32)(long)cur_arg; 1036 } 1037 tmp_buf += sizeof_cur_arg; 1038 } 1039 num_spec++; 1040 } 1041 1042 err = 0; 1043 out: 1044 if (err) 1045 bpf_bprintf_cleanup(data); 1046 return err; 1047 } 1048 1049 BPF_CALL_5(bpf_snprintf, char *, str, u32, str_size, char *, fmt, 1050 const void *, args, u32, data_len) 1051 { 1052 struct bpf_bprintf_data data = { 1053 .get_bin_args = true, 1054 }; 1055 int err, num_args; 1056 1057 if (data_len % 8 || data_len > MAX_BPRINTF_VARARGS * 8 || 1058 (data_len && !args)) 1059 return -EINVAL; 1060 num_args = data_len / 8; 1061 1062 /* ARG_PTR_TO_CONST_STR guarantees that fmt is zero-terminated so we 1063 * can safely give an unbounded size. 1064 */ 1065 err = bpf_bprintf_prepare(fmt, UINT_MAX, args, num_args, &data); 1066 if (err < 0) 1067 return err; 1068 1069 err = bstr_printf(str, str_size, fmt, data.bin_args); 1070 1071 bpf_bprintf_cleanup(&data); 1072 1073 return err + 1; 1074 } 1075 1076 const struct bpf_func_proto bpf_snprintf_proto = { 1077 .func = bpf_snprintf, 1078 .gpl_only = true, 1079 .ret_type = RET_INTEGER, 1080 .arg1_type = ARG_PTR_TO_MEM_OR_NULL, 1081 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 1082 .arg3_type = ARG_PTR_TO_CONST_STR, 1083 .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY, 1084 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 1085 }; 1086 1087 struct bpf_async_cb { 1088 struct bpf_map *map; 1089 struct bpf_prog *prog; 1090 void __rcu *callback_fn; 1091 void *value; 1092 union { 1093 struct rcu_head rcu; 1094 struct work_struct delete_work; 1095 }; 1096 u64 flags; 1097 }; 1098 1099 /* BPF map elements can contain 'struct bpf_timer'. 1100 * Such map owns all of its BPF timers. 1101 * 'struct bpf_timer' is allocated as part of map element allocation 1102 * and it's zero initialized. 1103 * That space is used to keep 'struct bpf_async_kern'. 1104 * bpf_timer_init() allocates 'struct bpf_hrtimer', inits hrtimer, and 1105 * remembers 'struct bpf_map *' pointer it's part of. 1106 * bpf_timer_set_callback() increments prog refcnt and assign bpf callback_fn. 1107 * bpf_timer_start() arms the timer. 1108 * If user space reference to a map goes to zero at this point 1109 * ops->map_release_uref callback is responsible for cancelling the timers, 1110 * freeing their memory, and decrementing prog's refcnts. 1111 * bpf_timer_cancel() cancels the timer and decrements prog's refcnt. 1112 * Inner maps can contain bpf timers as well. ops->map_release_uref is 1113 * freeing the timers when inner map is replaced or deleted by user space. 1114 */ 1115 struct bpf_hrtimer { 1116 struct bpf_async_cb cb; 1117 struct hrtimer timer; 1118 atomic_t cancelling; 1119 }; 1120 1121 struct bpf_work { 1122 struct bpf_async_cb cb; 1123 struct work_struct work; 1124 struct work_struct delete_work; 1125 }; 1126 1127 /* the actual struct hidden inside uapi struct bpf_timer and bpf_wq */ 1128 struct bpf_async_kern { 1129 union { 1130 struct bpf_async_cb *cb; 1131 struct bpf_hrtimer *timer; 1132 struct bpf_work *work; 1133 }; 1134 /* bpf_spin_lock is used here instead of spinlock_t to make 1135 * sure that it always fits into space reserved by struct bpf_timer 1136 * regardless of LOCKDEP and spinlock debug flags. 1137 */ 1138 struct bpf_spin_lock lock; 1139 } __attribute__((aligned(8))); 1140 1141 enum bpf_async_type { 1142 BPF_ASYNC_TYPE_TIMER = 0, 1143 BPF_ASYNC_TYPE_WQ, 1144 }; 1145 1146 static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running); 1147 1148 static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer) 1149 { 1150 struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer); 1151 struct bpf_map *map = t->cb.map; 1152 void *value = t->cb.value; 1153 bpf_callback_t callback_fn; 1154 void *key; 1155 u32 idx; 1156 1157 BTF_TYPE_EMIT(struct bpf_timer); 1158 callback_fn = rcu_dereference_check(t->cb.callback_fn, rcu_read_lock_bh_held()); 1159 if (!callback_fn) 1160 goto out; 1161 1162 /* bpf_timer_cb() runs in hrtimer_run_softirq. It doesn't migrate and 1163 * cannot be preempted by another bpf_timer_cb() on the same cpu. 1164 * Remember the timer this callback is servicing to prevent 1165 * deadlock if callback_fn() calls bpf_timer_cancel() or 1166 * bpf_map_delete_elem() on the same timer. 1167 */ 1168 this_cpu_write(hrtimer_running, t); 1169 if (map->map_type == BPF_MAP_TYPE_ARRAY) { 1170 struct bpf_array *array = container_of(map, struct bpf_array, map); 1171 1172 /* compute the key */ 1173 idx = ((char *)value - array->value) / array->elem_size; 1174 key = &idx; 1175 } else { /* hash or lru */ 1176 key = value - round_up(map->key_size, 8); 1177 } 1178 1179 callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0); 1180 /* The verifier checked that return value is zero. */ 1181 1182 this_cpu_write(hrtimer_running, NULL); 1183 out: 1184 return HRTIMER_NORESTART; 1185 } 1186 1187 static void bpf_wq_work(struct work_struct *work) 1188 { 1189 struct bpf_work *w = container_of(work, struct bpf_work, work); 1190 struct bpf_async_cb *cb = &w->cb; 1191 struct bpf_map *map = cb->map; 1192 bpf_callback_t callback_fn; 1193 void *value = cb->value; 1194 void *key; 1195 u32 idx; 1196 1197 BTF_TYPE_EMIT(struct bpf_wq); 1198 1199 callback_fn = READ_ONCE(cb->callback_fn); 1200 if (!callback_fn) 1201 return; 1202 1203 if (map->map_type == BPF_MAP_TYPE_ARRAY) { 1204 struct bpf_array *array = container_of(map, struct bpf_array, map); 1205 1206 /* compute the key */ 1207 idx = ((char *)value - array->value) / array->elem_size; 1208 key = &idx; 1209 } else { /* hash or lru */ 1210 key = value - round_up(map->key_size, 8); 1211 } 1212 1213 rcu_read_lock_trace(); 1214 migrate_disable(); 1215 1216 callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0); 1217 1218 migrate_enable(); 1219 rcu_read_unlock_trace(); 1220 } 1221 1222 static void bpf_wq_delete_work(struct work_struct *work) 1223 { 1224 struct bpf_work *w = container_of(work, struct bpf_work, delete_work); 1225 1226 cancel_work_sync(&w->work); 1227 1228 kfree_rcu(w, cb.rcu); 1229 } 1230 1231 static void bpf_timer_delete_work(struct work_struct *work) 1232 { 1233 struct bpf_hrtimer *t = container_of(work, struct bpf_hrtimer, cb.delete_work); 1234 1235 /* Cancel the timer and wait for callback to complete if it was running. 1236 * If hrtimer_cancel() can be safely called it's safe to call 1237 * kfree_rcu(t) right after for both preallocated and non-preallocated 1238 * maps. The async->cb = NULL was already done and no code path can see 1239 * address 't' anymore. Timer if armed for existing bpf_hrtimer before 1240 * bpf_timer_cancel_and_free will have been cancelled. 1241 */ 1242 hrtimer_cancel(&t->timer); 1243 kfree_rcu(t, cb.rcu); 1244 } 1245 1246 static int __bpf_async_init(struct bpf_async_kern *async, struct bpf_map *map, u64 flags, 1247 enum bpf_async_type type) 1248 { 1249 struct bpf_async_cb *cb; 1250 struct bpf_hrtimer *t; 1251 struct bpf_work *w; 1252 clockid_t clockid; 1253 size_t size; 1254 int ret = 0; 1255 1256 if (in_nmi()) 1257 return -EOPNOTSUPP; 1258 1259 switch (type) { 1260 case BPF_ASYNC_TYPE_TIMER: 1261 size = sizeof(struct bpf_hrtimer); 1262 break; 1263 case BPF_ASYNC_TYPE_WQ: 1264 size = sizeof(struct bpf_work); 1265 break; 1266 default: 1267 return -EINVAL; 1268 } 1269 1270 __bpf_spin_lock_irqsave(&async->lock); 1271 t = async->timer; 1272 if (t) { 1273 ret = -EBUSY; 1274 goto out; 1275 } 1276 1277 /* Allocate via bpf_map_kmalloc_node() for memcg accounting. Until 1278 * kmalloc_nolock() is available, avoid locking issues by using 1279 * __GFP_HIGH (GFP_ATOMIC & ~__GFP_RECLAIM). 1280 */ 1281 cb = bpf_map_kmalloc_node(map, size, __GFP_HIGH, map->numa_node); 1282 if (!cb) { 1283 ret = -ENOMEM; 1284 goto out; 1285 } 1286 1287 switch (type) { 1288 case BPF_ASYNC_TYPE_TIMER: 1289 clockid = flags & (MAX_CLOCKS - 1); 1290 t = (struct bpf_hrtimer *)cb; 1291 1292 atomic_set(&t->cancelling, 0); 1293 INIT_WORK(&t->cb.delete_work, bpf_timer_delete_work); 1294 hrtimer_setup(&t->timer, bpf_timer_cb, clockid, HRTIMER_MODE_REL_SOFT); 1295 cb->value = (void *)async - map->record->timer_off; 1296 break; 1297 case BPF_ASYNC_TYPE_WQ: 1298 w = (struct bpf_work *)cb; 1299 1300 INIT_WORK(&w->work, bpf_wq_work); 1301 INIT_WORK(&w->delete_work, bpf_wq_delete_work); 1302 cb->value = (void *)async - map->record->wq_off; 1303 break; 1304 } 1305 cb->map = map; 1306 cb->prog = NULL; 1307 cb->flags = flags; 1308 rcu_assign_pointer(cb->callback_fn, NULL); 1309 1310 WRITE_ONCE(async->cb, cb); 1311 /* Guarantee the order between async->cb and map->usercnt. So 1312 * when there are concurrent uref release and bpf timer init, either 1313 * bpf_timer_cancel_and_free() called by uref release reads a no-NULL 1314 * timer or atomic64_read() below returns a zero usercnt. 1315 */ 1316 smp_mb(); 1317 if (!atomic64_read(&map->usercnt)) { 1318 /* maps with timers must be either held by user space 1319 * or pinned in bpffs. 1320 */ 1321 WRITE_ONCE(async->cb, NULL); 1322 kfree(cb); 1323 ret = -EPERM; 1324 } 1325 out: 1326 __bpf_spin_unlock_irqrestore(&async->lock); 1327 return ret; 1328 } 1329 1330 BPF_CALL_3(bpf_timer_init, struct bpf_async_kern *, timer, struct bpf_map *, map, 1331 u64, flags) 1332 { 1333 clock_t clockid = flags & (MAX_CLOCKS - 1); 1334 1335 BUILD_BUG_ON(MAX_CLOCKS != 16); 1336 BUILD_BUG_ON(sizeof(struct bpf_async_kern) > sizeof(struct bpf_timer)); 1337 BUILD_BUG_ON(__alignof__(struct bpf_async_kern) != __alignof__(struct bpf_timer)); 1338 1339 if (flags >= MAX_CLOCKS || 1340 /* similar to timerfd except _ALARM variants are not supported */ 1341 (clockid != CLOCK_MONOTONIC && 1342 clockid != CLOCK_REALTIME && 1343 clockid != CLOCK_BOOTTIME)) 1344 return -EINVAL; 1345 1346 return __bpf_async_init(timer, map, flags, BPF_ASYNC_TYPE_TIMER); 1347 } 1348 1349 static const struct bpf_func_proto bpf_timer_init_proto = { 1350 .func = bpf_timer_init, 1351 .gpl_only = true, 1352 .ret_type = RET_INTEGER, 1353 .arg1_type = ARG_PTR_TO_TIMER, 1354 .arg2_type = ARG_CONST_MAP_PTR, 1355 .arg3_type = ARG_ANYTHING, 1356 }; 1357 1358 static int __bpf_async_set_callback(struct bpf_async_kern *async, void *callback_fn, 1359 struct bpf_prog_aux *aux, unsigned int flags, 1360 enum bpf_async_type type) 1361 { 1362 struct bpf_prog *prev, *prog = aux->prog; 1363 struct bpf_async_cb *cb; 1364 int ret = 0; 1365 1366 if (in_nmi()) 1367 return -EOPNOTSUPP; 1368 __bpf_spin_lock_irqsave(&async->lock); 1369 cb = async->cb; 1370 if (!cb) { 1371 ret = -EINVAL; 1372 goto out; 1373 } 1374 if (!atomic64_read(&cb->map->usercnt)) { 1375 /* maps with timers must be either held by user space 1376 * or pinned in bpffs. Otherwise timer might still be 1377 * running even when bpf prog is detached and user space 1378 * is gone, since map_release_uref won't ever be called. 1379 */ 1380 ret = -EPERM; 1381 goto out; 1382 } 1383 prev = cb->prog; 1384 if (prev != prog) { 1385 /* Bump prog refcnt once. Every bpf_timer_set_callback() 1386 * can pick different callback_fn-s within the same prog. 1387 */ 1388 prog = bpf_prog_inc_not_zero(prog); 1389 if (IS_ERR(prog)) { 1390 ret = PTR_ERR(prog); 1391 goto out; 1392 } 1393 if (prev) 1394 /* Drop prev prog refcnt when swapping with new prog */ 1395 bpf_prog_put(prev); 1396 cb->prog = prog; 1397 } 1398 rcu_assign_pointer(cb->callback_fn, callback_fn); 1399 out: 1400 __bpf_spin_unlock_irqrestore(&async->lock); 1401 return ret; 1402 } 1403 1404 BPF_CALL_3(bpf_timer_set_callback, struct bpf_async_kern *, timer, void *, callback_fn, 1405 struct bpf_prog_aux *, aux) 1406 { 1407 return __bpf_async_set_callback(timer, callback_fn, aux, 0, BPF_ASYNC_TYPE_TIMER); 1408 } 1409 1410 static const struct bpf_func_proto bpf_timer_set_callback_proto = { 1411 .func = bpf_timer_set_callback, 1412 .gpl_only = true, 1413 .ret_type = RET_INTEGER, 1414 .arg1_type = ARG_PTR_TO_TIMER, 1415 .arg2_type = ARG_PTR_TO_FUNC, 1416 }; 1417 1418 BPF_CALL_3(bpf_timer_start, struct bpf_async_kern *, timer, u64, nsecs, u64, flags) 1419 { 1420 struct bpf_hrtimer *t; 1421 int ret = 0; 1422 enum hrtimer_mode mode; 1423 1424 if (in_nmi()) 1425 return -EOPNOTSUPP; 1426 if (flags & ~(BPF_F_TIMER_ABS | BPF_F_TIMER_CPU_PIN)) 1427 return -EINVAL; 1428 __bpf_spin_lock_irqsave(&timer->lock); 1429 t = timer->timer; 1430 if (!t || !t->cb.prog) { 1431 ret = -EINVAL; 1432 goto out; 1433 } 1434 1435 if (flags & BPF_F_TIMER_ABS) 1436 mode = HRTIMER_MODE_ABS_SOFT; 1437 else 1438 mode = HRTIMER_MODE_REL_SOFT; 1439 1440 if (flags & BPF_F_TIMER_CPU_PIN) 1441 mode |= HRTIMER_MODE_PINNED; 1442 1443 hrtimer_start(&t->timer, ns_to_ktime(nsecs), mode); 1444 out: 1445 __bpf_spin_unlock_irqrestore(&timer->lock); 1446 return ret; 1447 } 1448 1449 static const struct bpf_func_proto bpf_timer_start_proto = { 1450 .func = bpf_timer_start, 1451 .gpl_only = true, 1452 .ret_type = RET_INTEGER, 1453 .arg1_type = ARG_PTR_TO_TIMER, 1454 .arg2_type = ARG_ANYTHING, 1455 .arg3_type = ARG_ANYTHING, 1456 }; 1457 1458 static void drop_prog_refcnt(struct bpf_async_cb *async) 1459 { 1460 struct bpf_prog *prog = async->prog; 1461 1462 if (prog) { 1463 bpf_prog_put(prog); 1464 async->prog = NULL; 1465 rcu_assign_pointer(async->callback_fn, NULL); 1466 } 1467 } 1468 1469 BPF_CALL_1(bpf_timer_cancel, struct bpf_async_kern *, timer) 1470 { 1471 struct bpf_hrtimer *t, *cur_t; 1472 bool inc = false; 1473 int ret = 0; 1474 1475 if (in_nmi()) 1476 return -EOPNOTSUPP; 1477 rcu_read_lock(); 1478 __bpf_spin_lock_irqsave(&timer->lock); 1479 t = timer->timer; 1480 if (!t) { 1481 ret = -EINVAL; 1482 goto out; 1483 } 1484 1485 cur_t = this_cpu_read(hrtimer_running); 1486 if (cur_t == t) { 1487 /* If bpf callback_fn is trying to bpf_timer_cancel() 1488 * its own timer the hrtimer_cancel() will deadlock 1489 * since it waits for callback_fn to finish. 1490 */ 1491 ret = -EDEADLK; 1492 goto out; 1493 } 1494 1495 /* Only account in-flight cancellations when invoked from a timer 1496 * callback, since we want to avoid waiting only if other _callbacks_ 1497 * are waiting on us, to avoid introducing lockups. Non-callback paths 1498 * are ok, since nobody would synchronously wait for their completion. 1499 */ 1500 if (!cur_t) 1501 goto drop; 1502 atomic_inc(&t->cancelling); 1503 /* Need full barrier after relaxed atomic_inc */ 1504 smp_mb__after_atomic(); 1505 inc = true; 1506 if (atomic_read(&cur_t->cancelling)) { 1507 /* We're cancelling timer t, while some other timer callback is 1508 * attempting to cancel us. In such a case, it might be possible 1509 * that timer t belongs to the other callback, or some other 1510 * callback waiting upon it (creating transitive dependencies 1511 * upon us), and we will enter a deadlock if we continue 1512 * cancelling and waiting for it synchronously, since it might 1513 * do the same. Bail! 1514 */ 1515 ret = -EDEADLK; 1516 goto out; 1517 } 1518 drop: 1519 drop_prog_refcnt(&t->cb); 1520 out: 1521 __bpf_spin_unlock_irqrestore(&timer->lock); 1522 /* Cancel the timer and wait for associated callback to finish 1523 * if it was running. 1524 */ 1525 ret = ret ?: hrtimer_cancel(&t->timer); 1526 if (inc) 1527 atomic_dec(&t->cancelling); 1528 rcu_read_unlock(); 1529 return ret; 1530 } 1531 1532 static const struct bpf_func_proto bpf_timer_cancel_proto = { 1533 .func = bpf_timer_cancel, 1534 .gpl_only = true, 1535 .ret_type = RET_INTEGER, 1536 .arg1_type = ARG_PTR_TO_TIMER, 1537 }; 1538 1539 static struct bpf_async_cb *__bpf_async_cancel_and_free(struct bpf_async_kern *async) 1540 { 1541 struct bpf_async_cb *cb; 1542 1543 /* Performance optimization: read async->cb without lock first. */ 1544 if (!READ_ONCE(async->cb)) 1545 return NULL; 1546 1547 __bpf_spin_lock_irqsave(&async->lock); 1548 /* re-read it under lock */ 1549 cb = async->cb; 1550 if (!cb) 1551 goto out; 1552 drop_prog_refcnt(cb); 1553 /* The subsequent bpf_timer_start/cancel() helpers won't be able to use 1554 * this timer, since it won't be initialized. 1555 */ 1556 WRITE_ONCE(async->cb, NULL); 1557 out: 1558 __bpf_spin_unlock_irqrestore(&async->lock); 1559 return cb; 1560 } 1561 1562 /* This function is called by map_delete/update_elem for individual element and 1563 * by ops->map_release_uref when the user space reference to a map reaches zero. 1564 */ 1565 void bpf_timer_cancel_and_free(void *val) 1566 { 1567 struct bpf_hrtimer *t; 1568 1569 t = (struct bpf_hrtimer *)__bpf_async_cancel_and_free(val); 1570 1571 if (!t) 1572 return; 1573 /* We check that bpf_map_delete/update_elem() was called from timer 1574 * callback_fn. In such case we don't call hrtimer_cancel() (since it 1575 * will deadlock) and don't call hrtimer_try_to_cancel() (since it will 1576 * just return -1). Though callback_fn is still running on this cpu it's 1577 * safe to do kfree(t) because bpf_timer_cb() read everything it needed 1578 * from 't'. The bpf subprog callback_fn won't be able to access 't', 1579 * since async->cb = NULL was already done. The timer will be 1580 * effectively cancelled because bpf_timer_cb() will return 1581 * HRTIMER_NORESTART. 1582 * 1583 * However, it is possible the timer callback_fn calling us armed the 1584 * timer _before_ calling us, such that failing to cancel it here will 1585 * cause it to possibly use struct hrtimer after freeing bpf_hrtimer. 1586 * Therefore, we _need_ to cancel any outstanding timers before we do 1587 * kfree_rcu, even though no more timers can be armed. 1588 * 1589 * Moreover, we need to schedule work even if timer does not belong to 1590 * the calling callback_fn, as on two different CPUs, we can end up in a 1591 * situation where both sides run in parallel, try to cancel one 1592 * another, and we end up waiting on both sides in hrtimer_cancel 1593 * without making forward progress, since timer1 depends on time2 1594 * callback to finish, and vice versa. 1595 * 1596 * CPU 1 (timer1_cb) CPU 2 (timer2_cb) 1597 * bpf_timer_cancel_and_free(timer2) bpf_timer_cancel_and_free(timer1) 1598 * 1599 * To avoid these issues, punt to workqueue context when we are in a 1600 * timer callback. 1601 */ 1602 if (this_cpu_read(hrtimer_running)) { 1603 queue_work(system_unbound_wq, &t->cb.delete_work); 1604 return; 1605 } 1606 1607 if (IS_ENABLED(CONFIG_PREEMPT_RT)) { 1608 /* If the timer is running on other CPU, also use a kworker to 1609 * wait for the completion of the timer instead of trying to 1610 * acquire a sleepable lock in hrtimer_cancel() to wait for its 1611 * completion. 1612 */ 1613 if (hrtimer_try_to_cancel(&t->timer) >= 0) 1614 kfree_rcu(t, cb.rcu); 1615 else 1616 queue_work(system_unbound_wq, &t->cb.delete_work); 1617 } else { 1618 bpf_timer_delete_work(&t->cb.delete_work); 1619 } 1620 } 1621 1622 /* This function is called by map_delete/update_elem for individual element and 1623 * by ops->map_release_uref when the user space reference to a map reaches zero. 1624 */ 1625 void bpf_wq_cancel_and_free(void *val) 1626 { 1627 struct bpf_work *work; 1628 1629 BTF_TYPE_EMIT(struct bpf_wq); 1630 1631 work = (struct bpf_work *)__bpf_async_cancel_and_free(val); 1632 if (!work) 1633 return; 1634 /* Trigger cancel of the sleepable work, but *do not* wait for 1635 * it to finish if it was running as we might not be in a 1636 * sleepable context. 1637 * kfree will be called once the work has finished. 1638 */ 1639 schedule_work(&work->delete_work); 1640 } 1641 1642 BPF_CALL_2(bpf_kptr_xchg, void *, dst, void *, ptr) 1643 { 1644 unsigned long *kptr = dst; 1645 1646 /* This helper may be inlined by verifier. */ 1647 return xchg(kptr, (unsigned long)ptr); 1648 } 1649 1650 /* Unlike other PTR_TO_BTF_ID helpers the btf_id in bpf_kptr_xchg() 1651 * helper is determined dynamically by the verifier. Use BPF_PTR_POISON to 1652 * denote type that verifier will determine. 1653 */ 1654 static const struct bpf_func_proto bpf_kptr_xchg_proto = { 1655 .func = bpf_kptr_xchg, 1656 .gpl_only = false, 1657 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 1658 .ret_btf_id = BPF_PTR_POISON, 1659 .arg1_type = ARG_KPTR_XCHG_DEST, 1660 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL | OBJ_RELEASE, 1661 .arg2_btf_id = BPF_PTR_POISON, 1662 }; 1663 1664 /* Since the upper 8 bits of dynptr->size is reserved, the 1665 * maximum supported size is 2^24 - 1. 1666 */ 1667 #define DYNPTR_MAX_SIZE ((1UL << 24) - 1) 1668 #define DYNPTR_TYPE_SHIFT 28 1669 #define DYNPTR_SIZE_MASK 0xFFFFFF 1670 #define DYNPTR_RDONLY_BIT BIT(31) 1671 1672 bool __bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr) 1673 { 1674 return ptr->size & DYNPTR_RDONLY_BIT; 1675 } 1676 1677 void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr) 1678 { 1679 ptr->size |= DYNPTR_RDONLY_BIT; 1680 } 1681 1682 static void bpf_dynptr_set_type(struct bpf_dynptr_kern *ptr, enum bpf_dynptr_type type) 1683 { 1684 ptr->size |= type << DYNPTR_TYPE_SHIFT; 1685 } 1686 1687 static enum bpf_dynptr_type bpf_dynptr_get_type(const struct bpf_dynptr_kern *ptr) 1688 { 1689 return (ptr->size & ~(DYNPTR_RDONLY_BIT)) >> DYNPTR_TYPE_SHIFT; 1690 } 1691 1692 u32 __bpf_dynptr_size(const struct bpf_dynptr_kern *ptr) 1693 { 1694 return ptr->size & DYNPTR_SIZE_MASK; 1695 } 1696 1697 static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size) 1698 { 1699 u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK; 1700 1701 ptr->size = new_size | metadata; 1702 } 1703 1704 int bpf_dynptr_check_size(u32 size) 1705 { 1706 return size > DYNPTR_MAX_SIZE ? -E2BIG : 0; 1707 } 1708 1709 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data, 1710 enum bpf_dynptr_type type, u32 offset, u32 size) 1711 { 1712 ptr->data = data; 1713 ptr->offset = offset; 1714 ptr->size = size; 1715 bpf_dynptr_set_type(ptr, type); 1716 } 1717 1718 void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr) 1719 { 1720 memset(ptr, 0, sizeof(*ptr)); 1721 } 1722 1723 BPF_CALL_4(bpf_dynptr_from_mem, void *, data, u32, size, u64, flags, struct bpf_dynptr_kern *, ptr) 1724 { 1725 int err; 1726 1727 BTF_TYPE_EMIT(struct bpf_dynptr); 1728 1729 err = bpf_dynptr_check_size(size); 1730 if (err) 1731 goto error; 1732 1733 /* flags is currently unsupported */ 1734 if (flags) { 1735 err = -EINVAL; 1736 goto error; 1737 } 1738 1739 bpf_dynptr_init(ptr, data, BPF_DYNPTR_TYPE_LOCAL, 0, size); 1740 1741 return 0; 1742 1743 error: 1744 bpf_dynptr_set_null(ptr); 1745 return err; 1746 } 1747 1748 static const struct bpf_func_proto bpf_dynptr_from_mem_proto = { 1749 .func = bpf_dynptr_from_mem, 1750 .gpl_only = false, 1751 .ret_type = RET_INTEGER, 1752 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 1753 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 1754 .arg3_type = ARG_ANYTHING, 1755 .arg4_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL | MEM_UNINIT | MEM_WRITE, 1756 }; 1757 1758 static int __bpf_dynptr_read(void *dst, u32 len, const struct bpf_dynptr_kern *src, 1759 u32 offset, u64 flags) 1760 { 1761 enum bpf_dynptr_type type; 1762 int err; 1763 1764 if (!src->data || flags) 1765 return -EINVAL; 1766 1767 err = bpf_dynptr_check_off_len(src, offset, len); 1768 if (err) 1769 return err; 1770 1771 type = bpf_dynptr_get_type(src); 1772 1773 switch (type) { 1774 case BPF_DYNPTR_TYPE_LOCAL: 1775 case BPF_DYNPTR_TYPE_RINGBUF: 1776 /* Source and destination may possibly overlap, hence use memmove to 1777 * copy the data. E.g. bpf_dynptr_from_mem may create two dynptr 1778 * pointing to overlapping PTR_TO_MAP_VALUE regions. 1779 */ 1780 memmove(dst, src->data + src->offset + offset, len); 1781 return 0; 1782 case BPF_DYNPTR_TYPE_SKB: 1783 return __bpf_skb_load_bytes(src->data, src->offset + offset, dst, len); 1784 case BPF_DYNPTR_TYPE_XDP: 1785 return __bpf_xdp_load_bytes(src->data, src->offset + offset, dst, len); 1786 case BPF_DYNPTR_TYPE_SKB_META: 1787 memmove(dst, bpf_skb_meta_pointer(src->data, src->offset + offset), len); 1788 return 0; 1789 default: 1790 WARN_ONCE(true, "bpf_dynptr_read: unknown dynptr type %d\n", type); 1791 return -EFAULT; 1792 } 1793 } 1794 1795 BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, const struct bpf_dynptr_kern *, src, 1796 u32, offset, u64, flags) 1797 { 1798 return __bpf_dynptr_read(dst, len, src, offset, flags); 1799 } 1800 1801 static const struct bpf_func_proto bpf_dynptr_read_proto = { 1802 .func = bpf_dynptr_read, 1803 .gpl_only = false, 1804 .ret_type = RET_INTEGER, 1805 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 1806 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 1807 .arg3_type = ARG_PTR_TO_DYNPTR | MEM_RDONLY, 1808 .arg4_type = ARG_ANYTHING, 1809 .arg5_type = ARG_ANYTHING, 1810 }; 1811 1812 int __bpf_dynptr_write(const struct bpf_dynptr_kern *dst, u32 offset, void *src, 1813 u32 len, u64 flags) 1814 { 1815 enum bpf_dynptr_type type; 1816 int err; 1817 1818 if (!dst->data || __bpf_dynptr_is_rdonly(dst)) 1819 return -EINVAL; 1820 1821 err = bpf_dynptr_check_off_len(dst, offset, len); 1822 if (err) 1823 return err; 1824 1825 type = bpf_dynptr_get_type(dst); 1826 1827 switch (type) { 1828 case BPF_DYNPTR_TYPE_LOCAL: 1829 case BPF_DYNPTR_TYPE_RINGBUF: 1830 if (flags) 1831 return -EINVAL; 1832 /* Source and destination may possibly overlap, hence use memmove to 1833 * copy the data. E.g. bpf_dynptr_from_mem may create two dynptr 1834 * pointing to overlapping PTR_TO_MAP_VALUE regions. 1835 */ 1836 memmove(dst->data + dst->offset + offset, src, len); 1837 return 0; 1838 case BPF_DYNPTR_TYPE_SKB: 1839 return __bpf_skb_store_bytes(dst->data, dst->offset + offset, src, len, 1840 flags); 1841 case BPF_DYNPTR_TYPE_XDP: 1842 if (flags) 1843 return -EINVAL; 1844 return __bpf_xdp_store_bytes(dst->data, dst->offset + offset, src, len); 1845 case BPF_DYNPTR_TYPE_SKB_META: 1846 if (flags) 1847 return -EINVAL; 1848 memmove(bpf_skb_meta_pointer(dst->data, dst->offset + offset), src, len); 1849 return 0; 1850 default: 1851 WARN_ONCE(true, "bpf_dynptr_write: unknown dynptr type %d\n", type); 1852 return -EFAULT; 1853 } 1854 } 1855 1856 BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u32, offset, void *, src, 1857 u32, len, u64, flags) 1858 { 1859 return __bpf_dynptr_write(dst, offset, src, len, flags); 1860 } 1861 1862 static const struct bpf_func_proto bpf_dynptr_write_proto = { 1863 .func = bpf_dynptr_write, 1864 .gpl_only = false, 1865 .ret_type = RET_INTEGER, 1866 .arg1_type = ARG_PTR_TO_DYNPTR | MEM_RDONLY, 1867 .arg2_type = ARG_ANYTHING, 1868 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, 1869 .arg4_type = ARG_CONST_SIZE_OR_ZERO, 1870 .arg5_type = ARG_ANYTHING, 1871 }; 1872 1873 BPF_CALL_3(bpf_dynptr_data, const struct bpf_dynptr_kern *, ptr, u32, offset, u32, len) 1874 { 1875 enum bpf_dynptr_type type; 1876 int err; 1877 1878 if (!ptr->data) 1879 return 0; 1880 1881 err = bpf_dynptr_check_off_len(ptr, offset, len); 1882 if (err) 1883 return 0; 1884 1885 if (__bpf_dynptr_is_rdonly(ptr)) 1886 return 0; 1887 1888 type = bpf_dynptr_get_type(ptr); 1889 1890 switch (type) { 1891 case BPF_DYNPTR_TYPE_LOCAL: 1892 case BPF_DYNPTR_TYPE_RINGBUF: 1893 return (unsigned long)(ptr->data + ptr->offset + offset); 1894 case BPF_DYNPTR_TYPE_SKB: 1895 case BPF_DYNPTR_TYPE_XDP: 1896 case BPF_DYNPTR_TYPE_SKB_META: 1897 /* skb and xdp dynptrs should use bpf_dynptr_slice / bpf_dynptr_slice_rdwr */ 1898 return 0; 1899 default: 1900 WARN_ONCE(true, "bpf_dynptr_data: unknown dynptr type %d\n", type); 1901 return 0; 1902 } 1903 } 1904 1905 static const struct bpf_func_proto bpf_dynptr_data_proto = { 1906 .func = bpf_dynptr_data, 1907 .gpl_only = false, 1908 .ret_type = RET_PTR_TO_DYNPTR_MEM_OR_NULL, 1909 .arg1_type = ARG_PTR_TO_DYNPTR | MEM_RDONLY, 1910 .arg2_type = ARG_ANYTHING, 1911 .arg3_type = ARG_CONST_ALLOC_SIZE_OR_ZERO, 1912 }; 1913 1914 const struct bpf_func_proto bpf_get_current_task_proto __weak; 1915 const struct bpf_func_proto bpf_get_current_task_btf_proto __weak; 1916 const struct bpf_func_proto bpf_probe_read_user_proto __weak; 1917 const struct bpf_func_proto bpf_probe_read_user_str_proto __weak; 1918 const struct bpf_func_proto bpf_probe_read_kernel_proto __weak; 1919 const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak; 1920 const struct bpf_func_proto bpf_task_pt_regs_proto __weak; 1921 const struct bpf_func_proto bpf_perf_event_read_proto __weak; 1922 const struct bpf_func_proto bpf_send_signal_proto __weak; 1923 const struct bpf_func_proto bpf_send_signal_thread_proto __weak; 1924 const struct bpf_func_proto bpf_get_task_stack_sleepable_proto __weak; 1925 const struct bpf_func_proto bpf_get_task_stack_proto __weak; 1926 const struct bpf_func_proto bpf_get_branch_snapshot_proto __weak; 1927 1928 const struct bpf_func_proto * 1929 bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1930 { 1931 switch (func_id) { 1932 case BPF_FUNC_map_lookup_elem: 1933 return &bpf_map_lookup_elem_proto; 1934 case BPF_FUNC_map_update_elem: 1935 return &bpf_map_update_elem_proto; 1936 case BPF_FUNC_map_delete_elem: 1937 return &bpf_map_delete_elem_proto; 1938 case BPF_FUNC_map_push_elem: 1939 return &bpf_map_push_elem_proto; 1940 case BPF_FUNC_map_pop_elem: 1941 return &bpf_map_pop_elem_proto; 1942 case BPF_FUNC_map_peek_elem: 1943 return &bpf_map_peek_elem_proto; 1944 case BPF_FUNC_map_lookup_percpu_elem: 1945 return &bpf_map_lookup_percpu_elem_proto; 1946 case BPF_FUNC_get_prandom_u32: 1947 return &bpf_get_prandom_u32_proto; 1948 case BPF_FUNC_get_smp_processor_id: 1949 return &bpf_get_raw_smp_processor_id_proto; 1950 case BPF_FUNC_get_numa_node_id: 1951 return &bpf_get_numa_node_id_proto; 1952 case BPF_FUNC_tail_call: 1953 return &bpf_tail_call_proto; 1954 case BPF_FUNC_ktime_get_ns: 1955 return &bpf_ktime_get_ns_proto; 1956 case BPF_FUNC_ktime_get_boot_ns: 1957 return &bpf_ktime_get_boot_ns_proto; 1958 case BPF_FUNC_ktime_get_tai_ns: 1959 return &bpf_ktime_get_tai_ns_proto; 1960 case BPF_FUNC_ringbuf_output: 1961 return &bpf_ringbuf_output_proto; 1962 case BPF_FUNC_ringbuf_reserve: 1963 return &bpf_ringbuf_reserve_proto; 1964 case BPF_FUNC_ringbuf_submit: 1965 return &bpf_ringbuf_submit_proto; 1966 case BPF_FUNC_ringbuf_discard: 1967 return &bpf_ringbuf_discard_proto; 1968 case BPF_FUNC_ringbuf_query: 1969 return &bpf_ringbuf_query_proto; 1970 case BPF_FUNC_strncmp: 1971 return &bpf_strncmp_proto; 1972 case BPF_FUNC_strtol: 1973 return &bpf_strtol_proto; 1974 case BPF_FUNC_strtoul: 1975 return &bpf_strtoul_proto; 1976 case BPF_FUNC_get_current_pid_tgid: 1977 return &bpf_get_current_pid_tgid_proto; 1978 case BPF_FUNC_get_ns_current_pid_tgid: 1979 return &bpf_get_ns_current_pid_tgid_proto; 1980 case BPF_FUNC_get_current_uid_gid: 1981 return &bpf_get_current_uid_gid_proto; 1982 default: 1983 break; 1984 } 1985 1986 if (!bpf_token_capable(prog->aux->token, CAP_BPF)) 1987 return NULL; 1988 1989 switch (func_id) { 1990 case BPF_FUNC_spin_lock: 1991 return &bpf_spin_lock_proto; 1992 case BPF_FUNC_spin_unlock: 1993 return &bpf_spin_unlock_proto; 1994 case BPF_FUNC_jiffies64: 1995 return &bpf_jiffies64_proto; 1996 case BPF_FUNC_per_cpu_ptr: 1997 return &bpf_per_cpu_ptr_proto; 1998 case BPF_FUNC_this_cpu_ptr: 1999 return &bpf_this_cpu_ptr_proto; 2000 case BPF_FUNC_timer_init: 2001 return &bpf_timer_init_proto; 2002 case BPF_FUNC_timer_set_callback: 2003 return &bpf_timer_set_callback_proto; 2004 case BPF_FUNC_timer_start: 2005 return &bpf_timer_start_proto; 2006 case BPF_FUNC_timer_cancel: 2007 return &bpf_timer_cancel_proto; 2008 case BPF_FUNC_kptr_xchg: 2009 return &bpf_kptr_xchg_proto; 2010 case BPF_FUNC_for_each_map_elem: 2011 return &bpf_for_each_map_elem_proto; 2012 case BPF_FUNC_loop: 2013 return &bpf_loop_proto; 2014 case BPF_FUNC_user_ringbuf_drain: 2015 return &bpf_user_ringbuf_drain_proto; 2016 case BPF_FUNC_ringbuf_reserve_dynptr: 2017 return &bpf_ringbuf_reserve_dynptr_proto; 2018 case BPF_FUNC_ringbuf_submit_dynptr: 2019 return &bpf_ringbuf_submit_dynptr_proto; 2020 case BPF_FUNC_ringbuf_discard_dynptr: 2021 return &bpf_ringbuf_discard_dynptr_proto; 2022 case BPF_FUNC_dynptr_from_mem: 2023 return &bpf_dynptr_from_mem_proto; 2024 case BPF_FUNC_dynptr_read: 2025 return &bpf_dynptr_read_proto; 2026 case BPF_FUNC_dynptr_write: 2027 return &bpf_dynptr_write_proto; 2028 case BPF_FUNC_dynptr_data: 2029 return &bpf_dynptr_data_proto; 2030 #ifdef CONFIG_CGROUPS 2031 case BPF_FUNC_cgrp_storage_get: 2032 return &bpf_cgrp_storage_get_proto; 2033 case BPF_FUNC_cgrp_storage_delete: 2034 return &bpf_cgrp_storage_delete_proto; 2035 case BPF_FUNC_get_current_cgroup_id: 2036 return &bpf_get_current_cgroup_id_proto; 2037 case BPF_FUNC_get_current_ancestor_cgroup_id: 2038 return &bpf_get_current_ancestor_cgroup_id_proto; 2039 case BPF_FUNC_current_task_under_cgroup: 2040 return &bpf_current_task_under_cgroup_proto; 2041 #endif 2042 #ifdef CONFIG_CGROUP_NET_CLASSID 2043 case BPF_FUNC_get_cgroup_classid: 2044 return &bpf_get_cgroup_classid_curr_proto; 2045 #endif 2046 case BPF_FUNC_task_storage_get: 2047 if (bpf_prog_check_recur(prog)) 2048 return &bpf_task_storage_get_recur_proto; 2049 return &bpf_task_storage_get_proto; 2050 case BPF_FUNC_task_storage_delete: 2051 if (bpf_prog_check_recur(prog)) 2052 return &bpf_task_storage_delete_recur_proto; 2053 return &bpf_task_storage_delete_proto; 2054 default: 2055 break; 2056 } 2057 2058 if (!bpf_token_capable(prog->aux->token, CAP_PERFMON)) 2059 return NULL; 2060 2061 switch (func_id) { 2062 case BPF_FUNC_trace_printk: 2063 return bpf_get_trace_printk_proto(); 2064 case BPF_FUNC_get_current_task: 2065 return &bpf_get_current_task_proto; 2066 case BPF_FUNC_get_current_task_btf: 2067 return &bpf_get_current_task_btf_proto; 2068 case BPF_FUNC_get_current_comm: 2069 return &bpf_get_current_comm_proto; 2070 case BPF_FUNC_probe_read_user: 2071 return &bpf_probe_read_user_proto; 2072 case BPF_FUNC_probe_read_kernel: 2073 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? 2074 NULL : &bpf_probe_read_kernel_proto; 2075 case BPF_FUNC_probe_read_user_str: 2076 return &bpf_probe_read_user_str_proto; 2077 case BPF_FUNC_probe_read_kernel_str: 2078 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? 2079 NULL : &bpf_probe_read_kernel_str_proto; 2080 case BPF_FUNC_copy_from_user: 2081 return &bpf_copy_from_user_proto; 2082 case BPF_FUNC_copy_from_user_task: 2083 return &bpf_copy_from_user_task_proto; 2084 case BPF_FUNC_snprintf_btf: 2085 return &bpf_snprintf_btf_proto; 2086 case BPF_FUNC_snprintf: 2087 return &bpf_snprintf_proto; 2088 case BPF_FUNC_task_pt_regs: 2089 return &bpf_task_pt_regs_proto; 2090 case BPF_FUNC_trace_vprintk: 2091 return bpf_get_trace_vprintk_proto(); 2092 case BPF_FUNC_perf_event_read_value: 2093 return bpf_get_perf_event_read_value_proto(); 2094 case BPF_FUNC_perf_event_read: 2095 return &bpf_perf_event_read_proto; 2096 case BPF_FUNC_send_signal: 2097 return &bpf_send_signal_proto; 2098 case BPF_FUNC_send_signal_thread: 2099 return &bpf_send_signal_thread_proto; 2100 case BPF_FUNC_get_task_stack: 2101 return prog->sleepable ? &bpf_get_task_stack_sleepable_proto 2102 : &bpf_get_task_stack_proto; 2103 case BPF_FUNC_get_branch_snapshot: 2104 return &bpf_get_branch_snapshot_proto; 2105 case BPF_FUNC_find_vma: 2106 return &bpf_find_vma_proto; 2107 default: 2108 return NULL; 2109 } 2110 } 2111 EXPORT_SYMBOL_GPL(bpf_base_func_proto); 2112 2113 void bpf_list_head_free(const struct btf_field *field, void *list_head, 2114 struct bpf_spin_lock *spin_lock) 2115 { 2116 struct list_head *head = list_head, *orig_head = list_head; 2117 2118 BUILD_BUG_ON(sizeof(struct list_head) > sizeof(struct bpf_list_head)); 2119 BUILD_BUG_ON(__alignof__(struct list_head) > __alignof__(struct bpf_list_head)); 2120 2121 /* Do the actual list draining outside the lock to not hold the lock for 2122 * too long, and also prevent deadlocks if tracing programs end up 2123 * executing on entry/exit of functions called inside the critical 2124 * section, and end up doing map ops that call bpf_list_head_free for 2125 * the same map value again. 2126 */ 2127 __bpf_spin_lock_irqsave(spin_lock); 2128 if (!head->next || list_empty(head)) 2129 goto unlock; 2130 head = head->next; 2131 unlock: 2132 INIT_LIST_HEAD(orig_head); 2133 __bpf_spin_unlock_irqrestore(spin_lock); 2134 2135 while (head != orig_head) { 2136 void *obj = head; 2137 2138 obj -= field->graph_root.node_offset; 2139 head = head->next; 2140 /* The contained type can also have resources, including a 2141 * bpf_list_head which needs to be freed. 2142 */ 2143 __bpf_obj_drop_impl(obj, field->graph_root.value_rec, false); 2144 } 2145 } 2146 2147 /* Like rbtree_postorder_for_each_entry_safe, but 'pos' and 'n' are 2148 * 'rb_node *', so field name of rb_node within containing struct is not 2149 * needed. 2150 * 2151 * Since bpf_rb_tree's node type has a corresponding struct btf_field with 2152 * graph_root.node_offset, it's not necessary to know field name 2153 * or type of node struct 2154 */ 2155 #define bpf_rbtree_postorder_for_each_entry_safe(pos, n, root) \ 2156 for (pos = rb_first_postorder(root); \ 2157 pos && ({ n = rb_next_postorder(pos); 1; }); \ 2158 pos = n) 2159 2160 void bpf_rb_root_free(const struct btf_field *field, void *rb_root, 2161 struct bpf_spin_lock *spin_lock) 2162 { 2163 struct rb_root_cached orig_root, *root = rb_root; 2164 struct rb_node *pos, *n; 2165 void *obj; 2166 2167 BUILD_BUG_ON(sizeof(struct rb_root_cached) > sizeof(struct bpf_rb_root)); 2168 BUILD_BUG_ON(__alignof__(struct rb_root_cached) > __alignof__(struct bpf_rb_root)); 2169 2170 __bpf_spin_lock_irqsave(spin_lock); 2171 orig_root = *root; 2172 *root = RB_ROOT_CACHED; 2173 __bpf_spin_unlock_irqrestore(spin_lock); 2174 2175 bpf_rbtree_postorder_for_each_entry_safe(pos, n, &orig_root.rb_root) { 2176 obj = pos; 2177 obj -= field->graph_root.node_offset; 2178 2179 2180 __bpf_obj_drop_impl(obj, field->graph_root.value_rec, false); 2181 } 2182 } 2183 2184 __bpf_kfunc_start_defs(); 2185 2186 __bpf_kfunc void *bpf_obj_new_impl(u64 local_type_id__k, void *meta__ign) 2187 { 2188 struct btf_struct_meta *meta = meta__ign; 2189 u64 size = local_type_id__k; 2190 void *p; 2191 2192 p = bpf_mem_alloc(&bpf_global_ma, size); 2193 if (!p) 2194 return NULL; 2195 if (meta) 2196 bpf_obj_init(meta->record, p); 2197 return p; 2198 } 2199 2200 __bpf_kfunc void *bpf_percpu_obj_new_impl(u64 local_type_id__k, void *meta__ign) 2201 { 2202 u64 size = local_type_id__k; 2203 2204 /* The verifier has ensured that meta__ign must be NULL */ 2205 return bpf_mem_alloc(&bpf_global_percpu_ma, size); 2206 } 2207 2208 /* Must be called under migrate_disable(), as required by bpf_mem_free */ 2209 void __bpf_obj_drop_impl(void *p, const struct btf_record *rec, bool percpu) 2210 { 2211 struct bpf_mem_alloc *ma; 2212 2213 if (rec && rec->refcount_off >= 0 && 2214 !refcount_dec_and_test((refcount_t *)(p + rec->refcount_off))) { 2215 /* Object is refcounted and refcount_dec didn't result in 0 2216 * refcount. Return without freeing the object 2217 */ 2218 return; 2219 } 2220 2221 if (rec) 2222 bpf_obj_free_fields(rec, p); 2223 2224 if (percpu) 2225 ma = &bpf_global_percpu_ma; 2226 else 2227 ma = &bpf_global_ma; 2228 bpf_mem_free_rcu(ma, p); 2229 } 2230 2231 __bpf_kfunc void bpf_obj_drop_impl(void *p__alloc, void *meta__ign) 2232 { 2233 struct btf_struct_meta *meta = meta__ign; 2234 void *p = p__alloc; 2235 2236 __bpf_obj_drop_impl(p, meta ? meta->record : NULL, false); 2237 } 2238 2239 __bpf_kfunc void bpf_percpu_obj_drop_impl(void *p__alloc, void *meta__ign) 2240 { 2241 /* The verifier has ensured that meta__ign must be NULL */ 2242 bpf_mem_free_rcu(&bpf_global_percpu_ma, p__alloc); 2243 } 2244 2245 __bpf_kfunc void *bpf_refcount_acquire_impl(void *p__refcounted_kptr, void *meta__ign) 2246 { 2247 struct btf_struct_meta *meta = meta__ign; 2248 struct bpf_refcount *ref; 2249 2250 /* Could just cast directly to refcount_t *, but need some code using 2251 * bpf_refcount type so that it is emitted in vmlinux BTF 2252 */ 2253 ref = (struct bpf_refcount *)(p__refcounted_kptr + meta->record->refcount_off); 2254 if (!refcount_inc_not_zero((refcount_t *)ref)) 2255 return NULL; 2256 2257 /* Verifier strips KF_RET_NULL if input is owned ref, see is_kfunc_ret_null 2258 * in verifier.c 2259 */ 2260 return (void *)p__refcounted_kptr; 2261 } 2262 2263 static int __bpf_list_add(struct bpf_list_node_kern *node, 2264 struct bpf_list_head *head, 2265 bool tail, struct btf_record *rec, u64 off) 2266 { 2267 struct list_head *n = &node->list_head, *h = (void *)head; 2268 2269 /* If list_head was 0-initialized by map, bpf_obj_init_field wasn't 2270 * called on its fields, so init here 2271 */ 2272 if (unlikely(!h->next)) 2273 INIT_LIST_HEAD(h); 2274 2275 /* node->owner != NULL implies !list_empty(n), no need to separately 2276 * check the latter 2277 */ 2278 if (cmpxchg(&node->owner, NULL, BPF_PTR_POISON)) { 2279 /* Only called from BPF prog, no need to migrate_disable */ 2280 __bpf_obj_drop_impl((void *)n - off, rec, false); 2281 return -EINVAL; 2282 } 2283 2284 tail ? list_add_tail(n, h) : list_add(n, h); 2285 WRITE_ONCE(node->owner, head); 2286 2287 return 0; 2288 } 2289 2290 __bpf_kfunc int bpf_list_push_front_impl(struct bpf_list_head *head, 2291 struct bpf_list_node *node, 2292 void *meta__ign, u64 off) 2293 { 2294 struct bpf_list_node_kern *n = (void *)node; 2295 struct btf_struct_meta *meta = meta__ign; 2296 2297 return __bpf_list_add(n, head, false, meta ? meta->record : NULL, off); 2298 } 2299 2300 __bpf_kfunc int bpf_list_push_back_impl(struct bpf_list_head *head, 2301 struct bpf_list_node *node, 2302 void *meta__ign, u64 off) 2303 { 2304 struct bpf_list_node_kern *n = (void *)node; 2305 struct btf_struct_meta *meta = meta__ign; 2306 2307 return __bpf_list_add(n, head, true, meta ? meta->record : NULL, off); 2308 } 2309 2310 static struct bpf_list_node *__bpf_list_del(struct bpf_list_head *head, bool tail) 2311 { 2312 struct list_head *n, *h = (void *)head; 2313 struct bpf_list_node_kern *node; 2314 2315 /* If list_head was 0-initialized by map, bpf_obj_init_field wasn't 2316 * called on its fields, so init here 2317 */ 2318 if (unlikely(!h->next)) 2319 INIT_LIST_HEAD(h); 2320 if (list_empty(h)) 2321 return NULL; 2322 2323 n = tail ? h->prev : h->next; 2324 node = container_of(n, struct bpf_list_node_kern, list_head); 2325 if (WARN_ON_ONCE(READ_ONCE(node->owner) != head)) 2326 return NULL; 2327 2328 list_del_init(n); 2329 WRITE_ONCE(node->owner, NULL); 2330 return (struct bpf_list_node *)n; 2331 } 2332 2333 __bpf_kfunc struct bpf_list_node *bpf_list_pop_front(struct bpf_list_head *head) 2334 { 2335 return __bpf_list_del(head, false); 2336 } 2337 2338 __bpf_kfunc struct bpf_list_node *bpf_list_pop_back(struct bpf_list_head *head) 2339 { 2340 return __bpf_list_del(head, true); 2341 } 2342 2343 __bpf_kfunc struct bpf_list_node *bpf_list_front(struct bpf_list_head *head) 2344 { 2345 struct list_head *h = (struct list_head *)head; 2346 2347 if (list_empty(h) || unlikely(!h->next)) 2348 return NULL; 2349 2350 return (struct bpf_list_node *)h->next; 2351 } 2352 2353 __bpf_kfunc struct bpf_list_node *bpf_list_back(struct bpf_list_head *head) 2354 { 2355 struct list_head *h = (struct list_head *)head; 2356 2357 if (list_empty(h) || unlikely(!h->next)) 2358 return NULL; 2359 2360 return (struct bpf_list_node *)h->prev; 2361 } 2362 2363 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root, 2364 struct bpf_rb_node *node) 2365 { 2366 struct bpf_rb_node_kern *node_internal = (struct bpf_rb_node_kern *)node; 2367 struct rb_root_cached *r = (struct rb_root_cached *)root; 2368 struct rb_node *n = &node_internal->rb_node; 2369 2370 /* node_internal->owner != root implies either RB_EMPTY_NODE(n) or 2371 * n is owned by some other tree. No need to check RB_EMPTY_NODE(n) 2372 */ 2373 if (READ_ONCE(node_internal->owner) != root) 2374 return NULL; 2375 2376 rb_erase_cached(n, r); 2377 RB_CLEAR_NODE(n); 2378 WRITE_ONCE(node_internal->owner, NULL); 2379 return (struct bpf_rb_node *)n; 2380 } 2381 2382 /* Need to copy rbtree_add_cached's logic here because our 'less' is a BPF 2383 * program 2384 */ 2385 static int __bpf_rbtree_add(struct bpf_rb_root *root, 2386 struct bpf_rb_node_kern *node, 2387 void *less, struct btf_record *rec, u64 off) 2388 { 2389 struct rb_node **link = &((struct rb_root_cached *)root)->rb_root.rb_node; 2390 struct rb_node *parent = NULL, *n = &node->rb_node; 2391 bpf_callback_t cb = (bpf_callback_t)less; 2392 bool leftmost = true; 2393 2394 /* node->owner != NULL implies !RB_EMPTY_NODE(n), no need to separately 2395 * check the latter 2396 */ 2397 if (cmpxchg(&node->owner, NULL, BPF_PTR_POISON)) { 2398 /* Only called from BPF prog, no need to migrate_disable */ 2399 __bpf_obj_drop_impl((void *)n - off, rec, false); 2400 return -EINVAL; 2401 } 2402 2403 while (*link) { 2404 parent = *link; 2405 if (cb((uintptr_t)node, (uintptr_t)parent, 0, 0, 0)) { 2406 link = &parent->rb_left; 2407 } else { 2408 link = &parent->rb_right; 2409 leftmost = false; 2410 } 2411 } 2412 2413 rb_link_node(n, parent, link); 2414 rb_insert_color_cached(n, (struct rb_root_cached *)root, leftmost); 2415 WRITE_ONCE(node->owner, root); 2416 return 0; 2417 } 2418 2419 __bpf_kfunc int bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node *node, 2420 bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b), 2421 void *meta__ign, u64 off) 2422 { 2423 struct btf_struct_meta *meta = meta__ign; 2424 struct bpf_rb_node_kern *n = (void *)node; 2425 2426 return __bpf_rbtree_add(root, n, (void *)less, meta ? meta->record : NULL, off); 2427 } 2428 2429 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root) 2430 { 2431 struct rb_root_cached *r = (struct rb_root_cached *)root; 2432 2433 return (struct bpf_rb_node *)rb_first_cached(r); 2434 } 2435 2436 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_root(struct bpf_rb_root *root) 2437 { 2438 struct rb_root_cached *r = (struct rb_root_cached *)root; 2439 2440 return (struct bpf_rb_node *)r->rb_root.rb_node; 2441 } 2442 2443 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_left(struct bpf_rb_root *root, struct bpf_rb_node *node) 2444 { 2445 struct bpf_rb_node_kern *node_internal = (struct bpf_rb_node_kern *)node; 2446 2447 if (READ_ONCE(node_internal->owner) != root) 2448 return NULL; 2449 2450 return (struct bpf_rb_node *)node_internal->rb_node.rb_left; 2451 } 2452 2453 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_right(struct bpf_rb_root *root, struct bpf_rb_node *node) 2454 { 2455 struct bpf_rb_node_kern *node_internal = (struct bpf_rb_node_kern *)node; 2456 2457 if (READ_ONCE(node_internal->owner) != root) 2458 return NULL; 2459 2460 return (struct bpf_rb_node *)node_internal->rb_node.rb_right; 2461 } 2462 2463 /** 2464 * bpf_task_acquire - Acquire a reference to a task. A task acquired by this 2465 * kfunc which is not stored in a map as a kptr, must be released by calling 2466 * bpf_task_release(). 2467 * @p: The task on which a reference is being acquired. 2468 */ 2469 __bpf_kfunc struct task_struct *bpf_task_acquire(struct task_struct *p) 2470 { 2471 if (refcount_inc_not_zero(&p->rcu_users)) 2472 return p; 2473 return NULL; 2474 } 2475 2476 /** 2477 * bpf_task_release - Release the reference acquired on a task. 2478 * @p: The task on which a reference is being released. 2479 */ 2480 __bpf_kfunc void bpf_task_release(struct task_struct *p) 2481 { 2482 put_task_struct_rcu_user(p); 2483 } 2484 2485 __bpf_kfunc void bpf_task_release_dtor(void *p) 2486 { 2487 put_task_struct_rcu_user(p); 2488 } 2489 CFI_NOSEAL(bpf_task_release_dtor); 2490 2491 #ifdef CONFIG_CGROUPS 2492 /** 2493 * bpf_cgroup_acquire - Acquire a reference to a cgroup. A cgroup acquired by 2494 * this kfunc which is not stored in a map as a kptr, must be released by 2495 * calling bpf_cgroup_release(). 2496 * @cgrp: The cgroup on which a reference is being acquired. 2497 */ 2498 __bpf_kfunc struct cgroup *bpf_cgroup_acquire(struct cgroup *cgrp) 2499 { 2500 return cgroup_tryget(cgrp) ? cgrp : NULL; 2501 } 2502 2503 /** 2504 * bpf_cgroup_release - Release the reference acquired on a cgroup. 2505 * If this kfunc is invoked in an RCU read region, the cgroup is guaranteed to 2506 * not be freed until the current grace period has ended, even if its refcount 2507 * drops to 0. 2508 * @cgrp: The cgroup on which a reference is being released. 2509 */ 2510 __bpf_kfunc void bpf_cgroup_release(struct cgroup *cgrp) 2511 { 2512 cgroup_put(cgrp); 2513 } 2514 2515 __bpf_kfunc void bpf_cgroup_release_dtor(void *cgrp) 2516 { 2517 cgroup_put(cgrp); 2518 } 2519 CFI_NOSEAL(bpf_cgroup_release_dtor); 2520 2521 /** 2522 * bpf_cgroup_ancestor - Perform a lookup on an entry in a cgroup's ancestor 2523 * array. A cgroup returned by this kfunc which is not subsequently stored in a 2524 * map, must be released by calling bpf_cgroup_release(). 2525 * @cgrp: The cgroup for which we're performing a lookup. 2526 * @level: The level of ancestor to look up. 2527 */ 2528 __bpf_kfunc struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level) 2529 { 2530 struct cgroup *ancestor; 2531 2532 if (level > cgrp->level || level < 0) 2533 return NULL; 2534 2535 /* cgrp's refcnt could be 0 here, but ancestors can still be accessed */ 2536 ancestor = cgrp->ancestors[level]; 2537 if (!cgroup_tryget(ancestor)) 2538 return NULL; 2539 return ancestor; 2540 } 2541 2542 /** 2543 * bpf_cgroup_from_id - Find a cgroup from its ID. A cgroup returned by this 2544 * kfunc which is not subsequently stored in a map, must be released by calling 2545 * bpf_cgroup_release(). 2546 * @cgid: cgroup id. 2547 */ 2548 __bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid) 2549 { 2550 struct cgroup *cgrp; 2551 2552 cgrp = cgroup_get_from_id(cgid); 2553 if (IS_ERR(cgrp)) 2554 return NULL; 2555 return cgrp; 2556 } 2557 2558 /** 2559 * bpf_task_under_cgroup - wrap task_under_cgroup_hierarchy() as a kfunc, test 2560 * task's membership of cgroup ancestry. 2561 * @task: the task to be tested 2562 * @ancestor: possible ancestor of @task's cgroup 2563 * 2564 * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor. 2565 * It follows all the same rules as cgroup_is_descendant, and only applies 2566 * to the default hierarchy. 2567 */ 2568 __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task, 2569 struct cgroup *ancestor) 2570 { 2571 long ret; 2572 2573 rcu_read_lock(); 2574 ret = task_under_cgroup_hierarchy(task, ancestor); 2575 rcu_read_unlock(); 2576 return ret; 2577 } 2578 2579 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) 2580 { 2581 struct bpf_array *array = container_of(map, struct bpf_array, map); 2582 struct cgroup *cgrp; 2583 2584 if (unlikely(idx >= array->map.max_entries)) 2585 return -E2BIG; 2586 2587 cgrp = READ_ONCE(array->ptrs[idx]); 2588 if (unlikely(!cgrp)) 2589 return -EAGAIN; 2590 2591 return task_under_cgroup_hierarchy(current, cgrp); 2592 } 2593 2594 const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { 2595 .func = bpf_current_task_under_cgroup, 2596 .gpl_only = false, 2597 .ret_type = RET_INTEGER, 2598 .arg1_type = ARG_CONST_MAP_PTR, 2599 .arg2_type = ARG_ANYTHING, 2600 }; 2601 2602 /** 2603 * bpf_task_get_cgroup1 - Acquires the associated cgroup of a task within a 2604 * specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its 2605 * hierarchy ID. 2606 * @task: The target task 2607 * @hierarchy_id: The ID of a cgroup1 hierarchy 2608 * 2609 * On success, the cgroup is returen. On failure, NULL is returned. 2610 */ 2611 __bpf_kfunc struct cgroup * 2612 bpf_task_get_cgroup1(struct task_struct *task, int hierarchy_id) 2613 { 2614 struct cgroup *cgrp = task_get_cgroup1(task, hierarchy_id); 2615 2616 if (IS_ERR(cgrp)) 2617 return NULL; 2618 return cgrp; 2619 } 2620 #endif /* CONFIG_CGROUPS */ 2621 2622 /** 2623 * bpf_task_from_pid - Find a struct task_struct from its pid by looking it up 2624 * in the root pid namespace idr. If a task is returned, it must either be 2625 * stored in a map, or released with bpf_task_release(). 2626 * @pid: The pid of the task being looked up. 2627 */ 2628 __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) 2629 { 2630 struct task_struct *p; 2631 2632 rcu_read_lock(); 2633 p = find_task_by_pid_ns(pid, &init_pid_ns); 2634 if (p) 2635 p = bpf_task_acquire(p); 2636 rcu_read_unlock(); 2637 2638 return p; 2639 } 2640 2641 /** 2642 * bpf_task_from_vpid - Find a struct task_struct from its vpid by looking it up 2643 * in the pid namespace of the current task. If a task is returned, it must 2644 * either be stored in a map, or released with bpf_task_release(). 2645 * @vpid: The vpid of the task being looked up. 2646 */ 2647 __bpf_kfunc struct task_struct *bpf_task_from_vpid(s32 vpid) 2648 { 2649 struct task_struct *p; 2650 2651 rcu_read_lock(); 2652 p = find_task_by_vpid(vpid); 2653 if (p) 2654 p = bpf_task_acquire(p); 2655 rcu_read_unlock(); 2656 2657 return p; 2658 } 2659 2660 /** 2661 * bpf_dynptr_slice() - Obtain a read-only pointer to the dynptr data. 2662 * @p: The dynptr whose data slice to retrieve 2663 * @offset: Offset into the dynptr 2664 * @buffer__opt: User-provided buffer to copy contents into. May be NULL 2665 * @buffer__szk: Size (in bytes) of the buffer if present. This is the 2666 * length of the requested slice. This must be a constant. 2667 * 2668 * For non-skb and non-xdp type dynptrs, there is no difference between 2669 * bpf_dynptr_slice and bpf_dynptr_data. 2670 * 2671 * If buffer__opt is NULL, the call will fail if buffer_opt was needed. 2672 * 2673 * If the intention is to write to the data slice, please use 2674 * bpf_dynptr_slice_rdwr. 2675 * 2676 * The user must check that the returned pointer is not null before using it. 2677 * 2678 * Please note that in the case of skb and xdp dynptrs, bpf_dynptr_slice 2679 * does not change the underlying packet data pointers, so a call to 2680 * bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in 2681 * the bpf program. 2682 * 2683 * Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only 2684 * data slice (can be either direct pointer to the data or a pointer to the user 2685 * provided buffer, with its contents containing the data, if unable to obtain 2686 * direct pointer) 2687 */ 2688 __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr *p, u32 offset, 2689 void *buffer__opt, u32 buffer__szk) 2690 { 2691 const struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 2692 enum bpf_dynptr_type type; 2693 u32 len = buffer__szk; 2694 int err; 2695 2696 if (!ptr->data) 2697 return NULL; 2698 2699 err = bpf_dynptr_check_off_len(ptr, offset, len); 2700 if (err) 2701 return NULL; 2702 2703 type = bpf_dynptr_get_type(ptr); 2704 2705 switch (type) { 2706 case BPF_DYNPTR_TYPE_LOCAL: 2707 case BPF_DYNPTR_TYPE_RINGBUF: 2708 return ptr->data + ptr->offset + offset; 2709 case BPF_DYNPTR_TYPE_SKB: 2710 if (buffer__opt) 2711 return skb_header_pointer(ptr->data, ptr->offset + offset, len, buffer__opt); 2712 else 2713 return skb_pointer_if_linear(ptr->data, ptr->offset + offset, len); 2714 case BPF_DYNPTR_TYPE_XDP: 2715 { 2716 void *xdp_ptr = bpf_xdp_pointer(ptr->data, ptr->offset + offset, len); 2717 if (!IS_ERR_OR_NULL(xdp_ptr)) 2718 return xdp_ptr; 2719 2720 if (!buffer__opt) 2721 return NULL; 2722 bpf_xdp_copy_buf(ptr->data, ptr->offset + offset, buffer__opt, len, false); 2723 return buffer__opt; 2724 } 2725 case BPF_DYNPTR_TYPE_SKB_META: 2726 return bpf_skb_meta_pointer(ptr->data, ptr->offset + offset); 2727 default: 2728 WARN_ONCE(true, "unknown dynptr type %d\n", type); 2729 return NULL; 2730 } 2731 } 2732 2733 /** 2734 * bpf_dynptr_slice_rdwr() - Obtain a writable pointer to the dynptr data. 2735 * @p: The dynptr whose data slice to retrieve 2736 * @offset: Offset into the dynptr 2737 * @buffer__opt: User-provided buffer to copy contents into. May be NULL 2738 * @buffer__szk: Size (in bytes) of the buffer if present. This is the 2739 * length of the requested slice. This must be a constant. 2740 * 2741 * For non-skb and non-xdp type dynptrs, there is no difference between 2742 * bpf_dynptr_slice and bpf_dynptr_data. 2743 * 2744 * If buffer__opt is NULL, the call will fail if buffer_opt was needed. 2745 * 2746 * The returned pointer is writable and may point to either directly the dynptr 2747 * data at the requested offset or to the buffer if unable to obtain a direct 2748 * data pointer to (example: the requested slice is to the paged area of an skb 2749 * packet). In the case where the returned pointer is to the buffer, the user 2750 * is responsible for persisting writes through calling bpf_dynptr_write(). This 2751 * usually looks something like this pattern: 2752 * 2753 * struct eth_hdr *eth = bpf_dynptr_slice_rdwr(&dynptr, 0, buffer, sizeof(buffer)); 2754 * if (!eth) 2755 * return TC_ACT_SHOT; 2756 * 2757 * // mutate eth header // 2758 * 2759 * if (eth == buffer) 2760 * bpf_dynptr_write(&ptr, 0, buffer, sizeof(buffer), 0); 2761 * 2762 * Please note that, as in the example above, the user must check that the 2763 * returned pointer is not null before using it. 2764 * 2765 * Please also note that in the case of skb and xdp dynptrs, bpf_dynptr_slice_rdwr 2766 * does not change the underlying packet data pointers, so a call to 2767 * bpf_dynptr_slice_rdwr will not invalidate any ctx->data/data_end pointers in 2768 * the bpf program. 2769 * 2770 * Return: NULL if the call failed (eg invalid dynptr), pointer to a 2771 * data slice (can be either direct pointer to the data or a pointer to the user 2772 * provided buffer, with its contents containing the data, if unable to obtain 2773 * direct pointer) 2774 */ 2775 __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr *p, u32 offset, 2776 void *buffer__opt, u32 buffer__szk) 2777 { 2778 const struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 2779 2780 if (!ptr->data || __bpf_dynptr_is_rdonly(ptr)) 2781 return NULL; 2782 2783 /* bpf_dynptr_slice_rdwr is the same logic as bpf_dynptr_slice. 2784 * 2785 * For skb-type dynptrs, it is safe to write into the returned pointer 2786 * if the bpf program allows skb data writes. There are two possibilities 2787 * that may occur when calling bpf_dynptr_slice_rdwr: 2788 * 2789 * 1) The requested slice is in the head of the skb. In this case, the 2790 * returned pointer is directly to skb data, and if the skb is cloned, the 2791 * verifier will have uncloned it (see bpf_unclone_prologue()) already. 2792 * The pointer can be directly written into. 2793 * 2794 * 2) Some portion of the requested slice is in the paged buffer area. 2795 * In this case, the requested data will be copied out into the buffer 2796 * and the returned pointer will be a pointer to the buffer. The skb 2797 * will not be pulled. To persist the write, the user will need to call 2798 * bpf_dynptr_write(), which will pull the skb and commit the write. 2799 * 2800 * Similarly for xdp programs, if the requested slice is not across xdp 2801 * fragments, then a direct pointer will be returned, otherwise the data 2802 * will be copied out into the buffer and the user will need to call 2803 * bpf_dynptr_write() to commit changes. 2804 */ 2805 return bpf_dynptr_slice(p, offset, buffer__opt, buffer__szk); 2806 } 2807 2808 __bpf_kfunc int bpf_dynptr_adjust(const struct bpf_dynptr *p, u32 start, u32 end) 2809 { 2810 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 2811 u32 size; 2812 2813 if (!ptr->data || start > end) 2814 return -EINVAL; 2815 2816 size = __bpf_dynptr_size(ptr); 2817 2818 if (start > size || end > size) 2819 return -ERANGE; 2820 2821 ptr->offset += start; 2822 bpf_dynptr_set_size(ptr, end - start); 2823 2824 return 0; 2825 } 2826 2827 __bpf_kfunc bool bpf_dynptr_is_null(const struct bpf_dynptr *p) 2828 { 2829 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 2830 2831 return !ptr->data; 2832 } 2833 2834 __bpf_kfunc bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *p) 2835 { 2836 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 2837 2838 if (!ptr->data) 2839 return false; 2840 2841 return __bpf_dynptr_is_rdonly(ptr); 2842 } 2843 2844 __bpf_kfunc __u32 bpf_dynptr_size(const struct bpf_dynptr *p) 2845 { 2846 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 2847 2848 if (!ptr->data) 2849 return -EINVAL; 2850 2851 return __bpf_dynptr_size(ptr); 2852 } 2853 2854 __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p, 2855 struct bpf_dynptr *clone__uninit) 2856 { 2857 struct bpf_dynptr_kern *clone = (struct bpf_dynptr_kern *)clone__uninit; 2858 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 2859 2860 if (!ptr->data) { 2861 bpf_dynptr_set_null(clone); 2862 return -EINVAL; 2863 } 2864 2865 *clone = *ptr; 2866 2867 return 0; 2868 } 2869 2870 /** 2871 * bpf_dynptr_copy() - Copy data from one dynptr to another. 2872 * @dst_ptr: Destination dynptr - where data should be copied to 2873 * @dst_off: Offset into the destination dynptr 2874 * @src_ptr: Source dynptr - where data should be copied from 2875 * @src_off: Offset into the source dynptr 2876 * @size: Length of the data to copy from source to destination 2877 * 2878 * Copies data from source dynptr to destination dynptr. 2879 * Returns 0 on success; negative error, otherwise. 2880 */ 2881 __bpf_kfunc int bpf_dynptr_copy(struct bpf_dynptr *dst_ptr, u32 dst_off, 2882 struct bpf_dynptr *src_ptr, u32 src_off, u32 size) 2883 { 2884 struct bpf_dynptr_kern *dst = (struct bpf_dynptr_kern *)dst_ptr; 2885 struct bpf_dynptr_kern *src = (struct bpf_dynptr_kern *)src_ptr; 2886 void *src_slice, *dst_slice; 2887 char buf[256]; 2888 u32 off; 2889 2890 src_slice = bpf_dynptr_slice(src_ptr, src_off, NULL, size); 2891 dst_slice = bpf_dynptr_slice_rdwr(dst_ptr, dst_off, NULL, size); 2892 2893 if (src_slice && dst_slice) { 2894 memmove(dst_slice, src_slice, size); 2895 return 0; 2896 } 2897 2898 if (src_slice) 2899 return __bpf_dynptr_write(dst, dst_off, src_slice, size, 0); 2900 2901 if (dst_slice) 2902 return __bpf_dynptr_read(dst_slice, size, src, src_off, 0); 2903 2904 if (bpf_dynptr_check_off_len(dst, dst_off, size) || 2905 bpf_dynptr_check_off_len(src, src_off, size)) 2906 return -E2BIG; 2907 2908 off = 0; 2909 while (off < size) { 2910 u32 chunk_sz = min_t(u32, sizeof(buf), size - off); 2911 int err; 2912 2913 err = __bpf_dynptr_read(buf, chunk_sz, src, src_off + off, 0); 2914 if (err) 2915 return err; 2916 err = __bpf_dynptr_write(dst, dst_off + off, buf, chunk_sz, 0); 2917 if (err) 2918 return err; 2919 2920 off += chunk_sz; 2921 } 2922 return 0; 2923 } 2924 2925 /** 2926 * bpf_dynptr_memset() - Fill dynptr memory with a constant byte. 2927 * @p: Destination dynptr - where data will be filled 2928 * @offset: Offset into the dynptr to start filling from 2929 * @size: Number of bytes to fill 2930 * @val: Constant byte to fill the memory with 2931 * 2932 * Fills the @size bytes of the memory area pointed to by @p 2933 * at @offset with the constant byte @val. 2934 * Returns 0 on success; negative error, otherwise. 2935 */ 2936 __bpf_kfunc int bpf_dynptr_memset(struct bpf_dynptr *p, u32 offset, u32 size, u8 val) 2937 { 2938 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 2939 u32 chunk_sz, write_off; 2940 char buf[256]; 2941 void* slice; 2942 int err; 2943 2944 slice = bpf_dynptr_slice_rdwr(p, offset, NULL, size); 2945 if (likely(slice)) { 2946 memset(slice, val, size); 2947 return 0; 2948 } 2949 2950 if (__bpf_dynptr_is_rdonly(ptr)) 2951 return -EINVAL; 2952 2953 err = bpf_dynptr_check_off_len(ptr, offset, size); 2954 if (err) 2955 return err; 2956 2957 /* Non-linear data under the dynptr, write from a local buffer */ 2958 chunk_sz = min_t(u32, sizeof(buf), size); 2959 memset(buf, val, chunk_sz); 2960 2961 for (write_off = 0; write_off < size; write_off += chunk_sz) { 2962 chunk_sz = min_t(u32, sizeof(buf), size - write_off); 2963 err = __bpf_dynptr_write(ptr, offset + write_off, buf, chunk_sz, 0); 2964 if (err) 2965 return err; 2966 } 2967 2968 return 0; 2969 } 2970 2971 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj) 2972 { 2973 return obj; 2974 } 2975 2976 __bpf_kfunc void *bpf_rdonly_cast(const void *obj__ign, u32 btf_id__k) 2977 { 2978 return (void *)obj__ign; 2979 } 2980 2981 __bpf_kfunc void bpf_rcu_read_lock(void) 2982 { 2983 rcu_read_lock(); 2984 } 2985 2986 __bpf_kfunc void bpf_rcu_read_unlock(void) 2987 { 2988 rcu_read_unlock(); 2989 } 2990 2991 struct bpf_throw_ctx { 2992 struct bpf_prog_aux *aux; 2993 u64 sp; 2994 u64 bp; 2995 int cnt; 2996 }; 2997 2998 static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp) 2999 { 3000 struct bpf_throw_ctx *ctx = cookie; 3001 struct bpf_prog *prog; 3002 3003 /* 3004 * The RCU read lock is held to safely traverse the latch tree, but we 3005 * don't need its protection when accessing the prog, since it has an 3006 * active stack frame on the current stack trace, and won't disappear. 3007 */ 3008 rcu_read_lock(); 3009 prog = bpf_prog_ksym_find(ip); 3010 rcu_read_unlock(); 3011 if (!prog) 3012 return !ctx->cnt; 3013 ctx->cnt++; 3014 if (bpf_is_subprog(prog)) 3015 return true; 3016 ctx->aux = prog->aux; 3017 ctx->sp = sp; 3018 ctx->bp = bp; 3019 return false; 3020 } 3021 3022 __bpf_kfunc void bpf_throw(u64 cookie) 3023 { 3024 struct bpf_throw_ctx ctx = {}; 3025 3026 arch_bpf_stack_walk(bpf_stack_walker, &ctx); 3027 WARN_ON_ONCE(!ctx.aux); 3028 if (ctx.aux) 3029 WARN_ON_ONCE(!ctx.aux->exception_boundary); 3030 WARN_ON_ONCE(!ctx.bp); 3031 WARN_ON_ONCE(!ctx.cnt); 3032 /* Prevent KASAN false positives for CONFIG_KASAN_STACK by unpoisoning 3033 * deeper stack depths than ctx.sp as we do not return from bpf_throw, 3034 * which skips compiler generated instrumentation to do the same. 3035 */ 3036 kasan_unpoison_task_stack_below((void *)(long)ctx.sp); 3037 ctx.aux->bpf_exception_cb(cookie, ctx.sp, ctx.bp, 0, 0); 3038 WARN(1, "A call to BPF exception callback should never return\n"); 3039 } 3040 3041 __bpf_kfunc int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) 3042 { 3043 struct bpf_async_kern *async = (struct bpf_async_kern *)wq; 3044 struct bpf_map *map = p__map; 3045 3046 BUILD_BUG_ON(sizeof(struct bpf_async_kern) > sizeof(struct bpf_wq)); 3047 BUILD_BUG_ON(__alignof__(struct bpf_async_kern) != __alignof__(struct bpf_wq)); 3048 3049 if (flags) 3050 return -EINVAL; 3051 3052 return __bpf_async_init(async, map, flags, BPF_ASYNC_TYPE_WQ); 3053 } 3054 3055 __bpf_kfunc int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) 3056 { 3057 struct bpf_async_kern *async = (struct bpf_async_kern *)wq; 3058 struct bpf_work *w; 3059 3060 if (in_nmi()) 3061 return -EOPNOTSUPP; 3062 if (flags) 3063 return -EINVAL; 3064 w = READ_ONCE(async->work); 3065 if (!w || !READ_ONCE(w->cb.prog)) 3066 return -EINVAL; 3067 3068 schedule_work(&w->work); 3069 return 0; 3070 } 3071 3072 __bpf_kfunc int bpf_wq_set_callback_impl(struct bpf_wq *wq, 3073 int (callback_fn)(void *map, int *key, void *value), 3074 unsigned int flags, 3075 void *aux__prog) 3076 { 3077 struct bpf_prog_aux *aux = (struct bpf_prog_aux *)aux__prog; 3078 struct bpf_async_kern *async = (struct bpf_async_kern *)wq; 3079 3080 if (flags) 3081 return -EINVAL; 3082 3083 return __bpf_async_set_callback(async, callback_fn, aux, flags, BPF_ASYNC_TYPE_WQ); 3084 } 3085 3086 __bpf_kfunc void bpf_preempt_disable(void) 3087 { 3088 preempt_disable(); 3089 } 3090 3091 __bpf_kfunc void bpf_preempt_enable(void) 3092 { 3093 preempt_enable(); 3094 } 3095 3096 struct bpf_iter_bits { 3097 __u64 __opaque[2]; 3098 } __aligned(8); 3099 3100 #define BITS_ITER_NR_WORDS_MAX 511 3101 3102 struct bpf_iter_bits_kern { 3103 union { 3104 __u64 *bits; 3105 __u64 bits_copy; 3106 }; 3107 int nr_bits; 3108 int bit; 3109 } __aligned(8); 3110 3111 /* On 64-bit hosts, unsigned long and u64 have the same size, so passing 3112 * a u64 pointer and an unsigned long pointer to find_next_bit() will 3113 * return the same result, as both point to the same 8-byte area. 3114 * 3115 * For 32-bit little-endian hosts, using a u64 pointer or unsigned long 3116 * pointer also makes no difference. This is because the first iterated 3117 * unsigned long is composed of bits 0-31 of the u64 and the second unsigned 3118 * long is composed of bits 32-63 of the u64. 3119 * 3120 * However, for 32-bit big-endian hosts, this is not the case. The first 3121 * iterated unsigned long will be bits 32-63 of the u64, so swap these two 3122 * ulong values within the u64. 3123 */ 3124 static void swap_ulong_in_u64(u64 *bits, unsigned int nr) 3125 { 3126 #if (BITS_PER_LONG == 32) && defined(__BIG_ENDIAN) 3127 unsigned int i; 3128 3129 for (i = 0; i < nr; i++) 3130 bits[i] = (bits[i] >> 32) | ((u64)(u32)bits[i] << 32); 3131 #endif 3132 } 3133 3134 /** 3135 * bpf_iter_bits_new() - Initialize a new bits iterator for a given memory area 3136 * @it: The new bpf_iter_bits to be created 3137 * @unsafe_ptr__ign: A pointer pointing to a memory area to be iterated over 3138 * @nr_words: The size of the specified memory area, measured in 8-byte units. 3139 * The maximum value of @nr_words is @BITS_ITER_NR_WORDS_MAX. This limit may be 3140 * further reduced by the BPF memory allocator implementation. 3141 * 3142 * This function initializes a new bpf_iter_bits structure for iterating over 3143 * a memory area which is specified by the @unsafe_ptr__ign and @nr_words. It 3144 * copies the data of the memory area to the newly created bpf_iter_bits @it for 3145 * subsequent iteration operations. 3146 * 3147 * On success, 0 is returned. On failure, ERR is returned. 3148 */ 3149 __bpf_kfunc int 3150 bpf_iter_bits_new(struct bpf_iter_bits *it, const u64 *unsafe_ptr__ign, u32 nr_words) 3151 { 3152 struct bpf_iter_bits_kern *kit = (void *)it; 3153 u32 nr_bytes = nr_words * sizeof(u64); 3154 u32 nr_bits = BYTES_TO_BITS(nr_bytes); 3155 int err; 3156 3157 BUILD_BUG_ON(sizeof(struct bpf_iter_bits_kern) != sizeof(struct bpf_iter_bits)); 3158 BUILD_BUG_ON(__alignof__(struct bpf_iter_bits_kern) != 3159 __alignof__(struct bpf_iter_bits)); 3160 3161 kit->nr_bits = 0; 3162 kit->bits_copy = 0; 3163 kit->bit = -1; 3164 3165 if (!unsafe_ptr__ign || !nr_words) 3166 return -EINVAL; 3167 if (nr_words > BITS_ITER_NR_WORDS_MAX) 3168 return -E2BIG; 3169 3170 /* Optimization for u64 mask */ 3171 if (nr_bits == 64) { 3172 err = bpf_probe_read_kernel_common(&kit->bits_copy, nr_bytes, unsafe_ptr__ign); 3173 if (err) 3174 return -EFAULT; 3175 3176 swap_ulong_in_u64(&kit->bits_copy, nr_words); 3177 3178 kit->nr_bits = nr_bits; 3179 return 0; 3180 } 3181 3182 if (bpf_mem_alloc_check_size(false, nr_bytes)) 3183 return -E2BIG; 3184 3185 /* Fallback to memalloc */ 3186 kit->bits = bpf_mem_alloc(&bpf_global_ma, nr_bytes); 3187 if (!kit->bits) 3188 return -ENOMEM; 3189 3190 err = bpf_probe_read_kernel_common(kit->bits, nr_bytes, unsafe_ptr__ign); 3191 if (err) { 3192 bpf_mem_free(&bpf_global_ma, kit->bits); 3193 return err; 3194 } 3195 3196 swap_ulong_in_u64(kit->bits, nr_words); 3197 3198 kit->nr_bits = nr_bits; 3199 return 0; 3200 } 3201 3202 /** 3203 * bpf_iter_bits_next() - Get the next bit in a bpf_iter_bits 3204 * @it: The bpf_iter_bits to be checked 3205 * 3206 * This function returns a pointer to a number representing the value of the 3207 * next bit in the bits. 3208 * 3209 * If there are no further bits available, it returns NULL. 3210 */ 3211 __bpf_kfunc int *bpf_iter_bits_next(struct bpf_iter_bits *it) 3212 { 3213 struct bpf_iter_bits_kern *kit = (void *)it; 3214 int bit = kit->bit, nr_bits = kit->nr_bits; 3215 const void *bits; 3216 3217 if (!nr_bits || bit >= nr_bits) 3218 return NULL; 3219 3220 bits = nr_bits == 64 ? &kit->bits_copy : kit->bits; 3221 bit = find_next_bit(bits, nr_bits, bit + 1); 3222 if (bit >= nr_bits) { 3223 kit->bit = bit; 3224 return NULL; 3225 } 3226 3227 kit->bit = bit; 3228 return &kit->bit; 3229 } 3230 3231 /** 3232 * bpf_iter_bits_destroy() - Destroy a bpf_iter_bits 3233 * @it: The bpf_iter_bits to be destroyed 3234 * 3235 * Destroy the resource associated with the bpf_iter_bits. 3236 */ 3237 __bpf_kfunc void bpf_iter_bits_destroy(struct bpf_iter_bits *it) 3238 { 3239 struct bpf_iter_bits_kern *kit = (void *)it; 3240 3241 if (kit->nr_bits <= 64) 3242 return; 3243 bpf_mem_free(&bpf_global_ma, kit->bits); 3244 } 3245 3246 /** 3247 * bpf_copy_from_user_str() - Copy a string from an unsafe user address 3248 * @dst: Destination address, in kernel space. This buffer must be 3249 * at least @dst__sz bytes long. 3250 * @dst__sz: Maximum number of bytes to copy, includes the trailing NUL. 3251 * @unsafe_ptr__ign: Source address, in user space. 3252 * @flags: The only supported flag is BPF_F_PAD_ZEROS 3253 * 3254 * Copies a NUL-terminated string from userspace to BPF space. If user string is 3255 * too long this will still ensure zero termination in the dst buffer unless 3256 * buffer size is 0. 3257 * 3258 * If BPF_F_PAD_ZEROS flag is set, memset the tail of @dst to 0 on success and 3259 * memset all of @dst on failure. 3260 */ 3261 __bpf_kfunc int bpf_copy_from_user_str(void *dst, u32 dst__sz, const void __user *unsafe_ptr__ign, u64 flags) 3262 { 3263 int ret; 3264 3265 if (unlikely(flags & ~BPF_F_PAD_ZEROS)) 3266 return -EINVAL; 3267 3268 if (unlikely(!dst__sz)) 3269 return 0; 3270 3271 ret = strncpy_from_user(dst, unsafe_ptr__ign, dst__sz - 1); 3272 if (ret < 0) { 3273 if (flags & BPF_F_PAD_ZEROS) 3274 memset((char *)dst, 0, dst__sz); 3275 3276 return ret; 3277 } 3278 3279 if (flags & BPF_F_PAD_ZEROS) 3280 memset((char *)dst + ret, 0, dst__sz - ret); 3281 else 3282 ((char *)dst)[ret] = '\0'; 3283 3284 return ret + 1; 3285 } 3286 3287 /** 3288 * bpf_copy_from_user_task_str() - Copy a string from an task's address space 3289 * @dst: Destination address, in kernel space. This buffer must be 3290 * at least @dst__sz bytes long. 3291 * @dst__sz: Maximum number of bytes to copy, includes the trailing NUL. 3292 * @unsafe_ptr__ign: Source address in the task's address space. 3293 * @tsk: The task whose address space will be used 3294 * @flags: The only supported flag is BPF_F_PAD_ZEROS 3295 * 3296 * Copies a NUL terminated string from a task's address space to @dst__sz 3297 * buffer. If user string is too long this will still ensure zero termination 3298 * in the @dst__sz buffer unless buffer size is 0. 3299 * 3300 * If BPF_F_PAD_ZEROS flag is set, memset the tail of @dst__sz to 0 on success 3301 * and memset all of @dst__sz on failure. 3302 * 3303 * Return: The number of copied bytes on success including the NUL terminator. 3304 * A negative error code on failure. 3305 */ 3306 __bpf_kfunc int bpf_copy_from_user_task_str(void *dst, u32 dst__sz, 3307 const void __user *unsafe_ptr__ign, 3308 struct task_struct *tsk, u64 flags) 3309 { 3310 int ret; 3311 3312 if (unlikely(flags & ~BPF_F_PAD_ZEROS)) 3313 return -EINVAL; 3314 3315 if (unlikely(dst__sz == 0)) 3316 return 0; 3317 3318 ret = copy_remote_vm_str(tsk, (unsigned long)unsafe_ptr__ign, dst, dst__sz, 0); 3319 if (ret < 0) { 3320 if (flags & BPF_F_PAD_ZEROS) 3321 memset(dst, 0, dst__sz); 3322 return ret; 3323 } 3324 3325 if (flags & BPF_F_PAD_ZEROS) 3326 memset(dst + ret, 0, dst__sz - ret); 3327 3328 return ret + 1; 3329 } 3330 3331 /* Keep unsinged long in prototype so that kfunc is usable when emitted to 3332 * vmlinux.h in BPF programs directly, but note that while in BPF prog, the 3333 * unsigned long always points to 8-byte region on stack, the kernel may only 3334 * read and write the 4-bytes on 32-bit. 3335 */ 3336 __bpf_kfunc void bpf_local_irq_save(unsigned long *flags__irq_flag) 3337 { 3338 local_irq_save(*flags__irq_flag); 3339 } 3340 3341 __bpf_kfunc void bpf_local_irq_restore(unsigned long *flags__irq_flag) 3342 { 3343 local_irq_restore(*flags__irq_flag); 3344 } 3345 3346 __bpf_kfunc void __bpf_trap(void) 3347 { 3348 } 3349 3350 /* 3351 * Kfuncs for string operations. 3352 * 3353 * Since strings are not necessarily %NUL-terminated, we cannot directly call 3354 * in-kernel implementations. Instead, we open-code the implementations using 3355 * __get_kernel_nofault instead of plain dereference to make them safe. 3356 */ 3357 3358 /** 3359 * bpf_strcmp - Compare two strings 3360 * @s1__ign: One string 3361 * @s2__ign: Another string 3362 * 3363 * Return: 3364 * * %0 - Strings are equal 3365 * * %-1 - @s1__ign is smaller 3366 * * %1 - @s2__ign is smaller 3367 * * %-EFAULT - Cannot read one of the strings 3368 * * %-E2BIG - One of strings is too large 3369 * * %-ERANGE - One of strings is outside of kernel address space 3370 */ 3371 __bpf_kfunc int bpf_strcmp(const char *s1__ign, const char *s2__ign) 3372 { 3373 char c1, c2; 3374 int i; 3375 3376 if (!copy_from_kernel_nofault_allowed(s1__ign, 1) || 3377 !copy_from_kernel_nofault_allowed(s2__ign, 1)) { 3378 return -ERANGE; 3379 } 3380 3381 guard(pagefault)(); 3382 for (i = 0; i < XATTR_SIZE_MAX; i++) { 3383 __get_kernel_nofault(&c1, s1__ign, char, err_out); 3384 __get_kernel_nofault(&c2, s2__ign, char, err_out); 3385 if (c1 != c2) 3386 return c1 < c2 ? -1 : 1; 3387 if (c1 == '\0') 3388 return 0; 3389 s1__ign++; 3390 s2__ign++; 3391 } 3392 return -E2BIG; 3393 err_out: 3394 return -EFAULT; 3395 } 3396 3397 /** 3398 * bpf_strnchr - Find a character in a length limited string 3399 * @s__ign: The string to be searched 3400 * @count: The number of characters to be searched 3401 * @c: The character to search for 3402 * 3403 * Note that the %NUL-terminator is considered part of the string, and can 3404 * be searched for. 3405 * 3406 * Return: 3407 * * >=0 - Index of the first occurrence of @c within @s__ign 3408 * * %-ENOENT - @c not found in the first @count characters of @s__ign 3409 * * %-EFAULT - Cannot read @s__ign 3410 * * %-E2BIG - @s__ign is too large 3411 * * %-ERANGE - @s__ign is outside of kernel address space 3412 */ 3413 __bpf_kfunc int bpf_strnchr(const char *s__ign, size_t count, char c) 3414 { 3415 char sc; 3416 int i; 3417 3418 if (!copy_from_kernel_nofault_allowed(s__ign, 1)) 3419 return -ERANGE; 3420 3421 guard(pagefault)(); 3422 for (i = 0; i < count && i < XATTR_SIZE_MAX; i++) { 3423 __get_kernel_nofault(&sc, s__ign, char, err_out); 3424 if (sc == c) 3425 return i; 3426 if (sc == '\0') 3427 return -ENOENT; 3428 s__ign++; 3429 } 3430 return i == XATTR_SIZE_MAX ? -E2BIG : -ENOENT; 3431 err_out: 3432 return -EFAULT; 3433 } 3434 3435 /** 3436 * bpf_strchr - Find the first occurrence of a character in a string 3437 * @s__ign: The string to be searched 3438 * @c: The character to search for 3439 * 3440 * Note that the %NUL-terminator is considered part of the string, and can 3441 * be searched for. 3442 * 3443 * Return: 3444 * * >=0 - The index of the first occurrence of @c within @s__ign 3445 * * %-ENOENT - @c not found in @s__ign 3446 * * %-EFAULT - Cannot read @s__ign 3447 * * %-E2BIG - @s__ign is too large 3448 * * %-ERANGE - @s__ign is outside of kernel address space 3449 */ 3450 __bpf_kfunc int bpf_strchr(const char *s__ign, char c) 3451 { 3452 return bpf_strnchr(s__ign, XATTR_SIZE_MAX, c); 3453 } 3454 3455 /** 3456 * bpf_strchrnul - Find and return a character in a string, or end of string 3457 * @s__ign: The string to be searched 3458 * @c: The character to search for 3459 * 3460 * Return: 3461 * * >=0 - Index of the first occurrence of @c within @s__ign or index of 3462 * the null byte at the end of @s__ign when @c is not found 3463 * * %-EFAULT - Cannot read @s__ign 3464 * * %-E2BIG - @s__ign is too large 3465 * * %-ERANGE - @s__ign is outside of kernel address space 3466 */ 3467 __bpf_kfunc int bpf_strchrnul(const char *s__ign, char c) 3468 { 3469 char sc; 3470 int i; 3471 3472 if (!copy_from_kernel_nofault_allowed(s__ign, 1)) 3473 return -ERANGE; 3474 3475 guard(pagefault)(); 3476 for (i = 0; i < XATTR_SIZE_MAX; i++) { 3477 __get_kernel_nofault(&sc, s__ign, char, err_out); 3478 if (sc == '\0' || sc == c) 3479 return i; 3480 s__ign++; 3481 } 3482 return -E2BIG; 3483 err_out: 3484 return -EFAULT; 3485 } 3486 3487 /** 3488 * bpf_strrchr - Find the last occurrence of a character in a string 3489 * @s__ign: The string to be searched 3490 * @c: The character to search for 3491 * 3492 * Return: 3493 * * >=0 - Index of the last occurrence of @c within @s__ign 3494 * * %-ENOENT - @c not found in @s__ign 3495 * * %-EFAULT - Cannot read @s__ign 3496 * * %-E2BIG - @s__ign is too large 3497 * * %-ERANGE - @s__ign is outside of kernel address space 3498 */ 3499 __bpf_kfunc int bpf_strrchr(const char *s__ign, int c) 3500 { 3501 char sc; 3502 int i, last = -ENOENT; 3503 3504 if (!copy_from_kernel_nofault_allowed(s__ign, 1)) 3505 return -ERANGE; 3506 3507 guard(pagefault)(); 3508 for (i = 0; i < XATTR_SIZE_MAX; i++) { 3509 __get_kernel_nofault(&sc, s__ign, char, err_out); 3510 if (sc == c) 3511 last = i; 3512 if (sc == '\0') 3513 return last; 3514 s__ign++; 3515 } 3516 return -E2BIG; 3517 err_out: 3518 return -EFAULT; 3519 } 3520 3521 /** 3522 * bpf_strnlen - Calculate the length of a length-limited string 3523 * @s__ign: The string 3524 * @count: The maximum number of characters to count 3525 * 3526 * Return: 3527 * * >=0 - The length of @s__ign 3528 * * %-EFAULT - Cannot read @s__ign 3529 * * %-E2BIG - @s__ign is too large 3530 * * %-ERANGE - @s__ign is outside of kernel address space 3531 */ 3532 __bpf_kfunc int bpf_strnlen(const char *s__ign, size_t count) 3533 { 3534 char c; 3535 int i; 3536 3537 if (!copy_from_kernel_nofault_allowed(s__ign, 1)) 3538 return -ERANGE; 3539 3540 guard(pagefault)(); 3541 for (i = 0; i < count && i < XATTR_SIZE_MAX; i++) { 3542 __get_kernel_nofault(&c, s__ign, char, err_out); 3543 if (c == '\0') 3544 return i; 3545 s__ign++; 3546 } 3547 return i == XATTR_SIZE_MAX ? -E2BIG : i; 3548 err_out: 3549 return -EFAULT; 3550 } 3551 3552 /** 3553 * bpf_strlen - Calculate the length of a string 3554 * @s__ign: The string 3555 * 3556 * Return: 3557 * * >=0 - The length of @s__ign 3558 * * %-EFAULT - Cannot read @s__ign 3559 * * %-E2BIG - @s__ign is too large 3560 * * %-ERANGE - @s__ign is outside of kernel address space 3561 */ 3562 __bpf_kfunc int bpf_strlen(const char *s__ign) 3563 { 3564 return bpf_strnlen(s__ign, XATTR_SIZE_MAX); 3565 } 3566 3567 /** 3568 * bpf_strspn - Calculate the length of the initial substring of @s__ign which 3569 * only contains letters in @accept__ign 3570 * @s__ign: The string to be searched 3571 * @accept__ign: The string to search for 3572 * 3573 * Return: 3574 * * >=0 - The length of the initial substring of @s__ign which only 3575 * contains letters from @accept__ign 3576 * * %-EFAULT - Cannot read one of the strings 3577 * * %-E2BIG - One of the strings is too large 3578 * * %-ERANGE - One of the strings is outside of kernel address space 3579 */ 3580 __bpf_kfunc int bpf_strspn(const char *s__ign, const char *accept__ign) 3581 { 3582 char cs, ca; 3583 int i, j; 3584 3585 if (!copy_from_kernel_nofault_allowed(s__ign, 1) || 3586 !copy_from_kernel_nofault_allowed(accept__ign, 1)) { 3587 return -ERANGE; 3588 } 3589 3590 guard(pagefault)(); 3591 for (i = 0; i < XATTR_SIZE_MAX; i++) { 3592 __get_kernel_nofault(&cs, s__ign, char, err_out); 3593 if (cs == '\0') 3594 return i; 3595 for (j = 0; j < XATTR_SIZE_MAX; j++) { 3596 __get_kernel_nofault(&ca, accept__ign + j, char, err_out); 3597 if (cs == ca || ca == '\0') 3598 break; 3599 } 3600 if (j == XATTR_SIZE_MAX) 3601 return -E2BIG; 3602 if (ca == '\0') 3603 return i; 3604 s__ign++; 3605 } 3606 return -E2BIG; 3607 err_out: 3608 return -EFAULT; 3609 } 3610 3611 /** 3612 * bpf_strcspn - Calculate the length of the initial substring of @s__ign which 3613 * does not contain letters in @reject__ign 3614 * @s__ign: The string to be searched 3615 * @reject__ign: The string to search for 3616 * 3617 * Return: 3618 * * >=0 - The length of the initial substring of @s__ign which does not 3619 * contain letters from @reject__ign 3620 * * %-EFAULT - Cannot read one of the strings 3621 * * %-E2BIG - One of the strings is too large 3622 * * %-ERANGE - One of the strings is outside of kernel address space 3623 */ 3624 __bpf_kfunc int bpf_strcspn(const char *s__ign, const char *reject__ign) 3625 { 3626 char cs, cr; 3627 int i, j; 3628 3629 if (!copy_from_kernel_nofault_allowed(s__ign, 1) || 3630 !copy_from_kernel_nofault_allowed(reject__ign, 1)) { 3631 return -ERANGE; 3632 } 3633 3634 guard(pagefault)(); 3635 for (i = 0; i < XATTR_SIZE_MAX; i++) { 3636 __get_kernel_nofault(&cs, s__ign, char, err_out); 3637 if (cs == '\0') 3638 return i; 3639 for (j = 0; j < XATTR_SIZE_MAX; j++) { 3640 __get_kernel_nofault(&cr, reject__ign + j, char, err_out); 3641 if (cs == cr || cr == '\0') 3642 break; 3643 } 3644 if (j == XATTR_SIZE_MAX) 3645 return -E2BIG; 3646 if (cr != '\0') 3647 return i; 3648 s__ign++; 3649 } 3650 return -E2BIG; 3651 err_out: 3652 return -EFAULT; 3653 } 3654 3655 /** 3656 * bpf_strnstr - Find the first substring in a length-limited string 3657 * @s1__ign: The string to be searched 3658 * @s2__ign: The string to search for 3659 * @len: the maximum number of characters to search 3660 * 3661 * Return: 3662 * * >=0 - Index of the first character of the first occurrence of @s2__ign 3663 * within the first @len characters of @s1__ign 3664 * * %-ENOENT - @s2__ign not found in the first @len characters of @s1__ign 3665 * * %-EFAULT - Cannot read one of the strings 3666 * * %-E2BIG - One of the strings is too large 3667 * * %-ERANGE - One of the strings is outside of kernel address space 3668 */ 3669 __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len) 3670 { 3671 char c1, c2; 3672 int i, j; 3673 3674 if (!copy_from_kernel_nofault_allowed(s1__ign, 1) || 3675 !copy_from_kernel_nofault_allowed(s2__ign, 1)) { 3676 return -ERANGE; 3677 } 3678 3679 guard(pagefault)(); 3680 for (i = 0; i < XATTR_SIZE_MAX; i++) { 3681 for (j = 0; i + j <= len && j < XATTR_SIZE_MAX; j++) { 3682 __get_kernel_nofault(&c2, s2__ign + j, char, err_out); 3683 if (c2 == '\0') 3684 return i; 3685 /* 3686 * We allow reading an extra byte from s2 (note the 3687 * `i + j <= len` above) to cover the case when s2 is 3688 * a suffix of the first len chars of s1. 3689 */ 3690 if (i + j == len) 3691 break; 3692 __get_kernel_nofault(&c1, s1__ign + j, char, err_out); 3693 if (c1 == '\0') 3694 return -ENOENT; 3695 if (c1 != c2) 3696 break; 3697 } 3698 if (j == XATTR_SIZE_MAX) 3699 return -E2BIG; 3700 if (i + j == len) 3701 return -ENOENT; 3702 s1__ign++; 3703 } 3704 return -E2BIG; 3705 err_out: 3706 return -EFAULT; 3707 } 3708 3709 /** 3710 * bpf_strstr - Find the first substring in a string 3711 * @s1__ign: The string to be searched 3712 * @s2__ign: The string to search for 3713 * 3714 * Return: 3715 * * >=0 - Index of the first character of the first occurrence of @s2__ign 3716 * within @s1__ign 3717 * * %-ENOENT - @s2__ign is not a substring of @s1__ign 3718 * * %-EFAULT - Cannot read one of the strings 3719 * * %-E2BIG - One of the strings is too large 3720 * * %-ERANGE - One of the strings is outside of kernel address space 3721 */ 3722 __bpf_kfunc int bpf_strstr(const char *s1__ign, const char *s2__ign) 3723 { 3724 return bpf_strnstr(s1__ign, s2__ign, XATTR_SIZE_MAX); 3725 } 3726 3727 __bpf_kfunc_end_defs(); 3728 3729 BTF_KFUNCS_START(generic_btf_ids) 3730 #ifdef CONFIG_CRASH_DUMP 3731 BTF_ID_FLAGS(func, crash_kexec, KF_DESTRUCTIVE) 3732 #endif 3733 BTF_ID_FLAGS(func, bpf_obj_new_impl, KF_ACQUIRE | KF_RET_NULL) 3734 BTF_ID_FLAGS(func, bpf_percpu_obj_new_impl, KF_ACQUIRE | KF_RET_NULL) 3735 BTF_ID_FLAGS(func, bpf_obj_drop_impl, KF_RELEASE) 3736 BTF_ID_FLAGS(func, bpf_percpu_obj_drop_impl, KF_RELEASE) 3737 BTF_ID_FLAGS(func, bpf_refcount_acquire_impl, KF_ACQUIRE | KF_RET_NULL | KF_RCU) 3738 BTF_ID_FLAGS(func, bpf_list_push_front_impl) 3739 BTF_ID_FLAGS(func, bpf_list_push_back_impl) 3740 BTF_ID_FLAGS(func, bpf_list_pop_front, KF_ACQUIRE | KF_RET_NULL) 3741 BTF_ID_FLAGS(func, bpf_list_pop_back, KF_ACQUIRE | KF_RET_NULL) 3742 BTF_ID_FLAGS(func, bpf_list_front, KF_RET_NULL) 3743 BTF_ID_FLAGS(func, bpf_list_back, KF_RET_NULL) 3744 BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) 3745 BTF_ID_FLAGS(func, bpf_task_release, KF_RELEASE) 3746 BTF_ID_FLAGS(func, bpf_rbtree_remove, KF_ACQUIRE | KF_RET_NULL) 3747 BTF_ID_FLAGS(func, bpf_rbtree_add_impl) 3748 BTF_ID_FLAGS(func, bpf_rbtree_first, KF_RET_NULL) 3749 BTF_ID_FLAGS(func, bpf_rbtree_root, KF_RET_NULL) 3750 BTF_ID_FLAGS(func, bpf_rbtree_left, KF_RET_NULL) 3751 BTF_ID_FLAGS(func, bpf_rbtree_right, KF_RET_NULL) 3752 3753 #ifdef CONFIG_CGROUPS 3754 BTF_ID_FLAGS(func, bpf_cgroup_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) 3755 BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE) 3756 BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_RCU | KF_RET_NULL) 3757 BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL) 3758 BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU) 3759 BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL) 3760 #endif 3761 BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) 3762 BTF_ID_FLAGS(func, bpf_task_from_vpid, KF_ACQUIRE | KF_RET_NULL) 3763 BTF_ID_FLAGS(func, bpf_throw) 3764 #ifdef CONFIG_BPF_EVENTS 3765 BTF_ID_FLAGS(func, bpf_send_signal_task, KF_TRUSTED_ARGS) 3766 #endif 3767 BTF_KFUNCS_END(generic_btf_ids) 3768 3769 static const struct btf_kfunc_id_set generic_kfunc_set = { 3770 .owner = THIS_MODULE, 3771 .set = &generic_btf_ids, 3772 }; 3773 3774 3775 BTF_ID_LIST(generic_dtor_ids) 3776 BTF_ID(struct, task_struct) 3777 BTF_ID(func, bpf_task_release_dtor) 3778 #ifdef CONFIG_CGROUPS 3779 BTF_ID(struct, cgroup) 3780 BTF_ID(func, bpf_cgroup_release_dtor) 3781 #endif 3782 3783 BTF_KFUNCS_START(common_btf_ids) 3784 BTF_ID_FLAGS(func, bpf_cast_to_kern_ctx, KF_FASTCALL) 3785 BTF_ID_FLAGS(func, bpf_rdonly_cast, KF_FASTCALL) 3786 BTF_ID_FLAGS(func, bpf_rcu_read_lock) 3787 BTF_ID_FLAGS(func, bpf_rcu_read_unlock) 3788 BTF_ID_FLAGS(func, bpf_dynptr_slice, KF_RET_NULL) 3789 BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL) 3790 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW) 3791 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL) 3792 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY) 3793 BTF_ID_FLAGS(func, bpf_iter_task_vma_new, KF_ITER_NEW | KF_RCU) 3794 BTF_ID_FLAGS(func, bpf_iter_task_vma_next, KF_ITER_NEXT | KF_RET_NULL) 3795 BTF_ID_FLAGS(func, bpf_iter_task_vma_destroy, KF_ITER_DESTROY) 3796 #ifdef CONFIG_CGROUPS 3797 BTF_ID_FLAGS(func, bpf_iter_css_task_new, KF_ITER_NEW | KF_TRUSTED_ARGS) 3798 BTF_ID_FLAGS(func, bpf_iter_css_task_next, KF_ITER_NEXT | KF_RET_NULL) 3799 BTF_ID_FLAGS(func, bpf_iter_css_task_destroy, KF_ITER_DESTROY) 3800 BTF_ID_FLAGS(func, bpf_iter_css_new, KF_ITER_NEW | KF_TRUSTED_ARGS | KF_RCU_PROTECTED) 3801 BTF_ID_FLAGS(func, bpf_iter_css_next, KF_ITER_NEXT | KF_RET_NULL) 3802 BTF_ID_FLAGS(func, bpf_iter_css_destroy, KF_ITER_DESTROY) 3803 #endif 3804 BTF_ID_FLAGS(func, bpf_iter_task_new, KF_ITER_NEW | KF_TRUSTED_ARGS | KF_RCU_PROTECTED) 3805 BTF_ID_FLAGS(func, bpf_iter_task_next, KF_ITER_NEXT | KF_RET_NULL) 3806 BTF_ID_FLAGS(func, bpf_iter_task_destroy, KF_ITER_DESTROY) 3807 BTF_ID_FLAGS(func, bpf_dynptr_adjust) 3808 BTF_ID_FLAGS(func, bpf_dynptr_is_null) 3809 BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) 3810 BTF_ID_FLAGS(func, bpf_dynptr_size) 3811 BTF_ID_FLAGS(func, bpf_dynptr_clone) 3812 BTF_ID_FLAGS(func, bpf_dynptr_copy) 3813 BTF_ID_FLAGS(func, bpf_dynptr_memset) 3814 #ifdef CONFIG_NET 3815 BTF_ID_FLAGS(func, bpf_modify_return_test_tp) 3816 #endif 3817 BTF_ID_FLAGS(func, bpf_wq_init) 3818 BTF_ID_FLAGS(func, bpf_wq_set_callback_impl) 3819 BTF_ID_FLAGS(func, bpf_wq_start) 3820 BTF_ID_FLAGS(func, bpf_preempt_disable) 3821 BTF_ID_FLAGS(func, bpf_preempt_enable) 3822 BTF_ID_FLAGS(func, bpf_iter_bits_new, KF_ITER_NEW) 3823 BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_NULL) 3824 BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY) 3825 BTF_ID_FLAGS(func, bpf_copy_from_user_str, KF_SLEEPABLE) 3826 BTF_ID_FLAGS(func, bpf_copy_from_user_task_str, KF_SLEEPABLE) 3827 BTF_ID_FLAGS(func, bpf_get_kmem_cache) 3828 BTF_ID_FLAGS(func, bpf_iter_kmem_cache_new, KF_ITER_NEW | KF_SLEEPABLE) 3829 BTF_ID_FLAGS(func, bpf_iter_kmem_cache_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE) 3830 BTF_ID_FLAGS(func, bpf_iter_kmem_cache_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) 3831 BTF_ID_FLAGS(func, bpf_local_irq_save) 3832 BTF_ID_FLAGS(func, bpf_local_irq_restore) 3833 BTF_ID_FLAGS(func, bpf_probe_read_user_dynptr) 3834 BTF_ID_FLAGS(func, bpf_probe_read_kernel_dynptr) 3835 BTF_ID_FLAGS(func, bpf_probe_read_user_str_dynptr) 3836 BTF_ID_FLAGS(func, bpf_probe_read_kernel_str_dynptr) 3837 BTF_ID_FLAGS(func, bpf_copy_from_user_dynptr, KF_SLEEPABLE) 3838 BTF_ID_FLAGS(func, bpf_copy_from_user_str_dynptr, KF_SLEEPABLE) 3839 BTF_ID_FLAGS(func, bpf_copy_from_user_task_dynptr, KF_SLEEPABLE | KF_TRUSTED_ARGS) 3840 BTF_ID_FLAGS(func, bpf_copy_from_user_task_str_dynptr, KF_SLEEPABLE | KF_TRUSTED_ARGS) 3841 #ifdef CONFIG_DMA_SHARED_BUFFER 3842 BTF_ID_FLAGS(func, bpf_iter_dmabuf_new, KF_ITER_NEW | KF_SLEEPABLE) 3843 BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE) 3844 BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) 3845 #endif 3846 BTF_ID_FLAGS(func, __bpf_trap) 3847 BTF_ID_FLAGS(func, bpf_strcmp); 3848 BTF_ID_FLAGS(func, bpf_strchr); 3849 BTF_ID_FLAGS(func, bpf_strchrnul); 3850 BTF_ID_FLAGS(func, bpf_strnchr); 3851 BTF_ID_FLAGS(func, bpf_strrchr); 3852 BTF_ID_FLAGS(func, bpf_strlen); 3853 BTF_ID_FLAGS(func, bpf_strnlen); 3854 BTF_ID_FLAGS(func, bpf_strspn); 3855 BTF_ID_FLAGS(func, bpf_strcspn); 3856 BTF_ID_FLAGS(func, bpf_strstr); 3857 BTF_ID_FLAGS(func, bpf_strnstr); 3858 #if defined(CONFIG_BPF_LSM) && defined(CONFIG_CGROUPS) 3859 BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU) 3860 #endif 3861 BTF_ID_FLAGS(func, bpf_stream_vprintk, KF_TRUSTED_ARGS) 3862 BTF_KFUNCS_END(common_btf_ids) 3863 3864 static const struct btf_kfunc_id_set common_kfunc_set = { 3865 .owner = THIS_MODULE, 3866 .set = &common_btf_ids, 3867 }; 3868 3869 static int __init kfunc_init(void) 3870 { 3871 int ret; 3872 const struct btf_id_dtor_kfunc generic_dtors[] = { 3873 { 3874 .btf_id = generic_dtor_ids[0], 3875 .kfunc_btf_id = generic_dtor_ids[1] 3876 }, 3877 #ifdef CONFIG_CGROUPS 3878 { 3879 .btf_id = generic_dtor_ids[2], 3880 .kfunc_btf_id = generic_dtor_ids[3] 3881 }, 3882 #endif 3883 }; 3884 3885 ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &generic_kfunc_set); 3886 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &generic_kfunc_set); 3887 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &generic_kfunc_set); 3888 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set); 3889 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &generic_kfunc_set); 3890 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, &generic_kfunc_set); 3891 ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors, 3892 ARRAY_SIZE(generic_dtors), 3893 THIS_MODULE); 3894 return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &common_kfunc_set); 3895 } 3896 3897 late_initcall(kfunc_init); 3898 3899 /* Get a pointer to dynptr data up to len bytes for read only access. If 3900 * the dynptr doesn't have continuous data up to len bytes, return NULL. 3901 */ 3902 const void *__bpf_dynptr_data(const struct bpf_dynptr_kern *ptr, u32 len) 3903 { 3904 const struct bpf_dynptr *p = (struct bpf_dynptr *)ptr; 3905 3906 return bpf_dynptr_slice(p, 0, NULL, len); 3907 } 3908 3909 /* Get a pointer to dynptr data up to len bytes for read write access. If 3910 * the dynptr doesn't have continuous data up to len bytes, or the dynptr 3911 * is read only, return NULL. 3912 */ 3913 void *__bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u32 len) 3914 { 3915 if (__bpf_dynptr_is_rdonly(ptr)) 3916 return NULL; 3917 return (void *)__bpf_dynptr_data(ptr, len); 3918 } 3919