fs: improve setgid stripping consistency even more
Subsystems (7)
Christian Brauner
maintainerv3 cover fs: improve setgid stripping consistency even more
From: "Christian Brauner (Microsoft)" <[email protected]>
Hey everyone,
A long while ago I found a few setgid inheritance bugs in overlayfs in certain conditions. Amir recently picked this back up in
https://lore.kernel.org/linux-fsdevel/[email protected]
and I jumped on board to fix this more generally. This series should make setgid stripping more consistent and fix the related overlayfs bugs.
Currently setgid stripping in file_remove_privs()'s should_remove_suid() helper is inconsistent with other parts of the vfs. Specifically, it only raises ATTR_KILL_SGID if the inode is S_ISGID and S_IXGRP but not if the inode isn't in the caller's groups and the caller isn't privileged over the inode although we require this already in setattr_prepare() and setattr_copy() and so all filesystem implement this requirement implicitly because they have to use setattr_{prepare,copy}() anyway.
But the inconsistency shows up in setgid stripping bugs for overlayfs in xfstests (e.g., generic/673, generic/683, generic/685, generic/686, generic/687). For example, we test whether suid and setgid stripping works correctly when performing various write-like operations as an unprivileged user (fallocate, reflink, write, etc.):
echo "Test 1 - qa_user, non-exec file $verb"setup_testfile
chmod a+rws $junk_filecommit_and_check "$qa_user" "$verb" 64k 64k
The test basically creates a file with 6666 permissions. While the file has the S_ISUID and S_ISGID bits set it does not have the S_IXGRP set. On a regular filesystem like xfs what will happen is:
sys_fallocate()
-> vfs_fallocate()
-> xfs_file_fallocate()
-> file_modified()
-> __file_remove_privs()
-> dentry_needs_remove_privs()
-> should_remove_suid()
-> __remove_privs()
newattrs.ia_valid = ATTR_FORCE | kill;
-> notify_change()
-> setattr_copy()In should_remove_suid() we can see that ATTR_KILL_SUID is raised unconditionally because the file in the test has S_ISUID set.
But we also see that ATTR_KILL_SGID won't be set because while the file is S_ISGID it is not S_IXGRP (see above) which is a condition for ATTR_KILL_SGID being raised.
So by the time we call notify_change() we have attr->ia_valid set to ATTR_KILL_SUID | ATTR_FORCE. Now notify_change() sees that ATTR_KILL_SUID is set and does:
ia_valid = attr->ia_valid |= ATTR_MODEattr->ia_mode = (inode->i_mode & ~S_ISUID);
which means that when we call setattr_copy() later we will definitely update inode->i_mode. Note that attr->ia_mode still contains S_ISGID.
Now we call into the filesystem's ->setattr() inode operation which will end up calling setattr_copy(). Since ATTR_MODE is set we will hit:
if (ia_valid & ATTR_MODE) {
umode_t mode = attr->ia_mode;
vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
if (!vfsgid_in_group_p(vfsgid) &&
!capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
mode &= ~S_ISGID;
inode->i_mode = mode;
}and since the caller in the test is neither capable nor in the group of the inode the S_ISGID bit is stripped.
But assume the file isn't suid then ATTR_KILL_SUID won't be raised which has the consequence that neither the setgid nor the suid bits are stripped even though it should be stripped because the inode isn't in the caller's groups and the caller isn't privileged over the inode.
If overlayfs is in the mix things become a bit more complicated and the bug shows up more clearly. When e.g., ovl_setattr() is hit from ovl_fallocate()'s call to file_remove_privs() then ATTR_KILL_SUID and ATTR_KILL_SGID might be raised but because the check in notify_change() is questioning the ATTR_KILL_SGID flag again by requiring S_IXGRP for it to be stripped the S_ISGID bit isn't removed even though it should be stripped:
sys_fallocate()
-> vfs_fallocate()
-> ovl_fallocate()
-> file_remove_privs()
-> dentry_needs_remove_privs()
-> should_remove_suid()
-> __remove_privs()
newattrs.ia_valid = ATTR_FORCE | kill;
-> notify_change()
-> ovl_setattr()
// TAKE ON MOUNTER'S CREDS
-> ovl_do_notify_change()
-> notify_change()
// GIVE UP MOUNTER'S CREDS
// TAKE ON MOUNTER'S CREDS
-> vfs_fallocate()
-> xfs_file_fallocate()
-> file_modified()
-> __file_remove_privs()
-> dentry_needs_remove_privs()
-> should_remove_suid()
-> __remove_privs()
newattrs.ia_valid = attr_force | kill;
-> notify_change()The fix for all of this is to make file_remove_privs()'s should_remove_suid() helper to perform the same checks as we already require in setattr_prepare() and setattr_copy() and have notify_change() not pointlessly requiring S_IXGRP again. It doesn't make any sense in the first place because the caller must calculate the flags via should_remove_suid() anyway which would raise ATTR_KILL_SGID.
While we're at it we move should_remove_suid() from inode.c to attr.c where it belongs with the rest of the iattr helpers. Especially since it returns ATTR_KILL_S{G,U}ID flags. We also rename it to setattr_should_drop_suidgid() to better reflect that it indicates both setuid and setgid bit removal and also that it returns attr flags.
Running xfstests with this doesn't report any regressions. We should really try and use consistent checks.
Thanks!
Christian
Amir Goldstein (2):
ovl: remove privs in ovl_copyfile()
ovl: remove privs in ovl_fallocate()
Christian Brauner (3):
attr: add setattr_drop_sgid()
attr: add setattr_should_drop_sgid()
attr: use consistent sgid stripping checks
Documentation/trace/ftrace.rst | 2 +- fs/attr.c | 92 +++++++++++++++++++++++++++++++--- fs/fuse/file.c | 2 +- fs/inode.c | 36 ++----------- fs/internal.h | 5 +- fs/ocfs2/file.c | 4 +- fs/open.c | 8 +-- fs/overlayfs/file.c | 28 +++++++++-- include/linux/fs.h | 2 +- 9 files changed, 128 insertions(+), 51 deletions(-)
base-commit: 9abf2313adc1ca1b6180c508c25f22f9395cc780
Signature · 1 line
2.34.1
Christian Brauner
maintainerPATCH v3 1/5 attr: add setattr_drop_sgid()
In setattr_{copy,prepare}() we need to perform the same permission checks to determine whether we need to drop the setgid bit or not. Instead of open-coding it twice add a simple helper the encapsulates the logic. We will reuse this helpers to make dropping the setgid bit during write operations more consistent in a follow up patch.
---
Notes:
/* v2 */
patch added
/* v3 */
Amir Goldstein <[email protected]>:
- Return 0 or ATTR_KILL_SGID to make all dropping helpers behave similarly.
fs/attr.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)
fs/attr.c +24 -5
@@ -18,6 +18,27 @@#include <linux/evm.h> #include <linux/ima.h> +/** +* setattr_drop_sgid - check generic setgid permissions +* @mnt_userns: user namespace of the mount @inode was found from +* @inode: inode to check +* @vfsgid: the new/current vfsgid of @inode +* +* This function determines whether the setgid bit needs to be removed because +* the caller lacks privileges over the inode. +* +* Return: ATTR_KILL_SGID if the setgid bit needs to be removed, 0 if not. +*/ +static int setattr_drop_sgid (struct user_namespace *mnt_userns, +const struct inode *inode,vfsgid_t vfsgid) +{ +if (vfsgid_in_group_p (vfsgid)) +return 0 ; +if (capable_wrt_inode_uidgid (mnt_userns, inode,CAP_FSETID )) +return 0 ; +return ATTR_KILL_SGID ; +} +/** * chown_ok - verify permissions to chown inode * @mnt_userns: user namespace of the mount @inode was found from
@@ -140,8 +161,7 @@ int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry, vfsgid =i_gid_into_vfsgid (mnt_userns, inode);/* Also check the setgid bit! */ -if (!vfsgid_in_group_p (vfsgid) && - !capable_wrt_inode_uidgid (mnt_userns, inode,CAP_FSETID )) +if (setattr_drop_sgid (mnt_userns, inode, vfsgid)) attr->ia_mode &= ~S_ISGID ; }
@@ -251,9 +271,8 @@ void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode, inode->i_ctime = attr->ia_ctime;if (ia_valid &ATTR_MODE ) {umode_t mode = attr->ia_mode; -vfsgid_t vfsgid =i_gid_into_vfsgid (mnt_userns, inode); -if (!vfsgid_in_group_p (vfsgid) && - !capable_wrt_inode_uidgid (mnt_userns, inode,CAP_FSETID )) +if (setattr_drop_sgid (mnt_userns, inode, +i_gid_into_vfsgid (mnt_userns, inode))) mode &= ~S_ISGID ; inode->i_mode = mode; }
Signature · 1 line
2.34.1
Re: [PATCH v3 1/5] attr: add setattr_drop_sgid()
Quoted 8 lines · Christian Brauner
In setattr_{copy,prepare}() we need to perform the same permission checks to determine whether we need to drop the setgid bit or not. Instead of open-coding it twice add a simple helper the encapsulates the logic. We will reuse this helpers to make dropping the setgid bit during write operations more consistent in a follow up patch.
Signed-off-by Christian Brauner (Microsoft) <[email protected]>
Quoted 70 lines
---
Notes:
/* v2 */
patch added/* v3 */
Amir Goldstein <[email protected]>:
- Return 0 orATTR_KILL_SGIDto make all dropping helpers behave similarly.fs/attr.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)fs/attr.c +24
-6@@ -18,6 +18,27 @@#include <linux/evm.h> #include <linux/ima.h> +/** +* setattr_drop_sgid - check generic setgid permissions +* @mnt_userns: user namespace of the mount @inode was found from +* @inode: inode to check +* @vfsgid: the new/current vfsgid of @inode +* +* This function determines whether the setgid bit needs to be removed because +* the caller lacks privileges over the inode. +* +* Return: ATTR_KILL_SGID if the setgid bit needs to be removed, 0 if not. +*/ +static int setattr_drop_sgid (struct user_namespace *mnt_userns, +const struct inode *inode,vfsgid_t vfsgid) +{ +if (vfsgid_in_group_p (vfsgid)) +return 0 ; +if (capable_wrt_inode_uidgid (mnt_userns, inode,CAP_FSETID )) +return 0 ; +return ATTR_KILL_SGID ; +} +/** * chown_ok - verify permissions to chown inode * @mnt_userns: user namespace of the mount @inode was found from @@ -140,8 +161,7 @@ int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry, vfsgid =i_gid_into_vfsgid (mnt_userns, inode);/* Also check the setgid bit! */ -if (!vfsgid_in_group_p (vfsgid) && - !capable_wrt_inode_uidgid (mnt_userns, inode,CAP_FSETID )) +if (setattr_drop_sgid (mnt_userns, inode, vfsgid)) attr->ia_mode &= ~S_ISGID ; }@@ -251,9 +271,8 @@ void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode, inode->i_ctime = attr->ia_ctime;if (ia_valid &ATTR_MODE ) {umode_t mode = attr->ia_mode; -vfsgid_t vfsgid =i_gid_into_vfsgid (mnt_userns, inode); -if (!vfsgid_in_group_p (vfsgid) && - !capable_wrt_inode_uidgid (mnt_userns, inode,CAP_FSETID )) +if (setattr_drop_sgid (mnt_userns, inode, +i_gid_into_vfsgid (mnt_userns, inode))) mode &= ~S_ISGID ; inode->i_mode = mode; } --2.34.1
Christian Brauner
maintainerPATCH v3 2/5 attr: add setattr_should_drop_sgid()
In reply to Christian BraunerThe current setgid stripping logic during write and ownership change operations is inconsistent and strewn over multiple places. In order to consolidate it and make more consistent we'll add a new helper setattr_should_drop_sgid(). The function retains the old behavior where we remove the S_ISGID bit unconditionally when S_IXGRP is set but also when it isn't set and the caller is neither in the group of the inode nor privileged over the inode.
We will use this helper both in write operation permission removal such as file_remove_privs() as well as in ownership change operations.
---
Notes:
/* v2 */
Dave Chinner <[email protected]>:
- Use easier to follow logic in the new helper.
/* v3 */
Amir Goldstein <[email protected]>:
- Rename helper from should_remove_sgid() to setattr_should_drop_sgid() to better indicate its semantics.
- Return setattr_should_drop_sgid() directly now that it returns ATTR_KILL_SGID instead of a boolean.
fs/attr.c | 26 ++++++++++++++++++++++++++ fs/internal.h | 2 ++ 2 files changed, 28 insertions(+)
fs/attr.c +26 -0
@@ -39,6 +39,32 @@ static int setattr_drop_sgid(struct user_namespace *mnt_userns,return ATTR_KILL_SGID ; } +/** +* setattr_should_drop_sgid - determine whether the setgid bit needs to be +* removed +* @mnt_userns: user namespace of the mount @inode was found from +* @inode: inode to check +* +* This function determines whether the setgid bit needs to be removed. +* We retain backwards compatibility and require setgid bit to be removed +* unconditionally if S_IXGRP is set. Otherwise we have the exact same +* requirements as setattr_prepare() and setattr_copy(). +* +* Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise. +*/ +int setattr_should_drop_sgid (struct user_namespace *mnt_userns, +const struct inode *inode) +{ +umode_t mode = inode->i_mode; + +if (!(mode &S_ISGID )) +return 0 ; +if (mode &S_IXGRP ) +return ATTR_KILL_SGID ; +return setattr_drop_sgid (mnt_userns, inode, +i_gid_into_vfsgid (mnt_userns, inode)); +} +/** * chown_ok - verify permissions to chown inode * @mnt_userns: user namespace of the mount @inode was found from
fs/internal.h +2 -0
@@ -234,3 +234,5 @@ int do_setxattr(struct user_namespace *mnt_userns, struct dentry *dentry, struct xattr_ctx *ctx);ssize_t __kernel_write_iter (struct file *file,struct iov_iter *from,loff_t *pos); +int setattr_should_drop_sgid (struct user_namespace *mnt_userns, +const struct inode *inode);
Signature · 1 line
2.34.1
Re: [PATCH v3 2/5] attr: add setattr_should_drop_sgid()
Quoted 9 lines · Christian Brauner
The current setgid stripping logic during write and ownership change operations is inconsistent and strewn over multiple places. In order to consolidate it and make more consistent we'll add a new helper
setattr_should_drop_sgid(). The function retains the old behavior where we remove theS_ISGIDbit unconditionally whenS_IXGRPis set but also when it isn't set and the caller is neither in the group of the inode nor privileged over the inode.
Quoted 4 lines
We will use this helper both in write operation permission removal such as
file_remove_privs()as well as in ownership change operations.Signed-off-by Christian Brauner (Microsoft) <[email protected]>
Some nits, but otherwise
Quoted 60 lines
---
Notes:
/* v2 */
Dave Chinner <[email protected]>:
- Use easier to follow logic in the new helper./* v3 */
Amir Goldstein <[email protected]>:
- Rename helper fromshould_remove_sgid()tosetattr_should_drop_sgid()to better indicate its semantics.
- Returnsetattr_should_drop_sgid()directly now that it returnsATTR_KILL_SGIDinstead of a boolean.fs/attr.c | 26 ++++++++++++++++++++++++++ fs/internal.h | 2 ++ 2 files changed, 28 insertions(+)fs/attr.c +26
-0@@ -39,6 +39,32 @@ static int setattr_drop_sgid(struct user_namespace *mnt_userns,return ATTR_KILL_SGID ; } +/** +* setattr_should_drop_sgid - determine whether the setgid bit needs to be +* removed +* @mnt_userns: user namespace of the mount @inode was found from +* @inode: inode to check +* +* This function determines whether the setgid bit needs to be removed. +* We retain backwards compatibility and require setgid bit to be removed +* unconditionally if S_IXGRP is set. Otherwise we have the exact same +* requirements as setattr_prepare() and setattr_copy(). +* +* Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise. +*/ +int setattr_should_drop_sgid (struct user_namespace *mnt_userns, +const struct inode *inode) +{ +umode_t mode = inode->i_mode; + +if (!(mode &S_ISGID )) +return 0 ; +if (mode &S_IXGRP ) +return ATTR_KILL_SGID ; +return setattr_drop_sgid (mnt_userns, inode, +i_gid_into_vfsgid (mnt_userns, inode)); +} +/** * chown_ok - verify permissions to chown inode * @mnt_userns: user namespace of the mount @inode was found from fs/internal.h +0
-0
fs/internal.h int do_setxattr(struct user_namespace *mnt_userns, struct dentry *dentry, · line 238
Quoted 5 lines
fs/internal.h +2
-0@@ -234,3 +234,5 @@ int do_setxattr(struct user_namespace *mnt_userns, struct dentry *dentry, struct xattr_ctx *ctx);ssize_t __kernel_write_iter (struct file *file,struct iov_iter *from,loff_t *pos); +int setattr_should_drop_sgid (struct user_namespace *mnt_userns, +const struct inode *inode);
Please add a section:
/*
* fs/attr.c:
*/
Might as well as the missing section for read_write.c above __kernel_write_iter() while at it.
Thanks,
Amir.
Christian Brauner
maintainerPATCH v3 3/5 attr: use consistent sgid stripping checks
In reply to Christian BraunerCurrently setgid stripping in file_remove_privs()'s should_remove_suid() helper is inconsistent with other parts of the vfs. Specifically, it only raises ATTR_KILL_SGID if the inode is S_ISGID and S_IXGRP but not if the inode isn't in the caller's groups and the caller isn't privileged over the inode although we require this already in setattr_prepare() and setattr_copy() and so all filesystem implement this requirement implicitly because they have to use setattr_{prepare,copy}() anyway.
But the inconsistency shows up in setgid stripping bugs for overlayfs in xfstests (e.g., generic/673, generic/683, generic/685, generic/686, generic/687). For example, we test whether suid and setgid stripping works correctly when performing various write-like operations as an unprivileged user (fallocate, reflink, write, etc.):
echo "Test 1 - qa_user, non-exec file $verb"setup_testfile
chmod a+rws $junk_filecommit_and_check "$qa_user" "$verb" 64k 64k
The test basically creates a file with 6666 permissions. While the file has the S_ISUID and S_ISGID bits set it does not have the S_IXGRP set. On a regular filesystem like xfs what will happen is:
sys_fallocate()
-> vfs_fallocate()
-> xfs_file_fallocate()
-> file_modified()
-> __file_remove_privs()
-> dentry_needs_remove_privs()
-> should_remove_suid()
-> __remove_privs()
newattrs.ia_valid = ATTR_FORCE | kill;
-> notify_change()
-> setattr_copy()In should_remove_suid() we can see that ATTR_KILL_SUID is raised unconditionally because the file in the test has S_ISUID set.
But we also see that ATTR_KILL_SGID won't be set because while the file is S_ISGID it is not S_IXGRP (see above) which is a condition for ATTR_KILL_SGID being raised.
So by the time we call notify_change() we have attr->ia_valid set to ATTR_KILL_SUID | ATTR_FORCE. Now notify_change() sees that ATTR_KILL_SUID is set and does:
ia_valid = attr->ia_valid |= ATTR_MODEattr->ia_mode = (inode->i_mode & ~S_ISUID);
which means that when we call setattr_copy() later we will definitely update inode->i_mode. Note that attr->ia_mode still contains S_ISGID.
Now we call into the filesystem's ->setattr() inode operation which will end up calling setattr_copy(). Since ATTR_MODE is set we will hit:
if (ia_valid & ATTR_MODE) {
umode_t mode = attr->ia_mode;
vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
if (!vfsgid_in_group_p(vfsgid) &&
!capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
mode &= ~S_ISGID;
inode->i_mode = mode;
}and since the caller in the test is neither capable nor in the group of the inode the S_ISGID bit is stripped.
But assume the file isn't suid then ATTR_KILL_SUID won't be raised which has the consequence that neither the setgid nor the suid bits are stripped even though it should be stripped because the inode isn't in the caller's groups and the caller isn't privileged over the inode.
If overlayfs is in the mix things become a bit more complicated and the bug shows up more clearly. When e.g., ovl_setattr() is hit from ovl_fallocate()'s call to file_remove_privs() then ATTR_KILL_SUID and ATTR_KILL_SGID might be raised but because the check in notify_change() is questioning the ATTR_KILL_SGID flag again by requiring S_IXGRP for it to be stripped the S_ISGID bit isn't removed even though it should be stripped:
sys_fallocate()
-> vfs_fallocate()
-> ovl_fallocate()
-> file_remove_privs()
-> dentry_needs_remove_privs()
-> should_remove_suid()
-> __remove_privs()
newattrs.ia_valid = ATTR_FORCE | kill;
-> notify_change()
-> ovl_setattr()
// TAKE ON MOUNTER'S CREDS
-> ovl_do_notify_change()
-> notify_change()
// GIVE UP MOUNTER'S CREDS
// TAKE ON MOUNTER'S CREDS
-> vfs_fallocate()
-> xfs_file_fallocate()
-> file_modified()
-> __file_remove_privs()
-> dentry_needs_remove_privs()
-> should_remove_suid()
-> __remove_privs()
newattrs.ia_valid = attr_force | kill;
-> notify_change()The fix for all of this is to make file_remove_privs()'s should_remove_suid() helper to perform the same checks as we already require in setattr_prepare() and setattr_copy() and have notify_change() not pointlessly requiring S_IXGRP again. It doesn't make any sense in the first place because the caller must calculate the flags via should_remove_suid() anyway which would raise ATTR_KILL_SGID.
While we're at it we move should_remove_suid() from inode.c to attr.c where it belongs with the rest of the iattr helpers. Especially since it returns ATTR_KILL_S{G,U}ID flags. We also rename it to setattr_should_drop_suidgid() to better reflect that it indicates both setuid and setgid bit removal and also that it returns attr flags.
Running xfstests with this doesn't report any regressions. We should really try and use consistent checks.
---
Notes:
/* v2 */
Amir Goldstein <[email protected]>:
- mention xfstests that failed prior to that
Christian Brauner <[email protected]>:
- Use should_remove_sgid() in chown_common() just like we do in do_truncate().
/* v3 */
Christian Brauner <[email protected]>:
- Move should_remove_suid() from inode.c to attr.c where it belongs with the rest of the iattr helpers. Especially since it returns ATTR_KILL_S{G,U}ID flags. We also rename it to setattr_should_drop_suidgid() to better reflect that it indicates both setuid and setgid bit removal and also that it returns attr flags.
- Since setattr_should_drop_suidgid() only needs the inode pass an inode, not a dentry.
Documentation/trace/ftrace.rst | 2 +- fs/attr.c | 37 +++++++++++++++++++++++++++++++++- fs/fuse/file.c | 2 +- fs/inode.c | 36 ++++----------------------------- fs/internal.h | 3 ++- fs/ocfs2/file.c | 4 ++-- fs/open.c | 8 ++++---- include/linux/fs.h | 2 +- 8 files changed, 51 insertions(+), 43 deletions(-)
Documentation/trace/ftrace.rst +1 -1
@@ -2940,7 +2940,7 @@ Produces:: bash-1994 [000] .... 4342.324898: ima_get_action <-process_measurement bash-1994 [000] .... 4342.324898: ima_match_policy <-ima_get_action bash-1994 [000] .... 4342.324899: do_truncate <-do_last - bash-1994 [000] .... 4342.324899: should_remove_suid <-do_truncate + bash-1994 [000] .... 4342.324899: setattr_should_drop_suidgid <-do_truncate bash-1994 [000] .... 4342.324899: notify_change <-do_truncate bash-1994 [000] .... 4342.324900: current_fs_time <-notify_change bash-1994 [000] .... 4342.324900: current_kernel_time <-current_fs_time
fs/attr.c +36 -1
@@ -65,6 +65,41 @@ int setattr_should_drop_sgid(struct user_namespace *mnt_userns, i_gid_into_vfsgid(mnt_userns, inode)); } +/** +* setattr_should_drop_suidgid - determine whether the set{g,u}id bit needs to +* be dropped +* @mnt_userns: user namespace of the mount @inode was found from +* @inode: inode to check +* +* This function determines whether the set{g,u}id bits need to be removed. +* If the setuid bit needs to be removed ATTR_KILL_SUID is returned. If the +* setgid bit needs to be removed ATTR_KILL_SGID is returned. If both +* set{g,u}id bits need to be removed the corresponding mask of both flags is +* returned. +* +* Return: A mask of ATTR_KILL_S{G,U}ID indicating which - if any - setid bits +* to remove, 0 otherwise. +*/ +int setattr_should_drop_suidgid (struct user_namespace *mnt_userns, +struct inode *inode) +{ +umode_t mode = inode->i_mode; +int kill =0 ; + +/* suid always must be killed */ +if (unlikely (mode &S_ISUID )) + kill =ATTR_KILL_SUID ; + +if (unlikely (setattr_should_drop_sgid (mnt_userns, inode))) + kill |=ATTR_KILL_SGID ; + +if (unlikely (kill && !capable (CAP_FSETID ) &&S_ISREG (mode))) +return kill; + +return 0 ; +} +EXPORT_SYMBOL (setattr_should_drop_suidgid); +/** * chown_ok - verify permissions to chown inode * @mnt_userns: user namespace of the mount @inode was found from
@@ -420,7 +455,7 @@ int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry, } }if (ia_valid &ATTR_KILL_SGID ) { -if ((mode & (S_ISGID |S_IXGRP )) == (S_ISGID |S_IXGRP )) { +if (mode &S_ISGID ) {if (!(ia_valid &ATTR_MODE )) { ia_valid = attr->ia_valid |=ATTR_MODE ; attr->ia_mode = inode->i_mode;
fs/fuse/file.c +1 -1
@@ -1313,7 +1313,7 @@ static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)return err;if (fc->handle_killpriv_v2 && -should_remove_suid (file_dentry (file))) { +setattr_should_drop_suidgid (&init_user_ns,file_inode (file))) { goto writethrough; }
fs/inode.c +4 -32
@@ -1948,41 +1948,13 @@ void touch_atime(const struct path *path) }EXPORT_SYMBOL (touch_atime); -/* -* The logic we want is -* -* if suid or (sgid and xgrp) -* remove privs -*/ -int should_remove_suid (struct dentry *dentry) -{ -umode_t mode =d_inode (dentry)->i_mode; -int kill =0 ; - -/* suid always must be killed */ -if (unlikely (mode &S_ISUID )) - kill =ATTR_KILL_SUID ; - -/* -* sgid without any exec bits is just a mandatory locking mark; leave -* it alone. If some exec bits are set, it's a real sgid; kill it. -*/ -if (unlikely ((mode &S_ISGID ) && (mode &S_IXGRP ))) - kill |=ATTR_KILL_SGID ; - -if (unlikely (kill && !capable (CAP_FSETID ) &&S_ISREG (mode))) -return kill; - -return 0 ; -} -EXPORT_SYMBOL (should_remove_suid); -/* * Return mask of changes for notify_change() that need to be done as a * response to write or truncate. Return 0 if nothing has to be changed. * Negative value on error (change should be denied). */ -int dentry_needs_remove_privs (struct dentry *dentry) +int dentry_needs_remove_privs (struct user_namespace *mnt_userns, +struct dentry *dentry) {struct inode *inode =d_inode (dentry);int mask =0 ;
@@ -1991,7 +1963,7 @@ int dentry_needs_remove_privs(struct dentry *dentry)if (IS_NOSEC (inode))return 0 ; - mask =should_remove_suid (dentry); + mask =setattr_should_drop_suidgid (mnt_userns, inode); ret =security_inode_need_killpriv (dentry);if (ret <0 )return ret;
@@ -2023,7 +1995,7 @@ static int __file_remove_privs(struct file *file, unsigned int flags)if (IS_NOSEC (inode) || !S_ISREG (inode->i_mode))return 0 ; - kill =dentry_needs_remove_privs (dentry); + kill =dentry_needs_remove_privs (file_mnt_user_ns (file), dentry);if (kill <0 )return kill;
fs/internal.h +2 -1
@@ -150,7 +150,8 @@ extern int vfs_open(const struct path *, struct file *);* inode.c */ extern long prune_icache_sb (struct super_block *sb,struct shrink_control *sc); -extern int dentry_needs_remove_privs (struct dentry *dentry); +extern int dentry_needs_remove_privs (struct user_namespace *, +struct dentry *dentry);/* * fs-writeback.c
fs/ocfs2/file.c +2 -2
@@ -1991,7 +1991,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode, } } -if (file &&should_remove_suid (file->f_path.dentry)) { +if (file &&setattr_should_drop_suidgid (&init_user_ns,file_inode (file))) { ret =__ocfs2_write_remove_suid (inode, di_bh);if (ret) {mlog_errno (ret);
@@ -2279,7 +2279,7 @@ static int ocfs2_prepare_inode_for_write(struct file *file,* inode. There's also the dinode i_size state which * can be lost via setattr during extending writes (we * set inode->i_size at the end of a write. */ -if (should_remove_suid (dentry)) { +if (setattr_should_drop_suidgid (&init_user_ns, inode)) {if (meta_level ==0 ) { ocfs2_inode_unlock_for_extent_tree(inode, &di_bh,
fs/open.c +4 -4
@@ -54,7 +54,7 @@ int do_truncate(struct user_namespace *mnt_userns, struct dentry *dentry, }/* Remove suid, sgid, and file capabilities on truncate too */ - ret =dentry_needs_remove_privs (dentry); + ret =dentry_needs_remove_privs (mnt_userns, dentry);if (ret <0 )return ret;if (ret)
@@ -723,10 +723,10 @@ int chown_common(const struct path *path, uid_t user, gid_t group)return -EINVAL ;if ((group != (gid_t)-1 ) && !setattr_vfsgid (&newattrs, gid))return -EINVAL ; -if (!S_ISDIR (inode->i_mode)) - newattrs.ia_valid |= -ATTR_KILL_SUID |ATTR_KILL_SGID |ATTR_KILL_PRIV ;inode_lock (inode); +if (!S_ISDIR (inode->i_mode)) + newattrs.ia_valid |=ATTR_KILL_SUID |ATTR_KILL_PRIV | +setattr_should_drop_sgid (mnt_userns, inode);/* Continue to send actual fs values, not the mount values. */ error = security_path_chown( path,
include/linux/fs.h +1 -1
@@ -3104,7 +3104,7 @@ extern void __destroy_inode(struct inode *);extern struct inode *new_inode_pseudo (struct super_block *sb);extern struct inode *new_inode (struct super_block *sb);extern void free_inode_nonrcu (struct inode *inode); -extern int should_remove_suid (struct dentry *); +extern int setattr_should_drop_suidgid (struct user_namespace *,struct inode *);extern int file_remove_privs (struct file *);/*
Signature · 1 line
2.34.1
Amir Goldstein
maintainerRe: [PATCH v3 3/5] attr: use consistent sgid stripping checks
Quoted 118 lines · Christian Brauner
Currently setgid stripping in file_remove_privs()'s
should_remove_suid()helper is inconsistent with other parts of the vfs. Specifically, it only raisesATTR_KILL_SGIDif the inode isS_ISGIDandS_IXGRPbut not if the inode isn't in the caller's groups and the caller isn't privileged over the inode although we require this already insetattr_prepare()andsetattr_copy()and so all filesystem implement this requirement implicitly because they have to use setattr_{prepare,copy}() anyway.But the inconsistency shows up in setgid stripping bugs for overlayfs in xfstests (e.g., generic/673, generic/683, generic/685, generic/686, generic/687). For example, we test whether suid and setgid stripping works correctly when performing various write-like operations as an unprivileged user (fallocate, reflink, write, etc.):
echo "Test 1 -
qa_user, non-exec file $verb"setup_testfile
chmod a+rws $junk_filecommit_and_check"$qa_user" "$verb" 64k 64kThe test basically creates a file with 6666 permissions. While the file has the
S_ISUIDandS_ISGIDbits set it does not have theS_IXGRPset. On a regular filesystem like xfs what will happen is:
sys_fallocate()
->vfs_fallocate()
->xfs_file_fallocate()-> file_modified() -> __file_remove_privs() -> dentry_needs_remove_privs() -> should_remove_suid() -> __remove_privs() newattrs.ia_valid = ATTR_FORCE | kill; -> notify_change() -> setattr_copy()In
should_remove_suid()we can see thatATTR_KILL_SUIDis raised unconditionally because the file in the test hasS_ISUIDset.But we also see that
ATTR_KILL_SGIDwon't be set because while the file isS_ISGIDit is notS_IXGRP(see above) which is a condition forATTR_KILL_SGIDbeing raised.So by the time we call
notify_change()we haveattr->ia_validset toATTR_KILL_SUID|ATTR_FORCE. Nownotify_change()sees thatATTR_KILL_SUIDis set and does:
ia_valid=attr->ia_valid|=ATTR_MODEattr->ia_mode= (inode->i_mode& ~S_ISUID);which means that when we call
setattr_copy()later we will definitely updateinode->i_mode. Note thatattr->ia_modestill containsS_ISGID.Now we call into the filesystem's ->setattr() inode operation which will end up calling
setattr_copy(). SinceATTR_MODEis set we will hit:if (ia_valid & ATTR_MODE) { umode_t mode = attr->ia_mode; vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode); if (!vfsgid_in_group_p(vfsgid) && !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID)) mode &= ~S_ISGID; inode->i_mode = mode; }and since the caller in the test is neither capable nor in the group of the inode the
S_ISGIDbit is stripped.But assume the file isn't suid then
ATTR_KILL_SUIDwon't be raised which has the consequence that neither the setgid nor the suid bits are stripped even though it should be stripped because the inode isn't in the caller's groups and the caller isn't privileged over the inode.If overlayfs is in the mix things become a bit more complicated and the bug shows up more clearly. When e.g.,
ovl_setattr()is hit from ovl_fallocate()'s call tofile_remove_privs()thenATTR_KILL_SUIDandATTR_KILL_SGIDmight be raised but because the check innotify_change()is questioning theATTR_KILL_SGIDflag again by requiringS_IXGRPfor it to be stripped theS_ISGIDbit isn't removed even though it should be stripped:
sys_fallocate()
->vfs_fallocate()
->ovl_fallocate()-> file_remove_privs() -> dentry_needs_remove_privs() -> should_remove_suid() -> __remove_privs() newattrs.ia_valid = ATTR_FORCE | kill; -> notify_change() -> ovl_setattr() // TAKE ON MOUNTER'S CREDS -> ovl_do_notify_change() -> notify_change() // GIVE UP MOUNTER'S CREDS // TAKE ON MOUNTER'S CREDS -> vfs_fallocate() -> xfs_file_fallocate() -> file_modified() -> __file_remove_privs() -> dentry_needs_remove_privs() -> should_remove_suid() -> __remove_privs() newattrs.ia_valid = attr_force | kill; -> notify_change()The fix for all of this is to make file_remove_privs()'s
should_remove_suid()helper to perform the same checks as we already require insetattr_prepare()andsetattr_copy()and havenotify_change()not pointlessly requiringS_IXGRPagain. It doesn't make any sense in the first place because the caller must calculate the flags viashould_remove_suid()anyway which would raiseATTR_KILL_SGID.While we're at it we move
should_remove_suid()frominode.ctoattr.cwhere it belongs with the rest of the iattr helpers. Especially since it returns ATTR_KILL_S{G,U}ID flags. We also rename it tosetattr_should_drop_suidgid()to better reflect that it indicates both setuid and setgid bit removal and also that it returns attr flags.
Quoted 6 lines
Running xfstests with this doesn't report any regressions. We should really try and use consistent checks.
Signed-off-by Christian Brauner (Microsoft) <[email protected]>Reviewed-by Amir Goldstein <[email protected]>Signed-off-by Christian Brauner (Microsoft) <[email protected]>
Doubly signed...
Quoted 10 lines
---
Notes:
/* v2 */
Amir Goldstein <[email protected]>:
- mention xfstests that failed prior to thatChristian Brauner <[email protected]>:
- Useshould_remove_sgid()inchown_common()just like we do indo_truncate().
Quoted 9 lines
/* v3 */
Christian Brauner <[email protected]>:
- Moveshould_remove_suid()frominode.ctoattr.cwhere it belongs with the rest of the iattr helpers. Especially since it returns ATTR_KILL_S{G,U}ID flags. We also rename it tosetattr_should_drop_suidgid()to better reflect that it indicates both setuid and setgid bit removal and also that it returns attr flags.
- Sincesetattr_should_drop_suidgid()only needs the inode pass an inode, not a dentry.
Moving between C files and changing code in the same commit is bad for review.
Please move between C files in a prep patch.
Thanks,
Amir.
Amir Goldstein
maintainerRe: [PATCH v3 3/5] attr: use consistent sgid stripping checks
In reply to Christian BraunerQuoted 192 lines · Christian Brauner
Currently setgid stripping in file_remove_privs()'s
should_remove_suid()helper is inconsistent with other parts of the vfs. Specifically, it only raisesATTR_KILL_SGIDif the inode isS_ISGIDandS_IXGRPbut not if the inode isn't in the caller's groups and the caller isn't privileged over the inode although we require this already insetattr_prepare()andsetattr_copy()and so all filesystem implement this requirement implicitly because they have to use setattr_{prepare,copy}() anyway.But the inconsistency shows up in setgid stripping bugs for overlayfs in xfstests (e.g., generic/673, generic/683, generic/685, generic/686, generic/687). For example, we test whether suid and setgid stripping works correctly when performing various write-like operations as an unprivileged user (fallocate, reflink, write, etc.):
echo "Test 1 -
qa_user, non-exec file $verb"setup_testfile
chmod a+rws $junk_filecommit_and_check"$qa_user" "$verb" 64k 64kThe test basically creates a file with 6666 permissions. While the file has the
S_ISUIDandS_ISGIDbits set it does not have theS_IXGRPset. On a regular filesystem like xfs what will happen is:
sys_fallocate()
->vfs_fallocate()
->xfs_file_fallocate()-> file_modified() -> __file_remove_privs() -> dentry_needs_remove_privs() -> should_remove_suid() -> __remove_privs() newattrs.ia_valid = ATTR_FORCE | kill; -> notify_change() -> setattr_copy()In
should_remove_suid()we can see thatATTR_KILL_SUIDis raised unconditionally because the file in the test hasS_ISUIDset.But we also see that
ATTR_KILL_SGIDwon't be set because while the file isS_ISGIDit is notS_IXGRP(see above) which is a condition forATTR_KILL_SGIDbeing raised.So by the time we call
notify_change()we haveattr->ia_validset toATTR_KILL_SUID|ATTR_FORCE. Nownotify_change()sees thatATTR_KILL_SUIDis set and does:
ia_valid=attr->ia_valid|=ATTR_MODEattr->ia_mode= (inode->i_mode& ~S_ISUID);which means that when we call
setattr_copy()later we will definitely updateinode->i_mode. Note thatattr->ia_modestill containsS_ISGID.Now we call into the filesystem's ->setattr() inode operation which will end up calling
setattr_copy(). SinceATTR_MODEis set we will hit:if (ia_valid & ATTR_MODE) { umode_t mode = attr->ia_mode; vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode); if (!vfsgid_in_group_p(vfsgid) && !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID)) mode &= ~S_ISGID; inode->i_mode = mode; }and since the caller in the test is neither capable nor in the group of the inode the
S_ISGIDbit is stripped.But assume the file isn't suid then
ATTR_KILL_SUIDwon't be raised which has the consequence that neither the setgid nor the suid bits are stripped even though it should be stripped because the inode isn't in the caller's groups and the caller isn't privileged over the inode.If overlayfs is in the mix things become a bit more complicated and the bug shows up more clearly. When e.g.,
ovl_setattr()is hit from ovl_fallocate()'s call tofile_remove_privs()thenATTR_KILL_SUIDandATTR_KILL_SGIDmight be raised but because the check innotify_change()is questioning theATTR_KILL_SGIDflag again by requiringS_IXGRPfor it to be stripped theS_ISGIDbit isn't removed even though it should be stripped:
sys_fallocate()
->vfs_fallocate()
->ovl_fallocate()-> file_remove_privs() -> dentry_needs_remove_privs() -> should_remove_suid() -> __remove_privs() newattrs.ia_valid = ATTR_FORCE | kill; -> notify_change() -> ovl_setattr() // TAKE ON MOUNTER'S CREDS -> ovl_do_notify_change() -> notify_change() // GIVE UP MOUNTER'S CREDS // TAKE ON MOUNTER'S CREDS -> vfs_fallocate() -> xfs_file_fallocate() -> file_modified() -> __file_remove_privs() -> dentry_needs_remove_privs() -> should_remove_suid() -> __remove_privs() newattrs.ia_valid = attr_force | kill; -> notify_change()The fix for all of this is to make file_remove_privs()'s
should_remove_suid()helper to perform the same checks as we already require insetattr_prepare()andsetattr_copy()and havenotify_change()not pointlessly requiringS_IXGRPagain. It doesn't make any sense in the first place because the caller must calculate the flags viashould_remove_suid()anyway which would raiseATTR_KILL_SGID.While we're at it we move
should_remove_suid()frominode.ctoattr.cwhere it belongs with the rest of the iattr helpers. Especially since it returns ATTR_KILL_S{G,U}ID flags. We also rename it tosetattr_should_drop_suidgid()to better reflect that it indicates both setuid and setgid bit removal and also that it returns attr flags.Running xfstests with this doesn't report any regressions. We should really try and use consistent checks.
Signed-off-by Christian Brauner (Microsoft) <[email protected]>Reviewed-by Amir Goldstein <[email protected]>Signed-off-by Christian Brauner (Microsoft) <[email protected]>---
Notes:
/* v2 */
Amir Goldstein <[email protected]>:
- mention xfstests that failed prior to thatChristian Brauner <[email protected]>:
- Useshould_remove_sgid()inchown_common()just like we do indo_truncate()./* v3 */
Christian Brauner <[email protected]>:
- Moveshould_remove_suid()frominode.ctoattr.cwhere it belongs with the rest of the iattr helpers. Especially since it returns ATTR_KILL_S{G,U}ID flags. We also rename it tosetattr_should_drop_suidgid()to better reflect that it indicates both setuid and setgid bit removal and also that it returns attr flags.
- Sincesetattr_should_drop_suidgid()only needs the inode pass an inode, not a dentry.Documentation/trace/ftrace.rst | 2 +- fs/attr.c | 37 +++++++++++++++++++++++++++++++++- fs/fuse/file.c | 2 +- fs/inode.c | 36 ++++----------------------------- fs/internal.h | 3 ++- fs/ocfs2/file.c | 4 ++-- fs/open.c | 8 ++++---- include/linux/fs.h | 2 +- 8 files changed, 51 insertions(+), 43 deletions(-)Documentation/trace/ftrace.rst +1
-1@@ -2940,7 +2940,7 @@ Produces:: bash-1994 [000] .... 4342.324898: ima_get_action <-process_measurement bash-1994 [000] .... 4342.324898: ima_match_policy <-ima_get_action bash-1994 [000] .... 4342.324899: do_truncate <-do_last - bash-1994 [000] .... 4342.324899: should_remove_suid <-do_truncate + bash-1994 [000] .... 4342.324899: setattr_should_drop_suidgid <-do_truncate bash-1994 [000] .... 4342.324899: notify_change <-do_truncate bash-1994 [000] .... 4342.324900: current_fs_time <-notify_change bash-1994 [000] .... 4342.324900: current_kernel_time <-current_fs_timefs/attr.c +17
-0@@ -65,6 +65,41 @@ int setattr_should_drop_sgid(struct user_namespace *mnt_userns, i_gid_into_vfsgid(mnt_userns, inode)); } +/** +* setattr_should_drop_suidgid - determine whether the set{g,u}id bit needs to +* be dropped +* @mnt_userns: user namespace of the mount @inode was found from +* @inode: inode to check +* +* This function determines whether the set{g,u}id bits need to be removed. +* If the setuid bit needs to be removed ATTR_KILL_SUID is returned. If the +* setgid bit needs to be removed ATTR_KILL_SGID is returned. If both +* set{g,u}id bits need to be removed the corresponding mask of both flags is +* returned. +* +* Return: A mask of ATTR_KILL_S{G,U}ID indicating which - if any - setid bits +* to remove, 0 otherwise. +*/ +int setattr_should_drop_suidgid (struct user_namespace *mnt_userns, +struct inode *inode)
fs/attr.c int setattr_should_drop_sgid(struct user_namespace *mnt_userns, · line 94
Quoted 10 lines
fs/attr.c +10
-0+{ +umode_t mode = inode->i_mode; +int kill =0 ; + +/* suid always must be killed */ +if (unlikely (mode &S_ISUID )) + kill =ATTR_KILL_SUID ; + +if (unlikely (setattr_should_drop_sgid (mnt_userns, inode))) + kill |=ATTR_KILL_SGID ;
kill |= setattr_should_drop_sgid(mnt_userns, inode);
Thanks,
Amir.
Christian Brauner
maintainerPATCH v3 4/5 ovl: remove privs in ovl_copyfile()
In reply to Christian BraunerFrom: Amir Goldstein <[email protected]>
Underlying fs doesn't remove privs because copy_range/remap_range are called with privileged mounter credentials.
This fixes some failures in fstest generic/673.
---
Notes:
/* v2 */
Acked-by: Miklos Szeredi <[email protected]>
/* v3 */
unchangedfs/overlayfs/file.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
fs/overlayfs/file.c +14 -2
@@ -567,14 +567,23 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,const struct cred *old_cred;loff_t ret; +inode_lock (inode_out); +if (op !=OVL_DEDUPE ) { +/* Update mode */ +ovl_copyattr (inode_out); + ret =file_remove_privs (file_out); +if (ret) + goto out_unlock; + } + ret =ovl_real_fdget (file_out, &real_out);if (ret) -return ret; + goto out_unlock; ret =ovl_real_fdget (file_in, &real_in);if (ret) {fdput (real_out); -return ret; + goto out_unlock; } old_cred =ovl_override_creds (file_inode (file_out)->i_sb);
@@ -603,6 +612,9 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,fdput (real_in);fdput (real_out); +out_unlock: +inode_unlock (inode_out); +return ret; }
Signature · 1 line
2.34.1
Christian Brauner
maintainerPATCH v3 5/5 ovl: remove privs in ovl_fallocate()
In reply to Christian BraunerFrom: Amir Goldstein <[email protected]>
Underlying fs doesn't remove privs because fallocate is called with privileged mounter credentials.
This fixes some failure in fstests generic/683..687.
---
Notes:
/* v2 */
Acked-by: Miklos Szeredi <[email protected]>
/* v3 */
unchangedfs/overlayfs/file.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
fs/overlayfs/file.c +11 -1
@@ -517,9 +517,16 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t lenconst struct cred *old_cred;int ret; +inode_lock (inode); +/* Update mode */ +ovl_copyattr (inode); + ret =file_remove_privs (file); +if (ret) + goto out_unlock; + ret =ovl_real_fdget (file, &real);if (ret) -return ret; + goto out_unlock; old_cred =ovl_override_creds (file_inode (file)->i_sb); ret =vfs_fallocate (real.file, mode, offset, len);
@@ -530,6 +537,9 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t lenfdput (real); +out_unlock: +inode_unlock (inode); +return ret; }
Signature · 1 line
2.34.1