blob: 5859edfb5ad9fa2c7dbe33e0f4aad366589c8ae3 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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/rcupdate.h>
6#include <linux/random.h>
7#include <linux/smp.h>
8#include <linux/topology.h>
9#include <linux/ktime.h>
10#include <linux/sched.h>
11#include <linux/uidgid.h>
12#include <linux/filter.h>
13#include <linux/ctype.h>
14
15#include "../../lib/kstrtox.h"
16
17/* If kernel subsystem is allowing eBPF programs to call this function,
18 * inside its own verifier_ops->get_func_proto() callback it should return
19 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
20 *
21 * Different map implementations will rely on rcu in map methods
22 * lookup/update/delete, therefore eBPF programs must run under rcu lock
23 * if program is allowed to access maps, so check rcu_read_lock_held in
24 * all three functions.
25 */
26BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
27{
28 WARN_ON_ONCE(!rcu_read_lock_held());
29 return (unsigned long) map->ops->map_lookup_elem(map, key);
30}
31
32const struct bpf_func_proto bpf_map_lookup_elem_proto = {
33 .func = bpf_map_lookup_elem,
34 .gpl_only = false,
35 .pkt_access = true,
36 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
37 .arg1_type = ARG_CONST_MAP_PTR,
38 .arg2_type = ARG_PTR_TO_MAP_KEY,
39};
40
41BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
42 void *, value, u64, flags)
43{
44 WARN_ON_ONCE(!rcu_read_lock_held());
45 return map->ops->map_update_elem(map, key, value, flags);
46}
47
48const struct bpf_func_proto bpf_map_update_elem_proto = {
49 .func = bpf_map_update_elem,
50 .gpl_only = false,
51 .pkt_access = true,
52 .ret_type = RET_INTEGER,
53 .arg1_type = ARG_CONST_MAP_PTR,
54 .arg2_type = ARG_PTR_TO_MAP_KEY,
55 .arg3_type = ARG_PTR_TO_MAP_VALUE,
56 .arg4_type = ARG_ANYTHING,
57};
58
59BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
60{
61 WARN_ON_ONCE(!rcu_read_lock_held());
62 return map->ops->map_delete_elem(map, key);
63}
64
65const struct bpf_func_proto bpf_map_delete_elem_proto = {
66 .func = bpf_map_delete_elem,
67 .gpl_only = false,
68 .pkt_access = true,
69 .ret_type = RET_INTEGER,
70 .arg1_type = ARG_CONST_MAP_PTR,
71 .arg2_type = ARG_PTR_TO_MAP_KEY,
72};
73
74BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags)
75{
76 return map->ops->map_push_elem(map, value, flags);
77}
78
79const struct bpf_func_proto bpf_map_push_elem_proto = {
80 .func = bpf_map_push_elem,
81 .gpl_only = false,
82 .pkt_access = true,
83 .ret_type = RET_INTEGER,
84 .arg1_type = ARG_CONST_MAP_PTR,
85 .arg2_type = ARG_PTR_TO_MAP_VALUE,
86 .arg3_type = ARG_ANYTHING,
87};
88
89BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value)
90{
91 return map->ops->map_pop_elem(map, value);
92}
93
94const struct bpf_func_proto bpf_map_pop_elem_proto = {
95 .func = bpf_map_pop_elem,
96 .gpl_only = false,
97 .ret_type = RET_INTEGER,
98 .arg1_type = ARG_CONST_MAP_PTR,
99 .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
100};
101
102BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value)
103{
104 return map->ops->map_peek_elem(map, value);
105}
106
107const struct bpf_func_proto bpf_map_peek_elem_proto = {
108 .func = bpf_map_peek_elem,
109 .gpl_only = false,
110 .ret_type = RET_INTEGER,
111 .arg1_type = ARG_CONST_MAP_PTR,
112 .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
113};
114
115const struct bpf_func_proto bpf_get_prandom_u32_proto = {
116 .func = bpf_user_rnd_u32,
117 .gpl_only = false,
118 .ret_type = RET_INTEGER,
119};
120
121BPF_CALL_0(bpf_get_smp_processor_id)
122{
123 return smp_processor_id();
124}
125
126const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
127 .func = bpf_get_smp_processor_id,
128 .gpl_only = false,
129 .ret_type = RET_INTEGER,
130};
131
132BPF_CALL_0(bpf_get_numa_node_id)
133{
134 return numa_node_id();
135}
136
137const struct bpf_func_proto bpf_get_numa_node_id_proto = {
138 .func = bpf_get_numa_node_id,
139 .gpl_only = false,
140 .ret_type = RET_INTEGER,
141};
142
143BPF_CALL_0(bpf_ktime_get_ns)
144{
145 /* NMI safe access to clock monotonic */
146 return ktime_get_mono_fast_ns();
147}
148
149const struct bpf_func_proto bpf_ktime_get_ns_proto = {
150 .func = bpf_ktime_get_ns,
151 .gpl_only = false,
152 .ret_type = RET_INTEGER,
153};
154
155BPF_CALL_0(bpf_ktime_get_boot_ns)
156{
157 /* NMI safe access to clock boottime */
158 return ktime_get_boot_fast_ns();
159}
160
161const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = {
162 .func = bpf_ktime_get_boot_ns,
163 .gpl_only = false,
164 .ret_type = RET_INTEGER,
165};
166
167BPF_CALL_0(bpf_get_current_pid_tgid)
168{
169 struct task_struct *task = current;
170
171 if (unlikely(!task))
172 return -EINVAL;
173
174 return (u64) task->tgid << 32 | task->pid;
175}
176
177const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
178 .func = bpf_get_current_pid_tgid,
179 .gpl_only = false,
180 .ret_type = RET_INTEGER,
181};
182
183BPF_CALL_0(bpf_get_current_uid_gid)
184{
185 struct task_struct *task = current;
186 kuid_t uid;
187 kgid_t gid;
188
189 if (unlikely(!task))
190 return -EINVAL;
191
192 current_uid_gid(&uid, &gid);
193 return (u64) from_kgid(&init_user_ns, gid) << 32 |
194 from_kuid(&init_user_ns, uid);
195}
196
197const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
198 .func = bpf_get_current_uid_gid,
199 .gpl_only = false,
200 .ret_type = RET_INTEGER,
201};
202
203BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
204{
205 struct task_struct *task = current;
206
207 if (unlikely(!task))
208 goto err_clear;
209
210 strncpy(buf, task->comm, size);
211
212 /* Verifier guarantees that size > 0. For task->comm exceeding
213 * size, guarantee that buf is %NUL-terminated. Unconditionally
214 * done here to save the size test.
215 */
216 buf[size - 1] = 0;
217 return 0;
218err_clear:
219 memset(buf, 0, size);
220 return -EINVAL;
221}
222
223const struct bpf_func_proto bpf_get_current_comm_proto = {
224 .func = bpf_get_current_comm,
225 .gpl_only = false,
226 .ret_type = RET_INTEGER,
227 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
228 .arg2_type = ARG_CONST_SIZE,
229};
230
231#if defined(CONFIG_QUEUED_SPINLOCKS) || defined(CONFIG_BPF_ARCH_SPINLOCK)
232
233static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
234{
235 arch_spinlock_t *l = (void *)lock;
236 union {
237 __u32 val;
238 arch_spinlock_t lock;
239 } u = { .lock = __ARCH_SPIN_LOCK_UNLOCKED };
240
241 compiletime_assert(u.val == 0, "__ARCH_SPIN_LOCK_UNLOCKED not 0");
242 BUILD_BUG_ON(sizeof(*l) != sizeof(__u32));
243 BUILD_BUG_ON(sizeof(*lock) != sizeof(__u32));
244 arch_spin_lock(l);
245}
246
247static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
248{
249 arch_spinlock_t *l = (void *)lock;
250
251 arch_spin_unlock(l);
252}
253
254#else
255
256static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
257{
258 atomic_t *l = (void *)lock;
259
260 BUILD_BUG_ON(sizeof(*l) != sizeof(*lock));
261 do {
262 atomic_cond_read_relaxed(l, !VAL);
263 } while (atomic_xchg(l, 1));
264}
265
266static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
267{
268 atomic_t *l = (void *)lock;
269
270 atomic_set_release(l, 0);
271}
272
273#endif
274
275static DEFINE_PER_CPU(unsigned long, irqsave_flags);
276
277static inline void __bpf_spin_lock_irqsave(struct bpf_spin_lock *lock)
278{
279 unsigned long flags;
280
281 local_irq_save(flags);
282 __bpf_spin_lock(lock);
283 __this_cpu_write(irqsave_flags, flags);
284}
285
286NOTRACE_BPF_CALL_1(bpf_spin_lock, struct bpf_spin_lock *, lock)
287{
288 __bpf_spin_lock_irqsave(lock);
289 return 0;
290}
291
292const struct bpf_func_proto bpf_spin_lock_proto = {
293 .func = bpf_spin_lock,
294 .gpl_only = false,
295 .ret_type = RET_VOID,
296 .arg1_type = ARG_PTR_TO_SPIN_LOCK,
297};
298
299static inline void __bpf_spin_unlock_irqrestore(struct bpf_spin_lock *lock)
300{
301 unsigned long flags;
302
303 flags = __this_cpu_read(irqsave_flags);
304 __bpf_spin_unlock(lock);
305 local_irq_restore(flags);
306}
307
308NOTRACE_BPF_CALL_1(bpf_spin_unlock, struct bpf_spin_lock *, lock)
309{
310 __bpf_spin_unlock_irqrestore(lock);
311 return 0;
312}
313
314const struct bpf_func_proto bpf_spin_unlock_proto = {
315 .func = bpf_spin_unlock,
316 .gpl_only = false,
317 .ret_type = RET_VOID,
318 .arg1_type = ARG_PTR_TO_SPIN_LOCK,
319};
320
321void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
322 bool lock_src)
323{
324 struct bpf_spin_lock *lock;
325
326 if (lock_src)
327 lock = src + map->spin_lock_off;
328 else
329 lock = dst + map->spin_lock_off;
330 preempt_disable();
331 __bpf_spin_lock_irqsave(lock);
332 copy_map_value(map, dst, src);
333 __bpf_spin_unlock_irqrestore(lock);
334 preempt_enable();
335}
336
337#ifdef CONFIG_CGROUPS
338BPF_CALL_0(bpf_get_current_cgroup_id)
339{
340 struct cgroup *cgrp = task_dfl_cgroup(current);
341
342 return cgrp->kn->id.id;
343}
344
345const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
346 .func = bpf_get_current_cgroup_id,
347 .gpl_only = false,
348 .ret_type = RET_INTEGER,
349};
350
351#ifdef CONFIG_CGROUP_BPF
352DECLARE_PER_CPU(struct bpf_cgroup_storage*,
353 bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
354
355BPF_CALL_2(bpf_get_local_storage, struct bpf_map *, map, u64, flags)
356{
357 /* flags argument is not used now,
358 * but provides an ability to extend the API.
359 * verifier checks that its value is correct.
360 */
361 enum bpf_cgroup_storage_type stype = cgroup_storage_type(map);
362 struct bpf_cgroup_storage *storage;
363 void *ptr;
364
365 storage = this_cpu_read(bpf_cgroup_storage[stype]);
366
367 if (stype == BPF_CGROUP_STORAGE_SHARED)
368 ptr = &READ_ONCE(storage->buf)->data[0];
369 else
370 ptr = this_cpu_ptr(storage->percpu_buf);
371
372 return (unsigned long)ptr;
373}
374
375const struct bpf_func_proto bpf_get_local_storage_proto = {
376 .func = bpf_get_local_storage,
377 .gpl_only = false,
378 .ret_type = RET_PTR_TO_MAP_VALUE,
379 .arg1_type = ARG_CONST_MAP_PTR,
380 .arg2_type = ARG_ANYTHING,
381};
382#endif
383
384#define BPF_STRTOX_BASE_MASK 0x1F
385
386static int __bpf_strtoull(const char *buf, size_t buf_len, u64 flags,
387 unsigned long long *res, bool *is_negative)
388{
389 unsigned int base = flags & BPF_STRTOX_BASE_MASK;
390 const char *cur_buf = buf;
391 size_t cur_len = buf_len;
392 unsigned int consumed;
393 size_t val_len;
394 char str[64];
395
396 if (!buf || !buf_len || !res || !is_negative)
397 return -EINVAL;
398
399 if (base != 0 && base != 8 && base != 10 && base != 16)
400 return -EINVAL;
401
402 if (flags & ~BPF_STRTOX_BASE_MASK)
403 return -EINVAL;
404
405 while (cur_buf < buf + buf_len && isspace(*cur_buf))
406 ++cur_buf;
407
408 *is_negative = (cur_buf < buf + buf_len && *cur_buf == '-');
409 if (*is_negative)
410 ++cur_buf;
411
412 consumed = cur_buf - buf;
413 cur_len -= consumed;
414 if (!cur_len)
415 return -EINVAL;
416
417 cur_len = min(cur_len, sizeof(str) - 1);
418 memcpy(str, cur_buf, cur_len);
419 str[cur_len] = '\0';
420 cur_buf = str;
421
422 cur_buf = _parse_integer_fixup_radix(cur_buf, &base);
423 val_len = _parse_integer(cur_buf, base, res);
424
425 if (val_len & KSTRTOX_OVERFLOW)
426 return -ERANGE;
427
428 if (val_len == 0)
429 return -EINVAL;
430
431 cur_buf += val_len;
432 consumed += cur_buf - str;
433
434 return consumed;
435}
436
437static int __bpf_strtoll(const char *buf, size_t buf_len, u64 flags,
438 long long *res)
439{
440 unsigned long long _res;
441 bool is_negative;
442 int err;
443
444 err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
445 if (err < 0)
446 return err;
447 if (is_negative) {
448 if ((long long)-_res > 0)
449 return -ERANGE;
450 *res = -_res;
451 } else {
452 if ((long long)_res < 0)
453 return -ERANGE;
454 *res = _res;
455 }
456 return err;
457}
458
459BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags,
460 s64 *, res)
461{
462 long long _res;
463 int err;
464
465 err = __bpf_strtoll(buf, buf_len, flags, &_res);
466 if (err < 0)
467 return err;
468 if (_res != (long)_res)
469 return -ERANGE;
470 *res = _res;
471 return err;
472}
473
474const struct bpf_func_proto bpf_strtol_proto = {
475 .func = bpf_strtol,
476 .gpl_only = false,
477 .ret_type = RET_INTEGER,
478 .arg1_type = ARG_PTR_TO_MEM,
479 .arg2_type = ARG_CONST_SIZE,
480 .arg3_type = ARG_ANYTHING,
481 .arg4_type = ARG_PTR_TO_LONG,
482};
483
484BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,
485 u64 *, res)
486{
487 unsigned long long _res;
488 bool is_negative;
489 int err;
490
491 err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
492 if (err < 0)
493 return err;
494 if (is_negative)
495 return -EINVAL;
496 if (_res != (unsigned long)_res)
497 return -ERANGE;
498 *res = _res;
499 return err;
500}
501
502const struct bpf_func_proto bpf_strtoul_proto = {
503 .func = bpf_strtoul,
504 .gpl_only = false,
505 .ret_type = RET_INTEGER,
506 .arg1_type = ARG_PTR_TO_MEM,
507 .arg2_type = ARG_CONST_SIZE,
508 .arg3_type = ARG_ANYTHING,
509 .arg4_type = ARG_PTR_TO_LONG,
510};
511#endif