Skip to content

Commit 2b3416c

Browse files
xuyang0410brauner
authored andcommitted
fs: add mode_strip_sgid() helper
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: e014f37 ("xfs: use setattr_copy to set vfs inode attributes") [1] Link: 01ea173 ("xfs: fix up non-directory creation in SGID directories") [2] Link: fd84bfd ("ceph: fix up non-directory creation in SGID directories") [3] Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Darrick J. Wong <[email protected]> Reviewed-by: Christian Brauner (Microsoft) <[email protected]> Reviewed-and-Tested-by: Jeff Layton <[email protected]> Signed-off-by: Yang Xu <[email protected]> Signed-off-by: Christian Brauner (Microsoft) <[email protected]>
1 parent ff69927 commit 2b3416c

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

fs/inode.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,10 +2246,8 @@ void inode_init_owner(struct user_namespace *mnt_userns, struct inode *inode,
22462246
/* Directories are special, and always inherit S_ISGID */
22472247
if (S_ISDIR(mode))
22482248
mode |= S_ISGID;
2249-
else if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP) &&
2250-
!in_group_p(i_gid_into_mnt(mnt_userns, dir)) &&
2251-
!capable_wrt_inode_uidgid(mnt_userns, dir, CAP_FSETID))
2252-
mode &= ~S_ISGID;
2249+
else
2250+
mode = mode_strip_sgid(mnt_userns, dir, mode);
22532251
} else
22542252
inode_fsgid_set(inode, mnt_userns);
22552253
inode->i_mode = mode;
@@ -2405,3 +2403,33 @@ struct timespec64 current_time(struct inode *inode)
24052403
return timestamp_truncate(now, inode);
24062404
}
24072405
EXPORT_SYMBOL(current_time);
2406+
2407+
/**
2408+
* mode_strip_sgid - handle the sgid bit for non-directories
2409+
* @mnt_userns: User namespace of the mount the inode was created from
2410+
* @dir: parent directory inode
2411+
* @mode: mode of the file to be created in @dir
2412+
*
2413+
* If the @mode of the new file has both the S_ISGID and S_IXGRP bit
2414+
* raised and @dir has the S_ISGID bit raised ensure that the caller is
2415+
* either in the group of the parent directory or they have CAP_FSETID
2416+
* in their user namespace and are privileged over the parent directory.
2417+
* In all other cases, strip the S_ISGID bit from @mode.
2418+
*
2419+
* Return: the new mode to use for the file
2420+
*/
2421+
umode_t mode_strip_sgid(struct user_namespace *mnt_userns,
2422+
const struct inode *dir, umode_t mode)
2423+
{
2424+
if ((mode & (S_ISGID | S_IXGRP)) != (S_ISGID | S_IXGRP))
2425+
return mode;
2426+
if (S_ISDIR(mode) || !dir || !(dir->i_mode & S_ISGID))
2427+
return mode;
2428+
if (in_group_p(i_gid_into_mnt(mnt_userns, dir)))
2429+
return mode;
2430+
if (capable_wrt_inode_uidgid(mnt_userns, dir, CAP_FSETID))
2431+
return mode;
2432+
2433+
return mode & ~S_ISGID;
2434+
}
2435+
EXPORT_SYMBOL(mode_strip_sgid);

include/linux/fs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,6 +1903,8 @@ extern long compat_ptr_ioctl(struct file *file, unsigned int cmd,
19031903
void inode_init_owner(struct user_namespace *mnt_userns, struct inode *inode,
19041904
const struct inode *dir, umode_t mode);
19051905
extern bool may_open_dev(const struct path *path);
1906+
umode_t mode_strip_sgid(struct user_namespace *mnt_userns,
1907+
const struct inode *dir, umode_t mode);
19061908

19071909
/*
19081910
* This is the "filldir" function type, used by readdir() to let

0 commit comments

Comments
 (0)