Commit b08c8fc0 authored by Daniel Borkmann's avatar Daniel Borkmann

bpf: Re-support uid and gid when mounting bpffs

For a clean, conflict-free revert of the token-related patches in commit
d17aff80 ("Revert BPF token-related functionality"), the bpf fs commit
750e7857 ("bpf: Support uid and gid when mounting bpffs") was undone
temporarily as well.

This patch manually re-adds the functionality from the original one back
in 750e7857, no other functional changes intended.

Testing:

  # mount -t bpf -o uid=65534,gid=65534 bpffs ./foo
  # ls -la . | grep foo
  drwxrwxrwt   2 nobody nogroup          0 Dec 20 13:16 foo
  # mount -t bpf
  bpffs on /root/foo type bpf (rw,relatime,uid=65534,gid=65534)

Also, passing invalid arguments for uid/gid are properly rejected as expected.

Fixes: d17aff80 ("Revert BPF token-related functionality")
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Reviewed-by: default avatarChristian Brauner <brauner@kernel.org>
Cc: Jie Jiang <jiejiang@chromium.org>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: linux-fsdevel@vger.kernel.org
Link: https://lore.kernel.org/bpf/20231220133805.20953-1-daniel@iogearbox.net
parent fc3a5534
...@@ -599,8 +599,15 @@ EXPORT_SYMBOL(bpf_prog_get_type_path); ...@@ -599,8 +599,15 @@ EXPORT_SYMBOL(bpf_prog_get_type_path);
*/ */
static int bpf_show_options(struct seq_file *m, struct dentry *root) static int bpf_show_options(struct seq_file *m, struct dentry *root)
{ {
umode_t mode = d_inode(root)->i_mode & S_IALLUGO & ~S_ISVTX; struct inode *inode = d_inode(root);
umode_t mode = inode->i_mode & S_IALLUGO & ~S_ISVTX;
if (!uid_eq(inode->i_uid, GLOBAL_ROOT_UID))
seq_printf(m, ",uid=%u",
from_kuid_munged(&init_user_ns, inode->i_uid));
if (!gid_eq(inode->i_gid, GLOBAL_ROOT_GID))
seq_printf(m, ",gid=%u",
from_kgid_munged(&init_user_ns, inode->i_gid));
if (mode != S_IRWXUGO) if (mode != S_IRWXUGO)
seq_printf(m, ",mode=%o", mode); seq_printf(m, ",mode=%o", mode);
return 0; return 0;
...@@ -625,15 +632,21 @@ static const struct super_operations bpf_super_ops = { ...@@ -625,15 +632,21 @@ static const struct super_operations bpf_super_ops = {
}; };
enum { enum {
OPT_UID,
OPT_GID,
OPT_MODE, OPT_MODE,
}; };
static const struct fs_parameter_spec bpf_fs_parameters[] = { static const struct fs_parameter_spec bpf_fs_parameters[] = {
fsparam_u32 ("uid", OPT_UID),
fsparam_u32 ("gid", OPT_GID),
fsparam_u32oct ("mode", OPT_MODE), fsparam_u32oct ("mode", OPT_MODE),
{} {}
}; };
struct bpf_mount_opts { struct bpf_mount_opts {
kuid_t uid;
kgid_t gid;
umode_t mode; umode_t mode;
}; };
...@@ -641,6 +654,8 @@ static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param) ...@@ -641,6 +654,8 @@ static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param)
{ {
struct bpf_mount_opts *opts = fc->fs_private; struct bpf_mount_opts *opts = fc->fs_private;
struct fs_parse_result result; struct fs_parse_result result;
kuid_t uid;
kgid_t gid;
int opt; int opt;
opt = fs_parse(fc, bpf_fs_parameters, param, &result); opt = fs_parse(fc, bpf_fs_parameters, param, &result);
...@@ -662,12 +677,42 @@ static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param) ...@@ -662,12 +677,42 @@ static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param)
} }
switch (opt) { switch (opt) {
case OPT_UID:
uid = make_kuid(current_user_ns(), result.uint_32);
if (!uid_valid(uid))
goto bad_value;
/*
* The requested uid must be representable in the
* filesystem's idmapping.
*/
if (!kuid_has_mapping(fc->user_ns, uid))
goto bad_value;
opts->uid = uid;
break;
case OPT_GID:
gid = make_kgid(current_user_ns(), result.uint_32);
if (!gid_valid(gid))
goto bad_value;
/*
* The requested gid must be representable in the
* filesystem's idmapping.
*/
if (!kgid_has_mapping(fc->user_ns, gid))
goto bad_value;
opts->gid = gid;
break;
case OPT_MODE: case OPT_MODE:
opts->mode = result.uint_32 & S_IALLUGO; opts->mode = result.uint_32 & S_IALLUGO;
break; break;
} }
return 0; return 0;
bad_value:
return invalfc(fc, "Bad value for '%s'", param->key);
} }
struct bpf_preload_ops *bpf_preload_ops; struct bpf_preload_ops *bpf_preload_ops;
...@@ -750,6 +795,8 @@ static int bpf_fill_super(struct super_block *sb, struct fs_context *fc) ...@@ -750,6 +795,8 @@ static int bpf_fill_super(struct super_block *sb, struct fs_context *fc)
sb->s_op = &bpf_super_ops; sb->s_op = &bpf_super_ops;
inode = sb->s_root->d_inode; inode = sb->s_root->d_inode;
inode->i_uid = opts->uid;
inode->i_gid = opts->gid;
inode->i_op = &bpf_dir_iops; inode->i_op = &bpf_dir_iops;
inode->i_mode &= ~S_IALLUGO; inode->i_mode &= ~S_IALLUGO;
populate_bpffs(sb->s_root); populate_bpffs(sb->s_root);
...@@ -785,6 +832,8 @@ static int bpf_init_fs_context(struct fs_context *fc) ...@@ -785,6 +832,8 @@ static int bpf_init_fs_context(struct fs_context *fc)
return -ENOMEM; return -ENOMEM;
opts->mode = S_IRWXUGO; opts->mode = S_IRWXUGO;
opts->uid = current_fsuid();
opts->gid = current_fsgid();
fc->fs_private = opts; fc->fs_private = opts;
fc->ops = &bpf_context_ops; fc->ops = &bpf_context_ops;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment