xref: /openbmc/linux/kernel/bpf/task_iter.c (revision 52cdded0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Facebook */
3 
4 #include <linux/init.h>
5 #include <linux/namei.h>
6 #include <linux/pid_namespace.h>
7 #include <linux/fs.h>
8 #include <linux/fdtable.h>
9 #include <linux/filter.h>
10 #include <linux/btf_ids.h>
11 
12 struct bpf_iter_seq_task_common {
13 	struct pid_namespace *ns;
14 };
15 
16 struct bpf_iter_seq_task_info {
17 	/* The first field must be struct bpf_iter_seq_task_common.
18 	 * this is assumed by {init, fini}_seq_pidns() callback functions.
19 	 */
20 	struct bpf_iter_seq_task_common common;
21 	u32 tid;
22 };
23 
24 static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
25 					     u32 *tid)
26 {
27 	struct task_struct *task = NULL;
28 	struct pid *pid;
29 
30 	rcu_read_lock();
31 retry:
32 	pid = idr_get_next(&ns->idr, tid);
33 	if (pid) {
34 		task = get_pid_task(pid, PIDTYPE_PID);
35 		if (!task) {
36 			++*tid;
37 			goto retry;
38 		}
39 	}
40 	rcu_read_unlock();
41 
42 	return task;
43 }
44 
45 static void *task_seq_start(struct seq_file *seq, loff_t *pos)
46 {
47 	struct bpf_iter_seq_task_info *info = seq->private;
48 	struct task_struct *task;
49 
50 	task = task_seq_get_next(info->common.ns, &info->tid);
51 	if (!task)
52 		return NULL;
53 
54 	if (*pos == 0)
55 		++*pos;
56 	return task;
57 }
58 
59 static void *task_seq_next(struct seq_file *seq, void *v, loff_t *pos)
60 {
61 	struct bpf_iter_seq_task_info *info = seq->private;
62 	struct task_struct *task;
63 
64 	++*pos;
65 	++info->tid;
66 	put_task_struct((struct task_struct *)v);
67 	task = task_seq_get_next(info->common.ns, &info->tid);
68 	if (!task)
69 		return NULL;
70 
71 	return task;
72 }
73 
74 struct bpf_iter__task {
75 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
76 	__bpf_md_ptr(struct task_struct *, task);
77 };
78 
79 DEFINE_BPF_ITER_FUNC(task, struct bpf_iter_meta *meta, struct task_struct *task)
80 
81 static int __task_seq_show(struct seq_file *seq, struct task_struct *task,
82 			   bool in_stop)
83 {
84 	struct bpf_iter_meta meta;
85 	struct bpf_iter__task ctx;
86 	struct bpf_prog *prog;
87 
88 	meta.seq = seq;
89 	prog = bpf_iter_get_info(&meta, in_stop);
90 	if (!prog)
91 		return 0;
92 
93 	meta.seq = seq;
94 	ctx.meta = &meta;
95 	ctx.task = task;
96 	return bpf_iter_run_prog(prog, &ctx);
97 }
98 
99 static int task_seq_show(struct seq_file *seq, void *v)
100 {
101 	return __task_seq_show(seq, v, false);
102 }
103 
104 static void task_seq_stop(struct seq_file *seq, void *v)
105 {
106 	if (!v)
107 		(void)__task_seq_show(seq, v, true);
108 	else
109 		put_task_struct((struct task_struct *)v);
110 }
111 
112 static const struct seq_operations task_seq_ops = {
113 	.start	= task_seq_start,
114 	.next	= task_seq_next,
115 	.stop	= task_seq_stop,
116 	.show	= task_seq_show,
117 };
118 
119 struct bpf_iter_seq_task_file_info {
120 	/* The first field must be struct bpf_iter_seq_task_common.
121 	 * this is assumed by {init, fini}_seq_pidns() callback functions.
122 	 */
123 	struct bpf_iter_seq_task_common common;
124 	struct task_struct *task;
125 	struct files_struct *files;
126 	u32 tid;
127 	u32 fd;
128 };
129 
130 static struct file *
131 task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info,
132 		       struct task_struct **task, struct files_struct **fstruct)
133 {
134 	struct pid_namespace *ns = info->common.ns;
135 	u32 curr_tid = info->tid, max_fds;
136 	struct files_struct *curr_files;
137 	struct task_struct *curr_task;
138 	int curr_fd = info->fd;
139 
140 	/* If this function returns a non-NULL file object,
141 	 * it held a reference to the task/files_struct/file.
142 	 * Otherwise, it does not hold any reference.
143 	 */
144 again:
145 	if (*task) {
146 		curr_task = *task;
147 		curr_files = *fstruct;
148 		curr_fd = info->fd;
149 	} else {
150 		curr_task = task_seq_get_next(ns, &curr_tid);
151 		if (!curr_task)
152 			return NULL;
153 
154 		curr_files = get_files_struct(curr_task);
155 		if (!curr_files) {
156 			put_task_struct(curr_task);
157 			curr_tid = ++(info->tid);
158 			info->fd = 0;
159 			goto again;
160 		}
161 
162 		/* set *fstruct, *task and info->tid */
163 		*fstruct = curr_files;
164 		*task = curr_task;
165 		if (curr_tid == info->tid) {
166 			curr_fd = info->fd;
167 		} else {
168 			info->tid = curr_tid;
169 			curr_fd = 0;
170 		}
171 	}
172 
173 	rcu_read_lock();
174 	max_fds = files_fdtable(curr_files)->max_fds;
175 	for (; curr_fd < max_fds; curr_fd++) {
176 		struct file *f;
177 
178 		f = fcheck_files(curr_files, curr_fd);
179 		if (!f)
180 			continue;
181 
182 		/* set info->fd */
183 		info->fd = curr_fd;
184 		get_file(f);
185 		rcu_read_unlock();
186 		return f;
187 	}
188 
189 	/* the current task is done, go to the next task */
190 	rcu_read_unlock();
191 	put_files_struct(curr_files);
192 	put_task_struct(curr_task);
193 	*task = NULL;
194 	*fstruct = NULL;
195 	info->fd = 0;
196 	curr_tid = ++(info->tid);
197 	goto again;
198 }
199 
200 static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
201 {
202 	struct bpf_iter_seq_task_file_info *info = seq->private;
203 	struct files_struct *files = NULL;
204 	struct task_struct *task = NULL;
205 	struct file *file;
206 
207 	file = task_file_seq_get_next(info, &task, &files);
208 	if (!file) {
209 		info->files = NULL;
210 		info->task = NULL;
211 		return NULL;
212 	}
213 
214 	if (*pos == 0)
215 		++*pos;
216 	info->task = task;
217 	info->files = files;
218 
219 	return file;
220 }
221 
222 static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
223 {
224 	struct bpf_iter_seq_task_file_info *info = seq->private;
225 	struct files_struct *files = info->files;
226 	struct task_struct *task = info->task;
227 	struct file *file;
228 
229 	++*pos;
230 	++info->fd;
231 	fput((struct file *)v);
232 	file = task_file_seq_get_next(info, &task, &files);
233 	if (!file) {
234 		info->files = NULL;
235 		info->task = NULL;
236 		return NULL;
237 	}
238 
239 	info->task = task;
240 	info->files = files;
241 
242 	return file;
243 }
244 
245 struct bpf_iter__task_file {
246 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
247 	__bpf_md_ptr(struct task_struct *, task);
248 	u32 fd __aligned(8);
249 	__bpf_md_ptr(struct file *, file);
250 };
251 
252 DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
253 		     struct task_struct *task, u32 fd,
254 		     struct file *file)
255 
256 static int __task_file_seq_show(struct seq_file *seq, struct file *file,
257 				bool in_stop)
258 {
259 	struct bpf_iter_seq_task_file_info *info = seq->private;
260 	struct bpf_iter__task_file ctx;
261 	struct bpf_iter_meta meta;
262 	struct bpf_prog *prog;
263 
264 	meta.seq = seq;
265 	prog = bpf_iter_get_info(&meta, in_stop);
266 	if (!prog)
267 		return 0;
268 
269 	ctx.meta = &meta;
270 	ctx.task = info->task;
271 	ctx.fd = info->fd;
272 	ctx.file = file;
273 	return bpf_iter_run_prog(prog, &ctx);
274 }
275 
276 static int task_file_seq_show(struct seq_file *seq, void *v)
277 {
278 	return __task_file_seq_show(seq, v, false);
279 }
280 
281 static void task_file_seq_stop(struct seq_file *seq, void *v)
282 {
283 	struct bpf_iter_seq_task_file_info *info = seq->private;
284 
285 	if (!v) {
286 		(void)__task_file_seq_show(seq, v, true);
287 	} else {
288 		fput((struct file *)v);
289 		put_files_struct(info->files);
290 		put_task_struct(info->task);
291 		info->files = NULL;
292 		info->task = NULL;
293 	}
294 }
295 
296 static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
297 {
298 	struct bpf_iter_seq_task_common *common = priv_data;
299 
300 	common->ns = get_pid_ns(task_active_pid_ns(current));
301 	return 0;
302 }
303 
304 static void fini_seq_pidns(void *priv_data)
305 {
306 	struct bpf_iter_seq_task_common *common = priv_data;
307 
308 	put_pid_ns(common->ns);
309 }
310 
311 static const struct seq_operations task_file_seq_ops = {
312 	.start	= task_file_seq_start,
313 	.next	= task_file_seq_next,
314 	.stop	= task_file_seq_stop,
315 	.show	= task_file_seq_show,
316 };
317 
318 BTF_ID_LIST(btf_task_file_ids)
319 BTF_ID(struct, task_struct)
320 BTF_ID(struct, file)
321 
322 static const struct bpf_iter_seq_info task_seq_info = {
323 	.seq_ops		= &task_seq_ops,
324 	.init_seq_private	= init_seq_pidns,
325 	.fini_seq_private	= fini_seq_pidns,
326 	.seq_priv_size		= sizeof(struct bpf_iter_seq_task_info),
327 };
328 
329 static struct bpf_iter_reg task_reg_info = {
330 	.target			= "task",
331 	.ctx_arg_info_size	= 1,
332 	.ctx_arg_info		= {
333 		{ offsetof(struct bpf_iter__task, task),
334 		  PTR_TO_BTF_ID_OR_NULL },
335 	},
336 	.seq_info		= &task_seq_info,
337 };
338 
339 static const struct bpf_iter_seq_info task_file_seq_info = {
340 	.seq_ops		= &task_file_seq_ops,
341 	.init_seq_private	= init_seq_pidns,
342 	.fini_seq_private	= fini_seq_pidns,
343 	.seq_priv_size		= sizeof(struct bpf_iter_seq_task_file_info),
344 };
345 
346 static struct bpf_iter_reg task_file_reg_info = {
347 	.target			= "task_file",
348 	.ctx_arg_info_size	= 2,
349 	.ctx_arg_info		= {
350 		{ offsetof(struct bpf_iter__task_file, task),
351 		  PTR_TO_BTF_ID_OR_NULL },
352 		{ offsetof(struct bpf_iter__task_file, file),
353 		  PTR_TO_BTF_ID_OR_NULL },
354 	},
355 	.seq_info		= &task_file_seq_info,
356 };
357 
358 static int __init task_iter_init(void)
359 {
360 	int ret;
361 
362 	task_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
363 	ret = bpf_iter_reg_target(&task_reg_info);
364 	if (ret)
365 		return ret;
366 
367 	task_file_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
368 	task_file_reg_info.ctx_arg_info[1].btf_id = btf_task_file_ids[1];
369 	return bpf_iter_reg_target(&task_file_reg_info);
370 }
371 late_initcall(task_iter_init);
372