blob: ccf9ffd5da7859a73538e1d02dced3a19563fa82 [file] [log] [blame]
Daniel Borkmannb2197752015-10-29 14:58:09 +01001/*
2 * Minimal file system backend for holding eBPF maps and programs,
3 * used by bpf(2) object pinning.
4 *
5 * Authors:
6 *
7 * Daniel Borkmann <daniel@iogearbox.net>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 */
13
Paul Gortmakera536a6e2016-07-11 12:51:01 -040014#include <linux/init.h>
Daniel Borkmannb2197752015-10-29 14:58:09 +010015#include <linux/magic.h>
16#include <linux/major.h>
17#include <linux/mount.h>
18#include <linux/namei.h>
19#include <linux/fs.h>
20#include <linux/kdev_t.h>
Daniel Borkmanna3af5f82016-11-26 01:28:08 +010021#include <linux/parser.h>
Daniel Borkmannb2197752015-10-29 14:58:09 +010022#include <linux/filter.h>
23#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010024#include <linux/bpf_trace.h>
Daniel Borkmannb2197752015-10-29 14:58:09 +010025
26enum bpf_type {
27 BPF_TYPE_UNSPEC = 0,
28 BPF_TYPE_PROG,
29 BPF_TYPE_MAP,
30};
31
32static void *bpf_any_get(void *raw, enum bpf_type type)
33{
34 switch (type) {
35 case BPF_TYPE_PROG:
Alexei Starovoitov92117d82016-04-27 18:56:20 -070036 raw = bpf_prog_inc(raw);
Daniel Borkmannb2197752015-10-29 14:58:09 +010037 break;
38 case BPF_TYPE_MAP:
Alexei Starovoitov92117d82016-04-27 18:56:20 -070039 raw = bpf_map_inc(raw, true);
Daniel Borkmannb2197752015-10-29 14:58:09 +010040 break;
41 default:
42 WARN_ON_ONCE(1);
43 break;
44 }
45
46 return raw;
47}
48
49static void bpf_any_put(void *raw, enum bpf_type type)
50{
51 switch (type) {
52 case BPF_TYPE_PROG:
53 bpf_prog_put(raw);
54 break;
55 case BPF_TYPE_MAP:
Daniel Borkmannc9da1612015-11-24 21:28:15 +010056 bpf_map_put_with_uref(raw);
Daniel Borkmannb2197752015-10-29 14:58:09 +010057 break;
58 default:
59 WARN_ON_ONCE(1);
60 break;
61 }
62}
63
64static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type)
65{
66 void *raw;
67
68 *type = BPF_TYPE_MAP;
Daniel Borkmannc9da1612015-11-24 21:28:15 +010069 raw = bpf_map_get_with_uref(ufd);
Daniel Borkmannb2197752015-10-29 14:58:09 +010070 if (IS_ERR(raw)) {
71 *type = BPF_TYPE_PROG;
72 raw = bpf_prog_get(ufd);
73 }
74
75 return raw;
76}
77
78static const struct inode_operations bpf_dir_iops;
79
80static const struct inode_operations bpf_prog_iops = { };
81static const struct inode_operations bpf_map_iops = { };
82
83static struct inode *bpf_get_inode(struct super_block *sb,
84 const struct inode *dir,
85 umode_t mode)
86{
87 struct inode *inode;
88
89 switch (mode & S_IFMT) {
90 case S_IFDIR:
91 case S_IFREG:
Daniel Borkmann0f986212016-10-29 02:30:46 +020092 case S_IFLNK:
Daniel Borkmannb2197752015-10-29 14:58:09 +010093 break;
94 default:
95 return ERR_PTR(-EINVAL);
96 }
97
98 inode = new_inode(sb);
99 if (!inode)
100 return ERR_PTR(-ENOSPC);
101
102 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -0700103 inode->i_atime = current_time(inode);
Daniel Borkmannb2197752015-10-29 14:58:09 +0100104 inode->i_mtime = inode->i_atime;
105 inode->i_ctime = inode->i_atime;
106
107 inode_init_owner(inode, dir, mode);
108
109 return inode;
110}
111
112static int bpf_inode_type(const struct inode *inode, enum bpf_type *type)
113{
114 *type = BPF_TYPE_UNSPEC;
115 if (inode->i_op == &bpf_prog_iops)
116 *type = BPF_TYPE_PROG;
117 else if (inode->i_op == &bpf_map_iops)
118 *type = BPF_TYPE_MAP;
119 else
120 return -EACCES;
121
122 return 0;
123}
124
Daniel Borkmann0f986212016-10-29 02:30:46 +0200125static void bpf_dentry_finalize(struct dentry *dentry, struct inode *inode,
126 struct inode *dir)
127{
128 d_instantiate(dentry, inode);
129 dget(dentry);
130
131 dir->i_mtime = current_time(dir);
132 dir->i_ctime = dir->i_mtime;
133}
134
Daniel Borkmannb2197752015-10-29 14:58:09 +0100135static int bpf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
136{
137 struct inode *inode;
138
Daniel Borkmannb2197752015-10-29 14:58:09 +0100139 inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFDIR);
140 if (IS_ERR(inode))
141 return PTR_ERR(inode);
142
143 inode->i_op = &bpf_dir_iops;
144 inode->i_fop = &simple_dir_operations;
145
146 inc_nlink(inode);
147 inc_nlink(dir);
148
Daniel Borkmann0f986212016-10-29 02:30:46 +0200149 bpf_dentry_finalize(dentry, inode, dir);
Daniel Borkmannb2197752015-10-29 14:58:09 +0100150 return 0;
151}
152
153static int bpf_mkobj_ops(struct inode *dir, struct dentry *dentry,
154 umode_t mode, const struct inode_operations *iops)
155{
156 struct inode *inode;
157
Daniel Borkmannb2197752015-10-29 14:58:09 +0100158 inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFREG);
159 if (IS_ERR(inode))
160 return PTR_ERR(inode);
161
162 inode->i_op = iops;
163 inode->i_private = dentry->d_fsdata;
164
Daniel Borkmann0f986212016-10-29 02:30:46 +0200165 bpf_dentry_finalize(dentry, inode, dir);
Daniel Borkmannb2197752015-10-29 14:58:09 +0100166 return 0;
167}
168
169static int bpf_mkobj(struct inode *dir, struct dentry *dentry, umode_t mode,
170 dev_t devt)
171{
172 enum bpf_type type = MINOR(devt);
173
174 if (MAJOR(devt) != UNNAMED_MAJOR || !S_ISREG(mode) ||
175 dentry->d_fsdata == NULL)
176 return -EPERM;
177
178 switch (type) {
179 case BPF_TYPE_PROG:
180 return bpf_mkobj_ops(dir, dentry, mode, &bpf_prog_iops);
181 case BPF_TYPE_MAP:
182 return bpf_mkobj_ops(dir, dentry, mode, &bpf_map_iops);
183 default:
184 return -EPERM;
185 }
186}
187
Al Viro0c93b7d2016-03-25 12:06:51 -0400188static struct dentry *
189bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags)
Daniel Borkmannbb35a6e2015-12-10 22:33:49 +0100190{
Al Viro0c93b7d2016-03-25 12:06:51 -0400191 if (strchr(dentry->d_name.name, '.'))
192 return ERR_PTR(-EPERM);
Daniel Borkmann0f986212016-10-29 02:30:46 +0200193
Al Viro0c93b7d2016-03-25 12:06:51 -0400194 return simple_lookup(dir, dentry, flags);
Daniel Borkmannbb35a6e2015-12-10 22:33:49 +0100195}
196
Daniel Borkmann0f986212016-10-29 02:30:46 +0200197static int bpf_symlink(struct inode *dir, struct dentry *dentry,
198 const char *target)
199{
200 char *link = kstrdup(target, GFP_USER | __GFP_NOWARN);
201 struct inode *inode;
202
203 if (!link)
204 return -ENOMEM;
205
206 inode = bpf_get_inode(dir->i_sb, dir, S_IRWXUGO | S_IFLNK);
207 if (IS_ERR(inode)) {
208 kfree(link);
209 return PTR_ERR(inode);
210 }
211
212 inode->i_op = &simple_symlink_inode_operations;
213 inode->i_link = link;
214
215 bpf_dentry_finalize(dentry, inode, dir);
216 return 0;
217}
218
Daniel Borkmannb2197752015-10-29 14:58:09 +0100219static const struct inode_operations bpf_dir_iops = {
Al Viro0c93b7d2016-03-25 12:06:51 -0400220 .lookup = bpf_lookup,
Daniel Borkmannb2197752015-10-29 14:58:09 +0100221 .mknod = bpf_mkobj,
222 .mkdir = bpf_mkdir,
Daniel Borkmann0f986212016-10-29 02:30:46 +0200223 .symlink = bpf_symlink,
Daniel Borkmannb2197752015-10-29 14:58:09 +0100224 .rmdir = simple_rmdir,
Al Viro0c93b7d2016-03-25 12:06:51 -0400225 .rename = simple_rename,
226 .link = simple_link,
Daniel Borkmannb2197752015-10-29 14:58:09 +0100227 .unlink = simple_unlink,
228};
229
230static int bpf_obj_do_pin(const struct filename *pathname, void *raw,
231 enum bpf_type type)
232{
233 struct dentry *dentry;
234 struct inode *dir;
235 struct path path;
236 umode_t mode;
237 dev_t devt;
238 int ret;
239
240 dentry = kern_path_create(AT_FDCWD, pathname->name, &path, 0);
241 if (IS_ERR(dentry))
242 return PTR_ERR(dentry);
243
244 mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
245 devt = MKDEV(UNNAMED_MAJOR, type);
246
247 ret = security_path_mknod(&path, dentry, mode, devt);
248 if (ret)
249 goto out;
250
251 dir = d_inode(path.dentry);
252 if (dir->i_op != &bpf_dir_iops) {
253 ret = -EPERM;
254 goto out;
255 }
256
257 dentry->d_fsdata = raw;
258 ret = vfs_mknod(dir, dentry, mode, devt);
259 dentry->d_fsdata = NULL;
260out:
261 done_path_create(&path, dentry);
262 return ret;
263}
264
265int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
266{
267 struct filename *pname;
268 enum bpf_type type;
269 void *raw;
270 int ret;
271
272 pname = getname(pathname);
273 if (IS_ERR(pname))
274 return PTR_ERR(pname);
275
276 raw = bpf_fd_probe_obj(ufd, &type);
277 if (IS_ERR(raw)) {
278 ret = PTR_ERR(raw);
279 goto out;
280 }
281
282 ret = bpf_obj_do_pin(pname, raw, type);
283 if (ret != 0)
284 bpf_any_put(raw, type);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100285 if ((trace_bpf_obj_pin_prog_enabled() ||
286 trace_bpf_obj_pin_map_enabled()) && !ret) {
287 if (type == BPF_TYPE_PROG)
288 trace_bpf_obj_pin_prog(raw, ufd, pname);
289 if (type == BPF_TYPE_MAP)
290 trace_bpf_obj_pin_map(raw, ufd, pname);
291 }
Daniel Borkmannb2197752015-10-29 14:58:09 +0100292out:
293 putname(pname);
294 return ret;
295}
296
297static void *bpf_obj_do_get(const struct filename *pathname,
298 enum bpf_type *type)
299{
300 struct inode *inode;
301 struct path path;
302 void *raw;
303 int ret;
304
305 ret = kern_path(pathname->name, LOOKUP_FOLLOW, &path);
306 if (ret)
307 return ERR_PTR(ret);
308
309 inode = d_backing_inode(path.dentry);
310 ret = inode_permission(inode, MAY_WRITE);
311 if (ret)
312 goto out;
313
314 ret = bpf_inode_type(inode, type);
315 if (ret)
316 goto out;
317
318 raw = bpf_any_get(inode->i_private, *type);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700319 if (!IS_ERR(raw))
320 touch_atime(&path);
Daniel Borkmannb2197752015-10-29 14:58:09 +0100321
322 path_put(&path);
323 return raw;
324out:
325 path_put(&path);
326 return ERR_PTR(ret);
327}
328
329int bpf_obj_get_user(const char __user *pathname)
330{
331 enum bpf_type type = BPF_TYPE_UNSPEC;
332 struct filename *pname;
333 int ret = -ENOENT;
334 void *raw;
335
336 pname = getname(pathname);
337 if (IS_ERR(pname))
338 return PTR_ERR(pname);
339
340 raw = bpf_obj_do_get(pname, &type);
341 if (IS_ERR(raw)) {
342 ret = PTR_ERR(raw);
343 goto out;
344 }
345
346 if (type == BPF_TYPE_PROG)
347 ret = bpf_prog_new_fd(raw);
348 else if (type == BPF_TYPE_MAP)
349 ret = bpf_map_new_fd(raw);
350 else
351 goto out;
352
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100353 if (ret < 0) {
Daniel Borkmannb2197752015-10-29 14:58:09 +0100354 bpf_any_put(raw, type);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100355 } else if (trace_bpf_obj_get_prog_enabled() ||
356 trace_bpf_obj_get_map_enabled()) {
357 if (type == BPF_TYPE_PROG)
358 trace_bpf_obj_get_prog(raw, ret, pname);
359 if (type == BPF_TYPE_MAP)
360 trace_bpf_obj_get_map(raw, ret, pname);
361 }
Daniel Borkmannb2197752015-10-29 14:58:09 +0100362out:
363 putname(pname);
364 return ret;
365}
Shmulik Ladkani98589a02017-10-09 15:27:15 +0300366EXPORT_SYMBOL_GPL(bpf_obj_get_user);
Daniel Borkmannb2197752015-10-29 14:58:09 +0100367
David Howells4cc7c182017-07-05 16:24:49 +0100368/*
369 * Display the mount options in /proc/mounts.
370 */
371static int bpf_show_options(struct seq_file *m, struct dentry *root)
372{
373 umode_t mode = d_inode(root)->i_mode & S_IALLUGO & ~S_ISVTX;
374
375 if (mode != S_IRWXUGO)
376 seq_printf(m, ",mode=%o", mode);
377 return 0;
378}
379
Daniel Borkmann02c2de92019-03-25 15:54:43 +0100380static void bpf_destroy_inode_deferred(struct rcu_head *head)
381{
382 struct inode *inode = container_of(head, struct inode, i_rcu);
383 enum bpf_type type;
384
385 if (S_ISLNK(inode->i_mode))
386 kfree(inode->i_link);
387 if (!bpf_inode_type(inode, &type))
388 bpf_any_put(inode->i_private, type);
389 free_inode_nonrcu(inode);
390}
391
392static void bpf_destroy_inode(struct inode *inode)
393{
394 call_rcu(&inode->i_rcu, bpf_destroy_inode_deferred);
395}
396
Daniel Borkmannb2197752015-10-29 14:58:09 +0100397static const struct super_operations bpf_super_ops = {
398 .statfs = simple_statfs,
399 .drop_inode = generic_delete_inode,
David Howells4cc7c182017-07-05 16:24:49 +0100400 .show_options = bpf_show_options,
Daniel Borkmann02c2de92019-03-25 15:54:43 +0100401 .destroy_inode = bpf_destroy_inode,
Daniel Borkmannb2197752015-10-29 14:58:09 +0100402};
403
Daniel Borkmanna3af5f82016-11-26 01:28:08 +0100404enum {
405 OPT_MODE,
406 OPT_ERR,
407};
408
409static const match_table_t bpf_mount_tokens = {
410 { OPT_MODE, "mode=%o" },
411 { OPT_ERR, NULL },
412};
413
414struct bpf_mount_opts {
415 umode_t mode;
416};
417
418static int bpf_parse_options(char *data, struct bpf_mount_opts *opts)
419{
420 substring_t args[MAX_OPT_ARGS];
421 int option, token;
422 char *ptr;
423
424 opts->mode = S_IRWXUGO;
425
426 while ((ptr = strsep(&data, ",")) != NULL) {
427 if (!*ptr)
428 continue;
429
430 token = match_token(ptr, bpf_mount_tokens, args);
431 switch (token) {
432 case OPT_MODE:
433 if (match_octal(&args[0], &option))
434 return -EINVAL;
435 opts->mode = option & S_IALLUGO;
436 break;
437 /* We might like to report bad mount options here, but
438 * traditionally we've ignored all mount options, so we'd
439 * better continue to ignore non-existing options for bpf.
440 */
441 }
442 }
443
444 return 0;
445}
446
Daniel Borkmannb2197752015-10-29 14:58:09 +0100447static int bpf_fill_super(struct super_block *sb, void *data, int silent)
448{
Eric Biggerscda37122017-03-25 21:15:37 -0700449 static const struct tree_descr bpf_rfiles[] = { { "" } };
Daniel Borkmanna3af5f82016-11-26 01:28:08 +0100450 struct bpf_mount_opts opts;
Daniel Borkmannb2197752015-10-29 14:58:09 +0100451 struct inode *inode;
452 int ret;
453
Daniel Borkmanna3af5f82016-11-26 01:28:08 +0100454 ret = bpf_parse_options(data, &opts);
455 if (ret)
456 return ret;
457
Daniel Borkmannb2197752015-10-29 14:58:09 +0100458 ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
459 if (ret)
460 return ret;
461
462 sb->s_op = &bpf_super_ops;
463
464 inode = sb->s_root->d_inode;
465 inode->i_op = &bpf_dir_iops;
466 inode->i_mode &= ~S_IALLUGO;
Daniel Borkmanna3af5f82016-11-26 01:28:08 +0100467 inode->i_mode |= S_ISVTX | opts.mode;
Daniel Borkmannb2197752015-10-29 14:58:09 +0100468
469 return 0;
470}
471
472static struct dentry *bpf_mount(struct file_system_type *type, int flags,
473 const char *dev_name, void *data)
474{
Eric W. Biedermane27f4a92016-05-20 17:22:48 -0500475 return mount_nodev(type, flags, data, bpf_fill_super);
Daniel Borkmannb2197752015-10-29 14:58:09 +0100476}
477
478static struct file_system_type bpf_fs_type = {
479 .owner = THIS_MODULE,
480 .name = "bpf",
481 .mount = bpf_mount,
482 .kill_sb = kill_litter_super,
Daniel Borkmannb2197752015-10-29 14:58:09 +0100483};
484
Daniel Borkmannb2197752015-10-29 14:58:09 +0100485static int __init bpf_init(void)
486{
487 int ret;
488
489 ret = sysfs_create_mount_point(fs_kobj, "bpf");
490 if (ret)
491 return ret;
492
493 ret = register_filesystem(&bpf_fs_type);
494 if (ret)
495 sysfs_remove_mount_point(fs_kobj, "bpf");
496
497 return ret;
498}
499fs_initcall(bpf_init);