blob: 232df29793e962201aa4875d39b8cbcc4d295a16 [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,
25 u32 *tid)
26{
27 struct task_struct *task = NULL;
28 struct pid *pid;
29
30 rcu_read_lock();
Andrii Nakryikoc70f34a2020-05-13 22:51:37 -070031retry:
Yonghong Songeaaacd22020-05-09 10:59:11 -070032 pid = idr_get_next(&ns->idr, tid);
Andrii Nakryikoc70f34a2020-05-13 22:51:37 -070033 if (pid) {
Yonghong Songeaaacd22020-05-09 10:59:11 -070034 task = get_pid_task(pid, PIDTYPE_PID);
Andrii Nakryikoc70f34a2020-05-13 22:51:37 -070035 if (!task) {
36 ++*tid;
37 goto retry;
38 }
39 }
Yonghong Songeaaacd22020-05-09 10:59:11 -070040 rcu_read_unlock();
41
42 return task;
43}
44
45static 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
Yonghong Song3f9969f2020-07-22 12:51:56 -070054 if (*pos == 0)
55 ++*pos;
Yonghong Songeaaacd22020-05-09 10:59:11 -070056 return task;
57}
58
59static 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
74struct bpf_iter__task {
75 __bpf_md_ptr(struct bpf_iter_meta *, meta);
76 __bpf_md_ptr(struct task_struct *, task);
77};
78
79DEFINE_BPF_ITER_FUNC(task, struct bpf_iter_meta *meta, struct task_struct *task)
80
81static 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
99static int task_seq_show(struct seq_file *seq, void *v)
100{
101 return __task_seq_show(seq, v, false);
102}
103
104static 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
112static 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
119struct 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
130static struct file *
131task_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 */
144again:
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
200static 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
Yonghong Song3f9969f2020-07-22 12:51:56 -0700214 if (*pos == 0)
215 ++*pos;
Yonghong Songeaaacd22020-05-09 10:59:11 -0700216 info->task = task;
217 info->files = files;
218
219 return file;
220}
221
222static 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
245struct 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
252DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
253 struct task_struct *task, u32 fd,
254 struct file *file)
255
256static 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
276static int task_file_seq_show(struct seq_file *seq, void *v)
277{
278 return __task_file_seq_show(seq, v, false);
279}
280
281static 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
Yonghong Songf9c79272020-07-23 11:41:10 -0700296static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
Yonghong Songeaaacd22020-05-09 10:59:11 -0700297{
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
304static 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
311static 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
Yonghong Song951cf362020-07-20 09:34:03 -0700318BTF_ID_LIST(btf_task_file_ids)
319BTF_ID(struct, task_struct)
320BTF_ID(struct, file)
321
Yonghong Song14fc6bd62020-07-23 11:41:09 -0700322static const struct bpf_iter_seq_info task_seq_info = {
Yonghong Song15172a42020-05-13 11:02:19 -0700323 .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),
Yonghong Song14fc6bd62020-07-23 11:41:09 -0700327};
328
329static struct bpf_iter_reg task_reg_info = {
330 .target = "task",
Yonghong Song3c32cc12020-05-13 11:02:21 -0700331 .ctx_arg_info_size = 1,
332 .ctx_arg_info = {
333 { offsetof(struct bpf_iter__task, task),
334 PTR_TO_BTF_ID_OR_NULL },
335 },
Yonghong Song14fc6bd62020-07-23 11:41:09 -0700336 .seq_info = &task_seq_info,
Yonghong Song15172a42020-05-13 11:02:19 -0700337};
338
Yonghong Song14fc6bd62020-07-23 11:41:09 -0700339static const struct bpf_iter_seq_info task_file_seq_info = {
Yonghong Song15172a42020-05-13 11:02:19 -0700340 .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),
Yonghong Song14fc6bd62020-07-23 11:41:09 -0700344};
345
346static struct bpf_iter_reg task_file_reg_info = {
347 .target = "task_file",
Yonghong Song3c32cc12020-05-13 11:02:21 -0700348 .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 },
Yonghong Song14fc6bd62020-07-23 11:41:09 -0700355 .seq_info = &task_file_seq_info,
Yonghong Song15172a42020-05-13 11:02:19 -0700356};
357
Yonghong Songeaaacd22020-05-09 10:59:11 -0700358static int __init task_iter_init(void)
359{
Yonghong Songeaaacd22020-05-09 10:59:11 -0700360 int ret;
361
Yonghong Song951cf362020-07-20 09:34:03 -0700362 task_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
Yonghong Songeaaacd22020-05-09 10:59:11 -0700363 ret = bpf_iter_reg_target(&task_reg_info);
364 if (ret)
365 return ret;
366
Yonghong Song951cf362020-07-20 09:34:03 -0700367 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];
Yonghong Songeaaacd22020-05-09 10:59:11 -0700369 return bpf_iter_reg_target(&task_file_reg_info);
370}
371late_initcall(task_iter_init);