blob: 5b6af30bfbcd887a09c3ebc8762589f138c12e8f [file] [log] [blame]
Yonghong Songeaaacd22020-05-09 10:59:11 -07001// 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>
Yonghong Song951cf362020-07-20 09:34:03 -070010#include <linux/btf_ids.h>
Yonghong Songeaaacd22020-05-09 10:59:11 -070011
12struct bpf_iter_seq_task_common {
13 struct pid_namespace *ns;
14};
15
16struct 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
24static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
Yonghong Song203d7b052020-09-01 19:31:12 -070025 u32 *tid,
26 bool skip_if_dup_files)
Yonghong Songeaaacd22020-05-09 10:59:11 -070027{
28 struct task_struct *task = NULL;
29 struct pid *pid;
30
31 rcu_read_lock();
Andrii Nakryikoc70f34a2020-05-13 22:51:37 -070032retry:
Yonghong Songe60572b2020-08-18 15:23:10 -070033 pid = find_ge_pid(*tid, ns);
Andrii Nakryikoc70f34a2020-05-13 22:51:37 -070034 if (pid) {
Yonghong Songe60572b2020-08-18 15:23:10 -070035 *tid = pid_nr_ns(pid, ns);
Yonghong Songeaaacd22020-05-09 10:59:11 -070036 task = get_pid_task(pid, PIDTYPE_PID);
Andrii Nakryikoc70f34a2020-05-13 22:51:37 -070037 if (!task) {
38 ++*tid;
39 goto retry;
Yonghong Song203d7b052020-09-01 19:31:12 -070040 } else if (skip_if_dup_files && task->tgid != task->pid &&
41 task->files == task->group_leader->files) {
42 put_task_struct(task);
43 task = NULL;
44 ++*tid;
45 goto retry;
Andrii Nakryikoc70f34a2020-05-13 22:51:37 -070046 }
47 }
Yonghong Songeaaacd22020-05-09 10:59:11 -070048 rcu_read_unlock();
49
50 return task;
51}
52
53static void *task_seq_start(struct seq_file *seq, loff_t *pos)
54{
55 struct bpf_iter_seq_task_info *info = seq->private;
56 struct task_struct *task;
57
Yonghong Song203d7b052020-09-01 19:31:12 -070058 task = task_seq_get_next(info->common.ns, &info->tid, false);
Yonghong Songeaaacd22020-05-09 10:59:11 -070059 if (!task)
60 return NULL;
61
Yonghong Song3f9969f2020-07-22 12:51:56 -070062 if (*pos == 0)
63 ++*pos;
Yonghong Songeaaacd22020-05-09 10:59:11 -070064 return task;
65}
66
67static void *task_seq_next(struct seq_file *seq, void *v, loff_t *pos)
68{
69 struct bpf_iter_seq_task_info *info = seq->private;
70 struct task_struct *task;
71
72 ++*pos;
73 ++info->tid;
74 put_task_struct((struct task_struct *)v);
Yonghong Song203d7b052020-09-01 19:31:12 -070075 task = task_seq_get_next(info->common.ns, &info->tid, false);
Yonghong Songeaaacd22020-05-09 10:59:11 -070076 if (!task)
77 return NULL;
78
79 return task;
80}
81
82struct bpf_iter__task {
83 __bpf_md_ptr(struct bpf_iter_meta *, meta);
84 __bpf_md_ptr(struct task_struct *, task);
85};
86
87DEFINE_BPF_ITER_FUNC(task, struct bpf_iter_meta *meta, struct task_struct *task)
88
89static int __task_seq_show(struct seq_file *seq, struct task_struct *task,
90 bool in_stop)
91{
92 struct bpf_iter_meta meta;
93 struct bpf_iter__task ctx;
94 struct bpf_prog *prog;
95
96 meta.seq = seq;
97 prog = bpf_iter_get_info(&meta, in_stop);
98 if (!prog)
99 return 0;
100
101 meta.seq = seq;
102 ctx.meta = &meta;
103 ctx.task = task;
104 return bpf_iter_run_prog(prog, &ctx);
105}
106
107static int task_seq_show(struct seq_file *seq, void *v)
108{
109 return __task_seq_show(seq, v, false);
110}
111
112static void task_seq_stop(struct seq_file *seq, void *v)
113{
114 if (!v)
115 (void)__task_seq_show(seq, v, true);
116 else
117 put_task_struct((struct task_struct *)v);
118}
119
120static const struct seq_operations task_seq_ops = {
121 .start = task_seq_start,
122 .next = task_seq_next,
123 .stop = task_seq_stop,
124 .show = task_seq_show,
125};
126
127struct bpf_iter_seq_task_file_info {
128 /* The first field must be struct bpf_iter_seq_task_common.
129 * this is assumed by {init, fini}_seq_pidns() callback functions.
130 */
131 struct bpf_iter_seq_task_common common;
132 struct task_struct *task;
133 struct files_struct *files;
134 u32 tid;
135 u32 fd;
136};
137
138static struct file *
139task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info,
140 struct task_struct **task, struct files_struct **fstruct)
141{
142 struct pid_namespace *ns = info->common.ns;
143 u32 curr_tid = info->tid, max_fds;
144 struct files_struct *curr_files;
145 struct task_struct *curr_task;
146 int curr_fd = info->fd;
147
148 /* If this function returns a non-NULL file object,
149 * it held a reference to the task/files_struct/file.
150 * Otherwise, it does not hold any reference.
151 */
152again:
153 if (*task) {
154 curr_task = *task;
155 curr_files = *fstruct;
156 curr_fd = info->fd;
157 } else {
Yonghong Song203d7b052020-09-01 19:31:12 -0700158 curr_task = task_seq_get_next(ns, &curr_tid, true);
Yonghong Songeaaacd22020-05-09 10:59:11 -0700159 if (!curr_task)
160 return NULL;
161
162 curr_files = get_files_struct(curr_task);
163 if (!curr_files) {
164 put_task_struct(curr_task);
165 curr_tid = ++(info->tid);
166 info->fd = 0;
167 goto again;
168 }
169
170 /* set *fstruct, *task and info->tid */
171 *fstruct = curr_files;
172 *task = curr_task;
173 if (curr_tid == info->tid) {
174 curr_fd = info->fd;
175 } else {
176 info->tid = curr_tid;
177 curr_fd = 0;
178 }
179 }
180
181 rcu_read_lock();
182 max_fds = files_fdtable(curr_files)->max_fds;
183 for (; curr_fd < max_fds; curr_fd++) {
184 struct file *f;
185
186 f = fcheck_files(curr_files, curr_fd);
187 if (!f)
188 continue;
Yonghong Songcf28f3b2020-08-17 10:42:14 -0700189 if (!get_file_rcu(f))
190 continue;
Yonghong Songeaaacd22020-05-09 10:59:11 -0700191
192 /* set info->fd */
193 info->fd = curr_fd;
Yonghong Songeaaacd22020-05-09 10:59:11 -0700194 rcu_read_unlock();
195 return f;
196 }
197
198 /* the current task is done, go to the next task */
199 rcu_read_unlock();
200 put_files_struct(curr_files);
201 put_task_struct(curr_task);
202 *task = NULL;
203 *fstruct = NULL;
204 info->fd = 0;
205 curr_tid = ++(info->tid);
206 goto again;
207}
208
209static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
210{
211 struct bpf_iter_seq_task_file_info *info = seq->private;
212 struct files_struct *files = NULL;
213 struct task_struct *task = NULL;
214 struct file *file;
215
216 file = task_file_seq_get_next(info, &task, &files);
217 if (!file) {
218 info->files = NULL;
219 info->task = NULL;
220 return NULL;
221 }
222
Yonghong Song3f9969f2020-07-22 12:51:56 -0700223 if (*pos == 0)
224 ++*pos;
Yonghong Songeaaacd22020-05-09 10:59:11 -0700225 info->task = task;
226 info->files = files;
227
228 return file;
229}
230
231static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
232{
233 struct bpf_iter_seq_task_file_info *info = seq->private;
234 struct files_struct *files = info->files;
235 struct task_struct *task = info->task;
236 struct file *file;
237
238 ++*pos;
239 ++info->fd;
240 fput((struct file *)v);
241 file = task_file_seq_get_next(info, &task, &files);
242 if (!file) {
243 info->files = NULL;
244 info->task = NULL;
245 return NULL;
246 }
247
248 info->task = task;
249 info->files = files;
250
251 return file;
252}
253
254struct bpf_iter__task_file {
255 __bpf_md_ptr(struct bpf_iter_meta *, meta);
256 __bpf_md_ptr(struct task_struct *, task);
257 u32 fd __aligned(8);
258 __bpf_md_ptr(struct file *, file);
259};
260
261DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
262 struct task_struct *task, u32 fd,
263 struct file *file)
264
265static int __task_file_seq_show(struct seq_file *seq, struct file *file,
266 bool in_stop)
267{
268 struct bpf_iter_seq_task_file_info *info = seq->private;
269 struct bpf_iter__task_file ctx;
270 struct bpf_iter_meta meta;
271 struct bpf_prog *prog;
272
273 meta.seq = seq;
274 prog = bpf_iter_get_info(&meta, in_stop);
275 if (!prog)
276 return 0;
277
278 ctx.meta = &meta;
279 ctx.task = info->task;
280 ctx.fd = info->fd;
281 ctx.file = file;
282 return bpf_iter_run_prog(prog, &ctx);
283}
284
285static int task_file_seq_show(struct seq_file *seq, void *v)
286{
287 return __task_file_seq_show(seq, v, false);
288}
289
290static void task_file_seq_stop(struct seq_file *seq, void *v)
291{
292 struct bpf_iter_seq_task_file_info *info = seq->private;
293
294 if (!v) {
295 (void)__task_file_seq_show(seq, v, true);
296 } else {
297 fput((struct file *)v);
298 put_files_struct(info->files);
299 put_task_struct(info->task);
300 info->files = NULL;
301 info->task = NULL;
302 }
303}
304
Yonghong Songf9c79272020-07-23 11:41:10 -0700305static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
Yonghong Songeaaacd22020-05-09 10:59:11 -0700306{
307 struct bpf_iter_seq_task_common *common = priv_data;
308
309 common->ns = get_pid_ns(task_active_pid_ns(current));
310 return 0;
311}
312
313static void fini_seq_pidns(void *priv_data)
314{
315 struct bpf_iter_seq_task_common *common = priv_data;
316
317 put_pid_ns(common->ns);
318}
319
320static const struct seq_operations task_file_seq_ops = {
321 .start = task_file_seq_start,
322 .next = task_file_seq_next,
323 .stop = task_file_seq_stop,
324 .show = task_file_seq_show,
325};
326
Yonghong Song951cf362020-07-20 09:34:03 -0700327BTF_ID_LIST(btf_task_file_ids)
328BTF_ID(struct, task_struct)
329BTF_ID(struct, file)
330
Yonghong Song14fc6bd62020-07-23 11:41:09 -0700331static const struct bpf_iter_seq_info task_seq_info = {
Yonghong Song15172a42020-05-13 11:02:19 -0700332 .seq_ops = &task_seq_ops,
333 .init_seq_private = init_seq_pidns,
334 .fini_seq_private = fini_seq_pidns,
335 .seq_priv_size = sizeof(struct bpf_iter_seq_task_info),
Yonghong Song14fc6bd62020-07-23 11:41:09 -0700336};
337
338static struct bpf_iter_reg task_reg_info = {
339 .target = "task",
Yonghong Song3c32cc12020-05-13 11:02:21 -0700340 .ctx_arg_info_size = 1,
341 .ctx_arg_info = {
342 { offsetof(struct bpf_iter__task, task),
343 PTR_TO_BTF_ID_OR_NULL },
344 },
Yonghong Song14fc6bd62020-07-23 11:41:09 -0700345 .seq_info = &task_seq_info,
Yonghong Song15172a42020-05-13 11:02:19 -0700346};
347
Yonghong Song14fc6bd62020-07-23 11:41:09 -0700348static const struct bpf_iter_seq_info task_file_seq_info = {
Yonghong Song15172a42020-05-13 11:02:19 -0700349 .seq_ops = &task_file_seq_ops,
350 .init_seq_private = init_seq_pidns,
351 .fini_seq_private = fini_seq_pidns,
352 .seq_priv_size = sizeof(struct bpf_iter_seq_task_file_info),
Yonghong Song14fc6bd62020-07-23 11:41:09 -0700353};
354
355static struct bpf_iter_reg task_file_reg_info = {
356 .target = "task_file",
Yonghong Song3c32cc12020-05-13 11:02:21 -0700357 .ctx_arg_info_size = 2,
358 .ctx_arg_info = {
359 { offsetof(struct bpf_iter__task_file, task),
360 PTR_TO_BTF_ID_OR_NULL },
361 { offsetof(struct bpf_iter__task_file, file),
362 PTR_TO_BTF_ID_OR_NULL },
363 },
Yonghong Song14fc6bd62020-07-23 11:41:09 -0700364 .seq_info = &task_file_seq_info,
Yonghong Song15172a42020-05-13 11:02:19 -0700365};
366
Yonghong Songeaaacd22020-05-09 10:59:11 -0700367static int __init task_iter_init(void)
368{
Yonghong Songeaaacd22020-05-09 10:59:11 -0700369 int ret;
370
Yonghong Song951cf362020-07-20 09:34:03 -0700371 task_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
Yonghong Songeaaacd22020-05-09 10:59:11 -0700372 ret = bpf_iter_reg_target(&task_reg_info);
373 if (ret)
374 return ret;
375
Yonghong Song951cf362020-07-20 09:34:03 -0700376 task_file_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
377 task_file_reg_info.ctx_arg_info[1].btf_id = btf_task_file_ids[1];
Yonghong Songeaaacd22020-05-09 10:59:11 -0700378 return bpf_iter_reg_target(&task_file_reg_info);
379}
380late_initcall(task_iter_init);