linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] btrfs: preserve i_mode if __btrfs_set_acl() fails
@ 2017-07-29  0:26 Ernesto A. Fernández
  2017-07-29  0:48 ` Josef Bacik
  2017-07-30  2:18 ` [PATCH v2] " Ernesto A. Fernández
  0 siblings, 2 replies; 6+ messages in thread
From: Ernesto A. Fernández @ 2017-07-29  0:26 UTC (permalink / raw)
  To: linux-btrfs
  Cc: Chris Mason, Josef Bacik, David Sterba, Ernesto A. Fernández

When changing a file's acl mask, btrfs_set_acl() will first set the
group bits of i_mode to the value of the mask, and only then set the
actual extended attribute representing the new acl.

If the second part fails (due to lack of space, for example) and the
file had no acl attribute to begin with, the system will from now on
assume that the mask permission bits are actual group permission bits,
potentially granting access to the wrong users.

Prevent this by starting the journal transaction before calling
__btrfs_set_acl and only changing the inode mode after it returns
successfully.

Signed-off-by: Ernesto A. Fernández <ernesto.mnd.fernandez@gmail.com>
---
This issue is covered by generic/449 in xfstests. Several filesystems
are affected; some of them have already applied patches:
  - fe26569 ext2: preserve i_mode if ext2_set_acl() fails
  - f070e5a jfs: preserve i_mode if __jfs_set_acl() fails
  - fcea8ae reiserfs: preserve i_mode if __reiserfs_set_acl() fails

 fs/btrfs/acl.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/acl.c b/fs/btrfs/acl.c
index 8d8370d..d041526 100644
--- a/fs/btrfs/acl.c
+++ b/fs/btrfs/acl.c
@@ -27,6 +27,7 @@
 #include "ctree.h"
 #include "btrfs_inode.h"
 #include "xattr.h"
+#include "transaction.h"
 
 struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
 {
@@ -113,14 +114,36 @@ static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
 
 int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 {
+	struct btrfs_root *root = BTRFS_I(inode)->root;
+	struct btrfs_trans_handle *trans;
 	int ret;
+	umode_t mode = inode->i_mode;
+
+	if (btrfs_root_readonly(root))
+		return -EROFS;
+
+	trans = btrfs_start_transaction(root, 2);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
 
 	if (type == ACL_TYPE_ACCESS && acl) {
-		ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
+		ret = posix_acl_update_mode(inode, &mode, &acl);
 		if (ret)
-			return ret;
+			goto out;
 	}
-	return __btrfs_set_acl(NULL, inode, acl, type);
+	ret = __btrfs_set_acl(trans, inode, acl, type);
+	if (ret)
+		goto out;
+
+	inode->i_mode = mode;
+	inode_inc_iversion(inode);
+	inode->i_ctime = current_time(inode);
+	set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
+	ret = btrfs_update_inode(trans, root, inode);
+	BUG_ON(ret);
+out:
+	btrfs_end_transaction(trans);
+	return ret;
 }
 
 /*
-- 
2.1.4


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] btrfs: preserve i_mode if __btrfs_set_acl() fails
  2017-07-29  0:26 [PATCH] btrfs: preserve i_mode if __btrfs_set_acl() fails Ernesto A. Fernández
@ 2017-07-29  0:48 ` Josef Bacik
  2017-07-29  5:04   ` Ernesto A. Fernández
  2017-07-30  2:18 ` [PATCH v2] " Ernesto A. Fernández
  1 sibling, 1 reply; 6+ messages in thread
From: Josef Bacik @ 2017-07-29  0:48 UTC (permalink / raw)
  To: Ernesto A. Fernández
  Cc: linux-btrfs, Chris Mason, Josef Bacik, David Sterba

On Fri, Jul 28, 2017 at 09:26:29PM -0300, Ernesto A. Fernández wrote:
> When changing a file's acl mask, btrfs_set_acl() will first set the
> group bits of i_mode to the value of the mask, and only then set the
> actual extended attribute representing the new acl.
> 
> If the second part fails (due to lack of space, for example) and the
> file had no acl attribute to begin with, the system will from now on
> assume that the mask permission bits are actual group permission bits,
> potentially granting access to the wrong users.
> 
> Prevent this by starting the journal transaction before calling
> __btrfs_set_acl and only changing the inode mode after it returns
> successfully.
> 
> Signed-off-by: Ernesto A. Fernández <ernesto.mnd.fernandez@gmail.com>
> ---
> This issue is covered by generic/449 in xfstests. Several filesystems
> are affected; some of them have already applied patches:
>   - fe26569 ext2: preserve i_mode if ext2_set_acl() fails
>   - f070e5a jfs: preserve i_mode if __jfs_set_acl() fails
>   - fcea8ae reiserfs: preserve i_mode if __reiserfs_set_acl() fails
> 
>  fs/btrfs/acl.c | 29 ++++++++++++++++++++++++++---
>  1 file changed, 26 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/btrfs/acl.c b/fs/btrfs/acl.c
> index 8d8370d..d041526 100644
> --- a/fs/btrfs/acl.c
> +++ b/fs/btrfs/acl.c
> @@ -27,6 +27,7 @@
>  #include "ctree.h"
>  #include "btrfs_inode.h"
>  #include "xattr.h"
> +#include "transaction.h"
>  
>  struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
>  {
> @@ -113,14 +114,36 @@ static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
>  
>  int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
>  {
> +	struct btrfs_root *root = BTRFS_I(inode)->root;
> +	struct btrfs_trans_handle *trans;
>  	int ret;
> +	umode_t mode = inode->i_mode;
> +
> +	if (btrfs_root_readonly(root))
> +		return -EROFS;
> +
> +	trans = btrfs_start_transaction(root, 2);
> +	if (IS_ERR(trans))
> +		return PTR_ERR(trans);
>  
>  	if (type == ACL_TYPE_ACCESS && acl) {
> -		ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
> +		ret = posix_acl_update_mode(inode, &mode, &acl);
>  		if (ret)
> -			return ret;
> +			goto out;
>  	}
> -	return __btrfs_set_acl(NULL, inode, acl, type);
> +	ret = __btrfs_set_acl(trans, inode, acl, type);
> +	if (ret)
> +		goto out;
> +
> +	inode->i_mode = mode;
> +	inode_inc_iversion(inode);
> +	inode->i_ctime = current_time(inode);
> +	set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);

This only needs to be set if we actually set the xattr.  I'd fix setxattr to
call it every time it's called.

> +	ret = btrfs_update_inode(trans, root, inode);
> +	BUG_ON(ret);

No BUG_ON, return the error.  Thanks,

Josef

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] btrfs: preserve i_mode if __btrfs_set_acl() fails
  2017-07-29  0:48 ` Josef Bacik
@ 2017-07-29  5:04   ` Ernesto A. Fernández
  0 siblings, 0 replies; 6+ messages in thread
From: Ernesto A. Fernández @ 2017-07-29  5:04 UTC (permalink / raw)
  To: Josef Bacik
  Cc: linux-btrfs, Chris Mason, Josef Bacik, David Sterba,
	Ernesto A. Fernández

On Sat, Jul 29, 2017 at 12:48:04AM +0000, Josef Bacik wrote:
> On Fri, Jul 28, 2017 at 09:26:29PM -0300, Ernesto A. Fernández wrote:
> > +	ret = __btrfs_set_acl(trans, inode, acl, type);
> > +	if (ret)
> > +		goto out;
> > +
> > +	inode->i_mode = mode;
> > +	inode_inc_iversion(inode);
> > +	inode->i_ctime = current_time(inode);
> > +	set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
> 
> This only needs to be set if we actually set the xattr.  I'd fix setxattr to
> call it every time it's called.

I had not thought of that, thank you.

If I'm understanding this correctly the issue would be only when setting
a NULL default acl on an inode that is not a directory. In that case I
probably shouldn't be calling btrfs_update_inode either, but I can't move
that back to setxattr.

Perhaps __btrfs_set_acl could return an error in that case, like -ENOTDIR,
and then we can set ret back to 0 before returning from btrfs_set_acl.

> > +	ret = btrfs_update_inode(trans, root, inode);
> > +	BUG_ON(ret);
> 
> No BUG_ON, return the error.

The call to BUG_ON was already there before my patch, only inside the 
__btrfs_setxattr function. Since I didn't know the reason I thought it
was best not to change it. I'll do as you say in the next version.

Thank you for your review.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] btrfs: preserve i_mode if __btrfs_set_acl() fails
  2017-07-29  0:26 [PATCH] btrfs: preserve i_mode if __btrfs_set_acl() fails Ernesto A. Fernández
  2017-07-29  0:48 ` Josef Bacik
@ 2017-07-30  2:18 ` Ernesto A. Fernández
  2017-08-02  6:18   ` [PATCH v3] " Ernesto A. Fernández
  1 sibling, 1 reply; 6+ messages in thread
From: Ernesto A. Fernández @ 2017-07-30  2:18 UTC (permalink / raw)
  To: linux-btrfs
  Cc: Chris Mason, Josef Bacik, David Sterba, Ernesto A. Fernández

When changing a file's acl mask, btrfs_set_acl() will first set the
group bits of i_mode to the value of the mask, and only then set the
actual extended attribute representing the new acl.

If the second part fails (due to lack of space, for example) and the
file had no acl attribute to begin with, the system will from now on
assume that the mask permission bits are actual group permission bits,
potentially granting access to the wrong users.

Prevent this by starting the journal transaction before calling
__btrfs_set_acl and only changing the inode mode after the acl is set
successfully.

Signed-off-by: Ernesto A. Fernández <ernesto.mnd.fernandez@gmail.com>
---
Changes in v2:
  - Take the code that checks if we are setting a default acl on something
    that is not a dir, remove it from the __btrfs_set_acl function, and
    place it in btrfs_set_acl instead. This should fix the issue pointed out
    by Josef Bacik, that I was sometimes updating the inode even when there
    was no change.
  - Don't call BUG_ON when the inode failed to update. Also requested by
    Josef Bacik. It should be noted that __btrfs_setxattr was already
    calling BUG_ON before my patch; that has not been changed.

 fs/btrfs/acl.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/acl.c b/fs/btrfs/acl.c
index 8d8370d..f62e8ac 100644
--- a/fs/btrfs/acl.c
+++ b/fs/btrfs/acl.c
@@ -27,6 +27,7 @@
 #include "ctree.h"
 #include "btrfs_inode.h"
 #include "xattr.h"
+#include "transaction.h"
 
 struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
 {
@@ -80,8 +81,6 @@ static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
 		name = XATTR_NAME_POSIX_ACL_ACCESS;
 		break;
 	case ACL_TYPE_DEFAULT:
-		if (!S_ISDIR(inode->i_mode))
-			return acl ? -EINVAL : 0;
 		name = XATTR_NAME_POSIX_ACL_DEFAULT;
 		break;
 	default:
@@ -113,14 +112,38 @@ static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
 
 int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 {
+	struct btrfs_root *root = BTRFS_I(inode)->root;
+	struct btrfs_trans_handle *trans;
 	int ret;
+	umode_t mode = inode->i_mode;
 
+	if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
+		return acl ? -EINVAL : 0;
 	if (type == ACL_TYPE_ACCESS && acl) {
-		ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
+		ret = posix_acl_update_mode(inode, &mode, &acl);
 		if (ret)
 			return ret;
 	}
-	return __btrfs_set_acl(NULL, inode, acl, type);
+
+	if (btrfs_root_readonly(root))
+		return -EROFS;
+
+	trans = btrfs_start_transaction(root, 2);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
+
+	ret = __btrfs_set_acl(trans, inode, acl, type);
+	if (ret)
+		goto out;
+
+	inode->i_mode = mode;
+	inode_inc_iversion(inode);
+	inode->i_ctime = current_time(inode);
+	set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
+	ret = btrfs_update_inode(trans, root, inode);
+out:
+	btrfs_end_transaction(trans);
+	return ret;
 }
 
 /*
-- 
2.1.4



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3] btrfs: preserve i_mode if __btrfs_set_acl() fails
  2017-07-30  2:18 ` [PATCH v2] " Ernesto A. Fernández
@ 2017-08-02  6:18   ` Ernesto A. Fernández
  2017-08-14 16:05     ` David Sterba
  0 siblings, 1 reply; 6+ messages in thread
From: Ernesto A. Fernández @ 2017-08-02  6:18 UTC (permalink / raw)
  To: linux-btrfs
  Cc: Chris Mason, Josef Bacik, David Sterba, Ernesto A. Fernández

When changing a file's acl mask, btrfs_set_acl() will first set the
group bits of i_mode to the value of the mask, and only then set the
actual extended attribute representing the new acl.

If the second part fails (due to lack of space, for example) and the
file had no acl attribute to begin with, the system will from now on
assume that the mask permission bits are actual group permission bits,
potentially granting access to the wrong users.

Prevent this by restoring the original mode bits if __btrfs_set_acl
fails.

Signed-off-by: Ernesto A. Fernández <ernesto.mnd.fernandez@gmail.com>
---
Please ignore the two previous versions, this is far simpler and has the
same effect. To Josef Bacik: thank you for your review, I'm sorry I
wasted your time.

 fs/btrfs/acl.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/acl.c b/fs/btrfs/acl.c
index 8d8370d..1ba49eb 100644
--- a/fs/btrfs/acl.c
+++ b/fs/btrfs/acl.c
@@ -114,13 +114,17 @@ static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
 int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 {
 	int ret;
+	umode_t old_mode = inode->i_mode;
 
 	if (type == ACL_TYPE_ACCESS && acl) {
 		ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
 		if (ret)
 			return ret;
 	}
-	return __btrfs_set_acl(NULL, inode, acl, type);
+	ret = __btrfs_set_acl(NULL, inode, acl, type);
+	if (ret)
+		inode->i_mode = old_mode;
+	return ret;
 }
 
 /*
-- 
2.1.4



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] btrfs: preserve i_mode if __btrfs_set_acl() fails
  2017-08-02  6:18   ` [PATCH v3] " Ernesto A. Fernández
@ 2017-08-14 16:05     ` David Sterba
  0 siblings, 0 replies; 6+ messages in thread
From: David Sterba @ 2017-08-14 16:05 UTC (permalink / raw)
  To: Ernesto A. Fernández
  Cc: linux-btrfs, Chris Mason, Josef Bacik, David Sterba

On Wed, Aug 02, 2017 at 03:18:27AM -0300, Ernesto A. Fernández wrote:
> When changing a file's acl mask, btrfs_set_acl() will first set the
> group bits of i_mode to the value of the mask, and only then set the
> actual extended attribute representing the new acl.
> 
> If the second part fails (due to lack of space, for example) and the
> file had no acl attribute to begin with, the system will from now on
> assume that the mask permission bits are actual group permission bits,
> potentially granting access to the wrong users.
> 
> Prevent this by restoring the original mode bits if __btrfs_set_acl
> fails.
> 
> Signed-off-by: Ernesto A. Fernández <ernesto.mnd.fernandez@gmail.com>
> ---
> Please ignore the two previous versions, this is far simpler and has the
> same effect. To Josef Bacik: thank you for your review, I'm sorry I
> wasted your time.

This version is much better, starting the transaction would add some
overhead and would need to be measured so we have an idea about the
impact.

Reviewed-by: David Sterba <dsterba@suse.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-08-14 16:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-07-29  0:26 [PATCH] btrfs: preserve i_mode if __btrfs_set_acl() fails Ernesto A. Fernández
2017-07-29  0:48 ` Josef Bacik
2017-07-29  5:04   ` Ernesto A. Fernández
2017-07-30  2:18 ` [PATCH v2] " Ernesto A. Fernández
2017-08-02  6:18   ` [PATCH v3] " Ernesto A. Fernández
2017-08-14 16:05     ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).