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/rcupdate.h>
8 #include <linux/random.h>
9 #include <linux/smp.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/poison.h>
19 #include <linux/proc_ns.h>
20 #include <linux/security.h>
21 #include <linux/btf_ids.h>
22
23 #include "../../lib/kstrtox.h"
24
25 /* If kernel subsystem is allowing eBPF programs to call this function,
26 * inside its own verifier_ops->get_func_proto() callback it should return
27 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
28 *
29 * Different map implementations will rely on rcu in map methods
30 * lookup/update/delete, therefore eBPF programs must run under rcu lock
31 * if program is allowed to access maps, so check rcu_read_lock_held() or
32 * rcu_read_lock_trace_held() in all three functions.
33 */
BPF_CALL_2(bpf_map_lookup_elem,struct bpf_map *,map,void *,key)34 BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
35 {
36 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
37 !rcu_read_lock_bh_held());
38 return (unsigned long) map->ops->map_lookup_elem(map, key);
39 }
40
41 const struct bpf_func_proto bpf_map_lookup_elem_proto = {
42 .func = bpf_map_lookup_elem,
43 .gpl_only = false,
44 .pkt_access = true,
45 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
46 .arg1_type = ARG_CONST_MAP_PTR,
47 .arg2_type = ARG_PTR_TO_MAP_KEY,
48 };
49
BPF_CALL_4(bpf_map_update_elem,struct bpf_map *,map,void *,key,void *,value,u64,flags)50 BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
51 void *, value, u64, flags)
52 {
53 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
54 !rcu_read_lock_bh_held());
55 return map->ops->map_update_elem(map, key, value, flags);
56 }
57
58 const struct bpf_func_proto bpf_map_update_elem_proto = {
59 .func = bpf_map_update_elem,
60 .gpl_only = false,
61 .pkt_access = true,
62 .ret_type = RET_INTEGER,
63 .arg1_type = ARG_CONST_MAP_PTR,
64 .arg2_type = ARG_PTR_TO_MAP_KEY,
65 .arg3_type = ARG_PTR_TO_MAP_VALUE,
66 .arg4_type = ARG_ANYTHING,
67 };
68
BPF_CALL_2(bpf_map_delete_elem,struct bpf_map *,map,void *,key)69 BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
70 {
71 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
72 !rcu_read_lock_bh_held());
73 return map->ops->map_delete_elem(map, key);
74 }
75
76 const struct bpf_func_proto bpf_map_delete_elem_proto = {
77 .func = bpf_map_delete_elem,
78 .gpl_only = false,
79 .pkt_access = true,
80 .ret_type = RET_INTEGER,
81 .arg1_type = ARG_CONST_MAP_PTR,
82 .arg2_type = ARG_PTR_TO_MAP_KEY,
83 };
84
BPF_CALL_3(bpf_map_push_elem,struct bpf_map *,map,void *,value,u64,flags)85 BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags)
86 {
87 return map->ops->map_push_elem(map, value, flags);
88 }
89
90 const struct bpf_func_proto bpf_map_push_elem_proto = {
91 .func = bpf_map_push_elem,
92 .gpl_only = false,
93 .pkt_access = true,
94 .ret_type = RET_INTEGER,
95 .arg1_type = ARG_CONST_MAP_PTR,
96 .arg2_type = ARG_PTR_TO_MAP_VALUE,
97 .arg3_type = ARG_ANYTHING,
98 };
99
BPF_CALL_2(bpf_map_pop_elem,struct bpf_map *,map,void *,value)100 BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value)
101 {
102 return map->ops->map_pop_elem(map, value);
103 }
104
105 const struct bpf_func_proto bpf_map_pop_elem_proto = {
106 .func = bpf_map_pop_elem,
107 .gpl_only = false,
108 .ret_type = RET_INTEGER,
109 .arg1_type = ARG_CONST_MAP_PTR,
110 .arg2_type = ARG_PTR_TO_MAP_VALUE | MEM_UNINIT,
111 };
112
BPF_CALL_2(bpf_map_peek_elem,struct bpf_map *,map,void *,value)113 BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value)
114 {
115 return map->ops->map_peek_elem(map, value);
116 }
117
118 const struct bpf_func_proto bpf_map_peek_elem_proto = {
119 .func = bpf_map_peek_elem,
120 .gpl_only = false,
121 .ret_type = RET_INTEGER,
122 .arg1_type = ARG_CONST_MAP_PTR,
123 .arg2_type = ARG_PTR_TO_MAP_VALUE | MEM_UNINIT,
124 };
125
BPF_CALL_3(bpf_map_lookup_percpu_elem,struct bpf_map *,map,void *,key,u32,cpu)126 BPF_CALL_3(bpf_map_lookup_percpu_elem, struct bpf_map *, map, void *, key, u32, cpu)
127 {
128 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
129 return (unsigned long) map->ops->map_lookup_percpu_elem(map, key, cpu);
130 }
131
132 const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto = {
133 .func = bpf_map_lookup_percpu_elem,
134 .gpl_only = false,
135 .pkt_access = true,
136 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
137 .arg1_type = ARG_CONST_MAP_PTR,
138 .arg2_type = ARG_PTR_TO_MAP_KEY,
139 .arg3_type = ARG_ANYTHING,
140 };
141
142 const struct bpf_func_proto bpf_get_prandom_u32_proto = {
143 .func = bpf_user_rnd_u32,
144 .gpl_only = false,
145 .ret_type = RET_INTEGER,
146 };
147
BPF_CALL_0(bpf_get_smp_processor_id)148 BPF_CALL_0(bpf_get_smp_processor_id)
149 {
150 return smp_processor_id();
151 }
152
153 const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
154 .func = bpf_get_smp_processor_id,
155 .gpl_only = false,
156 .ret_type = RET_INTEGER,
157 };
158
BPF_CALL_0(bpf_get_numa_node_id)159 BPF_CALL_0(bpf_get_numa_node_id)
160 {
161 return numa_node_id();
162 }
163
164 const struct bpf_func_proto bpf_get_numa_node_id_proto = {
165 .func = bpf_get_numa_node_id,
166 .gpl_only = false,
167 .ret_type = RET_INTEGER,
168 };
169
BPF_CALL_0(bpf_ktime_get_ns)170 BPF_CALL_0(bpf_ktime_get_ns)
171 {
172 /* NMI safe access to clock monotonic */
173 return ktime_get_mono_fast_ns();
174 }
175
176 const struct bpf_func_proto bpf_ktime_get_ns_proto = {
177 .func = bpf_ktime_get_ns,
178 .gpl_only = false,
179 .ret_type = RET_INTEGER,
180 };
181
BPF_CALL_0(bpf_ktime_get_boot_ns)182 BPF_CALL_0(bpf_ktime_get_boot_ns)
183 {
184 /* NMI safe access to clock boottime */
185 return ktime_get_boot_fast_ns();
186 }
187
188 const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = {
189 .func = bpf_ktime_get_boot_ns,
190 .gpl_only = false,
191 .ret_type = RET_INTEGER,
192 };
193
BPF_CALL_0(bpf_ktime_get_coarse_ns)194 BPF_CALL_0(bpf_ktime_get_coarse_ns)
195 {
196 return ktime_get_coarse_ns();
197 }
198
199 const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto = {
200 .func = bpf_ktime_get_coarse_ns,
201 .gpl_only = false,
202 .ret_type = RET_INTEGER,
203 };
204
BPF_CALL_0(bpf_ktime_get_tai_ns)205 BPF_CALL_0(bpf_ktime_get_tai_ns)
206 {
207 /* NMI safe access to clock tai */
208 return ktime_get_tai_fast_ns();
209 }
210
211 const struct bpf_func_proto bpf_ktime_get_tai_ns_proto = {
212 .func = bpf_ktime_get_tai_ns,
213 .gpl_only = false,
214 .ret_type = RET_INTEGER,
215 };
216
BPF_CALL_0(bpf_get_current_pid_tgid)217 BPF_CALL_0(bpf_get_current_pid_tgid)
218 {
219 struct task_struct *task = current;
220
221 if (unlikely(!task))
222 return -EINVAL;
223
224 return (u64) task->tgid << 32 | task->pid;
225 }
226
227 const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
228 .func = bpf_get_current_pid_tgid,
229 .gpl_only = false,
230 .ret_type = RET_INTEGER,
231 };
232
BPF_CALL_0(bpf_get_current_uid_gid)233 BPF_CALL_0(bpf_get_current_uid_gid)
234 {
235 struct task_struct *task = current;
236 kuid_t uid;
237 kgid_t gid;
238
239 if (unlikely(!task))
240 return -EINVAL;
241
242 current_uid_gid(&uid, &gid);
243 return (u64) from_kgid(&init_user_ns, gid) << 32 |
244 from_kuid(&init_user_ns, uid);
245 }
246
247 const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
248 .func = bpf_get_current_uid_gid,
249 .gpl_only = false,
250 .ret_type = RET_INTEGER,
251 };
252
BPF_CALL_2(bpf_get_current_comm,char *,buf,u32,size)253 BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
254 {
255 struct task_struct *task = current;
256
257 if (unlikely(!task))
258 goto err_clear;
259
260 /* Verifier guarantees that size > 0 */
261 strscpy(buf, task->comm, size);
262 return 0;
263 err_clear:
264 memset(buf, 0, size);
265 return -EINVAL;
266 }
267
268 const struct bpf_func_proto bpf_get_current_comm_proto = {
269 .func = bpf_get_current_comm,
270 .gpl_only = false,
271 .ret_type = RET_INTEGER,
272 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
273 .arg2_type = ARG_CONST_SIZE,
274 };
275
276 #if defined(CONFIG_QUEUED_SPINLOCKS) || defined(CONFIG_BPF_ARCH_SPINLOCK)
277
__bpf_spin_lock(struct bpf_spin_lock * lock)278 static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
279 {
280 arch_spinlock_t *l = (void *)lock;
281 union {
282 __u32 val;
283 arch_spinlock_t lock;
284 } u = { .lock = __ARCH_SPIN_LOCK_UNLOCKED };
285
286 compiletime_assert(u.val == 0, "__ARCH_SPIN_LOCK_UNLOCKED not 0");
287 BUILD_BUG_ON(sizeof(*l) != sizeof(__u32));
288 BUILD_BUG_ON(sizeof(*lock) != sizeof(__u32));
289 arch_spin_lock(l);
290 }
291
__bpf_spin_unlock(struct bpf_spin_lock * lock)292 static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
293 {
294 arch_spinlock_t *l = (void *)lock;
295
296 arch_spin_unlock(l);
297 }
298
299 #else
300
__bpf_spin_lock(struct bpf_spin_lock * lock)301 static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
302 {
303 atomic_t *l = (void *)lock;
304
305 BUILD_BUG_ON(sizeof(*l) != sizeof(*lock));
306 do {
307 atomic_cond_read_relaxed(l, !VAL);
308 } while (atomic_xchg(l, 1));
309 }
310
__bpf_spin_unlock(struct bpf_spin_lock * lock)311 static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
312 {
313 atomic_t *l = (void *)lock;
314
315 atomic_set_release(l, 0);
316 }
317
318 #endif
319
320 static DEFINE_PER_CPU(unsigned long, irqsave_flags);
321
__bpf_spin_lock_irqsave(struct bpf_spin_lock * lock)322 static inline void __bpf_spin_lock_irqsave(struct bpf_spin_lock *lock)
323 {
324 unsigned long flags;
325
326 local_irq_save(flags);
327 __bpf_spin_lock(lock);
328 __this_cpu_write(irqsave_flags, flags);
329 }
330
BPF_CALL_1(bpf_spin_lock,struct bpf_spin_lock *,lock)331 notrace BPF_CALL_1(bpf_spin_lock, struct bpf_spin_lock *, lock)
332 {
333 __bpf_spin_lock_irqsave(lock);
334 return 0;
335 }
336
337 const struct bpf_func_proto bpf_spin_lock_proto = {
338 .func = bpf_spin_lock,
339 .gpl_only = false,
340 .ret_type = RET_VOID,
341 .arg1_type = ARG_PTR_TO_SPIN_LOCK,
342 };
343
__bpf_spin_unlock_irqrestore(struct bpf_spin_lock * lock)344 static inline void __bpf_spin_unlock_irqrestore(struct bpf_spin_lock *lock)
345 {
346 unsigned long flags;
347
348 flags = __this_cpu_read(irqsave_flags);
349 __bpf_spin_unlock(lock);
350 local_irq_restore(flags);
351 }
352
BPF_CALL_1(bpf_spin_unlock,struct bpf_spin_lock *,lock)353 notrace BPF_CALL_1(bpf_spin_unlock, struct bpf_spin_lock *, lock)
354 {
355 __bpf_spin_unlock_irqrestore(lock);
356 return 0;
357 }
358
359 const struct bpf_func_proto bpf_spin_unlock_proto = {
360 .func = bpf_spin_unlock,
361 .gpl_only = false,
362 .ret_type = RET_VOID,
363 .arg1_type = ARG_PTR_TO_SPIN_LOCK,
364 };
365
copy_map_value_locked(struct bpf_map * map,void * dst,void * src,bool lock_src)366 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
367 bool lock_src)
368 {
369 struct bpf_spin_lock *lock;
370
371 if (lock_src)
372 lock = src + map->spin_lock_off;
373 else
374 lock = dst + map->spin_lock_off;
375 preempt_disable();
376 __bpf_spin_lock_irqsave(lock);
377 copy_map_value(map, dst, src);
378 __bpf_spin_unlock_irqrestore(lock);
379 preempt_enable();
380 }
381
BPF_CALL_0(bpf_jiffies64)382 BPF_CALL_0(bpf_jiffies64)
383 {
384 return get_jiffies_64();
385 }
386
387 const struct bpf_func_proto bpf_jiffies64_proto = {
388 .func = bpf_jiffies64,
389 .gpl_only = false,
390 .ret_type = RET_INTEGER,
391 };
392
393 #ifdef CONFIG_CGROUPS
BPF_CALL_0(bpf_get_current_cgroup_id)394 BPF_CALL_0(bpf_get_current_cgroup_id)
395 {
396 struct cgroup *cgrp;
397 u64 cgrp_id;
398
399 rcu_read_lock();
400 cgrp = task_dfl_cgroup(current);
401 cgrp_id = cgroup_id(cgrp);
402 rcu_read_unlock();
403
404 return cgrp_id;
405 }
406
407 const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
408 .func = bpf_get_current_cgroup_id,
409 .gpl_only = false,
410 .ret_type = RET_INTEGER,
411 };
412
BPF_CALL_1(bpf_get_current_ancestor_cgroup_id,int,ancestor_level)413 BPF_CALL_1(bpf_get_current_ancestor_cgroup_id, int, ancestor_level)
414 {
415 struct cgroup *cgrp;
416 struct cgroup *ancestor;
417 u64 cgrp_id;
418
419 rcu_read_lock();
420 cgrp = task_dfl_cgroup(current);
421 ancestor = cgroup_ancestor(cgrp, ancestor_level);
422 cgrp_id = ancestor ? cgroup_id(ancestor) : 0;
423 rcu_read_unlock();
424
425 return cgrp_id;
426 }
427
428 const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto = {
429 .func = bpf_get_current_ancestor_cgroup_id,
430 .gpl_only = false,
431 .ret_type = RET_INTEGER,
432 .arg1_type = ARG_ANYTHING,
433 };
434 #endif /* CONFIG_CGROUPS */
435
436 #define BPF_STRTOX_BASE_MASK 0x1F
437
__bpf_strtoull(const char * buf,size_t buf_len,u64 flags,unsigned long long * res,bool * is_negative)438 static int __bpf_strtoull(const char *buf, size_t buf_len, u64 flags,
439 unsigned long long *res, bool *is_negative)
440 {
441 unsigned int base = flags & BPF_STRTOX_BASE_MASK;
442 const char *cur_buf = buf;
443 size_t cur_len = buf_len;
444 unsigned int consumed;
445 size_t val_len;
446 char str[64];
447
448 if (!buf || !buf_len || !res || !is_negative)
449 return -EINVAL;
450
451 if (base != 0 && base != 8 && base != 10 && base != 16)
452 return -EINVAL;
453
454 if (flags & ~BPF_STRTOX_BASE_MASK)
455 return -EINVAL;
456
457 while (cur_buf < buf + buf_len && isspace(*cur_buf))
458 ++cur_buf;
459
460 *is_negative = (cur_buf < buf + buf_len && *cur_buf == '-');
461 if (*is_negative)
462 ++cur_buf;
463
464 consumed = cur_buf - buf;
465 cur_len -= consumed;
466 if (!cur_len)
467 return -EINVAL;
468
469 cur_len = min(cur_len, sizeof(str) - 1);
470 memcpy(str, cur_buf, cur_len);
471 str[cur_len] = '\0';
472 cur_buf = str;
473
474 cur_buf = _parse_integer_fixup_radix(cur_buf, &base);
475 val_len = _parse_integer(cur_buf, base, res);
476
477 if (val_len & KSTRTOX_OVERFLOW)
478 return -ERANGE;
479
480 if (val_len == 0)
481 return -EINVAL;
482
483 cur_buf += val_len;
484 consumed += cur_buf - str;
485
486 return consumed;
487 }
488
__bpf_strtoll(const char * buf,size_t buf_len,u64 flags,long long * res)489 static int __bpf_strtoll(const char *buf, size_t buf_len, u64 flags,
490 long long *res)
491 {
492 unsigned long long _res;
493 bool is_negative;
494 int err;
495
496 err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
497 if (err < 0)
498 return err;
499 if (is_negative) {
500 if ((long long)-_res > 0)
501 return -ERANGE;
502 *res = -_res;
503 } else {
504 if ((long long)_res < 0)
505 return -ERANGE;
506 *res = _res;
507 }
508 return err;
509 }
510
BPF_CALL_4(bpf_strtol,const char *,buf,size_t,buf_len,u64,flags,long *,res)511 BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags,
512 long *, res)
513 {
514 long long _res;
515 int err;
516
517 err = __bpf_strtoll(buf, buf_len, flags, &_res);
518 if (err < 0)
519 return err;
520 if (_res != (long)_res)
521 return -ERANGE;
522 *res = _res;
523 return err;
524 }
525
526 const struct bpf_func_proto bpf_strtol_proto = {
527 .func = bpf_strtol,
528 .gpl_only = false,
529 .ret_type = RET_INTEGER,
530 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
531 .arg2_type = ARG_CONST_SIZE,
532 .arg3_type = ARG_ANYTHING,
533 .arg4_type = ARG_PTR_TO_LONG,
534 };
535
BPF_CALL_4(bpf_strtoul,const char *,buf,size_t,buf_len,u64,flags,unsigned long *,res)536 BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,
537 unsigned long *, res)
538 {
539 unsigned long long _res;
540 bool is_negative;
541 int err;
542
543 err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
544 if (err < 0)
545 return err;
546 if (is_negative)
547 return -EINVAL;
548 if (_res != (unsigned long)_res)
549 return -ERANGE;
550 *res = _res;
551 return err;
552 }
553
554 const struct bpf_func_proto bpf_strtoul_proto = {
555 .func = bpf_strtoul,
556 .gpl_only = false,
557 .ret_type = RET_INTEGER,
558 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
559 .arg2_type = ARG_CONST_SIZE,
560 .arg3_type = ARG_ANYTHING,
561 .arg4_type = ARG_PTR_TO_LONG,
562 };
563
BPF_CALL_3(bpf_strncmp,const char *,s1,u32,s1_sz,const char *,s2)564 BPF_CALL_3(bpf_strncmp, const char *, s1, u32, s1_sz, const char *, s2)
565 {
566 return strncmp(s1, s2, s1_sz);
567 }
568
569 static const struct bpf_func_proto bpf_strncmp_proto = {
570 .func = bpf_strncmp,
571 .gpl_only = false,
572 .ret_type = RET_INTEGER,
573 .arg1_type = ARG_PTR_TO_MEM,
574 .arg2_type = ARG_CONST_SIZE,
575 .arg3_type = ARG_PTR_TO_CONST_STR,
576 };
577
BPF_CALL_4(bpf_get_ns_current_pid_tgid,u64,dev,u64,ino,struct bpf_pidns_info *,nsdata,u32,size)578 BPF_CALL_4(bpf_get_ns_current_pid_tgid, u64, dev, u64, ino,
579 struct bpf_pidns_info *, nsdata, u32, size)
580 {
581 struct task_struct *task = current;
582 struct pid_namespace *pidns;
583 int err = -EINVAL;
584
585 if (unlikely(size != sizeof(struct bpf_pidns_info)))
586 goto clear;
587
588 if (unlikely((u64)(dev_t)dev != dev))
589 goto clear;
590
591 if (unlikely(!task))
592 goto clear;
593
594 pidns = task_active_pid_ns(task);
595 if (unlikely(!pidns)) {
596 err = -ENOENT;
597 goto clear;
598 }
599
600 if (!ns_match(&pidns->ns, (dev_t)dev, ino))
601 goto clear;
602
603 nsdata->pid = task_pid_nr_ns(task, pidns);
604 nsdata->tgid = task_tgid_nr_ns(task, pidns);
605 return 0;
606 clear:
607 memset((void *)nsdata, 0, (size_t) size);
608 return err;
609 }
610
611 const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = {
612 .func = bpf_get_ns_current_pid_tgid,
613 .gpl_only = false,
614 .ret_type = RET_INTEGER,
615 .arg1_type = ARG_ANYTHING,
616 .arg2_type = ARG_ANYTHING,
617 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
618 .arg4_type = ARG_CONST_SIZE,
619 };
620
621 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
622 .func = bpf_get_raw_cpu_id,
623 .gpl_only = false,
624 .ret_type = RET_INTEGER,
625 };
626
BPF_CALL_5(bpf_event_output_data,void *,ctx,struct bpf_map *,map,u64,flags,void *,data,u64,size)627 BPF_CALL_5(bpf_event_output_data, void *, ctx, struct bpf_map *, map,
628 u64, flags, void *, data, u64, size)
629 {
630 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
631 return -EINVAL;
632
633 return bpf_event_output(map, flags, data, size, NULL, 0, NULL);
634 }
635
636 const struct bpf_func_proto bpf_event_output_data_proto = {
637 .func = bpf_event_output_data,
638 .gpl_only = true,
639 .ret_type = RET_INTEGER,
640 .arg1_type = ARG_PTR_TO_CTX,
641 .arg2_type = ARG_CONST_MAP_PTR,
642 .arg3_type = ARG_ANYTHING,
643 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
644 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
645 };
646
BPF_CALL_3(bpf_copy_from_user,void *,dst,u32,size,const void __user *,user_ptr)647 BPF_CALL_3(bpf_copy_from_user, void *, dst, u32, size,
648 const void __user *, user_ptr)
649 {
650 int ret = copy_from_user(dst, user_ptr, size);
651
652 if (unlikely(ret)) {
653 memset(dst, 0, size);
654 ret = -EFAULT;
655 }
656
657 return ret;
658 }
659
660 const struct bpf_func_proto bpf_copy_from_user_proto = {
661 .func = bpf_copy_from_user,
662 .gpl_only = false,
663 .ret_type = RET_INTEGER,
664 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
665 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
666 .arg3_type = ARG_ANYTHING,
667 };
668
BPF_CALL_5(bpf_copy_from_user_task,void *,dst,u32,size,const void __user *,user_ptr,struct task_struct *,tsk,u64,flags)669 BPF_CALL_5(bpf_copy_from_user_task, void *, dst, u32, size,
670 const void __user *, user_ptr, struct task_struct *, tsk, u64, flags)
671 {
672 int ret;
673
674 /* flags is not used yet */
675 if (unlikely(flags))
676 return -EINVAL;
677
678 if (unlikely(!size))
679 return 0;
680
681 ret = access_process_vm(tsk, (unsigned long)user_ptr, dst, size, 0);
682 if (ret == size)
683 return 0;
684
685 memset(dst, 0, size);
686 /* Return -EFAULT for partial read */
687 return ret < 0 ? ret : -EFAULT;
688 }
689
690 const struct bpf_func_proto bpf_copy_from_user_task_proto = {
691 .func = bpf_copy_from_user_task,
692 .gpl_only = true,
693 .ret_type = RET_INTEGER,
694 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
695 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
696 .arg3_type = ARG_ANYTHING,
697 .arg4_type = ARG_PTR_TO_BTF_ID,
698 .arg4_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
699 .arg5_type = ARG_ANYTHING
700 };
701
BPF_CALL_2(bpf_per_cpu_ptr,const void *,ptr,u32,cpu)702 BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu)
703 {
704 if (cpu >= nr_cpu_ids)
705 return (unsigned long)NULL;
706
707 return (unsigned long)per_cpu_ptr((const void __percpu *)ptr, cpu);
708 }
709
710 const struct bpf_func_proto bpf_per_cpu_ptr_proto = {
711 .func = bpf_per_cpu_ptr,
712 .gpl_only = false,
713 .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | PTR_MAYBE_NULL | MEM_RDONLY,
714 .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
715 .arg2_type = ARG_ANYTHING,
716 };
717
BPF_CALL_1(bpf_this_cpu_ptr,const void *,percpu_ptr)718 BPF_CALL_1(bpf_this_cpu_ptr, const void *, percpu_ptr)
719 {
720 return (unsigned long)this_cpu_ptr((const void __percpu *)percpu_ptr);
721 }
722
723 const struct bpf_func_proto bpf_this_cpu_ptr_proto = {
724 .func = bpf_this_cpu_ptr,
725 .gpl_only = false,
726 .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | MEM_RDONLY,
727 .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
728 };
729
bpf_trace_copy_string(char * buf,void * unsafe_ptr,char fmt_ptype,size_t bufsz)730 static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
731 size_t bufsz)
732 {
733 void __user *user_ptr = (__force void __user *)unsafe_ptr;
734
735 buf[0] = 0;
736
737 switch (fmt_ptype) {
738 case 's':
739 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
740 if ((unsigned long)unsafe_ptr < TASK_SIZE)
741 return strncpy_from_user_nofault(buf, user_ptr, bufsz);
742 fallthrough;
743 #endif
744 case 'k':
745 return strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz);
746 case 'u':
747 return strncpy_from_user_nofault(buf, user_ptr, bufsz);
748 }
749
750 return -EINVAL;
751 }
752
753 /* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary
754 * arguments representation.
755 */
756 #define MAX_BPRINTF_BIN_ARGS 512
757
758 /* Support executing three nested bprintf helper calls on a given CPU */
759 #define MAX_BPRINTF_NEST_LEVEL 3
760 struct bpf_bprintf_buffers {
761 char bin_args[MAX_BPRINTF_BIN_ARGS];
762 char buf[MAX_BPRINTF_BUF];
763 };
764
765 static DEFINE_PER_CPU(struct bpf_bprintf_buffers[MAX_BPRINTF_NEST_LEVEL], bpf_bprintf_bufs);
766 static DEFINE_PER_CPU(int, bpf_bprintf_nest_level);
767
try_get_buffers(struct bpf_bprintf_buffers ** bufs)768 static int try_get_buffers(struct bpf_bprintf_buffers **bufs)
769 {
770 int nest_level;
771
772 preempt_disable();
773 nest_level = this_cpu_inc_return(bpf_bprintf_nest_level);
774 if (WARN_ON_ONCE(nest_level > MAX_BPRINTF_NEST_LEVEL)) {
775 this_cpu_dec(bpf_bprintf_nest_level);
776 preempt_enable();
777 return -EBUSY;
778 }
779 *bufs = this_cpu_ptr(&bpf_bprintf_bufs[nest_level - 1]);
780
781 return 0;
782 }
783
bpf_bprintf_cleanup(struct bpf_bprintf_data * data)784 void bpf_bprintf_cleanup(struct bpf_bprintf_data *data)
785 {
786 if (!data->bin_args && !data->buf)
787 return;
788 if (WARN_ON_ONCE(this_cpu_read(bpf_bprintf_nest_level) == 0))
789 return;
790 this_cpu_dec(bpf_bprintf_nest_level);
791 preempt_enable();
792 }
793
794 /*
795 * bpf_bprintf_prepare - Generic pass on format strings for bprintf-like helpers
796 *
797 * Returns a negative value if fmt is an invalid format string or 0 otherwise.
798 *
799 * This can be used in two ways:
800 * - Format string verification only: when data->get_bin_args is false
801 * - Arguments preparation: in addition to the above verification, it writes in
802 * data->bin_args a binary representation of arguments usable by bstr_printf
803 * where pointers from BPF have been sanitized.
804 *
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.
807 */
bpf_bprintf_prepare(char * fmt,u32 fmt_size,const u64 * raw_args,u32 num_args,struct bpf_bprintf_data * data)808 int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
809 u32 num_args, struct bpf_bprintf_data *data)
810 {
811 bool get_buffers = (data->get_bin_args && num_args) || data->get_buf;
812 char *unsafe_ptr = NULL, *tmp_buf = NULL, *tmp_buf_end, *fmt_end;
813 struct bpf_bprintf_buffers *buffers = NULL;
814 size_t sizeof_cur_arg, sizeof_cur_ip;
815 int err, i, num_spec = 0;
816 u64 cur_arg;
817 char fmt_ptype, cur_ip[16], ip_spec[] = "%pXX";
818
819 fmt_end = strnchr(fmt, fmt_size, 0);
820 if (!fmt_end)
821 return -EINVAL;
822 fmt_size = fmt_end - fmt;
823
824 if (get_buffers && try_get_buffers(&buffers))
825 return -EBUSY;
826
827 if (data->get_bin_args) {
828 if (num_args)
829 tmp_buf = buffers->bin_args;
830 tmp_buf_end = tmp_buf + MAX_BPRINTF_BIN_ARGS;
831 data->bin_args = (u32 *)tmp_buf;
832 }
833
834 if (data->get_buf)
835 data->buf = buffers->buf;
836
837 for (i = 0; i < fmt_size; i++) {
838 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
839 err = -EINVAL;
840 goto out;
841 }
842
843 if (fmt[i] != '%')
844 continue;
845
846 if (fmt[i + 1] == '%') {
847 i++;
848 continue;
849 }
850
851 if (num_spec >= num_args) {
852 err = -EINVAL;
853 goto out;
854 }
855
856 /* The string is zero-terminated so if fmt[i] != 0, we can
857 * always access fmt[i + 1], in the worst case it will be a 0
858 */
859 i++;
860
861 /* skip optional "[0 +-][num]" width formatting field */
862 while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' ||
863 fmt[i] == ' ')
864 i++;
865 if (fmt[i] >= '1' && fmt[i] <= '9') {
866 i++;
867 while (fmt[i] >= '0' && fmt[i] <= '9')
868 i++;
869 }
870
871 if (fmt[i] == 'p') {
872 sizeof_cur_arg = sizeof(long);
873
874 if ((fmt[i + 1] == 'k' || fmt[i + 1] == 'u') &&
875 fmt[i + 2] == 's') {
876 fmt_ptype = fmt[i + 1];
877 i += 2;
878 goto fmt_str;
879 }
880
881 if (fmt[i + 1] == 0 || isspace(fmt[i + 1]) ||
882 ispunct(fmt[i + 1]) || fmt[i + 1] == 'K' ||
883 fmt[i + 1] == 'x' || fmt[i + 1] == 's' ||
884 fmt[i + 1] == 'S') {
885 /* just kernel pointers */
886 if (tmp_buf)
887 cur_arg = raw_args[num_spec];
888 i++;
889 goto nocopy_fmt;
890 }
891
892 if (fmt[i + 1] == 'B') {
893 if (tmp_buf) {
894 err = snprintf(tmp_buf,
895 (tmp_buf_end - tmp_buf),
896 "%pB",
897 (void *)(long)raw_args[num_spec]);
898 tmp_buf += (err + 1);
899 }
900
901 i++;
902 num_spec++;
903 continue;
904 }
905
906 /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */
907 if ((fmt[i + 1] != 'i' && fmt[i + 1] != 'I') ||
908 (fmt[i + 2] != '4' && fmt[i + 2] != '6')) {
909 err = -EINVAL;
910 goto out;
911 }
912
913 i += 2;
914 if (!tmp_buf)
915 goto nocopy_fmt;
916
917 sizeof_cur_ip = (fmt[i] == '4') ? 4 : 16;
918 if (tmp_buf_end - tmp_buf < sizeof_cur_ip) {
919 err = -ENOSPC;
920 goto out;
921 }
922
923 unsafe_ptr = (char *)(long)raw_args[num_spec];
924 err = copy_from_kernel_nofault(cur_ip, unsafe_ptr,
925 sizeof_cur_ip);
926 if (err < 0)
927 memset(cur_ip, 0, sizeof_cur_ip);
928
929 /* hack: bstr_printf expects IP addresses to be
930 * pre-formatted as strings, ironically, the easiest way
931 * to do that is to call snprintf.
932 */
933 ip_spec[2] = fmt[i - 1];
934 ip_spec[3] = fmt[i];
935 err = snprintf(tmp_buf, tmp_buf_end - tmp_buf,
936 ip_spec, &cur_ip);
937
938 tmp_buf += err + 1;
939 num_spec++;
940
941 continue;
942 } else if (fmt[i] == 's') {
943 fmt_ptype = fmt[i];
944 fmt_str:
945 if (fmt[i + 1] != 0 &&
946 !isspace(fmt[i + 1]) &&
947 !ispunct(fmt[i + 1])) {
948 err = -EINVAL;
949 goto out;
950 }
951
952 if (!tmp_buf)
953 goto nocopy_fmt;
954
955 if (tmp_buf_end == tmp_buf) {
956 err = -ENOSPC;
957 goto out;
958 }
959
960 unsafe_ptr = (char *)(long)raw_args[num_spec];
961 err = bpf_trace_copy_string(tmp_buf, unsafe_ptr,
962 fmt_ptype,
963 tmp_buf_end - tmp_buf);
964 if (err < 0) {
965 tmp_buf[0] = '\0';
966 err = 1;
967 }
968
969 tmp_buf += err;
970 num_spec++;
971
972 continue;
973 } else if (fmt[i] == 'c') {
974 if (!tmp_buf)
975 goto nocopy_fmt;
976
977 if (tmp_buf_end == tmp_buf) {
978 err = -ENOSPC;
979 goto out;
980 }
981
982 *tmp_buf = raw_args[num_spec];
983 tmp_buf++;
984 num_spec++;
985
986 continue;
987 }
988
989 sizeof_cur_arg = sizeof(int);
990
991 if (fmt[i] == 'l') {
992 sizeof_cur_arg = sizeof(long);
993 i++;
994 }
995 if (fmt[i] == 'l') {
996 sizeof_cur_arg = sizeof(long long);
997 i++;
998 }
999
1000 if (fmt[i] != 'i' && fmt[i] != 'd' && fmt[i] != 'u' &&
1001 fmt[i] != 'x' && fmt[i] != 'X') {
1002 err = -EINVAL;
1003 goto out;
1004 }
1005
1006 if (tmp_buf)
1007 cur_arg = raw_args[num_spec];
1008 nocopy_fmt:
1009 if (tmp_buf) {
1010 tmp_buf = PTR_ALIGN(tmp_buf, sizeof(u32));
1011 if (tmp_buf_end - tmp_buf < sizeof_cur_arg) {
1012 err = -ENOSPC;
1013 goto out;
1014 }
1015
1016 if (sizeof_cur_arg == 8) {
1017 *(u32 *)tmp_buf = *(u32 *)&cur_arg;
1018 *(u32 *)(tmp_buf + 4) = *((u32 *)&cur_arg + 1);
1019 } else {
1020 *(u32 *)tmp_buf = (u32)(long)cur_arg;
1021 }
1022 tmp_buf += sizeof_cur_arg;
1023 }
1024 num_spec++;
1025 }
1026
1027 err = 0;
1028 out:
1029 if (err)
1030 bpf_bprintf_cleanup(data);
1031 return err;
1032 }
1033
BPF_CALL_5(bpf_snprintf,char *,str,u32,str_size,char *,fmt,const void *,args,u32,data_len)1034 BPF_CALL_5(bpf_snprintf, char *, str, u32, str_size, char *, fmt,
1035 const void *, args, u32, data_len)
1036 {
1037 struct bpf_bprintf_data data = {
1038 .get_bin_args = true,
1039 };
1040 int err, num_args;
1041
1042 if (data_len % 8 || data_len > MAX_BPRINTF_VARARGS * 8 ||
1043 (data_len && !args))
1044 return -EINVAL;
1045 num_args = data_len / 8;
1046
1047 /* ARG_PTR_TO_CONST_STR guarantees that fmt is zero-terminated so we
1048 * can safely give an unbounded size.
1049 */
1050 err = bpf_bprintf_prepare(fmt, UINT_MAX, args, num_args, &data);
1051 if (err < 0)
1052 return err;
1053
1054 err = bstr_printf(str, str_size, fmt, data.bin_args);
1055
1056 bpf_bprintf_cleanup(&data);
1057
1058 return err + 1;
1059 }
1060
1061 const struct bpf_func_proto bpf_snprintf_proto = {
1062 .func = bpf_snprintf,
1063 .gpl_only = true,
1064 .ret_type = RET_INTEGER,
1065 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1066 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1067 .arg3_type = ARG_PTR_TO_CONST_STR,
1068 .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
1069 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1070 };
1071
1072 /* BPF map elements can contain 'struct bpf_timer'.
1073 * Such map owns all of its BPF timers.
1074 * 'struct bpf_timer' is allocated as part of map element allocation
1075 * and it's zero initialized.
1076 * That space is used to keep 'struct bpf_timer_kern'.
1077 * bpf_timer_init() allocates 'struct bpf_hrtimer', inits hrtimer, and
1078 * remembers 'struct bpf_map *' pointer it's part of.
1079 * bpf_timer_set_callback() increments prog refcnt and assign bpf callback_fn.
1080 * bpf_timer_start() arms the timer.
1081 * If user space reference to a map goes to zero at this point
1082 * ops->map_release_uref callback is responsible for cancelling the timers,
1083 * freeing their memory, and decrementing prog's refcnts.
1084 * bpf_timer_cancel() cancels the timer and decrements prog's refcnt.
1085 * Inner maps can contain bpf timers as well. ops->map_release_uref is
1086 * freeing the timers when inner map is replaced or deleted by user space.
1087 */
1088 struct bpf_hrtimer {
1089 struct hrtimer timer;
1090 struct bpf_map *map;
1091 struct bpf_prog *prog;
1092 void __rcu *callback_fn;
1093 void *value;
1094 struct rcu_head rcu;
1095 };
1096
1097 /* the actual struct hidden inside uapi struct bpf_timer */
1098 struct bpf_timer_kern {
1099 struct bpf_hrtimer *timer;
1100 /* bpf_spin_lock is used here instead of spinlock_t to make
1101 * sure that it always fits into space reserved by struct bpf_timer
1102 * regardless of LOCKDEP and spinlock debug flags.
1103 */
1104 struct bpf_spin_lock lock;
1105 } __attribute__((aligned(8)));
1106
1107 static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running);
1108
bpf_timer_cb(struct hrtimer * hrtimer)1109 static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
1110 {
1111 struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer);
1112 struct bpf_map *map = t->map;
1113 void *value = t->value;
1114 bpf_callback_t callback_fn;
1115 void *key;
1116 u32 idx;
1117
1118 BTF_TYPE_EMIT(struct bpf_timer);
1119 callback_fn = rcu_dereference_check(t->callback_fn, rcu_read_lock_bh_held());
1120 if (!callback_fn)
1121 goto out;
1122
1123 /* bpf_timer_cb() runs in hrtimer_run_softirq. It doesn't migrate and
1124 * cannot be preempted by another bpf_timer_cb() on the same cpu.
1125 * Remember the timer this callback is servicing to prevent
1126 * deadlock if callback_fn() calls bpf_timer_cancel() or
1127 * bpf_map_delete_elem() on the same timer.
1128 */
1129 this_cpu_write(hrtimer_running, t);
1130 if (map->map_type == BPF_MAP_TYPE_ARRAY) {
1131 struct bpf_array *array = container_of(map, struct bpf_array, map);
1132
1133 /* compute the key */
1134 idx = ((char *)value - array->value) / array->elem_size;
1135 key = &idx;
1136 } else { /* hash or lru */
1137 key = value - round_up(map->key_size, 8);
1138 }
1139
1140 callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
1141 /* The verifier checked that return value is zero. */
1142
1143 this_cpu_write(hrtimer_running, NULL);
1144 out:
1145 return HRTIMER_NORESTART;
1146 }
1147
BPF_CALL_3(bpf_timer_init,struct bpf_timer_kern *,timer,struct bpf_map *,map,u64,flags)1148 BPF_CALL_3(bpf_timer_init, struct bpf_timer_kern *, timer, struct bpf_map *, map,
1149 u64, flags)
1150 {
1151 clockid_t clockid = flags & (MAX_CLOCKS - 1);
1152 struct bpf_hrtimer *t;
1153 int ret = 0;
1154
1155 BUILD_BUG_ON(MAX_CLOCKS != 16);
1156 BUILD_BUG_ON(sizeof(struct bpf_timer_kern) > sizeof(struct bpf_timer));
1157 BUILD_BUG_ON(__alignof__(struct bpf_timer_kern) != __alignof__(struct bpf_timer));
1158
1159 if (in_nmi())
1160 return -EOPNOTSUPP;
1161
1162 if (flags >= MAX_CLOCKS ||
1163 /* similar to timerfd except _ALARM variants are not supported */
1164 (clockid != CLOCK_MONOTONIC &&
1165 clockid != CLOCK_REALTIME &&
1166 clockid != CLOCK_BOOTTIME))
1167 return -EINVAL;
1168 __bpf_spin_lock_irqsave(&timer->lock);
1169 t = timer->timer;
1170 if (t) {
1171 ret = -EBUSY;
1172 goto out;
1173 }
1174 /* allocate hrtimer via map_kmalloc to use memcg accounting */
1175 t = bpf_map_kmalloc_node(map, sizeof(*t), GFP_ATOMIC, map->numa_node);
1176 if (!t) {
1177 ret = -ENOMEM;
1178 goto out;
1179 }
1180 t->value = (void *)timer - map->timer_off;
1181 t->map = map;
1182 t->prog = NULL;
1183 rcu_assign_pointer(t->callback_fn, NULL);
1184 hrtimer_init(&t->timer, clockid, HRTIMER_MODE_REL_SOFT);
1185 t->timer.function = bpf_timer_cb;
1186 WRITE_ONCE(timer->timer, t);
1187 /* Guarantee the order between timer->timer and map->usercnt. So
1188 * when there are concurrent uref release and bpf timer init, either
1189 * bpf_timer_cancel_and_free() called by uref release reads a no-NULL
1190 * timer or atomic64_read() below returns a zero usercnt.
1191 */
1192 smp_mb();
1193 if (!atomic64_read(&map->usercnt)) {
1194 /* maps with timers must be either held by user space
1195 * or pinned in bpffs.
1196 */
1197 WRITE_ONCE(timer->timer, NULL);
1198 kfree(t);
1199 ret = -EPERM;
1200 }
1201 out:
1202 __bpf_spin_unlock_irqrestore(&timer->lock);
1203 return ret;
1204 }
1205
1206 static const struct bpf_func_proto bpf_timer_init_proto = {
1207 .func = bpf_timer_init,
1208 .gpl_only = true,
1209 .ret_type = RET_INTEGER,
1210 .arg1_type = ARG_PTR_TO_TIMER,
1211 .arg2_type = ARG_CONST_MAP_PTR,
1212 .arg3_type = ARG_ANYTHING,
1213 };
1214
BPF_CALL_3(bpf_timer_set_callback,struct bpf_timer_kern *,timer,void *,callback_fn,struct bpf_prog_aux *,aux)1215 BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callback_fn,
1216 struct bpf_prog_aux *, aux)
1217 {
1218 struct bpf_prog *prev, *prog = aux->prog;
1219 struct bpf_hrtimer *t;
1220 int ret = 0;
1221
1222 if (in_nmi())
1223 return -EOPNOTSUPP;
1224 __bpf_spin_lock_irqsave(&timer->lock);
1225 t = timer->timer;
1226 if (!t) {
1227 ret = -EINVAL;
1228 goto out;
1229 }
1230 if (!atomic64_read(&t->map->usercnt)) {
1231 /* maps with timers must be either held by user space
1232 * or pinned in bpffs. Otherwise timer might still be
1233 * running even when bpf prog is detached and user space
1234 * is gone, since map_release_uref won't ever be called.
1235 */
1236 ret = -EPERM;
1237 goto out;
1238 }
1239 prev = t->prog;
1240 if (prev != prog) {
1241 /* Bump prog refcnt once. Every bpf_timer_set_callback()
1242 * can pick different callback_fn-s within the same prog.
1243 */
1244 prog = bpf_prog_inc_not_zero(prog);
1245 if (IS_ERR(prog)) {
1246 ret = PTR_ERR(prog);
1247 goto out;
1248 }
1249 if (prev)
1250 /* Drop prev prog refcnt when swapping with new prog */
1251 bpf_prog_put(prev);
1252 t->prog = prog;
1253 }
1254 rcu_assign_pointer(t->callback_fn, callback_fn);
1255 out:
1256 __bpf_spin_unlock_irqrestore(&timer->lock);
1257 return ret;
1258 }
1259
1260 static const struct bpf_func_proto bpf_timer_set_callback_proto = {
1261 .func = bpf_timer_set_callback,
1262 .gpl_only = true,
1263 .ret_type = RET_INTEGER,
1264 .arg1_type = ARG_PTR_TO_TIMER,
1265 .arg2_type = ARG_PTR_TO_FUNC,
1266 };
1267
BPF_CALL_3(bpf_timer_start,struct bpf_timer_kern *,timer,u64,nsecs,u64,flags)1268 BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, flags)
1269 {
1270 struct bpf_hrtimer *t;
1271 int ret = 0;
1272
1273 if (in_nmi())
1274 return -EOPNOTSUPP;
1275 if (flags)
1276 return -EINVAL;
1277 __bpf_spin_lock_irqsave(&timer->lock);
1278 t = timer->timer;
1279 if (!t || !t->prog) {
1280 ret = -EINVAL;
1281 goto out;
1282 }
1283 hrtimer_start(&t->timer, ns_to_ktime(nsecs), HRTIMER_MODE_REL_SOFT);
1284 out:
1285 __bpf_spin_unlock_irqrestore(&timer->lock);
1286 return ret;
1287 }
1288
1289 static const struct bpf_func_proto bpf_timer_start_proto = {
1290 .func = bpf_timer_start,
1291 .gpl_only = true,
1292 .ret_type = RET_INTEGER,
1293 .arg1_type = ARG_PTR_TO_TIMER,
1294 .arg2_type = ARG_ANYTHING,
1295 .arg3_type = ARG_ANYTHING,
1296 };
1297
drop_prog_refcnt(struct bpf_hrtimer * t)1298 static void drop_prog_refcnt(struct bpf_hrtimer *t)
1299 {
1300 struct bpf_prog *prog = t->prog;
1301
1302 if (prog) {
1303 bpf_prog_put(prog);
1304 t->prog = NULL;
1305 rcu_assign_pointer(t->callback_fn, NULL);
1306 }
1307 }
1308
BPF_CALL_1(bpf_timer_cancel,struct bpf_timer_kern *,timer)1309 BPF_CALL_1(bpf_timer_cancel, struct bpf_timer_kern *, timer)
1310 {
1311 struct bpf_hrtimer *t;
1312 int ret = 0;
1313
1314 if (in_nmi())
1315 return -EOPNOTSUPP;
1316 rcu_read_lock();
1317 __bpf_spin_lock_irqsave(&timer->lock);
1318 t = timer->timer;
1319 if (!t) {
1320 ret = -EINVAL;
1321 goto out;
1322 }
1323 if (this_cpu_read(hrtimer_running) == t) {
1324 /* If bpf callback_fn is trying to bpf_timer_cancel()
1325 * its own timer the hrtimer_cancel() will deadlock
1326 * since it waits for callback_fn to finish
1327 */
1328 ret = -EDEADLK;
1329 goto out;
1330 }
1331 drop_prog_refcnt(t);
1332 out:
1333 __bpf_spin_unlock_irqrestore(&timer->lock);
1334 /* Cancel the timer and wait for associated callback to finish
1335 * if it was running.
1336 */
1337 ret = ret ?: hrtimer_cancel(&t->timer);
1338 rcu_read_unlock();
1339 return ret;
1340 }
1341
1342 static const struct bpf_func_proto bpf_timer_cancel_proto = {
1343 .func = bpf_timer_cancel,
1344 .gpl_only = true,
1345 .ret_type = RET_INTEGER,
1346 .arg1_type = ARG_PTR_TO_TIMER,
1347 };
1348
1349 /* This function is called by map_delete/update_elem for individual element and
1350 * by ops->map_release_uref when the user space reference to a map reaches zero.
1351 */
bpf_timer_cancel_and_free(void * val)1352 void bpf_timer_cancel_and_free(void *val)
1353 {
1354 struct bpf_timer_kern *timer = val;
1355 struct bpf_hrtimer *t;
1356
1357 /* Performance optimization: read timer->timer without lock first. */
1358 if (!READ_ONCE(timer->timer))
1359 return;
1360
1361 __bpf_spin_lock_irqsave(&timer->lock);
1362 /* re-read it under lock */
1363 t = timer->timer;
1364 if (!t)
1365 goto out;
1366 drop_prog_refcnt(t);
1367 /* The subsequent bpf_timer_start/cancel() helpers won't be able to use
1368 * this timer, since it won't be initialized.
1369 */
1370 WRITE_ONCE(timer->timer, NULL);
1371 out:
1372 __bpf_spin_unlock_irqrestore(&timer->lock);
1373 if (!t)
1374 return;
1375 /* Cancel the timer and wait for callback to complete if it was running.
1376 * If hrtimer_cancel() can be safely called it's safe to call kfree(t)
1377 * right after for both preallocated and non-preallocated maps.
1378 * The timer->timer = NULL was already done and no code path can
1379 * see address 't' anymore.
1380 *
1381 * Check that bpf_map_delete/update_elem() wasn't called from timer
1382 * callback_fn. In such case don't call hrtimer_cancel() (since it will
1383 * deadlock) and don't call hrtimer_try_to_cancel() (since it will just
1384 * return -1). Though callback_fn is still running on this cpu it's
1385 * safe to do kfree(t) because bpf_timer_cb() read everything it needed
1386 * from 't'. The bpf subprog callback_fn won't be able to access 't',
1387 * since timer->timer = NULL was already done. The timer will be
1388 * effectively cancelled because bpf_timer_cb() will return
1389 * HRTIMER_NORESTART.
1390 */
1391 if (this_cpu_read(hrtimer_running) != t)
1392 hrtimer_cancel(&t->timer);
1393 kfree_rcu(t, rcu);
1394 }
1395
BPF_CALL_2(bpf_kptr_xchg,void *,map_value,void *,ptr)1396 BPF_CALL_2(bpf_kptr_xchg, void *, map_value, void *, ptr)
1397 {
1398 unsigned long *kptr = map_value;
1399
1400 return xchg(kptr, (unsigned long)ptr);
1401 }
1402
1403 /* Unlike other PTR_TO_BTF_ID helpers the btf_id in bpf_kptr_xchg()
1404 * helper is determined dynamically by the verifier. Use BPF_PTR_POISON to
1405 * denote type that verifier will determine.
1406 */
1407 static const struct bpf_func_proto bpf_kptr_xchg_proto = {
1408 .func = bpf_kptr_xchg,
1409 .gpl_only = false,
1410 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
1411 .ret_btf_id = BPF_PTR_POISON,
1412 .arg1_type = ARG_PTR_TO_KPTR,
1413 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL | OBJ_RELEASE,
1414 .arg2_btf_id = BPF_PTR_POISON,
1415 };
1416
1417 /* Since the upper 8 bits of dynptr->size is reserved, the
1418 * maximum supported size is 2^24 - 1.
1419 */
1420 #define DYNPTR_MAX_SIZE ((1UL << 24) - 1)
1421 #define DYNPTR_TYPE_SHIFT 28
1422 #define DYNPTR_SIZE_MASK 0xFFFFFF
1423 #define DYNPTR_RDONLY_BIT BIT(31)
1424
bpf_dynptr_is_rdonly(struct bpf_dynptr_kern * ptr)1425 static bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
1426 {
1427 return ptr->size & DYNPTR_RDONLY_BIT;
1428 }
1429
bpf_dynptr_set_type(struct bpf_dynptr_kern * ptr,enum bpf_dynptr_type type)1430 static void bpf_dynptr_set_type(struct bpf_dynptr_kern *ptr, enum bpf_dynptr_type type)
1431 {
1432 ptr->size |= type << DYNPTR_TYPE_SHIFT;
1433 }
1434
bpf_dynptr_get_size(struct bpf_dynptr_kern * ptr)1435 u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr)
1436 {
1437 return ptr->size & DYNPTR_SIZE_MASK;
1438 }
1439
bpf_dynptr_check_size(u32 size)1440 int bpf_dynptr_check_size(u32 size)
1441 {
1442 return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
1443 }
1444
bpf_dynptr_init(struct bpf_dynptr_kern * ptr,void * data,enum bpf_dynptr_type type,u32 offset,u32 size)1445 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
1446 enum bpf_dynptr_type type, u32 offset, u32 size)
1447 {
1448 ptr->data = data;
1449 ptr->offset = offset;
1450 ptr->size = size;
1451 bpf_dynptr_set_type(ptr, type);
1452 }
1453
bpf_dynptr_set_null(struct bpf_dynptr_kern * ptr)1454 void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)
1455 {
1456 memset(ptr, 0, sizeof(*ptr));
1457 }
1458
bpf_dynptr_check_off_len(struct bpf_dynptr_kern * ptr,u32 offset,u32 len)1459 static int bpf_dynptr_check_off_len(struct bpf_dynptr_kern *ptr, u32 offset, u32 len)
1460 {
1461 u32 size = bpf_dynptr_get_size(ptr);
1462
1463 if (len > size || offset > size - len)
1464 return -E2BIG;
1465
1466 return 0;
1467 }
1468
BPF_CALL_4(bpf_dynptr_from_mem,void *,data,u32,size,u64,flags,struct bpf_dynptr_kern *,ptr)1469 BPF_CALL_4(bpf_dynptr_from_mem, void *, data, u32, size, u64, flags, struct bpf_dynptr_kern *, ptr)
1470 {
1471 int err;
1472
1473 BTF_TYPE_EMIT(struct bpf_dynptr);
1474
1475 err = bpf_dynptr_check_size(size);
1476 if (err)
1477 goto error;
1478
1479 /* flags is currently unsupported */
1480 if (flags) {
1481 err = -EINVAL;
1482 goto error;
1483 }
1484
1485 bpf_dynptr_init(ptr, data, BPF_DYNPTR_TYPE_LOCAL, 0, size);
1486
1487 return 0;
1488
1489 error:
1490 bpf_dynptr_set_null(ptr);
1491 return err;
1492 }
1493
1494 static const struct bpf_func_proto bpf_dynptr_from_mem_proto = {
1495 .func = bpf_dynptr_from_mem,
1496 .gpl_only = false,
1497 .ret_type = RET_INTEGER,
1498 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
1499 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1500 .arg3_type = ARG_ANYTHING,
1501 .arg4_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL | MEM_UNINIT,
1502 };
1503
BPF_CALL_5(bpf_dynptr_read,void *,dst,u32,len,struct bpf_dynptr_kern *,src,u32,offset,u64,flags)1504 BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src,
1505 u32, offset, u64, flags)
1506 {
1507 int err;
1508
1509 if (!src->data || flags)
1510 return -EINVAL;
1511
1512 err = bpf_dynptr_check_off_len(src, offset, len);
1513 if (err)
1514 return err;
1515
1516 memcpy(dst, src->data + src->offset + offset, len);
1517
1518 return 0;
1519 }
1520
1521 static const struct bpf_func_proto bpf_dynptr_read_proto = {
1522 .func = bpf_dynptr_read,
1523 .gpl_only = false,
1524 .ret_type = RET_INTEGER,
1525 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
1526 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1527 .arg3_type = ARG_PTR_TO_DYNPTR,
1528 .arg4_type = ARG_ANYTHING,
1529 .arg5_type = ARG_ANYTHING,
1530 };
1531
BPF_CALL_5(bpf_dynptr_write,struct bpf_dynptr_kern *,dst,u32,offset,void *,src,u32,len,u64,flags)1532 BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *, src,
1533 u32, len, u64, flags)
1534 {
1535 int err;
1536
1537 if (!dst->data || flags || bpf_dynptr_is_rdonly(dst))
1538 return -EINVAL;
1539
1540 err = bpf_dynptr_check_off_len(dst, offset, len);
1541 if (err)
1542 return err;
1543
1544 memcpy(dst->data + dst->offset + offset, src, len);
1545
1546 return 0;
1547 }
1548
1549 static const struct bpf_func_proto bpf_dynptr_write_proto = {
1550 .func = bpf_dynptr_write,
1551 .gpl_only = false,
1552 .ret_type = RET_INTEGER,
1553 .arg1_type = ARG_PTR_TO_DYNPTR,
1554 .arg2_type = ARG_ANYTHING,
1555 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1556 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1557 .arg5_type = ARG_ANYTHING,
1558 };
1559
BPF_CALL_3(bpf_dynptr_data,struct bpf_dynptr_kern *,ptr,u32,offset,u32,len)1560 BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len)
1561 {
1562 int err;
1563
1564 if (!ptr->data)
1565 return 0;
1566
1567 err = bpf_dynptr_check_off_len(ptr, offset, len);
1568 if (err)
1569 return 0;
1570
1571 if (bpf_dynptr_is_rdonly(ptr))
1572 return 0;
1573
1574 return (unsigned long)(ptr->data + ptr->offset + offset);
1575 }
1576
1577 static const struct bpf_func_proto bpf_dynptr_data_proto = {
1578 .func = bpf_dynptr_data,
1579 .gpl_only = false,
1580 .ret_type = RET_PTR_TO_DYNPTR_MEM_OR_NULL,
1581 .arg1_type = ARG_PTR_TO_DYNPTR,
1582 .arg2_type = ARG_ANYTHING,
1583 .arg3_type = ARG_CONST_ALLOC_SIZE_OR_ZERO,
1584 };
1585
1586 const struct bpf_func_proto bpf_get_current_task_proto __weak;
1587 const struct bpf_func_proto bpf_get_current_task_btf_proto __weak;
1588 const struct bpf_func_proto bpf_probe_read_user_proto __weak;
1589 const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
1590 const struct bpf_func_proto bpf_probe_read_kernel_proto __weak;
1591 const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
1592 const struct bpf_func_proto bpf_task_pt_regs_proto __weak;
1593
1594 const struct bpf_func_proto *
bpf_base_func_proto(enum bpf_func_id func_id)1595 bpf_base_func_proto(enum bpf_func_id func_id)
1596 {
1597 switch (func_id) {
1598 case BPF_FUNC_map_lookup_elem:
1599 return &bpf_map_lookup_elem_proto;
1600 case BPF_FUNC_map_update_elem:
1601 return &bpf_map_update_elem_proto;
1602 case BPF_FUNC_map_delete_elem:
1603 return &bpf_map_delete_elem_proto;
1604 case BPF_FUNC_map_push_elem:
1605 return &bpf_map_push_elem_proto;
1606 case BPF_FUNC_map_pop_elem:
1607 return &bpf_map_pop_elem_proto;
1608 case BPF_FUNC_map_peek_elem:
1609 return &bpf_map_peek_elem_proto;
1610 case BPF_FUNC_map_lookup_percpu_elem:
1611 return &bpf_map_lookup_percpu_elem_proto;
1612 case BPF_FUNC_get_prandom_u32:
1613 return &bpf_get_prandom_u32_proto;
1614 case BPF_FUNC_get_smp_processor_id:
1615 return &bpf_get_raw_smp_processor_id_proto;
1616 case BPF_FUNC_get_numa_node_id:
1617 return &bpf_get_numa_node_id_proto;
1618 case BPF_FUNC_tail_call:
1619 return &bpf_tail_call_proto;
1620 case BPF_FUNC_ktime_get_ns:
1621 return &bpf_ktime_get_ns_proto;
1622 case BPF_FUNC_ktime_get_boot_ns:
1623 return &bpf_ktime_get_boot_ns_proto;
1624 case BPF_FUNC_ktime_get_tai_ns:
1625 return &bpf_ktime_get_tai_ns_proto;
1626 case BPF_FUNC_ringbuf_output:
1627 return &bpf_ringbuf_output_proto;
1628 case BPF_FUNC_ringbuf_reserve:
1629 return &bpf_ringbuf_reserve_proto;
1630 case BPF_FUNC_ringbuf_submit:
1631 return &bpf_ringbuf_submit_proto;
1632 case BPF_FUNC_ringbuf_discard:
1633 return &bpf_ringbuf_discard_proto;
1634 case BPF_FUNC_ringbuf_query:
1635 return &bpf_ringbuf_query_proto;
1636 case BPF_FUNC_strncmp:
1637 return &bpf_strncmp_proto;
1638 case BPF_FUNC_strtol:
1639 return &bpf_strtol_proto;
1640 case BPF_FUNC_strtoul:
1641 return &bpf_strtoul_proto;
1642 default:
1643 break;
1644 }
1645
1646 if (!bpf_capable())
1647 return NULL;
1648
1649 switch (func_id) {
1650 case BPF_FUNC_spin_lock:
1651 return &bpf_spin_lock_proto;
1652 case BPF_FUNC_spin_unlock:
1653 return &bpf_spin_unlock_proto;
1654 case BPF_FUNC_jiffies64:
1655 return &bpf_jiffies64_proto;
1656 case BPF_FUNC_per_cpu_ptr:
1657 return &bpf_per_cpu_ptr_proto;
1658 case BPF_FUNC_this_cpu_ptr:
1659 return &bpf_this_cpu_ptr_proto;
1660 case BPF_FUNC_timer_init:
1661 return &bpf_timer_init_proto;
1662 case BPF_FUNC_timer_set_callback:
1663 return &bpf_timer_set_callback_proto;
1664 case BPF_FUNC_timer_start:
1665 return &bpf_timer_start_proto;
1666 case BPF_FUNC_timer_cancel:
1667 return &bpf_timer_cancel_proto;
1668 case BPF_FUNC_kptr_xchg:
1669 return &bpf_kptr_xchg_proto;
1670 case BPF_FUNC_for_each_map_elem:
1671 return &bpf_for_each_map_elem_proto;
1672 case BPF_FUNC_loop:
1673 return &bpf_loop_proto;
1674 case BPF_FUNC_user_ringbuf_drain:
1675 return &bpf_user_ringbuf_drain_proto;
1676 case BPF_FUNC_ringbuf_reserve_dynptr:
1677 return &bpf_ringbuf_reserve_dynptr_proto;
1678 case BPF_FUNC_ringbuf_submit_dynptr:
1679 return &bpf_ringbuf_submit_dynptr_proto;
1680 case BPF_FUNC_ringbuf_discard_dynptr:
1681 return &bpf_ringbuf_discard_dynptr_proto;
1682 case BPF_FUNC_dynptr_from_mem:
1683 return &bpf_dynptr_from_mem_proto;
1684 case BPF_FUNC_dynptr_read:
1685 return &bpf_dynptr_read_proto;
1686 case BPF_FUNC_dynptr_write:
1687 return &bpf_dynptr_write_proto;
1688 case BPF_FUNC_dynptr_data:
1689 return &bpf_dynptr_data_proto;
1690 default:
1691 break;
1692 }
1693
1694 if (!perfmon_capable())
1695 return NULL;
1696
1697 switch (func_id) {
1698 case BPF_FUNC_trace_printk:
1699 return bpf_get_trace_printk_proto();
1700 case BPF_FUNC_get_current_task:
1701 return &bpf_get_current_task_proto;
1702 case BPF_FUNC_get_current_task_btf:
1703 return &bpf_get_current_task_btf_proto;
1704 case BPF_FUNC_probe_read_user:
1705 return &bpf_probe_read_user_proto;
1706 case BPF_FUNC_probe_read_kernel:
1707 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1708 NULL : &bpf_probe_read_kernel_proto;
1709 case BPF_FUNC_probe_read_user_str:
1710 return &bpf_probe_read_user_str_proto;
1711 case BPF_FUNC_probe_read_kernel_str:
1712 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1713 NULL : &bpf_probe_read_kernel_str_proto;
1714 case BPF_FUNC_snprintf_btf:
1715 return &bpf_snprintf_btf_proto;
1716 case BPF_FUNC_snprintf:
1717 return &bpf_snprintf_proto;
1718 case BPF_FUNC_task_pt_regs:
1719 return &bpf_task_pt_regs_proto;
1720 case BPF_FUNC_trace_vprintk:
1721 return bpf_get_trace_vprintk_proto();
1722 default:
1723 return NULL;
1724 }
1725 }
1726
1727 BTF_SET8_START(tracing_btf_ids)
1728 #ifdef CONFIG_KEXEC_CORE
1729 BTF_ID_FLAGS(func, crash_kexec, KF_DESTRUCTIVE)
1730 #endif
1731 BTF_SET8_END(tracing_btf_ids)
1732
1733 static const struct btf_kfunc_id_set tracing_kfunc_set = {
1734 .owner = THIS_MODULE,
1735 .set = &tracing_btf_ids,
1736 };
1737
kfunc_init(void)1738 static int __init kfunc_init(void)
1739 {
1740 return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &tracing_kfunc_set);
1741 }
1742
1743 late_initcall(kfunc_init);
1744