Skip to contentSkip to conversation

ovl: update S_ISGID when setting posix ACLs

PATCH Superseded Amir Goldstein 4 participants 8 messages Oct 26, 2016 – Oct 28, 2016

Subsystems (2)
lore
Share & tools
lore

Amir Goldstein

author maintainer
#0 lore raw

PATCH 1/1 ovl: update S_ISGID when setting posix ACLs

Since operations on upper are performed using mounter's credentials, we need to call posix_acl_update_mode() with user credentials on overlay inode to possibly copy-up and clear setgid bit, before setting extended attributes on upper inode.

This change fixes xfstest generic/375, which failed to clear the setgid bit in the following test case over overlayfs:

touch $testfile
chown 100:100 $testfile
chmod 2755 $testfile
_runas -u 100 -g 101 -- setfacl -m u::rwx,g::rwx,o::rwx $testfile

---
 fs/overlayfs/super.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
fs/overlayfs/super.c +28 -0
index 30263a5..16cc844 100644
@@ -522,6 +522,22 @@ static unsigned int ovl_split_lowerdirs(char *str)
 	return ctr;
 }
 
+static int
+ovl_set_mode(struct dentry *dentry, umode_t mode)
+{
+	struct iattr iattr;
+	int err;
+
+	if (mode == d_inode(dentry)->i_mode)
+		return 0;
+
+	iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
+	iattr.ia_mode = mode;
+	iattr.ia_ctime = current_time(d_inode(dentry));
+
+	return ovl_setattr(dentry, &iattr);
+}
+
 static int __maybe_unused
 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
 			struct dentry *dentry, struct inode *inode,
@@ -560,6 +576,18 @@ ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
 	if (!inode_owner_or_capable(inode))
 		goto out_acl_release;
 
+	if (handler->flags == ACL_TYPE_ACCESS) {
+		umode_t mode;
+		struct posix_acl *newacl = acl;
+
+		err = posix_acl_update_mode(inode, &mode, &newacl);
+		if (err)
+			goto out_acl_release;
+		err = ovl_set_mode(dentry, mode);
+		if (err)
+			goto out_acl_release;
+	}
+
 	posix_acl_release(acl);
 
 	err = ovl_xattr_set(dentry, handler->name, value, size, flags);
Signature · 1 line
2.7.4
author maintainer
#1 lore raw

PATCH v2 1/1 ovl: update S_ISGID when setting posix ACLs

Since operations on upper are performed using mounter's credentials, we need to call posix_acl_update_mode() with current credentials on overlay inode to possibly copy-up and clear setgid bit, before setting posix ACLs on upper inode.

Also wrap posix acl handlers with #ifdef CONFIG_FS_POSIX_ACL to avoid compiler warning for implicit declaration of function 'posix_acl_update_mode' on build without that config option.

This change fixes xfstest generic/375, which failed to clear the setgid bit in the following test case over overlayfs:

touch $testfile
chown 100:100 $testfile
chmod 2755 $testfile
_runas -u 100 -g 101 -- setfacl -m u::rwx,g::rwx,o::rwx $testfile

---
 fs/overlayfs/super.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
fs/overlayfs/super.c +31 -0
index 30263a5..7071790 100644
@@ -523,6 +523,22 @@ static unsigned int ovl_split_lowerdirs(char *str)
 }
 
 static int __maybe_unused
+ovl_set_mode(struct dentry *dentry, umode_t mode)
+{
+	struct iattr iattr;
+
+	if (mode == d_inode(dentry)->i_mode)
+		return 0;
+
+	iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
+	iattr.ia_mode = mode;
+	iattr.ia_ctime = current_time(d_inode(dentry));
+
+	return ovl_setattr(dentry, &iattr);
+}
+
+#ifdef CONFIG_FS_POSIX_ACL
+static int __maybe_unused
 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
 			struct dentry *dentry, struct inode *inode,
 			const char *name, void *buffer, size_t size)
@@ -560,6 +576,18 @@ ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
 	if (!inode_owner_or_capable(inode))
 		goto out_acl_release;
 
+	if (handler->flags == ACL_TYPE_ACCESS) {
+		umode_t mode;
+		struct posix_acl *newacl = acl;
+
+		err = posix_acl_update_mode(inode, &mode, &newacl);
+		if (err)
+			goto out_acl_release;
+		err = ovl_set_mode(dentry, mode);
+		if (err)
+			goto out_acl_release;
+	}
+
 	posix_acl_release(acl);
 
 	err = ovl_xattr_set(dentry, handler->name, value, size, flags);
@@ -572,6 +600,7 @@ ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
 	posix_acl_release(acl);
 	return err;
 }
+#endif
 
 static int ovl_own_xattr_get(const struct xattr_handler *handler,
 			     struct dentry *dentry, struct inode *inode,
@@ -603,6 +632,7 @@ static int ovl_other_xattr_set(const struct xattr_handler *handler,
 	return ovl_xattr_set(dentry, name, value, size, flags);
 }
 
+#ifdef CONFIG_FS_POSIX_ACL
 static const struct xattr_handler __maybe_unused
 ovl_posix_acl_access_xattr_handler = {
 	.name = XATTR_NAME_POSIX_ACL_ACCESS,
@@ -618,6 +648,7 @@ ovl_posix_acl_default_xattr_handler = {
 	.get = ovl_posix_acl_xattr_get,
 	.set = ovl_posix_acl_xattr_set,
 };
+#endif
 
 static const struct xattr_handler ovl_own_xattr_handler = {
 	.prefix	= OVL_XATTR_PREFIX,
Signature · 1 line
2.7.4
#2 lore raw

Amir,

Quoted 4 lines · Amir Goldstein

Since operations on upper are performed using mounter's credentials, we need to call posix_acl_update_mode() with current credentials on overlay inode to possibly copy-up and clear setgid bit, before setting posix ACLs on upper inode.

I'll let Miklos decide if this is the right approach.

Quoted 3 lines

Also wrap posix acl handlers with #ifdef CONFIG_FS_POSIX_ACL to avoid compiler warning for implicit declaration of function 'posix_acl_update_mode' on build without that config option.

If you do that, make sure to remove the __maybe_unused declarations in that file well:
they were added to silence the compiler when CONFIG_FS_POSIX_ACL isn't set. Or else add a dummy posix_acl_update_mode function to include/linux/posix_acl.h.

Thanks,
Andreas

#3 lore raw
In reply to Amir Goldstein

fs/overlayfs/super.c ovl_posix_acl_xattr_set(const struct xattr_handler *handler,

Quoted 69 lines · Amir Goldstein

Since operations on upper are performed using mounter's credentials, we need to call posix_acl_update_mode() with current credentials on overlay inode to possibly copy-up and clear setgid bit, before setting posix ACLs on upper inode.

Also wrap posix acl handlers with #ifdef CONFIG_FS_POSIX_ACL to avoid compiler warning for implicit declaration of function 'posix_acl_update_mode' on build without that config option.

This change fixes xfstest generic/375, which failed to clear the setgid bit in the following test case over overlayfs:

touch $testfile
chown 100:100 $testfile
chmod 2755 $testfile
_runas -u 100 -g 101 -- setfacl -m u::rwx,g::rwx,o::rwx $testfile

---
 fs/overlayfs/super.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
fs/overlayfs/super.c +28 -0
index 30263a5..7071790 100644
@@ -523,6 +523,22 @@ static unsigned int ovl_split_lowerdirs(char *str)
 }
 
 static int __maybe_unused
+ovl_set_mode(struct dentry *dentry, umode_t mode)
+{
+	struct iattr iattr;
+
+	if (mode == d_inode(dentry)->i_mode)
+		return 0;
+
+	iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
+	iattr.ia_mode = mode;
+	iattr.ia_ctime = current_time(d_inode(dentry));
+
+	return ovl_setattr(dentry, &iattr);
+}
+
+#ifdef CONFIG_FS_POSIX_ACL
+static int __maybe_unused
 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
 			struct dentry *dentry, struct inode *inode,
 			const char *name, void *buffer, size_t size)
@@ -560,6 +576,18 @@ ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
 	if (!inode_owner_or_capable(inode))
 		goto out_acl_release;
 
+	if (handler->flags == ACL_TYPE_ACCESS) {
+		umode_t mode;
+		struct posix_acl *newacl = acl;
+
+		err = posix_acl_update_mode(inode, &mode, &newacl);
+		if (err)
+			goto out_acl_release;
+		err = ovl_set_mode(dentry, mode);
+		if (err)
+			goto out_acl_release;
+	}
+
 	posix_acl_release(acl);
 
 	err = ovl_xattr_set(dentry, handler->name, value, size, flags);
@@ -572,6 +600,7 @@ ovl_posix_acl_xattr_set(const struct xattr_handler *handler,

Hi Amir,

What happens if ovl_set_mode() is successful but ovl_xattr_set() fails. We will leave file with S_ISGID clear?

Vivek

Quoted 25 lines
fs/overlayfs/super.c +3 -0
 	posix_acl_release(acl);
 	return err;
 }
+#endif
 
 static int ovl_own_xattr_get(const struct xattr_handler *handler,
 			     struct dentry *dentry, struct inode *inode,
@@ -603,6 +632,7 @@ static int ovl_other_xattr_set(const struct xattr_handler *handler,
 	return ovl_xattr_set(dentry, name, value, size, flags);
 }
 
+#ifdef CONFIG_FS_POSIX_ACL
 static const struct xattr_handler __maybe_unused
 ovl_posix_acl_access_xattr_handler = {
 	.name = XATTR_NAME_POSIX_ACL_ACCESS,
@@ -618,6 +648,7 @@ ovl_posix_acl_default_xattr_handler = {
 	.get = ovl_posix_acl_xattr_get,
 	.set = ovl_posix_acl_xattr_set,
 };
+#endif
 
 static const struct xattr_handler ovl_own_xattr_handler = {
 	.prefix	= OVL_XATTR_PREFIX,
Signature · 1 line
2.7.4
author maintainer
#4 lore raw
Quoted 68 lines · Vivek Goyal
Quoted 67 lines · Amir Goldstein

Since operations on upper are performed using mounter's credentials, we need to call posix_acl_update_mode() with current credentials on overlay inode to possibly copy-up and clear setgid bit, before setting posix ACLs on upper inode.

Also wrap posix acl handlers with #ifdef CONFIG_FS_POSIX_ACL to avoid compiler warning for implicit declaration of function 'posix_acl_update_mode' on build without that config option.

This change fixes xfstest generic/375, which failed to clear the setgid bit in the following test case over overlayfs:

touch $testfile
chown 100:100 $testfile
chmod 2755 $testfile
_runas -u 100 -g 101 -- setfacl -m u::rwx,g::rwx,o::rwx $testfile

---
 fs/overlayfs/super.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
fs/overlayfs/super.c +28 -0
index 30263a5..7071790 100644
@@ -523,6 +523,22 @@ static unsigned int ovl_split_lowerdirs(char *str)
 }
 
 static int __maybe_unused
+ovl_set_mode(struct dentry *dentry, umode_t mode)
+{
+     struct iattr iattr;
+
+     if (mode == d_inode(dentry)->i_mode)
+             return 0;
+
+     iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
+     iattr.ia_mode = mode;
+     iattr.ia_ctime = current_time(d_inode(dentry));
+
+     return ovl_setattr(dentry, &iattr);
+}
+
+#ifdef CONFIG_FS_POSIX_ACL
+static int __maybe_unused
 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
                      struct dentry *dentry, struct inode *inode,
                      const char *name, void *buffer, size_t size)
@@ -560,6 +576,18 @@ ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
      if (!inode_owner_or_capable(inode))
              goto out_acl_release;
 
+     if (handler->flags == ACL_TYPE_ACCESS) {
+             umode_t mode;
+             struct posix_acl *newacl = acl;
+
+             err = posix_acl_update_mode(inode, &mode, &newacl);
+             if (err)
+                     goto out_acl_release;
+             err = ovl_set_mode(dentry, mode);
+             if (err)
+                     goto out_acl_release;
+     }
+
      posix_acl_release(acl);
 
Quoted 8 lines

fs/overlayfs/super.c ovl_posix_acl_xattr_set(const struct xattr_handler *handler,

Quoted 2 lines
fs/overlayfs/super.c +0 -0
      err = ovl_xattr_set(dentry, handler->name, value, size, flags);
@@ -572,6 +600,7 @@ ovl_posix_acl_xattr_set(const struct xattr_handler *handler,

Hi Amir,

What happens if ovl_set_mode() is successful but ovl_xattr_set() fails. We will leave file with S_ISGID clear?

Yes, we will, and that is the same thing that both xfs and ext4 do. We already tested that current user has sufficient credentials to perform the operation (and some other validity checks). IMO, if user has the right to perform an operation with the knowledge that this operation will clear S_ISGID and user executes this operation, then at least from security point of view, the outcome of clearing the bit even though the operation itself failed makes sense.

Thanks,
Amir.

maintainer
#5 lore raw
Quoted 11 lines · Amir Goldstein
Quoted 10 lines · Vivek Goyal
Quoted 9 lines · Amir Goldstein

Since operations on upper are performed using mounter's credentials, we need to call posix_acl_update_mode() with current credentials on overlay inode to possibly copy-up and clear setgid bit, before setting posix ACLs on upper inode.

Also wrap posix acl handlers with #ifdef CONFIG_FS_POSIX_ACL to avoid compiler warning for implicit declaration of function 'posix_acl_update_mode' on build without that config option.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

This change fixes xfstest generic/375, which failed to clear the setgid bit in the following test case over overlayfs:

touch $testfile
chown 100:100 $testfile
chmod 2755 $testfile
_runas -u 100 -g 101 -- setfacl -m u::rwx,g::rwx,o::rwx $testfile

Instead of calculating and setting the equivalent mode in overlayfs code (as well as in the upper layer later), how about just clearing the sgid bit when necessary?

Untested patch follows.

Thanks,
Miklos

---
 fs/overlayfs/super.c |   15 +++++++++++++++
 1 file changed, 15 insertions(+)
fs/overlayfs/super.c +15 -0
@@ -562,6 +562,21 @@ ovl_posix_acl_xattr_set(const struct xat
 
 	posix_acl_release(acl);
 
+	/*
+	 * Check if sgid bit needs to be cleared (actual setacl operation will
+	 * be done with mounter's capabilities and so that won't do it for us).
+	 */
+	if (unlikely(inode->i_mode & S_ISGID) &&
+	    handler->flags == ACL_TYPE_ACCESS &&
+	    !in_group_p(inode->i_gid) &&
+	    !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
+		struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
+
+		err = ovl_setattr(dentry, &iattr);
+		if (err)
+			return err;
+	}
+
 	err = ovl_xattr_set(dentry, handler->name, value, size, flags);
 	if (!err)
 		ovl_copyattr(ovl_inode_real(inode, NULL), inode);
author maintainer
#6 lore raw
Quoted 7 lines · Miklos Szeredi
Quoted 5 lines · Amir Goldstein
Quoted 4 lines · Vivek Goyal
Quoted 3 lines · Amir Goldstein

Since operations on upper are performed using mounter's credentials, we need to call posix_acl_update_mode() with current credentials on overlay inode to possibly copy-up and clear setgid bit, before

setting

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

posix ACLs on upper inode.

Also wrap posix acl handlers with #ifdef CONFIG_FS_POSIX_ACL to avoid compiler warning for implicit declaration of function 'posix_acl_update_mode' on build without that config option.

This change fixes xfstest generic/375, which failed to clear the setgid bit in the following test case over overlayfs:

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

touch $testfile
chown 100:100 $testfile
chmod 2755 $testfile
_runas -u 100 -g 101 -- setfacl -m u::rwx,g::rwx,o::rwx $testfile

Instead of calculating and setting the equivalent mode in overlayfs code

(as

Quoted 1 line

well as in the upper layer later), how about just clearing the sgid bit

when

Quoted 1 line

necessary?

That looks reasonable.
I'll be able to test tomorrow.

Quoted 12 lines

Untested patch follows.

Thanks,
Miklos

---
 fs/overlayfs/super.c |   15 +++++++++++++++
 1 file changed, 15 insertions(+)

fs/overlayfs/super.c ovl_posix_acl_xattr_set(const struct xat · line 566

Quoted 5 lines
fs/overlayfs/super.c +2 -0
@@ -562,6 +562,21 @@ ovl_posix_acl_xattr_set(const struct xat
 
        posix_acl_release(acl);
 
+       /*
+        * Check if sgid bit needs to be cleared (actual setacl operation

will

fs/overlayfs/super.c ovl_posix_acl_xattr_set(const struct xat · line 567

Quoted 1 line
fs/overlayfs/super.c +1 -0
+        * be done with mounter's capabilities and so that won't do it

for us).

Quoted 15 lines
fs/overlayfs/super.c +12 -0
+        */
+       if (unlikely(inode->i_mode & S_ISGID) &&
+           handler->flags == ACL_TYPE_ACCESS &&
+           !in_group_p(inode->i_gid) &&
+           !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
+               struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
+
+               err = ovl_setattr(dentry, &iattr);
+               if (err)
+                       return err;
+       }
+
        err = ovl_xattr_set(dentry, handler->name, value, size, flags);
        if (!err)
                ovl_copyattr(ovl_inode_real(inode, NULL), inode);
author maintainer
#7 lore raw
In reply to Miklos Szeredi
Quoted 20 lines · Miklos Szeredi
Quoted 11 lines · Amir Goldstein
Quoted 10 lines · Vivek Goyal
Quoted 9 lines · Amir Goldstein

Since operations on upper are performed using mounter's credentials, we need to call posix_acl_update_mode() with current credentials on overlay inode to possibly copy-up and clear setgid bit, before setting posix ACLs on upper inode.

Also wrap posix acl handlers with #ifdef CONFIG_FS_POSIX_ACL to avoid compiler warning for implicit declaration of function 'posix_acl_update_mode' on build without that config option.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

This change fixes xfstest generic/375, which failed to clear the setgid bit in the following test case over overlayfs:

touch $testfile
chown 100:100 $testfile
chmod 2755 $testfile
_runas -u 100 -g 101 -- setfacl -m u::rwx,g::rwx,o::rwx $testfile

Quoted 6 lines

Instead of calculating and setting the equivalent mode in overlayfs code (as well as in the upper layer later), how about just clearing the sgid bit when necessary?

Untested patch follows.

Tested your patch. It fixes generic/375 and passes xfs/pjd/union tests. You may re-cycle my commit message (relevant parts) and either keep my Signed-off-by or add Tested-by me

Thanks,
Amir.

Quoted 31 lines

Thanks,
Miklos

---
 fs/overlayfs/super.c |   15 +++++++++++++++
 1 file changed, 15 insertions(+)
fs/overlayfs/super.c +15 -0
@@ -562,6 +562,21 @@ ovl_posix_acl_xattr_set(const struct xat
 
        posix_acl_release(acl);
 
+       /*
+        * Check if sgid bit needs to be cleared (actual setacl operation will
+        * be done with mounter's capabilities and so that won't do it for us).
+        */
+       if (unlikely(inode->i_mode & S_ISGID) &&
+           handler->flags == ACL_TYPE_ACCESS &&
+           !in_group_p(inode->i_gid) &&
+           !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
+               struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
+
+               err = ovl_setattr(dentry, &iattr);
+               if (err)
+                       return err;
+       }
+
        err = ovl_xattr_set(dentry, handler->name, value, size, flags);
        if (!err)
                ovl_copyattr(ovl_inode_real(inode, NULL), inode);