1 ext4: fix setattr project check in fssetxattr ioctl
3 From: Wang Shilong <wangshilong1991@gmail.com>
5 Currently, project quota could be changed by fssetxattr
6 ioctl, and existed permission check inode_owner_or_capable()
7 is obviously not enough, just think that common users could
8 change project id of file, that could make users to
9 break project quota easily.
11 This patch try to follow same regular of xfs project
14 "Project Quota ID state is only allowed to change from
15 within the init namespace. Enforce that restriction only
16 if we are trying to change the quota ID state.
17 Everything else is allowed in user namespaces."
19 Besides that, check and set project id'state should
20 be an atomic operation, protect whole operation with
21 inode lock, ext4_ioctl_setproject() is only used for
22 ioctl EXT4_IOC_FSSETXATTR, we have held mnt_want_write_file()
23 before ext4_ioctl_setflags(), and ext4_ioctl_setproject()
24 is called after ext4_ioctl_setflags(), we could share
25 codes, so remove it inside ext4_ioctl_setproject().
27 Signed-off-by: Wang Shilong <wshilong@ddn.com>
28 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
29 Reviewed-by: Andreas Dilger <adilger@dilger.ca>
32 v3->v4: explain why patch removed mnt_want/drop_write_file()
33 inside ext4_ioctl_setproject() since diff file is
35 v2->v3: change function declaration to one line.
36 v1->v2: rename ext4_ioctl_setattr_check_projid()
37 to ext4_ioctl_check_project()
38 fs/ext4/ioctl.c | 60 +++++++++++++++++++++++++++++++++++----------------------
39 1 file changed, 37 insertions(+), 23 deletions(-)
41 diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
42 index a707411..f81102b 100644
45 @@ -339,19 +339,14 @@ static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
46 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
49 - err = mnt_want_write_file(filp);
55 /* Is it quota file? Do not allow user to mess with it */
56 if (ext4_is_quota_file(inode))
60 err = ext4_get_inode_loc(inode, &iloc);
65 raw_inode = ext4_raw_inode(&iloc);
66 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
67 @@ -359,7 +354,7 @@ static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
68 EXT4_SB(sb)->s_want_extra_isize,
76 @@ -369,10 +364,8 @@ static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
77 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
78 EXT4_QUOTA_INIT_BLOCKS(sb) +
79 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
80 - if (IS_ERR(handle)) {
81 - err = PTR_ERR(handle);
85 + return PTR_ERR(handle);
87 err = ext4_reserve_inode_write(handle, inode, &iloc);
89 @@ -400,9 +393,6 @@ static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
92 ext4_journal_stop(handle);
94 - inode_unlock(inode);
95 - mnt_drop_write_file(filp);
99 @@ -626,6 +616,30 @@ static long ext4_ioctl_group_add(struct file *file,
103 +static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
106 + * Project Quota ID state is only allowed to change from within the init
107 + * namespace. Enforce that restriction only if we are trying to change
108 + * the quota ID state. Everything else is allowed in user namespaces.
110 + if (current_user_ns() == &init_user_ns)
113 + if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid)
116 + if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) {
117 + if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
120 + if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
127 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
129 struct inode *inode = file_inode(filp);
130 @@ -1025,19 +1039,19 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
134 + err = ext4_ioctl_check_project(inode, &fa);
137 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
138 (flags & EXT4_FL_XFLAG_VISIBLE);
139 err = ext4_ioctl_setflags(inode, flags);
140 - inode_unlock(inode);
141 - mnt_drop_write_file(filp);
146 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
152 + inode_unlock(inode);
153 + mnt_drop_write_file(filp);
156 case EXT4_IOC_SHUTDOWN:
157 return ext4_shutdown(sb, arg);