Skip to content

Commit 8cb0832

Browse files
ebiedermMiklos Szeredi
authored andcommitted
fuse: Support fuse filesystems outside of init_user_ns
In order to support mounts from namespaces other than init_user_ns, fuse must translate uids and gids to/from the userns of the process servicing requests on /dev/fuse. This patch does that, with a couple of restrictions on the namespace: - The userns for the fuse connection is fixed to the namespace from which /dev/fuse is opened. - The namespace must be the same as s_user_ns. These restrictions simplify the implementation by avoiding the need to pass around userns references and by allowing fuse to rely on the checks in setattr_prepare for ownership changes. Either restriction could be relaxed in the future if needed. For cuse the userns used is the opener of /dev/cuse. Semantically the cuse support does not appear safe for unprivileged users. Practically the permissions on /dev/cuse only make it accessible to the global root user. If something slips through the cracks in a user namespace the only users who will be able to use the cuse device are those users mapped into the user namespace. Translation in the posix acl is updated to use the uuser namespace of the filesystem. Avoiding cases which might bypass this translation is handled in a following change. This change is stronlgy based on a similar change from Seth Forshee and Dongsu Park. Cc: Seth Forshee <seth.forshee@canonical.com> Cc: Dongsu Park <dongsu@kinvolk.io> Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
1 parent c9582eb commit 8cb0832

6 files changed

Lines changed: 43 additions & 27 deletions

File tree

‎fs/fuse/acl.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct posix_acl *fuse_get_acl(struct inode *inode, int type)
3434
return ERR_PTR(-ENOMEM);
3535
size = fuse_getxattr(inode, name, value, PAGE_SIZE);
3636
if (size > 0)
37-
acl = posix_acl_from_xattr(&init_user_ns, value, size);
37+
acl = posix_acl_from_xattr(fc->user_ns, value, size);
3838
else if ((size == 0) || (size == -ENODATA) ||
3939
(size == -EOPNOTSUPP && fc->no_getxattr))
4040
acl = NULL;
@@ -81,7 +81,7 @@ int fuse_set_acl(struct inode *inode, struct posix_acl *acl, int type)
8181
if (!value)
8282
return -ENOMEM;
8383

84-
ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
84+
ret = posix_acl_to_xattr(fc->user_ns, acl, value, size);
8585
if (ret < 0) {
8686
kfree(value);
8787
return ret;

‎fs/fuse/cuse.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <linux/stat.h>
4949
#include <linux/module.h>
5050
#include <linux/uio.h>
51+
#include <linux/user_namespace.h>
5152

5253
#include "fuse_i.h"
5354

@@ -498,7 +499,11 @@ static int cuse_channel_open(struct inode *inode, struct file *file)
498499
if (!cc)
499500
return -ENOMEM;
500501

501-
fuse_conn_init(&cc->fc);
502+
/*
503+
* Limit the cuse channel to requests that can
504+
* be represented in file->f_cred->user_ns.
505+
*/
506+
fuse_conn_init(&cc->fc, file->f_cred->user_ns);
502507

503508
fud = fuse_dev_alloc(&cc->fc);
504509
if (!fud) {

‎fs/fuse/dev.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
156156
goto out;
157157
}
158158

159-
req->in.h.uid = from_kuid(&init_user_ns, current_fsuid());
160-
req->in.h.gid = from_kgid(&init_user_ns, current_fsgid());
159+
req->in.h.uid = from_kuid(fc->user_ns, current_fsuid());
160+
req->in.h.gid = from_kgid(fc->user_ns, current_fsgid());
161161
req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
162162

163163
__set_bit(FR_WAITING, &req->flags);
@@ -257,8 +257,8 @@ struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
257257
if (!req)
258258
req = get_reserved_req(fc, file);
259259

260-
req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
261-
req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
260+
req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid());
261+
req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid());
262262
req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
263263

264264
__set_bit(FR_WAITING, &req->flags);

‎fs/fuse/dir.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,8 @@ static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
858858
stat->ino = attr->ino;
859859
stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
860860
stat->nlink = attr->nlink;
861-
stat->uid = make_kuid(&init_user_ns, attr->uid);
862-
stat->gid = make_kgid(&init_user_ns, attr->gid);
861+
stat->uid = make_kuid(fc->user_ns, attr->uid);
862+
stat->gid = make_kgid(fc->user_ns, attr->gid);
863863
stat->rdev = inode->i_rdev;
864864
stat->atime.tv_sec = attr->atime;
865865
stat->atime.tv_nsec = attr->atimensec;
@@ -1475,17 +1475,17 @@ static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
14751475
return true;
14761476
}
14771477

1478-
static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
1479-
bool trust_local_cmtime)
1478+
static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1479+
struct fuse_setattr_in *arg, bool trust_local_cmtime)
14801480
{
14811481
unsigned ivalid = iattr->ia_valid;
14821482

14831483
if (ivalid & ATTR_MODE)
14841484
arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
14851485
if (ivalid & ATTR_UID)
1486-
arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1486+
arg->valid |= FATTR_UID, arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
14871487
if (ivalid & ATTR_GID)
1488-
arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1488+
arg->valid |= FATTR_GID, arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
14891489
if (ivalid & ATTR_SIZE)
14901490
arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
14911491
if (ivalid & ATTR_ATIME) {
@@ -1657,7 +1657,7 @@ int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
16571657

16581658
memset(&inarg, 0, sizeof(inarg));
16591659
memset(&outarg, 0, sizeof(outarg));
1660-
iattr_to_fattr(attr, &inarg, trust_local_cmtime);
1660+
iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
16611661
if (file) {
16621662
struct fuse_file *ff = file->private_data;
16631663
inarg.valid |= FATTR_FH;

‎fs/fuse/fuse_i.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/xattr.h>
2727
#include <linux/pid_namespace.h>
2828
#include <linux/refcount.h>
29+
#include <linux/user_namespace.h>
2930

3031
/** Max number of pages that can be used in a single read request */
3132
#define FUSE_MAX_PAGES_PER_REQ 32
@@ -466,6 +467,9 @@ struct fuse_conn {
466467
/** The pid namespace for this mount */
467468
struct pid_namespace *pid_ns;
468469

470+
/** The user namespace for this mount */
471+
struct user_namespace *user_ns;
472+
469473
/** Maximum read size */
470474
unsigned max_read;
471475

@@ -876,7 +880,7 @@ struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
876880
/**
877881
* Initialize fuse_conn
878882
*/
879-
void fuse_conn_init(struct fuse_conn *fc);
883+
void fuse_conn_init(struct fuse_conn *fc, struct user_namespace *user_ns);
880884

881885
/**
882886
* Release reference to fuse_conn

‎fs/fuse/inode.c‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
171171
inode->i_ino = fuse_squash_ino(attr->ino);
172172
inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
173173
set_nlink(inode, attr->nlink);
174-
inode->i_uid = make_kuid(&init_user_ns, attr->uid);
175-
inode->i_gid = make_kgid(&init_user_ns, attr->gid);
174+
inode->i_uid = make_kuid(fc->user_ns, attr->uid);
175+
inode->i_gid = make_kgid(fc->user_ns, attr->gid);
176176
inode->i_blocks = attr->blocks;
177177
inode->i_atime.tv_sec = attr->atime;
178178
inode->i_atime.tv_nsec = attr->atimensec;
@@ -477,7 +477,8 @@ static int fuse_match_uint(substring_t *s, unsigned int *res)
477477
return err;
478478
}
479479

480-
static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev)
480+
static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev,
481+
struct user_namespace *user_ns)
481482
{
482483
char *p;
483484
memset(d, 0, sizeof(struct fuse_mount_data));
@@ -513,7 +514,7 @@ static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev)
513514
case OPT_USER_ID:
514515
if (fuse_match_uint(&args[0], &uv))
515516
return 0;
516-
d->user_id = make_kuid(current_user_ns(), uv);
517+
d->user_id = make_kuid(user_ns, uv);
517518
if (!uid_valid(d->user_id))
518519
return 0;
519520
d->user_id_present = 1;
@@ -522,7 +523,7 @@ static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev)
522523
case OPT_GROUP_ID:
523524
if (fuse_match_uint(&args[0], &uv))
524525
return 0;
525-
d->group_id = make_kgid(current_user_ns(), uv);
526+
d->group_id = make_kgid(user_ns, uv);
526527
if (!gid_valid(d->group_id))
527528
return 0;
528529
d->group_id_present = 1;
@@ -565,8 +566,8 @@ static int fuse_show_options(struct seq_file *m, struct dentry *root)
565566
struct super_block *sb = root->d_sb;
566567
struct fuse_conn *fc = get_fuse_conn_super(sb);
567568

568-
seq_printf(m, ",user_id=%u", from_kuid_munged(&init_user_ns, fc->user_id));
569-
seq_printf(m, ",group_id=%u", from_kgid_munged(&init_user_ns, fc->group_id));
569+
seq_printf(m, ",user_id=%u", from_kuid_munged(fc->user_ns, fc->user_id));
570+
seq_printf(m, ",group_id=%u", from_kgid_munged(fc->user_ns, fc->group_id));
570571
if (fc->default_permissions)
571572
seq_puts(m, ",default_permissions");
572573
if (fc->allow_other)
@@ -597,7 +598,7 @@ static void fuse_pqueue_init(struct fuse_pqueue *fpq)
597598
fpq->connected = 1;
598599
}
599600

600-
void fuse_conn_init(struct fuse_conn *fc)
601+
void fuse_conn_init(struct fuse_conn *fc, struct user_namespace *user_ns)
601602
{
602603
memset(fc, 0, sizeof(*fc));
603604
spin_lock_init(&fc->lock);
@@ -621,6 +622,7 @@ void fuse_conn_init(struct fuse_conn *fc)
621622
fc->attr_version = 1;
622623
get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
623624
fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
625+
fc->user_ns = get_user_ns(user_ns);
624626
}
625627
EXPORT_SYMBOL_GPL(fuse_conn_init);
626628

@@ -630,6 +632,7 @@ void fuse_conn_put(struct fuse_conn *fc)
630632
if (fc->destroy_req)
631633
fuse_request_free(fc->destroy_req);
632634
put_pid_ns(fc->pid_ns);
635+
put_user_ns(fc->user_ns);
633636
fc->release(fc);
634637
}
635638
}
@@ -1064,7 +1067,7 @@ static int fuse_fill_super(struct super_block *sb, void *data, int silent)
10641067

10651068
sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
10661069

1067-
if (!parse_fuse_opt(data, &d, is_bdev))
1070+
if (!parse_fuse_opt(data, &d, is_bdev, sb->s_user_ns))
10681071
goto err;
10691072

10701073
if (is_bdev) {
@@ -1089,16 +1092,20 @@ static int fuse_fill_super(struct super_block *sb, void *data, int silent)
10891092
if (!file)
10901093
goto err;
10911094

1092-
if ((file->f_op != &fuse_dev_operations) ||
1093-
(file->f_cred->user_ns != &init_user_ns))
1095+
/*
1096+
* Require mount to happen from the same user namespace which
1097+
* opened /dev/fuse to prevent potential attacks.
1098+
*/
1099+
if (file->f_op != &fuse_dev_operations ||
1100+
file->f_cred->user_ns != sb->s_user_ns)
10941101
goto err_fput;
10951102

10961103
fc = kmalloc(sizeof(*fc), GFP_KERNEL);
10971104
err = -ENOMEM;
10981105
if (!fc)
10991106
goto err_fput;
11001107

1101-
fuse_conn_init(fc);
1108+
fuse_conn_init(fc, sb->s_user_ns);
11021109
fc->release = fuse_free_conn;
11031110

11041111
fud = fuse_dev_alloc(fc);

0 commit comments

Comments
 (0)