1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
6 #include <linux/bpf-cgroup.h>
7 #include <linux/rcupdate.h>
8 #include <linux/random.h>
10 #include <linux/topology.h>
11 #include <linux/ktime.h>
12 #include <linux/sched.h>
13 #include <linux/uidgid.h>
14 #include <linux/filter.h>
15 #include <linux/ctype.h>
16 #include <linux/jiffies.h>
17 #include <linux/pid_namespace.h>
18 #include <linux/proc_ns.h>
19 #include <linux/security.h>
20 #include <linux/btf_ids.h>
22 #include "../../lib/kstrtox.h"
24 /* If kernel subsystem is allowing eBPF programs to call this function,
25 * inside its own verifier_ops->get_func_proto() callback it should return
26 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
28 * Different map implementations will rely on rcu in map methods
29 * lookup/update/delete, therefore eBPF programs must run under rcu lock
30 * if program is allowed to access maps, so check rcu_read_lock_held in
31 * all three functions.
33 BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
35 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
36 return (unsigned long) map->ops->map_lookup_elem(map, key);
39 const struct bpf_func_proto bpf_map_lookup_elem_proto = {
40 .func = bpf_map_lookup_elem,
43 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
44 .arg1_type = ARG_CONST_MAP_PTR,
45 .arg2_type = ARG_PTR_TO_MAP_KEY,
48 BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
49 void *, value, u64, flags)
51 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
52 return map->ops->map_update_elem(map, key, value, flags);
55 const struct bpf_func_proto bpf_map_update_elem_proto = {
56 .func = bpf_map_update_elem,
59 .ret_type = RET_INTEGER,
60 .arg1_type = ARG_CONST_MAP_PTR,
61 .arg2_type = ARG_PTR_TO_MAP_KEY,
62 .arg3_type = ARG_PTR_TO_MAP_VALUE,
63 .arg4_type = ARG_ANYTHING,
66 BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
68 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
69 return map->ops->map_delete_elem(map, key);
72 const struct bpf_func_proto bpf_map_delete_elem_proto = {
73 .func = bpf_map_delete_elem,
76 .ret_type = RET_INTEGER,
77 .arg1_type = ARG_CONST_MAP_PTR,
78 .arg2_type = ARG_PTR_TO_MAP_KEY,
81 BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags)
83 return map->ops->map_push_elem(map, value, flags);
86 const struct bpf_func_proto bpf_map_push_elem_proto = {
87 .func = bpf_map_push_elem,
90 .ret_type = RET_INTEGER,
91 .arg1_type = ARG_CONST_MAP_PTR,
92 .arg2_type = ARG_PTR_TO_MAP_VALUE,
93 .arg3_type = ARG_ANYTHING,
96 BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value)
98 return map->ops->map_pop_elem(map, value);
101 const struct bpf_func_proto bpf_map_pop_elem_proto = {
102 .func = bpf_map_pop_elem,
104 .ret_type = RET_INTEGER,
105 .arg1_type = ARG_CONST_MAP_PTR,
106 .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
109 BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value)
111 return map->ops->map_peek_elem(map, value);
114 const struct bpf_func_proto bpf_map_peek_elem_proto = {
115 .func = bpf_map_peek_elem,
117 .ret_type = RET_INTEGER,
118 .arg1_type = ARG_CONST_MAP_PTR,
119 .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
122 const struct bpf_func_proto bpf_get_prandom_u32_proto = {
123 .func = bpf_user_rnd_u32,
125 .ret_type = RET_INTEGER,
128 BPF_CALL_0(bpf_get_smp_processor_id)
130 return smp_processor_id();
133 const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
134 .func = bpf_get_smp_processor_id,
136 .ret_type = RET_INTEGER,
139 BPF_CALL_0(bpf_get_numa_node_id)
141 return numa_node_id();
144 const struct bpf_func_proto bpf_get_numa_node_id_proto = {
145 .func = bpf_get_numa_node_id,
147 .ret_type = RET_INTEGER,
150 BPF_CALL_0(bpf_ktime_get_ns)
152 /* NMI safe access to clock monotonic */
153 return ktime_get_mono_fast_ns();
156 const struct bpf_func_proto bpf_ktime_get_ns_proto = {
157 .func = bpf_ktime_get_ns,
159 .ret_type = RET_INTEGER,
162 BPF_CALL_0(bpf_ktime_get_boot_ns)
164 /* NMI safe access to clock boottime */
165 return ktime_get_boot_fast_ns();
168 const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = {
169 .func = bpf_ktime_get_boot_ns,
171 .ret_type = RET_INTEGER,
174 BPF_CALL_0(bpf_ktime_get_coarse_ns)
176 return ktime_get_coarse_ns();
179 const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto = {
180 .func = bpf_ktime_get_coarse_ns,
182 .ret_type = RET_INTEGER,
185 BPF_CALL_0(bpf_get_current_pid_tgid)
187 struct task_struct *task = current;
192 return (u64) task->tgid << 32 | task->pid;
195 const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
196 .func = bpf_get_current_pid_tgid,
198 .ret_type = RET_INTEGER,
201 BPF_CALL_0(bpf_get_current_uid_gid)
203 struct task_struct *task = current;
210 current_uid_gid(&uid, &gid);
211 return (u64) from_kgid(&init_user_ns, gid) << 32 |
212 from_kuid(&init_user_ns, uid);
215 const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
216 .func = bpf_get_current_uid_gid,
218 .ret_type = RET_INTEGER,
221 BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
223 struct task_struct *task = current;
228 /* Verifier guarantees that size > 0 */
229 strscpy(buf, task->comm, size);
232 memset(buf, 0, size);
236 const struct bpf_func_proto bpf_get_current_comm_proto = {
237 .func = bpf_get_current_comm,
239 .ret_type = RET_INTEGER,
240 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
241 .arg2_type = ARG_CONST_SIZE,
244 #if defined(CONFIG_QUEUED_SPINLOCKS) || defined(CONFIG_BPF_ARCH_SPINLOCK)
246 static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
248 arch_spinlock_t *l = (void *)lock;
251 arch_spinlock_t lock;
252 } u = { .lock = __ARCH_SPIN_LOCK_UNLOCKED };
254 compiletime_assert(u.val == 0, "__ARCH_SPIN_LOCK_UNLOCKED not 0");
255 BUILD_BUG_ON(sizeof(*l) != sizeof(__u32));
256 BUILD_BUG_ON(sizeof(*lock) != sizeof(__u32));
260 static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
262 arch_spinlock_t *l = (void *)lock;
269 static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
271 atomic_t *l = (void *)lock;
273 BUILD_BUG_ON(sizeof(*l) != sizeof(*lock));
275 atomic_cond_read_relaxed(l, !VAL);
276 } while (atomic_xchg(l, 1));
279 static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
281 atomic_t *l = (void *)lock;
283 atomic_set_release(l, 0);
288 static DEFINE_PER_CPU(unsigned long, irqsave_flags);
290 static inline void __bpf_spin_lock_irqsave(struct bpf_spin_lock *lock)
294 local_irq_save(flags);
295 __bpf_spin_lock(lock);
296 __this_cpu_write(irqsave_flags, flags);
299 notrace BPF_CALL_1(bpf_spin_lock, struct bpf_spin_lock *, lock)
301 __bpf_spin_lock_irqsave(lock);
305 const struct bpf_func_proto bpf_spin_lock_proto = {
306 .func = bpf_spin_lock,
308 .ret_type = RET_VOID,
309 .arg1_type = ARG_PTR_TO_SPIN_LOCK,
312 static inline void __bpf_spin_unlock_irqrestore(struct bpf_spin_lock *lock)
316 flags = __this_cpu_read(irqsave_flags);
317 __bpf_spin_unlock(lock);
318 local_irq_restore(flags);
321 notrace BPF_CALL_1(bpf_spin_unlock, struct bpf_spin_lock *, lock)
323 __bpf_spin_unlock_irqrestore(lock);
327 const struct bpf_func_proto bpf_spin_unlock_proto = {
328 .func = bpf_spin_unlock,
330 .ret_type = RET_VOID,
331 .arg1_type = ARG_PTR_TO_SPIN_LOCK,
334 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
337 struct bpf_spin_lock *lock;
340 lock = src + map->spin_lock_off;
342 lock = dst + map->spin_lock_off;
344 __bpf_spin_lock_irqsave(lock);
345 copy_map_value(map, dst, src);
346 __bpf_spin_unlock_irqrestore(lock);
350 BPF_CALL_0(bpf_jiffies64)
352 return get_jiffies_64();
355 const struct bpf_func_proto bpf_jiffies64_proto = {
356 .func = bpf_jiffies64,
358 .ret_type = RET_INTEGER,
361 #ifdef CONFIG_CGROUPS
362 BPF_CALL_0(bpf_get_current_cgroup_id)
368 cgrp = task_dfl_cgroup(current);
369 cgrp_id = cgroup_id(cgrp);
375 const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
376 .func = bpf_get_current_cgroup_id,
378 .ret_type = RET_INTEGER,
381 BPF_CALL_1(bpf_get_current_ancestor_cgroup_id, int, ancestor_level)
384 struct cgroup *ancestor;
388 cgrp = task_dfl_cgroup(current);
389 ancestor = cgroup_ancestor(cgrp, ancestor_level);
390 cgrp_id = ancestor ? cgroup_id(ancestor) : 0;
396 const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto = {
397 .func = bpf_get_current_ancestor_cgroup_id,
399 .ret_type = RET_INTEGER,
400 .arg1_type = ARG_ANYTHING,
403 #ifdef CONFIG_CGROUP_BPF
405 BPF_CALL_2(bpf_get_local_storage, struct bpf_map *, map, u64, flags)
407 /* flags argument is not used now,
408 * but provides an ability to extend the API.
409 * verifier checks that its value is correct.
411 enum bpf_cgroup_storage_type stype = cgroup_storage_type(map);
412 struct bpf_cgroup_storage *storage;
413 struct bpf_cg_run_ctx *ctx;
416 /* get current cgroup storage from BPF run context */
417 ctx = container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx);
418 storage = ctx->prog_item->cgroup_storage[stype];
420 if (stype == BPF_CGROUP_STORAGE_SHARED)
421 ptr = &READ_ONCE(storage->buf)->data[0];
423 ptr = this_cpu_ptr(storage->percpu_buf);
425 return (unsigned long)ptr;
428 const struct bpf_func_proto bpf_get_local_storage_proto = {
429 .func = bpf_get_local_storage,
431 .ret_type = RET_PTR_TO_MAP_VALUE,
432 .arg1_type = ARG_CONST_MAP_PTR,
433 .arg2_type = ARG_ANYTHING,
437 #define BPF_STRTOX_BASE_MASK 0x1F
439 static int __bpf_strtoull(const char *buf, size_t buf_len, u64 flags,
440 unsigned long long *res, bool *is_negative)
442 unsigned int base = flags & BPF_STRTOX_BASE_MASK;
443 const char *cur_buf = buf;
444 size_t cur_len = buf_len;
445 unsigned int consumed;
449 if (!buf || !buf_len || !res || !is_negative)
452 if (base != 0 && base != 8 && base != 10 && base != 16)
455 if (flags & ~BPF_STRTOX_BASE_MASK)
458 while (cur_buf < buf + buf_len && isspace(*cur_buf))
461 *is_negative = (cur_buf < buf + buf_len && *cur_buf == '-');
465 consumed = cur_buf - buf;
470 cur_len = min(cur_len, sizeof(str) - 1);
471 memcpy(str, cur_buf, cur_len);
475 cur_buf = _parse_integer_fixup_radix(cur_buf, &base);
476 val_len = _parse_integer(cur_buf, base, res);
478 if (val_len & KSTRTOX_OVERFLOW)
485 consumed += cur_buf - str;
490 static int __bpf_strtoll(const char *buf, size_t buf_len, u64 flags,
493 unsigned long long _res;
497 err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
501 if ((long long)-_res > 0)
505 if ((long long)_res < 0)
512 BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags,
518 err = __bpf_strtoll(buf, buf_len, flags, &_res);
521 if (_res != (long)_res)
527 const struct bpf_func_proto bpf_strtol_proto = {
530 .ret_type = RET_INTEGER,
531 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
532 .arg2_type = ARG_CONST_SIZE,
533 .arg3_type = ARG_ANYTHING,
534 .arg4_type = ARG_PTR_TO_LONG,
537 BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,
538 unsigned long *, res)
540 unsigned long long _res;
544 err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
549 if (_res != (unsigned long)_res)
555 const struct bpf_func_proto bpf_strtoul_proto = {
558 .ret_type = RET_INTEGER,
559 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
560 .arg2_type = ARG_CONST_SIZE,
561 .arg3_type = ARG_ANYTHING,
562 .arg4_type = ARG_PTR_TO_LONG,
566 BPF_CALL_3(bpf_strncmp, const char *, s1, u32, s1_sz, const char *, s2)
568 return strncmp(s1, s2, s1_sz);
571 const struct bpf_func_proto bpf_strncmp_proto = {
574 .ret_type = RET_INTEGER,
575 .arg1_type = ARG_PTR_TO_MEM,
576 .arg2_type = ARG_CONST_SIZE,
577 .arg3_type = ARG_PTR_TO_CONST_STR,
580 BPF_CALL_4(bpf_get_ns_current_pid_tgid, u64, dev, u64, ino,
581 struct bpf_pidns_info *, nsdata, u32, size)
583 struct task_struct *task = current;
584 struct pid_namespace *pidns;
587 if (unlikely(size != sizeof(struct bpf_pidns_info)))
590 if (unlikely((u64)(dev_t)dev != dev))
596 pidns = task_active_pid_ns(task);
597 if (unlikely(!pidns)) {
602 if (!ns_match(&pidns->ns, (dev_t)dev, ino))
605 nsdata->pid = task_pid_nr_ns(task, pidns);
606 nsdata->tgid = task_tgid_nr_ns(task, pidns);
609 memset((void *)nsdata, 0, (size_t) size);
613 const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = {
614 .func = bpf_get_ns_current_pid_tgid,
616 .ret_type = RET_INTEGER,
617 .arg1_type = ARG_ANYTHING,
618 .arg2_type = ARG_ANYTHING,
619 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
620 .arg4_type = ARG_CONST_SIZE,
623 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
624 .func = bpf_get_raw_cpu_id,
626 .ret_type = RET_INTEGER,
629 BPF_CALL_5(bpf_event_output_data, void *, ctx, struct bpf_map *, map,
630 u64, flags, void *, data, u64, size)
632 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
635 return bpf_event_output(map, flags, data, size, NULL, 0, NULL);
638 const struct bpf_func_proto bpf_event_output_data_proto = {
639 .func = bpf_event_output_data,
641 .ret_type = RET_INTEGER,
642 .arg1_type = ARG_PTR_TO_CTX,
643 .arg2_type = ARG_CONST_MAP_PTR,
644 .arg3_type = ARG_ANYTHING,
645 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
646 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
649 BPF_CALL_3(bpf_copy_from_user, void *, dst, u32, size,
650 const void __user *, user_ptr)
652 int ret = copy_from_user(dst, user_ptr, size);
655 memset(dst, 0, size);
662 const struct bpf_func_proto bpf_copy_from_user_proto = {
663 .func = bpf_copy_from_user,
665 .ret_type = RET_INTEGER,
666 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
667 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
668 .arg3_type = ARG_ANYTHING,
671 BPF_CALL_5(bpf_copy_from_user_task, void *, dst, u32, size,
672 const void __user *, user_ptr, struct task_struct *, tsk, u64, flags)
676 /* flags is not used yet */
683 ret = access_process_vm(tsk, (unsigned long)user_ptr, dst, size, 0);
687 memset(dst, 0, size);
688 /* Return -EFAULT for partial read */
689 return ret < 0 ? ret : -EFAULT;
692 const struct bpf_func_proto bpf_copy_from_user_task_proto = {
693 .func = bpf_copy_from_user_task,
695 .ret_type = RET_INTEGER,
696 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
697 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
698 .arg3_type = ARG_ANYTHING,
699 .arg4_type = ARG_PTR_TO_BTF_ID,
700 .arg4_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
701 .arg5_type = ARG_ANYTHING
704 BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu)
706 if (cpu >= nr_cpu_ids)
707 return (unsigned long)NULL;
709 return (unsigned long)per_cpu_ptr((const void __percpu *)ptr, cpu);
712 const struct bpf_func_proto bpf_per_cpu_ptr_proto = {
713 .func = bpf_per_cpu_ptr,
715 .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | PTR_MAYBE_NULL | MEM_RDONLY,
716 .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
717 .arg2_type = ARG_ANYTHING,
720 BPF_CALL_1(bpf_this_cpu_ptr, const void *, percpu_ptr)
722 return (unsigned long)this_cpu_ptr((const void __percpu *)percpu_ptr);
725 const struct bpf_func_proto bpf_this_cpu_ptr_proto = {
726 .func = bpf_this_cpu_ptr,
728 .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | MEM_RDONLY,
729 .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
732 static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
735 void __user *user_ptr = (__force void __user *)unsafe_ptr;
741 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
742 if ((unsigned long)unsafe_ptr < TASK_SIZE)
743 return strncpy_from_user_nofault(buf, user_ptr, bufsz);
747 return strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz);
749 return strncpy_from_user_nofault(buf, user_ptr, bufsz);
755 /* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary
756 * arguments representation.
758 #define MAX_BPRINTF_BUF_LEN 512
760 /* Support executing three nested bprintf helper calls on a given CPU */
761 #define MAX_BPRINTF_NEST_LEVEL 3
762 struct bpf_bprintf_buffers {
763 char tmp_bufs[MAX_BPRINTF_NEST_LEVEL][MAX_BPRINTF_BUF_LEN];
765 static DEFINE_PER_CPU(struct bpf_bprintf_buffers, bpf_bprintf_bufs);
766 static DEFINE_PER_CPU(int, bpf_bprintf_nest_level);
768 static int try_get_fmt_tmp_buf(char **tmp_buf)
770 struct bpf_bprintf_buffers *bufs;
774 nest_level = this_cpu_inc_return(bpf_bprintf_nest_level);
775 if (WARN_ON_ONCE(nest_level > MAX_BPRINTF_NEST_LEVEL)) {
776 this_cpu_dec(bpf_bprintf_nest_level);
780 bufs = this_cpu_ptr(&bpf_bprintf_bufs);
781 *tmp_buf = bufs->tmp_bufs[nest_level - 1];
786 void bpf_bprintf_cleanup(void)
788 if (this_cpu_read(bpf_bprintf_nest_level)) {
789 this_cpu_dec(bpf_bprintf_nest_level);
795 * bpf_bprintf_prepare - Generic pass on format strings for bprintf-like helpers
797 * Returns a negative value if fmt is an invalid format string or 0 otherwise.
799 * This can be used in two ways:
800 * - Format string verification only: when bin_args is NULL
801 * - Arguments preparation: in addition to the above verification, it writes in
802 * bin_args a binary representation of arguments usable by bstr_printf where
803 * pointers from BPF have been sanitized.
805 * In argument preparation mode, if 0 is returned, safe temporary buffers are
806 * allocated and bpf_bprintf_cleanup should be called to free them after use.
808 int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
809 u32 **bin_args, u32 num_args)
811 char *unsafe_ptr = NULL, *tmp_buf = NULL, *tmp_buf_end, *fmt_end;
812 size_t sizeof_cur_arg, sizeof_cur_ip;
813 int err, i, num_spec = 0;
815 char fmt_ptype, cur_ip[16], ip_spec[] = "%pXX";
817 fmt_end = strnchr(fmt, fmt_size, 0);
820 fmt_size = fmt_end - fmt;
823 if (num_args && try_get_fmt_tmp_buf(&tmp_buf))
826 tmp_buf_end = tmp_buf + MAX_BPRINTF_BUF_LEN;
827 *bin_args = (u32 *)tmp_buf;
830 for (i = 0; i < fmt_size; i++) {
831 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
839 if (fmt[i + 1] == '%') {
844 if (num_spec >= num_args) {
849 /* The string is zero-terminated so if fmt[i] != 0, we can
850 * always access fmt[i + 1], in the worst case it will be a 0
854 /* skip optional "[0 +-][num]" width formatting field */
855 while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' ||
858 if (fmt[i] >= '1' && fmt[i] <= '9') {
860 while (fmt[i] >= '0' && fmt[i] <= '9')
865 sizeof_cur_arg = sizeof(long);
867 if ((fmt[i + 1] == 'k' || fmt[i + 1] == 'u') &&
869 fmt_ptype = fmt[i + 1];
874 if (fmt[i + 1] == 0 || isspace(fmt[i + 1]) ||
875 ispunct(fmt[i + 1]) || fmt[i + 1] == 'K' ||
876 fmt[i + 1] == 'x' || fmt[i + 1] == 's' ||
878 /* just kernel pointers */
880 cur_arg = raw_args[num_spec];
885 if (fmt[i + 1] == 'B') {
887 err = snprintf(tmp_buf,
888 (tmp_buf_end - tmp_buf),
890 (void *)(long)raw_args[num_spec]);
891 tmp_buf += (err + 1);
899 /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */
900 if ((fmt[i + 1] != 'i' && fmt[i + 1] != 'I') ||
901 (fmt[i + 2] != '4' && fmt[i + 2] != '6')) {
910 sizeof_cur_ip = (fmt[i] == '4') ? 4 : 16;
911 if (tmp_buf_end - tmp_buf < sizeof_cur_ip) {
916 unsafe_ptr = (char *)(long)raw_args[num_spec];
917 err = copy_from_kernel_nofault(cur_ip, unsafe_ptr,
920 memset(cur_ip, 0, sizeof_cur_ip);
922 /* hack: bstr_printf expects IP addresses to be
923 * pre-formatted as strings, ironically, the easiest way
924 * to do that is to call snprintf.
926 ip_spec[2] = fmt[i - 1];
928 err = snprintf(tmp_buf, tmp_buf_end - tmp_buf,
935 } else if (fmt[i] == 's') {
938 if (fmt[i + 1] != 0 &&
939 !isspace(fmt[i + 1]) &&
940 !ispunct(fmt[i + 1])) {
948 if (tmp_buf_end == tmp_buf) {
953 unsafe_ptr = (char *)(long)raw_args[num_spec];
954 err = bpf_trace_copy_string(tmp_buf, unsafe_ptr,
956 tmp_buf_end - tmp_buf);
966 } else if (fmt[i] == 'c') {
970 if (tmp_buf_end == tmp_buf) {
975 *tmp_buf = raw_args[num_spec];
982 sizeof_cur_arg = sizeof(int);
985 sizeof_cur_arg = sizeof(long);
989 sizeof_cur_arg = sizeof(long long);
993 if (fmt[i] != 'i' && fmt[i] != 'd' && fmt[i] != 'u' &&
994 fmt[i] != 'x' && fmt[i] != 'X') {
1000 cur_arg = raw_args[num_spec];
1003 tmp_buf = PTR_ALIGN(tmp_buf, sizeof(u32));
1004 if (tmp_buf_end - tmp_buf < sizeof_cur_arg) {
1009 if (sizeof_cur_arg == 8) {
1010 *(u32 *)tmp_buf = *(u32 *)&cur_arg;
1011 *(u32 *)(tmp_buf + 4) = *((u32 *)&cur_arg + 1);
1013 *(u32 *)tmp_buf = (u32)(long)cur_arg;
1015 tmp_buf += sizeof_cur_arg;
1023 bpf_bprintf_cleanup();
1027 BPF_CALL_5(bpf_snprintf, char *, str, u32, str_size, char *, fmt,
1028 const void *, data, u32, data_len)
1033 if (data_len % 8 || data_len > MAX_BPRINTF_VARARGS * 8 ||
1034 (data_len && !data))
1036 num_args = data_len / 8;
1038 /* ARG_PTR_TO_CONST_STR guarantees that fmt is zero-terminated so we
1039 * can safely give an unbounded size.
1041 err = bpf_bprintf_prepare(fmt, UINT_MAX, data, &bin_args, num_args);
1045 err = bstr_printf(str, str_size, fmt, bin_args);
1047 bpf_bprintf_cleanup();
1052 const struct bpf_func_proto bpf_snprintf_proto = {
1053 .func = bpf_snprintf,
1055 .ret_type = RET_INTEGER,
1056 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1057 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1058 .arg3_type = ARG_PTR_TO_CONST_STR,
1059 .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
1060 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1063 /* BPF map elements can contain 'struct bpf_timer'.
1064 * Such map owns all of its BPF timers.
1065 * 'struct bpf_timer' is allocated as part of map element allocation
1066 * and it's zero initialized.
1067 * That space is used to keep 'struct bpf_timer_kern'.
1068 * bpf_timer_init() allocates 'struct bpf_hrtimer', inits hrtimer, and
1069 * remembers 'struct bpf_map *' pointer it's part of.
1070 * bpf_timer_set_callback() increments prog refcnt and assign bpf callback_fn.
1071 * bpf_timer_start() arms the timer.
1072 * If user space reference to a map goes to zero at this point
1073 * ops->map_release_uref callback is responsible for cancelling the timers,
1074 * freeing their memory, and decrementing prog's refcnts.
1075 * bpf_timer_cancel() cancels the timer and decrements prog's refcnt.
1076 * Inner maps can contain bpf timers as well. ops->map_release_uref is
1077 * freeing the timers when inner map is replaced or deleted by user space.
1079 struct bpf_hrtimer {
1080 struct hrtimer timer;
1081 struct bpf_map *map;
1082 struct bpf_prog *prog;
1083 void __rcu *callback_fn;
1087 /* the actual struct hidden inside uapi struct bpf_timer */
1088 struct bpf_timer_kern {
1089 struct bpf_hrtimer *timer;
1090 /* bpf_spin_lock is used here instead of spinlock_t to make
1091 * sure that it always fits into space reserved by struct bpf_timer
1092 * regardless of LOCKDEP and spinlock debug flags.
1094 struct bpf_spin_lock lock;
1095 } __attribute__((aligned(8)));
1097 static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running);
1099 static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
1101 struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer);
1102 struct bpf_map *map = t->map;
1103 void *value = t->value;
1104 bpf_callback_t callback_fn;
1108 BTF_TYPE_EMIT(struct bpf_timer);
1109 callback_fn = rcu_dereference_check(t->callback_fn, rcu_read_lock_bh_held());
1113 /* bpf_timer_cb() runs in hrtimer_run_softirq. It doesn't migrate and
1114 * cannot be preempted by another bpf_timer_cb() on the same cpu.
1115 * Remember the timer this callback is servicing to prevent
1116 * deadlock if callback_fn() calls bpf_timer_cancel() or
1117 * bpf_map_delete_elem() on the same timer.
1119 this_cpu_write(hrtimer_running, t);
1120 if (map->map_type == BPF_MAP_TYPE_ARRAY) {
1121 struct bpf_array *array = container_of(map, struct bpf_array, map);
1123 /* compute the key */
1124 idx = ((char *)value - array->value) / array->elem_size;
1126 } else { /* hash or lru */
1127 key = value - round_up(map->key_size, 8);
1130 callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
1131 /* The verifier checked that return value is zero. */
1133 this_cpu_write(hrtimer_running, NULL);
1135 return HRTIMER_NORESTART;
1138 BPF_CALL_3(bpf_timer_init, struct bpf_timer_kern *, timer, struct bpf_map *, map,
1141 clockid_t clockid = flags & (MAX_CLOCKS - 1);
1142 struct bpf_hrtimer *t;
1145 BUILD_BUG_ON(MAX_CLOCKS != 16);
1146 BUILD_BUG_ON(sizeof(struct bpf_timer_kern) > sizeof(struct bpf_timer));
1147 BUILD_BUG_ON(__alignof__(struct bpf_timer_kern) != __alignof__(struct bpf_timer));
1152 if (flags >= MAX_CLOCKS ||
1153 /* similar to timerfd except _ALARM variants are not supported */
1154 (clockid != CLOCK_MONOTONIC &&
1155 clockid != CLOCK_REALTIME &&
1156 clockid != CLOCK_BOOTTIME))
1158 __bpf_spin_lock_irqsave(&timer->lock);
1164 if (!atomic64_read(&map->usercnt)) {
1165 /* maps with timers must be either held by user space
1166 * or pinned in bpffs.
1171 /* allocate hrtimer via map_kmalloc to use memcg accounting */
1172 t = bpf_map_kmalloc_node(map, sizeof(*t), GFP_ATOMIC, map->numa_node);
1177 t->value = (void *)timer - map->timer_off;
1180 rcu_assign_pointer(t->callback_fn, NULL);
1181 hrtimer_init(&t->timer, clockid, HRTIMER_MODE_REL_SOFT);
1182 t->timer.function = bpf_timer_cb;
1185 __bpf_spin_unlock_irqrestore(&timer->lock);
1189 static const struct bpf_func_proto bpf_timer_init_proto = {
1190 .func = bpf_timer_init,
1192 .ret_type = RET_INTEGER,
1193 .arg1_type = ARG_PTR_TO_TIMER,
1194 .arg2_type = ARG_CONST_MAP_PTR,
1195 .arg3_type = ARG_ANYTHING,
1198 BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callback_fn,
1199 struct bpf_prog_aux *, aux)
1201 struct bpf_prog *prev, *prog = aux->prog;
1202 struct bpf_hrtimer *t;
1207 __bpf_spin_lock_irqsave(&timer->lock);
1213 if (!atomic64_read(&t->map->usercnt)) {
1214 /* maps with timers must be either held by user space
1215 * or pinned in bpffs. Otherwise timer might still be
1216 * running even when bpf prog is detached and user space
1217 * is gone, since map_release_uref won't ever be called.
1224 /* Bump prog refcnt once. Every bpf_timer_set_callback()
1225 * can pick different callback_fn-s within the same prog.
1227 prog = bpf_prog_inc_not_zero(prog);
1229 ret = PTR_ERR(prog);
1233 /* Drop prev prog refcnt when swapping with new prog */
1237 rcu_assign_pointer(t->callback_fn, callback_fn);
1239 __bpf_spin_unlock_irqrestore(&timer->lock);
1243 static const struct bpf_func_proto bpf_timer_set_callback_proto = {
1244 .func = bpf_timer_set_callback,
1246 .ret_type = RET_INTEGER,
1247 .arg1_type = ARG_PTR_TO_TIMER,
1248 .arg2_type = ARG_PTR_TO_FUNC,
1251 BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, flags)
1253 struct bpf_hrtimer *t;
1260 __bpf_spin_lock_irqsave(&timer->lock);
1262 if (!t || !t->prog) {
1266 hrtimer_start(&t->timer, ns_to_ktime(nsecs), HRTIMER_MODE_REL_SOFT);
1268 __bpf_spin_unlock_irqrestore(&timer->lock);
1272 static const struct bpf_func_proto bpf_timer_start_proto = {
1273 .func = bpf_timer_start,
1275 .ret_type = RET_INTEGER,
1276 .arg1_type = ARG_PTR_TO_TIMER,
1277 .arg2_type = ARG_ANYTHING,
1278 .arg3_type = ARG_ANYTHING,
1281 static void drop_prog_refcnt(struct bpf_hrtimer *t)
1283 struct bpf_prog *prog = t->prog;
1288 rcu_assign_pointer(t->callback_fn, NULL);
1292 BPF_CALL_1(bpf_timer_cancel, struct bpf_timer_kern *, timer)
1294 struct bpf_hrtimer *t;
1299 __bpf_spin_lock_irqsave(&timer->lock);
1305 if (this_cpu_read(hrtimer_running) == t) {
1306 /* If bpf callback_fn is trying to bpf_timer_cancel()
1307 * its own timer the hrtimer_cancel() will deadlock
1308 * since it waits for callback_fn to finish
1313 drop_prog_refcnt(t);
1315 __bpf_spin_unlock_irqrestore(&timer->lock);
1316 /* Cancel the timer and wait for associated callback to finish
1317 * if it was running.
1319 ret = ret ?: hrtimer_cancel(&t->timer);
1323 static const struct bpf_func_proto bpf_timer_cancel_proto = {
1324 .func = bpf_timer_cancel,
1326 .ret_type = RET_INTEGER,
1327 .arg1_type = ARG_PTR_TO_TIMER,
1330 /* This function is called by map_delete/update_elem for individual element and
1331 * by ops->map_release_uref when the user space reference to a map reaches zero.
1333 void bpf_timer_cancel_and_free(void *val)
1335 struct bpf_timer_kern *timer = val;
1336 struct bpf_hrtimer *t;
1338 /* Performance optimization: read timer->timer without lock first. */
1339 if (!READ_ONCE(timer->timer))
1342 __bpf_spin_lock_irqsave(&timer->lock);
1343 /* re-read it under lock */
1347 drop_prog_refcnt(t);
1348 /* The subsequent bpf_timer_start/cancel() helpers won't be able to use
1349 * this timer, since it won't be initialized.
1351 timer->timer = NULL;
1353 __bpf_spin_unlock_irqrestore(&timer->lock);
1356 /* Cancel the timer and wait for callback to complete if it was running.
1357 * If hrtimer_cancel() can be safely called it's safe to call kfree(t)
1358 * right after for both preallocated and non-preallocated maps.
1359 * The timer->timer = NULL was already done and no code path can
1360 * see address 't' anymore.
1362 * Check that bpf_map_delete/update_elem() wasn't called from timer
1363 * callback_fn. In such case don't call hrtimer_cancel() (since it will
1364 * deadlock) and don't call hrtimer_try_to_cancel() (since it will just
1365 * return -1). Though callback_fn is still running on this cpu it's
1366 * safe to do kfree(t) because bpf_timer_cb() read everything it needed
1367 * from 't'. The bpf subprog callback_fn won't be able to access 't',
1368 * since timer->timer = NULL was already done. The timer will be
1369 * effectively cancelled because bpf_timer_cb() will return
1370 * HRTIMER_NORESTART.
1372 if (this_cpu_read(hrtimer_running) != t)
1373 hrtimer_cancel(&t->timer);
1377 const struct bpf_func_proto bpf_get_current_task_proto __weak;
1378 const struct bpf_func_proto bpf_get_current_task_btf_proto __weak;
1379 const struct bpf_func_proto bpf_probe_read_user_proto __weak;
1380 const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
1381 const struct bpf_func_proto bpf_probe_read_kernel_proto __weak;
1382 const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
1383 const struct bpf_func_proto bpf_task_pt_regs_proto __weak;
1385 const struct bpf_func_proto *
1386 bpf_base_func_proto(enum bpf_func_id func_id)
1389 case BPF_FUNC_map_lookup_elem:
1390 return &bpf_map_lookup_elem_proto;
1391 case BPF_FUNC_map_update_elem:
1392 return &bpf_map_update_elem_proto;
1393 case BPF_FUNC_map_delete_elem:
1394 return &bpf_map_delete_elem_proto;
1395 case BPF_FUNC_map_push_elem:
1396 return &bpf_map_push_elem_proto;
1397 case BPF_FUNC_map_pop_elem:
1398 return &bpf_map_pop_elem_proto;
1399 case BPF_FUNC_map_peek_elem:
1400 return &bpf_map_peek_elem_proto;
1401 case BPF_FUNC_get_prandom_u32:
1402 return &bpf_get_prandom_u32_proto;
1403 case BPF_FUNC_get_smp_processor_id:
1404 return &bpf_get_raw_smp_processor_id_proto;
1405 case BPF_FUNC_get_numa_node_id:
1406 return &bpf_get_numa_node_id_proto;
1407 case BPF_FUNC_tail_call:
1408 return &bpf_tail_call_proto;
1409 case BPF_FUNC_ktime_get_ns:
1410 return &bpf_ktime_get_ns_proto;
1411 case BPF_FUNC_ktime_get_boot_ns:
1412 return &bpf_ktime_get_boot_ns_proto;
1413 case BPF_FUNC_ringbuf_output:
1414 return &bpf_ringbuf_output_proto;
1415 case BPF_FUNC_ringbuf_reserve:
1416 return &bpf_ringbuf_reserve_proto;
1417 case BPF_FUNC_ringbuf_submit:
1418 return &bpf_ringbuf_submit_proto;
1419 case BPF_FUNC_ringbuf_discard:
1420 return &bpf_ringbuf_discard_proto;
1421 case BPF_FUNC_ringbuf_query:
1422 return &bpf_ringbuf_query_proto;
1423 case BPF_FUNC_for_each_map_elem:
1424 return &bpf_for_each_map_elem_proto;
1426 return &bpf_loop_proto;
1427 case BPF_FUNC_strncmp:
1428 return &bpf_strncmp_proto;
1437 case BPF_FUNC_spin_lock:
1438 return &bpf_spin_lock_proto;
1439 case BPF_FUNC_spin_unlock:
1440 return &bpf_spin_unlock_proto;
1441 case BPF_FUNC_jiffies64:
1442 return &bpf_jiffies64_proto;
1443 case BPF_FUNC_per_cpu_ptr:
1444 return &bpf_per_cpu_ptr_proto;
1445 case BPF_FUNC_this_cpu_ptr:
1446 return &bpf_this_cpu_ptr_proto;
1447 case BPF_FUNC_timer_init:
1448 return &bpf_timer_init_proto;
1449 case BPF_FUNC_timer_set_callback:
1450 return &bpf_timer_set_callback_proto;
1451 case BPF_FUNC_timer_start:
1452 return &bpf_timer_start_proto;
1453 case BPF_FUNC_timer_cancel:
1454 return &bpf_timer_cancel_proto;
1459 if (!perfmon_capable())
1463 case BPF_FUNC_trace_printk:
1464 return bpf_get_trace_printk_proto();
1465 case BPF_FUNC_get_current_task:
1466 return &bpf_get_current_task_proto;
1467 case BPF_FUNC_get_current_task_btf:
1468 return &bpf_get_current_task_btf_proto;
1469 case BPF_FUNC_probe_read_user:
1470 return &bpf_probe_read_user_proto;
1471 case BPF_FUNC_probe_read_kernel:
1472 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1473 NULL : &bpf_probe_read_kernel_proto;
1474 case BPF_FUNC_probe_read_user_str:
1475 return &bpf_probe_read_user_str_proto;
1476 case BPF_FUNC_probe_read_kernel_str:
1477 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1478 NULL : &bpf_probe_read_kernel_str_proto;
1479 case BPF_FUNC_snprintf_btf:
1480 return &bpf_snprintf_btf_proto;
1481 case BPF_FUNC_snprintf:
1482 return &bpf_snprintf_proto;
1483 case BPF_FUNC_task_pt_regs:
1484 return &bpf_task_pt_regs_proto;
1485 case BPF_FUNC_trace_vprintk:
1486 return bpf_get_trace_vprintk_proto();