diff options
author | Darrick J. Wong <darrick.wong@oracle.com> | 2019-06-20 15:21:45 -0700 |
---|---|---|
committer | Darrick J. Wong <darrick.wong@oracle.com> | 2019-06-28 10:57:53 -0700 |
commit | 67c2c5742599c5a0ebf4cb7413755ee89dee7d67 (patch) | |
tree | 20a0e5dd28332a95fcb97b44be0de35adb54d500 /fs | |
parent | 81268851da178edd33a099b1d178cc63cae27569 (diff) |
vfs: don't allow writes to swap filesimmutable-swapfiles_2019-06-28
Don't let userspace write to an active swap file because the kernel
effectively has a long term lease on the storage and things could get
seriously corrupted if we let this happen.
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/attr.c | 16 | ||||
-rw-r--r-- | fs/block_dev.c | 3 |
2 files changed, 11 insertions, 8 deletions
diff --git a/fs/attr.c b/fs/attr.c index 1fcfdcc5b367..7480d5dd22c0 100644 --- a/fs/attr.c +++ b/fs/attr.c @@ -134,6 +134,14 @@ EXPORT_SYMBOL(setattr_prepare); */ int inode_newsize_ok(const struct inode *inode, loff_t offset) { + /* + * Truncation of in-use swapfiles is disallowed - the kernel owns the + * disk space now. We must prevent subsequent swapout to scribble on + * the now-freed blocks. + */ + if (IS_SWAPFILE(inode) && inode->i_size != offset) + return -ETXTBSY; + if (inode->i_size < offset) { unsigned long limit; @@ -142,14 +150,6 @@ int inode_newsize_ok(const struct inode *inode, loff_t offset) goto out_sig; if (offset > inode->i_sb->s_maxbytes) goto out_big; - } else { - /* - * truncation of in-use swapfiles is disallowed - it would - * cause subsequent swapout to scribble on the now-freed - * blocks. - */ - if (IS_SWAPFILE(inode)) - return -ETXTBSY; } return 0; diff --git a/fs/block_dev.c b/fs/block_dev.c index 749f5984425d..f57d15e5338b 100644 --- a/fs/block_dev.c +++ b/fs/block_dev.c @@ -1948,6 +1948,9 @@ ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from) if (bdev_read_only(I_BDEV(bd_inode))) return -EPERM; + if (IS_SWAPFILE(bd_inode)) + return -ETXTBSY; + if (!iov_iter_count(from)) return 0; |