Skip to content

Commit 4b78993

Browse files
author
Christian Brauner
committed
fat: handle idmapped mounts
Let fat handle idmapped mounts. This allows to have the same fat mount appear in multiple locations with different id mappings. This allows to expose a vfat formatted USB stick to multiple user with different ids on the host or in user namespaces allowing for dac permissions: mount -o uid=1000,gid=1000 /dev/sdb /mnt u1001@f2-vm:/lower1$ ls -ln /mnt/ total 4 -rwxr-xr-x 1 1000 1000 4 Oct 28 03:44 aaa -rwxr-xr-x 1 1000 1000 0 Oct 28 01:09 bbb -rwxr-xr-x 1 1000 1000 0 Oct 28 01:10 ccc -rwxr-xr-x 1 1000 1000 0 Oct 28 03:46 ddd -rwxr-xr-x 1 1000 1000 0 Oct 28 04:01 eee mount-idmapped --map-mount b:1000:1001:1 u1001@f2-vm:/lower1$ ls -ln /lower1/ total 4 -rwxr-xr-x 1 1001 1001 4 Oct 28 03:44 aaa -rwxr-xr-x 1 1001 1001 0 Oct 28 01:09 bbb -rwxr-xr-x 1 1001 1001 0 Oct 28 01:10 ccc -rwxr-xr-x 1 1001 1001 0 Oct 28 03:46 ddd -rwxr-xr-x 1 1001 1001 0 Oct 28 04:01 eee u1001@f2-vm:/lower1$ touch /lower1/fff u1001@f2-vm:/lower1$ ls -ln /lower1/fff -rwxr-xr-x 1 1001 1001 0 Oct 28 04:03 /lower1/fff u1001@f2-vm:/lower1$ ls -ln /mnt/fff -rwxr-xr-x 1 1000 1000 0 Oct 28 04:03 /mnt/fff Link: https://lore.kernel.org/r/20210121131959.646623-38-christian.brauner@ubuntu.com Cc: Christoph Hellwig <hch@lst.de> Cc: David Howells <dhowells@redhat.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
1 parent 01eadc8 commit 4b78993

3 files changed

Lines changed: 10 additions & 9 deletions

File tree

fs/fat/file.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ int fat_getattr(struct user_namespace *mnt_userns, const struct path *path,
398398
struct kstat *stat, u32 request_mask, unsigned int flags)
399399
{
400400
struct inode *inode = d_inode(path->dentry);
401-
generic_fillattr(&init_user_ns, inode, stat);
401+
generic_fillattr(mnt_userns, inode, stat);
402402
stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
403403

404404
if (MSDOS_SB(inode->i_sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
@@ -447,12 +447,13 @@ static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
447447
return 0;
448448
}
449449

450-
static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
450+
static int fat_allow_set_time(struct user_namespace *mnt_userns,
451+
struct msdos_sb_info *sbi, struct inode *inode)
451452
{
452453
umode_t allow_utime = sbi->options.allow_utime;
453454

454-
if (!uid_eq(current_fsuid(), inode->i_uid)) {
455-
if (in_group_p(inode->i_gid))
455+
if (!uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode))) {
456+
if (in_group_p(i_gid_into_mnt(mnt_userns, inode)))
456457
allow_utime >>= 3;
457458
if (allow_utime & MAY_WRITE)
458459
return 1;
@@ -477,11 +478,11 @@ int fat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
477478
/* Check for setting the inode time. */
478479
ia_valid = attr->ia_valid;
479480
if (ia_valid & TIMES_SET_FLAGS) {
480-
if (fat_allow_set_time(sbi, inode))
481+
if (fat_allow_set_time(mnt_userns, sbi, inode))
481482
attr->ia_valid &= ~TIMES_SET_FLAGS;
482483
}
483484

484-
error = setattr_prepare(&init_user_ns, dentry, attr);
485+
error = setattr_prepare(mnt_userns, dentry, attr);
485486
attr->ia_valid = ia_valid;
486487
if (error) {
487488
if (sbi->options.quiet)
@@ -551,7 +552,7 @@ int fat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
551552
fat_truncate_time(inode, &attr->ia_mtime, S_MTIME);
552553
attr->ia_valid &= ~(ATTR_ATIME|ATTR_CTIME|ATTR_MTIME);
553554

554-
setattr_copy(&init_user_ns, inode, attr);
555+
setattr_copy(mnt_userns, inode, attr);
555556
mark_inode_dirty(inode);
556557
out:
557558
return error;

fs/fat/namei_msdos.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ static struct file_system_type msdos_fs_type = {
667667
.name = "msdos",
668668
.mount = msdos_mount,
669669
.kill_sb = kill_block_super,
670-
.fs_flags = FS_REQUIRES_DEV,
670+
.fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
671671
};
672672
MODULE_ALIAS_FS("msdos");
673673

fs/fat/namei_vfat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ static struct file_system_type vfat_fs_type = {
10631063
.name = "vfat",
10641064
.mount = vfat_mount,
10651065
.kill_sb = kill_block_super,
1066-
.fs_flags = FS_REQUIRES_DEV,
1066+
.fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
10671067
};
10681068
MODULE_ALIAS_FS("vfat");
10691069

0 commit comments

Comments
 (0)