1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 #include "bpf_tracing_net.h"
8 #include "bpf_misc.h"
9
10 char _license[] SEC("license") = "GPL";
11
12 struct {
13 __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
14 __uint(map_flags, BPF_F_NO_PREALLOC);
15 __type(key, int);
16 __type(value, long);
17 } map_a SEC(".maps");
18
19 __u32 user_data, key_serial, target_pid;
20 __u64 flags, task_storage_val, cgroup_id;
21
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;
28
29 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
get_cgroup_id(void * ctx)30 int get_cgroup_id(void *ctx)
31 {
32 struct task_struct *task;
33 struct css_set *cgroups;
34
35 task = bpf_get_current_task_btf();
36 if (task->pid != target_pid)
37 return 0;
38
39 /* simulate bpf_get_current_cgroup_id() helper */
40 bpf_rcu_read_lock();
41 cgroups = task->cgroups;
42 if (!cgroups)
43 goto unlock;
44 cgroup_id = cgroups->dfl_cgrp->kn->id;
45 unlock:
46 bpf_rcu_read_unlock();
47 return 0;
48 }
49
50 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
task_succ(void * ctx)51 int task_succ(void *ctx)
52 {
53 struct task_struct *task, *real_parent;
54 long init_val = 2;
55 long *ptr;
56
57 task = bpf_get_current_task_btf();
58 if (task->pid != target_pid)
59 return 0;
60
61 bpf_rcu_read_lock();
62 /* region including helper using rcu ptr real_parent */
63 real_parent = task->real_parent;
64 if (!real_parent)
65 goto out;
66 ptr = bpf_task_storage_get(&map_a, real_parent, &init_val,
67 BPF_LOCAL_STORAGE_GET_F_CREATE);
68 if (!ptr)
69 goto out;
70 ptr = bpf_task_storage_get(&map_a, real_parent, 0, 0);
71 if (!ptr)
72 goto out;
73 task_storage_val = *ptr;
74 out:
75 bpf_rcu_read_unlock();
76 return 0;
77 }
78
79 SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
no_lock(void * ctx)80 int no_lock(void *ctx)
81 {
82 struct task_struct *task, *real_parent;
83
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);
88 return 0;
89 }
90
91 SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
two_regions(void * ctx)92 int two_regions(void *ctx)
93 {
94 struct task_struct *task, *real_parent;
95
96 /* two regions */
97 task = bpf_get_current_task_btf();
98 bpf_rcu_read_lock();
99 bpf_rcu_read_unlock();
100 bpf_rcu_read_lock();
101 real_parent = task->real_parent;
102 if (!real_parent)
103 goto out;
104 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
105 out:
106 bpf_rcu_read_unlock();
107 return 0;
108 }
109
110 SEC("?fentry/" SYS_PREFIX "sys_getpgid")
non_sleepable_1(void * ctx)111 int non_sleepable_1(void *ctx)
112 {
113 struct task_struct *task, *real_parent;
114
115 task = bpf_get_current_task_btf();
116 bpf_rcu_read_lock();
117 real_parent = task->real_parent;
118 if (!real_parent)
119 goto out;
120 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
121 out:
122 bpf_rcu_read_unlock();
123 return 0;
124 }
125
126 SEC("?fentry/" SYS_PREFIX "sys_getpgid")
non_sleepable_2(void * ctx)127 int non_sleepable_2(void *ctx)
128 {
129 struct task_struct *task, *real_parent;
130
131 bpf_rcu_read_lock();
132 task = bpf_get_current_task_btf();
133 bpf_rcu_read_unlock();
134
135 bpf_rcu_read_lock();
136 real_parent = task->real_parent;
137 if (!real_parent)
138 goto out;
139 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
140 out:
141 bpf_rcu_read_unlock();
142 return 0;
143 }
144
145 SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
task_acquire(void * ctx)146 int task_acquire(void *ctx)
147 {
148 struct task_struct *task, *real_parent, *gparent;
149
150 task = bpf_get_current_task_btf();
151 bpf_rcu_read_lock();
152 real_parent = task->real_parent;
153 if (!real_parent)
154 goto out;
155
156 /* rcu_ptr->rcu_field */
157 gparent = real_parent->real_parent;
158 if (!gparent)
159 goto out;
160
161 /* acquire a reference which can be used outside rcu read lock region */
162 gparent = bpf_task_acquire(gparent);
163 if (!gparent)
164 goto out;
165
166 (void)bpf_task_storage_get(&map_a, gparent, 0, 0);
167 bpf_task_release(gparent);
168 out:
169 bpf_rcu_read_unlock();
170 return 0;
171 }
172
173 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
miss_lock(void * ctx)174 int miss_lock(void *ctx)
175 {
176 struct task_struct *task;
177
178 /* missing bpf_rcu_read_lock() */
179 task = bpf_get_current_task_btf();
180 bpf_rcu_read_lock();
181 (void)bpf_task_storage_get(&map_a, task, 0, 0);
182 bpf_rcu_read_unlock();
183 bpf_rcu_read_unlock();
184 return 0;
185 }
186
187 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
miss_unlock(void * ctx)188 int miss_unlock(void *ctx)
189 {
190 struct task_struct *task;
191
192 /* missing bpf_rcu_read_unlock() */
193 task = bpf_get_current_task_btf();
194 bpf_rcu_read_lock();
195 (void)bpf_task_storage_get(&map_a, task, 0, 0);
196 return 0;
197 }
198
199 SEC("?fentry/" SYS_PREFIX "sys_getpgid")
non_sleepable_rcu_mismatch(void * ctx)200 int non_sleepable_rcu_mismatch(void *ctx)
201 {
202 struct task_struct *task, *real_parent;
203
204 task = bpf_get_current_task_btf();
205 /* non-sleepable: missing bpf_rcu_read_unlock() in one path */
206 bpf_rcu_read_lock();
207 real_parent = task->real_parent;
208 if (!real_parent)
209 goto out;
210 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
211 if (real_parent)
212 bpf_rcu_read_unlock();
213 out:
214 return 0;
215 }
216
217 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
inproper_sleepable_helper(void * ctx)218 int inproper_sleepable_helper(void *ctx)
219 {
220 struct task_struct *task, *real_parent;
221 struct pt_regs *regs;
222 __u32 value = 0;
223 void *ptr;
224
225 task = bpf_get_current_task_btf();
226 /* sleepable helper in rcu read lock region */
227 bpf_rcu_read_lock();
228 real_parent = task->real_parent;
229 if (!real_parent)
230 goto out;
231 regs = (struct pt_regs *)bpf_task_pt_regs(real_parent);
232 if (!regs)
233 goto out;
234
235 ptr = (void *)PT_REGS_IP(regs);
236 (void)bpf_copy_from_user_task(&value, sizeof(uint32_t), ptr, task, 0);
237 user_data = value;
238 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
239 out:
240 bpf_rcu_read_unlock();
241 return 0;
242 }
243
244 SEC("?lsm.s/bpf")
BPF_PROG(inproper_sleepable_kfunc,int cmd,union bpf_attr * attr,unsigned int size,bool kernel)245 int BPF_PROG(inproper_sleepable_kfunc, int cmd, union bpf_attr *attr, unsigned int size,
246 bool kernel)
247 {
248 struct bpf_key *bkey;
249
250 /* sleepable kfunc in rcu read lock region */
251 bpf_rcu_read_lock();
252 bkey = bpf_lookup_user_key(key_serial, flags);
253 bpf_rcu_read_unlock();
254 if (!bkey)
255 return -1;
256 bpf_key_put(bkey);
257
258 return 0;
259 }
260
261 SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
nested_rcu_region(void * ctx)262 int nested_rcu_region(void *ctx)
263 {
264 struct task_struct *task, *real_parent;
265
266 /* nested rcu read lock regions */
267 task = bpf_get_current_task_btf();
268 bpf_rcu_read_lock();
269 bpf_rcu_read_lock();
270 real_parent = task->real_parent;
271 if (!real_parent)
272 goto out;
273 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
274 out:
275 bpf_rcu_read_unlock();
276 bpf_rcu_read_unlock();
277 return 0;
278 }
279
280 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
task_trusted_non_rcuptr(void * ctx)281 int task_trusted_non_rcuptr(void *ctx)
282 {
283 struct task_struct *task, *group_leader;
284
285 task = bpf_get_current_task_btf();
286 bpf_rcu_read_lock();
287 /* the pointer group_leader is explicitly marked as trusted */
288 group_leader = task->real_parent->group_leader;
289 (void)bpf_task_storage_get(&map_a, group_leader, 0, 0);
290 bpf_rcu_read_unlock();
291 return 0;
292 }
293
294 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
task_untrusted_rcuptr(void * ctx)295 int task_untrusted_rcuptr(void *ctx)
296 {
297 struct task_struct *task, *real_parent;
298
299 task = bpf_get_current_task_btf();
300 bpf_rcu_read_lock();
301 real_parent = task->real_parent;
302 bpf_rcu_read_unlock();
303 /* helper use of rcu ptr outside the rcu read lock region */
304 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
305 return 0;
306 }
307
308 SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
cross_rcu_region(void * ctx)309 int cross_rcu_region(void *ctx)
310 {
311 struct task_struct *task, *real_parent;
312
313 /* rcu ptr define/use in different regions */
314 task = bpf_get_current_task_btf();
315 bpf_rcu_read_lock();
316 real_parent = task->real_parent;
317 bpf_rcu_read_unlock();
318 bpf_rcu_read_lock();
319 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
320 bpf_rcu_read_unlock();
321 return 0;
322 }
323
324 __noinline
static_subprog(void * ctx)325 static int static_subprog(void *ctx)
326 {
327 volatile int ret = 0;
328
329 if (bpf_get_prandom_u32())
330 return ret + 42;
331 return ret + bpf_get_prandom_u32();
332 }
333
334 __noinline
global_subprog(u64 a)335 int global_subprog(u64 a)
336 {
337 volatile int ret = a;
338
339 return ret + static_subprog(NULL);
340 }
341
342 __noinline
static_subprog_lock(void * ctx)343 static int static_subprog_lock(void *ctx)
344 {
345 volatile int ret = 0;
346
347 bpf_rcu_read_lock();
348 if (bpf_get_prandom_u32())
349 return ret + 42;
350 return ret + bpf_get_prandom_u32();
351 }
352
353 __noinline
global_subprog_lock(u64 a)354 int global_subprog_lock(u64 a)
355 {
356 volatile int ret = a;
357
358 return ret + static_subprog_lock(NULL);
359 }
360
361 __noinline
static_subprog_unlock(void * ctx)362 static int static_subprog_unlock(void *ctx)
363 {
364 volatile int ret = 0;
365
366 bpf_rcu_read_unlock();
367 if (bpf_get_prandom_u32())
368 return ret + 42;
369 return ret + bpf_get_prandom_u32();
370 }
371
372 __noinline
global_subprog_unlock(u64 a)373 int global_subprog_unlock(u64 a)
374 {
375 volatile int ret = a;
376
377 return ret + static_subprog_unlock(NULL);
378 }
379
380 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
rcu_read_lock_subprog(void * ctx)381 int rcu_read_lock_subprog(void *ctx)
382 {
383 volatile int ret = 0;
384
385 bpf_rcu_read_lock();
386 if (bpf_get_prandom_u32())
387 ret += static_subprog(ctx);
388 bpf_rcu_read_unlock();
389 return 0;
390 }
391
392 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
rcu_read_lock_global_subprog(void * ctx)393 int rcu_read_lock_global_subprog(void *ctx)
394 {
395 volatile int ret = 0;
396
397 bpf_rcu_read_lock();
398 if (bpf_get_prandom_u32())
399 ret += global_subprog(ret);
400 bpf_rcu_read_unlock();
401 return 0;
402 }
403
404 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
rcu_read_lock_subprog_lock(void * ctx)405 int rcu_read_lock_subprog_lock(void *ctx)
406 {
407 volatile int ret = 0;
408
409 ret += static_subprog_lock(ctx);
410 bpf_rcu_read_unlock();
411 return 0;
412 }
413
414 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
rcu_read_lock_global_subprog_lock(void * ctx)415 int rcu_read_lock_global_subprog_lock(void *ctx)
416 {
417 volatile int ret = 0;
418
419 ret += global_subprog_lock(ret);
420 bpf_rcu_read_unlock();
421 return 0;
422 }
423
424 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
rcu_read_lock_subprog_unlock(void * ctx)425 int rcu_read_lock_subprog_unlock(void *ctx)
426 {
427 volatile int ret = 0;
428
429 bpf_rcu_read_lock();
430 ret += static_subprog_unlock(ctx);
431 return 0;
432 }
433
434 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
rcu_read_lock_global_subprog_unlock(void * ctx)435 int rcu_read_lock_global_subprog_unlock(void *ctx)
436 {
437 volatile int ret = 0;
438
439 bpf_rcu_read_lock();
440 ret += global_subprog_unlock(ret);
441 return 0;
442 }
443
444 int __noinline
global_sleepable_helper_subprog(int i)445 global_sleepable_helper_subprog(int i)
446 {
447 if (i)
448 bpf_copy_from_user(&i, sizeof(i), NULL);
449 return i;
450 }
451
452 int __noinline
global_sleepable_kfunc_subprog(int i)453 global_sleepable_kfunc_subprog(int i)
454 {
455 if (i)
456 bpf_copy_from_user_str(&i, sizeof(i), NULL, 0);
457 global_subprog(i);
458 return i;
459 }
460
461 int __noinline
global_subprog_calling_sleepable_global(int i)462 global_subprog_calling_sleepable_global(int i)
463 {
464 if (!i)
465 global_sleepable_kfunc_subprog(i);
466 return i;
467 }
468
469 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
rcu_read_lock_sleepable_helper_global_subprog(void * ctx)470 int rcu_read_lock_sleepable_helper_global_subprog(void *ctx)
471 {
472 volatile int ret = 0;
473
474 bpf_rcu_read_lock();
475 ret += global_sleepable_helper_subprog(ret);
476 bpf_rcu_read_unlock();
477 return 0;
478 }
479
480 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
rcu_read_lock_sleepable_kfunc_global_subprog(void * ctx)481 int rcu_read_lock_sleepable_kfunc_global_subprog(void *ctx)
482 {
483 volatile int ret = 0;
484
485 bpf_rcu_read_lock();
486 ret += global_sleepable_kfunc_subprog(ret);
487 bpf_rcu_read_unlock();
488 return 0;
489 }
490
491 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
rcu_read_lock_sleepable_global_subprog_indirect(void * ctx)492 int rcu_read_lock_sleepable_global_subprog_indirect(void *ctx)
493 {
494 volatile int ret = 0;
495
496 bpf_rcu_read_lock();
497 ret += global_subprog_calling_sleepable_global(ret);
498 bpf_rcu_read_unlock();
499 return 0;
500 }
501