fs: add mode_strip_sgid() helper
authorYang Xu <xuyang2018.jy@fujitsu.com>
Thu, 14 Jul 2022 06:11:25 +0000 (14:11 +0800)
committerChristian Brauner (Microsoft) <brauner@kernel.org>
Tue, 19 Jul 2022 13:13:02 +0000 (15:13 +0200)
Add a dedicated helper to handle the setgid bit when creating a new file
in a setgid directory. This is a preparatory patch for moving setgid
stripping into the vfs. The patch contains no functional changes.

Currently the setgid stripping logic is open-coded directly in
inode_init_owner() and the individual filesystems are responsible for
handling setgid inheritance. Since this has proven to be brittle as
evidenced by old issues we uncovered over the last months (see [1] to
[3] below) we will try to move this logic into the vfs.

Link: e014f37db1a2 ("xfs: use setattr_copy to set vfs inode attributes") [1]
Link: 01ea173e103e ("xfs: fix up non-directory creation in SGID directories") [2]
Link: fd84bfdddd16 ("ceph: fix up non-directory creation in SGID directories") [3]
Link: https://lore.kernel.org/r/1657779088-2242-1-git-send-email-xuyang2018.jy@fujitsu.com
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christian Brauner (Microsoft) <brauner@kernel.org>
Reviewed-and-Tested-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
fs/inode.c
include/linux/fs.h

index bd4da9c5207eabb0cec8722ee3d30acd963169bf..71b36afc3893a6a624ab14f04fbca95fd220ca78 100644 (file)
@@ -2246,10 +2246,8 @@ void inode_init_owner(struct user_namespace *mnt_userns, struct inode *inode,
                /* Directories are special, and always inherit S_ISGID */
                if (S_ISDIR(mode))
                        mode |= S_ISGID;
-               else if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP) &&
-                        !in_group_p(i_gid_into_mnt(mnt_userns, dir)) &&
-                        !capable_wrt_inode_uidgid(mnt_userns, dir, CAP_FSETID))
-                       mode &= ~S_ISGID;
+               else
+                       mode = mode_strip_sgid(mnt_userns, dir, mode);
        } else
                inode_fsgid_set(inode, mnt_userns);
        inode->i_mode = mode;
@@ -2405,3 +2403,33 @@ struct timespec64 current_time(struct inode *inode)
        return timestamp_truncate(now, inode);
 }
 EXPORT_SYMBOL(current_time);
+
+/**
+ * mode_strip_sgid - handle the sgid bit for non-directories
+ * @mnt_userns: User namespace of the mount the inode was created from
+ * @dir: parent directory inode
+ * @mode: mode of the file to be created in @dir
+ *
+ * If the @mode of the new file has both the S_ISGID and S_IXGRP bit
+ * raised and @dir has the S_ISGID bit raised ensure that the caller is
+ * either in the group of the parent directory or they have CAP_FSETID
+ * in their user namespace and are privileged over the parent directory.
+ * In all other cases, strip the S_ISGID bit from @mode.
+ *
+ * Return: the new mode to use for the file
+ */
+umode_t mode_strip_sgid(struct user_namespace *mnt_userns,
+                       const struct inode *dir, umode_t mode)
+{
+       if ((mode & (S_ISGID | S_IXGRP)) != (S_ISGID | S_IXGRP))
+               return mode;
+       if (S_ISDIR(mode) || !dir || !(dir->i_mode & S_ISGID))
+               return mode;
+       if (in_group_p(i_gid_into_mnt(mnt_userns, dir)))
+               return mode;
+       if (capable_wrt_inode_uidgid(mnt_userns, dir, CAP_FSETID))
+               return mode;
+
+       return mode & ~S_ISGID;
+}
+EXPORT_SYMBOL(mode_strip_sgid);
index 9ad5e3520fae577c2a216859aba20f8d26241172..50642668c60fabbadef8fc8b8d8c5b4158ab46b6 100644 (file)
@@ -1903,6 +1903,8 @@ extern long compat_ptr_ioctl(struct file *file, unsigned int cmd,
 void inode_init_owner(struct user_namespace *mnt_userns, struct inode *inode,
                      const struct inode *dir, umode_t mode);
 extern bool may_open_dev(const struct path *path);
+umode_t mode_strip_sgid(struct user_namespace *mnt_userns,
+                       const struct inode *dir, umode_t mode);
 
 /*
  * This is the "filldir" function type, used by readdir() to let