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 #include <linux/verification.h> 29 #include <linux/task_work.h> 30 #include <linux/irq_work.h> 31 #include <linux/buildid.h> 32 33 #include "../../lib/kstrtox.h" 34 35 /* If kernel subsystem is allowing eBPF programs to call this function, 36 * inside its own verifier_ops->get_func_proto() callback it should return 37 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments 38 * 39 * Different map implementations will rely on rcu in map methods 40 * lookup/update/delete, therefore eBPF programs must run under rcu lock 41 * if program is allowed to access maps, so check rcu_read_lock_held() or 42 * rcu_read_lock_trace_held() in all three functions. 43 */ 44 BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key) 45 { 46 WARN_ON_ONCE(!bpf_rcu_lock_held()); 47 return (unsigned long) map->ops->map_lookup_elem(map, key); 48 } 49 50 const struct bpf_func_proto bpf_map_lookup_elem_proto = { 51 .func = bpf_map_lookup_elem, 52 .gpl_only = false, 53 .pkt_access = true, 54 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, 55 .arg1_type = ARG_CONST_MAP_PTR, 56 .arg2_type = ARG_PTR_TO_MAP_KEY, 57 }; 58 59 BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key, 60 void *, value, u64, flags) 61 { 62 WARN_ON_ONCE(!bpf_rcu_lock_held()); 63 return map->ops->map_update_elem(map, key, value, flags); 64 } 65 66 const struct bpf_func_proto bpf_map_update_elem_proto = { 67 .func = bpf_map_update_elem, 68 .gpl_only = false, 69 .pkt_access = true, 70 .ret_type = RET_INTEGER, 71 .arg1_type = ARG_CONST_MAP_PTR, 72 .arg2_type = ARG_PTR_TO_MAP_KEY, 73 .arg3_type = ARG_PTR_TO_MAP_VALUE, 74 .arg4_type = ARG_ANYTHING, 75 }; 76 77 BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key) 78 { 79 WARN_ON_ONCE(!bpf_rcu_lock_held()); 80 return map->ops->map_delete_elem(map, key); 81 } 82 83 const struct bpf_func_proto bpf_map_delete_elem_proto = { 84 .func = bpf_map_delete_elem, 85 .gpl_only = false, 86 .pkt_access = true, 87 .ret_type = RET_INTEGER, 88 .arg1_type = ARG_CONST_MAP_PTR, 89 .arg2_type = ARG_PTR_TO_MAP_KEY, 90 }; 91 92 BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags) 93 { 94 return map->ops->map_push_elem(map, value, flags); 95 } 96 97 const struct bpf_func_proto bpf_map_push_elem_proto = { 98 .func = bpf_map_push_elem, 99 .gpl_only = false, 100 .pkt_access = true, 101 .ret_type = RET_INTEGER, 102 .arg1_type = ARG_CONST_MAP_PTR, 103 .arg2_type = ARG_PTR_TO_MAP_VALUE, 104 .arg3_type = ARG_ANYTHING, 105 }; 106 107 BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value) 108 { 109 return map->ops->map_pop_elem(map, value); 110 } 111 112 const struct bpf_func_proto bpf_map_pop_elem_proto = { 113 .func = bpf_map_pop_elem, 114 .gpl_only = false, 115 .ret_type = RET_INTEGER, 116 .arg1_type = ARG_CONST_MAP_PTR, 117 .arg2_type = ARG_PTR_TO_MAP_VALUE | MEM_UNINIT | MEM_WRITE, 118 }; 119 120 BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value) 121 { 122 return map->ops->map_peek_elem(map, value); 123 } 124 125 const struct bpf_func_proto bpf_map_peek_elem_proto = { 126 .func = bpf_map_peek_elem, 127 .gpl_only = false, 128 .ret_type = RET_INTEGER, 129 .arg1_type = ARG_CONST_MAP_PTR, 130 .arg2_type = ARG_PTR_TO_MAP_VALUE | MEM_UNINIT | MEM_WRITE, 131 }; 132 133 BPF_CALL_3(bpf_map_lookup_percpu_elem, struct bpf_map *, map, void *, key, u32, cpu) 134 { 135 WARN_ON_ONCE(!bpf_rcu_lock_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 | MEM_WRITE, 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 static void *map_key_from_value(struct bpf_map *map, void *value, u32 *arr_idx) 1088 { 1089 if (map->map_type == BPF_MAP_TYPE_ARRAY) { 1090 struct bpf_array *array = container_of(map, struct bpf_array, map); 1091 1092 *arr_idx = ((char *)value - array->value) / array->elem_size; 1093 return arr_idx; 1094 } 1095 return (void *)value - round_up(map->key_size, 8); 1096 } 1097 1098 enum bpf_async_type { 1099 BPF_ASYNC_TYPE_TIMER = 0, 1100 BPF_ASYNC_TYPE_WQ, 1101 }; 1102 1103 enum bpf_async_op { 1104 BPF_ASYNC_START, 1105 BPF_ASYNC_CANCEL 1106 }; 1107 1108 struct bpf_async_cmd { 1109 struct llist_node node; 1110 u64 nsec; 1111 u32 mode; 1112 enum bpf_async_op op; 1113 }; 1114 1115 struct bpf_async_cb { 1116 struct bpf_map *map; 1117 struct bpf_prog *prog; 1118 void __rcu *callback_fn; 1119 void *value; 1120 struct rcu_head rcu; 1121 u64 flags; 1122 struct irq_work worker; 1123 refcount_t refcnt; 1124 enum bpf_async_type type; 1125 struct llist_head async_cmds; 1126 }; 1127 1128 /* BPF map elements can contain 'struct bpf_timer'. 1129 * Such map owns all of its BPF timers. 1130 * 'struct bpf_timer' is allocated as part of map element allocation 1131 * and it's zero initialized. 1132 * That space is used to keep 'struct bpf_async_kern'. 1133 * bpf_timer_init() allocates 'struct bpf_hrtimer', inits hrtimer, and 1134 * remembers 'struct bpf_map *' pointer it's part of. 1135 * bpf_timer_set_callback() increments prog refcnt and assign bpf callback_fn. 1136 * bpf_timer_start() arms the timer. 1137 * If user space reference to a map goes to zero at this point 1138 * ops->map_release_uref callback is responsible for cancelling the timers, 1139 * freeing their memory, and decrementing prog's refcnts. 1140 * bpf_timer_cancel() cancels the timer and decrements prog's refcnt. 1141 * Inner maps can contain bpf timers as well. ops->map_release_uref is 1142 * freeing the timers when inner map is replaced or deleted by user space. 1143 */ 1144 struct bpf_hrtimer { 1145 struct bpf_async_cb cb; 1146 struct hrtimer timer; 1147 atomic_t cancelling; 1148 }; 1149 1150 struct bpf_work { 1151 struct bpf_async_cb cb; 1152 struct work_struct work; 1153 }; 1154 1155 /* the actual struct hidden inside uapi struct bpf_timer and bpf_wq */ 1156 struct bpf_async_kern { 1157 union { 1158 struct bpf_async_cb *cb; 1159 struct bpf_hrtimer *timer; 1160 struct bpf_work *work; 1161 }; 1162 } __attribute__((aligned(8))); 1163 1164 static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running); 1165 1166 static void bpf_async_refcount_put(struct bpf_async_cb *cb); 1167 1168 static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer) 1169 { 1170 struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer); 1171 struct bpf_map *map = t->cb.map; 1172 void *value = t->cb.value; 1173 bpf_callback_t callback_fn; 1174 void *key; 1175 u32 idx; 1176 1177 BTF_TYPE_EMIT(struct bpf_timer); 1178 callback_fn = rcu_dereference_check(t->cb.callback_fn, rcu_read_lock_bh_held()); 1179 if (!callback_fn) 1180 goto out; 1181 1182 /* bpf_timer_cb() runs in hrtimer_run_softirq. It doesn't migrate and 1183 * cannot be preempted by another bpf_timer_cb() on the same cpu. 1184 * Remember the timer this callback is servicing to prevent 1185 * deadlock if callback_fn() calls bpf_timer_cancel() or 1186 * bpf_map_delete_elem() on the same timer. 1187 */ 1188 this_cpu_write(hrtimer_running, t); 1189 1190 key = map_key_from_value(map, value, &idx); 1191 1192 callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0); 1193 /* The verifier checked that return value is zero. */ 1194 1195 this_cpu_write(hrtimer_running, NULL); 1196 out: 1197 return HRTIMER_NORESTART; 1198 } 1199 1200 static void bpf_wq_work(struct work_struct *work) 1201 { 1202 struct bpf_work *w = container_of(work, struct bpf_work, work); 1203 struct bpf_async_cb *cb = &w->cb; 1204 struct bpf_map *map = cb->map; 1205 bpf_callback_t callback_fn; 1206 void *value = cb->value; 1207 void *key; 1208 u32 idx; 1209 1210 BTF_TYPE_EMIT(struct bpf_wq); 1211 1212 callback_fn = READ_ONCE(cb->callback_fn); 1213 if (!callback_fn) 1214 return; 1215 1216 key = map_key_from_value(map, value, &idx); 1217 1218 rcu_read_lock_trace(); 1219 migrate_disable(); 1220 1221 callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0); 1222 1223 migrate_enable(); 1224 rcu_read_unlock_trace(); 1225 } 1226 1227 static void bpf_async_cb_rcu_free(struct rcu_head *rcu) 1228 { 1229 struct bpf_async_cb *cb = container_of(rcu, struct bpf_async_cb, rcu); 1230 1231 /* 1232 * Drop the last reference to prog only after RCU GP, as set_callback() 1233 * may race with cancel_and_free() 1234 */ 1235 if (cb->prog) 1236 bpf_prog_put(cb->prog); 1237 1238 kfree_nolock(cb); 1239 } 1240 1241 /* Callback from call_rcu_tasks_trace, chains to call_rcu for final free */ 1242 static void bpf_async_cb_rcu_tasks_trace_free(struct rcu_head *rcu) 1243 { 1244 struct bpf_async_cb *cb = container_of(rcu, struct bpf_async_cb, rcu); 1245 struct bpf_hrtimer *t = container_of(cb, struct bpf_hrtimer, cb); 1246 struct bpf_work *w = container_of(cb, struct bpf_work, cb); 1247 bool retry = false; 1248 1249 /* 1250 * bpf_async_cancel_and_free() tried to cancel timer/wq, but it 1251 * could have raced with timer/wq_start. Now refcnt is zero and 1252 * srcu/rcu GP completed. Cancel timer/wq again. 1253 */ 1254 switch (cb->type) { 1255 case BPF_ASYNC_TYPE_TIMER: 1256 if (hrtimer_try_to_cancel(&t->timer) < 0) 1257 retry = true; 1258 break; 1259 case BPF_ASYNC_TYPE_WQ: 1260 if (!cancel_work(&w->work) && work_busy(&w->work)) 1261 retry = true; 1262 break; 1263 } 1264 if (retry) { 1265 /* 1266 * hrtimer or wq callback may still be running. It must be 1267 * in rcu_tasks_trace or rcu CS, so wait for GP again. 1268 * It won't retry forever, since refcnt zero prevents all 1269 * operations on timer/wq. 1270 */ 1271 call_rcu_tasks_trace(&cb->rcu, bpf_async_cb_rcu_tasks_trace_free); 1272 return; 1273 } 1274 1275 /* RCU Tasks Trace grace period implies RCU grace period. */ 1276 bpf_async_cb_rcu_free(rcu); 1277 } 1278 1279 static void worker_for_call_rcu(struct irq_work *work) 1280 { 1281 struct bpf_async_cb *cb = container_of(work, struct bpf_async_cb, worker); 1282 1283 call_rcu_tasks_trace(&cb->rcu, bpf_async_cb_rcu_tasks_trace_free); 1284 } 1285 1286 static void bpf_async_refcount_put(struct bpf_async_cb *cb) 1287 { 1288 if (!refcount_dec_and_test(&cb->refcnt)) 1289 return; 1290 1291 if (irqs_disabled()) { 1292 cb->worker = IRQ_WORK_INIT(worker_for_call_rcu); 1293 irq_work_queue(&cb->worker); 1294 } else { 1295 call_rcu_tasks_trace(&cb->rcu, bpf_async_cb_rcu_tasks_trace_free); 1296 } 1297 } 1298 1299 static void bpf_async_cancel_and_free(struct bpf_async_kern *async); 1300 static void bpf_async_irq_worker(struct irq_work *work); 1301 1302 static int __bpf_async_init(struct bpf_async_kern *async, struct bpf_map *map, u64 flags, 1303 enum bpf_async_type type) 1304 { 1305 struct bpf_async_cb *cb, *old_cb; 1306 struct bpf_hrtimer *t; 1307 struct bpf_work *w; 1308 clockid_t clockid; 1309 size_t size; 1310 1311 switch (type) { 1312 case BPF_ASYNC_TYPE_TIMER: 1313 size = sizeof(struct bpf_hrtimer); 1314 break; 1315 case BPF_ASYNC_TYPE_WQ: 1316 size = sizeof(struct bpf_work); 1317 break; 1318 default: 1319 return -EINVAL; 1320 } 1321 1322 old_cb = READ_ONCE(async->cb); 1323 if (old_cb) 1324 return -EBUSY; 1325 1326 cb = bpf_map_kmalloc_nolock(map, size, 0, map->numa_node); 1327 if (!cb) 1328 return -ENOMEM; 1329 1330 switch (type) { 1331 case BPF_ASYNC_TYPE_TIMER: 1332 clockid = flags & (MAX_CLOCKS - 1); 1333 t = (struct bpf_hrtimer *)cb; 1334 1335 atomic_set(&t->cancelling, 0); 1336 hrtimer_setup(&t->timer, bpf_timer_cb, clockid, HRTIMER_MODE_REL_SOFT); 1337 cb->value = (void *)async - map->record->timer_off; 1338 break; 1339 case BPF_ASYNC_TYPE_WQ: 1340 w = (struct bpf_work *)cb; 1341 1342 INIT_WORK(&w->work, bpf_wq_work); 1343 cb->value = (void *)async - map->record->wq_off; 1344 break; 1345 } 1346 cb->map = map; 1347 cb->prog = NULL; 1348 cb->flags = flags; 1349 cb->worker = IRQ_WORK_INIT(bpf_async_irq_worker); 1350 init_llist_head(&cb->async_cmds); 1351 refcount_set(&cb->refcnt, 1); /* map's reference */ 1352 cb->type = type; 1353 rcu_assign_pointer(cb->callback_fn, NULL); 1354 1355 old_cb = cmpxchg(&async->cb, NULL, cb); 1356 if (old_cb) { 1357 /* Lost the race to initialize this bpf_async_kern, drop the allocated object */ 1358 kfree_nolock(cb); 1359 return -EBUSY; 1360 } 1361 /* Guarantee the order between async->cb and map->usercnt. So 1362 * when there are concurrent uref release and bpf timer init, either 1363 * bpf_timer_cancel_and_free() called by uref release reads a no-NULL 1364 * timer or atomic64_read() below returns a zero usercnt. 1365 */ 1366 smp_mb(); 1367 if (!atomic64_read(&map->usercnt)) { 1368 /* maps with timers must be either held by user space 1369 * or pinned in bpffs. 1370 */ 1371 bpf_async_cancel_and_free(async); 1372 return -EPERM; 1373 } 1374 1375 return 0; 1376 } 1377 1378 BPF_CALL_3(bpf_timer_init, struct bpf_async_kern *, timer, struct bpf_map *, map, 1379 u64, flags) 1380 { 1381 clock_t clockid = flags & (MAX_CLOCKS - 1); 1382 1383 BUILD_BUG_ON(MAX_CLOCKS != 16); 1384 BUILD_BUG_ON(sizeof(struct bpf_async_kern) > sizeof(struct bpf_timer)); 1385 BUILD_BUG_ON(__alignof__(struct bpf_async_kern) != __alignof__(struct bpf_timer)); 1386 1387 if (flags >= MAX_CLOCKS || 1388 /* similar to timerfd except _ALARM variants are not supported */ 1389 (clockid != CLOCK_MONOTONIC && 1390 clockid != CLOCK_REALTIME && 1391 clockid != CLOCK_BOOTTIME)) 1392 return -EINVAL; 1393 1394 return __bpf_async_init(timer, map, flags, BPF_ASYNC_TYPE_TIMER); 1395 } 1396 1397 static const struct bpf_func_proto bpf_timer_init_proto = { 1398 .func = bpf_timer_init, 1399 .gpl_only = true, 1400 .ret_type = RET_INTEGER, 1401 .arg1_type = ARG_PTR_TO_TIMER, 1402 .arg2_type = ARG_CONST_MAP_PTR, 1403 .arg3_type = ARG_ANYTHING, 1404 }; 1405 1406 static int bpf_async_update_prog_callback(struct bpf_async_cb *cb, 1407 struct bpf_prog *prog, 1408 void *callback_fn) 1409 { 1410 struct bpf_prog *prev; 1411 1412 /* Acquire a guard reference on prog to prevent it from being freed during the loop */ 1413 if (prog) { 1414 prog = bpf_prog_inc_not_zero(prog); 1415 if (IS_ERR(prog)) 1416 return PTR_ERR(prog); 1417 } 1418 1419 do { 1420 if (prog) 1421 prog = bpf_prog_inc_not_zero(prog); 1422 prev = xchg(&cb->prog, prog); 1423 rcu_assign_pointer(cb->callback_fn, callback_fn); 1424 1425 /* 1426 * Release previous prog, make sure that if other CPU is contending, 1427 * to set bpf_prog, references are not leaked as each iteration acquires and 1428 * releases one reference. 1429 */ 1430 if (prev) 1431 bpf_prog_put(prev); 1432 1433 } while (READ_ONCE(cb->prog) != prog || 1434 (void __force *)READ_ONCE(cb->callback_fn) != callback_fn); 1435 1436 if (prog) 1437 bpf_prog_put(prog); 1438 1439 return 0; 1440 } 1441 1442 static DEFINE_PER_CPU(struct bpf_async_cb *, async_cb_running); 1443 1444 static int bpf_async_schedule_op(struct bpf_async_cb *cb, enum bpf_async_op op, 1445 u64 nsec, u32 timer_mode) 1446 { 1447 /* 1448 * Do not schedule another operation on this cpu if it's in irq_work 1449 * callback that is processing async_cmds queue. Otherwise the following 1450 * loop is possible: 1451 * bpf_timer_start() -> bpf_async_schedule_op() -> irq_work_queue(). 1452 * irqrestore -> bpf_async_irq_worker() -> tracepoint -> bpf_timer_start(). 1453 */ 1454 if (this_cpu_read(async_cb_running) == cb) { 1455 bpf_async_refcount_put(cb); 1456 return -EDEADLK; 1457 } 1458 1459 struct bpf_async_cmd *cmd = kmalloc_nolock(sizeof(*cmd), 0, NUMA_NO_NODE); 1460 1461 if (!cmd) { 1462 bpf_async_refcount_put(cb); 1463 return -ENOMEM; 1464 } 1465 init_llist_node(&cmd->node); 1466 cmd->nsec = nsec; 1467 cmd->mode = timer_mode; 1468 cmd->op = op; 1469 if (llist_add(&cmd->node, &cb->async_cmds)) 1470 irq_work_queue(&cb->worker); 1471 return 0; 1472 } 1473 1474 static int __bpf_async_set_callback(struct bpf_async_kern *async, void *callback_fn, 1475 struct bpf_prog *prog) 1476 { 1477 struct bpf_async_cb *cb; 1478 1479 cb = READ_ONCE(async->cb); 1480 if (!cb) 1481 return -EINVAL; 1482 1483 return bpf_async_update_prog_callback(cb, prog, callback_fn); 1484 } 1485 1486 BPF_CALL_3(bpf_timer_set_callback, struct bpf_async_kern *, timer, void *, callback_fn, 1487 struct bpf_prog_aux *, aux) 1488 { 1489 return __bpf_async_set_callback(timer, callback_fn, aux->prog); 1490 } 1491 1492 static const struct bpf_func_proto bpf_timer_set_callback_proto = { 1493 .func = bpf_timer_set_callback, 1494 .gpl_only = true, 1495 .ret_type = RET_INTEGER, 1496 .arg1_type = ARG_PTR_TO_TIMER, 1497 .arg2_type = ARG_PTR_TO_FUNC, 1498 }; 1499 1500 static bool defer_timer_wq_op(void) 1501 { 1502 return in_hardirq() || irqs_disabled(); 1503 } 1504 1505 BPF_CALL_3(bpf_timer_start, struct bpf_async_kern *, async, u64, nsecs, u64, flags) 1506 { 1507 struct bpf_hrtimer *t; 1508 u32 mode; 1509 1510 if (flags & ~(BPF_F_TIMER_ABS | BPF_F_TIMER_CPU_PIN)) 1511 return -EINVAL; 1512 1513 t = READ_ONCE(async->timer); 1514 if (!t || !READ_ONCE(t->cb.prog)) 1515 return -EINVAL; 1516 1517 if (flags & BPF_F_TIMER_ABS) 1518 mode = HRTIMER_MODE_ABS_SOFT; 1519 else 1520 mode = HRTIMER_MODE_REL_SOFT; 1521 1522 if (flags & BPF_F_TIMER_CPU_PIN) 1523 mode |= HRTIMER_MODE_PINNED; 1524 1525 /* 1526 * bpf_async_cancel_and_free() could have dropped refcnt to zero. In 1527 * such case BPF progs are not allowed to arm the timer to prevent UAF. 1528 */ 1529 if (!refcount_inc_not_zero(&t->cb.refcnt)) 1530 return -ENOENT; 1531 1532 if (!defer_timer_wq_op()) { 1533 hrtimer_start(&t->timer, ns_to_ktime(nsecs), mode); 1534 bpf_async_refcount_put(&t->cb); 1535 return 0; 1536 } else { 1537 return bpf_async_schedule_op(&t->cb, BPF_ASYNC_START, nsecs, mode); 1538 } 1539 } 1540 1541 static const struct bpf_func_proto bpf_timer_start_proto = { 1542 .func = bpf_timer_start, 1543 .gpl_only = true, 1544 .ret_type = RET_INTEGER, 1545 .arg1_type = ARG_PTR_TO_TIMER, 1546 .arg2_type = ARG_ANYTHING, 1547 .arg3_type = ARG_ANYTHING, 1548 }; 1549 1550 BPF_CALL_1(bpf_timer_cancel, struct bpf_async_kern *, async) 1551 { 1552 struct bpf_hrtimer *t, *cur_t; 1553 bool inc = false; 1554 int ret = 0; 1555 1556 if (defer_timer_wq_op()) 1557 return -EOPNOTSUPP; 1558 1559 t = READ_ONCE(async->timer); 1560 if (!t) 1561 return -EINVAL; 1562 1563 cur_t = this_cpu_read(hrtimer_running); 1564 if (cur_t == t) { 1565 /* If bpf callback_fn is trying to bpf_timer_cancel() 1566 * its own timer the hrtimer_cancel() will deadlock 1567 * since it waits for callback_fn to finish. 1568 */ 1569 return -EDEADLK; 1570 } 1571 1572 /* Only account in-flight cancellations when invoked from a timer 1573 * callback, since we want to avoid waiting only if other _callbacks_ 1574 * are waiting on us, to avoid introducing lockups. Non-callback paths 1575 * are ok, since nobody would synchronously wait for their completion. 1576 */ 1577 if (!cur_t) 1578 goto drop; 1579 atomic_inc(&t->cancelling); 1580 /* Need full barrier after relaxed atomic_inc */ 1581 smp_mb__after_atomic(); 1582 inc = true; 1583 if (atomic_read(&cur_t->cancelling)) { 1584 /* We're cancelling timer t, while some other timer callback is 1585 * attempting to cancel us. In such a case, it might be possible 1586 * that timer t belongs to the other callback, or some other 1587 * callback waiting upon it (creating transitive dependencies 1588 * upon us), and we will enter a deadlock if we continue 1589 * cancelling and waiting for it synchronously, since it might 1590 * do the same. Bail! 1591 */ 1592 atomic_dec(&t->cancelling); 1593 return -EDEADLK; 1594 } 1595 drop: 1596 bpf_async_update_prog_callback(&t->cb, NULL, NULL); 1597 /* Cancel the timer and wait for associated callback to finish 1598 * if it was running. 1599 */ 1600 ret = hrtimer_cancel(&t->timer); 1601 if (inc) 1602 atomic_dec(&t->cancelling); 1603 return ret; 1604 } 1605 1606 static const struct bpf_func_proto bpf_timer_cancel_proto = { 1607 .func = bpf_timer_cancel, 1608 .gpl_only = true, 1609 .ret_type = RET_INTEGER, 1610 .arg1_type = ARG_PTR_TO_TIMER, 1611 }; 1612 1613 static void bpf_async_process_op(struct bpf_async_cb *cb, u32 op, 1614 u64 timer_nsec, u32 timer_mode) 1615 { 1616 switch (cb->type) { 1617 case BPF_ASYNC_TYPE_TIMER: { 1618 struct bpf_hrtimer *t = container_of(cb, struct bpf_hrtimer, cb); 1619 1620 switch (op) { 1621 case BPF_ASYNC_START: 1622 hrtimer_start(&t->timer, ns_to_ktime(timer_nsec), timer_mode); 1623 break; 1624 case BPF_ASYNC_CANCEL: 1625 hrtimer_try_to_cancel(&t->timer); 1626 break; 1627 } 1628 break; 1629 } 1630 case BPF_ASYNC_TYPE_WQ: { 1631 struct bpf_work *w = container_of(cb, struct bpf_work, cb); 1632 1633 switch (op) { 1634 case BPF_ASYNC_START: 1635 schedule_work(&w->work); 1636 break; 1637 case BPF_ASYNC_CANCEL: 1638 cancel_work(&w->work); 1639 break; 1640 } 1641 break; 1642 } 1643 } 1644 bpf_async_refcount_put(cb); 1645 } 1646 1647 static void bpf_async_irq_worker(struct irq_work *work) 1648 { 1649 struct bpf_async_cb *cb = container_of(work, struct bpf_async_cb, worker); 1650 struct llist_node *pos, *n, *list; 1651 1652 list = llist_del_all(&cb->async_cmds); 1653 if (!list) 1654 return; 1655 1656 list = llist_reverse_order(list); 1657 this_cpu_write(async_cb_running, cb); 1658 llist_for_each_safe(pos, n, list) { 1659 struct bpf_async_cmd *cmd; 1660 1661 cmd = container_of(pos, struct bpf_async_cmd, node); 1662 bpf_async_process_op(cb, cmd->op, cmd->nsec, cmd->mode); 1663 kfree_nolock(cmd); 1664 } 1665 this_cpu_write(async_cb_running, NULL); 1666 } 1667 1668 static void bpf_async_cancel_and_free(struct bpf_async_kern *async) 1669 { 1670 struct bpf_async_cb *cb; 1671 1672 if (!READ_ONCE(async->cb)) 1673 return; 1674 1675 cb = xchg(&async->cb, NULL); 1676 if (!cb) 1677 return; 1678 1679 bpf_async_update_prog_callback(cb, NULL, NULL); 1680 /* 1681 * No refcount_inc_not_zero(&cb->refcnt) here. Dropping the last 1682 * refcnt. Either synchronously or asynchronously in irq_work. 1683 */ 1684 1685 if (!defer_timer_wq_op()) { 1686 bpf_async_process_op(cb, BPF_ASYNC_CANCEL, 0, 0); 1687 } else { 1688 (void)bpf_async_schedule_op(cb, BPF_ASYNC_CANCEL, 0, 0); 1689 /* 1690 * bpf_async_schedule_op() either enqueues allocated cmd into llist 1691 * or fails with ENOMEM and drop the last refcnt. 1692 * This is unlikely, but safe, since bpf_async_cb_rcu_tasks_trace_free() 1693 * callback will do additional timer/wq_cancel due to races anyway. 1694 */ 1695 } 1696 } 1697 1698 /* 1699 * This function is called by map_delete/update_elem for individual element and 1700 * by ops->map_release_uref when the user space reference to a map reaches zero. 1701 */ 1702 void bpf_timer_cancel_and_free(void *val) 1703 { 1704 bpf_async_cancel_and_free(val); 1705 } 1706 1707 /* 1708 * This function is called by map_delete/update_elem for individual element and 1709 * by ops->map_release_uref when the user space reference to a map reaches zero. 1710 */ 1711 void bpf_wq_cancel_and_free(void *val) 1712 { 1713 bpf_async_cancel_and_free(val); 1714 } 1715 1716 BPF_CALL_2(bpf_kptr_xchg, void *, dst, void *, ptr) 1717 { 1718 unsigned long *kptr = dst; 1719 1720 /* This helper may be inlined by verifier. */ 1721 return xchg(kptr, (unsigned long)ptr); 1722 } 1723 1724 /* Unlike other PTR_TO_BTF_ID helpers the btf_id in bpf_kptr_xchg() 1725 * helper is determined dynamically by the verifier. Use BPF_PTR_POISON to 1726 * denote type that verifier will determine. 1727 */ 1728 static const struct bpf_func_proto bpf_kptr_xchg_proto = { 1729 .func = bpf_kptr_xchg, 1730 .gpl_only = false, 1731 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 1732 .ret_btf_id = BPF_PTR_POISON, 1733 .arg1_type = ARG_KPTR_XCHG_DEST, 1734 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL | OBJ_RELEASE, 1735 .arg2_btf_id = BPF_PTR_POISON, 1736 }; 1737 1738 struct bpf_dynptr_file_impl { 1739 struct freader freader; 1740 /* 64 bit offset and size overriding 32 bit ones in bpf_dynptr_kern */ 1741 u64 offset; 1742 u64 size; 1743 }; 1744 1745 /* Since the upper 8 bits of dynptr->size is reserved, the 1746 * maximum supported size is 2^24 - 1. 1747 */ 1748 #define DYNPTR_MAX_SIZE ((1UL << 24) - 1) 1749 #define DYNPTR_TYPE_SHIFT 28 1750 #define DYNPTR_SIZE_MASK 0xFFFFFF 1751 #define DYNPTR_RDONLY_BIT BIT(31) 1752 1753 bool __bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr) 1754 { 1755 return ptr->size & DYNPTR_RDONLY_BIT; 1756 } 1757 1758 void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr) 1759 { 1760 ptr->size |= DYNPTR_RDONLY_BIT; 1761 } 1762 1763 static void bpf_dynptr_set_type(struct bpf_dynptr_kern *ptr, enum bpf_dynptr_type type) 1764 { 1765 ptr->size |= type << DYNPTR_TYPE_SHIFT; 1766 } 1767 1768 static enum bpf_dynptr_type bpf_dynptr_get_type(const struct bpf_dynptr_kern *ptr) 1769 { 1770 return (ptr->size & ~(DYNPTR_RDONLY_BIT)) >> DYNPTR_TYPE_SHIFT; 1771 } 1772 1773 u64 __bpf_dynptr_size(const struct bpf_dynptr_kern *ptr) 1774 { 1775 if (bpf_dynptr_get_type(ptr) == BPF_DYNPTR_TYPE_FILE) { 1776 struct bpf_dynptr_file_impl *df = ptr->data; 1777 1778 return df->size; 1779 } 1780 1781 return ptr->size & DYNPTR_SIZE_MASK; 1782 } 1783 1784 static void bpf_dynptr_advance_offset(struct bpf_dynptr_kern *ptr, u64 off) 1785 { 1786 if (bpf_dynptr_get_type(ptr) == BPF_DYNPTR_TYPE_FILE) { 1787 struct bpf_dynptr_file_impl *df = ptr->data; 1788 1789 df->offset += off; 1790 return; 1791 } 1792 ptr->offset += off; 1793 } 1794 1795 static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u64 new_size) 1796 { 1797 u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK; 1798 1799 if (bpf_dynptr_get_type(ptr) == BPF_DYNPTR_TYPE_FILE) { 1800 struct bpf_dynptr_file_impl *df = ptr->data; 1801 1802 df->size = new_size; 1803 return; 1804 } 1805 ptr->size = (u32)new_size | metadata; 1806 } 1807 1808 int bpf_dynptr_check_size(u64 size) 1809 { 1810 return size > DYNPTR_MAX_SIZE ? -E2BIG : 0; 1811 } 1812 1813 static int bpf_file_fetch_bytes(struct bpf_dynptr_file_impl *df, u64 offset, void *buf, u64 len) 1814 { 1815 const void *ptr; 1816 1817 if (!buf) 1818 return -EINVAL; 1819 1820 df->freader.buf = buf; 1821 df->freader.buf_sz = len; 1822 ptr = freader_fetch(&df->freader, offset + df->offset, len); 1823 if (!ptr) 1824 return df->freader.err; 1825 1826 if (ptr != buf) /* Force copying into the buffer */ 1827 memcpy(buf, ptr, len); 1828 1829 return 0; 1830 } 1831 1832 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data, 1833 enum bpf_dynptr_type type, u32 offset, u32 size) 1834 { 1835 ptr->data = data; 1836 ptr->offset = offset; 1837 ptr->size = size; 1838 bpf_dynptr_set_type(ptr, type); 1839 } 1840 1841 void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr) 1842 { 1843 memset(ptr, 0, sizeof(*ptr)); 1844 } 1845 1846 BPF_CALL_4(bpf_dynptr_from_mem, void *, data, u64, size, u64, flags, struct bpf_dynptr_kern *, ptr) 1847 { 1848 int err; 1849 1850 BTF_TYPE_EMIT(struct bpf_dynptr); 1851 1852 err = bpf_dynptr_check_size(size); 1853 if (err) 1854 goto error; 1855 1856 /* flags is currently unsupported */ 1857 if (flags) { 1858 err = -EINVAL; 1859 goto error; 1860 } 1861 1862 bpf_dynptr_init(ptr, data, BPF_DYNPTR_TYPE_LOCAL, 0, size); 1863 1864 return 0; 1865 1866 error: 1867 bpf_dynptr_set_null(ptr); 1868 return err; 1869 } 1870 1871 static const struct bpf_func_proto bpf_dynptr_from_mem_proto = { 1872 .func = bpf_dynptr_from_mem, 1873 .gpl_only = false, 1874 .ret_type = RET_INTEGER, 1875 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 1876 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 1877 .arg3_type = ARG_ANYTHING, 1878 .arg4_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL | MEM_UNINIT | MEM_WRITE, 1879 }; 1880 1881 static int __bpf_dynptr_read(void *dst, u64 len, const struct bpf_dynptr_kern *src, 1882 u64 offset, u64 flags) 1883 { 1884 enum bpf_dynptr_type type; 1885 int err; 1886 1887 if (!src->data || flags) 1888 return -EINVAL; 1889 1890 err = bpf_dynptr_check_off_len(src, offset, len); 1891 if (err) 1892 return err; 1893 1894 type = bpf_dynptr_get_type(src); 1895 1896 switch (type) { 1897 case BPF_DYNPTR_TYPE_LOCAL: 1898 case BPF_DYNPTR_TYPE_RINGBUF: 1899 /* Source and destination may possibly overlap, hence use memmove to 1900 * copy the data. E.g. bpf_dynptr_from_mem may create two dynptr 1901 * pointing to overlapping PTR_TO_MAP_VALUE regions. 1902 */ 1903 memmove(dst, src->data + src->offset + offset, len); 1904 return 0; 1905 case BPF_DYNPTR_TYPE_SKB: 1906 return __bpf_skb_load_bytes(src->data, src->offset + offset, dst, len); 1907 case BPF_DYNPTR_TYPE_XDP: 1908 return __bpf_xdp_load_bytes(src->data, src->offset + offset, dst, len); 1909 case BPF_DYNPTR_TYPE_SKB_META: 1910 memmove(dst, bpf_skb_meta_pointer(src->data, src->offset + offset), len); 1911 return 0; 1912 case BPF_DYNPTR_TYPE_FILE: 1913 return bpf_file_fetch_bytes(src->data, offset, dst, len); 1914 default: 1915 WARN_ONCE(true, "bpf_dynptr_read: unknown dynptr type %d\n", type); 1916 return -EFAULT; 1917 } 1918 } 1919 1920 BPF_CALL_5(bpf_dynptr_read, void *, dst, u64, len, const struct bpf_dynptr_kern *, src, 1921 u64, offset, u64, flags) 1922 { 1923 return __bpf_dynptr_read(dst, len, src, offset, flags); 1924 } 1925 1926 static const struct bpf_func_proto bpf_dynptr_read_proto = { 1927 .func = bpf_dynptr_read, 1928 .gpl_only = false, 1929 .ret_type = RET_INTEGER, 1930 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 1931 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 1932 .arg3_type = ARG_PTR_TO_DYNPTR | MEM_RDONLY, 1933 .arg4_type = ARG_ANYTHING, 1934 .arg5_type = ARG_ANYTHING, 1935 }; 1936 1937 int __bpf_dynptr_write(const struct bpf_dynptr_kern *dst, u64 offset, void *src, 1938 u64 len, u64 flags) 1939 { 1940 enum bpf_dynptr_type type; 1941 int err; 1942 1943 if (!dst->data || __bpf_dynptr_is_rdonly(dst)) 1944 return -EINVAL; 1945 1946 err = bpf_dynptr_check_off_len(dst, offset, len); 1947 if (err) 1948 return err; 1949 1950 type = bpf_dynptr_get_type(dst); 1951 1952 switch (type) { 1953 case BPF_DYNPTR_TYPE_LOCAL: 1954 case BPF_DYNPTR_TYPE_RINGBUF: 1955 if (flags) 1956 return -EINVAL; 1957 /* Source and destination may possibly overlap, hence use memmove to 1958 * copy the data. E.g. bpf_dynptr_from_mem may create two dynptr 1959 * pointing to overlapping PTR_TO_MAP_VALUE regions. 1960 */ 1961 memmove(dst->data + dst->offset + offset, src, len); 1962 return 0; 1963 case BPF_DYNPTR_TYPE_SKB: 1964 return __bpf_skb_store_bytes(dst->data, dst->offset + offset, src, len, 1965 flags); 1966 case BPF_DYNPTR_TYPE_XDP: 1967 if (flags) 1968 return -EINVAL; 1969 return __bpf_xdp_store_bytes(dst->data, dst->offset + offset, src, len); 1970 case BPF_DYNPTR_TYPE_SKB_META: 1971 return __bpf_skb_meta_store_bytes(dst->data, dst->offset + offset, src, 1972 len, flags); 1973 default: 1974 WARN_ONCE(true, "bpf_dynptr_write: unknown dynptr type %d\n", type); 1975 return -EFAULT; 1976 } 1977 } 1978 1979 BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u64, offset, void *, src, 1980 u64, len, u64, flags) 1981 { 1982 return __bpf_dynptr_write(dst, offset, src, len, flags); 1983 } 1984 1985 static const struct bpf_func_proto bpf_dynptr_write_proto = { 1986 .func = bpf_dynptr_write, 1987 .gpl_only = false, 1988 .ret_type = RET_INTEGER, 1989 .arg1_type = ARG_PTR_TO_DYNPTR | MEM_RDONLY, 1990 .arg2_type = ARG_ANYTHING, 1991 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, 1992 .arg4_type = ARG_CONST_SIZE_OR_ZERO, 1993 .arg5_type = ARG_ANYTHING, 1994 }; 1995 1996 BPF_CALL_3(bpf_dynptr_data, const struct bpf_dynptr_kern *, ptr, u64, offset, u64, len) 1997 { 1998 enum bpf_dynptr_type type; 1999 int err; 2000 2001 if (!ptr->data) 2002 return 0; 2003 2004 err = bpf_dynptr_check_off_len(ptr, offset, len); 2005 if (err) 2006 return 0; 2007 2008 if (__bpf_dynptr_is_rdonly(ptr)) 2009 return 0; 2010 2011 type = bpf_dynptr_get_type(ptr); 2012 2013 switch (type) { 2014 case BPF_DYNPTR_TYPE_LOCAL: 2015 case BPF_DYNPTR_TYPE_RINGBUF: 2016 return (unsigned long)(ptr->data + ptr->offset + offset); 2017 case BPF_DYNPTR_TYPE_SKB: 2018 case BPF_DYNPTR_TYPE_XDP: 2019 case BPF_DYNPTR_TYPE_SKB_META: 2020 /* skb and xdp dynptrs should use bpf_dynptr_slice / bpf_dynptr_slice_rdwr */ 2021 return 0; 2022 default: 2023 WARN_ONCE(true, "bpf_dynptr_data: unknown dynptr type %d\n", type); 2024 return 0; 2025 } 2026 } 2027 2028 static const struct bpf_func_proto bpf_dynptr_data_proto = { 2029 .func = bpf_dynptr_data, 2030 .gpl_only = false, 2031 .ret_type = RET_PTR_TO_DYNPTR_MEM_OR_NULL, 2032 .arg1_type = ARG_PTR_TO_DYNPTR | MEM_RDONLY, 2033 .arg2_type = ARG_ANYTHING, 2034 .arg3_type = ARG_CONST_ALLOC_SIZE_OR_ZERO, 2035 }; 2036 2037 const struct bpf_func_proto bpf_get_current_task_proto __weak; 2038 const struct bpf_func_proto bpf_get_current_task_btf_proto __weak; 2039 const struct bpf_func_proto bpf_probe_read_user_proto __weak; 2040 const struct bpf_func_proto bpf_probe_read_user_str_proto __weak; 2041 const struct bpf_func_proto bpf_probe_read_kernel_proto __weak; 2042 const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak; 2043 const struct bpf_func_proto bpf_task_pt_regs_proto __weak; 2044 const struct bpf_func_proto bpf_perf_event_read_proto __weak; 2045 const struct bpf_func_proto bpf_send_signal_proto __weak; 2046 const struct bpf_func_proto bpf_send_signal_thread_proto __weak; 2047 const struct bpf_func_proto bpf_get_task_stack_sleepable_proto __weak; 2048 const struct bpf_func_proto bpf_get_task_stack_proto __weak; 2049 const struct bpf_func_proto bpf_get_branch_snapshot_proto __weak; 2050 2051 const struct bpf_func_proto * 2052 bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 2053 { 2054 switch (func_id) { 2055 case BPF_FUNC_map_lookup_elem: 2056 return &bpf_map_lookup_elem_proto; 2057 case BPF_FUNC_map_update_elem: 2058 return &bpf_map_update_elem_proto; 2059 case BPF_FUNC_map_delete_elem: 2060 return &bpf_map_delete_elem_proto; 2061 case BPF_FUNC_map_push_elem: 2062 return &bpf_map_push_elem_proto; 2063 case BPF_FUNC_map_pop_elem: 2064 return &bpf_map_pop_elem_proto; 2065 case BPF_FUNC_map_peek_elem: 2066 return &bpf_map_peek_elem_proto; 2067 case BPF_FUNC_map_lookup_percpu_elem: 2068 return &bpf_map_lookup_percpu_elem_proto; 2069 case BPF_FUNC_get_prandom_u32: 2070 return &bpf_get_prandom_u32_proto; 2071 case BPF_FUNC_get_smp_processor_id: 2072 return &bpf_get_raw_smp_processor_id_proto; 2073 case BPF_FUNC_get_numa_node_id: 2074 return &bpf_get_numa_node_id_proto; 2075 case BPF_FUNC_tail_call: 2076 return &bpf_tail_call_proto; 2077 case BPF_FUNC_ktime_get_ns: 2078 return &bpf_ktime_get_ns_proto; 2079 case BPF_FUNC_ktime_get_boot_ns: 2080 return &bpf_ktime_get_boot_ns_proto; 2081 case BPF_FUNC_ktime_get_tai_ns: 2082 return &bpf_ktime_get_tai_ns_proto; 2083 case BPF_FUNC_ringbuf_output: 2084 return &bpf_ringbuf_output_proto; 2085 case BPF_FUNC_ringbuf_reserve: 2086 return &bpf_ringbuf_reserve_proto; 2087 case BPF_FUNC_ringbuf_submit: 2088 return &bpf_ringbuf_submit_proto; 2089 case BPF_FUNC_ringbuf_discard: 2090 return &bpf_ringbuf_discard_proto; 2091 case BPF_FUNC_ringbuf_query: 2092 return &bpf_ringbuf_query_proto; 2093 case BPF_FUNC_strncmp: 2094 return &bpf_strncmp_proto; 2095 case BPF_FUNC_strtol: 2096 return &bpf_strtol_proto; 2097 case BPF_FUNC_strtoul: 2098 return &bpf_strtoul_proto; 2099 case BPF_FUNC_get_current_pid_tgid: 2100 return &bpf_get_current_pid_tgid_proto; 2101 case BPF_FUNC_get_ns_current_pid_tgid: 2102 return &bpf_get_ns_current_pid_tgid_proto; 2103 case BPF_FUNC_get_current_uid_gid: 2104 return &bpf_get_current_uid_gid_proto; 2105 default: 2106 break; 2107 } 2108 2109 if (!bpf_token_capable(prog->aux->token, CAP_BPF)) 2110 return NULL; 2111 2112 switch (func_id) { 2113 case BPF_FUNC_spin_lock: 2114 return &bpf_spin_lock_proto; 2115 case BPF_FUNC_spin_unlock: 2116 return &bpf_spin_unlock_proto; 2117 case BPF_FUNC_jiffies64: 2118 return &bpf_jiffies64_proto; 2119 case BPF_FUNC_per_cpu_ptr: 2120 return &bpf_per_cpu_ptr_proto; 2121 case BPF_FUNC_this_cpu_ptr: 2122 return &bpf_this_cpu_ptr_proto; 2123 case BPF_FUNC_timer_init: 2124 return &bpf_timer_init_proto; 2125 case BPF_FUNC_timer_set_callback: 2126 return &bpf_timer_set_callback_proto; 2127 case BPF_FUNC_timer_start: 2128 return &bpf_timer_start_proto; 2129 case BPF_FUNC_timer_cancel: 2130 return &bpf_timer_cancel_proto; 2131 case BPF_FUNC_kptr_xchg: 2132 return &bpf_kptr_xchg_proto; 2133 case BPF_FUNC_for_each_map_elem: 2134 return &bpf_for_each_map_elem_proto; 2135 case BPF_FUNC_loop: 2136 return &bpf_loop_proto; 2137 case BPF_FUNC_user_ringbuf_drain: 2138 return &bpf_user_ringbuf_drain_proto; 2139 case BPF_FUNC_ringbuf_reserve_dynptr: 2140 return &bpf_ringbuf_reserve_dynptr_proto; 2141 case BPF_FUNC_ringbuf_submit_dynptr: 2142 return &bpf_ringbuf_submit_dynptr_proto; 2143 case BPF_FUNC_ringbuf_discard_dynptr: 2144 return &bpf_ringbuf_discard_dynptr_proto; 2145 case BPF_FUNC_dynptr_from_mem: 2146 return &bpf_dynptr_from_mem_proto; 2147 case BPF_FUNC_dynptr_read: 2148 return &bpf_dynptr_read_proto; 2149 case BPF_FUNC_dynptr_write: 2150 return &bpf_dynptr_write_proto; 2151 case BPF_FUNC_dynptr_data: 2152 return &bpf_dynptr_data_proto; 2153 #ifdef CONFIG_CGROUPS 2154 case BPF_FUNC_cgrp_storage_get: 2155 return &bpf_cgrp_storage_get_proto; 2156 case BPF_FUNC_cgrp_storage_delete: 2157 return &bpf_cgrp_storage_delete_proto; 2158 case BPF_FUNC_get_current_cgroup_id: 2159 return &bpf_get_current_cgroup_id_proto; 2160 case BPF_FUNC_get_current_ancestor_cgroup_id: 2161 return &bpf_get_current_ancestor_cgroup_id_proto; 2162 case BPF_FUNC_current_task_under_cgroup: 2163 return &bpf_current_task_under_cgroup_proto; 2164 #endif 2165 #ifdef CONFIG_CGROUP_NET_CLASSID 2166 case BPF_FUNC_get_cgroup_classid: 2167 return &bpf_get_cgroup_classid_curr_proto; 2168 #endif 2169 case BPF_FUNC_task_storage_get: 2170 return &bpf_task_storage_get_proto; 2171 case BPF_FUNC_task_storage_delete: 2172 return &bpf_task_storage_delete_proto; 2173 default: 2174 break; 2175 } 2176 2177 if (!bpf_token_capable(prog->aux->token, CAP_PERFMON)) 2178 return NULL; 2179 2180 switch (func_id) { 2181 case BPF_FUNC_trace_printk: 2182 return bpf_get_trace_printk_proto(); 2183 case BPF_FUNC_get_current_task: 2184 return &bpf_get_current_task_proto; 2185 case BPF_FUNC_get_current_task_btf: 2186 return &bpf_get_current_task_btf_proto; 2187 case BPF_FUNC_get_current_comm: 2188 return &bpf_get_current_comm_proto; 2189 case BPF_FUNC_probe_read_user: 2190 return &bpf_probe_read_user_proto; 2191 case BPF_FUNC_probe_read_kernel: 2192 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? 2193 NULL : &bpf_probe_read_kernel_proto; 2194 case BPF_FUNC_probe_read_user_str: 2195 return &bpf_probe_read_user_str_proto; 2196 case BPF_FUNC_probe_read_kernel_str: 2197 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? 2198 NULL : &bpf_probe_read_kernel_str_proto; 2199 case BPF_FUNC_copy_from_user: 2200 return &bpf_copy_from_user_proto; 2201 case BPF_FUNC_copy_from_user_task: 2202 return &bpf_copy_from_user_task_proto; 2203 case BPF_FUNC_snprintf_btf: 2204 return &bpf_snprintf_btf_proto; 2205 case BPF_FUNC_snprintf: 2206 return &bpf_snprintf_proto; 2207 case BPF_FUNC_task_pt_regs: 2208 return &bpf_task_pt_regs_proto; 2209 case BPF_FUNC_trace_vprintk: 2210 return bpf_get_trace_vprintk_proto(); 2211 case BPF_FUNC_perf_event_read_value: 2212 return bpf_get_perf_event_read_value_proto(); 2213 case BPF_FUNC_perf_event_read: 2214 return &bpf_perf_event_read_proto; 2215 case BPF_FUNC_send_signal: 2216 return &bpf_send_signal_proto; 2217 case BPF_FUNC_send_signal_thread: 2218 return &bpf_send_signal_thread_proto; 2219 case BPF_FUNC_get_task_stack: 2220 return prog->sleepable ? &bpf_get_task_stack_sleepable_proto 2221 : &bpf_get_task_stack_proto; 2222 case BPF_FUNC_get_branch_snapshot: 2223 return &bpf_get_branch_snapshot_proto; 2224 case BPF_FUNC_find_vma: 2225 return &bpf_find_vma_proto; 2226 default: 2227 return NULL; 2228 } 2229 } 2230 EXPORT_SYMBOL_GPL(bpf_base_func_proto); 2231 2232 void bpf_list_head_free(const struct btf_field *field, void *list_head, 2233 struct bpf_spin_lock *spin_lock) 2234 { 2235 struct list_head *head = list_head, *orig_head = list_head; 2236 2237 BUILD_BUG_ON(sizeof(struct list_head) > sizeof(struct bpf_list_head)); 2238 BUILD_BUG_ON(__alignof__(struct list_head) > __alignof__(struct bpf_list_head)); 2239 2240 /* Do the actual list draining outside the lock to not hold the lock for 2241 * too long, and also prevent deadlocks if tracing programs end up 2242 * executing on entry/exit of functions called inside the critical 2243 * section, and end up doing map ops that call bpf_list_head_free for 2244 * the same map value again. 2245 */ 2246 __bpf_spin_lock_irqsave(spin_lock); 2247 if (!head->next || list_empty(head)) 2248 goto unlock; 2249 head = head->next; 2250 unlock: 2251 INIT_LIST_HEAD(orig_head); 2252 __bpf_spin_unlock_irqrestore(spin_lock); 2253 2254 while (head != orig_head) { 2255 void *obj = head; 2256 2257 obj -= field->graph_root.node_offset; 2258 head = head->next; 2259 /* The contained type can also have resources, including a 2260 * bpf_list_head which needs to be freed. 2261 */ 2262 __bpf_obj_drop_impl(obj, field->graph_root.value_rec, false); 2263 } 2264 } 2265 2266 /* Like rbtree_postorder_for_each_entry_safe, but 'pos' and 'n' are 2267 * 'rb_node *', so field name of rb_node within containing struct is not 2268 * needed. 2269 * 2270 * Since bpf_rb_tree's node type has a corresponding struct btf_field with 2271 * graph_root.node_offset, it's not necessary to know field name 2272 * or type of node struct 2273 */ 2274 #define bpf_rbtree_postorder_for_each_entry_safe(pos, n, root) \ 2275 for (pos = rb_first_postorder(root); \ 2276 pos && ({ n = rb_next_postorder(pos); 1; }); \ 2277 pos = n) 2278 2279 void bpf_rb_root_free(const struct btf_field *field, void *rb_root, 2280 struct bpf_spin_lock *spin_lock) 2281 { 2282 struct rb_root_cached orig_root, *root = rb_root; 2283 struct rb_node *pos, *n; 2284 void *obj; 2285 2286 BUILD_BUG_ON(sizeof(struct rb_root_cached) > sizeof(struct bpf_rb_root)); 2287 BUILD_BUG_ON(__alignof__(struct rb_root_cached) > __alignof__(struct bpf_rb_root)); 2288 2289 __bpf_spin_lock_irqsave(spin_lock); 2290 orig_root = *root; 2291 *root = RB_ROOT_CACHED; 2292 __bpf_spin_unlock_irqrestore(spin_lock); 2293 2294 bpf_rbtree_postorder_for_each_entry_safe(pos, n, &orig_root.rb_root) { 2295 obj = pos; 2296 obj -= field->graph_root.node_offset; 2297 2298 2299 __bpf_obj_drop_impl(obj, field->graph_root.value_rec, false); 2300 } 2301 } 2302 2303 __bpf_kfunc_start_defs(); 2304 2305 /** 2306 * bpf_obj_new() - allocate an object described by program BTF 2307 * @local_type_id__k: type ID in program BTF 2308 * @meta: verifier-supplied struct metadata 2309 * 2310 * Allocate an object of the type identified by @local_type_id__k and 2311 * initialize its special fields. BPF programs can use 2312 * bpf_core_type_id_local() to provide @local_type_id__k. The verifier 2313 * rewrites @meta; BPF programs do not set it. 2314 * 2315 * Return: Pointer to the allocated object, or %NULL on failure. 2316 */ 2317 __bpf_kfunc void *bpf_obj_new(u64 local_type_id__k, struct btf_struct_meta *meta) 2318 { 2319 u64 size = local_type_id__k; 2320 void *p; 2321 2322 p = bpf_mem_alloc(&bpf_global_ma, size); 2323 if (!p) 2324 return NULL; 2325 if (meta) 2326 bpf_obj_init(meta->record, p); 2327 2328 return p; 2329 } 2330 2331 __bpf_kfunc void *bpf_obj_new_impl(u64 local_type_id__k, void *meta__ign) 2332 { 2333 return bpf_obj_new(local_type_id__k, meta__ign); 2334 } 2335 2336 /** 2337 * bpf_percpu_obj_new() - allocate a percpu object described by program BTF 2338 * @local_type_id__k: type ID in program BTF 2339 * @meta: verifier-supplied struct metadata 2340 * 2341 * Allocate a percpu object of the type identified by @local_type_id__k. BPF 2342 * programs can use bpf_core_type_id_local() to provide @local_type_id__k. 2343 * The verifier rewrites @meta; BPF programs do not set it. 2344 * 2345 * Return: Pointer to the allocated percpu object, or %NULL on failure. 2346 */ 2347 __bpf_kfunc void *bpf_percpu_obj_new(u64 local_type_id__k, struct btf_struct_meta *meta) 2348 { 2349 u64 size = local_type_id__k; 2350 2351 /* The verifier has ensured that meta must be NULL */ 2352 return bpf_mem_alloc(&bpf_global_percpu_ma, size); 2353 } 2354 2355 __bpf_kfunc void *bpf_percpu_obj_new_impl(u64 local_type_id__k, void *meta__ign) 2356 { 2357 return bpf_percpu_obj_new(local_type_id__k, meta__ign); 2358 } 2359 2360 /* Must be called under migrate_disable(), as required by bpf_mem_free */ 2361 void __bpf_obj_drop_impl(void *p, const struct btf_record *rec, bool percpu) 2362 { 2363 struct bpf_mem_alloc *ma; 2364 2365 if (rec && rec->refcount_off >= 0 && 2366 !refcount_dec_and_test((refcount_t *)(p + rec->refcount_off))) { 2367 /* Object is refcounted and refcount_dec didn't result in 0 2368 * refcount. Return without freeing the object 2369 */ 2370 return; 2371 } 2372 2373 if (rec) 2374 bpf_obj_free_fields(rec, p); 2375 2376 if (percpu) 2377 ma = &bpf_global_percpu_ma; 2378 else 2379 ma = &bpf_global_ma; 2380 bpf_mem_free_rcu(ma, p); 2381 } 2382 2383 /** 2384 * bpf_obj_drop() - drop a previously allocated object 2385 * @p__alloc: object to free 2386 * @meta: verifier-supplied struct metadata 2387 * 2388 * Destroy special fields in @p__alloc as needed and free the object. The 2389 * verifier rewrites @meta; BPF programs do not set it. 2390 */ 2391 __bpf_kfunc void bpf_obj_drop(void *p__alloc, struct btf_struct_meta *meta) 2392 { 2393 void *p = p__alloc; 2394 2395 __bpf_obj_drop_impl(p, meta ? meta->record : NULL, false); 2396 } 2397 2398 __bpf_kfunc void bpf_obj_drop_impl(void *p__alloc, void *meta__ign) 2399 { 2400 return bpf_obj_drop(p__alloc, meta__ign); 2401 } 2402 2403 /** 2404 * bpf_percpu_obj_drop() - drop a previously allocated percpu object 2405 * @p__alloc: percpu object to free 2406 * @meta: verifier-supplied struct metadata 2407 * 2408 * Free @p__alloc. The verifier rewrites @meta; BPF programs do not set it. 2409 */ 2410 __bpf_kfunc void bpf_percpu_obj_drop(void *p__alloc, struct btf_struct_meta *meta) 2411 { 2412 /* The verifier has ensured that meta must be NULL */ 2413 bpf_mem_free_rcu(&bpf_global_percpu_ma, p__alloc); 2414 } 2415 2416 __bpf_kfunc void bpf_percpu_obj_drop_impl(void *p__alloc, void *meta__ign) 2417 { 2418 bpf_percpu_obj_drop(p__alloc, meta__ign); 2419 } 2420 2421 /** 2422 * bpf_refcount_acquire() - turn a local kptr into an owning reference 2423 * @p__refcounted_kptr: non-owning local kptr 2424 * @meta: verifier-supplied struct metadata 2425 * 2426 * Increment the refcount for @p__refcounted_kptr. The verifier rewrites 2427 * @meta; BPF programs do not set it. 2428 * 2429 * Return: Owning reference to @p__refcounted_kptr, or %NULL on failure. 2430 */ 2431 __bpf_kfunc void *bpf_refcount_acquire(void *p__refcounted_kptr, struct btf_struct_meta *meta) 2432 { 2433 struct bpf_refcount *ref; 2434 2435 /* Could just cast directly to refcount_t *, but need some code using 2436 * bpf_refcount type so that it is emitted in vmlinux BTF 2437 */ 2438 ref = (struct bpf_refcount *)(p__refcounted_kptr + meta->record->refcount_off); 2439 if (!refcount_inc_not_zero((refcount_t *)ref)) 2440 return NULL; 2441 2442 /* Verifier strips KF_RET_NULL if input is owned ref, see is_kfunc_ret_null 2443 * in verifier.c 2444 */ 2445 return (void *)p__refcounted_kptr; 2446 } 2447 2448 __bpf_kfunc void *bpf_refcount_acquire_impl(void *p__refcounted_kptr, void *meta__ign) 2449 { 2450 return bpf_refcount_acquire(p__refcounted_kptr, meta__ign); 2451 } 2452 2453 static int __bpf_list_add(struct bpf_list_node_kern *node, 2454 struct bpf_list_head *head, 2455 bool tail, struct btf_record *rec, u64 off) 2456 { 2457 struct list_head *n = &node->list_head, *h = (void *)head; 2458 2459 /* If list_head was 0-initialized by map, bpf_obj_init_field wasn't 2460 * called on its fields, so init here 2461 */ 2462 if (unlikely(!h->next)) 2463 INIT_LIST_HEAD(h); 2464 2465 /* node->owner != NULL implies !list_empty(n), no need to separately 2466 * check the latter 2467 */ 2468 if (cmpxchg(&node->owner, NULL, BPF_PTR_POISON)) { 2469 /* Only called from BPF prog, no need to migrate_disable */ 2470 __bpf_obj_drop_impl((void *)n - off, rec, false); 2471 return -EINVAL; 2472 } 2473 2474 tail ? list_add_tail(n, h) : list_add(n, h); 2475 WRITE_ONCE(node->owner, head); 2476 2477 return 0; 2478 } 2479 2480 /** 2481 * bpf_list_push_front() - add a node to the front of a BPF linked list 2482 * @head: list head 2483 * @node: node to insert 2484 * @meta: verifier-supplied struct metadata 2485 * @off: verifier-supplied offset of @node within the containing object 2486 * 2487 * Insert @node at the front of @head. The verifier rewrites @meta and @off; 2488 * BPF programs do not set them. 2489 * 2490 * Return: 0 on success, or %-EINVAL if @node is already linked. 2491 */ 2492 __bpf_kfunc int bpf_list_push_front(struct bpf_list_head *head, 2493 struct bpf_list_node *node, 2494 struct btf_struct_meta *meta, 2495 u64 off) 2496 { 2497 struct bpf_list_node_kern *n = (void *)node; 2498 2499 return __bpf_list_add(n, head, false, meta ? meta->record : NULL, off); 2500 } 2501 2502 __bpf_kfunc int bpf_list_push_front_impl(struct bpf_list_head *head, 2503 struct bpf_list_node *node, 2504 void *meta__ign, u64 off) 2505 { 2506 return bpf_list_push_front(head, node, meta__ign, off); 2507 } 2508 2509 /** 2510 * bpf_list_push_back() - add a node to the back of a BPF linked list 2511 * @head: list head 2512 * @node: node to insert 2513 * @meta: verifier-supplied struct metadata 2514 * @off: verifier-supplied offset of @node within the containing object 2515 * 2516 * Insert @node at the back of @head. The verifier rewrites @meta and @off; 2517 * BPF programs do not set them. 2518 * 2519 * Return: 0 on success, or %-EINVAL if @node is already linked. 2520 */ 2521 __bpf_kfunc int bpf_list_push_back(struct bpf_list_head *head, 2522 struct bpf_list_node *node, 2523 struct btf_struct_meta *meta, 2524 u64 off) 2525 { 2526 struct bpf_list_node_kern *n = (void *)node; 2527 2528 return __bpf_list_add(n, head, true, meta ? meta->record : NULL, off); 2529 } 2530 2531 __bpf_kfunc int bpf_list_push_back_impl(struct bpf_list_head *head, 2532 struct bpf_list_node *node, 2533 void *meta__ign, u64 off) 2534 { 2535 return bpf_list_push_back(head, node, meta__ign, off); 2536 } 2537 2538 static struct bpf_list_node *__bpf_list_del(struct bpf_list_head *head, bool tail) 2539 { 2540 struct list_head *n, *h = (void *)head; 2541 struct bpf_list_node_kern *node; 2542 2543 /* If list_head was 0-initialized by map, bpf_obj_init_field wasn't 2544 * called on its fields, so init here 2545 */ 2546 if (unlikely(!h->next)) 2547 INIT_LIST_HEAD(h); 2548 if (list_empty(h)) 2549 return NULL; 2550 2551 n = tail ? h->prev : h->next; 2552 node = container_of(n, struct bpf_list_node_kern, list_head); 2553 if (WARN_ON_ONCE(READ_ONCE(node->owner) != head)) 2554 return NULL; 2555 2556 list_del_init(n); 2557 WRITE_ONCE(node->owner, NULL); 2558 return (struct bpf_list_node *)n; 2559 } 2560 2561 __bpf_kfunc struct bpf_list_node *bpf_list_pop_front(struct bpf_list_head *head) 2562 { 2563 return __bpf_list_del(head, false); 2564 } 2565 2566 __bpf_kfunc struct bpf_list_node *bpf_list_pop_back(struct bpf_list_head *head) 2567 { 2568 return __bpf_list_del(head, true); 2569 } 2570 2571 __bpf_kfunc struct bpf_list_node *bpf_list_front(struct bpf_list_head *head) 2572 { 2573 struct list_head *h = (struct list_head *)head; 2574 2575 if (list_empty(h) || unlikely(!h->next)) 2576 return NULL; 2577 2578 return (struct bpf_list_node *)h->next; 2579 } 2580 2581 __bpf_kfunc struct bpf_list_node *bpf_list_back(struct bpf_list_head *head) 2582 { 2583 struct list_head *h = (struct list_head *)head; 2584 2585 if (list_empty(h) || unlikely(!h->next)) 2586 return NULL; 2587 2588 return (struct bpf_list_node *)h->prev; 2589 } 2590 2591 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root, 2592 struct bpf_rb_node *node) 2593 { 2594 struct bpf_rb_node_kern *node_internal = (struct bpf_rb_node_kern *)node; 2595 struct rb_root_cached *r = (struct rb_root_cached *)root; 2596 struct rb_node *n = &node_internal->rb_node; 2597 2598 /* node_internal->owner != root implies either RB_EMPTY_NODE(n) or 2599 * n is owned by some other tree. No need to check RB_EMPTY_NODE(n) 2600 */ 2601 if (READ_ONCE(node_internal->owner) != root) 2602 return NULL; 2603 2604 rb_erase_cached(n, r); 2605 RB_CLEAR_NODE(n); 2606 WRITE_ONCE(node_internal->owner, NULL); 2607 return (struct bpf_rb_node *)n; 2608 } 2609 2610 /* Need to copy rbtree_add_cached's logic here because our 'less' is a BPF 2611 * program 2612 */ 2613 static int __bpf_rbtree_add(struct bpf_rb_root *root, 2614 struct bpf_rb_node_kern *node, 2615 void *less, struct btf_record *rec, u64 off) 2616 { 2617 struct rb_node **link = &((struct rb_root_cached *)root)->rb_root.rb_node; 2618 struct rb_node *parent = NULL, *n = &node->rb_node; 2619 bpf_callback_t cb = (bpf_callback_t)less; 2620 bool leftmost = true; 2621 2622 /* node->owner != NULL implies !RB_EMPTY_NODE(n), no need to separately 2623 * check the latter 2624 */ 2625 if (cmpxchg(&node->owner, NULL, BPF_PTR_POISON)) { 2626 /* Only called from BPF prog, no need to migrate_disable */ 2627 __bpf_obj_drop_impl((void *)n - off, rec, false); 2628 return -EINVAL; 2629 } 2630 2631 while (*link) { 2632 parent = *link; 2633 if (cb((uintptr_t)node, (uintptr_t)parent, 0, 0, 0)) { 2634 link = &parent->rb_left; 2635 } else { 2636 link = &parent->rb_right; 2637 leftmost = false; 2638 } 2639 } 2640 2641 rb_link_node(n, parent, link); 2642 rb_insert_color_cached(n, (struct rb_root_cached *)root, leftmost); 2643 WRITE_ONCE(node->owner, root); 2644 return 0; 2645 } 2646 2647 /** 2648 * bpf_rbtree_add() - add a node to a BPF rbtree 2649 * @root: tree root 2650 * @node: node to insert 2651 * @less: comparator used to order nodes 2652 * @meta: verifier-supplied struct metadata 2653 * @off: verifier-supplied offset of @node within the containing object 2654 * 2655 * Insert @node into @root using @less. The verifier rewrites @meta and @off; 2656 * BPF programs do not set them. 2657 * 2658 * Return: 0 on success, or %-EINVAL if @node is already linked in a tree. 2659 */ 2660 __bpf_kfunc int bpf_rbtree_add(struct bpf_rb_root *root, 2661 struct bpf_rb_node *node, 2662 bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b), 2663 struct btf_struct_meta *meta, 2664 u64 off) 2665 { 2666 struct bpf_rb_node_kern *n = (void *)node; 2667 2668 return __bpf_rbtree_add(root, n, (void *)less, meta ? meta->record : NULL, off); 2669 } 2670 2671 __bpf_kfunc int bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node *node, 2672 bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b), 2673 void *meta__ign, u64 off) 2674 { 2675 return bpf_rbtree_add(root, node, less, meta__ign, off); 2676 } 2677 2678 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root) 2679 { 2680 struct rb_root_cached *r = (struct rb_root_cached *)root; 2681 2682 return (struct bpf_rb_node *)rb_first_cached(r); 2683 } 2684 2685 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_root(struct bpf_rb_root *root) 2686 { 2687 struct rb_root_cached *r = (struct rb_root_cached *)root; 2688 2689 return (struct bpf_rb_node *)r->rb_root.rb_node; 2690 } 2691 2692 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_left(struct bpf_rb_root *root, struct bpf_rb_node *node) 2693 { 2694 struct bpf_rb_node_kern *node_internal = (struct bpf_rb_node_kern *)node; 2695 2696 if (READ_ONCE(node_internal->owner) != root) 2697 return NULL; 2698 2699 return (struct bpf_rb_node *)node_internal->rb_node.rb_left; 2700 } 2701 2702 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_right(struct bpf_rb_root *root, struct bpf_rb_node *node) 2703 { 2704 struct bpf_rb_node_kern *node_internal = (struct bpf_rb_node_kern *)node; 2705 2706 if (READ_ONCE(node_internal->owner) != root) 2707 return NULL; 2708 2709 return (struct bpf_rb_node *)node_internal->rb_node.rb_right; 2710 } 2711 2712 /** 2713 * bpf_task_acquire - Acquire a reference to a task. A task acquired by this 2714 * kfunc which is not stored in a map as a kptr, must be released by calling 2715 * bpf_task_release(). 2716 * @p: The task on which a reference is being acquired. 2717 */ 2718 __bpf_kfunc struct task_struct *bpf_task_acquire(struct task_struct *p) 2719 { 2720 if (refcount_inc_not_zero(&p->rcu_users)) 2721 return p; 2722 return NULL; 2723 } 2724 2725 /** 2726 * bpf_task_release - Release the reference acquired on a task. 2727 * @p: The task on which a reference is being released. 2728 */ 2729 __bpf_kfunc void bpf_task_release(struct task_struct *p) 2730 { 2731 put_task_struct_rcu_user(p); 2732 } 2733 2734 __bpf_kfunc void bpf_task_release_dtor(void *p) 2735 { 2736 put_task_struct_rcu_user(p); 2737 } 2738 CFI_NOSEAL(bpf_task_release_dtor); 2739 2740 #ifdef CONFIG_CGROUPS 2741 /** 2742 * bpf_cgroup_acquire - Acquire a reference to a cgroup. A cgroup acquired by 2743 * this kfunc which is not stored in a map as a kptr, must be released by 2744 * calling bpf_cgroup_release(). 2745 * @cgrp: The cgroup on which a reference is being acquired. 2746 */ 2747 __bpf_kfunc struct cgroup *bpf_cgroup_acquire(struct cgroup *cgrp) 2748 { 2749 return cgroup_tryget(cgrp) ? cgrp : NULL; 2750 } 2751 2752 /** 2753 * bpf_cgroup_release - Release the reference acquired on a cgroup. 2754 * If this kfunc is invoked in an RCU read region, the cgroup is guaranteed to 2755 * not be freed until the current grace period has ended, even if its refcount 2756 * drops to 0. 2757 * @cgrp: The cgroup on which a reference is being released. 2758 */ 2759 __bpf_kfunc void bpf_cgroup_release(struct cgroup *cgrp) 2760 { 2761 cgroup_put(cgrp); 2762 } 2763 2764 __bpf_kfunc void bpf_cgroup_release_dtor(void *cgrp) 2765 { 2766 cgroup_put(cgrp); 2767 } 2768 CFI_NOSEAL(bpf_cgroup_release_dtor); 2769 2770 /** 2771 * bpf_cgroup_ancestor - Perform a lookup on an entry in a cgroup's ancestor 2772 * array. A cgroup returned by this kfunc which is not subsequently stored in a 2773 * map, must be released by calling bpf_cgroup_release(). 2774 * @cgrp: The cgroup for which we're performing a lookup. 2775 * @level: The level of ancestor to look up. 2776 */ 2777 __bpf_kfunc struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level) 2778 { 2779 struct cgroup *ancestor; 2780 2781 if (level > cgrp->level || level < 0) 2782 return NULL; 2783 2784 /* cgrp's refcnt could be 0 here, but ancestors can still be accessed */ 2785 ancestor = cgrp->ancestors[level]; 2786 if (!cgroup_tryget(ancestor)) 2787 return NULL; 2788 return ancestor; 2789 } 2790 2791 /** 2792 * bpf_cgroup_from_id - Find a cgroup from its ID. A cgroup returned by this 2793 * kfunc which is not subsequently stored in a map, must be released by calling 2794 * bpf_cgroup_release(). 2795 * @cgid: cgroup id. 2796 */ 2797 __bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid) 2798 { 2799 struct cgroup *cgrp; 2800 2801 cgrp = __cgroup_get_from_id(cgid); 2802 if (IS_ERR(cgrp)) 2803 return NULL; 2804 return cgrp; 2805 } 2806 2807 /** 2808 * bpf_task_under_cgroup - wrap task_under_cgroup_hierarchy() as a kfunc, test 2809 * task's membership of cgroup ancestry. 2810 * @task: the task to be tested 2811 * @ancestor: possible ancestor of @task's cgroup 2812 * 2813 * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor. 2814 * It follows all the same rules as cgroup_is_descendant, and only applies 2815 * to the default hierarchy. 2816 */ 2817 __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task, 2818 struct cgroup *ancestor) 2819 { 2820 long ret; 2821 2822 rcu_read_lock(); 2823 ret = task_under_cgroup_hierarchy(task, ancestor); 2824 rcu_read_unlock(); 2825 return ret; 2826 } 2827 2828 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) 2829 { 2830 struct bpf_array *array = container_of(map, struct bpf_array, map); 2831 struct cgroup *cgrp; 2832 2833 if (unlikely(idx >= array->map.max_entries)) 2834 return -E2BIG; 2835 2836 cgrp = READ_ONCE(array->ptrs[idx]); 2837 if (unlikely(!cgrp)) 2838 return -EAGAIN; 2839 2840 return task_under_cgroup_hierarchy(current, cgrp); 2841 } 2842 2843 const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { 2844 .func = bpf_current_task_under_cgroup, 2845 .gpl_only = false, 2846 .ret_type = RET_INTEGER, 2847 .arg1_type = ARG_CONST_MAP_PTR, 2848 .arg2_type = ARG_ANYTHING, 2849 }; 2850 2851 /** 2852 * bpf_task_get_cgroup1 - Acquires the associated cgroup of a task within a 2853 * specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its 2854 * hierarchy ID. 2855 * @task: The target task 2856 * @hierarchy_id: The ID of a cgroup1 hierarchy 2857 * 2858 * On success, the cgroup is returen. On failure, NULL is returned. 2859 */ 2860 __bpf_kfunc struct cgroup * 2861 bpf_task_get_cgroup1(struct task_struct *task, int hierarchy_id) 2862 { 2863 struct cgroup *cgrp = task_get_cgroup1(task, hierarchy_id); 2864 2865 if (IS_ERR(cgrp)) 2866 return NULL; 2867 return cgrp; 2868 } 2869 #endif /* CONFIG_CGROUPS */ 2870 2871 /** 2872 * bpf_task_from_pid - Find a struct task_struct from its pid by looking it up 2873 * in the root pid namespace idr. If a task is returned, it must either be 2874 * stored in a map, or released with bpf_task_release(). 2875 * @pid: The pid of the task being looked up. 2876 */ 2877 __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) 2878 { 2879 struct task_struct *p; 2880 2881 rcu_read_lock(); 2882 p = find_task_by_pid_ns(pid, &init_pid_ns); 2883 if (p) 2884 p = bpf_task_acquire(p); 2885 rcu_read_unlock(); 2886 2887 return p; 2888 } 2889 2890 /** 2891 * bpf_task_from_vpid - Find a struct task_struct from its vpid by looking it up 2892 * in the pid namespace of the current task. If a task is returned, it must 2893 * either be stored in a map, or released with bpf_task_release(). 2894 * @vpid: The vpid of the task being looked up. 2895 */ 2896 __bpf_kfunc struct task_struct *bpf_task_from_vpid(s32 vpid) 2897 { 2898 struct task_struct *p; 2899 2900 rcu_read_lock(); 2901 p = find_task_by_vpid(vpid); 2902 if (p) 2903 p = bpf_task_acquire(p); 2904 rcu_read_unlock(); 2905 2906 return p; 2907 } 2908 2909 /** 2910 * bpf_dynptr_slice() - Obtain a read-only pointer to the dynptr data. 2911 * @p: The dynptr whose data slice to retrieve 2912 * @offset: Offset into the dynptr 2913 * @buffer__nullable: User-provided buffer to copy contents into. May be NULL 2914 * @buffer__szk: Size (in bytes) of the buffer if present. This is the 2915 * length of the requested slice. This must be a constant. 2916 * 2917 * For non-skb and non-xdp type dynptrs, there is no difference between 2918 * bpf_dynptr_slice and bpf_dynptr_data. 2919 * 2920 * If buffer__nullable is NULL, the call will fail if buffer_opt was needed. 2921 * 2922 * If the intention is to write to the data slice, please use 2923 * bpf_dynptr_slice_rdwr. 2924 * 2925 * The user must check that the returned pointer is not null before using it. 2926 * 2927 * Please note that in the case of skb and xdp dynptrs, bpf_dynptr_slice 2928 * does not change the underlying packet data pointers, so a call to 2929 * bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in 2930 * the bpf program. 2931 * 2932 * Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only 2933 * data slice (can be either direct pointer to the data or a pointer to the user 2934 * provided buffer, with its contents containing the data, if unable to obtain 2935 * direct pointer) 2936 */ 2937 __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr *p, u64 offset, 2938 void *buffer__nullable, u64 buffer__szk) 2939 { 2940 const struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 2941 enum bpf_dynptr_type type; 2942 u64 len = buffer__szk; 2943 int err; 2944 2945 if (!ptr->data) 2946 return NULL; 2947 2948 err = bpf_dynptr_check_off_len(ptr, offset, len); 2949 if (err) 2950 return NULL; 2951 2952 type = bpf_dynptr_get_type(ptr); 2953 2954 switch (type) { 2955 case BPF_DYNPTR_TYPE_LOCAL: 2956 case BPF_DYNPTR_TYPE_RINGBUF: 2957 return ptr->data + ptr->offset + offset; 2958 case BPF_DYNPTR_TYPE_SKB: 2959 if (buffer__nullable) 2960 return skb_header_pointer(ptr->data, ptr->offset + offset, len, buffer__nullable); 2961 else 2962 return skb_pointer_if_linear(ptr->data, ptr->offset + offset, len); 2963 case BPF_DYNPTR_TYPE_XDP: 2964 { 2965 void *xdp_ptr = bpf_xdp_pointer(ptr->data, ptr->offset + offset, len); 2966 if (!IS_ERR_OR_NULL(xdp_ptr)) 2967 return xdp_ptr; 2968 2969 if (!buffer__nullable) 2970 return NULL; 2971 bpf_xdp_copy_buf(ptr->data, ptr->offset + offset, buffer__nullable, len, false); 2972 return buffer__nullable; 2973 } 2974 case BPF_DYNPTR_TYPE_SKB_META: 2975 return bpf_skb_meta_pointer(ptr->data, ptr->offset + offset); 2976 case BPF_DYNPTR_TYPE_FILE: 2977 err = bpf_file_fetch_bytes(ptr->data, offset, buffer__nullable, buffer__szk); 2978 return err ? NULL : buffer__nullable; 2979 default: 2980 WARN_ONCE(true, "unknown dynptr type %d\n", type); 2981 return NULL; 2982 } 2983 } 2984 2985 /** 2986 * bpf_dynptr_slice_rdwr() - Obtain a writable pointer to the dynptr data. 2987 * @p: The dynptr whose data slice to retrieve 2988 * @offset: Offset into the dynptr 2989 * @buffer__nullable: User-provided buffer to copy contents into. May be NULL 2990 * @buffer__szk: Size (in bytes) of the buffer if present. This is the 2991 * length of the requested slice. This must be a constant. 2992 * 2993 * For non-skb and non-xdp type dynptrs, there is no difference between 2994 * bpf_dynptr_slice and bpf_dynptr_data. 2995 * 2996 * If buffer__nullable is NULL, the call will fail if buffer_opt was needed. 2997 * 2998 * The returned pointer is writable and may point to either directly the dynptr 2999 * data at the requested offset or to the buffer if unable to obtain a direct 3000 * data pointer to (example: the requested slice is to the paged area of an skb 3001 * packet). In the case where the returned pointer is to the buffer, the user 3002 * is responsible for persisting writes through calling bpf_dynptr_write(). This 3003 * usually looks something like this pattern: 3004 * 3005 * struct eth_hdr *eth = bpf_dynptr_slice_rdwr(&dynptr, 0, buffer, sizeof(buffer)); 3006 * if (!eth) 3007 * return TC_ACT_SHOT; 3008 * 3009 * // mutate eth header // 3010 * 3011 * if (eth == buffer) 3012 * bpf_dynptr_write(&ptr, 0, buffer, sizeof(buffer), 0); 3013 * 3014 * Please note that, as in the example above, the user must check that the 3015 * returned pointer is not null before using it. 3016 * 3017 * Please also note that in the case of skb and xdp dynptrs, bpf_dynptr_slice_rdwr 3018 * does not change the underlying packet data pointers, so a call to 3019 * bpf_dynptr_slice_rdwr will not invalidate any ctx->data/data_end pointers in 3020 * the bpf program. 3021 * 3022 * Return: NULL if the call failed (eg invalid dynptr), pointer to a 3023 * data slice (can be either direct pointer to the data or a pointer to the user 3024 * provided buffer, with its contents containing the data, if unable to obtain 3025 * direct pointer) 3026 */ 3027 __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr *p, u64 offset, 3028 void *buffer__nullable, u64 buffer__szk) 3029 { 3030 const struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 3031 3032 if (!ptr->data || __bpf_dynptr_is_rdonly(ptr)) 3033 return NULL; 3034 3035 /* bpf_dynptr_slice_rdwr is the same logic as bpf_dynptr_slice. 3036 * 3037 * For skb-type dynptrs, it is safe to write into the returned pointer 3038 * if the bpf program allows skb data writes. There are two possibilities 3039 * that may occur when calling bpf_dynptr_slice_rdwr: 3040 * 3041 * 1) The requested slice is in the head of the skb. In this case, the 3042 * returned pointer is directly to skb data, and if the skb is cloned, the 3043 * verifier will have uncloned it (see bpf_unclone_prologue()) already. 3044 * The pointer can be directly written into. 3045 * 3046 * 2) Some portion of the requested slice is in the paged buffer area. 3047 * In this case, the requested data will be copied out into the buffer 3048 * and the returned pointer will be a pointer to the buffer. The skb 3049 * will not be pulled. To persist the write, the user will need to call 3050 * bpf_dynptr_write(), which will pull the skb and commit the write. 3051 * 3052 * Similarly for xdp programs, if the requested slice is not across xdp 3053 * fragments, then a direct pointer will be returned, otherwise the data 3054 * will be copied out into the buffer and the user will need to call 3055 * bpf_dynptr_write() to commit changes. 3056 */ 3057 return bpf_dynptr_slice(p, offset, buffer__nullable, buffer__szk); 3058 } 3059 3060 __bpf_kfunc int bpf_dynptr_adjust(const struct bpf_dynptr *p, u64 start, u64 end) 3061 { 3062 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 3063 u64 size; 3064 3065 if (!ptr->data || start > end) 3066 return -EINVAL; 3067 3068 size = __bpf_dynptr_size(ptr); 3069 3070 if (start > size || end > size) 3071 return -ERANGE; 3072 3073 bpf_dynptr_advance_offset(ptr, start); 3074 bpf_dynptr_set_size(ptr, end - start); 3075 3076 return 0; 3077 } 3078 3079 __bpf_kfunc bool bpf_dynptr_is_null(const struct bpf_dynptr *p) 3080 { 3081 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 3082 3083 return !ptr->data; 3084 } 3085 3086 __bpf_kfunc bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *p) 3087 { 3088 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 3089 3090 if (!ptr->data) 3091 return false; 3092 3093 return __bpf_dynptr_is_rdonly(ptr); 3094 } 3095 3096 __bpf_kfunc u64 bpf_dynptr_size(const struct bpf_dynptr *p) 3097 { 3098 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 3099 3100 if (!ptr->data) 3101 return -EINVAL; 3102 3103 return __bpf_dynptr_size(ptr); 3104 } 3105 3106 __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p, 3107 struct bpf_dynptr *clone__uninit) 3108 { 3109 struct bpf_dynptr_kern *clone = (struct bpf_dynptr_kern *)clone__uninit; 3110 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 3111 3112 if (!ptr->data) { 3113 bpf_dynptr_set_null(clone); 3114 return -EINVAL; 3115 } 3116 3117 *clone = *ptr; 3118 3119 return 0; 3120 } 3121 3122 /** 3123 * bpf_dynptr_copy() - Copy data from one dynptr to another. 3124 * @dst_ptr: Destination dynptr - where data should be copied to 3125 * @dst_off: Offset into the destination dynptr 3126 * @src_ptr: Source dynptr - where data should be copied from 3127 * @src_off: Offset into the source dynptr 3128 * @size: Length of the data to copy from source to destination 3129 * 3130 * Copies data from source dynptr to destination dynptr. 3131 * Returns 0 on success; negative error, otherwise. 3132 */ 3133 __bpf_kfunc int bpf_dynptr_copy(struct bpf_dynptr *dst_ptr, u64 dst_off, 3134 struct bpf_dynptr *src_ptr, u64 src_off, u64 size) 3135 { 3136 struct bpf_dynptr_kern *dst = (struct bpf_dynptr_kern *)dst_ptr; 3137 struct bpf_dynptr_kern *src = (struct bpf_dynptr_kern *)src_ptr; 3138 void *src_slice, *dst_slice; 3139 char buf[256]; 3140 u64 off; 3141 3142 src_slice = bpf_dynptr_slice(src_ptr, src_off, NULL, size); 3143 dst_slice = bpf_dynptr_slice_rdwr(dst_ptr, dst_off, NULL, size); 3144 3145 if (src_slice && dst_slice) { 3146 memmove(dst_slice, src_slice, size); 3147 return 0; 3148 } 3149 3150 if (src_slice) 3151 return __bpf_dynptr_write(dst, dst_off, src_slice, size, 0); 3152 3153 if (dst_slice) 3154 return __bpf_dynptr_read(dst_slice, size, src, src_off, 0); 3155 3156 if (bpf_dynptr_check_off_len(dst, dst_off, size) || 3157 bpf_dynptr_check_off_len(src, src_off, size)) 3158 return -E2BIG; 3159 3160 off = 0; 3161 while (off < size) { 3162 u64 chunk_sz = min_t(u64, sizeof(buf), size - off); 3163 int err; 3164 3165 err = __bpf_dynptr_read(buf, chunk_sz, src, src_off + off, 0); 3166 if (err) 3167 return err; 3168 err = __bpf_dynptr_write(dst, dst_off + off, buf, chunk_sz, 0); 3169 if (err) 3170 return err; 3171 3172 off += chunk_sz; 3173 } 3174 return 0; 3175 } 3176 3177 /** 3178 * bpf_dynptr_memset() - Fill dynptr memory with a constant byte. 3179 * @p: Destination dynptr - where data will be filled 3180 * @offset: Offset into the dynptr to start filling from 3181 * @size: Number of bytes to fill 3182 * @val: Constant byte to fill the memory with 3183 * 3184 * Fills the @size bytes of the memory area pointed to by @p 3185 * at @offset with the constant byte @val. 3186 * Returns 0 on success; negative error, otherwise. 3187 */ 3188 __bpf_kfunc int bpf_dynptr_memset(struct bpf_dynptr *p, u64 offset, u64 size, u8 val) 3189 { 3190 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)p; 3191 u64 chunk_sz, write_off; 3192 char buf[256]; 3193 void* slice; 3194 int err; 3195 3196 slice = bpf_dynptr_slice_rdwr(p, offset, NULL, size); 3197 if (likely(slice)) { 3198 memset(slice, val, size); 3199 return 0; 3200 } 3201 3202 if (__bpf_dynptr_is_rdonly(ptr)) 3203 return -EINVAL; 3204 3205 err = bpf_dynptr_check_off_len(ptr, offset, size); 3206 if (err) 3207 return err; 3208 3209 /* Non-linear data under the dynptr, write from a local buffer */ 3210 chunk_sz = min_t(u64, sizeof(buf), size); 3211 memset(buf, val, chunk_sz); 3212 3213 for (write_off = 0; write_off < size; write_off += chunk_sz) { 3214 chunk_sz = min_t(u64, sizeof(buf), size - write_off); 3215 err = __bpf_dynptr_write(ptr, offset + write_off, buf, chunk_sz, 0); 3216 if (err) 3217 return err; 3218 } 3219 3220 return 0; 3221 } 3222 3223 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj) 3224 { 3225 return obj; 3226 } 3227 3228 __bpf_kfunc void *bpf_rdonly_cast(const void *obj__ign, u32 btf_id__k) 3229 { 3230 return (void *)obj__ign; 3231 } 3232 3233 __bpf_kfunc void bpf_rcu_read_lock(void) 3234 { 3235 rcu_read_lock(); 3236 } 3237 3238 __bpf_kfunc void bpf_rcu_read_unlock(void) 3239 { 3240 rcu_read_unlock(); 3241 } 3242 3243 struct bpf_throw_ctx { 3244 struct bpf_prog_aux *aux; 3245 u64 sp; 3246 u64 bp; 3247 int cnt; 3248 }; 3249 3250 static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp) 3251 { 3252 struct bpf_throw_ctx *ctx = cookie; 3253 struct bpf_prog *prog; 3254 3255 /* 3256 * The RCU read lock is held to safely traverse the latch tree, but we 3257 * don't need its protection when accessing the prog, since it has an 3258 * active stack frame on the current stack trace, and won't disappear. 3259 */ 3260 rcu_read_lock(); 3261 prog = bpf_prog_ksym_find(ip); 3262 rcu_read_unlock(); 3263 if (!prog) 3264 return !ctx->cnt; 3265 ctx->cnt++; 3266 if (bpf_is_subprog(prog)) 3267 return true; 3268 ctx->aux = prog->aux; 3269 ctx->sp = sp; 3270 ctx->bp = bp; 3271 return false; 3272 } 3273 3274 __bpf_kfunc void bpf_throw(u64 cookie) 3275 { 3276 struct bpf_throw_ctx ctx = {}; 3277 3278 arch_bpf_stack_walk(bpf_stack_walker, &ctx); 3279 WARN_ON_ONCE(!ctx.aux); 3280 if (ctx.aux) 3281 WARN_ON_ONCE(!ctx.aux->exception_boundary); 3282 WARN_ON_ONCE(!ctx.bp); 3283 WARN_ON_ONCE(!ctx.cnt); 3284 /* Prevent KASAN false positives for CONFIG_KASAN_STACK by unpoisoning 3285 * deeper stack depths than ctx.sp as we do not return from bpf_throw, 3286 * which skips compiler generated instrumentation to do the same. 3287 */ 3288 kasan_unpoison_task_stack_below((void *)(long)ctx.sp); 3289 ctx.aux->bpf_exception_cb(cookie, ctx.sp, ctx.bp, 0, 0); 3290 WARN(1, "A call to BPF exception callback should never return\n"); 3291 } 3292 3293 __bpf_kfunc int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) 3294 { 3295 struct bpf_async_kern *async = (struct bpf_async_kern *)wq; 3296 struct bpf_map *map = p__map; 3297 3298 BUILD_BUG_ON(sizeof(struct bpf_async_kern) > sizeof(struct bpf_wq)); 3299 BUILD_BUG_ON(__alignof__(struct bpf_async_kern) != __alignof__(struct bpf_wq)); 3300 3301 if (flags) 3302 return -EINVAL; 3303 3304 return __bpf_async_init(async, map, flags, BPF_ASYNC_TYPE_WQ); 3305 } 3306 3307 __bpf_kfunc int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) 3308 { 3309 struct bpf_async_kern *async = (struct bpf_async_kern *)wq; 3310 struct bpf_work *w; 3311 3312 if (flags) 3313 return -EINVAL; 3314 3315 w = READ_ONCE(async->work); 3316 if (!w || !READ_ONCE(w->cb.prog)) 3317 return -EINVAL; 3318 3319 if (!refcount_inc_not_zero(&w->cb.refcnt)) 3320 return -ENOENT; 3321 3322 if (!defer_timer_wq_op()) { 3323 schedule_work(&w->work); 3324 bpf_async_refcount_put(&w->cb); 3325 return 0; 3326 } else { 3327 return bpf_async_schedule_op(&w->cb, BPF_ASYNC_START, 0, 0); 3328 } 3329 } 3330 3331 __bpf_kfunc int bpf_wq_set_callback(struct bpf_wq *wq, 3332 int (callback_fn)(void *map, int *key, void *value), 3333 unsigned int flags, 3334 struct bpf_prog_aux *aux) 3335 { 3336 struct bpf_async_kern *async = (struct bpf_async_kern *)wq; 3337 3338 if (flags) 3339 return -EINVAL; 3340 3341 return __bpf_async_set_callback(async, callback_fn, aux->prog); 3342 } 3343 3344 __bpf_kfunc void bpf_preempt_disable(void) 3345 { 3346 preempt_disable(); 3347 } 3348 3349 __bpf_kfunc void bpf_preempt_enable(void) 3350 { 3351 preempt_enable(); 3352 } 3353 3354 struct bpf_iter_bits { 3355 __u64 __opaque[2]; 3356 } __aligned(8); 3357 3358 #define BITS_ITER_NR_WORDS_MAX 511 3359 3360 struct bpf_iter_bits_kern { 3361 union { 3362 __u64 *bits; 3363 __u64 bits_copy; 3364 }; 3365 int nr_bits; 3366 int bit; 3367 } __aligned(8); 3368 3369 /* On 64-bit hosts, unsigned long and u64 have the same size, so passing 3370 * a u64 pointer and an unsigned long pointer to find_next_bit() will 3371 * return the same result, as both point to the same 8-byte area. 3372 * 3373 * For 32-bit little-endian hosts, using a u64 pointer or unsigned long 3374 * pointer also makes no difference. This is because the first iterated 3375 * unsigned long is composed of bits 0-31 of the u64 and the second unsigned 3376 * long is composed of bits 32-63 of the u64. 3377 * 3378 * However, for 32-bit big-endian hosts, this is not the case. The first 3379 * iterated unsigned long will be bits 32-63 of the u64, so swap these two 3380 * ulong values within the u64. 3381 */ 3382 static void swap_ulong_in_u64(u64 *bits, unsigned int nr) 3383 { 3384 #if (BITS_PER_LONG == 32) && defined(__BIG_ENDIAN) 3385 unsigned int i; 3386 3387 for (i = 0; i < nr; i++) 3388 bits[i] = (bits[i] >> 32) | ((u64)(u32)bits[i] << 32); 3389 #endif 3390 } 3391 3392 /** 3393 * bpf_iter_bits_new() - Initialize a new bits iterator for a given memory area 3394 * @it: The new bpf_iter_bits to be created 3395 * @unsafe_ptr__ign: A pointer pointing to a memory area to be iterated over 3396 * @nr_words: The size of the specified memory area, measured in 8-byte units. 3397 * The maximum value of @nr_words is @BITS_ITER_NR_WORDS_MAX. This limit may be 3398 * further reduced by the BPF memory allocator implementation. 3399 * 3400 * This function initializes a new bpf_iter_bits structure for iterating over 3401 * a memory area which is specified by the @unsafe_ptr__ign and @nr_words. It 3402 * copies the data of the memory area to the newly created bpf_iter_bits @it for 3403 * subsequent iteration operations. 3404 * 3405 * On success, 0 is returned. On failure, ERR is returned. 3406 */ 3407 __bpf_kfunc int 3408 bpf_iter_bits_new(struct bpf_iter_bits *it, const u64 *unsafe_ptr__ign, u32 nr_words) 3409 { 3410 struct bpf_iter_bits_kern *kit = (void *)it; 3411 u32 nr_bytes = nr_words * sizeof(u64); 3412 u32 nr_bits = BYTES_TO_BITS(nr_bytes); 3413 int err; 3414 3415 BUILD_BUG_ON(sizeof(struct bpf_iter_bits_kern) != sizeof(struct bpf_iter_bits)); 3416 BUILD_BUG_ON(__alignof__(struct bpf_iter_bits_kern) != 3417 __alignof__(struct bpf_iter_bits)); 3418 3419 kit->nr_bits = 0; 3420 kit->bits_copy = 0; 3421 kit->bit = -1; 3422 3423 if (!unsafe_ptr__ign || !nr_words) 3424 return -EINVAL; 3425 if (nr_words > BITS_ITER_NR_WORDS_MAX) 3426 return -E2BIG; 3427 3428 /* Optimization for u64 mask */ 3429 if (nr_bits == 64) { 3430 err = bpf_probe_read_kernel_common(&kit->bits_copy, nr_bytes, unsafe_ptr__ign); 3431 if (err) 3432 return -EFAULT; 3433 3434 swap_ulong_in_u64(&kit->bits_copy, nr_words); 3435 3436 kit->nr_bits = nr_bits; 3437 return 0; 3438 } 3439 3440 if (bpf_mem_alloc_check_size(false, nr_bytes)) 3441 return -E2BIG; 3442 3443 /* Fallback to memalloc */ 3444 kit->bits = bpf_mem_alloc(&bpf_global_ma, nr_bytes); 3445 if (!kit->bits) 3446 return -ENOMEM; 3447 3448 err = bpf_probe_read_kernel_common(kit->bits, nr_bytes, unsafe_ptr__ign); 3449 if (err) { 3450 bpf_mem_free(&bpf_global_ma, kit->bits); 3451 return err; 3452 } 3453 3454 swap_ulong_in_u64(kit->bits, nr_words); 3455 3456 kit->nr_bits = nr_bits; 3457 return 0; 3458 } 3459 3460 /** 3461 * bpf_iter_bits_next() - Get the next bit in a bpf_iter_bits 3462 * @it: The bpf_iter_bits to be checked 3463 * 3464 * This function returns a pointer to a number representing the value of the 3465 * next bit in the bits. 3466 * 3467 * If there are no further bits available, it returns NULL. 3468 */ 3469 __bpf_kfunc int *bpf_iter_bits_next(struct bpf_iter_bits *it) 3470 { 3471 struct bpf_iter_bits_kern *kit = (void *)it; 3472 int bit = kit->bit, nr_bits = kit->nr_bits; 3473 const void *bits; 3474 3475 if (!nr_bits || bit >= nr_bits) 3476 return NULL; 3477 3478 bits = nr_bits == 64 ? &kit->bits_copy : kit->bits; 3479 bit = find_next_bit(bits, nr_bits, bit + 1); 3480 if (bit >= nr_bits) { 3481 kit->bit = bit; 3482 return NULL; 3483 } 3484 3485 kit->bit = bit; 3486 return &kit->bit; 3487 } 3488 3489 /** 3490 * bpf_iter_bits_destroy() - Destroy a bpf_iter_bits 3491 * @it: The bpf_iter_bits to be destroyed 3492 * 3493 * Destroy the resource associated with the bpf_iter_bits. 3494 */ 3495 __bpf_kfunc void bpf_iter_bits_destroy(struct bpf_iter_bits *it) 3496 { 3497 struct bpf_iter_bits_kern *kit = (void *)it; 3498 3499 if (kit->nr_bits <= 64) 3500 return; 3501 bpf_mem_free(&bpf_global_ma, kit->bits); 3502 } 3503 3504 /** 3505 * bpf_copy_from_user_str() - Copy a string from an unsafe user address 3506 * @dst: Destination address, in kernel space. This buffer must be 3507 * at least @dst__sz bytes long. 3508 * @dst__sz: Maximum number of bytes to copy, includes the trailing NUL. 3509 * @unsafe_ptr__ign: Source address, in user space. 3510 * @flags: The only supported flag is BPF_F_PAD_ZEROS 3511 * 3512 * Copies a NUL-terminated string from userspace to BPF space. If user string is 3513 * too long this will still ensure zero termination in the dst buffer unless 3514 * buffer size is 0. 3515 * 3516 * If BPF_F_PAD_ZEROS flag is set, memset the tail of @dst to 0 on success and 3517 * memset all of @dst on failure. 3518 */ 3519 __bpf_kfunc int bpf_copy_from_user_str(void *dst, u32 dst__sz, const void __user *unsafe_ptr__ign, u64 flags) 3520 { 3521 int ret; 3522 3523 if (unlikely(flags & ~BPF_F_PAD_ZEROS)) 3524 return -EINVAL; 3525 3526 if (unlikely(!dst__sz)) 3527 return 0; 3528 3529 ret = strncpy_from_user(dst, unsafe_ptr__ign, dst__sz - 1); 3530 if (ret < 0) { 3531 if (flags & BPF_F_PAD_ZEROS) 3532 memset((char *)dst, 0, dst__sz); 3533 3534 return ret; 3535 } 3536 3537 if (flags & BPF_F_PAD_ZEROS) 3538 memset((char *)dst + ret, 0, dst__sz - ret); 3539 else 3540 ((char *)dst)[ret] = '\0'; 3541 3542 return ret + 1; 3543 } 3544 3545 /** 3546 * bpf_copy_from_user_task_str() - Copy a string from an task's address space 3547 * @dst: Destination address, in kernel space. This buffer must be 3548 * at least @dst__sz bytes long. 3549 * @dst__sz: Maximum number of bytes to copy, includes the trailing NUL. 3550 * @unsafe_ptr__ign: Source address in the task's address space. 3551 * @tsk: The task whose address space will be used 3552 * @flags: The only supported flag is BPF_F_PAD_ZEROS 3553 * 3554 * Copies a NUL terminated string from a task's address space to @dst__sz 3555 * buffer. If user string is too long this will still ensure zero termination 3556 * in the @dst__sz buffer unless buffer size is 0. 3557 * 3558 * If BPF_F_PAD_ZEROS flag is set, memset the tail of @dst__sz to 0 on success 3559 * and memset all of @dst__sz on failure. 3560 * 3561 * Return: The number of copied bytes on success including the NUL terminator. 3562 * A negative error code on failure. 3563 */ 3564 __bpf_kfunc int bpf_copy_from_user_task_str(void *dst, u32 dst__sz, 3565 const void __user *unsafe_ptr__ign, 3566 struct task_struct *tsk, u64 flags) 3567 { 3568 int ret; 3569 3570 if (unlikely(flags & ~BPF_F_PAD_ZEROS)) 3571 return -EINVAL; 3572 3573 if (unlikely(dst__sz == 0)) 3574 return 0; 3575 3576 ret = copy_remote_vm_str(tsk, (unsigned long)unsafe_ptr__ign, dst, dst__sz, 0); 3577 if (ret < 0) { 3578 if (flags & BPF_F_PAD_ZEROS) 3579 memset(dst, 0, dst__sz); 3580 return ret; 3581 } 3582 3583 if (flags & BPF_F_PAD_ZEROS) 3584 memset(dst + ret, 0, dst__sz - ret); 3585 3586 return ret + 1; 3587 } 3588 3589 /* Keep unsinged long in prototype so that kfunc is usable when emitted to 3590 * vmlinux.h in BPF programs directly, but note that while in BPF prog, the 3591 * unsigned long always points to 8-byte region on stack, the kernel may only 3592 * read and write the 4-bytes on 32-bit. 3593 */ 3594 __bpf_kfunc void bpf_local_irq_save(unsigned long *flags__irq_flag) 3595 { 3596 local_irq_save(*flags__irq_flag); 3597 } 3598 3599 __bpf_kfunc void bpf_local_irq_restore(unsigned long *flags__irq_flag) 3600 { 3601 local_irq_restore(*flags__irq_flag); 3602 } 3603 3604 __bpf_kfunc void __bpf_trap(void) 3605 { 3606 } 3607 3608 /* 3609 * Kfuncs for string operations. 3610 * 3611 * Since strings are not necessarily %NUL-terminated, we cannot directly call 3612 * in-kernel implementations. Instead, we open-code the implementations using 3613 * __get_kernel_nofault instead of plain dereference to make them safe. 3614 */ 3615 3616 static int __bpf_strncasecmp(const char *s1, const char *s2, bool ignore_case, size_t len) 3617 { 3618 char c1, c2; 3619 int i; 3620 3621 if (!copy_from_kernel_nofault_allowed(s1, 1) || 3622 !copy_from_kernel_nofault_allowed(s2, 1)) { 3623 return -ERANGE; 3624 } 3625 3626 guard(pagefault)(); 3627 for (i = 0; i < len && i < XATTR_SIZE_MAX; i++) { 3628 __get_kernel_nofault(&c1, s1, char, err_out); 3629 __get_kernel_nofault(&c2, s2, char, err_out); 3630 if (ignore_case) { 3631 c1 = tolower(c1); 3632 c2 = tolower(c2); 3633 } 3634 if (c1 != c2) 3635 return c1 < c2 ? -1 : 1; 3636 if (c1 == '\0') 3637 return 0; 3638 s1++; 3639 s2++; 3640 } 3641 return i == XATTR_SIZE_MAX ? -E2BIG : 0; 3642 err_out: 3643 return -EFAULT; 3644 } 3645 3646 /** 3647 * bpf_strcmp - Compare two strings 3648 * @s1__ign: One string 3649 * @s2__ign: Another string 3650 * 3651 * Return: 3652 * * %0 - Strings are equal 3653 * * %-1 - @s1__ign is smaller 3654 * * %1 - @s2__ign is smaller 3655 * * %-EFAULT - Cannot read one of the strings 3656 * * %-E2BIG - One of strings is too large 3657 * * %-ERANGE - One of strings is outside of kernel address space 3658 */ 3659 __bpf_kfunc int bpf_strcmp(const char *s1__ign, const char *s2__ign) 3660 { 3661 return __bpf_strncasecmp(s1__ign, s2__ign, false, XATTR_SIZE_MAX); 3662 } 3663 3664 /** 3665 * bpf_strcasecmp - Compare two strings, ignoring the case of the characters 3666 * @s1__ign: One string 3667 * @s2__ign: Another string 3668 * 3669 * Return: 3670 * * %0 - Strings are equal 3671 * * %-1 - @s1__ign is smaller 3672 * * %1 - @s2__ign is smaller 3673 * * %-EFAULT - Cannot read one of the strings 3674 * * %-E2BIG - One of strings is too large 3675 * * %-ERANGE - One of strings is outside of kernel address space 3676 */ 3677 __bpf_kfunc int bpf_strcasecmp(const char *s1__ign, const char *s2__ign) 3678 { 3679 return __bpf_strncasecmp(s1__ign, s2__ign, true, XATTR_SIZE_MAX); 3680 } 3681 3682 /* 3683 * bpf_strncasecmp - Compare two length-limited strings, ignoring case 3684 * @s1__ign: One string 3685 * @s2__ign: Another string 3686 * @len: The maximum number of characters to compare 3687 * 3688 * Return: 3689 * * %0 - Strings are equal 3690 * * %-1 - @s1__ign is smaller 3691 * * %1 - @s2__ign is smaller 3692 * * %-EFAULT - Cannot read one of the strings 3693 * * %-E2BIG - One of strings is too large 3694 * * %-ERANGE - One of strings is outside of kernel address space 3695 */ 3696 __bpf_kfunc int bpf_strncasecmp(const char *s1__ign, const char *s2__ign, size_t len) 3697 { 3698 return __bpf_strncasecmp(s1__ign, s2__ign, true, len); 3699 } 3700 3701 /** 3702 * bpf_strnchr - Find a character in a length limited string 3703 * @s__ign: The string to be searched 3704 * @count: The number of characters to be searched 3705 * @c: The character to search for 3706 * 3707 * Note that the %NUL-terminator is considered part of the string, and can 3708 * be searched for. 3709 * 3710 * Return: 3711 * * >=0 - Index of the first occurrence of @c within @s__ign 3712 * * %-ENOENT - @c not found in the first @count characters of @s__ign 3713 * * %-EFAULT - Cannot read @s__ign 3714 * * %-E2BIG - @s__ign is too large 3715 * * %-ERANGE - @s__ign is outside of kernel address space 3716 */ 3717 __bpf_kfunc int bpf_strnchr(const char *s__ign, size_t count, char c) 3718 { 3719 char sc; 3720 int i; 3721 3722 if (!copy_from_kernel_nofault_allowed(s__ign, 1)) 3723 return -ERANGE; 3724 3725 guard(pagefault)(); 3726 for (i = 0; i < count && i < XATTR_SIZE_MAX; i++) { 3727 __get_kernel_nofault(&sc, s__ign, char, err_out); 3728 if (sc == c) 3729 return i; 3730 if (sc == '\0') 3731 return -ENOENT; 3732 s__ign++; 3733 } 3734 return i == XATTR_SIZE_MAX ? -E2BIG : -ENOENT; 3735 err_out: 3736 return -EFAULT; 3737 } 3738 3739 /** 3740 * bpf_strchr - Find the first occurrence of a character in a string 3741 * @s__ign: The string to be searched 3742 * @c: The character to search for 3743 * 3744 * Note that the %NUL-terminator is considered part of the string, and can 3745 * be searched for. 3746 * 3747 * Return: 3748 * * >=0 - The index of the first occurrence of @c within @s__ign 3749 * * %-ENOENT - @c not found in @s__ign 3750 * * %-EFAULT - Cannot read @s__ign 3751 * * %-E2BIG - @s__ign is too large 3752 * * %-ERANGE - @s__ign is outside of kernel address space 3753 */ 3754 __bpf_kfunc int bpf_strchr(const char *s__ign, char c) 3755 { 3756 return bpf_strnchr(s__ign, XATTR_SIZE_MAX, c); 3757 } 3758 3759 /** 3760 * bpf_strchrnul - Find and return a character in a string, or end of string 3761 * @s__ign: The string to be searched 3762 * @c: The character to search for 3763 * 3764 * Return: 3765 * * >=0 - Index of the first occurrence of @c within @s__ign or index of 3766 * the null byte at the end of @s__ign when @c is not found 3767 * * %-EFAULT - Cannot read @s__ign 3768 * * %-E2BIG - @s__ign is too large 3769 * * %-ERANGE - @s__ign is outside of kernel address space 3770 */ 3771 __bpf_kfunc int bpf_strchrnul(const char *s__ign, char c) 3772 { 3773 char sc; 3774 int i; 3775 3776 if (!copy_from_kernel_nofault_allowed(s__ign, 1)) 3777 return -ERANGE; 3778 3779 guard(pagefault)(); 3780 for (i = 0; i < XATTR_SIZE_MAX; i++) { 3781 __get_kernel_nofault(&sc, s__ign, char, err_out); 3782 if (sc == '\0' || sc == c) 3783 return i; 3784 s__ign++; 3785 } 3786 return -E2BIG; 3787 err_out: 3788 return -EFAULT; 3789 } 3790 3791 /** 3792 * bpf_strrchr - Find the last occurrence of a character in a string 3793 * @s__ign: The string to be searched 3794 * @c: The character to search for 3795 * 3796 * Return: 3797 * * >=0 - Index of the last occurrence of @c within @s__ign 3798 * * %-ENOENT - @c not found in @s__ign 3799 * * %-EFAULT - Cannot read @s__ign 3800 * * %-E2BIG - @s__ign is too large 3801 * * %-ERANGE - @s__ign is outside of kernel address space 3802 */ 3803 __bpf_kfunc int bpf_strrchr(const char *s__ign, int c) 3804 { 3805 char sc; 3806 int i, last = -ENOENT; 3807 3808 if (!copy_from_kernel_nofault_allowed(s__ign, 1)) 3809 return -ERANGE; 3810 3811 guard(pagefault)(); 3812 for (i = 0; i < XATTR_SIZE_MAX; i++) { 3813 __get_kernel_nofault(&sc, s__ign, char, err_out); 3814 if (sc == c) 3815 last = i; 3816 if (sc == '\0') 3817 return last; 3818 s__ign++; 3819 } 3820 return -E2BIG; 3821 err_out: 3822 return -EFAULT; 3823 } 3824 3825 /** 3826 * bpf_strnlen - Calculate the length of a length-limited string 3827 * @s__ign: The string 3828 * @count: The maximum number of characters to count 3829 * 3830 * Return: 3831 * * >=0 - The length of @s__ign 3832 * * %-EFAULT - Cannot read @s__ign 3833 * * %-E2BIG - @s__ign is too large 3834 * * %-ERANGE - @s__ign is outside of kernel address space 3835 */ 3836 __bpf_kfunc int bpf_strnlen(const char *s__ign, size_t count) 3837 { 3838 char c; 3839 int i; 3840 3841 if (!copy_from_kernel_nofault_allowed(s__ign, 1)) 3842 return -ERANGE; 3843 3844 guard(pagefault)(); 3845 for (i = 0; i < count && i < XATTR_SIZE_MAX; i++) { 3846 __get_kernel_nofault(&c, s__ign, char, err_out); 3847 if (c == '\0') 3848 return i; 3849 s__ign++; 3850 } 3851 return i == XATTR_SIZE_MAX ? -E2BIG : i; 3852 err_out: 3853 return -EFAULT; 3854 } 3855 3856 /** 3857 * bpf_strlen - Calculate the length of a string 3858 * @s__ign: The string 3859 * 3860 * Return: 3861 * * >=0 - The length of @s__ign 3862 * * %-EFAULT - Cannot read @s__ign 3863 * * %-E2BIG - @s__ign is too large 3864 * * %-ERANGE - @s__ign is outside of kernel address space 3865 */ 3866 __bpf_kfunc int bpf_strlen(const char *s__ign) 3867 { 3868 return bpf_strnlen(s__ign, XATTR_SIZE_MAX); 3869 } 3870 3871 /** 3872 * bpf_strspn - Calculate the length of the initial substring of @s__ign which 3873 * only contains letters in @accept__ign 3874 * @s__ign: The string to be searched 3875 * @accept__ign: The string to search for 3876 * 3877 * Return: 3878 * * >=0 - The length of the initial substring of @s__ign which only 3879 * contains letters from @accept__ign 3880 * * %-EFAULT - Cannot read one of the strings 3881 * * %-E2BIG - One of the strings is too large 3882 * * %-ERANGE - One of the strings is outside of kernel address space 3883 */ 3884 __bpf_kfunc int bpf_strspn(const char *s__ign, const char *accept__ign) 3885 { 3886 char cs, ca; 3887 int i, j; 3888 3889 if (!copy_from_kernel_nofault_allowed(s__ign, 1) || 3890 !copy_from_kernel_nofault_allowed(accept__ign, 1)) { 3891 return -ERANGE; 3892 } 3893 3894 guard(pagefault)(); 3895 for (i = 0; i < XATTR_SIZE_MAX; i++) { 3896 __get_kernel_nofault(&cs, s__ign, char, err_out); 3897 if (cs == '\0') 3898 return i; 3899 for (j = 0; j < XATTR_SIZE_MAX; j++) { 3900 __get_kernel_nofault(&ca, accept__ign + j, char, err_out); 3901 if (cs == ca || ca == '\0') 3902 break; 3903 } 3904 if (j == XATTR_SIZE_MAX) 3905 return -E2BIG; 3906 if (ca == '\0') 3907 return i; 3908 s__ign++; 3909 } 3910 return -E2BIG; 3911 err_out: 3912 return -EFAULT; 3913 } 3914 3915 /** 3916 * bpf_strcspn - Calculate the length of the initial substring of @s__ign which 3917 * does not contain letters in @reject__ign 3918 * @s__ign: The string to be searched 3919 * @reject__ign: The string to search for 3920 * 3921 * Return: 3922 * * >=0 - The length of the initial substring of @s__ign which does not 3923 * contain letters from @reject__ign 3924 * * %-EFAULT - Cannot read one of the strings 3925 * * %-E2BIG - One of the strings is too large 3926 * * %-ERANGE - One of the strings is outside of kernel address space 3927 */ 3928 __bpf_kfunc int bpf_strcspn(const char *s__ign, const char *reject__ign) 3929 { 3930 char cs, cr; 3931 int i, j; 3932 3933 if (!copy_from_kernel_nofault_allowed(s__ign, 1) || 3934 !copy_from_kernel_nofault_allowed(reject__ign, 1)) { 3935 return -ERANGE; 3936 } 3937 3938 guard(pagefault)(); 3939 for (i = 0; i < XATTR_SIZE_MAX; i++) { 3940 __get_kernel_nofault(&cs, s__ign, char, err_out); 3941 if (cs == '\0') 3942 return i; 3943 for (j = 0; j < XATTR_SIZE_MAX; j++) { 3944 __get_kernel_nofault(&cr, reject__ign + j, char, err_out); 3945 if (cs == cr || cr == '\0') 3946 break; 3947 } 3948 if (j == XATTR_SIZE_MAX) 3949 return -E2BIG; 3950 if (cr != '\0') 3951 return i; 3952 s__ign++; 3953 } 3954 return -E2BIG; 3955 err_out: 3956 return -EFAULT; 3957 } 3958 3959 static int __bpf_strnstr(const char *s1, const char *s2, size_t len, 3960 bool ignore_case) 3961 { 3962 char c1, c2; 3963 int i, j; 3964 3965 if (!copy_from_kernel_nofault_allowed(s1, 1) || 3966 !copy_from_kernel_nofault_allowed(s2, 1)) { 3967 return -ERANGE; 3968 } 3969 3970 guard(pagefault)(); 3971 for (i = 0; i < XATTR_SIZE_MAX; i++) { 3972 for (j = 0; i + j <= len && j < XATTR_SIZE_MAX; j++) { 3973 __get_kernel_nofault(&c2, s2 + j, char, err_out); 3974 if (c2 == '\0') 3975 return i; 3976 /* 3977 * We allow reading an extra byte from s2 (note the 3978 * `i + j <= len` above) to cover the case when s2 is 3979 * a suffix of the first len chars of s1. 3980 */ 3981 if (i + j == len) 3982 break; 3983 __get_kernel_nofault(&c1, s1 + j, char, err_out); 3984 3985 if (ignore_case) { 3986 c1 = tolower(c1); 3987 c2 = tolower(c2); 3988 } 3989 3990 if (c1 == '\0') 3991 return -ENOENT; 3992 if (c1 != c2) 3993 break; 3994 } 3995 if (j == XATTR_SIZE_MAX) 3996 return -E2BIG; 3997 if (i + j == len) 3998 return -ENOENT; 3999 s1++; 4000 } 4001 return -E2BIG; 4002 err_out: 4003 return -EFAULT; 4004 } 4005 4006 /** 4007 * bpf_strstr - Find the first substring in a string 4008 * @s1__ign: The string to be searched 4009 * @s2__ign: The string to search for 4010 * 4011 * Return: 4012 * * >=0 - Index of the first character of the first occurrence of @s2__ign 4013 * within @s1__ign 4014 * * %-ENOENT - @s2__ign is not a substring of @s1__ign 4015 * * %-EFAULT - Cannot read one of the strings 4016 * * %-E2BIG - One of the strings is too large 4017 * * %-ERANGE - One of the strings is outside of kernel address space 4018 */ 4019 __bpf_kfunc int bpf_strstr(const char *s1__ign, const char *s2__ign) 4020 { 4021 return __bpf_strnstr(s1__ign, s2__ign, XATTR_SIZE_MAX, false); 4022 } 4023 4024 /** 4025 * bpf_strcasestr - Find the first substring in a string, ignoring the case of 4026 * the characters 4027 * @s1__ign: The string to be searched 4028 * @s2__ign: The string to search for 4029 * 4030 * Return: 4031 * * >=0 - Index of the first character of the first occurrence of @s2__ign 4032 * within @s1__ign 4033 * * %-ENOENT - @s2__ign is not a substring of @s1__ign 4034 * * %-EFAULT - Cannot read one of the strings 4035 * * %-E2BIG - One of the strings is too large 4036 * * %-ERANGE - One of the strings is outside of kernel address space 4037 */ 4038 __bpf_kfunc int bpf_strcasestr(const char *s1__ign, const char *s2__ign) 4039 { 4040 return __bpf_strnstr(s1__ign, s2__ign, XATTR_SIZE_MAX, true); 4041 } 4042 4043 /** 4044 * bpf_strnstr - Find the first substring in a length-limited string 4045 * @s1__ign: The string to be searched 4046 * @s2__ign: The string to search for 4047 * @len: the maximum number of characters to search 4048 * 4049 * Return: 4050 * * >=0 - Index of the first character of the first occurrence of @s2__ign 4051 * within the first @len characters of @s1__ign 4052 * * %-ENOENT - @s2__ign not found in the first @len characters of @s1__ign 4053 * * %-EFAULT - Cannot read one of the strings 4054 * * %-E2BIG - One of the strings is too large 4055 * * %-ERANGE - One of the strings is outside of kernel address space 4056 */ 4057 __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, 4058 size_t len) 4059 { 4060 return __bpf_strnstr(s1__ign, s2__ign, len, false); 4061 } 4062 4063 /** 4064 * bpf_strncasestr - Find the first substring in a length-limited string, 4065 * ignoring the case of the characters 4066 * @s1__ign: The string to be searched 4067 * @s2__ign: The string to search for 4068 * @len: the maximum number of characters to search 4069 * 4070 * Return: 4071 * * >=0 - Index of the first character of the first occurrence of @s2__ign 4072 * within the first @len characters of @s1__ign 4073 * * %-ENOENT - @s2__ign not found in the first @len characters of @s1__ign 4074 * * %-EFAULT - Cannot read one of the strings 4075 * * %-E2BIG - One of the strings is too large 4076 * * %-ERANGE - One of the strings is outside of kernel address space 4077 */ 4078 __bpf_kfunc int bpf_strncasestr(const char *s1__ign, const char *s2__ign, 4079 size_t len) 4080 { 4081 return __bpf_strnstr(s1__ign, s2__ign, len, true); 4082 } 4083 4084 #ifdef CONFIG_KEYS 4085 /** 4086 * bpf_lookup_user_key - lookup a key by its serial 4087 * @serial: key handle serial number 4088 * @flags: lookup-specific flags 4089 * 4090 * Search a key with a given *serial* and the provided *flags*. 4091 * If found, increment the reference count of the key by one, and 4092 * return it in the bpf_key structure. 4093 * 4094 * The bpf_key structure must be passed to bpf_key_put() when done 4095 * with it, so that the key reference count is decremented and the 4096 * bpf_key structure is freed. 4097 * 4098 * Permission checks are deferred to the time the key is used by 4099 * one of the available key-specific kfuncs. 4100 * 4101 * Set *flags* with KEY_LOOKUP_CREATE, to attempt creating a requested 4102 * special keyring (e.g. session keyring), if it doesn't yet exist. 4103 * Set *flags* with KEY_LOOKUP_PARTIAL, to lookup a key without waiting 4104 * for the key construction, and to retrieve uninstantiated keys (keys 4105 * without data attached to them). 4106 * 4107 * Return: a bpf_key pointer with a valid key pointer if the key is found, a 4108 * NULL pointer otherwise. 4109 */ 4110 __bpf_kfunc struct bpf_key *bpf_lookup_user_key(s32 serial, u64 flags) 4111 { 4112 key_ref_t key_ref; 4113 struct bpf_key *bkey; 4114 4115 if (flags & ~KEY_LOOKUP_ALL) 4116 return NULL; 4117 4118 /* 4119 * Permission check is deferred until the key is used, as the 4120 * intent of the caller is unknown here. 4121 */ 4122 key_ref = lookup_user_key(serial, flags, KEY_DEFER_PERM_CHECK); 4123 if (IS_ERR(key_ref)) 4124 return NULL; 4125 4126 bkey = kmalloc_obj(*bkey); 4127 if (!bkey) { 4128 key_put(key_ref_to_ptr(key_ref)); 4129 return NULL; 4130 } 4131 4132 bkey->key = key_ref_to_ptr(key_ref); 4133 bkey->has_ref = true; 4134 4135 return bkey; 4136 } 4137 4138 /** 4139 * bpf_lookup_system_key - lookup a key by a system-defined ID 4140 * @id: key ID 4141 * 4142 * Obtain a bpf_key structure with a key pointer set to the passed key ID. 4143 * The key pointer is marked as invalid, to prevent bpf_key_put() from 4144 * attempting to decrement the key reference count on that pointer. The key 4145 * pointer set in such way is currently understood only by 4146 * verify_pkcs7_signature(). 4147 * 4148 * Set *id* to one of the values defined in include/linux/verification.h: 4149 * 0 for the primary keyring (immutable keyring of system keys); 4150 * VERIFY_USE_SECONDARY_KEYRING for both the primary and secondary keyring 4151 * (where keys can be added only if they are vouched for by existing keys 4152 * in those keyrings); VERIFY_USE_PLATFORM_KEYRING for the platform 4153 * keyring (primarily used by the integrity subsystem to verify a kexec'ed 4154 * kerned image and, possibly, the initramfs signature). 4155 * 4156 * Return: a bpf_key pointer with an invalid key pointer set from the 4157 * pre-determined ID on success, a NULL pointer otherwise 4158 */ 4159 __bpf_kfunc struct bpf_key *bpf_lookup_system_key(u64 id) 4160 { 4161 struct bpf_key *bkey; 4162 4163 if (system_keyring_id_check(id) < 0) 4164 return NULL; 4165 4166 bkey = kmalloc_obj(*bkey, GFP_ATOMIC); 4167 if (!bkey) 4168 return NULL; 4169 4170 bkey->key = (struct key *)(unsigned long)id; 4171 bkey->has_ref = false; 4172 4173 return bkey; 4174 } 4175 4176 /** 4177 * bpf_key_put - decrement key reference count if key is valid and free bpf_key 4178 * @bkey: bpf_key structure 4179 * 4180 * Decrement the reference count of the key inside *bkey*, if the pointer 4181 * is valid, and free *bkey*. 4182 */ 4183 __bpf_kfunc void bpf_key_put(struct bpf_key *bkey) 4184 { 4185 if (bkey->has_ref) 4186 key_put(bkey->key); 4187 4188 kfree(bkey); 4189 } 4190 4191 /** 4192 * bpf_verify_pkcs7_signature - verify a PKCS#7 signature 4193 * @data_p: data to verify 4194 * @sig_p: signature of the data 4195 * @trusted_keyring: keyring with keys trusted for signature verification 4196 * 4197 * Verify the PKCS#7 signature *sig_ptr* against the supplied *data_ptr* 4198 * with keys in a keyring referenced by *trusted_keyring*. 4199 * 4200 * Return: 0 on success, a negative value on error. 4201 */ 4202 __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_p, 4203 struct bpf_dynptr *sig_p, 4204 struct bpf_key *trusted_keyring) 4205 { 4206 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION 4207 struct bpf_dynptr_kern *data_ptr = (struct bpf_dynptr_kern *)data_p; 4208 struct bpf_dynptr_kern *sig_ptr = (struct bpf_dynptr_kern *)sig_p; 4209 const void *data, *sig; 4210 u32 data_len, sig_len; 4211 int ret; 4212 4213 if (trusted_keyring->has_ref) { 4214 /* 4215 * Do the permission check deferred in bpf_lookup_user_key(). 4216 * See bpf_lookup_user_key() for more details. 4217 * 4218 * A call to key_task_permission() here would be redundant, as 4219 * it is already done by keyring_search() called by 4220 * find_asymmetric_key(). 4221 */ 4222 ret = key_validate(trusted_keyring->key); 4223 if (ret < 0) 4224 return ret; 4225 } 4226 4227 data_len = __bpf_dynptr_size(data_ptr); 4228 data = __bpf_dynptr_data(data_ptr, data_len); 4229 sig_len = __bpf_dynptr_size(sig_ptr); 4230 sig = __bpf_dynptr_data(sig_ptr, sig_len); 4231 4232 return verify_pkcs7_signature(data, data_len, sig, sig_len, 4233 trusted_keyring->key, 4234 VERIFYING_BPF_SIGNATURE, NULL, 4235 NULL); 4236 #else 4237 return -EOPNOTSUPP; 4238 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */ 4239 } 4240 #endif /* CONFIG_KEYS */ 4241 4242 typedef int (*bpf_task_work_callback_t)(struct bpf_map *map, void *key, void *value); 4243 4244 enum bpf_task_work_state { 4245 /* bpf_task_work is ready to be used */ 4246 BPF_TW_STANDBY = 0, 4247 /* irq work scheduling in progress */ 4248 BPF_TW_PENDING, 4249 /* task work scheduling in progress */ 4250 BPF_TW_SCHEDULING, 4251 /* task work is scheduled successfully */ 4252 BPF_TW_SCHEDULED, 4253 /* callback is running */ 4254 BPF_TW_RUNNING, 4255 /* associated BPF map value is deleted */ 4256 BPF_TW_FREED, 4257 }; 4258 4259 struct bpf_task_work_ctx { 4260 enum bpf_task_work_state state; 4261 refcount_t refcnt; 4262 struct callback_head work; 4263 struct irq_work irq_work; 4264 /* bpf_prog that schedules task work */ 4265 struct bpf_prog *prog; 4266 /* task for which callback is scheduled */ 4267 struct task_struct *task; 4268 /* the map and map value associated with this context */ 4269 struct bpf_map *map; 4270 void *map_val; 4271 enum task_work_notify_mode mode; 4272 bpf_task_work_callback_t callback_fn; 4273 struct rcu_head rcu; 4274 } __aligned(8); 4275 4276 /* Actual type for struct bpf_task_work */ 4277 struct bpf_task_work_kern { 4278 struct bpf_task_work_ctx *ctx; 4279 }; 4280 4281 static void bpf_task_work_ctx_reset(struct bpf_task_work_ctx *ctx) 4282 { 4283 if (ctx->prog) { 4284 bpf_prog_put(ctx->prog); 4285 ctx->prog = NULL; 4286 } 4287 if (ctx->task) { 4288 bpf_task_release(ctx->task); 4289 ctx->task = NULL; 4290 } 4291 } 4292 4293 static bool bpf_task_work_ctx_tryget(struct bpf_task_work_ctx *ctx) 4294 { 4295 return refcount_inc_not_zero(&ctx->refcnt); 4296 } 4297 4298 static void bpf_task_work_destroy(struct irq_work *irq_work) 4299 { 4300 struct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work); 4301 4302 bpf_task_work_ctx_reset(ctx); 4303 kfree_rcu(ctx, rcu); 4304 } 4305 4306 static void bpf_task_work_ctx_put(struct bpf_task_work_ctx *ctx) 4307 { 4308 if (!refcount_dec_and_test(&ctx->refcnt)) 4309 return; 4310 4311 if (irqs_disabled()) { 4312 ctx->irq_work = IRQ_WORK_INIT(bpf_task_work_destroy); 4313 irq_work_queue(&ctx->irq_work); 4314 } else { 4315 bpf_task_work_destroy(&ctx->irq_work); 4316 } 4317 } 4318 4319 static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx) 4320 { 4321 /* 4322 * Scheduled task_work callback holds ctx ref, so if we successfully 4323 * cancelled, we put that ref on callback's behalf. If we couldn't 4324 * cancel, callback will inevitably run or has already completed 4325 * running, and it would have taken care of its ctx ref itself. 4326 */ 4327 if (task_work_cancel(ctx->task, &ctx->work)) 4328 bpf_task_work_ctx_put(ctx); 4329 } 4330 4331 static void bpf_task_work_callback(struct callback_head *cb) 4332 { 4333 struct bpf_task_work_ctx *ctx = container_of(cb, struct bpf_task_work_ctx, work); 4334 enum bpf_task_work_state state; 4335 u32 idx; 4336 void *key; 4337 4338 /* Read lock is needed to protect ctx and map key/value access */ 4339 guard(rcu_tasks_trace)(); 4340 /* 4341 * This callback may start running before bpf_task_work_irq() switched to 4342 * SCHEDULED state, so handle both transition variants SCHEDULING|SCHEDULED -> RUNNING. 4343 */ 4344 state = cmpxchg(&ctx->state, BPF_TW_SCHEDULING, BPF_TW_RUNNING); 4345 if (state == BPF_TW_SCHEDULED) 4346 state = cmpxchg(&ctx->state, BPF_TW_SCHEDULED, BPF_TW_RUNNING); 4347 if (state == BPF_TW_FREED) { 4348 bpf_task_work_ctx_put(ctx); 4349 return; 4350 } 4351 4352 key = (void *)map_key_from_value(ctx->map, ctx->map_val, &idx); 4353 4354 migrate_disable(); 4355 ctx->callback_fn(ctx->map, key, ctx->map_val); 4356 migrate_enable(); 4357 4358 bpf_task_work_ctx_reset(ctx); 4359 (void)cmpxchg(&ctx->state, BPF_TW_RUNNING, BPF_TW_STANDBY); 4360 4361 bpf_task_work_ctx_put(ctx); 4362 } 4363 4364 static void bpf_task_work_irq(struct irq_work *irq_work) 4365 { 4366 struct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work); 4367 enum bpf_task_work_state state; 4368 int err; 4369 4370 guard(rcu)(); 4371 4372 if (cmpxchg(&ctx->state, BPF_TW_PENDING, BPF_TW_SCHEDULING) != BPF_TW_PENDING) { 4373 bpf_task_work_ctx_put(ctx); 4374 return; 4375 } 4376 4377 err = task_work_add(ctx->task, &ctx->work, ctx->mode); 4378 if (err) { 4379 bpf_task_work_ctx_reset(ctx); 4380 /* 4381 * try to switch back to STANDBY for another task_work reuse, but we might have 4382 * gone to FREED already, which is fine as we already cleaned up after ourselves 4383 */ 4384 (void)cmpxchg(&ctx->state, BPF_TW_SCHEDULING, BPF_TW_STANDBY); 4385 bpf_task_work_ctx_put(ctx); 4386 return; 4387 } 4388 4389 /* 4390 * It's technically possible for just scheduled task_work callback to 4391 * complete running by now, going SCHEDULING -> RUNNING and then 4392 * dropping its ctx refcount. Instead of capturing an extra ref just 4393 * to protect below ctx->state access, we rely on rcu_read_lock 4394 * above to prevent kfree_rcu from freeing ctx before we return. 4395 */ 4396 state = cmpxchg(&ctx->state, BPF_TW_SCHEDULING, BPF_TW_SCHEDULED); 4397 if (state == BPF_TW_FREED) 4398 bpf_task_work_cancel(ctx); /* clean up if we switched into FREED state */ 4399 } 4400 4401 static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw, 4402 struct bpf_map *map) 4403 { 4404 struct bpf_task_work_kern *twk = (void *)tw; 4405 struct bpf_task_work_ctx *ctx, *old_ctx; 4406 4407 ctx = READ_ONCE(twk->ctx); 4408 if (ctx) 4409 return ctx; 4410 4411 ctx = bpf_map_kmalloc_nolock(map, sizeof(*ctx), 0, NUMA_NO_NODE); 4412 if (!ctx) 4413 return ERR_PTR(-ENOMEM); 4414 4415 memset(ctx, 0, sizeof(*ctx)); 4416 refcount_set(&ctx->refcnt, 1); /* map's own ref */ 4417 ctx->state = BPF_TW_STANDBY; 4418 4419 old_ctx = cmpxchg(&twk->ctx, NULL, ctx); 4420 if (old_ctx) { 4421 /* 4422 * tw->ctx is set by concurrent BPF program, release allocated 4423 * memory and try to reuse already set context. 4424 */ 4425 kfree_nolock(ctx); 4426 return old_ctx; 4427 } 4428 4429 return ctx; /* Success */ 4430 } 4431 4432 static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw, 4433 struct bpf_map *map) 4434 { 4435 struct bpf_task_work_ctx *ctx; 4436 4437 /* 4438 * Sleepable BPF programs hold rcu_read_lock_trace but not 4439 * regular rcu_read_lock. Since kfree_rcu waits for regular 4440 * RCU GP, the ctx can be freed while we're between reading 4441 * the pointer and incrementing the refcount. Take regular 4442 * rcu_read_lock to prevent kfree_rcu from freeing the ctx 4443 * before we can tryget it. 4444 */ 4445 scoped_guard(rcu) { 4446 ctx = bpf_task_work_fetch_ctx(tw, map); 4447 if (IS_ERR(ctx)) 4448 return ctx; 4449 4450 /* try to get ref for task_work callback to hold */ 4451 if (!bpf_task_work_ctx_tryget(ctx)) 4452 return ERR_PTR(-EBUSY); 4453 } 4454 4455 if (cmpxchg(&ctx->state, BPF_TW_STANDBY, BPF_TW_PENDING) != BPF_TW_STANDBY) { 4456 /* lost acquiring race or map_release_uref() stole it from us, put ref and bail */ 4457 bpf_task_work_ctx_put(ctx); 4458 return ERR_PTR(-EBUSY); 4459 } 4460 4461 /* 4462 * If no process or bpffs is holding a reference to the map, no new callbacks should be 4463 * scheduled. This does not address any race or correctness issue, but rather is a policy 4464 * choice: dropping user references should stop everything. 4465 */ 4466 if (!atomic64_read(&map->usercnt)) { 4467 /* drop ref we just got for task_work callback itself */ 4468 bpf_task_work_ctx_put(ctx); 4469 /* transfer map's ref into cancel_and_free() */ 4470 bpf_task_work_cancel_and_free(tw); 4471 return ERR_PTR(-EBUSY); 4472 } 4473 4474 return ctx; 4475 } 4476 4477 static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work *tw, 4478 struct bpf_map *map, bpf_task_work_callback_t callback_fn, 4479 struct bpf_prog_aux *aux, enum task_work_notify_mode mode) 4480 { 4481 struct bpf_prog *prog; 4482 struct bpf_task_work_ctx *ctx; 4483 int err; 4484 4485 BTF_TYPE_EMIT(struct bpf_task_work); 4486 4487 prog = bpf_prog_inc_not_zero(aux->prog); 4488 if (IS_ERR(prog)) 4489 return -EBADF; 4490 task = bpf_task_acquire(task); 4491 if (!task) { 4492 err = -EBADF; 4493 goto release_prog; 4494 } 4495 4496 ctx = bpf_task_work_acquire_ctx(tw, map); 4497 if (IS_ERR(ctx)) { 4498 err = PTR_ERR(ctx); 4499 goto release_all; 4500 } 4501 4502 ctx->task = task; 4503 ctx->callback_fn = callback_fn; 4504 ctx->prog = prog; 4505 ctx->mode = mode; 4506 ctx->map = map; 4507 ctx->map_val = (void *)tw - map->record->task_work_off; 4508 init_task_work(&ctx->work, bpf_task_work_callback); 4509 init_irq_work(&ctx->irq_work, bpf_task_work_irq); 4510 4511 irq_work_queue(&ctx->irq_work); 4512 return 0; 4513 4514 release_all: 4515 bpf_task_release(task); 4516 release_prog: 4517 bpf_prog_put(prog); 4518 return err; 4519 } 4520 4521 /** 4522 * bpf_task_work_schedule_signal - Schedule BPF callback using task_work_add with TWA_SIGNAL 4523 * mode 4524 * @task: Task struct for which callback should be scheduled 4525 * @tw: Pointer to struct bpf_task_work in BPF map value for internal bookkeeping 4526 * @map__map: bpf_map that embeds struct bpf_task_work in the values 4527 * @callback: pointer to BPF subprogram to call 4528 * @aux: pointer to bpf_prog_aux of the caller BPF program, implicitly set by the verifier 4529 * 4530 * Return: 0 if task work has been scheduled successfully, negative error code otherwise 4531 */ 4532 __bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct bpf_task_work *tw, 4533 void *map__map, bpf_task_work_callback_t callback, 4534 struct bpf_prog_aux *aux) 4535 { 4536 return bpf_task_work_schedule(task, tw, map__map, callback, aux, TWA_SIGNAL); 4537 } 4538 4539 /** 4540 * bpf_task_work_schedule_resume - Schedule BPF callback using task_work_add with TWA_RESUME 4541 * mode 4542 * @task: Task struct for which callback should be scheduled 4543 * @tw: Pointer to struct bpf_task_work in BPF map value for internal bookkeeping 4544 * @map__map: bpf_map that embeds struct bpf_task_work in the values 4545 * @callback: pointer to BPF subprogram to call 4546 * @aux: pointer to bpf_prog_aux of the caller BPF program, implicitly set by the verifier 4547 * 4548 * Return: 0 if task work has been scheduled successfully, negative error code otherwise 4549 */ 4550 __bpf_kfunc int bpf_task_work_schedule_resume(struct task_struct *task, struct bpf_task_work *tw, 4551 void *map__map, bpf_task_work_callback_t callback, 4552 struct bpf_prog_aux *aux) 4553 { 4554 return bpf_task_work_schedule(task, tw, map__map, callback, aux, TWA_RESUME); 4555 } 4556 4557 static int make_file_dynptr(struct file *file, u32 flags, bool may_sleep, 4558 struct bpf_dynptr_kern *ptr) 4559 { 4560 struct bpf_dynptr_file_impl *state; 4561 4562 /* flags is currently unsupported */ 4563 if (flags) { 4564 bpf_dynptr_set_null(ptr); 4565 return -EINVAL; 4566 } 4567 4568 state = kmalloc_nolock(sizeof(*state), 0, NUMA_NO_NODE); 4569 if (!state) { 4570 bpf_dynptr_set_null(ptr); 4571 return -ENOMEM; 4572 } 4573 state->offset = 0; 4574 state->size = U64_MAX; /* Don't restrict size, as file may change anyways */ 4575 freader_init_from_file(&state->freader, NULL, 0, file, may_sleep); 4576 bpf_dynptr_init(ptr, state, BPF_DYNPTR_TYPE_FILE, 0, 0); 4577 bpf_dynptr_set_rdonly(ptr); 4578 return 0; 4579 } 4580 4581 __bpf_kfunc int bpf_dynptr_from_file(struct file *file, u32 flags, struct bpf_dynptr *ptr__uninit) 4582 { 4583 return make_file_dynptr(file, flags, false, (struct bpf_dynptr_kern *)ptr__uninit); 4584 } 4585 4586 int bpf_dynptr_from_file_sleepable(struct file *file, u32 flags, struct bpf_dynptr *ptr__uninit) 4587 { 4588 return make_file_dynptr(file, flags, true, (struct bpf_dynptr_kern *)ptr__uninit); 4589 } 4590 4591 __bpf_kfunc int bpf_dynptr_file_discard(struct bpf_dynptr *dynptr) 4592 { 4593 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)dynptr; 4594 struct bpf_dynptr_file_impl *df = ptr->data; 4595 4596 if (!df) 4597 return 0; 4598 4599 freader_cleanup(&df->freader); 4600 kfree_nolock(df); 4601 bpf_dynptr_set_null(ptr); 4602 return 0; 4603 } 4604 4605 /** 4606 * bpf_timer_cancel_async - try to deactivate a timer 4607 * @timer: bpf_timer to stop 4608 * 4609 * Returns: 4610 * 4611 * * 0 when the timer was not active 4612 * * 1 when the timer was active 4613 * * -1 when the timer is currently executing the callback function and 4614 * cannot be stopped 4615 * * -ECANCELED when the timer will be cancelled asynchronously 4616 * * -ENOMEM when out of memory 4617 * * -EINVAL when the timer was not initialized 4618 * * -ENOENT when this kfunc is racing with timer deletion 4619 */ 4620 __bpf_kfunc int bpf_timer_cancel_async(struct bpf_timer *timer) 4621 { 4622 struct bpf_async_kern *async = (void *)timer; 4623 struct bpf_async_cb *cb; 4624 int ret; 4625 4626 cb = READ_ONCE(async->cb); 4627 if (!cb) 4628 return -EINVAL; 4629 4630 /* 4631 * Unlike hrtimer_start() it's ok to synchronously call 4632 * hrtimer_try_to_cancel() when refcnt reached zero, but deferring to 4633 * irq_work is not, since irq callback may execute after RCU GP and 4634 * cb could be freed at that time. Check for refcnt zero for 4635 * consistency. 4636 */ 4637 if (!refcount_inc_not_zero(&cb->refcnt)) 4638 return -ENOENT; 4639 4640 if (!defer_timer_wq_op()) { 4641 struct bpf_hrtimer *t = container_of(cb, struct bpf_hrtimer, cb); 4642 4643 ret = hrtimer_try_to_cancel(&t->timer); 4644 bpf_async_refcount_put(cb); 4645 return ret; 4646 } else { 4647 ret = bpf_async_schedule_op(cb, BPF_ASYNC_CANCEL, 0, 0); 4648 return ret ? ret : -ECANCELED; 4649 } 4650 } 4651 4652 __bpf_kfunc_end_defs(); 4653 4654 static void bpf_task_work_cancel_scheduled(struct irq_work *irq_work) 4655 { 4656 struct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work); 4657 4658 bpf_task_work_cancel(ctx); /* this might put task_work callback's ref */ 4659 bpf_task_work_ctx_put(ctx); /* and here we put map's own ref that was transferred to us */ 4660 } 4661 4662 void bpf_task_work_cancel_and_free(void *val) 4663 { 4664 struct bpf_task_work_kern *twk = val; 4665 struct bpf_task_work_ctx *ctx; 4666 enum bpf_task_work_state state; 4667 4668 ctx = xchg(&twk->ctx, NULL); 4669 if (!ctx) 4670 return; 4671 4672 state = xchg(&ctx->state, BPF_TW_FREED); 4673 if (state == BPF_TW_SCHEDULED) { 4674 /* run in irq_work to avoid locks in NMI */ 4675 init_irq_work(&ctx->irq_work, bpf_task_work_cancel_scheduled); 4676 irq_work_queue(&ctx->irq_work); 4677 return; 4678 } 4679 4680 bpf_task_work_ctx_put(ctx); /* put bpf map's ref */ 4681 } 4682 4683 BTF_KFUNCS_START(generic_btf_ids) 4684 #ifdef CONFIG_CRASH_DUMP 4685 BTF_ID_FLAGS(func, crash_kexec, KF_DESTRUCTIVE) 4686 #endif 4687 BTF_ID_FLAGS(func, bpf_obj_new, KF_ACQUIRE | KF_RET_NULL | KF_IMPLICIT_ARGS) 4688 BTF_ID_FLAGS(func, bpf_obj_new_impl, KF_ACQUIRE | KF_RET_NULL) 4689 BTF_ID_FLAGS(func, bpf_percpu_obj_new, KF_ACQUIRE | KF_RET_NULL | KF_IMPLICIT_ARGS) 4690 BTF_ID_FLAGS(func, bpf_percpu_obj_new_impl, KF_ACQUIRE | KF_RET_NULL) 4691 BTF_ID_FLAGS(func, bpf_obj_drop, KF_RELEASE | KF_IMPLICIT_ARGS) 4692 BTF_ID_FLAGS(func, bpf_obj_drop_impl, KF_RELEASE) 4693 BTF_ID_FLAGS(func, bpf_percpu_obj_drop, KF_RELEASE | KF_IMPLICIT_ARGS) 4694 BTF_ID_FLAGS(func, bpf_percpu_obj_drop_impl, KF_RELEASE) 4695 BTF_ID_FLAGS(func, bpf_refcount_acquire, KF_ACQUIRE | KF_RET_NULL | KF_RCU | KF_IMPLICIT_ARGS) 4696 BTF_ID_FLAGS(func, bpf_refcount_acquire_impl, KF_ACQUIRE | KF_RET_NULL | KF_RCU) 4697 BTF_ID_FLAGS(func, bpf_list_push_front, KF_IMPLICIT_ARGS) 4698 BTF_ID_FLAGS(func, bpf_list_push_front_impl) 4699 BTF_ID_FLAGS(func, bpf_list_push_back, KF_IMPLICIT_ARGS) 4700 BTF_ID_FLAGS(func, bpf_list_push_back_impl) 4701 BTF_ID_FLAGS(func, bpf_list_pop_front, KF_ACQUIRE | KF_RET_NULL) 4702 BTF_ID_FLAGS(func, bpf_list_pop_back, KF_ACQUIRE | KF_RET_NULL) 4703 BTF_ID_FLAGS(func, bpf_list_front, KF_RET_NULL) 4704 BTF_ID_FLAGS(func, bpf_list_back, KF_RET_NULL) 4705 BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) 4706 BTF_ID_FLAGS(func, bpf_task_release, KF_RELEASE) 4707 BTF_ID_FLAGS(func, bpf_rbtree_remove, KF_ACQUIRE | KF_RET_NULL) 4708 BTF_ID_FLAGS(func, bpf_rbtree_add, KF_IMPLICIT_ARGS) 4709 BTF_ID_FLAGS(func, bpf_rbtree_add_impl) 4710 BTF_ID_FLAGS(func, bpf_rbtree_first, KF_RET_NULL) 4711 BTF_ID_FLAGS(func, bpf_rbtree_root, KF_RET_NULL) 4712 BTF_ID_FLAGS(func, bpf_rbtree_left, KF_RET_NULL) 4713 BTF_ID_FLAGS(func, bpf_rbtree_right, KF_RET_NULL) 4714 4715 #ifdef CONFIG_CGROUPS 4716 BTF_ID_FLAGS(func, bpf_cgroup_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) 4717 BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE) 4718 BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_RCU | KF_RET_NULL) 4719 BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL) 4720 BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU) 4721 BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL) 4722 #endif 4723 BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) 4724 BTF_ID_FLAGS(func, bpf_task_from_vpid, KF_ACQUIRE | KF_RET_NULL) 4725 BTF_ID_FLAGS(func, bpf_throw) 4726 #ifdef CONFIG_BPF_EVENTS 4727 BTF_ID_FLAGS(func, bpf_send_signal_task) 4728 #endif 4729 #ifdef CONFIG_KEYS 4730 BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE) 4731 BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL) 4732 BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE) 4733 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION 4734 BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE) 4735 #endif 4736 #endif 4737 #ifdef CONFIG_S390 4738 BTF_ID_FLAGS(func, bpf_get_lowcore) 4739 #endif 4740 BTF_KFUNCS_END(generic_btf_ids) 4741 4742 static const struct btf_kfunc_id_set generic_kfunc_set = { 4743 .owner = THIS_MODULE, 4744 .set = &generic_btf_ids, 4745 }; 4746 4747 4748 BTF_ID_LIST(generic_dtor_ids) 4749 BTF_ID(struct, task_struct) 4750 BTF_ID(func, bpf_task_release_dtor) 4751 #ifdef CONFIG_CGROUPS 4752 BTF_ID(struct, cgroup) 4753 BTF_ID(func, bpf_cgroup_release_dtor) 4754 #endif 4755 4756 BTF_KFUNCS_START(common_btf_ids) 4757 BTF_ID_FLAGS(func, bpf_cast_to_kern_ctx, KF_FASTCALL) 4758 BTF_ID_FLAGS(func, bpf_rdonly_cast, KF_FASTCALL) 4759 BTF_ID_FLAGS(func, bpf_rcu_read_lock) 4760 BTF_ID_FLAGS(func, bpf_rcu_read_unlock) 4761 BTF_ID_FLAGS(func, bpf_dynptr_slice, KF_RET_NULL) 4762 BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL) 4763 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW) 4764 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL) 4765 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY) 4766 BTF_ID_FLAGS(func, bpf_iter_task_vma_new, KF_ITER_NEW | KF_RCU) 4767 BTF_ID_FLAGS(func, bpf_iter_task_vma_next, KF_ITER_NEXT | KF_RET_NULL) 4768 BTF_ID_FLAGS(func, bpf_iter_task_vma_destroy, KF_ITER_DESTROY) 4769 #ifdef CONFIG_CGROUPS 4770 BTF_ID_FLAGS(func, bpf_iter_css_task_new, KF_ITER_NEW) 4771 BTF_ID_FLAGS(func, bpf_iter_css_task_next, KF_ITER_NEXT | KF_RET_NULL) 4772 BTF_ID_FLAGS(func, bpf_iter_css_task_destroy, KF_ITER_DESTROY) 4773 BTF_ID_FLAGS(func, bpf_iter_css_new, KF_ITER_NEW | KF_RCU_PROTECTED) 4774 BTF_ID_FLAGS(func, bpf_iter_css_next, KF_ITER_NEXT | KF_RET_NULL) 4775 BTF_ID_FLAGS(func, bpf_iter_css_destroy, KF_ITER_DESTROY) 4776 #endif 4777 BTF_ID_FLAGS(func, bpf_iter_task_new, KF_ITER_NEW | KF_RCU_PROTECTED) 4778 BTF_ID_FLAGS(func, bpf_iter_task_next, KF_ITER_NEXT | KF_RET_NULL) 4779 BTF_ID_FLAGS(func, bpf_iter_task_destroy, KF_ITER_DESTROY) 4780 BTF_ID_FLAGS(func, bpf_dynptr_adjust) 4781 BTF_ID_FLAGS(func, bpf_dynptr_is_null) 4782 BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) 4783 BTF_ID_FLAGS(func, bpf_dynptr_size) 4784 BTF_ID_FLAGS(func, bpf_dynptr_clone) 4785 BTF_ID_FLAGS(func, bpf_dynptr_copy) 4786 BTF_ID_FLAGS(func, bpf_dynptr_memset) 4787 #ifdef CONFIG_NET 4788 BTF_ID_FLAGS(func, bpf_modify_return_test_tp) 4789 #endif 4790 BTF_ID_FLAGS(func, bpf_wq_init) 4791 BTF_ID_FLAGS(func, bpf_wq_set_callback, KF_IMPLICIT_ARGS) 4792 BTF_ID_FLAGS(func, bpf_wq_start) 4793 BTF_ID_FLAGS(func, bpf_preempt_disable) 4794 BTF_ID_FLAGS(func, bpf_preempt_enable) 4795 BTF_ID_FLAGS(func, bpf_iter_bits_new, KF_ITER_NEW) 4796 BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_NULL) 4797 BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY) 4798 BTF_ID_FLAGS(func, bpf_copy_from_user_str, KF_SLEEPABLE) 4799 BTF_ID_FLAGS(func, bpf_copy_from_user_task_str, KF_SLEEPABLE) 4800 BTF_ID_FLAGS(func, bpf_get_kmem_cache) 4801 BTF_ID_FLAGS(func, bpf_iter_kmem_cache_new, KF_ITER_NEW | KF_SLEEPABLE) 4802 BTF_ID_FLAGS(func, bpf_iter_kmem_cache_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE) 4803 BTF_ID_FLAGS(func, bpf_iter_kmem_cache_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) 4804 BTF_ID_FLAGS(func, bpf_local_irq_save) 4805 BTF_ID_FLAGS(func, bpf_local_irq_restore) 4806 #ifdef CONFIG_BPF_EVENTS 4807 BTF_ID_FLAGS(func, bpf_probe_read_user_dynptr) 4808 BTF_ID_FLAGS(func, bpf_probe_read_kernel_dynptr) 4809 BTF_ID_FLAGS(func, bpf_probe_read_user_str_dynptr) 4810 BTF_ID_FLAGS(func, bpf_probe_read_kernel_str_dynptr) 4811 BTF_ID_FLAGS(func, bpf_copy_from_user_dynptr, KF_SLEEPABLE) 4812 BTF_ID_FLAGS(func, bpf_copy_from_user_str_dynptr, KF_SLEEPABLE) 4813 BTF_ID_FLAGS(func, bpf_copy_from_user_task_dynptr, KF_SLEEPABLE) 4814 BTF_ID_FLAGS(func, bpf_copy_from_user_task_str_dynptr, KF_SLEEPABLE) 4815 #endif 4816 #ifdef CONFIG_DMA_SHARED_BUFFER 4817 BTF_ID_FLAGS(func, bpf_iter_dmabuf_new, KF_ITER_NEW | KF_SLEEPABLE) 4818 BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE) 4819 BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) 4820 #endif 4821 BTF_ID_FLAGS(func, __bpf_trap) 4822 BTF_ID_FLAGS(func, bpf_strcmp); 4823 BTF_ID_FLAGS(func, bpf_strcasecmp); 4824 BTF_ID_FLAGS(func, bpf_strncasecmp); 4825 BTF_ID_FLAGS(func, bpf_strchr); 4826 BTF_ID_FLAGS(func, bpf_strchrnul); 4827 BTF_ID_FLAGS(func, bpf_strnchr); 4828 BTF_ID_FLAGS(func, bpf_strrchr); 4829 BTF_ID_FLAGS(func, bpf_strlen); 4830 BTF_ID_FLAGS(func, bpf_strnlen); 4831 BTF_ID_FLAGS(func, bpf_strspn); 4832 BTF_ID_FLAGS(func, bpf_strcspn); 4833 BTF_ID_FLAGS(func, bpf_strstr); 4834 BTF_ID_FLAGS(func, bpf_strcasestr); 4835 BTF_ID_FLAGS(func, bpf_strnstr); 4836 BTF_ID_FLAGS(func, bpf_strncasestr); 4837 #if defined(CONFIG_BPF_LSM) && defined(CONFIG_CGROUPS) 4838 BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU) 4839 #endif 4840 BTF_ID_FLAGS(func, bpf_stream_vprintk, KF_IMPLICIT_ARGS) 4841 BTF_ID_FLAGS(func, bpf_stream_print_stack, KF_IMPLICIT_ARGS) 4842 BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_IMPLICIT_ARGS) 4843 BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_IMPLICIT_ARGS) 4844 BTF_ID_FLAGS(func, bpf_dynptr_from_file) 4845 BTF_ID_FLAGS(func, bpf_dynptr_file_discard) 4846 BTF_ID_FLAGS(func, bpf_timer_cancel_async) 4847 BTF_KFUNCS_END(common_btf_ids) 4848 4849 static const struct btf_kfunc_id_set common_kfunc_set = { 4850 .owner = THIS_MODULE, 4851 .set = &common_btf_ids, 4852 }; 4853 4854 static int __init kfunc_init(void) 4855 { 4856 int ret; 4857 const struct btf_id_dtor_kfunc generic_dtors[] = { 4858 { 4859 .btf_id = generic_dtor_ids[0], 4860 .kfunc_btf_id = generic_dtor_ids[1] 4861 }, 4862 #ifdef CONFIG_CGROUPS 4863 { 4864 .btf_id = generic_dtor_ids[2], 4865 .kfunc_btf_id = generic_dtor_ids[3] 4866 }, 4867 #endif 4868 }; 4869 4870 ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &generic_kfunc_set); 4871 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &generic_kfunc_set); 4872 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &generic_kfunc_set); 4873 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set); 4874 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &generic_kfunc_set); 4875 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, &generic_kfunc_set); 4876 ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors, 4877 ARRAY_SIZE(generic_dtors), 4878 THIS_MODULE); 4879 return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &common_kfunc_set); 4880 } 4881 4882 late_initcall(kfunc_init); 4883 4884 /* Get a pointer to dynptr data up to len bytes for read only access. If 4885 * the dynptr doesn't have continuous data up to len bytes, return NULL. 4886 */ 4887 const void *__bpf_dynptr_data(const struct bpf_dynptr_kern *ptr, u64 len) 4888 { 4889 const struct bpf_dynptr *p = (struct bpf_dynptr *)ptr; 4890 4891 return bpf_dynptr_slice(p, 0, NULL, len); 4892 } 4893 4894 /* Get a pointer to dynptr data up to len bytes for read write access. If 4895 * the dynptr doesn't have continuous data up to len bytes, or the dynptr 4896 * is read only, return NULL. 4897 */ 4898 void *__bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u64 len) 4899 { 4900 if (__bpf_dynptr_is_rdonly(ptr)) 4901 return NULL; 4902 return (void *)__bpf_dynptr_data(ptr, len); 4903 } 4904 4905 void bpf_map_free_internal_structs(struct bpf_map *map, void *val) 4906 { 4907 if (btf_record_has_field(map->record, BPF_TIMER)) 4908 bpf_obj_free_timer(map->record, val); 4909 if (btf_record_has_field(map->record, BPF_WORKQUEUE)) 4910 bpf_obj_free_workqueue(map->record, val); 4911 if (btf_record_has_field(map->record, BPF_TASK_WORK)) 4912 bpf_obj_free_task_work(map->record, val); 4913 } 4914