Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/pmladek...
[linux-2.6-block.git] / kernel / bpf / inode.c
1 /*
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
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>
19 #include <linux/fs.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>
25
26 enum bpf_type {
27         BPF_TYPE_UNSPEC = 0,
28         BPF_TYPE_PROG,
29         BPF_TYPE_MAP,
30 };
31
32 static void *bpf_any_get(void *raw, enum bpf_type type)
33 {
34         switch (type) {
35         case BPF_TYPE_PROG:
36                 raw = bpf_prog_inc(raw);
37                 break;
38         case BPF_TYPE_MAP:
39                 raw = bpf_map_inc(raw, true);
40                 break;
41         default:
42                 WARN_ON_ONCE(1);
43                 break;
44         }
45
46         return raw;
47 }
48
49 static 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:
56                 bpf_map_put_with_uref(raw);
57                 break;
58         default:
59                 WARN_ON_ONCE(1);
60                 break;
61         }
62 }
63
64 static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type)
65 {
66         void *raw;
67
68         *type = BPF_TYPE_MAP;
69         raw = bpf_map_get_with_uref(ufd);
70         if (IS_ERR(raw)) {
71                 *type = BPF_TYPE_PROG;
72                 raw = bpf_prog_get(ufd);
73         }
74
75         return raw;
76 }
77
78 static const struct inode_operations bpf_dir_iops;
79
80 static const struct inode_operations bpf_prog_iops = { };
81 static const struct inode_operations bpf_map_iops  = { };
82
83 static 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:
92         case S_IFLNK:
93                 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();
103         inode->i_atime = current_time(inode);
104         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
112 static 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
125 static 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
135 static int bpf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
136 {
137         struct inode *inode;
138
139         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
149         bpf_dentry_finalize(dentry, inode, dir);
150         return 0;
151 }
152
153 static int bpf_mkobj_ops(struct dentry *dentry, umode_t mode, void *raw,
154                          const struct inode_operations *iops)
155 {
156         struct inode *dir = dentry->d_parent->d_inode;
157         struct inode *inode = bpf_get_inode(dir->i_sb, dir, mode);
158         if (IS_ERR(inode))
159                 return PTR_ERR(inode);
160
161         inode->i_op = iops;
162         inode->i_private = raw;
163
164         bpf_dentry_finalize(dentry, inode, dir);
165         return 0;
166 }
167
168 static int bpf_mkprog(struct dentry *dentry, umode_t mode, void *arg)
169 {
170         return bpf_mkobj_ops(dentry, mode, arg, &bpf_prog_iops);
171 }
172
173 static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
174 {
175         return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops);
176 }
177
178 static struct dentry *
179 bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags)
180 {
181         if (strchr(dentry->d_name.name, '.'))
182                 return ERR_PTR(-EPERM);
183
184         return simple_lookup(dir, dentry, flags);
185 }
186
187 static int bpf_symlink(struct inode *dir, struct dentry *dentry,
188                        const char *target)
189 {
190         char *link = kstrdup(target, GFP_USER | __GFP_NOWARN);
191         struct inode *inode;
192
193         if (!link)
194                 return -ENOMEM;
195
196         inode = bpf_get_inode(dir->i_sb, dir, S_IRWXUGO | S_IFLNK);
197         if (IS_ERR(inode)) {
198                 kfree(link);
199                 return PTR_ERR(inode);
200         }
201
202         inode->i_op = &simple_symlink_inode_operations;
203         inode->i_link = link;
204
205         bpf_dentry_finalize(dentry, inode, dir);
206         return 0;
207 }
208
209 static const struct inode_operations bpf_dir_iops = {
210         .lookup         = bpf_lookup,
211         .mkdir          = bpf_mkdir,
212         .symlink        = bpf_symlink,
213         .rmdir          = simple_rmdir,
214         .rename         = simple_rename,
215         .link           = simple_link,
216         .unlink         = simple_unlink,
217 };
218
219 static int bpf_obj_do_pin(const struct filename *pathname, void *raw,
220                           enum bpf_type type)
221 {
222         struct dentry *dentry;
223         struct inode *dir;
224         struct path path;
225         umode_t mode;
226         int ret;
227
228         dentry = kern_path_create(AT_FDCWD, pathname->name, &path, 0);
229         if (IS_ERR(dentry))
230                 return PTR_ERR(dentry);
231
232         mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
233
234         ret = security_path_mknod(&path, dentry, mode, 0);
235         if (ret)
236                 goto out;
237
238         dir = d_inode(path.dentry);
239         if (dir->i_op != &bpf_dir_iops) {
240                 ret = -EPERM;
241                 goto out;
242         }
243
244         switch (type) {
245         case BPF_TYPE_PROG:
246                 ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw);
247                 break;
248         case BPF_TYPE_MAP:
249                 ret = vfs_mkobj(dentry, mode, bpf_mkmap, raw);
250                 break;
251         default:
252                 ret = -EPERM;
253         }
254 out:
255         done_path_create(&path, dentry);
256         return ret;
257 }
258
259 int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
260 {
261         struct filename *pname;
262         enum bpf_type type;
263         void *raw;
264         int ret;
265
266         pname = getname(pathname);
267         if (IS_ERR(pname))
268                 return PTR_ERR(pname);
269
270         raw = bpf_fd_probe_obj(ufd, &type);
271         if (IS_ERR(raw)) {
272                 ret = PTR_ERR(raw);
273                 goto out;
274         }
275
276         ret = bpf_obj_do_pin(pname, raw, type);
277         if (ret != 0)
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);
285         }
286 out:
287         putname(pname);
288         return ret;
289 }
290
291 static void *bpf_obj_do_get(const struct filename *pathname,
292                             enum bpf_type *type, int flags)
293 {
294         struct inode *inode;
295         struct path path;
296         void *raw;
297         int ret;
298
299         ret = kern_path(pathname->name, LOOKUP_FOLLOW, &path);
300         if (ret)
301                 return ERR_PTR(ret);
302
303         inode = d_backing_inode(path.dentry);
304         ret = inode_permission(inode, ACC_MODE(flags));
305         if (ret)
306                 goto out;
307
308         ret = bpf_inode_type(inode, type);
309         if (ret)
310                 goto out;
311
312         raw = bpf_any_get(inode->i_private, *type);
313         if (!IS_ERR(raw))
314                 touch_atime(&path);
315
316         path_put(&path);
317         return raw;
318 out:
319         path_put(&path);
320         return ERR_PTR(ret);
321 }
322
323 int bpf_obj_get_user(const char __user *pathname, int flags)
324 {
325         enum bpf_type type = BPF_TYPE_UNSPEC;
326         struct filename *pname;
327         int ret = -ENOENT;
328         int f_flags;
329         void *raw;
330
331         f_flags = bpf_get_file_flag(flags);
332         if (f_flags < 0)
333                 return f_flags;
334
335         pname = getname(pathname);
336         if (IS_ERR(pname))
337                 return PTR_ERR(pname);
338
339         raw = bpf_obj_do_get(pname, &type, f_flags);
340         if (IS_ERR(raw)) {
341                 ret = PTR_ERR(raw);
342                 goto out;
343         }
344
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);
349         else
350                 goto out;
351
352         if (ret < 0) {
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);
360         }
361 out:
362         putname(pname);
363         return ret;
364 }
365
366 static struct bpf_prog *__get_prog_inode(struct inode *inode, enum bpf_prog_type type)
367 {
368         struct bpf_prog *prog;
369         int ret = inode_permission(inode, MAY_READ | MAY_WRITE);
370         if (ret)
371                 return ERR_PTR(ret);
372
373         if (inode->i_op == &bpf_map_iops)
374                 return ERR_PTR(-EINVAL);
375         if (inode->i_op != &bpf_prog_iops)
376                 return ERR_PTR(-EACCES);
377
378         prog = inode->i_private;
379
380         ret = security_bpf_prog(prog);
381         if (ret < 0)
382                 return ERR_PTR(ret);
383
384         if (!bpf_prog_get_ok(prog, &type, false))
385                 return ERR_PTR(-EINVAL);
386
387         return bpf_prog_inc(prog);
388 }
389
390 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type)
391 {
392         struct bpf_prog *prog;
393         struct path path;
394         int ret = kern_path(name, LOOKUP_FOLLOW, &path);
395         if (ret)
396                 return ERR_PTR(ret);
397         prog = __get_prog_inode(d_backing_inode(path.dentry), type);
398         if (!IS_ERR(prog))
399                 touch_atime(&path);
400         path_put(&path);
401         return prog;
402 }
403 EXPORT_SYMBOL(bpf_prog_get_type_path);
404
405 static void bpf_evict_inode(struct inode *inode)
406 {
407         enum bpf_type type;
408
409         truncate_inode_pages_final(&inode->i_data);
410         clear_inode(inode);
411
412         if (S_ISLNK(inode->i_mode))
413                 kfree(inode->i_link);
414         if (!bpf_inode_type(inode, &type))
415                 bpf_any_put(inode->i_private, type);
416 }
417
418 /*
419  * Display the mount options in /proc/mounts.
420  */
421 static int bpf_show_options(struct seq_file *m, struct dentry *root)
422 {
423         umode_t mode = d_inode(root)->i_mode & S_IALLUGO & ~S_ISVTX;
424
425         if (mode != S_IRWXUGO)
426                 seq_printf(m, ",mode=%o", mode);
427         return 0;
428 }
429
430 static const struct super_operations bpf_super_ops = {
431         .statfs         = simple_statfs,
432         .drop_inode     = generic_delete_inode,
433         .show_options   = bpf_show_options,
434         .evict_inode    = bpf_evict_inode,
435 };
436
437 enum {
438         OPT_MODE,
439         OPT_ERR,
440 };
441
442 static const match_table_t bpf_mount_tokens = {
443         { OPT_MODE, "mode=%o" },
444         { OPT_ERR, NULL },
445 };
446
447 struct bpf_mount_opts {
448         umode_t mode;
449 };
450
451 static int bpf_parse_options(char *data, struct bpf_mount_opts *opts)
452 {
453         substring_t args[MAX_OPT_ARGS];
454         int option, token;
455         char *ptr;
456
457         opts->mode = S_IRWXUGO;
458
459         while ((ptr = strsep(&data, ",")) != NULL) {
460                 if (!*ptr)
461                         continue;
462
463                 token = match_token(ptr, bpf_mount_tokens, args);
464                 switch (token) {
465                 case OPT_MODE:
466                         if (match_octal(&args[0], &option))
467                                 return -EINVAL;
468                         opts->mode = option & S_IALLUGO;
469                         break;
470                 /* We might like to report bad mount options here, but
471                  * traditionally we've ignored all mount options, so we'd
472                  * better continue to ignore non-existing options for bpf.
473                  */
474                 }
475         }
476
477         return 0;
478 }
479
480 static int bpf_fill_super(struct super_block *sb, void *data, int silent)
481 {
482         static const struct tree_descr bpf_rfiles[] = { { "" } };
483         struct bpf_mount_opts opts;
484         struct inode *inode;
485         int ret;
486
487         ret = bpf_parse_options(data, &opts);
488         if (ret)
489                 return ret;
490
491         ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
492         if (ret)
493                 return ret;
494
495         sb->s_op = &bpf_super_ops;
496
497         inode = sb->s_root->d_inode;
498         inode->i_op = &bpf_dir_iops;
499         inode->i_mode &= ~S_IALLUGO;
500         inode->i_mode |= S_ISVTX | opts.mode;
501
502         return 0;
503 }
504
505 static struct dentry *bpf_mount(struct file_system_type *type, int flags,
506                                 const char *dev_name, void *data)
507 {
508         return mount_nodev(type, flags, data, bpf_fill_super);
509 }
510
511 static struct file_system_type bpf_fs_type = {
512         .owner          = THIS_MODULE,
513         .name           = "bpf",
514         .mount          = bpf_mount,
515         .kill_sb        = kill_litter_super,
516 };
517
518 static int __init bpf_init(void)
519 {
520         int ret;
521
522         ret = sysfs_create_mount_point(fs_kobj, "bpf");
523         if (ret)
524                 return ret;
525
526         ret = register_filesystem(&bpf_fs_type);
527         if (ret)
528                 sysfs_remove_mount_point(fs_kobj, "bpf");
529
530         return ret;
531 }
532 fs_initcall(bpf_init);