Commit a88a2be3 authored by Jan Kara's avatar Jan Kara Committed by Jiri Slaby

posix_acl: Clear SGID bit when setting file permissions

commit 07393101 upstream.

When file permissions are modified via chmod(2) and the user is not in
the owning group or capable of CAP_FSETID, the setgid bit is cleared in
inode_change_ok().  Setting a POSIX ACL via setxattr(2) sets the file
permissions as well as the new ACL, but doesn't clear the setgid bit in
a similar way; this allows to bypass the check in chmod(2).  Fix that.
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarJeff Layton <jlayton@redhat.com>
Signed-off-by: default avatarJan Kara <jack@suse.cz>
Signed-off-by: default avatarAndreas Gruenbacher <agruenba@redhat.com>
Signed-off-by: default avatarJiri Slaby <jslaby@suse.cz>
parent 1c4d4c69
...@@ -320,32 +320,26 @@ static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name, ...@@ -320,32 +320,26 @@ static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
case ACL_TYPE_ACCESS: case ACL_TYPE_ACCESS:
name = POSIX_ACL_XATTR_ACCESS; name = POSIX_ACL_XATTR_ACCESS;
if (acl) { if (acl) {
umode_t mode = inode->i_mode; struct iattr iattr;
retval = posix_acl_equiv_mode(acl, &mode);
if (retval < 0) retval = posix_acl_update_mode(inode, &iattr.ia_mode, &acl);
if (retval)
goto err_out; goto err_out;
else { if (!acl) {
struct iattr iattr; /*
if (retval == 0) { * ACL can be represented
/* * by the mode bits. So don't
* ACL can be represented * update ACL.
* by the mode bits. So don't
* update ACL.
*/
acl = NULL;
value = NULL;
size = 0;
}
/* Updte the mode bits */
iattr.ia_mode = ((mode & S_IALLUGO) |
(inode->i_mode & ~S_IALLUGO));
iattr.ia_valid = ATTR_MODE;
/* FIXME should we update ctime ?
* What is the following setxattr update the
* mode ?
*/ */
v9fs_vfs_setattr_dotl(dentry, &iattr); value = NULL;
size = 0;
} }
iattr.ia_valid = ATTR_MODE;
/* FIXME should we update ctime ?
* What is the following setxattr update the
* mode ?
*/
v9fs_vfs_setattr_dotl(dentry, &iattr);
} }
break; break;
case ACL_TYPE_DEFAULT: case ACL_TYPE_DEFAULT:
......
...@@ -118,11 +118,9 @@ static int btrfs_set_acl(struct btrfs_trans_handle *trans, ...@@ -118,11 +118,9 @@ static int btrfs_set_acl(struct btrfs_trans_handle *trans,
case ACL_TYPE_ACCESS: case ACL_TYPE_ACCESS:
name = POSIX_ACL_XATTR_ACCESS; name = POSIX_ACL_XATTR_ACCESS;
if (acl) { if (acl) {
ret = posix_acl_equiv_mode(acl, &inode->i_mode); ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
if (ret < 0) if (ret)
return ret; return ret;
if (ret == 0)
acl = NULL;
} }
ret = 0; ret = 0;
break; break;
......
...@@ -206,15 +206,11 @@ ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl) ...@@ -206,15 +206,11 @@ ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
case ACL_TYPE_ACCESS: case ACL_TYPE_ACCESS:
name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS; name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
if (acl) { if (acl) {
error = posix_acl_equiv_mode(acl, &inode->i_mode); error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
if (error < 0) if (error)
return error; return error;
else { inode->i_ctime = CURRENT_TIME_SEC;
inode->i_ctime = CURRENT_TIME_SEC; mark_inode_dirty(inode);
mark_inode_dirty(inode);
if (error == 0)
acl = NULL;
}
} }
break; break;
......
...@@ -205,15 +205,11 @@ ext3_set_acl(handle_t *handle, struct inode *inode, int type, ...@@ -205,15 +205,11 @@ ext3_set_acl(handle_t *handle, struct inode *inode, int type,
case ACL_TYPE_ACCESS: case ACL_TYPE_ACCESS:
name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS; name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
if (acl) { if (acl) {
error = posix_acl_equiv_mode(acl, &inode->i_mode); error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
if (error < 0) if (error < 0)
return error; return error;
else { inode->i_ctime = CURRENT_TIME_SEC;
inode->i_ctime = CURRENT_TIME_SEC; ext3_mark_inode_dirty(handle, inode);
ext3_mark_inode_dirty(handle, inode);
if (error == 0)
acl = NULL;
}
} }
break; break;
......
...@@ -211,15 +211,11 @@ ext4_set_acl(handle_t *handle, struct inode *inode, int type, ...@@ -211,15 +211,11 @@ ext4_set_acl(handle_t *handle, struct inode *inode, int type,
case ACL_TYPE_ACCESS: case ACL_TYPE_ACCESS:
name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
if (acl) { if (acl) {
error = posix_acl_equiv_mode(acl, &inode->i_mode); error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
if (error < 0) if (error)
return error; return error;
else { inode->i_ctime = ext4_current_time(inode);
inode->i_ctime = ext4_current_time(inode); ext4_mark_inode_dirty(handle, inode);
ext4_mark_inode_dirty(handle, inode);
if (error == 0)
acl = NULL;
}
} }
break; break;
......
...@@ -223,12 +223,10 @@ static int f2fs_set_acl(struct inode *inode, int type, struct posix_acl *acl) ...@@ -223,12 +223,10 @@ static int f2fs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
case ACL_TYPE_ACCESS: case ACL_TYPE_ACCESS:
name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS; name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
if (acl) { if (acl) {
error = posix_acl_equiv_mode(acl, &inode->i_mode); error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
if (error < 0) if (error)
return error; return error;
set_acl_inode(fi, inode->i_mode); set_acl_inode(fi, inode->i_mode);
if (error == 0)
acl = NULL;
} }
break; break;
......
...@@ -268,15 +268,13 @@ static int gfs2_xattr_system_set(struct dentry *dentry, const char *name, ...@@ -268,15 +268,13 @@ static int gfs2_xattr_system_set(struct dentry *dentry, const char *name,
if (type == ACL_TYPE_ACCESS) { if (type == ACL_TYPE_ACCESS) {
umode_t mode = inode->i_mode; umode_t mode = inode->i_mode;
error = posix_acl_equiv_mode(acl, &mode); struct posix_acl *old_acl = acl;
if (error <= 0) { error = posix_acl_update_mode(inode, &mode, &acl);
posix_acl_release(acl); if (error < 0)
acl = NULL; goto out_release;
if (!acl)
if (error < 0) posix_acl_release(old_acl);
return error;
}
error = gfs2_set_mode(inode, mode); error = gfs2_set_mode(inode, mode);
if (error) if (error)
......
...@@ -72,8 +72,8 @@ static int hfsplus_set_posix_acl(struct inode *inode, ...@@ -72,8 +72,8 @@ static int hfsplus_set_posix_acl(struct inode *inode,
case ACL_TYPE_ACCESS: case ACL_TYPE_ACCESS:
xattr_name = POSIX_ACL_XATTR_ACCESS; xattr_name = POSIX_ACL_XATTR_ACCESS;
if (acl) { if (acl) {
err = posix_acl_equiv_mode(acl, &inode->i_mode); err = posix_acl_update_mode(inode, &inode->i_mode, &acl);
if (err < 0) if (err)
return err; return err;
} }
err = 0; err = 0;
......
...@@ -69,8 +69,9 @@ static int can_set_system_xattr(struct inode *inode, const char *name, ...@@ -69,8 +69,9 @@ static int can_set_system_xattr(struct inode *inode, const char *name,
if (IS_ERR(acl)) if (IS_ERR(acl))
return PTR_ERR(acl); return PTR_ERR(acl);
if (acl) { if (acl) {
err = posix_acl_equiv_mode(acl, &inode->i_mode); struct posix_acl *old_acl = acl;
posix_acl_release(acl); err = posix_acl_update_mode(inode, &inode->i_mode, &acl);
posix_acl_release(old_acl);
if (err < 0) if (err < 0)
return err; return err;
mark_inode_dirty(inode); mark_inode_dirty(inode);
......
...@@ -243,9 +243,10 @@ static int jffs2_set_acl(struct inode *inode, int type, struct posix_acl *acl) ...@@ -243,9 +243,10 @@ static int jffs2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
case ACL_TYPE_ACCESS: case ACL_TYPE_ACCESS:
xprefix = JFFS2_XPREFIX_ACL_ACCESS; xprefix = JFFS2_XPREFIX_ACL_ACCESS;
if (acl) { if (acl) {
umode_t mode = inode->i_mode; umode_t mode;
rc = posix_acl_equiv_mode(acl, &mode);
if (rc < 0) rc = posix_acl_update_mode(inode, &mode, &acl);
if (rc)
return rc; return rc;
if (inode->i_mode != mode) { if (inode->i_mode != mode) {
struct iattr attr; struct iattr attr;
...@@ -257,8 +258,6 @@ static int jffs2_set_acl(struct inode *inode, int type, struct posix_acl *acl) ...@@ -257,8 +258,6 @@ static int jffs2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
if (rc < 0) if (rc < 0)
return rc; return rc;
} }
if (rc == 0)
acl = NULL;
} }
break; break;
case ACL_TYPE_DEFAULT: case ACL_TYPE_DEFAULT:
......
...@@ -693,8 +693,9 @@ static int can_set_system_xattr(struct inode *inode, const char *name, ...@@ -693,8 +693,9 @@ static int can_set_system_xattr(struct inode *inode, const char *name,
return rc; return rc;
} }
if (acl) { if (acl) {
rc = posix_acl_equiv_mode(acl, &inode->i_mode); struct posix_acl *old_acl = acl;
posix_acl_release(acl); rc = posix_acl_update_mode(inode, &inode->i_mode, &acl);
posix_acl_release(old_acl);
if (rc < 0) { if (rc < 0) {
printk(KERN_ERR printk(KERN_ERR
"posix_acl_equiv_mode returned %d\n", "posix_acl_equiv_mode returned %d\n",
......
...@@ -270,20 +270,14 @@ static int ocfs2_set_acl(handle_t *handle, ...@@ -270,20 +270,14 @@ static int ocfs2_set_acl(handle_t *handle,
case ACL_TYPE_ACCESS: case ACL_TYPE_ACCESS:
name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS; name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
if (acl) { if (acl) {
umode_t mode = inode->i_mode; umode_t mode;
ret = posix_acl_equiv_mode(acl, &mode); ret = posix_acl_update_mode(inode, &mode, &acl);
if (ret < 0) if (ret)
return ret;
ret = ocfs2_acl_set_mode(inode, di_bh,
handle, mode);
if (ret)
return ret; return ret;
else {
if (ret == 0)
acl = NULL;
ret = ocfs2_acl_set_mode(inode, di_bh,
handle, mode);
if (ret)
return ret;
}
} }
break; break;
case ACL_TYPE_DEFAULT: case ACL_TYPE_DEFAULT:
......
...@@ -407,6 +407,37 @@ posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p) ...@@ -407,6 +407,37 @@ posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
} }
EXPORT_SYMBOL(posix_acl_create); EXPORT_SYMBOL(posix_acl_create);
/**
* posix_acl_update_mode - update mode in set_acl
*
* Update the file mode when setting an ACL: compute the new file permission
* bits based on the ACL. In addition, if the ACL is equivalent to the new
* file mode, set *acl to NULL to indicate that no ACL should be set.
*
* As with chmod, clear the setgit bit if the caller is not in the owning group
* or capable of CAP_FSETID (see inode_change_ok).
*
* Called from set_acl inode operations.
*/
int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
struct posix_acl **acl)
{
umode_t mode = inode->i_mode;
int error;
error = posix_acl_equiv_mode(*acl, &mode);
if (error < 0)
return error;
if (error == 0)
*acl = NULL;
if (!in_group_p(inode->i_gid) &&
!capable_wrt_inode_uidgid(inode, CAP_FSETID))
mode &= ~S_ISGID;
*mode_p = mode;
return 0;
}
EXPORT_SYMBOL(posix_acl_update_mode);
int int
posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode) posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
{ {
......
...@@ -288,13 +288,9 @@ reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode, ...@@ -288,13 +288,9 @@ reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
case ACL_TYPE_ACCESS: case ACL_TYPE_ACCESS:
name = POSIX_ACL_XATTR_ACCESS; name = POSIX_ACL_XATTR_ACCESS;
if (acl) { if (acl) {
error = posix_acl_equiv_mode(acl, &inode->i_mode); error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
if (error < 0) if (error)
return error; return error;
else {
if (error == 0)
acl = NULL;
}
} }
break; break;
case ACL_TYPE_DEFAULT: case ACL_TYPE_DEFAULT:
......
...@@ -402,16 +402,15 @@ xfs_xattr_acl_set(struct dentry *dentry, const char *name, ...@@ -402,16 +402,15 @@ xfs_xattr_acl_set(struct dentry *dentry, const char *name,
goto out_release; goto out_release;
if (type == ACL_TYPE_ACCESS) { if (type == ACL_TYPE_ACCESS) {
umode_t mode = inode->i_mode; umode_t mode;
error = posix_acl_equiv_mode(acl, &mode); struct posix_acl *old_acl = acl;
if (error <= 0) { error = posix_acl_update_mode(inode, &mode, &acl);
posix_acl_release(acl);
acl = NULL;
if (error < 0) if (error)
return error; goto out_release;
} if (!acl)
posix_acl_release(old_acl);
error = xfs_set_mode(inode, mode); error = xfs_set_mode(inode, mode);
if (error) if (error)
......
...@@ -89,6 +89,7 @@ extern int posix_acl_permission(struct inode *, const struct posix_acl *, int); ...@@ -89,6 +89,7 @@ extern int posix_acl_permission(struct inode *, const struct posix_acl *, int);
extern struct posix_acl *posix_acl_from_mode(umode_t, gfp_t); extern struct posix_acl *posix_acl_from_mode(umode_t, gfp_t);
extern int posix_acl_equiv_mode(const struct posix_acl *, umode_t *); extern int posix_acl_equiv_mode(const struct posix_acl *, umode_t *);
extern int posix_acl_create(struct posix_acl **, gfp_t, umode_t *); extern int posix_acl_create(struct posix_acl **, gfp_t, umode_t *);
extern int posix_acl_update_mode(struct inode *, umode_t *, struct posix_acl **);
extern int posix_acl_chmod(struct posix_acl **, gfp_t, umode_t); extern int posix_acl_chmod(struct posix_acl **, gfp_t, umode_t);
extern struct posix_acl *get_posix_acl(struct inode *, int); extern struct posix_acl *get_posix_acl(struct inode *, int);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment