xref: /openbmc/linux/kernel/bpf/task_iter.c (revision cf28f3bb)
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 		if (!get_file_rcu(f))
182 			continue;
183 
184 		/* set info->fd */
185 		info->fd = curr_fd;
186 		rcu_read_unlock();
187 		return f;
188 	}
189 
190 	/* the current task is done, go to the next task */
191 	rcu_read_unlock();
192 	put_files_struct(curr_files);
193 	put_task_struct(curr_task);
194 	*task = NULL;
195 	*fstruct = NULL;
196 	info->fd = 0;
197 	curr_tid = ++(info->tid);
198 	goto again;
199 }
200 
201 static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
202 {
203 	struct bpf_iter_seq_task_file_info *info = seq->private;
204 	struct files_struct *files = NULL;
205 	struct task_struct *task = NULL;
206 	struct file *file;
207 
208 	file = task_file_seq_get_next(info, &task, &files);
209 	if (!file) {
210 		info->files = NULL;
211 		info->task = NULL;
212 		return NULL;
213 	}
214 
215 	if (*pos == 0)
216 		++*pos;
217 	info->task = task;
218 	info->files = files;
219 
220 	return file;
221 }
222 
223 static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
224 {
225 	struct bpf_iter_seq_task_file_info *info = seq->private;
226 	struct files_struct *files = info->files;
227 	struct task_struct *task = info->task;
228 	struct file *file;
229 
230 	++*pos;
231 	++info->fd;
232 	fput((struct file *)v);
233 	file = task_file_seq_get_next(info, &task, &files);
234 	if (!file) {
235 		info->files = NULL;
236 		info->task = NULL;
237 		return NULL;
238 	}
239 
240 	info->task = task;
241 	info->files = files;
242 
243 	return file;
244 }
245 
246 struct bpf_iter__task_file {
247 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
248 	__bpf_md_ptr(struct task_struct *, task);
249 	u32 fd __aligned(8);
250 	__bpf_md_ptr(struct file *, file);
251 };
252 
253 DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
254 		     struct task_struct *task, u32 fd,
255 		     struct file *file)
256 
257 static int __task_file_seq_show(struct seq_file *seq, struct file *file,
258 				bool in_stop)
259 {
260 	struct bpf_iter_seq_task_file_info *info = seq->private;
261 	struct bpf_iter__task_file ctx;
262 	struct bpf_iter_meta meta;
263 	struct bpf_prog *prog;
264 
265 	meta.seq = seq;
266 	prog = bpf_iter_get_info(&meta, in_stop);
267 	if (!prog)
268 		return 0;
269 
270 	ctx.meta = &meta;
271 	ctx.task = info->task;
272 	ctx.fd = info->fd;
273 	ctx.file = file;
274 	return bpf_iter_run_prog(prog, &ctx);
275 }
276 
277 static int task_file_seq_show(struct seq_file *seq, void *v)
278 {
279 	return __task_file_seq_show(seq, v, false);
280 }
281 
282 static void task_file_seq_stop(struct seq_file *seq, void *v)
283 {
284 	struct bpf_iter_seq_task_file_info *info = seq->private;
285 
286 	if (!v) {
287 		(void)__task_file_seq_show(seq, v, true);
288 	} else {
289 		fput((struct file *)v);
290 		put_files_struct(info->files);
291 		put_task_struct(info->task);
292 		info->files = NULL;
293 		info->task = NULL;
294 	}
295 }
296 
297 static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
298 {
299 	struct bpf_iter_seq_task_common *common = priv_data;
300 
301 	common->ns = get_pid_ns(task_active_pid_ns(current));
302 	return 0;
303 }
304 
305 static void fini_seq_pidns(void *priv_data)
306 {
307 	struct bpf_iter_seq_task_common *common = priv_data;
308 
309 	put_pid_ns(common->ns);
310 }
311 
312 static const struct seq_operations task_file_seq_ops = {
313 	.start	= task_file_seq_start,
314 	.next	= task_file_seq_next,
315 	.stop	= task_file_seq_stop,
316 	.show	= task_file_seq_show,
317 };
318 
319 BTF_ID_LIST(btf_task_file_ids)
320 BTF_ID(struct, task_struct)
321 BTF_ID(struct, file)
322 
323 static const struct bpf_iter_seq_info task_seq_info = {
324 	.seq_ops		= &task_seq_ops,
325 	.init_seq_private	= init_seq_pidns,
326 	.fini_seq_private	= fini_seq_pidns,
327 	.seq_priv_size		= sizeof(struct bpf_iter_seq_task_info),
328 };
329 
330 static struct bpf_iter_reg task_reg_info = {
331 	.target			= "task",
332 	.ctx_arg_info_size	= 1,
333 	.ctx_arg_info		= {
334 		{ offsetof(struct bpf_iter__task, task),
335 		  PTR_TO_BTF_ID_OR_NULL },
336 	},
337 	.seq_info		= &task_seq_info,
338 };
339 
340 static const struct bpf_iter_seq_info task_file_seq_info = {
341 	.seq_ops		= &task_file_seq_ops,
342 	.init_seq_private	= init_seq_pidns,
343 	.fini_seq_private	= fini_seq_pidns,
344 	.seq_priv_size		= sizeof(struct bpf_iter_seq_task_file_info),
345 };
346 
347 static struct bpf_iter_reg task_file_reg_info = {
348 	.target			= "task_file",
349 	.ctx_arg_info_size	= 2,
350 	.ctx_arg_info		= {
351 		{ offsetof(struct bpf_iter__task_file, task),
352 		  PTR_TO_BTF_ID_OR_NULL },
353 		{ offsetof(struct bpf_iter__task_file, file),
354 		  PTR_TO_BTF_ID_OR_NULL },
355 	},
356 	.seq_info		= &task_file_seq_info,
357 };
358 
359 static int __init task_iter_init(void)
360 {
361 	int ret;
362 
363 	task_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
364 	ret = bpf_iter_reg_target(&task_reg_info);
365 	if (ret)
366 		return ret;
367 
368 	task_file_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
369 	task_file_reg_info.ctx_arg_info[1].btf_id = btf_task_file_ids[1];
370 	return bpf_iter_reg_target(&task_file_reg_info);
371 }
372 late_initcall(task_iter_init);
373