fs: port fs{g,u}id helpers to mnt_idmap
authorChristian Brauner <brauner@kernel.org>
Fri, 13 Jan 2023 11:49:31 +0000 (12:49 +0100)
committerChristian Brauner (Microsoft) <brauner@kernel.org>
Thu, 19 Jan 2023 08:24:30 +0000 (09:24 +0100)
Convert to struct mnt_idmap.

Last cycle we merged the necessary infrastructure in
256c8aed2b42 ("fs: introduce dedicated idmap type for mounts").
This is just the conversion to struct mnt_idmap.

Currently we still pass around the plain namespace that was attached to a
mount. This is in general pretty convenient but it makes it easy to
conflate namespaces that are relevant on the filesystem with namespaces
that are relevent on the mount level. Especially for non-vfs developers
without detailed knowledge in this area this can be a potential source for
bugs.

Once the conversion to struct mnt_idmap is done all helpers down to the
really low-level helpers will take a struct mnt_idmap argument instead of
two namespace arguments. This way it becomes impossible to conflate the two
eliminating the possibility of any bugs. All of the vfs and all filesystems
only operate on struct mnt_idmap.

Acked-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
fs/ext4/ialloc.c
fs/inode.c
fs/xfs/xfs_inode.c
fs/xfs/xfs_symlink.c
include/linux/fs.h
include/linux/mnt_idmapping.h

index 1024b0c0243145f434a163076a00bc40d86daedb..157663031f8c919d7380426bbfe00a3866eb93d2 100644 (file)
@@ -943,7 +943,6 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
        ext4_group_t flex_group;
        struct ext4_group_info *grp = NULL;
        bool encrypt = false;
-       struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
 
        /* Cannot create files in a deleted directory */
        if (!dir || !dir->i_nlink)
@@ -973,7 +972,7 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
                i_gid_write(inode, owner[1]);
        } else if (test_opt(sb, GRPID)) {
                inode->i_mode = mode;
-               inode_fsuid_set(inode, mnt_userns);
+               inode_fsuid_set(inode, idmap);
                inode->i_gid = dir->i_gid;
        } else
                inode_init_owner(idmap, inode, dir, mode);
index 1aec92141fab98d18659a9f07a511be26783e1cc..1b05e0e4b5c8d04b6b5b3710ea44c6ae2f30825d 100644 (file)
@@ -2293,9 +2293,7 @@ EXPORT_SYMBOL(init_special_inode);
 void inode_init_owner(struct mnt_idmap *idmap, struct inode *inode,
                      const struct inode *dir, umode_t mode)
 {
-       struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
-
-       inode_fsuid_set(inode, mnt_userns);
+       inode_fsuid_set(inode, idmap);
        if (dir && dir->i_mode & S_ISGID) {
                inode->i_gid = dir->i_gid;
 
@@ -2303,7 +2301,7 @@ void inode_init_owner(struct mnt_idmap *idmap, struct inode *inode,
                if (S_ISDIR(mode))
                        mode |= S_ISGID;
        } else
-               inode_fsgid_set(inode, mnt_userns);
+               inode_fsgid_set(inode, idmap);
        inode->i_mode = mode;
 }
 EXPORT_SYMBOL(inode_init_owner);
index 59fb064e2df3073985158642eddbed265988f18c..7f1d715faab527b888100408e7c513c897c9df1c 100644 (file)
@@ -788,7 +788,6 @@ xfs_init_new_inode(
        bool                    init_xattrs,
        struct xfs_inode        **ipp)
 {
-       struct user_namespace   *mnt_userns = mnt_idmap_owner(idmap);
        struct inode            *dir = pip ? VFS_I(pip) : NULL;
        struct xfs_mount        *mp = tp->t_mountp;
        struct xfs_inode        *ip;
@@ -824,7 +823,7 @@ xfs_init_new_inode(
        ip->i_projid = prid;
 
        if (dir && !(dir->i_mode & S_ISGID) && xfs_has_grpid(mp)) {
-               inode_fsuid_set(inode, mnt_userns);
+               inode_fsuid_set(inode, idmap);
                inode->i_gid = dir->i_gid;
                inode->i_mode = mode;
        } else {
@@ -955,7 +954,6 @@ xfs_create(
        bool                    init_xattrs,
        xfs_inode_t             **ipp)
 {
-       struct user_namespace   *mnt_userns = mnt_idmap_owner(idmap);
        int                     is_dir = S_ISDIR(mode);
        struct xfs_mount        *mp = dp->i_mount;
        struct xfs_inode        *ip = NULL;
@@ -980,8 +978,8 @@ xfs_create(
        /*
         * Make sure that we have allocated dquot(s) on disk.
         */
-       error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns, &init_user_ns),
-                       mapped_fsgid(mnt_userns, &init_user_ns), prid,
+       error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(idmap, &init_user_ns),
+                       mapped_fsgid(idmap, &init_user_ns), prid,
                        XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
                        &udqp, &gdqp, &pdqp);
        if (error)
@@ -1109,7 +1107,6 @@ xfs_create_tmpfile(
        umode_t                 mode,
        struct xfs_inode        **ipp)
 {
-       struct user_namespace   *mnt_userns = mnt_idmap_owner(idmap);
        struct xfs_mount        *mp = dp->i_mount;
        struct xfs_inode        *ip = NULL;
        struct xfs_trans        *tp = NULL;
@@ -1130,8 +1127,8 @@ xfs_create_tmpfile(
        /*
         * Make sure that we have allocated dquot(s) on disk.
         */
-       error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns, &init_user_ns),
-                       mapped_fsgid(mnt_userns, &init_user_ns), prid,
+       error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(idmap, &init_user_ns),
+                       mapped_fsgid(idmap, &init_user_ns), prid,
                        XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
                        &udqp, &gdqp, &pdqp);
        if (error)
index 24cf0a16bf35acdd7b50e4c2b02343f40fe7c45a..85e433df6a3f91e82a56aa2026a460ad92574b6b 100644 (file)
@@ -151,7 +151,6 @@ xfs_symlink(
        umode_t                 mode,
        struct xfs_inode        **ipp)
 {
-       struct user_namespace   *mnt_userns = mnt_idmap_owner(idmap);
        struct xfs_mount        *mp = dp->i_mount;
        struct xfs_trans        *tp = NULL;
        struct xfs_inode        *ip = NULL;
@@ -194,8 +193,8 @@ xfs_symlink(
        /*
         * Make sure that we have allocated dquot(s) on disk.
         */
-       error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns, &init_user_ns),
-                       mapped_fsgid(mnt_userns, &init_user_ns), prid,
+       error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(idmap, &init_user_ns),
+                       mapped_fsgid(idmap, &init_user_ns), prid,
                        XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
                        &udqp, &gdqp, &pdqp);
        if (error)
index 173c5274a63aa4d1c8d330ca706dbbba41fcfe1d..54a95ed68322c0e9b473cd1f33ffbd2310287e07 100644 (file)
@@ -1744,29 +1744,29 @@ static inline void i_gid_update(struct mnt_idmap *idmap,
 /**
  * inode_fsuid_set - initialize inode's i_uid field with callers fsuid
  * @inode: inode to initialize
- * @mnt_userns: user namespace of the mount the inode was found from
+ * @idmap: idmap of the mount the inode was found from
  *
  * Initialize the i_uid field of @inode. If the inode was found/created via
- * an idmapped mount map the caller's fsuid according to @mnt_users.
+ * an idmapped mount map the caller's fsuid according to @idmap.
  */
 static inline void inode_fsuid_set(struct inode *inode,
-                                  struct user_namespace *mnt_userns)
+                                  struct mnt_idmap *idmap)
 {
-       inode->i_uid = mapped_fsuid(mnt_userns, i_user_ns(inode));
+       inode->i_uid = mapped_fsuid(idmap, i_user_ns(inode));
 }
 
 /**
  * inode_fsgid_set - initialize inode's i_gid field with callers fsgid
  * @inode: inode to initialize
- * @mnt_userns: user namespace of the mount the inode was found from
+ * @idmap: idmap of the mount the inode was found from
  *
  * Initialize the i_gid field of @inode. If the inode was found/created via
- * an idmapped mount map the caller's fsgid according to @mnt_users.
+ * an idmapped mount map the caller's fsgid according to @idmap.
  */
 static inline void inode_fsgid_set(struct inode *inode,
-                                  struct user_namespace *mnt_userns)
+                                  struct mnt_idmap *idmap)
 {
-       inode->i_gid = mapped_fsgid(mnt_userns, i_user_ns(inode));
+       inode->i_gid = mapped_fsgid(idmap, i_user_ns(inode));
 }
 
 /**
@@ -1784,14 +1784,13 @@ static inline bool fsuidgid_has_mapping(struct super_block *sb,
                                        struct mnt_idmap *idmap)
 {
        struct user_namespace *fs_userns = sb->s_user_ns;
-       struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
        kuid_t kuid;
        kgid_t kgid;
 
-       kuid = mapped_fsuid(mnt_userns, fs_userns);
+       kuid = mapped_fsuid(idmap, fs_userns);
        if (!uid_valid(kuid))
                return false;
-       kgid = mapped_fsgid(mnt_userns, fs_userns);
+       kgid = mapped_fsgid(idmap, fs_userns);
        if (!gid_valid(kgid))
                return false;
        return kuid_has_mapping(fs_userns, kuid) &&
index 0ccca33a7a6dfbf0bf3887f13fb384770dc23f7c..d63e7c84a389d668b8ee94d484b1898a019fbd6d 100644 (file)
@@ -375,8 +375,8 @@ static inline kgid_t vfsgid_into_kgid(vfsgid_t vfsgid)
 }
 
 /**
- * mapped_fsuid - return caller's fsuid mapped up into a mnt_userns
- * @mnt_userns: the mount's idmapping
+ * mapped_fsuid - return caller's fsuid mapped according to an idmapping
+ * @idmap: the mount's idmapping
  * @fs_userns: the filesystem's idmapping
  *
  * Use this helper to initialize a new vfs or filesystem object based on
@@ -385,18 +385,19 @@ static inline kgid_t vfsgid_into_kgid(vfsgid_t vfsgid)
  * O_CREAT. Other examples include the allocation of quotas for a specific
  * user.
  *
- * Return: the caller's current fsuid mapped up according to @mnt_userns.
+ * Return: the caller's current fsuid mapped up according to @idmap.
  */
-static inline kuid_t mapped_fsuid(struct user_namespace *mnt_userns,
+static inline kuid_t mapped_fsuid(struct mnt_idmap *idmap,
                                  struct user_namespace *fs_userns)
 {
+       struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
        return from_vfsuid(mnt_userns, fs_userns,
                           VFSUIDT_INIT(current_fsuid()));
 }
 
 /**
- * mapped_fsgid - return caller's fsgid mapped up into a mnt_userns
- * @mnt_userns: the mount's idmapping
+ * mapped_fsgid - return caller's fsgid mapped according to an idmapping
+ * @idmap: the mount's idmapping
  * @fs_userns: the filesystem's idmapping
  *
  * Use this helper to initialize a new vfs or filesystem object based on
@@ -405,11 +406,12 @@ static inline kuid_t mapped_fsuid(struct user_namespace *mnt_userns,
  * O_CREAT. Other examples include the allocation of quotas for a specific
  * user.
  *
- * Return: the caller's current fsgid mapped up according to @mnt_userns.
+ * Return: the caller's current fsgid mapped up according to @idmap.
  */
-static inline kgid_t mapped_fsgid(struct user_namespace *mnt_userns,
+static inline kgid_t mapped_fsgid(struct mnt_idmap *idmap,
                                  struct user_namespace *fs_userns)
 {
+       struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
        return from_vfsgid(mnt_userns, fs_userns,
                           VFSGIDT_INIT(current_fsgid()));
 }