Skip to content

Commit 0739310

Browse files
committed
posix_acl: Clear SGID bit when setting file permissions
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. References: CVE-2016-7097 Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Jeff Layton <jlayton@redhat.com> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
1 parent 5d3ddd8 commit 0739310

File tree

16 files changed

+89
-102
lines changed

16 files changed

+89
-102
lines changed

fs/9p/acl.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -276,32 +276,26 @@ static int v9fs_xattr_set_acl(const struct xattr_handler *handler,
276276
switch (handler->flags) {
277277
case ACL_TYPE_ACCESS:
278278
if (acl) {
279-
umode_t mode = inode->i_mode;
280-
retval = posix_acl_equiv_mode(acl, &mode);
281-
if (retval < 0)
279+
struct iattr iattr;
280+
281+
retval = posix_acl_update_mode(inode, &iattr.ia_mode, &acl);
282+
if (retval)
282283
goto err_out;
283-
else {
284-
struct iattr iattr;
285-
if (retval == 0) {
286-
/*
287-
* ACL can be represented
288-
* by the mode bits. So don't
289-
* update ACL.
290-
*/
291-
acl = NULL;
292-
value = NULL;
293-
size = 0;
294-
}
295-
/* Updte the mode bits */
296-
iattr.ia_mode = ((mode & S_IALLUGO) |
297-
(inode->i_mode & ~S_IALLUGO));
298-
iattr.ia_valid = ATTR_MODE;
299-
/* FIXME should we update ctime ?
300-
* What is the following setxattr update the
301-
* mode ?
284+
if (!acl) {
285+
/*
286+
* ACL can be represented
287+
* by the mode bits. So don't
288+
* update ACL.
302289
*/
303-
v9fs_vfs_setattr_dotl(dentry, &iattr);
290+
value = NULL;
291+
size = 0;
304292
}
293+
iattr.ia_valid = ATTR_MODE;
294+
/* FIXME should we update ctime ?
295+
* What is the following setxattr update the
296+
* mode ?
297+
*/
298+
v9fs_vfs_setattr_dotl(dentry, &iattr);
305299
}
306300
break;
307301
case ACL_TYPE_DEFAULT:

fs/btrfs/acl.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,9 @@ static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
7979
case ACL_TYPE_ACCESS:
8080
name = XATTR_NAME_POSIX_ACL_ACCESS;
8181
if (acl) {
82-
ret = posix_acl_equiv_mode(acl, &inode->i_mode);
83-
if (ret < 0)
82+
ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
83+
if (ret)
8484
return ret;
85-
if (ret == 0)
86-
acl = NULL;
8785
}
8886
ret = 0;
8987
break;

fs/ceph/acl.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,9 @@ int ceph_set_acl(struct inode *inode, struct posix_acl *acl, int type)
9595
case ACL_TYPE_ACCESS:
9696
name = XATTR_NAME_POSIX_ACL_ACCESS;
9797
if (acl) {
98-
ret = posix_acl_equiv_mode(acl, &new_mode);
99-
if (ret < 0)
98+
ret = posix_acl_update_mode(inode, &new_mode, &acl);
99+
if (ret)
100100
goto out;
101-
if (ret == 0)
102-
acl = NULL;
103101
}
104102
break;
105103
case ACL_TYPE_DEFAULT:

fs/ext2/acl.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,11 @@ ext2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
190190
case ACL_TYPE_ACCESS:
191191
name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
192192
if (acl) {
193-
error = posix_acl_equiv_mode(acl, &inode->i_mode);
194-
if (error < 0)
193+
error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
194+
if (error)
195195
return error;
196-
else {
197-
inode->i_ctime = CURRENT_TIME_SEC;
198-
mark_inode_dirty(inode);
199-
if (error == 0)
200-
acl = NULL;
201-
}
196+
inode->i_ctime = CURRENT_TIME_SEC;
197+
mark_inode_dirty(inode);
202198
}
203199
break;
204200

fs/ext4/acl.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,11 @@ __ext4_set_acl(handle_t *handle, struct inode *inode, int type,
193193
case ACL_TYPE_ACCESS:
194194
name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
195195
if (acl) {
196-
error = posix_acl_equiv_mode(acl, &inode->i_mode);
197-
if (error < 0)
196+
error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
197+
if (error)
198198
return error;
199-
else {
200-
inode->i_ctime = ext4_current_time(inode);
201-
ext4_mark_inode_dirty(handle, inode);
202-
if (error == 0)
203-
acl = NULL;
204-
}
199+
inode->i_ctime = ext4_current_time(inode);
200+
ext4_mark_inode_dirty(handle, inode);
205201
}
206202
break;
207203

fs/f2fs/acl.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,10 @@ static int __f2fs_set_acl(struct inode *inode, int type,
210210
case ACL_TYPE_ACCESS:
211211
name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
212212
if (acl) {
213-
error = posix_acl_equiv_mode(acl, &inode->i_mode);
214-
if (error < 0)
213+
error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
214+
if (error)
215215
return error;
216216
set_acl_inode(inode, inode->i_mode);
217-
if (error == 0)
218-
acl = NULL;
219217
}
220218
break;
221219

fs/gfs2/acl.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,11 @@ int __gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
9292
if (type == ACL_TYPE_ACCESS) {
9393
umode_t mode = inode->i_mode;
9494

95-
error = posix_acl_equiv_mode(acl, &mode);
96-
if (error < 0)
95+
error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
96+
if (error)
9797
return error;
98-
99-
if (error == 0)
100-
acl = NULL;
101-
102-
if (mode != inode->i_mode) {
103-
inode->i_mode = mode;
98+
if (mode != inode->i_mode)
10499
mark_inode_dirty(inode);
105-
}
106100
}
107101

108102
if (acl) {

fs/hfsplus/posix_acl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ int hfsplus_set_posix_acl(struct inode *inode, struct posix_acl *acl,
6565
case ACL_TYPE_ACCESS:
6666
xattr_name = XATTR_NAME_POSIX_ACL_ACCESS;
6767
if (acl) {
68-
err = posix_acl_equiv_mode(acl, &inode->i_mode);
69-
if (err < 0)
68+
err = posix_acl_update_mode(inode, &inode->i_mode, &acl);
69+
if (err)
7070
return err;
7171
}
7272
err = 0;

fs/jffs2/acl.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,10 @@ int jffs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
233233
case ACL_TYPE_ACCESS:
234234
xprefix = JFFS2_XPREFIX_ACL_ACCESS;
235235
if (acl) {
236-
umode_t mode = inode->i_mode;
237-
rc = posix_acl_equiv_mode(acl, &mode);
238-
if (rc < 0)
236+
umode_t mode;
237+
238+
rc = posix_acl_update_mode(inode, &mode, &acl);
239+
if (rc)
239240
return rc;
240241
if (inode->i_mode != mode) {
241242
struct iattr attr;
@@ -247,8 +248,6 @@ int jffs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
247248
if (rc < 0)
248249
return rc;
249250
}
250-
if (rc == 0)
251-
acl = NULL;
252251
}
253252
break;
254253
case ACL_TYPE_DEFAULT:

fs/jfs/acl.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,11 @@ static int __jfs_set_acl(tid_t tid, struct inode *inode, int type,
7878
case ACL_TYPE_ACCESS:
7979
ea_name = XATTR_NAME_POSIX_ACL_ACCESS;
8080
if (acl) {
81-
rc = posix_acl_equiv_mode(acl, &inode->i_mode);
82-
if (rc < 0)
81+
rc = posix_acl_update_mode(inode, &inode->i_mode, &acl);
82+
if (rc)
8383
return rc;
8484
inode->i_ctime = CURRENT_TIME;
8585
mark_inode_dirty(inode);
86-
if (rc == 0)
87-
acl = NULL;
8886
}
8987
break;
9088
case ACL_TYPE_DEFAULT:

fs/ocfs2/acl.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,11 @@ int ocfs2_set_acl(handle_t *handle,
241241
case ACL_TYPE_ACCESS:
242242
name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
243243
if (acl) {
244-
umode_t mode = inode->i_mode;
245-
ret = posix_acl_equiv_mode(acl, &mode);
246-
if (ret < 0)
247-
return ret;
244+
umode_t mode;
248245

249-
if (ret == 0)
250-
acl = NULL;
246+
ret = posix_acl_update_mode(inode, &mode, &acl);
247+
if (ret)
248+
return ret;
251249

252250
ret = ocfs2_acl_set_mode(inode, di_bh,
253251
handle, mode);

fs/orangefs/acl.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,11 @@ int orangefs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
7373
case ACL_TYPE_ACCESS:
7474
name = XATTR_NAME_POSIX_ACL_ACCESS;
7575
if (acl) {
76-
umode_t mode = inode->i_mode;
77-
/*
78-
* can we represent this with the traditional file
79-
* mode permission bits?
80-
*/
81-
error = posix_acl_equiv_mode(acl, &mode);
82-
if (error < 0) {
83-
gossip_err("%s: posix_acl_equiv_mode err: %d\n",
76+
umode_t mode;
77+
78+
error = posix_acl_update_mode(inode, &mode, &acl);
79+
if (error) {
80+
gossip_err("%s: posix_acl_update_mode err: %d\n",
8481
__func__,
8582
error);
8683
return error;
@@ -90,8 +87,6 @@ int orangefs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
9087
SetModeFlag(orangefs_inode);
9188
inode->i_mode = mode;
9289
mark_inode_dirty_sync(inode);
93-
if (error == 0)
94-
acl = NULL;
9590
}
9691
break;
9792
case ACL_TYPE_DEFAULT:

fs/posix_acl.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,37 @@ posix_acl_create(struct inode *dir, umode_t *mode,
626626
}
627627
EXPORT_SYMBOL_GPL(posix_acl_create);
628628

629+
/**
630+
* posix_acl_update_mode - update mode in set_acl
631+
*
632+
* Update the file mode when setting an ACL: compute the new file permission
633+
* bits based on the ACL. In addition, if the ACL is equivalent to the new
634+
* file mode, set *acl to NULL to indicate that no ACL should be set.
635+
*
636+
* As with chmod, clear the setgit bit if the caller is not in the owning group
637+
* or capable of CAP_FSETID (see inode_change_ok).
638+
*
639+
* Called from set_acl inode operations.
640+
*/
641+
int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
642+
struct posix_acl **acl)
643+
{
644+
umode_t mode = inode->i_mode;
645+
int error;
646+
647+
error = posix_acl_equiv_mode(*acl, &mode);
648+
if (error < 0)
649+
return error;
650+
if (error == 0)
651+
*acl = NULL;
652+
if (!in_group_p(inode->i_gid) &&
653+
!capable_wrt_inode_uidgid(inode, CAP_FSETID))
654+
mode &= ~S_ISGID;
655+
*mode_p = mode;
656+
return 0;
657+
}
658+
EXPORT_SYMBOL(posix_acl_update_mode);
659+
629660
/*
630661
* Fix up the uids and gids in posix acl extended attributes in place.
631662
*/

fs/reiserfs/xattr_acl.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,9 @@ __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
242242
case ACL_TYPE_ACCESS:
243243
name = XATTR_NAME_POSIX_ACL_ACCESS;
244244
if (acl) {
245-
error = posix_acl_equiv_mode(acl, &inode->i_mode);
246-
if (error < 0)
245+
error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
246+
if (error)
247247
return error;
248-
else {
249-
if (error == 0)
250-
acl = NULL;
251-
}
252248
}
253249
break;
254250
case ACL_TYPE_DEFAULT:

fs/xfs/xfs_acl.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,11 @@ xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
257257
return error;
258258

259259
if (type == ACL_TYPE_ACCESS) {
260-
umode_t mode = inode->i_mode;
261-
error = posix_acl_equiv_mode(acl, &mode);
262-
263-
if (error <= 0) {
264-
acl = NULL;
265-
266-
if (error < 0)
267-
return error;
268-
}
260+
umode_t mode;
269261

262+
error = posix_acl_update_mode(inode, &mode, &acl);
263+
if (error)
264+
return error;
270265
error = xfs_set_mode(inode, mode);
271266
if (error)
272267
return error;

include/linux/posix_acl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ extern int set_posix_acl(struct inode *, int, struct posix_acl *);
9393
extern int posix_acl_chmod(struct inode *, umode_t);
9494
extern int posix_acl_create(struct inode *, umode_t *, struct posix_acl **,
9595
struct posix_acl **);
96+
extern int posix_acl_update_mode(struct inode *, umode_t *, struct posix_acl **);
9697

9798
extern int simple_set_acl(struct inode *, struct posix_acl *, int);
9899
extern int simple_acl_create(struct inode *, struct inode *);

0 commit comments

Comments
 (0)