1 // SPDX-License-Identifier: GPL-2.0
3 #include <bpf/bpf_tracing.h>
4 #include <bpf/bpf_helpers.h>
5 #include "../bpf_testmod/bpf_testmod_kfunc.h"
8 struct prog_test_ref_kfunc __kptr_untrusted *unref_ptr;
9 struct prog_test_ref_kfunc __kptr *ref_ptr;
13 __uint(type, BPF_MAP_TYPE_ARRAY);
15 __type(value, struct map_value);
16 __uint(max_entries, 1);
17 } array_map SEC(".maps");
19 struct pcpu_array_map {
20 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
22 __type(value, struct map_value);
23 __uint(max_entries, 1);
24 } pcpu_array_map SEC(".maps");
27 __uint(type, BPF_MAP_TYPE_HASH);
29 __type(value, struct map_value);
30 __uint(max_entries, 1);
31 } hash_map SEC(".maps");
33 struct pcpu_hash_map {
34 __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
36 __type(value, struct map_value);
37 __uint(max_entries, 1);
38 } pcpu_hash_map SEC(".maps");
40 struct hash_malloc_map {
41 __uint(type, BPF_MAP_TYPE_HASH);
43 __type(value, struct map_value);
44 __uint(max_entries, 1);
45 __uint(map_flags, BPF_F_NO_PREALLOC);
46 } hash_malloc_map SEC(".maps");
48 struct pcpu_hash_malloc_map {
49 __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
51 __type(value, struct map_value);
52 __uint(max_entries, 1);
53 __uint(map_flags, BPF_F_NO_PREALLOC);
54 } pcpu_hash_malloc_map SEC(".maps");
57 __uint(type, BPF_MAP_TYPE_LRU_HASH);
59 __type(value, struct map_value);
60 __uint(max_entries, 1);
61 } lru_hash_map SEC(".maps");
63 struct lru_pcpu_hash_map {
64 __uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH);
66 __type(value, struct map_value);
67 __uint(max_entries, 1);
68 } lru_pcpu_hash_map SEC(".maps");
71 __uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
72 __uint(map_flags, BPF_F_NO_PREALLOC);
74 __type(value, struct map_value);
75 } cgrp_ls_map SEC(".maps");
78 __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
79 __uint(map_flags, BPF_F_NO_PREALLOC);
81 __type(value, struct map_value);
82 } task_ls_map SEC(".maps");
85 __uint(type, BPF_MAP_TYPE_INODE_STORAGE);
86 __uint(map_flags, BPF_F_NO_PREALLOC);
88 __type(value, struct map_value);
89 } inode_ls_map SEC(".maps");
92 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
93 __uint(map_flags, BPF_F_NO_PREALLOC);
95 __type(value, struct map_value);
96 } sk_ls_map SEC(".maps");
98 #define DEFINE_MAP_OF_MAP(map_type, inner_map_type, name) \
100 __uint(type, map_type); \
101 __uint(max_entries, 1); \
102 __uint(key_size, sizeof(int)); \
103 __uint(value_size, sizeof(int)); \
104 __array(values, struct inner_map_type); \
105 } name SEC(".maps") = { \
106 .values = { [0] = &inner_map_type }, \
109 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, array_map, array_of_array_maps);
110 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, hash_map, array_of_hash_maps);
111 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, hash_malloc_map, array_of_hash_malloc_maps);
112 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, lru_hash_map, array_of_lru_hash_maps);
113 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, array_map, hash_of_array_maps);
114 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, hash_map, hash_of_hash_maps);
115 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, hash_malloc_map, hash_of_hash_malloc_maps);
116 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, lru_hash_map, hash_of_lru_hash_maps);
118 #define WRITE_ONCE(x, val) ((*(volatile typeof(x) *) &(x)) = (val))
120 static void test_kptr_unref(struct map_value *v)
122 struct prog_test_ref_kfunc *p;
125 /* store untrusted_ptr_or_null_ */
126 WRITE_ONCE(v->unref_ptr, p);
129 if (p->a + p->b > 100)
131 /* store untrusted_ptr_ */
132 WRITE_ONCE(v->unref_ptr, p);
134 WRITE_ONCE(v->unref_ptr, NULL);
137 static void test_kptr_ref(struct map_value *v)
139 struct prog_test_ref_kfunc *p;
142 /* store ptr_or_null_ */
143 WRITE_ONCE(v->unref_ptr, p);
147 * p is rcu_ptr_prog_test_ref_kfunc,
148 * because bpf prog is non-sleepable and runs in RCU CS.
149 * p can be passed to kfunc that requires KF_RCU.
151 bpf_kfunc_call_test_ref(p);
152 if (p->a + p->b > 100)
155 p = bpf_kptr_xchg(&v->ref_ptr, NULL);
159 * p is trusted_ptr_prog_test_ref_kfunc.
160 * p can be passed to kfunc that requires KF_RCU.
162 bpf_kfunc_call_test_ref(p);
163 if (p->a + p->b > 100) {
164 bpf_kfunc_call_test_release(p);
168 WRITE_ONCE(v->unref_ptr, p);
169 bpf_kfunc_call_test_release(p);
171 p = bpf_kfunc_call_test_acquire(&(unsigned long){0});
175 p = bpf_kptr_xchg(&v->ref_ptr, p);
178 if (p->a + p->b > 100) {
179 bpf_kfunc_call_test_release(p);
182 bpf_kfunc_call_test_release(p);
185 static void test_kptr(struct map_value *v)
192 int test_map_kptr(struct __sk_buff *ctx)
198 v = bpf_map_lookup_elem(&map, &key); \
205 TEST(hash_malloc_map);
212 SEC("tp_btf/cgroup_mkdir")
213 int BPF_PROG(test_cgrp_map_kptr, struct cgroup *cgrp, const char *path)
217 v = bpf_cgrp_storage_get(&cgrp_ls_map, cgrp, NULL, BPF_LOCAL_STORAGE_GET_F_CREATE);
223 SEC("lsm/inode_unlink")
224 int BPF_PROG(test_task_map_kptr, struct inode *inode, struct dentry *victim)
226 struct task_struct *task;
229 task = bpf_get_current_task_btf();
232 v = bpf_task_storage_get(&task_ls_map, task, NULL, BPF_LOCAL_STORAGE_GET_F_CREATE);
238 SEC("lsm/inode_unlink")
239 int BPF_PROG(test_inode_map_kptr, struct inode *inode, struct dentry *victim)
243 v = bpf_inode_storage_get(&inode_ls_map, inode, NULL, BPF_LOCAL_STORAGE_GET_F_CREATE);
250 int test_sk_map_kptr(struct __sk_buff *ctx)
258 v = bpf_sk_storage_get(&sk_ls_map, sk, NULL, BPF_LOCAL_STORAGE_GET_F_CREATE);
265 int test_map_in_map_kptr(struct __sk_buff *ctx)
271 #define TEST(map_in_map) \
272 map = bpf_map_lookup_elem(&map_in_map, &key); \
275 v = bpf_map_lookup_elem(map, &key); \
280 TEST(array_of_array_maps);
281 TEST(array_of_hash_maps);
282 TEST(array_of_hash_malloc_maps);
283 TEST(array_of_lru_hash_maps);
284 TEST(hash_of_array_maps);
285 TEST(hash_of_hash_maps);
286 TEST(hash_of_hash_malloc_maps);
287 TEST(hash_of_lru_hash_maps);
295 static __always_inline
296 int test_map_kptr_ref_pre(struct map_value *v)
298 struct prog_test_ref_kfunc *p, *p_st;
299 unsigned long arg = 0;
302 p = bpf_kfunc_call_test_acquire(&arg);
308 if (p_st->cnt.refs.counter != ref) {
313 p = bpf_kptr_xchg(&v->ref_ptr, p);
318 if (p_st->cnt.refs.counter != ref)
321 p = bpf_kptr_xchg(&v->ref_ptr, NULL);
324 bpf_kfunc_call_test_release(p);
326 if (p_st->cnt.refs.counter != ref)
329 p = bpf_kfunc_call_test_acquire(&arg);
333 p = bpf_kptr_xchg(&v->ref_ptr, p);
338 if (p_st->cnt.refs.counter != ref)
345 bpf_kfunc_call_test_release(p);
349 static __always_inline
350 int test_map_kptr_ref_post(struct map_value *v)
352 struct prog_test_ref_kfunc *p, *p_st;
355 if (!p_st || p_st->cnt.refs.counter != ref)
358 p = bpf_kptr_xchg(&v->ref_ptr, NULL);
361 if (p_st->cnt.refs.counter != ref) {
362 bpf_kfunc_call_test_release(p);
366 p = bpf_kptr_xchg(&v->ref_ptr, p);
368 bpf_kfunc_call_test_release(p);
371 if (p_st->cnt.refs.counter != ref)
378 v = bpf_map_lookup_elem(&map, &key); \
381 ret = test_map_kptr_ref_pre(v); \
385 #define TEST_PCPU(map) \
386 v = bpf_map_lookup_percpu_elem(&map, &key, 0); \
389 ret = test_map_kptr_ref_pre(v); \
394 int test_map_kptr_ref1(struct __sk_buff *ctx)
396 struct map_value *v, val = {};
399 bpf_map_update_elem(&hash_map, &key, &val, 0);
400 bpf_map_update_elem(&hash_malloc_map, &key, &val, 0);
401 bpf_map_update_elem(&lru_hash_map, &key, &val, 0);
403 bpf_map_update_elem(&pcpu_hash_map, &key, &val, 0);
404 bpf_map_update_elem(&pcpu_hash_malloc_map, &key, &val, 0);
405 bpf_map_update_elem(&lru_pcpu_hash_map, &key, &val, 0);
409 TEST(hash_malloc_map);
412 TEST_PCPU(pcpu_array_map);
413 TEST_PCPU(pcpu_hash_map);
414 TEST_PCPU(pcpu_hash_malloc_map);
415 TEST_PCPU(lru_pcpu_hash_map);
424 v = bpf_map_lookup_elem(&map, &key); \
427 ret = test_map_kptr_ref_post(v); \
431 #define TEST_PCPU(map) \
432 v = bpf_map_lookup_percpu_elem(&map, &key, 0); \
435 ret = test_map_kptr_ref_post(v); \
440 int test_map_kptr_ref2(struct __sk_buff *ctx)
447 TEST(hash_malloc_map);
450 TEST_PCPU(pcpu_array_map);
451 TEST_PCPU(pcpu_hash_map);
452 TEST_PCPU(pcpu_hash_malloc_map);
453 TEST_PCPU(lru_pcpu_hash_map);
462 int test_map_kptr_ref3(struct __sk_buff *ctx)
464 struct prog_test_ref_kfunc *p;
465 unsigned long sp = 0;
467 p = bpf_kfunc_call_test_acquire(&sp);
471 if (p->cnt.refs.counter != ref) {
472 bpf_kfunc_call_test_release(p);
475 bpf_kfunc_call_test_release(p);
481 int test_ls_map_kptr_ref1(void *ctx)
483 struct task_struct *current;
486 current = bpf_get_current_task_btf();
489 v = bpf_task_storage_get(&task_ls_map, current, NULL, 0);
492 v = bpf_task_storage_get(&task_ls_map, current, NULL, BPF_LOCAL_STORAGE_GET_F_CREATE);
495 return test_map_kptr_ref_pre(v);
499 int test_ls_map_kptr_ref2(void *ctx)
501 struct task_struct *current;
504 current = bpf_get_current_task_btf();
507 v = bpf_task_storage_get(&task_ls_map, current, NULL, 0);
510 return test_map_kptr_ref_post(v);
514 int test_ls_map_kptr_ref_del(void *ctx)
516 struct task_struct *current;
519 current = bpf_get_current_task_btf();
522 v = bpf_task_storage_get(&task_ls_map, current, NULL, 0);
527 return bpf_task_storage_delete(&task_ls_map, current);
530 char _license[] SEC("license") = "GPL";