xfs: fix up non-directory creation in SGID directories
PATCH 1/1 xfs: fix up non-directory creation in SGID directories
XFS always inherits the SGID bit if it is set on the parent inode, while the generic inode_init_owner does not do this in a few cases where it can create a possible security problem, see commit 0fa3ecd87848 ("Fix up non-directory creation in SGID directories") for details.
Switch XFS to use the generic helper for the normal path to fix this, just keeping the simple field inheritance open coded for the case of the non-sgid case with the bsdgrpid mount option.
--- fs/xfs/xfs_inode.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
fs/xfs/xfs_inode.c +7 -7
@@ -775,6 +775,7 @@ xfs_init_new_inode(prid_t prid, struct xfs_inode **ipp) { + struct inode *dir = pip ? VFS_I(pip) : NULL;struct xfs_mount *mp = tp->t_mountp;struct xfs_inode *ip;unsigned int flags;
@@ -804,18 +805,17 @@ xfs_init_new_inode(ASSERT (ip !=NULL ); inode =VFS_I (ip); - inode->i_mode = mode;set_nlink (inode, nlink); - inode->i_uid =current_fsuid (); inode->i_rdev = rdev; ip->i_d.di_projid = prid; -if (pip &&XFS_INHERIT_GID (pip)) { - inode->i_gid =VFS_I (pip)->i_gid; -if ((VFS_I (pip)->i_mode &S_ISGID ) &&S_ISDIR (mode)) - inode->i_mode |=S_ISGID ; +if (dir && !(dir->i_mode &S_ISGID ) && + (mp->m_flags &XFS_MOUNT_GRPID )) { + inode->i_uid =current_fsuid (); + inode->i_gid = dir->i_gid; + inode->i_mode = mode; }else { - inode->i_gid =current_fsgid (); +inode_init_owner (inode, dir, mode); }/*
Signature · 1 line
2.29.2
Quoted 5 lines · Christoph Hellwig
XFS always inherits the SGID bit if it is set on the parent inode, while the generic
inode_init_ownerdoes not do this in a few cases where it can create a possible security problem, see commit0fa3ecd87848("Fix up non-directory creation in SGID directories") for details.
Quoted 8 lines
Switch XFS to use the generic helper for the normal path to fix this, just keeping the simple field inheritance open coded for the case of the non-sgid case with the bsdgrpid mount option.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Reported-by Christian Brauner <[email protected]>Signed-off-by Christoph Hellwig <[email protected]>---
I ran the idmapped mounts xfstests on this patchset. With this patch applied I was able to remove the special casing for xfs (apart from the irix compatibility check) and got clean test runs:
1. with regular setgid inheritance rules
root@f2-vm:/xfstests# ./check generic/622 FSTYP -- xfs (non-debug) PLATFORM -- Linux/x86_64 f2-vm 5.11.0-rc3-brauner-idmapped-mounts-xfs #311 SMP Thu Jan 14 09:55:14 UTC 2021 MKFS_OPTIONS -- -f -bsize=4096 /dev/loop7 MOUNT_OPTIONS -- /dev/loop7 /mnt/scratch
generic/622 1s ... 2s
Ran: generic/622
Passed all 1 tests
2. with irix_sgid_inherit setgid inheritance rules
root@f2-vm:/xfstests# echo 1 > /proc/sys/fs/xfs/irix_sgid_inherit root@f2-vm:/xfstests# ./check generic/622 FSTYP -- xfs (non-debug) PLATFORM -- Linux/x86_64 f2-vm 5.11.0-rc3-brauner-idmapped-mounts-xfs #311 SMP Thu Jan 14 09:55:14 UTC 2021 MKFS_OPTIONS -- -f -bsize=4096 /dev/loop7 MOUNT_OPTIONS -- /dev/loop7 /mnt/scratch
generic/622 2s ... 1s
Ran: generic/622
Passed all 1 tests
Thanks!
Christian
Quoted 17 lines · Christian Brauner
Quoted 5 lines · Christoph Hellwig
XFS always inherits the SGID bit if it is set on the parent inode, while the generic
inode_init_ownerdoes not do this in a few cases where it can create a possible security problem, see commit0fa3ecd87848("Fix up non-directory creation in SGID directories") for details.Quoted 8 lines
Switch XFS to use the generic helper for the normal path to fix this, just keeping the simple field inheritance open coded for the case of the non-sgid case with the bsdgrpid mount option.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Reported-by Christian Brauner <[email protected]>Signed-off-by Christoph Hellwig <[email protected]>---
Reviewed-by Christian Brauner <[email protected]>
Quoted 7 lines
I ran the idmapped mounts xfstests on this patchset. With this patch applied I was able to remove the special casing for xfs (apart from the irix compatibility check) and got clean test runs:
1. with regular setgid inheritance rules
root@f2-vm:/xfstests# ./check generic/622
Is this test posted to fstests somewhere?
FWIW the code change looks reasonable to me, but I wanted to see the functionality exercise test first. :)
--D
Quoted 23 lines
FSTYP -- xfs (non-debug) PLATFORM -- Linux/x86_64 f2-vm 5.11.0-rc3-brauner-idmapped-mounts-xfs #311 SMP Thu Jan 14 09:55:14 UTC 2021 MKFS_OPTIONS -- -f -bsize=4096 /dev/loop7
MOUNT_OPTIONS--/dev/loop7/mnt/scratchgeneric/622 1s ... 2s
Ran: generic/622
Passed all 1 tests2. with
irix_sgid_inheritsetgid inheritance rulesroot@f2-vm:/xfstests# echo 1 > /proc/sys/fs/xfs/irix_sgid_inherit root@f2-vm:/xfstests# ./check generic/622 FSTYP -- xfs (non-debug) PLATFORM -- Linux/x86_64 f2-vm 5.11.0-rc3-brauner-idmapped-mounts-xfs #311 SMP Thu Jan 14 09:55:14 UTC 2021 MKFS_OPTIONS -- -f -bsize=4096 /dev/loop7 MOUNT_OPTIONS -- /dev/loop7 /mnt/scratchgeneric/622 2s ... 1s
Ran: generic/622
Passed all 1 testsThanks!
Christian
Quoted 23 lines · Darrick J. Wong
Quoted 22 lines · Christian Brauner
Quoted 5 lines · Christoph Hellwig
XFS always inherits the SGID bit if it is set on the parent inode, while the generic
inode_init_ownerdoes not do this in a few cases where it can create a possible security problem, see commit0fa3ecd87848("Fix up non-directory creation in SGID directories") for details.Quoted 8 lines
Switch XFS to use the generic helper for the normal path to fix this, just keeping the simple field inheritance open coded for the case of the non-sgid case with the bsdgrpid mount option.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Reported-by Christian Brauner <[email protected]>Signed-off-by Christoph Hellwig <[email protected]>---
Reviewed-by Christian Brauner <[email protected]>I ran the idmapped mounts xfstests on this patchset. With this patch applied I was able to remove the special casing for xfs (apart from the irix compatibility check) and got clean test runs:
Quoted 7 lines
Quoted 2 lines
1. with regular setgid inheritance rules
root@f2-vm:/xfstests# ./check generic/622Is this test posted to fstests somewhere?
FWIW the code change looks reasonable to me, but I wanted to see the functionality exercise test first. :)
(The test is part of the idmapped mount testsuite. This will be sent for
inclusion after the v5.12 merge window has closed.)
I'll paste the test here (The one on list still works around the xfs bug and is thus not suitable for testing a fixed xfs. :)) and the complete test is found under [1]:
/* The following tests are concerned with setgid inheritance. These can be
* filesystem type specific. For xfs, if a new file or directory is created
* within a setgid directory and irix_sgid_inhiert is set then inherit the
* setgid bit if the caller is in the group of the directory. */
static int setgid_create(void)
{
int fret = -1;
int file1_fd = -EBADF;
pid_t pid;
if (!caps_supported())
return 0;
if (fchmod(t_dir1_fd, S_IRUSR |
S_IWUSR |
S_IRGRP |
S_IWGRP |
S_IROTH |
S_IWOTH |
S_IXUSR |
S_IXGRP |
S_IXOTH |
S_ISGID), 0) {
log_stderr("failure: fchmod");
goto out;
}
/* Verify that the setgid bit got raised. */
if (!is_setgid(t_dir1_fd, "", AT_EMPTY_PATH)) {
log_stderr("failure: is_setgid");
goto out;
}
pid = fork();
if (pid < 0) {
log_stderr("failure: fork");
goto out;
}
if (pid == 0) {
/* create regular file via open() */
file1_fd = openat(t_dir1_fd, FILE1, O_CREAT | O_EXCL | O_CLOEXEC, S_IXGRP | S_ISGID);
if (file1_fd < 0)
die("failure: create");
/* We're capable_wrt_inode_uidgid() and also our fsgid matches
* the directories gid.
*/
if (!is_setgid(t_dir1_fd, FILE1, 0))
die("failure: is_setgid");
/* create directory */
if (mkdirat(t_dir1_fd, DIR1, 0000))
die("failure: create");
/* Directories always inherit the setgid bit. */
if (!is_setgid(t_dir1_fd, DIR1, 0))
die("failure: is_setgid");
if (unlinkat(t_dir1_fd, FILE1, 0))
die("failure: delete");
if (unlinkat(t_dir1_fd, DIR1, AT_REMOVEDIR))
die("failure: delete");
exit(EXIT_SUCCESS);
}
if (wait_for_pid(pid))
goto out;
pid = fork();
if (pid < 0) {
log_stderr("failure: fork");
goto out;
}
if (pid == 0) {
if (!switch_ids(0, 10000))
die("failure: switch_ids");
if (!caps_down())
die("failure: caps_down");
/* create regular file via open() */
file1_fd = openat(t_dir1_fd, FILE1, O_CREAT | O_EXCL | O_CLOEXEC, S_IXGRP | S_ISGID);
if (file1_fd < 0)
die("failure: create");
/* Neither in_group_p() nor capable_wrt_inode_uidgid() so setgid
* bit needs to be stripped.
*/
if (is_setgid(t_dir1_fd, FILE1, 0))
die("failure: is_setgid");
/* create directory */
if (mkdirat(t_dir1_fd, DIR1, 0000))
die("failure: create");
if (xfs_irix_sgid_inherit_enabled()) {
/* We're not in_group_p(). */
if (is_setgid(t_dir1_fd, DIR1, 0))
die("failure: is_setgid");
} else {
/* Directories always inherit the setgid bit. */
if (!is_setgid(t_dir1_fd, DIR1, 0))
die("failure: is_setgid");
}
exit(EXIT_SUCCESS);
}
if (wait_for_pid(pid))
goto out;
fret = 0;
out:
safe_close(file1_fd);
return fret;
}[1]: https://gitlab.com/brauner/xfstests/-/commit/d1859aecc341edeefe712ab8a3fa63159356c119.patch
and specifically