2 * Minimal file system backend for holding eBPF maps and programs,
3 * used by bpf(2) object pinning.
7 * Daniel Borkmann <daniel@iogearbox.net>
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.
14 #include <linux/init.h>
15 #include <linux/magic.h>
16 #include <linux/major.h>
17 #include <linux/mount.h>
18 #include <linux/namei.h>
20 #include <linux/kdev_t.h>
21 #include <linux/parser.h>
22 #include <linux/filter.h>
23 #include <linux/bpf.h>
24 #include <linux/bpf_trace.h>
32 static void *bpf_any_get(void *raw, enum bpf_type type)
36 raw = bpf_prog_inc(raw);
39 raw = bpf_map_inc(raw, true);
49 static void bpf_any_put(void *raw, enum bpf_type type)
56 bpf_map_put_with_uref(raw);
64 static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type)
69 raw = bpf_map_get_with_uref(ufd);
71 *type = BPF_TYPE_PROG;
72 raw = bpf_prog_get(ufd);
78 static const struct inode_operations bpf_dir_iops;
80 static const struct inode_operations bpf_prog_iops = { };
81 static const struct inode_operations bpf_map_iops = { };
83 static struct inode *bpf_get_inode(struct super_block *sb,
84 const struct inode *dir,
89 switch (mode & S_IFMT) {
95 return ERR_PTR(-EINVAL);
98 inode = new_inode(sb);
100 return ERR_PTR(-ENOSPC);
102 inode->i_ino = get_next_ino();
103 inode->i_atime = current_time(inode);
104 inode->i_mtime = inode->i_atime;
105 inode->i_ctime = inode->i_atime;
107 inode_init_owner(inode, dir, mode);
112 static int bpf_inode_type(const struct inode *inode, enum bpf_type *type)
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;
125 static void bpf_dentry_finalize(struct dentry *dentry, struct inode *inode,
128 d_instantiate(dentry, inode);
131 dir->i_mtime = current_time(dir);
132 dir->i_ctime = dir->i_mtime;
135 static int bpf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
139 inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFDIR);
141 return PTR_ERR(inode);
143 inode->i_op = &bpf_dir_iops;
144 inode->i_fop = &simple_dir_operations;
149 bpf_dentry_finalize(dentry, inode, dir);
153 static int bpf_mkobj_ops(struct dentry *dentry, umode_t mode, void *raw,
154 const struct inode_operations *iops)
156 struct inode *dir = dentry->d_parent->d_inode;
157 struct inode *inode = bpf_get_inode(dir->i_sb, dir, mode);
159 return PTR_ERR(inode);
162 inode->i_private = raw;
164 bpf_dentry_finalize(dentry, inode, dir);
168 static int bpf_mkprog(struct dentry *dentry, umode_t mode, void *arg)
170 return bpf_mkobj_ops(dentry, mode, arg, &bpf_prog_iops);
173 static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
175 return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops);
178 static struct dentry *
179 bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags)
181 if (strchr(dentry->d_name.name, '.'))
182 return ERR_PTR(-EPERM);
184 return simple_lookup(dir, dentry, flags);
187 static int bpf_symlink(struct inode *dir, struct dentry *dentry,
190 char *link = kstrdup(target, GFP_USER | __GFP_NOWARN);
196 inode = bpf_get_inode(dir->i_sb, dir, S_IRWXUGO | S_IFLNK);
199 return PTR_ERR(inode);
202 inode->i_op = &simple_symlink_inode_operations;
203 inode->i_link = link;
205 bpf_dentry_finalize(dentry, inode, dir);
209 static const struct inode_operations bpf_dir_iops = {
210 .lookup = bpf_lookup,
212 .symlink = bpf_symlink,
213 .rmdir = simple_rmdir,
214 .rename = simple_rename,
216 .unlink = simple_unlink,
219 static int bpf_obj_do_pin(const struct filename *pathname, void *raw,
222 struct dentry *dentry;
228 dentry = kern_path_create(AT_FDCWD, pathname->name, &path, 0);
230 return PTR_ERR(dentry);
232 mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
234 ret = security_path_mknod(&path, dentry, mode, 0);
238 dir = d_inode(path.dentry);
239 if (dir->i_op != &bpf_dir_iops) {
246 ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw);
249 ret = vfs_mkobj(dentry, mode, bpf_mkmap, raw);
255 done_path_create(&path, dentry);
259 int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
261 struct filename *pname;
266 pname = getname(pathname);
268 return PTR_ERR(pname);
270 raw = bpf_fd_probe_obj(ufd, &type);
276 ret = bpf_obj_do_pin(pname, raw, type);
278 bpf_any_put(raw, type);
279 if ((trace_bpf_obj_pin_prog_enabled() ||
280 trace_bpf_obj_pin_map_enabled()) && !ret) {
281 if (type == BPF_TYPE_PROG)
282 trace_bpf_obj_pin_prog(raw, ufd, pname);
283 if (type == BPF_TYPE_MAP)
284 trace_bpf_obj_pin_map(raw, ufd, pname);
291 static void *bpf_obj_do_get(const struct filename *pathname,
292 enum bpf_type *type, int flags)
299 ret = kern_path(pathname->name, LOOKUP_FOLLOW, &path);
303 inode = d_backing_inode(path.dentry);
304 ret = inode_permission(inode, ACC_MODE(flags));
308 ret = bpf_inode_type(inode, type);
312 raw = bpf_any_get(inode->i_private, *type);
323 int bpf_obj_get_user(const char __user *pathname, int flags)
325 enum bpf_type type = BPF_TYPE_UNSPEC;
326 struct filename *pname;
331 f_flags = bpf_get_file_flag(flags);
335 pname = getname(pathname);
337 return PTR_ERR(pname);
339 raw = bpf_obj_do_get(pname, &type, f_flags);
345 if (type == BPF_TYPE_PROG)
346 ret = bpf_prog_new_fd(raw);
347 else if (type == BPF_TYPE_MAP)
348 ret = bpf_map_new_fd(raw, f_flags);
353 bpf_any_put(raw, type);
354 } else if (trace_bpf_obj_get_prog_enabled() ||
355 trace_bpf_obj_get_map_enabled()) {
356 if (type == BPF_TYPE_PROG)
357 trace_bpf_obj_get_prog(raw, ret, pname);
358 if (type == BPF_TYPE_MAP)
359 trace_bpf_obj_get_map(raw, ret, pname);
365 EXPORT_SYMBOL_GPL(bpf_obj_get_user);
367 static void bpf_evict_inode(struct inode *inode)
371 truncate_inode_pages_final(&inode->i_data);
374 if (S_ISLNK(inode->i_mode))
375 kfree(inode->i_link);
376 if (!bpf_inode_type(inode, &type))
377 bpf_any_put(inode->i_private, type);
381 * Display the mount options in /proc/mounts.
383 static int bpf_show_options(struct seq_file *m, struct dentry *root)
385 umode_t mode = d_inode(root)->i_mode & S_IALLUGO & ~S_ISVTX;
387 if (mode != S_IRWXUGO)
388 seq_printf(m, ",mode=%o", mode);
392 static const struct super_operations bpf_super_ops = {
393 .statfs = simple_statfs,
394 .drop_inode = generic_delete_inode,
395 .show_options = bpf_show_options,
396 .evict_inode = bpf_evict_inode,
404 static const match_table_t bpf_mount_tokens = {
405 { OPT_MODE, "mode=%o" },
409 struct bpf_mount_opts {
413 static int bpf_parse_options(char *data, struct bpf_mount_opts *opts)
415 substring_t args[MAX_OPT_ARGS];
419 opts->mode = S_IRWXUGO;
421 while ((ptr = strsep(&data, ",")) != NULL) {
425 token = match_token(ptr, bpf_mount_tokens, args);
428 if (match_octal(&args[0], &option))
430 opts->mode = option & S_IALLUGO;
432 /* We might like to report bad mount options here, but
433 * traditionally we've ignored all mount options, so we'd
434 * better continue to ignore non-existing options for bpf.
442 static int bpf_fill_super(struct super_block *sb, void *data, int silent)
444 static const struct tree_descr bpf_rfiles[] = { { "" } };
445 struct bpf_mount_opts opts;
449 ret = bpf_parse_options(data, &opts);
453 ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
457 sb->s_op = &bpf_super_ops;
459 inode = sb->s_root->d_inode;
460 inode->i_op = &bpf_dir_iops;
461 inode->i_mode &= ~S_IALLUGO;
462 inode->i_mode |= S_ISVTX | opts.mode;
467 static struct dentry *bpf_mount(struct file_system_type *type, int flags,
468 const char *dev_name, void *data)
470 return mount_nodev(type, flags, data, bpf_fill_super);
473 static struct file_system_type bpf_fs_type = {
474 .owner = THIS_MODULE,
477 .kill_sb = kill_litter_super,
480 static int __init bpf_init(void)
484 ret = sysfs_create_mount_point(fs_kobj, "bpf");
488 ret = register_filesystem(&bpf_fs_type);
490 sysfs_remove_mount_point(fs_kobj, "bpf");
494 fs_initcall(bpf_init);