1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 #include "bpf_tracing_net.h"
10 char _license[] SEC("license") = "GPL";
13 __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
14 __uint(map_flags, BPF_F_NO_PREALLOC);
19 __u32 user_data, key_serial, target_pid;
20 __u64 flags, task_storage_val, cgroup_id;
22 struct bpf_key *bpf_lookup_user_key(__u32 serial, __u64 flags) __ksym;
23 void bpf_key_put(struct bpf_key *key) __ksym;
24 void bpf_rcu_read_lock(void) __ksym;
25 void bpf_rcu_read_unlock(void) __ksym;
26 struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
27 void bpf_task_release(struct task_struct *p) __ksym;
29 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
30 int get_cgroup_id(void *ctx)
32 struct task_struct *task;
33 struct css_set *cgroups;
35 task = bpf_get_current_task_btf();
36 if (task->pid != target_pid)
39 /* simulate bpf_get_current_cgroup_id() helper */
41 cgroups = task->cgroups;
44 cgroup_id = cgroups->dfl_cgrp->kn->id;
46 bpf_rcu_read_unlock();
50 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
51 int task_succ(void *ctx)
53 struct task_struct *task, *real_parent;
57 task = bpf_get_current_task_btf();
58 if (task->pid != target_pid)
62 /* region including helper using rcu ptr real_parent */
63 real_parent = task->real_parent;
66 ptr = bpf_task_storage_get(&map_a, real_parent, &init_val,
67 BPF_LOCAL_STORAGE_GET_F_CREATE);
70 ptr = bpf_task_storage_get(&map_a, real_parent, 0, 0);
73 task_storage_val = *ptr;
75 bpf_rcu_read_unlock();
79 SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
80 int no_lock(void *ctx)
82 struct task_struct *task, *real_parent;
84 /* old style ptr_to_btf_id is not allowed in sleepable */
85 task = bpf_get_current_task_btf();
86 real_parent = task->real_parent;
87 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
91 SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
92 int two_regions(void *ctx)
94 struct task_struct *task, *real_parent;
97 task = bpf_get_current_task_btf();
99 bpf_rcu_read_unlock();
101 real_parent = task->real_parent;
104 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
106 bpf_rcu_read_unlock();
110 SEC("?fentry/" SYS_PREFIX "sys_getpgid")
111 int non_sleepable_1(void *ctx)
113 struct task_struct *task, *real_parent;
115 task = bpf_get_current_task_btf();
117 real_parent = task->real_parent;
120 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
122 bpf_rcu_read_unlock();
126 SEC("?fentry/" SYS_PREFIX "sys_getpgid")
127 int non_sleepable_2(void *ctx)
129 struct task_struct *task, *real_parent;
132 task = bpf_get_current_task_btf();
133 bpf_rcu_read_unlock();
136 real_parent = task->real_parent;
139 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
141 bpf_rcu_read_unlock();
145 SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
146 int task_acquire(void *ctx)
148 struct task_struct *task, *real_parent, *gparent;
150 task = bpf_get_current_task_btf();
152 real_parent = task->real_parent;
156 /* rcu_ptr->rcu_field */
157 gparent = real_parent->real_parent;
161 /* acquire a reference which can be used outside rcu read lock region */
162 gparent = bpf_task_acquire(gparent);
166 (void)bpf_task_storage_get(&map_a, gparent, 0, 0);
167 bpf_task_release(gparent);
169 bpf_rcu_read_unlock();
173 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
174 int miss_lock(void *ctx)
176 struct task_struct *task;
178 /* missing bpf_rcu_read_lock() */
179 task = bpf_get_current_task_btf();
181 (void)bpf_task_storage_get(&map_a, task, 0, 0);
182 bpf_rcu_read_unlock();
183 bpf_rcu_read_unlock();
187 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
188 int miss_unlock(void *ctx)
190 struct task_struct *task;
192 /* missing bpf_rcu_read_unlock() */
193 task = bpf_get_current_task_btf();
195 (void)bpf_task_storage_get(&map_a, task, 0, 0);
199 SEC("?fentry/" SYS_PREFIX "sys_getpgid")
200 int non_sleepable_rcu_mismatch(void *ctx)
202 struct task_struct *task, *real_parent;
204 task = bpf_get_current_task_btf();
205 /* non-sleepable: missing bpf_rcu_read_unlock() in one path */
207 real_parent = task->real_parent;
210 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
212 bpf_rcu_read_unlock();
217 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
218 int inproper_sleepable_helper(void *ctx)
220 struct task_struct *task, *real_parent;
221 struct pt_regs *regs;
225 task = bpf_get_current_task_btf();
226 /* sleepable helper in rcu read lock region */
228 real_parent = task->real_parent;
231 regs = (struct pt_regs *)bpf_task_pt_regs(real_parent);
235 ptr = (void *)PT_REGS_IP(regs);
236 (void)bpf_copy_from_user_task(&value, sizeof(uint32_t), ptr, task, 0);
238 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
240 bpf_rcu_read_unlock();
245 int BPF_PROG(inproper_sleepable_kfunc, int cmd, union bpf_attr *attr, unsigned int size)
247 struct bpf_key *bkey;
249 /* sleepable kfunc in rcu read lock region */
251 bkey = bpf_lookup_user_key(key_serial, flags);
252 bpf_rcu_read_unlock();
260 SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
261 int nested_rcu_region(void *ctx)
263 struct task_struct *task, *real_parent;
265 /* nested rcu read lock regions */
266 task = bpf_get_current_task_btf();
269 real_parent = task->real_parent;
272 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
274 bpf_rcu_read_unlock();
275 bpf_rcu_read_unlock();
279 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
280 int task_trusted_non_rcuptr(void *ctx)
282 struct task_struct *task, *group_leader;
284 task = bpf_get_current_task_btf();
286 /* the pointer group_leader is explicitly marked as trusted */
287 group_leader = task->real_parent->group_leader;
288 (void)bpf_task_storage_get(&map_a, group_leader, 0, 0);
289 bpf_rcu_read_unlock();
293 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
294 int task_untrusted_rcuptr(void *ctx)
296 struct task_struct *task, *real_parent;
298 task = bpf_get_current_task_btf();
300 real_parent = task->real_parent;
301 bpf_rcu_read_unlock();
302 /* helper use of rcu ptr outside the rcu read lock region */
303 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
307 SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
308 int cross_rcu_region(void *ctx)
310 struct task_struct *task, *real_parent;
312 /* rcu ptr define/use in different regions */
313 task = bpf_get_current_task_btf();
315 real_parent = task->real_parent;
316 bpf_rcu_read_unlock();
318 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
319 bpf_rcu_read_unlock();