[RFC,bpf-next,2/8] bpf: allow bpf_link pinning as read-only and enforce LINK_UPDATE

Message ID 20200404000948.3980903-3-andriin@fb.com
State New
Headers show
Series
  • bpf_link observability APIs
Related show

Commit Message

Andrii Nakryiko April 4, 2020, 12:09 a.m.
Make it possible to pin bpf_link as read-only to control whether LINK_UPDATE
operation is allowed or not. bpf_links provided through read-only files are
not allowed to perform LINK_UPDATE operations, which this patch starts
enforcing. bpf_map and bpf_prog are still always treated as read-write ones,
just like before.

This is a critical property for bpf_links and is going to be relied upon for
BPF_LINK_GET_FD_BY_ID operation implemented later in the series. GET_FD_BY_ID
will only return read-only links to prevent processes that do not "own"
bpf_link from updating underlying bpf_prog.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 include/linux/bpf.h  |  6 +++---
 kernel/bpf/inode.c   | 30 ++++++++++++++++++++++--------
 kernel/bpf/syscall.c | 26 +++++++++++++++++++-------
 3 files changed, 44 insertions(+), 18 deletions(-)

Patch

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index ea65c3165e4c..3474f8e34a63 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1103,11 +1103,11 @@  void bpf_link_cleanup(struct bpf_link *link, struct file *link_file,
 		      int link_fd);
 void bpf_link_inc(struct bpf_link *link);
 void bpf_link_put(struct bpf_link *link);
-int bpf_link_new_fd(struct bpf_link *link);
+int bpf_link_new_fd(struct bpf_link *link, int flags);
 struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
-struct bpf_link *bpf_link_get_from_fd(u32 ufd);
+struct bpf_link *bpf_link_get_from_fd(u32 ufd, fmode_t *link_mode);
 
-int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
+int bpf_obj_pin_user(u32 ufd, const char __user *pathname, int file_flags);
 int bpf_obj_get_user(const char __user *pathname, int flags);
 
 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
index 95087d9f4ed3..3fd71c1e3c33 100644
--- a/kernel/bpf/inode.c
+++ b/kernel/bpf/inode.c
@@ -66,23 +66,25 @@  static void bpf_any_put(void *raw, enum bpf_type type)
 	}
 }
 
-static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type)
+static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type, fmode_t *file_mode)
 {
 	void *raw;
 
 	raw = bpf_map_get_with_uref(ufd);
 	if (!IS_ERR(raw)) {
 		*type = BPF_TYPE_MAP;
+		*file_mode = O_RDWR;
 		return raw;
 	}
 
 	raw = bpf_prog_get(ufd);
 	if (!IS_ERR(raw)) {
 		*type = BPF_TYPE_PROG;
+		*file_mode = O_RDWR;
 		return raw;
 	}
 
-	raw = bpf_link_get_from_fd(ufd);
+	raw = bpf_link_get_from_fd(ufd, file_mode);
 	if (!IS_ERR(raw)) {
 		*type = BPF_TYPE_LINK;
 		return raw;
@@ -407,7 +409,7 @@  static const struct inode_operations bpf_dir_iops = {
 };
 
 static int bpf_obj_do_pin(const char __user *pathname, void *raw,
-			  enum bpf_type type)
+			  enum bpf_type type, fmode_t file_mode)
 {
 	struct dentry *dentry;
 	struct inode *dir;
@@ -419,7 +421,7 @@  static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 	if (IS_ERR(dentry))
 		return PTR_ERR(dentry);
 
-	mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
+	mode = S_IFREG | (ACC_MODE(file_mode) & ~current_umask());
 
 	ret = security_path_mknod(&path, dentry, mode, 0);
 	if (ret)
@@ -449,17 +451,29 @@  static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 	return ret;
 }
 
-int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
+int bpf_obj_pin_user(u32 ufd, const char __user *pathname, int file_flags)
 {
 	enum bpf_type type;
+	fmode_t file_mode;
 	void *raw;
 	int ret;
 
-	raw = bpf_fd_probe_obj(ufd, &type);
+	raw = bpf_fd_probe_obj(ufd, &type, &file_mode);
 	if (IS_ERR(raw))
 		return PTR_ERR(raw);
 
-	ret = bpf_obj_do_pin(pathname, raw, type);
+	if ((type == BPF_TYPE_MAP || type == BPF_TYPE_PROG) && file_flags)
+		return -EINVAL;
+
+	/* requested pinned file mode has to be a valid subset */
+	if (!file_flags) {
+		file_flags = file_mode;
+	} else if ((file_mode & file_flags) != file_flags) {
+		bpf_any_put(raw, type);
+		return -EPERM;
+	}
+
+	ret = bpf_obj_do_pin(pathname, raw, type, file_flags);
 	if (ret != 0)
 		bpf_any_put(raw, type);
 
@@ -518,7 +532,7 @@  int bpf_obj_get_user(const char __user *pathname, int flags)
 	else if (type == BPF_TYPE_MAP)
 		ret = bpf_map_new_fd(raw, f_flags);
 	else if (type == BPF_TYPE_LINK)
-		ret = bpf_link_new_fd(raw);
+		ret = bpf_link_new_fd(raw, f_flags);
 	else
 		return -ENOENT;
 
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 40993d8c936e..47f323901ed9 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2167,10 +2167,11 @@  static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
 
 static int bpf_obj_pin(const union bpf_attr *attr)
 {
-	if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
+	if (CHECK_ATTR(BPF_OBJ))
 		return -EINVAL;
 
-	return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
+	return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname),
+				attr->file_flags);
 }
 
 static int bpf_obj_get(const union bpf_attr *attr)
@@ -2294,9 +2295,10 @@  const struct file_operations bpf_link_fops = {
 	.write		= bpf_dummy_write,
 };
 
-int bpf_link_new_fd(struct bpf_link *link)
+int bpf_link_new_fd(struct bpf_link *link, int flags)
 {
-	return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
+	return anon_inode_getfd("bpf-link", &bpf_link_fops, link,
+				flags | O_CLOEXEC);
 }
 
 /* Similar to bpf_link_new_fd, create anon_inode for given bpf_link, but
@@ -2316,7 +2318,8 @@  struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd)
 	if (fd < 0)
 		return ERR_PTR(fd);
 
-	file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
+	file = anon_inode_getfile("bpf_link", &bpf_link_fops, link,
+				  O_RDWR | O_CLOEXEC);
 	if (IS_ERR(file)) {
 		put_unused_fd(fd);
 		return file;
@@ -2326,7 +2329,7 @@  struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd)
 	return file;
 }
 
-struct bpf_link *bpf_link_get_from_fd(u32 ufd)
+struct bpf_link *bpf_link_get_from_fd(u32 ufd, fmode_t *link_mode)
 {
 	struct fd f = fdget(ufd);
 	struct bpf_link *link;
@@ -2340,6 +2343,8 @@  struct bpf_link *bpf_link_get_from_fd(u32 ufd)
 
 	link = f.file->private_data;
 	bpf_link_inc(link);
+	if (link_mode)
+		*link_mode = f.file->f_mode;
 	fdput(f);
 
 	return link;
@@ -3612,6 +3617,7 @@  static int link_update(union bpf_attr *attr)
 {
 	struct bpf_prog *old_prog = NULL, *new_prog;
 	struct bpf_link *link;
+	fmode_t link_mode;
 	u32 flags;
 	int ret;
 
@@ -3625,10 +3631,16 @@  static int link_update(union bpf_attr *attr)
 	if (flags & ~BPF_F_REPLACE)
 		return -EINVAL;
 
-	link = bpf_link_get_from_fd(attr->link_update.link_fd);
+	link = bpf_link_get_from_fd(attr->link_update.link_fd, &link_mode);
 	if (IS_ERR(link))
 		return PTR_ERR(link);
 
+	/* read-only link references are not allowed to perform LINK_UPDATE */
+	if (!(link_mode & O_WRONLY)) {
+		bpf_link_put(link);
+		return -EACCES;
+	}
+
 	new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
 	if (IS_ERR(new_prog))
 		return PTR_ERR(new_prog);