FUSE: regression when clearing setuid bits on chown
Subsystems (2)
Hi Miklos,
I think we've found a "regression" that has crept in due to this patch:
commit a09f99eddef44035ec764075a37bace8181bec38
Author: Miklos Szeredi <[email protected]>
Date: Sat Oct 1 07:32:32 2016 +0200
fuse: fix killing s[ug]id in setattr
Basically, the pjdfstests set the ownership of a file to 06555, and then chowns it (as root) to a new uid/gid. Prior to the patch above, fuse would send down a setattr with both the uid/gid change and a new mode. Now, it just sends down the uid/gid change.
Technically this is NOTABUG, since POSIX doesn't _require_ that we clear these bits for a privileged process, but Linux (wisely) has done that and I think we don't want to change that behavior here.
So, the issue I think is the use of should_remove_suid, which will always return 0 when the process has CAP_FSETID. That's appropriate (I think) for write/truncate, but not chown, where we want to ignore that.
Thoughts on the right fix here? A simple fix would be to add an "override" bool to should_remove_suid, but maybe there's some more elegant way to do it?
Signature · 1 line
Jeff Layton <[email protected]>
Miklos Szeredi
maintainerQuoted 10 lines · Jeff Layton
Hi Miklos,
I think we've found a "regression" that has crept in due to this patch:
commit
a09f99eddef44035ec764075a37bace8181bec38
Author: Miklos Szeredi <[email protected]>
Date: Sat Oct 1 07:32:32 2016 +0200fuse: fix killing s[ug]id in setattr
Thanks for the report. Below patch should fix it. Not sure what I've been thinking when adding all that complexity, when the issue here was not whether to clear the suid or not, but how stale the mode used is.
Thanks,
Miklos
---
From: Miklos Szeredi <[email protected]>
Subject: fuse: fix clearing suid, sgid for chown()
Basically, the pjdfstests set the ownership of a file to 06555, and then chowns it (as root) to a new uid/gid. Prior to commit a09f99eddef4 ("fuse:
fix killing s[ug]id in setattr"), fuse would send down a setattr with both the uid/gid change and a new mode. Now, it just sends down the uid/gid change.
Technically this is NOTABUG, since POSIX doesn't _require_ that we clear these bits for a privileged process, but Linux (wisely) has done that and I think we don't want to change that behavior here.
This is caused by the use of should_remove_suid(), which will always return 0 when the process has CAP_FSETID.
In fact we really don't need to be calling should_remove_suid() at all, since we've already been indicated that we should remove the suid, we just don't want to use a (very) stale mode for that.
This patch should fix the above as well as simplify the logic.
--- fs/fuse/dir.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)
fs/fuse/dir.c +2 -12
@@ -1739,8 +1739,6 @@ static int fuse_setattr(struct dentry *e* This should be done on write(), truncate() and chown(). */ if (!fc->handle_killpriv) { -int kill; -/* * ia_mode calculation may have used stale i_mode. * Refresh and recalculate.
@@ -1749,16 +1747,8 @@ static int fuse_setattr(struct dentry *eif (ret)return ret; - attr->ia_mode = inode->i_mode; - kill =should_remove_suid (entry); -if (kill &ATTR_KILL_SUID ) { - attr->ia_valid |=ATTR_MODE ; - attr->ia_mode &= ~S_ISUID ; - } -if (kill &ATTR_KILL_SGID ) { - attr->ia_valid |=ATTR_MODE ; - attr->ia_mode &= ~S_ISGID ; - } + attr->ia_mode = inode->i_mode & ~(S_ISUID |S_ISGID ); + attr->ia_valid |=ATTR_MODE ; } }if (!attr->ia_valid)
Quoted 66 lines · Miklos Szeredi
Quoted 11 lines · Jeff Layton
Hi Miklos,
I think we've found a "regression" that has crept in due to this patch:
commit
a09f99eddef44035ec764075a37bace8181bec38
Author: Miklos Szeredi <[email protected]>
Date: Sat Oct 1 07:32:32 2016 +0200fuse: fix killing s[ug]id in setattr
Thanks for the report. Below patch should fix it. Not sure what I've been thinking when adding all that complexity, when the issue here was not whether to clear the suid or not, but how stale the mode used is.
Thanks,
Miklos---
From: Miklos Szeredi <[email protected]>
Subject: fuse: fix clearing suid, sgid forchown()Basically, the pjdfstests set the ownership of a file to 06555, and then chowns it (as root) to a new uid/gid. Prior to commit
a09f99eddef4("fuse:
fix killing s[ug]id in setattr"), fuse would send down a setattr with both the uid/gid change and a new mode. Now, it just sends down the uid/gid change.Technically this is NOTABUG, since POSIX doesn't _require_ that we clear these bits for a privileged process, but Linux (wisely) has done that and I think we don't want to change that behavior here.
This is caused by the use of
should_remove_suid(), which will always return 0 when the process hasCAP_FSETID.In fact we really don't need to be calling
should_remove_suid()at all, since we've already been indicated that we should remove the suid, we just don't want to use a (very) stale mode for that.This patch should fix the above as well as simplify the logic.
Reported-by Jeff Layton <[email protected]>Signed-off-by Miklos Szeredi <[email protected]>Fixes a09f99eddef4 ("fuse: fix killing s[ug]id in setattr")Cc <[email protected]>--- fs/fuse/dir.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)fs/fuse/dir.c +0
-2@@ -1739,8 +1739,6 @@ static int fuse_setattr(struct dentry *e* This should be done on write(), truncate() and chown(). */ if (!fc->handle_killpriv) { -int kill; -/* * ia_mode calculation may have used stale i_mode. * Refresh and recalculate. @@ -1749,16 +1747,8 @@ static int fuse_setattr(struct dentry *eif (ret)return ret;
fs/fuse/dir.c static int fuse_setattr(struct dentry *e · line 1761
Quoted 10 lines
fs/fuse/dir.c +0
-10- attr->ia_mode = inode->i_mode; - kill =should_remove_suid (entry); -if (kill &ATTR_KILL_SUID ) { - attr->ia_valid |=ATTR_MODE ; - attr->ia_mode &= ~S_ISUID ; - } -if (kill &ATTR_KILL_SGID ) { - attr->ia_valid |=ATTR_MODE ; - attr->ia_mode &= ~S_ISGID ; - }
Should we be checking that the latest i_mode even has these bits before sending down the mode change?
fs/fuse/dir.c static int fuse_setattr(struct dentry *e · line 1754
Quoted 5 lines
fs/fuse/dir.c +1
-0> + attr->ia_mode = inode->i_mode & ~(S_ISUID | S_ISGID); + attr->ia_valid |=ATTR_MODE ; } }if (!attr->ia_valid)
Yeah that is quite a bit simpler.
That said...if either ATTR_KILL flag is set, then we're going to end up clearing both bits in the new mode. I guess that's ok since we always want to clear them both, and we'll only have one set and not the other if one of the mode bits was set and not the other.
But...I'm starting to wonder if we really need two flags for this. Would be be better served with a single ATTR_KILL_SUID_SGID flag? I wonder if that would simplify some of the logic in the whole setuid clearing morass.
--
Jeff Layton <[email protected]>
Miklos Szeredi
maintainerQuoted 2 lines · Jeff Layton
Should we be checking that the latest
i_modeeven has these bits before sending down the mode change?
Fixed, see updated patch below.
It also fixes a bug in the previous patch where in case of "-rwsrwSr-x" it would clear the sgid bit without execute.
Quoted 14 lines
Review
Quoted 6 lines
Quoted 2 lines
(quoted diff) +1
-0+ attr->ia_mode = inode->i_mode & ~(S_ISUID | S_ISGID);(quoted diff) +1
-0+ attr->ia_valid |= ATTR_MODE; } } if (!attr->ia_valid)Yeah that is quite a bit simpler.
That said...if either
ATTR_KILLflag is set, then we're going to end up clearing both bits in the new mode. I guess that's ok since we always want to clear them both, and we'll only have one set and not the other if one of the mode bits was set and not the other.
Quoted 4 lines
But...I'm starting to wonder if we really need two flags for this. Would be be better served with a single
ATTR_KILL_SUID_SGIDflag? I wonder if that would simplify some of the logic in the whole setuid clearing morass.
Yeah, that would be a nice little cleanup.
Thanks,
Miklos
---
From: Miklos Szeredi <[email protected]>
Subject: fuse: fix clearing suid, sgid for chown()
Basically, the pjdfstests set the ownership of a file to 06555, and then chowns it (as root) to a new uid/gid. Prior to commit a09f99eddef4 ("fuse:
fix killing s[ug]id in setattr"), fuse would send down a setattr with both the uid/gid change and a new mode. Now, it just sends down the uid/gid change.
Technically this is NOTABUG, since POSIX doesn't _require_ that we clear these bits for a privileged process, but Linux (wisely) has done that and I think we don't want to change that behavior here.
This is caused by the use of should_remove_suid(), which will always return 0 when the process has CAP_FSETID.
In fact we really don't need to be calling should_remove_suid() at all, since we've already been indicated that we should remove the suid, we just don't want to use a (very) stale mode for that.
This patch should fix the above as well as simplify the logic.
--- fs/fuse/dir.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
fs/fuse/dir.c +2 -5
@@ -1739,8 +1739,6 @@ static int fuse_setattr(struct dentry *e* This should be done on write(), truncate() and chown(). */ if (!fc->handle_killpriv) { -int kill; -/* * ia_mode calculation may have used stale i_mode. * Refresh and recalculate.
@@ -1750,12 +1748,11 @@ static int fuse_setattr(struct dentry *ereturn ret; attr->ia_mode = inode->i_mode; - kill =should_remove_suid (entry); -if (kill &ATTR_KILL_SUID ) { +if (inode->i_mode &S_ISUID ) { attr->ia_valid |=ATTR_MODE ; attr->ia_mode &= ~S_ISUID ; } -if (kill &ATTR_KILL_SGID ) { +if ((inode->i_mode & (S_ISGID |S_IXGRP )) == (S_ISGID |S_IXGRP )) { attr->ia_valid |=ATTR_MODE ; attr->ia_mode &= ~S_ISGID ; }
Quoted 72 lines · Miklos Szeredi
Quoted 3 lines · Jeff Layton
Should we be checking that the latest
i_modeeven has these bits before sending down the mode change?Fixed, see updated patch below.
It also fixes a bug in the previous patch where in case of "-rwsrwSr-x" it would clear the sgid bit without execute.
Quoted 17 lines
Review
Quoted 9 lines
Quoted 5 lines
(quoted diff) +1
-0+ attr->ia_mode = inode->i_mode & ~(S_ISUID | S_ISGID);(quoted diff) +1
-0+ attr->ia_valid |= ATTR_MODE; } } if (!attr->ia_valid)Yeah that is quite a bit simpler.
That said...if either
ATTR_KILLflag is set, then we're going to end up clearing both bits in the new mode. I guess that's ok since we always want to clear them both, and we'll only have one set and not the other if one of the mode bits was set and not the other.Quoted 4 lines
But...I'm starting to wonder if we really need two flags for this. Would be be better served with a single
ATTR_KILL_SUID_SGIDflag? I wonder if that would simplify some of the logic in the whole setuid clearing morass.Yeah, that would be a nice little cleanup.
Thanks,
Miklos
---From: Miklos Szeredi <[email protected]>
Subject: fuse: fix clearing suid, sgid forchown()Basically, the pjdfstests set the ownership of a file to 06555, and then chowns it (as root) to a new uid/gid. Prior to commit
a09f99eddef4("fuse:
fix killing s[ug]id in setattr"), fuse would send down a setattr with both the uid/gid change and a new mode. Now, it just sends down the uid/gid change.Technically this is NOTABUG, since POSIX doesn't _require_ that we clear these bits for a privileged process, but Linux (wisely) has done that and I think we don't want to change that behavior here.
This is caused by the use of
should_remove_suid(), which will always return 0 when the process hasCAP_FSETID.In fact we really don't need to be calling
should_remove_suid()at all, since we've already been indicated that we should remove the suid, we just don't want to use a (very) stale mode for that.This patch should fix the above as well as simplify the logic.
Reported-by Jeff Layton <[email protected]>Signed-off-by Miklos Szeredi <[email protected]>Fixes a09f99eddef4 ("fuse: fix killing s[ug]id in setattr")Cc <[email protected]>--- fs/fuse/dir.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
fs/fuse/dir.c static int fuse_setattr(struct dentry *e · line 1741
Quoted 3 lines
fs/fuse/dir.c +0
-0@@ -1739,8 +1739,6 @@ static int fuse_setattr(struct dentry *e* This should be done on write(), truncate() and chown(). */ if (!fc->handle_killpriv) {
One more thing too. I don't think we really want to monkey with the mode at all if there is a request to set the mode already in the request. So maybe this should be:
if (!fc->handle_killpriv && !(attr->ia_mode & ATTR_MODE))
Granted that won't generally happen from normal process context, but we could have knfsd in here too and I think that's possible from there.
Quoted 10 lines
fs/fuse/dir.c +0
-3-int kill; -/* * ia_mode calculation may have used stale i_mode. * Refresh and recalculate. @@ -1750,12 +1748,11 @@ static int fuse_setattr(struct dentry *ereturn ret; attr->ia_mode = inode->i_mode; - kill =should_remove_suid (entry);
fs/fuse/dir.c static int fuse_setattr(struct dentry *e · line 1758
Quoted 10 lines
fs/fuse/dir.c +2
-2-if (kill &ATTR_KILL_SUID ) { +if (inode->i_mode &S_ISUID ) { attr->ia_valid |=ATTR_MODE ; attr->ia_mode &= ~S_ISUID ; } -if (kill &ATTR_KILL_SGID ) { +if ((inode->i_mode & (S_ISGID |S_IXGRP )) == (S_ISGID |S_IXGRP )) { attr->ia_valid |=ATTR_MODE ; attr->ia_mode &= ~S_ISGID ; }
Looks good otherwise!
Signature · 1 line
Jeff Layton <[email protected]>
Miklos Szeredi
maintainerQuoted 9 lines · Jeff Layton
fs/fuse/dir.c static int fuse_setattr(struct dentry *e · line 1741
Quoted 4 lines
fs/fuse/dir.c +0
-0@@ -1739,8 +1739,6 @@ static int fuse_setattr(struct dentry *e* This should be done on write(), truncate() and chown(). */ if (!fc->handle_killpriv) {One more thing too. I don't think we really want to monkey with the mode at all if there is a request to set the mode already in the request. So maybe this should be:
Quoted 4 lines
if (!fc->handle_killpriv && !(attr->ia_mode & ATTR_MODE))Granted that won't generally happen from normal process context, but we could have knfsd in here too and I think that's possible from there.
Apparently this can't happen even from knfsd; notify_change() has this comment:
/*
* We now pass ATTR_KILL_S*ID to the lower level setattr function so
* that the function has the ability to reinterpret a mode change
* that's due to these bits. This adds an implicit restriction that
* no function will ever call notify_change with both ATTR_MODE and
* ATTR_KILL_S*ID set.
*/
if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
(ia_valid & ATTR_MODE))
BUG();
Thanks,
Miklos
Jeff Layton
Reviewed-byQuoted 20 lines · Miklos Szeredi
Quoted 11 lines · Jeff Layton
fs/fuse/dir.c static int fuse_setattr(struct dentry *e · line 1741
Quoted 6 lines
fs/fuse/dir.c +0
-0@@ -1739,8 +1739,6 @@ static int fuse_setattr(struct dentry *e* This should be done on write(), truncate() and chown(). */ if (!fc->handle_killpriv) {One more thing too. I don't think we really want to monkey with the mode at all if there is a request to set the mode already in the request. So maybe this should be:
Quoted 4 lines
if (!fc->handle_killpriv && !(attr->ia_mode & ATTR_MODE))Granted that won't generally happen from normal process context, but we could have knfsd in here too and I think that's possible from there.
Apparently this can't happen even from knfsd;
notify_change()has this comment:
Quoted 12 lines
/*
* We now pass ATTR_KILL_S*ID to the lower level setattr function so
* that the function has the ability to reinterpret a mode change
* that's due to these bits. This adds an implicit restriction that
* no function will ever callnotify_changewith bothATTR_MODEand
* ATTR_KILL_S*ID set.
*/
if ((ia_valid& (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
(ia_valid&ATTR_MODE))
BUG();
Ahh right, I had forgotten about that. Eventually we may want to lift that restriction, but you can add this to the current patch:
Thanks for fixing it quickly!