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_not_zero(struct task_struct *p) __ksym; 27 void bpf_task_release(struct task_struct *p) __ksym; 28 29 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") 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") 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") 80 int no_lock(void *ctx) 81 { 82 struct task_struct *task, *real_parent; 83 84 /* no bpf_rcu_read_lock(), old code still works */ 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") 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") 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") 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") 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_not_zero(gparent); 163 if (!gparent) 164 /* Until we resolve the issues with using task->rcu_users, we 165 * expect bpf_task_acquire_not_zero() to return a NULL task. 166 * See the comment at the definition of 167 * bpf_task_acquire_not_zero() for more details. 168 */ 169 goto out; 170 171 (void)bpf_task_storage_get(&map_a, gparent, 0, 0); 172 bpf_task_release(gparent); 173 out: 174 bpf_rcu_read_unlock(); 175 return 0; 176 } 177 178 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") 179 int miss_lock(void *ctx) 180 { 181 struct task_struct *task; 182 struct css_set *cgroups; 183 struct cgroup *dfl_cgrp; 184 185 /* missing bpf_rcu_read_lock() */ 186 task = bpf_get_current_task_btf(); 187 bpf_rcu_read_lock(); 188 (void)bpf_task_storage_get(&map_a, task, 0, 0); 189 bpf_rcu_read_unlock(); 190 bpf_rcu_read_unlock(); 191 return 0; 192 } 193 194 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") 195 int miss_unlock(void *ctx) 196 { 197 struct task_struct *task; 198 struct css_set *cgroups; 199 struct cgroup *dfl_cgrp; 200 201 /* missing bpf_rcu_read_unlock() */ 202 task = bpf_get_current_task_btf(); 203 bpf_rcu_read_lock(); 204 (void)bpf_task_storage_get(&map_a, task, 0, 0); 205 return 0; 206 } 207 208 SEC("?fentry/" SYS_PREFIX "sys_getpgid") 209 int non_sleepable_rcu_mismatch(void *ctx) 210 { 211 struct task_struct *task, *real_parent; 212 213 task = bpf_get_current_task_btf(); 214 /* non-sleepable: missing bpf_rcu_read_unlock() in one path */ 215 bpf_rcu_read_lock(); 216 real_parent = task->real_parent; 217 if (!real_parent) 218 goto out; 219 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0); 220 if (real_parent) 221 bpf_rcu_read_unlock(); 222 out: 223 return 0; 224 } 225 226 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") 227 int inproper_sleepable_helper(void *ctx) 228 { 229 struct task_struct *task, *real_parent; 230 struct pt_regs *regs; 231 __u32 value = 0; 232 void *ptr; 233 234 task = bpf_get_current_task_btf(); 235 /* sleepable helper in rcu read lock region */ 236 bpf_rcu_read_lock(); 237 real_parent = task->real_parent; 238 if (!real_parent) 239 goto out; 240 regs = (struct pt_regs *)bpf_task_pt_regs(real_parent); 241 if (!regs) 242 goto out; 243 244 ptr = (void *)PT_REGS_IP(regs); 245 (void)bpf_copy_from_user_task(&value, sizeof(uint32_t), ptr, task, 0); 246 user_data = value; 247 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0); 248 out: 249 bpf_rcu_read_unlock(); 250 return 0; 251 } 252 253 SEC("?lsm.s/bpf") 254 int BPF_PROG(inproper_sleepable_kfunc, int cmd, union bpf_attr *attr, unsigned int size) 255 { 256 struct bpf_key *bkey; 257 258 /* sleepable kfunc in rcu read lock region */ 259 bpf_rcu_read_lock(); 260 bkey = bpf_lookup_user_key(key_serial, flags); 261 bpf_rcu_read_unlock(); 262 if (!bkey) 263 return -1; 264 bpf_key_put(bkey); 265 266 return 0; 267 } 268 269 SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep") 270 int nested_rcu_region(void *ctx) 271 { 272 struct task_struct *task, *real_parent; 273 274 /* nested rcu read lock regions */ 275 task = bpf_get_current_task_btf(); 276 bpf_rcu_read_lock(); 277 bpf_rcu_read_lock(); 278 real_parent = task->real_parent; 279 if (!real_parent) 280 goto out; 281 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0); 282 out: 283 bpf_rcu_read_unlock(); 284 bpf_rcu_read_unlock(); 285 return 0; 286 } 287 288 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") 289 int task_untrusted_non_rcuptr(void *ctx) 290 { 291 struct task_struct *task, *group_leader; 292 293 task = bpf_get_current_task_btf(); 294 bpf_rcu_read_lock(); 295 /* the pointer group_leader marked as untrusted */ 296 group_leader = task->real_parent->group_leader; 297 (void)bpf_task_storage_get(&map_a, group_leader, 0, 0); 298 bpf_rcu_read_unlock(); 299 return 0; 300 } 301 302 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") 303 int task_untrusted_rcuptr(void *ctx) 304 { 305 struct task_struct *task, *real_parent; 306 307 task = bpf_get_current_task_btf(); 308 bpf_rcu_read_lock(); 309 real_parent = task->real_parent; 310 bpf_rcu_read_unlock(); 311 /* helper use of rcu ptr outside the rcu read lock region */ 312 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0); 313 return 0; 314 } 315 316 SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep") 317 int cross_rcu_region(void *ctx) 318 { 319 struct task_struct *task, *real_parent; 320 321 /* rcu ptr define/use in different regions */ 322 task = bpf_get_current_task_btf(); 323 bpf_rcu_read_lock(); 324 real_parent = task->real_parent; 325 bpf_rcu_read_unlock(); 326 bpf_rcu_read_lock(); 327 (void)bpf_task_storage_get(&map_a, real_parent, 0, 0); 328 bpf_rcu_read_unlock(); 329 return 0; 330 } 331