Skip to content

Commit 2c27c65

Browse files
Christoph HellwigAl Viro
authored andcommitted
check ATTR_SIZE contraints in inode_change_ok
Make sure we check the truncate constraints early on in ->setattr by adding those checks to inode_change_ok. Also clean up and document inode_change_ok to make this obvious. As a fallout we don't have to call inode_newsize_ok from simple_setsize and simplify it down to a truncate_setsize which doesn't return an error. This simplifies a lot of setattr implementations and means we use truncate_setsize almost everywhere. Get rid of fat_setsize now that it's trivial and mark ext2_setsize static to make the calling convention obvious. Keep the inode_newsize_ok in vmtruncate for now as all callers need an audit for its removal anyway. Note: setattr code in ecryptfs doesn't call inode_change_ok at all and needs a deeper audit, but that is left for later. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent db78b87 commit 2c27c65

21 files changed

Lines changed: 108 additions & 156 deletions

File tree

‎fs/adfs/inode.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,7 @@ adfs_notify_change(struct dentry *dentry, struct iattr *attr)
333333

334334
/* XXX: this is missing some actual on-disk truncation.. */
335335
if (ia_valid & ATTR_SIZE)
336-
error = simple_setsize(inode, attr->ia_size);
337-
338-
if (error)
339-
goto out;
336+
truncate_setsize(inode, attr->ia_size);
340337

341338
if (ia_valid & ATTR_MTIME) {
342339
inode->i_mtime = attr->ia_mtime;

‎fs/attr.c‎

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,53 @@
1414
#include <linux/fcntl.h>
1515
#include <linux/security.h>
1616

17-
/* Taken over from the old code... */
18-
19-
/* POSIX UID/GID verification for setting inode attributes. */
17+
/**
18+
* inode_change_ok - check if attribute changes to an inode are allowed
19+
* @inode: inode to check
20+
* @attr: attributes to change
21+
*
22+
* Check if we are allowed to change the attributes contained in @attr
23+
* in the given inode. This includes the normal unix access permission
24+
* checks, as well as checks for rlimits and others.
25+
*
26+
* Should be called as the first thing in ->setattr implementations,
27+
* possibly after taking additional locks.
28+
*/
2029
int inode_change_ok(const struct inode *inode, struct iattr *attr)
2130
{
22-
int retval = -EPERM;
2331
unsigned int ia_valid = attr->ia_valid;
2432

33+
/*
34+
* First check size constraints. These can't be overriden using
35+
* ATTR_FORCE.
36+
*/
37+
if (ia_valid & ATTR_SIZE) {
38+
int error = inode_newsize_ok(inode, attr->ia_size);
39+
if (error)
40+
return error;
41+
}
42+
2543
/* If force is set do it anyway. */
2644
if (ia_valid & ATTR_FORCE)
27-
goto fine;
45+
return 0;
2846

2947
/* Make sure a caller can chown. */
3048
if ((ia_valid & ATTR_UID) &&
3149
(current_fsuid() != inode->i_uid ||
3250
attr->ia_uid != inode->i_uid) && !capable(CAP_CHOWN))
33-
goto error;
51+
return -EPERM;
3452

3553
/* Make sure caller can chgrp. */
3654
if ((ia_valid & ATTR_GID) &&
3755
(current_fsuid() != inode->i_uid ||
3856
(!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid)) &&
3957
!capable(CAP_CHOWN))
40-
goto error;
58+
return -EPERM;
4159

4260
/* Make sure a caller can chmod. */
4361
if (ia_valid & ATTR_MODE) {
4462
if (!is_owner_or_cap(inode))
45-
goto error;
63+
return -EPERM;
4664
/* Also check the setgid bit! */
4765
if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
4866
inode->i_gid) && !capable(CAP_FSETID))
@@ -52,12 +70,10 @@ int inode_change_ok(const struct inode *inode, struct iattr *attr)
5270
/* Check for setting the inode time. */
5371
if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
5472
if (!is_owner_or_cap(inode))
55-
goto error;
73+
return -EPERM;
5674
}
57-
fine:
58-
retval = 0;
59-
error:
60-
return retval;
75+
76+
return 0;
6177
}
6278
EXPORT_SYMBOL(inode_change_ok);
6379

@@ -113,7 +129,7 @@ EXPORT_SYMBOL(inode_newsize_ok);
113129
*
114130
* setattr_copy updates the inode's metadata with that specified
115131
* in attr. Noticably missing is inode size update, which is more complex
116-
* as it requires pagecache updates. See simple_setsize.
132+
* as it requires pagecache updates.
117133
*
118134
* The inode is not marked as dirty after this operation. The rationale is
119135
* that for "simple" filesystems, the struct inode is the inode storage.

‎fs/ecryptfs/inode.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -804,10 +804,20 @@ static int truncate_upper(struct dentry *dentry, struct iattr *ia,
804804
size_t num_zeros = (PAGE_CACHE_SIZE
805805
- (ia->ia_size & ~PAGE_CACHE_MASK));
806806

807+
808+
/*
809+
* XXX(truncate) this should really happen at the begginning
810+
* of ->setattr. But the code is too messy to that as part
811+
* of a larger patch. ecryptfs is also totally missing out
812+
* on the inode_change_ok check at the beginning of
813+
* ->setattr while would include this.
814+
*/
815+
rc = inode_newsize_ok(inode, ia->ia_size);
816+
if (rc)
817+
goto out;
818+
807819
if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
808-
rc = simple_setsize(inode, ia->ia_size);
809-
if (rc)
810-
goto out;
820+
truncate_setsize(inode, ia->ia_size);
811821
lower_ia->ia_size = ia->ia_size;
812822
lower_ia->ia_valid |= ATTR_SIZE;
813823
goto out;
@@ -830,7 +840,7 @@ static int truncate_upper(struct dentry *dentry, struct iattr *ia,
830840
goto out;
831841
}
832842
}
833-
simple_setsize(inode, ia->ia_size);
843+
truncate_setsize(inode, ia->ia_size);
834844
rc = ecryptfs_write_inode_size_to_metadata(inode);
835845
if (rc) {
836846
printk(KERN_ERR "Problem with "

‎fs/ext2/inode.c‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,15 +1156,10 @@ static void ext2_truncate_blocks(struct inode *inode, loff_t offset)
11561156
__ext2_truncate_blocks(inode, offset);
11571157
}
11581158

1159-
int ext2_setsize(struct inode *inode, loff_t newsize)
1159+
static int ext2_setsize(struct inode *inode, loff_t newsize)
11601160
{
1161-
loff_t oldsize;
11621161
int error;
11631162

1164-
error = inode_newsize_ok(inode, newsize);
1165-
if (error)
1166-
return error;
1167-
11681163
if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
11691164
S_ISLNK(inode->i_mode)))
11701165
return -EINVAL;
@@ -1184,10 +1179,7 @@ int ext2_setsize(struct inode *inode, loff_t newsize)
11841179
if (error)
11851180
return error;
11861181

1187-
oldsize = inode->i_size;
1188-
i_size_write(inode, newsize);
1189-
truncate_pagecache(inode, oldsize, newsize);
1190-
1182+
truncate_setsize(inode, newsize);
11911183
__ext2_truncate_blocks(inode, newsize);
11921184

11931185
inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;

‎fs/fat/fat.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ extern long fat_generic_ioctl(struct file *filp, unsigned int cmd,
306306
extern const struct file_operations fat_file_operations;
307307
extern const struct inode_operations fat_file_inode_operations;
308308
extern int fat_setattr(struct dentry * dentry, struct iattr * attr);
309-
extern int fat_setsize(struct inode *inode, loff_t offset);
310309
extern void fat_truncate_blocks(struct inode *inode, loff_t offset);
311310
extern int fat_getattr(struct vfsmount *mnt, struct dentry *dentry,
312311
struct kstat *stat);

‎fs/fat/file.c‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -364,18 +364,6 @@ static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
364364
return 0;
365365
}
366366

367-
int fat_setsize(struct inode *inode, loff_t offset)
368-
{
369-
int error;
370-
371-
error = simple_setsize(inode, offset);
372-
if (error)
373-
return error;
374-
fat_truncate_blocks(inode, offset);
375-
376-
return error;
377-
}
378-
379367
#define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
380368
/* valid file mode bits */
381369
#define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
@@ -441,9 +429,8 @@ int fat_setattr(struct dentry *dentry, struct iattr *attr)
441429
}
442430

443431
if (attr->ia_valid & ATTR_SIZE) {
444-
error = fat_setsize(inode, attr->ia_size);
445-
if (error)
446-
goto out;
432+
truncate_setsize(inode, attr->ia_size);
433+
fat_truncate_blocks(inode, attr->ia_size);
447434
}
448435

449436
setattr_copy(inode, attr);

‎fs/fuse/dir.c‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,12 +1280,8 @@ static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
12801280
if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
12811281
return 0;
12821282

1283-
if (attr->ia_valid & ATTR_SIZE) {
1284-
err = inode_newsize_ok(inode, attr->ia_size);
1285-
if (err)
1286-
return err;
1283+
if (attr->ia_valid & ATTR_SIZE)
12871284
is_truncate = true;
1288-
}
12891285

12901286
req = fuse_get_req(fc);
12911287
if (IS_ERR(req))

‎fs/gfs2/aops.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,12 +702,12 @@ static int gfs2_write_begin(struct file *file, struct address_space *mapping,
702702
page_cache_release(page);
703703

704704
/*
705-
* XXX(hch): the call below should probably be replaced with
705+
* XXX(truncate): the call below should probably be replaced with
706706
* a call to the gfs2-specific truncate blocks helper to actually
707707
* release disk blocks..
708708
*/
709709
if (pos + len > ip->i_inode.i_size)
710-
simple_setsize(&ip->i_inode, ip->i_inode.i_size);
710+
truncate_setsize(&ip->i_inode, ip->i_inode.i_size);
711711
out_endtrans:
712712
gfs2_trans_end(sdp);
713713
out_trans_fail:

‎fs/gfs2/ops_inode.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ int gfs2_permission(struct inode *inode, int mask)
10721072
}
10731073

10741074
/*
1075-
* XXX: should be changed to have proper ordering by opencoding simple_setsize
1075+
* XXX(truncate): the truncate_setsize calls should be moved to the end.
10761076
*/
10771077
static int setattr_size(struct inode *inode, struct iattr *attr)
10781078
{
@@ -1084,10 +1084,8 @@ static int setattr_size(struct inode *inode, struct iattr *attr)
10841084
error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
10851085
if (error)
10861086
return error;
1087-
error = simple_setsize(inode, attr->ia_size);
1087+
truncate_setsize(inode, attr->ia_size);
10881088
gfs2_trans_end(sdp);
1089-
if (error)
1090-
return error;
10911089
}
10921090

10931091
error = gfs2_truncatei(ip, attr->ia_size);

‎fs/jffs2/fs.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
169169
mutex_unlock(&f->sem);
170170
jffs2_complete_reservation(c);
171171

172-
/* We have to do the simple_setsize() without f->sem held, since
172+
/* We have to do the truncate_setsize() without f->sem held, since
173173
some pages may be locked and waiting for it in readpage().
174174
We are protected from a simultaneous write() extending i_size
175175
back past iattr->ia_size, because do_truncate() holds the
176176
generic inode semaphore. */
177177
if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size) {
178-
simple_setsize(inode, iattr->ia_size);
178+
truncate_setsize(inode, iattr->ia_size);
179179
inode->i_blocks = (inode->i_size + 511) >> 9;
180180
}
181181

0 commit comments

Comments
 (0)