2019-06-04 15:11:33 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-10-29 20:58:09 +07:00
|
|
|
/*
|
|
|
|
* Minimal file system backend for holding eBPF maps and programs,
|
|
|
|
* used by bpf(2) object pinning.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
*
|
|
|
|
* Daniel Borkmann <daniel@iogearbox.net>
|
|
|
|
*/
|
|
|
|
|
2016-07-11 23:51:01 +07:00
|
|
|
#include <linux/init.h>
|
2015-10-29 20:58:09 +07:00
|
|
|
#include <linux/magic.h>
|
|
|
|
#include <linux/major.h>
|
|
|
|
#include <linux/mount.h>
|
|
|
|
#include <linux/namei.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/kdev_t.h>
|
2016-11-26 07:28:08 +07:00
|
|
|
#include <linux/parser.h>
|
2015-10-29 20:58:09 +07:00
|
|
|
#include <linux/filter.h>
|
|
|
|
#include <linux/bpf.h>
|
bpf: add initial bpf tracepoints
This work adds a number of tracepoints to paths that are either
considered slow-path or exception-like states, where monitoring or
inspecting them would be desirable.
For bpf(2) syscall, tracepoints have been placed for main commands
when they succeed. In XDP case, tracepoint is for exceptions, that
is, f.e. on abnormal BPF program exit such as unknown or XDP_ABORTED
return code, or when error occurs during XDP_TX action and the packet
could not be forwarded.
Both have been split into separate event headers, and can be further
extended. Worst case, if they unexpectedly should get into our way in
future, they can also removed [1]. Of course, these tracepoints (like
any other) can be analyzed by eBPF itself, etc. Example output:
# ./perf record -a -e bpf:* sleep 10
# ./perf script
sock_example 6197 [005] 283.980322: bpf:bpf_map_create: map type=ARRAY ufd=4 key=4 val=8 max=256 flags=0
sock_example 6197 [005] 283.980721: bpf:bpf_prog_load: prog=a5ea8fa30ea6849c type=SOCKET_FILTER ufd=5
sock_example 6197 [005] 283.988423: bpf:bpf_prog_get_type: prog=a5ea8fa30ea6849c type=SOCKET_FILTER
sock_example 6197 [005] 283.988443: bpf:bpf_map_lookup_elem: map type=ARRAY ufd=4 key=[06 00 00 00] val=[00 00 00 00 00 00 00 00]
[...]
sock_example 6197 [005] 288.990868: bpf:bpf_map_lookup_elem: map type=ARRAY ufd=4 key=[01 00 00 00] val=[14 00 00 00 00 00 00 00]
swapper 0 [005] 289.338243: bpf:bpf_prog_put_rcu: prog=a5ea8fa30ea6849c type=SOCKET_FILTER
[1] https://lwn.net/Articles/705270/
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-01-25 08:28:18 +07:00
|
|
|
#include <linux/bpf_trace.h>
|
2015-10-29 20:58:09 +07:00
|
|
|
|
|
|
|
enum bpf_type {
|
|
|
|
BPF_TYPE_UNSPEC = 0,
|
|
|
|
BPF_TYPE_PROG,
|
|
|
|
BPF_TYPE_MAP,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *bpf_any_get(void *raw, enum bpf_type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case BPF_TYPE_PROG:
|
2016-04-28 08:56:20 +07:00
|
|
|
raw = bpf_prog_inc(raw);
|
2015-10-29 20:58:09 +07:00
|
|
|
break;
|
|
|
|
case BPF_TYPE_MAP:
|
2016-04-28 08:56:20 +07:00
|
|
|
raw = bpf_map_inc(raw, true);
|
2015-10-29 20:58:09 +07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bpf_any_put(void *raw, enum bpf_type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case BPF_TYPE_PROG:
|
|
|
|
bpf_prog_put(raw);
|
|
|
|
break;
|
|
|
|
case BPF_TYPE_MAP:
|
bpf: fix clearing on persistent program array maps
Currently, when having map file descriptors pointing to program arrays,
there's still the issue that we unconditionally flush program array
contents via bpf_fd_array_map_clear() in bpf_map_release(). This happens
when such a file descriptor is released and is independent of the map's
refcount.
Having this flush independent of the refcount is for a reason: there
can be arbitrary complex dependency chains among tail calls, also circular
ones (direct or indirect, nesting limit determined during runtime), and
we need to make sure that the map drops all references to eBPF programs
it holds, so that the map's refcount can eventually drop to zero and
initiate its freeing. Btw, a walk of the whole dependency graph would
not be possible for various reasons, one being complexity and another
one inconsistency, i.e. new programs can be added to parts of the graph
at any time, so there's no guaranteed consistent state for the time of
such a walk.
Now, the program array pinning itself works, but the issue is that each
derived file descriptor on close would nevertheless call unconditionally
into bpf_fd_array_map_clear(). Instead, keep track of users and postpone
this flush until the last reference to a user is dropped. As this only
concerns a subset of references (f.e. a prog array could hold a program
that itself has reference on the prog array holding it, etc), we need to
track them separately.
Short analysis on the refcounting: on map creation time usercnt will be
one, so there's no change in behaviour for bpf_map_release(), if unpinned.
If we already fail in map_create(), we are immediately freed, and no
file descriptor has been made public yet. In bpf_obj_pin_user(), we need
to probe for a possible map in bpf_fd_probe_obj() already with a usercnt
reference, so before we drop the reference on the fd with fdput().
Therefore, if actual pinning fails, we need to drop that reference again
in bpf_any_put(), otherwise we keep holding it. When last reference
drops on the inode, the bpf_any_put() in bpf_evict_inode() will take
care of dropping the usercnt again. In the bpf_obj_get_user() case, the
bpf_any_get() will grab a reference on the usercnt, still at a time when
we have the reference on the path. Should we later on fail to grab a new
file descriptor, bpf_any_put() will drop it, otherwise we hold it until
bpf_map_release() time.
Joint work with Alexei.
Fixes: b2197755b263 ("bpf: add support for persistent maps/progs")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-11-25 03:28:15 +07:00
|
|
|
bpf_map_put_with_uref(raw);
|
2015-10-29 20:58:09 +07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type)
|
|
|
|
{
|
|
|
|
void *raw;
|
|
|
|
|
|
|
|
*type = BPF_TYPE_MAP;
|
bpf: fix clearing on persistent program array maps
Currently, when having map file descriptors pointing to program arrays,
there's still the issue that we unconditionally flush program array
contents via bpf_fd_array_map_clear() in bpf_map_release(). This happens
when such a file descriptor is released and is independent of the map's
refcount.
Having this flush independent of the refcount is for a reason: there
can be arbitrary complex dependency chains among tail calls, also circular
ones (direct or indirect, nesting limit determined during runtime), and
we need to make sure that the map drops all references to eBPF programs
it holds, so that the map's refcount can eventually drop to zero and
initiate its freeing. Btw, a walk of the whole dependency graph would
not be possible for various reasons, one being complexity and another
one inconsistency, i.e. new programs can be added to parts of the graph
at any time, so there's no guaranteed consistent state for the time of
such a walk.
Now, the program array pinning itself works, but the issue is that each
derived file descriptor on close would nevertheless call unconditionally
into bpf_fd_array_map_clear(). Instead, keep track of users and postpone
this flush until the last reference to a user is dropped. As this only
concerns a subset of references (f.e. a prog array could hold a program
that itself has reference on the prog array holding it, etc), we need to
track them separately.
Short analysis on the refcounting: on map creation time usercnt will be
one, so there's no change in behaviour for bpf_map_release(), if unpinned.
If we already fail in map_create(), we are immediately freed, and no
file descriptor has been made public yet. In bpf_obj_pin_user(), we need
to probe for a possible map in bpf_fd_probe_obj() already with a usercnt
reference, so before we drop the reference on the fd with fdput().
Therefore, if actual pinning fails, we need to drop that reference again
in bpf_any_put(), otherwise we keep holding it. When last reference
drops on the inode, the bpf_any_put() in bpf_evict_inode() will take
care of dropping the usercnt again. In the bpf_obj_get_user() case, the
bpf_any_get() will grab a reference on the usercnt, still at a time when
we have the reference on the path. Should we later on fail to grab a new
file descriptor, bpf_any_put() will drop it, otherwise we hold it until
bpf_map_release() time.
Joint work with Alexei.
Fixes: b2197755b263 ("bpf: add support for persistent maps/progs")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-11-25 03:28:15 +07:00
|
|
|
raw = bpf_map_get_with_uref(ufd);
|
2015-10-29 20:58:09 +07:00
|
|
|
if (IS_ERR(raw)) {
|
|
|
|
*type = BPF_TYPE_PROG;
|
|
|
|
raw = bpf_prog_get(ufd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct inode_operations bpf_dir_iops;
|
|
|
|
|
|
|
|
static const struct inode_operations bpf_prog_iops = { };
|
|
|
|
static const struct inode_operations bpf_map_iops = { };
|
|
|
|
|
|
|
|
static struct inode *bpf_get_inode(struct super_block *sb,
|
|
|
|
const struct inode *dir,
|
|
|
|
umode_t mode)
|
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
switch (mode & S_IFMT) {
|
|
|
|
case S_IFDIR:
|
|
|
|
case S_IFREG:
|
bpf, inode: add support for symlinks and fix mtime/ctime
While commit bb35a6ef7da4 ("bpf, inode: allow for rename and link ops")
added support for hard links that can be used for prog and map nodes,
this work adds simple symlink support, which can be used f.e. for
directories also when unpriviledged and works with cmdline tooling that
understands S_IFLNK anyway. Since the switch in e27f4a942a0e ("bpf: Use
mount_nodev not mount_ns to mount the bpf filesystem"), there can be
various mount instances with mount_nodev() and thus hierarchy can be
flattened to facilitate object sharing. Thus, we can keep bpf tooling
also working by repointing paths.
Most of the functionality can be used from vfs library operations. The
symlink is stored in the inode itself, that is in i_link, which is
sufficient in our case as opposed to storing it in the page cache.
While at it, I noticed that bpf_mkdir() and bpf_mkobj() don't update
the directories mtime and ctime, so add a common helper for it called
bpf_dentry_finalize() that takes care of it for all cases now.
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-10-29 07:30:46 +07:00
|
|
|
case S_IFLNK:
|
2015-10-29 20:58:09 +07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
inode = new_inode(sb);
|
|
|
|
if (!inode)
|
|
|
|
return ERR_PTR(-ENOSPC);
|
|
|
|
|
|
|
|
inode->i_ino = get_next_ino();
|
2016-09-14 21:48:04 +07:00
|
|
|
inode->i_atime = current_time(inode);
|
2015-10-29 20:58:09 +07:00
|
|
|
inode->i_mtime = inode->i_atime;
|
|
|
|
inode->i_ctime = inode->i_atime;
|
|
|
|
|
|
|
|
inode_init_owner(inode, dir, mode);
|
|
|
|
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bpf_inode_type(const struct inode *inode, enum bpf_type *type)
|
|
|
|
{
|
|
|
|
*type = BPF_TYPE_UNSPEC;
|
|
|
|
if (inode->i_op == &bpf_prog_iops)
|
|
|
|
*type = BPF_TYPE_PROG;
|
|
|
|
else if (inode->i_op == &bpf_map_iops)
|
|
|
|
*type = BPF_TYPE_MAP;
|
|
|
|
else
|
|
|
|
return -EACCES;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
bpf, inode: add support for symlinks and fix mtime/ctime
While commit bb35a6ef7da4 ("bpf, inode: allow for rename and link ops")
added support for hard links that can be used for prog and map nodes,
this work adds simple symlink support, which can be used f.e. for
directories also when unpriviledged and works with cmdline tooling that
understands S_IFLNK anyway. Since the switch in e27f4a942a0e ("bpf: Use
mount_nodev not mount_ns to mount the bpf filesystem"), there can be
various mount instances with mount_nodev() and thus hierarchy can be
flattened to facilitate object sharing. Thus, we can keep bpf tooling
also working by repointing paths.
Most of the functionality can be used from vfs library operations. The
symlink is stored in the inode itself, that is in i_link, which is
sufficient in our case as opposed to storing it in the page cache.
While at it, I noticed that bpf_mkdir() and bpf_mkobj() don't update
the directories mtime and ctime, so add a common helper for it called
bpf_dentry_finalize() that takes care of it for all cases now.
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-10-29 07:30:46 +07:00
|
|
|
static void bpf_dentry_finalize(struct dentry *dentry, struct inode *inode,
|
|
|
|
struct inode *dir)
|
|
|
|
{
|
|
|
|
d_instantiate(dentry, inode);
|
|
|
|
dget(dentry);
|
|
|
|
|
|
|
|
dir->i_mtime = current_time(dir);
|
|
|
|
dir->i_ctime = dir->i_mtime;
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:58:09 +07:00
|
|
|
static int bpf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
|
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFDIR);
|
|
|
|
if (IS_ERR(inode))
|
|
|
|
return PTR_ERR(inode);
|
|
|
|
|
|
|
|
inode->i_op = &bpf_dir_iops;
|
|
|
|
inode->i_fop = &simple_dir_operations;
|
|
|
|
|
|
|
|
inc_nlink(inode);
|
|
|
|
inc_nlink(dir);
|
|
|
|
|
bpf, inode: add support for symlinks and fix mtime/ctime
While commit bb35a6ef7da4 ("bpf, inode: allow for rename and link ops")
added support for hard links that can be used for prog and map nodes,
this work adds simple symlink support, which can be used f.e. for
directories also when unpriviledged and works with cmdline tooling that
understands S_IFLNK anyway. Since the switch in e27f4a942a0e ("bpf: Use
mount_nodev not mount_ns to mount the bpf filesystem"), there can be
various mount instances with mount_nodev() and thus hierarchy can be
flattened to facilitate object sharing. Thus, we can keep bpf tooling
also working by repointing paths.
Most of the functionality can be used from vfs library operations. The
symlink is stored in the inode itself, that is in i_link, which is
sufficient in our case as opposed to storing it in the page cache.
While at it, I noticed that bpf_mkdir() and bpf_mkobj() don't update
the directories mtime and ctime, so add a common helper for it called
bpf_dentry_finalize() that takes care of it for all cases now.
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-10-29 07:30:46 +07:00
|
|
|
bpf_dentry_finalize(dentry, inode, dir);
|
2015-10-29 20:58:09 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-19 05:56:03 +07:00
|
|
|
struct map_iter {
|
|
|
|
void *key;
|
|
|
|
bool done;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct map_iter *map_iter(struct seq_file *m)
|
|
|
|
{
|
|
|
|
return m->private;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct bpf_map *seq_file_to_map(struct seq_file *m)
|
|
|
|
{
|
|
|
|
return file_inode(m->file)->i_private;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void map_iter_free(struct map_iter *iter)
|
|
|
|
{
|
|
|
|
if (iter) {
|
|
|
|
kfree(iter->key);
|
|
|
|
kfree(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct map_iter *map_iter_alloc(struct bpf_map *map)
|
|
|
|
{
|
|
|
|
struct map_iter *iter;
|
|
|
|
|
|
|
|
iter = kzalloc(sizeof(*iter), GFP_KERNEL | __GFP_NOWARN);
|
|
|
|
if (!iter)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
iter->key = kzalloc(map->key_size, GFP_KERNEL | __GFP_NOWARN);
|
|
|
|
if (!iter->key)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return iter;
|
|
|
|
|
|
|
|
error:
|
|
|
|
map_iter_free(iter);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
|
|
|
|
{
|
|
|
|
struct bpf_map *map = seq_file_to_map(m);
|
|
|
|
void *key = map_iter(m)->key;
|
2018-08-09 22:55:19 +07:00
|
|
|
void *prev_key;
|
2018-04-19 05:56:03 +07:00
|
|
|
|
|
|
|
if (map_iter(m)->done)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (unlikely(v == SEQ_START_TOKEN))
|
2018-08-09 22:55:19 +07:00
|
|
|
prev_key = NULL;
|
|
|
|
else
|
|
|
|
prev_key = key;
|
2018-04-19 05:56:03 +07:00
|
|
|
|
2018-08-09 22:55:19 +07:00
|
|
|
if (map->ops->map_get_next_key(map, prev_key, key)) {
|
2018-04-19 05:56:03 +07:00
|
|
|
map_iter(m)->done = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
++(*pos);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *map_seq_start(struct seq_file *m, loff_t *pos)
|
|
|
|
{
|
|
|
|
if (map_iter(m)->done)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return *pos ? map_iter(m)->key : SEQ_START_TOKEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void map_seq_stop(struct seq_file *m, void *v)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static int map_seq_show(struct seq_file *m, void *v)
|
|
|
|
{
|
|
|
|
struct bpf_map *map = seq_file_to_map(m);
|
|
|
|
void *key = map_iter(m)->key;
|
|
|
|
|
|
|
|
if (unlikely(v == SEQ_START_TOKEN)) {
|
|
|
|
seq_puts(m, "# WARNING!! The output is for debug purpose only\n");
|
|
|
|
seq_puts(m, "# WARNING!! The output format will change\n");
|
|
|
|
} else {
|
|
|
|
map->ops->map_seq_show_elem(map, key, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct seq_operations bpffs_map_seq_ops = {
|
|
|
|
.start = map_seq_start,
|
|
|
|
.next = map_seq_next,
|
|
|
|
.show = map_seq_show,
|
|
|
|
.stop = map_seq_stop,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int bpffs_map_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct bpf_map *map = inode->i_private;
|
|
|
|
struct map_iter *iter;
|
|
|
|
struct seq_file *m;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
iter = map_iter_alloc(map);
|
|
|
|
if (!iter)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
err = seq_open(file, &bpffs_map_seq_ops);
|
|
|
|
if (err) {
|
|
|
|
map_iter_free(iter);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
m = file->private_data;
|
|
|
|
m->private = iter;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bpffs_map_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct seq_file *m = file->private_data;
|
|
|
|
|
|
|
|
map_iter_free(map_iter(m));
|
|
|
|
|
|
|
|
return seq_release(inode, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bpffs_map_fops should only implement the basic
|
|
|
|
* read operation for a BPF map. The purpose is to
|
|
|
|
* provide a simple user intuitive way to do
|
|
|
|
* "cat bpffs/pathto/a-pinned-map".
|
|
|
|
*
|
|
|
|
* Other operations (e.g. write, lookup...) should be realized by
|
|
|
|
* the userspace tools (e.g. bpftool) through the
|
|
|
|
* BPF_OBJ_GET_INFO_BY_FD and the map's lookup/update
|
|
|
|
* interface.
|
|
|
|
*/
|
|
|
|
static const struct file_operations bpffs_map_fops = {
|
|
|
|
.open = bpffs_map_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.release = bpffs_map_release,
|
|
|
|
};
|
|
|
|
|
2018-06-08 23:10:34 +07:00
|
|
|
static int bpffs_obj_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations bpffs_obj_fops = {
|
|
|
|
.open = bpffs_obj_open,
|
|
|
|
};
|
|
|
|
|
2017-12-02 05:22:19 +07:00
|
|
|
static int bpf_mkobj_ops(struct dentry *dentry, umode_t mode, void *raw,
|
2018-04-19 05:56:03 +07:00
|
|
|
const struct inode_operations *iops,
|
|
|
|
const struct file_operations *fops)
|
2015-10-29 20:58:09 +07:00
|
|
|
{
|
2017-12-02 05:22:19 +07:00
|
|
|
struct inode *dir = dentry->d_parent->d_inode;
|
|
|
|
struct inode *inode = bpf_get_inode(dir->i_sb, dir, mode);
|
2015-10-29 20:58:09 +07:00
|
|
|
if (IS_ERR(inode))
|
|
|
|
return PTR_ERR(inode);
|
|
|
|
|
|
|
|
inode->i_op = iops;
|
2018-04-19 05:56:03 +07:00
|
|
|
inode->i_fop = fops;
|
2017-12-02 05:22:19 +07:00
|
|
|
inode->i_private = raw;
|
2015-10-29 20:58:09 +07:00
|
|
|
|
bpf, inode: add support for symlinks and fix mtime/ctime
While commit bb35a6ef7da4 ("bpf, inode: allow for rename and link ops")
added support for hard links that can be used for prog and map nodes,
this work adds simple symlink support, which can be used f.e. for
directories also when unpriviledged and works with cmdline tooling that
understands S_IFLNK anyway. Since the switch in e27f4a942a0e ("bpf: Use
mount_nodev not mount_ns to mount the bpf filesystem"), there can be
various mount instances with mount_nodev() and thus hierarchy can be
flattened to facilitate object sharing. Thus, we can keep bpf tooling
also working by repointing paths.
Most of the functionality can be used from vfs library operations. The
symlink is stored in the inode itself, that is in i_link, which is
sufficient in our case as opposed to storing it in the page cache.
While at it, I noticed that bpf_mkdir() and bpf_mkobj() don't update
the directories mtime and ctime, so add a common helper for it called
bpf_dentry_finalize() that takes care of it for all cases now.
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-10-29 07:30:46 +07:00
|
|
|
bpf_dentry_finalize(dentry, inode, dir);
|
2015-10-29 20:58:09 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-02 05:22:19 +07:00
|
|
|
static int bpf_mkprog(struct dentry *dentry, umode_t mode, void *arg)
|
2015-10-29 20:58:09 +07:00
|
|
|
{
|
2018-06-08 23:10:34 +07:00
|
|
|
return bpf_mkobj_ops(dentry, mode, arg, &bpf_prog_iops,
|
|
|
|
&bpffs_obj_fops);
|
2017-12-02 05:22:19 +07:00
|
|
|
}
|
2015-10-29 20:58:09 +07:00
|
|
|
|
2017-12-02 05:22:19 +07:00
|
|
|
static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
|
|
|
|
{
|
2018-04-19 05:56:03 +07:00
|
|
|
struct bpf_map *map = arg;
|
|
|
|
|
|
|
|
return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops,
|
2018-08-12 06:59:17 +07:00
|
|
|
bpf_map_support_seq_show(map) ?
|
|
|
|
&bpffs_map_fops : &bpffs_obj_fops);
|
2015-10-29 20:58:09 +07:00
|
|
|
}
|
|
|
|
|
2016-03-25 23:06:51 +07:00
|
|
|
static struct dentry *
|
|
|
|
bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags)
|
2015-12-11 04:33:49 +07:00
|
|
|
{
|
2018-03-09 14:46:33 +07:00
|
|
|
/* Dots in names (e.g. "/sys/fs/bpf/foo.bar") are reserved for future
|
|
|
|
* extensions.
|
|
|
|
*/
|
2016-03-25 23:06:51 +07:00
|
|
|
if (strchr(dentry->d_name.name, '.'))
|
|
|
|
return ERR_PTR(-EPERM);
|
bpf, inode: add support for symlinks and fix mtime/ctime
While commit bb35a6ef7da4 ("bpf, inode: allow for rename and link ops")
added support for hard links that can be used for prog and map nodes,
this work adds simple symlink support, which can be used f.e. for
directories also when unpriviledged and works with cmdline tooling that
understands S_IFLNK anyway. Since the switch in e27f4a942a0e ("bpf: Use
mount_nodev not mount_ns to mount the bpf filesystem"), there can be
various mount instances with mount_nodev() and thus hierarchy can be
flattened to facilitate object sharing. Thus, we can keep bpf tooling
also working by repointing paths.
Most of the functionality can be used from vfs library operations. The
symlink is stored in the inode itself, that is in i_link, which is
sufficient in our case as opposed to storing it in the page cache.
While at it, I noticed that bpf_mkdir() and bpf_mkobj() don't update
the directories mtime and ctime, so add a common helper for it called
bpf_dentry_finalize() that takes care of it for all cases now.
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-10-29 07:30:46 +07:00
|
|
|
|
2016-03-25 23:06:51 +07:00
|
|
|
return simple_lookup(dir, dentry, flags);
|
2015-12-11 04:33:49 +07:00
|
|
|
}
|
|
|
|
|
bpf, inode: add support for symlinks and fix mtime/ctime
While commit bb35a6ef7da4 ("bpf, inode: allow for rename and link ops")
added support for hard links that can be used for prog and map nodes,
this work adds simple symlink support, which can be used f.e. for
directories also when unpriviledged and works with cmdline tooling that
understands S_IFLNK anyway. Since the switch in e27f4a942a0e ("bpf: Use
mount_nodev not mount_ns to mount the bpf filesystem"), there can be
various mount instances with mount_nodev() and thus hierarchy can be
flattened to facilitate object sharing. Thus, we can keep bpf tooling
also working by repointing paths.
Most of the functionality can be used from vfs library operations. The
symlink is stored in the inode itself, that is in i_link, which is
sufficient in our case as opposed to storing it in the page cache.
While at it, I noticed that bpf_mkdir() and bpf_mkobj() don't update
the directories mtime and ctime, so add a common helper for it called
bpf_dentry_finalize() that takes care of it for all cases now.
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-10-29 07:30:46 +07:00
|
|
|
static int bpf_symlink(struct inode *dir, struct dentry *dentry,
|
|
|
|
const char *target)
|
|
|
|
{
|
|
|
|
char *link = kstrdup(target, GFP_USER | __GFP_NOWARN);
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
if (!link)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
inode = bpf_get_inode(dir->i_sb, dir, S_IRWXUGO | S_IFLNK);
|
|
|
|
if (IS_ERR(inode)) {
|
|
|
|
kfree(link);
|
|
|
|
return PTR_ERR(inode);
|
|
|
|
}
|
|
|
|
|
|
|
|
inode->i_op = &simple_symlink_inode_operations;
|
|
|
|
inode->i_link = link;
|
|
|
|
|
|
|
|
bpf_dentry_finalize(dentry, inode, dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:58:09 +07:00
|
|
|
static const struct inode_operations bpf_dir_iops = {
|
2016-03-25 23:06:51 +07:00
|
|
|
.lookup = bpf_lookup,
|
2015-10-29 20:58:09 +07:00
|
|
|
.mkdir = bpf_mkdir,
|
bpf, inode: add support for symlinks and fix mtime/ctime
While commit bb35a6ef7da4 ("bpf, inode: allow for rename and link ops")
added support for hard links that can be used for prog and map nodes,
this work adds simple symlink support, which can be used f.e. for
directories also when unpriviledged and works with cmdline tooling that
understands S_IFLNK anyway. Since the switch in e27f4a942a0e ("bpf: Use
mount_nodev not mount_ns to mount the bpf filesystem"), there can be
various mount instances with mount_nodev() and thus hierarchy can be
flattened to facilitate object sharing. Thus, we can keep bpf tooling
also working by repointing paths.
Most of the functionality can be used from vfs library operations. The
symlink is stored in the inode itself, that is in i_link, which is
sufficient in our case as opposed to storing it in the page cache.
While at it, I noticed that bpf_mkdir() and bpf_mkobj() don't update
the directories mtime and ctime, so add a common helper for it called
bpf_dentry_finalize() that takes care of it for all cases now.
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-10-29 07:30:46 +07:00
|
|
|
.symlink = bpf_symlink,
|
2015-10-29 20:58:09 +07:00
|
|
|
.rmdir = simple_rmdir,
|
2016-03-25 23:06:51 +07:00
|
|
|
.rename = simple_rename,
|
|
|
|
.link = simple_link,
|
2015-10-29 20:58:09 +07:00
|
|
|
.unlink = simple_unlink,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int bpf_obj_do_pin(const struct filename *pathname, void *raw,
|
|
|
|
enum bpf_type type)
|
|
|
|
{
|
|
|
|
struct dentry *dentry;
|
|
|
|
struct inode *dir;
|
|
|
|
struct path path;
|
|
|
|
umode_t mode;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
dentry = kern_path_create(AT_FDCWD, pathname->name, &path, 0);
|
|
|
|
if (IS_ERR(dentry))
|
|
|
|
return PTR_ERR(dentry);
|
|
|
|
|
|
|
|
mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
|
|
|
|
|
2017-12-02 05:22:19 +07:00
|
|
|
ret = security_path_mknod(&path, dentry, mode, 0);
|
2015-10-29 20:58:09 +07:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
dir = d_inode(path.dentry);
|
|
|
|
if (dir->i_op != &bpf_dir_iops) {
|
|
|
|
ret = -EPERM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-12-02 05:22:19 +07:00
|
|
|
switch (type) {
|
|
|
|
case BPF_TYPE_PROG:
|
|
|
|
ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw);
|
|
|
|
break;
|
|
|
|
case BPF_TYPE_MAP:
|
|
|
|
ret = vfs_mkobj(dentry, mode, bpf_mkmap, raw);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EPERM;
|
|
|
|
}
|
2015-10-29 20:58:09 +07:00
|
|
|
out:
|
|
|
|
done_path_create(&path, dentry);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
|
|
|
|
{
|
|
|
|
struct filename *pname;
|
|
|
|
enum bpf_type type;
|
|
|
|
void *raw;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pname = getname(pathname);
|
|
|
|
if (IS_ERR(pname))
|
|
|
|
return PTR_ERR(pname);
|
|
|
|
|
|
|
|
raw = bpf_fd_probe_obj(ufd, &type);
|
|
|
|
if (IS_ERR(raw)) {
|
|
|
|
ret = PTR_ERR(raw);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = bpf_obj_do_pin(pname, raw, type);
|
|
|
|
if (ret != 0)
|
|
|
|
bpf_any_put(raw, type);
|
|
|
|
out:
|
|
|
|
putname(pname);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *bpf_obj_do_get(const struct filename *pathname,
|
2017-10-19 03:00:22 +07:00
|
|
|
enum bpf_type *type, int flags)
|
2015-10-29 20:58:09 +07:00
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
struct path path;
|
|
|
|
void *raw;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = kern_path(pathname->name, LOOKUP_FOLLOW, &path);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
|
|
|
inode = d_backing_inode(path.dentry);
|
2017-10-19 03:00:22 +07:00
|
|
|
ret = inode_permission(inode, ACC_MODE(flags));
|
2015-10-29 20:58:09 +07:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = bpf_inode_type(inode, type);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
raw = bpf_any_get(inode->i_private, *type);
|
2016-04-28 08:56:20 +07:00
|
|
|
if (!IS_ERR(raw))
|
|
|
|
touch_atime(&path);
|
2015-10-29 20:58:09 +07:00
|
|
|
|
|
|
|
path_put(&path);
|
|
|
|
return raw;
|
|
|
|
out:
|
|
|
|
path_put(&path);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
2017-10-19 03:00:22 +07:00
|
|
|
int bpf_obj_get_user(const char __user *pathname, int flags)
|
2015-10-29 20:58:09 +07:00
|
|
|
{
|
|
|
|
enum bpf_type type = BPF_TYPE_UNSPEC;
|
|
|
|
struct filename *pname;
|
|
|
|
int ret = -ENOENT;
|
2017-10-19 03:00:22 +07:00
|
|
|
int f_flags;
|
2015-10-29 20:58:09 +07:00
|
|
|
void *raw;
|
|
|
|
|
2017-10-19 03:00:22 +07:00
|
|
|
f_flags = bpf_get_file_flag(flags);
|
|
|
|
if (f_flags < 0)
|
|
|
|
return f_flags;
|
|
|
|
|
2015-10-29 20:58:09 +07:00
|
|
|
pname = getname(pathname);
|
|
|
|
if (IS_ERR(pname))
|
|
|
|
return PTR_ERR(pname);
|
|
|
|
|
2017-10-19 03:00:22 +07:00
|
|
|
raw = bpf_obj_do_get(pname, &type, f_flags);
|
2015-10-29 20:58:09 +07:00
|
|
|
if (IS_ERR(raw)) {
|
|
|
|
ret = PTR_ERR(raw);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == BPF_TYPE_PROG)
|
|
|
|
ret = bpf_prog_new_fd(raw);
|
|
|
|
else if (type == BPF_TYPE_MAP)
|
2017-10-19 03:00:22 +07:00
|
|
|
ret = bpf_map_new_fd(raw, f_flags);
|
2015-10-29 20:58:09 +07:00
|
|
|
else
|
|
|
|
goto out;
|
|
|
|
|
2018-04-29 09:56:37 +07:00
|
|
|
if (ret < 0)
|
2015-10-29 20:58:09 +07:00
|
|
|
bpf_any_put(raw, type);
|
|
|
|
out:
|
|
|
|
putname(pname);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-12-03 08:20:38 +07:00
|
|
|
|
|
|
|
static struct bpf_prog *__get_prog_inode(struct inode *inode, enum bpf_prog_type type)
|
|
|
|
{
|
|
|
|
struct bpf_prog *prog;
|
2019-05-15 09:42:57 +07:00
|
|
|
int ret = inode_permission(inode, MAY_READ);
|
2017-12-03 08:20:38 +07:00
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
|
|
|
if (inode->i_op == &bpf_map_iops)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
if (inode->i_op != &bpf_prog_iops)
|
|
|
|
return ERR_PTR(-EACCES);
|
|
|
|
|
|
|
|
prog = inode->i_private;
|
|
|
|
|
|
|
|
ret = security_bpf_prog(prog);
|
|
|
|
if (ret < 0)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
|
|
|
if (!bpf_prog_get_ok(prog, &type, false))
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
return bpf_prog_inc(prog);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type)
|
|
|
|
{
|
|
|
|
struct bpf_prog *prog;
|
|
|
|
struct path path;
|
|
|
|
int ret = kern_path(name, LOOKUP_FOLLOW, &path);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
prog = __get_prog_inode(d_backing_inode(path.dentry), type);
|
|
|
|
if (!IS_ERR(prog))
|
|
|
|
touch_atime(&path);
|
|
|
|
path_put(&path);
|
|
|
|
return prog;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(bpf_prog_get_type_path);
|
2015-10-29 20:58:09 +07:00
|
|
|
|
2017-07-05 22:24:49 +07:00
|
|
|
/*
|
|
|
|
* Display the mount options in /proc/mounts.
|
|
|
|
*/
|
|
|
|
static int bpf_show_options(struct seq_file *m, struct dentry *root)
|
|
|
|
{
|
|
|
|
umode_t mode = d_inode(root)->i_mode & S_IALLUGO & ~S_ISVTX;
|
|
|
|
|
|
|
|
if (mode != S_IRWXUGO)
|
|
|
|
seq_printf(m, ",mode=%o", mode);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-16 09:31:29 +07:00
|
|
|
static void bpf_free_inode(struct inode *inode)
|
2019-03-25 21:54:43 +07:00
|
|
|
{
|
|
|
|
enum bpf_type type;
|
|
|
|
|
|
|
|
if (S_ISLNK(inode->i_mode))
|
|
|
|
kfree(inode->i_link);
|
|
|
|
if (!bpf_inode_type(inode, &type))
|
|
|
|
bpf_any_put(inode->i_private, type);
|
|
|
|
free_inode_nonrcu(inode);
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:58:09 +07:00
|
|
|
static const struct super_operations bpf_super_ops = {
|
|
|
|
.statfs = simple_statfs,
|
|
|
|
.drop_inode = generic_delete_inode,
|
2017-07-05 22:24:49 +07:00
|
|
|
.show_options = bpf_show_options,
|
2019-04-16 09:31:29 +07:00
|
|
|
.free_inode = bpf_free_inode,
|
2015-10-29 20:58:09 +07:00
|
|
|
};
|
|
|
|
|
2016-11-26 07:28:08 +07:00
|
|
|
enum {
|
|
|
|
OPT_MODE,
|
|
|
|
OPT_ERR,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const match_table_t bpf_mount_tokens = {
|
|
|
|
{ OPT_MODE, "mode=%o" },
|
|
|
|
{ OPT_ERR, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
struct bpf_mount_opts {
|
|
|
|
umode_t mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int bpf_parse_options(char *data, struct bpf_mount_opts *opts)
|
|
|
|
{
|
|
|
|
substring_t args[MAX_OPT_ARGS];
|
|
|
|
int option, token;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
opts->mode = S_IRWXUGO;
|
|
|
|
|
|
|
|
while ((ptr = strsep(&data, ",")) != NULL) {
|
|
|
|
if (!*ptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
token = match_token(ptr, bpf_mount_tokens, args);
|
|
|
|
switch (token) {
|
|
|
|
case OPT_MODE:
|
|
|
|
if (match_octal(&args[0], &option))
|
|
|
|
return -EINVAL;
|
|
|
|
opts->mode = option & S_IALLUGO;
|
|
|
|
break;
|
|
|
|
/* We might like to report bad mount options here, but
|
|
|
|
* traditionally we've ignored all mount options, so we'd
|
|
|
|
* better continue to ignore non-existing options for bpf.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:58:09 +07:00
|
|
|
static int bpf_fill_super(struct super_block *sb, void *data, int silent)
|
|
|
|
{
|
2017-03-26 11:15:37 +07:00
|
|
|
static const struct tree_descr bpf_rfiles[] = { { "" } };
|
2016-11-26 07:28:08 +07:00
|
|
|
struct bpf_mount_opts opts;
|
2015-10-29 20:58:09 +07:00
|
|
|
struct inode *inode;
|
|
|
|
int ret;
|
|
|
|
|
2016-11-26 07:28:08 +07:00
|
|
|
ret = bpf_parse_options(data, &opts);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-10-29 20:58:09 +07:00
|
|
|
ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
sb->s_op = &bpf_super_ops;
|
|
|
|
|
|
|
|
inode = sb->s_root->d_inode;
|
|
|
|
inode->i_op = &bpf_dir_iops;
|
|
|
|
inode->i_mode &= ~S_IALLUGO;
|
2016-11-26 07:28:08 +07:00
|
|
|
inode->i_mode |= S_ISVTX | opts.mode;
|
2015-10-29 20:58:09 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct dentry *bpf_mount(struct file_system_type *type, int flags,
|
|
|
|
const char *dev_name, void *data)
|
|
|
|
{
|
2016-05-21 05:22:48 +07:00
|
|
|
return mount_nodev(type, flags, data, bpf_fill_super);
|
2015-10-29 20:58:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct file_system_type bpf_fs_type = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "bpf",
|
|
|
|
.mount = bpf_mount,
|
|
|
|
.kill_sb = kill_litter_super,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init bpf_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sysfs_create_mount_point(fs_kobj, "bpf");
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = register_filesystem(&bpf_fs_type);
|
|
|
|
if (ret)
|
|
|
|
sysfs_remove_mount_point(fs_kobj, "bpf");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
fs_initcall(bpf_init);
|