From: "Darrick J. Wong" <darrick.wong@oracle.com>
To: matthew.garrett@nebula.com, yuchao0@huawei.com, tytso@mit.edu,
darrick.wong@oracle.com, ard.biesheuvel@linaro.org,
josef@toxicpanda.com, hch@infradead.org, clm@fb.com,
adilger.kernel@dilger.ca, viro@zeniv.linux.org.uk, jack@suse.com,
dsterba@suse.com, jaegeuk@kernel.org, jk@ozlabs.org
Cc: reiserfs-devel@vger.kernel.org, linux-efi@vger.kernel.org,
devel@lists.orangefs.org, linux-kernel@vger.kernel.org,
linux-f2fs-devel@lists.sourceforge.net,
linux-xfs@vger.kernel.org, linux-mm@kvack.org,
linux-nilfs@vger.kernel.org, linux-mtd@lists.infradead.org,
ocfs2-devel@oss.oracle.com, linux-fsdevel@vger.kernel.org,
linux-ext4@vger.kernel.org, linux-btrfs@vger.kernel.org
Subject: [PATCH 5/5] vfs: don't allow writes to swap files
Date: Tue, 25 Jun 2019 19:33:31 -0700 [thread overview]
Message-ID: <156151641177.2283603.7806026378321236401.stgit@magnolia> (raw)
In-Reply-To: <156151637248.2283603.8458727861336380714.stgit@magnolia>
From: Darrick J. Wong <darrick.wong@oracle.com>
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>
---
fs/attr.c | 3 +++
mm/filemap.c | 3 +++
mm/memory.c | 4 +++-
mm/mmap.c | 2 ++
mm/swapfile.c | 15 +++++++++++++--
5 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/fs/attr.c b/fs/attr.c
index 1fcfdcc5b367..42f4d4fb0631 100644
--- a/fs/attr.c
+++ b/fs/attr.c
@@ -236,6 +236,9 @@ int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **de
if (IS_IMMUTABLE(inode))
return -EPERM;
+ if (IS_SWAPFILE(inode))
+ return -ETXTBSY;
+
if ((ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) &&
IS_APPEND(inode))
return -EPERM;
diff --git a/mm/filemap.c b/mm/filemap.c
index dad85e10f5f8..fd80bc20e30a 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2938,6 +2938,9 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
if (IS_IMMUTABLE(inode))
return -EPERM;
+ if (IS_SWAPFILE(inode))
+ return -ETXTBSY;
+
if (!iov_iter_count(from))
return 0;
diff --git a/mm/memory.c b/mm/memory.c
index 4311cfdade90..c04c6a689995 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2235,7 +2235,9 @@ static vm_fault_t do_page_mkwrite(struct vm_fault *vmf)
vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
- if (vmf->vma->vm_file && IS_IMMUTABLE(file_inode(vmf->vma->vm_file)))
+ if (vmf->vma->vm_file &&
+ (IS_IMMUTABLE(file_inode(vmf->vma->vm_file)) ||
+ IS_SWAPFILE(file_inode(vmf->vma->vm_file))))
return VM_FAULT_SIGBUS;
ret = vmf->vma->vm_ops->page_mkwrite(vmf);
diff --git a/mm/mmap.c b/mm/mmap.c
index ac1e32205237..031807339869 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1488,6 +1488,8 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
return -EACCES;
if (IS_IMMUTABLE(file_inode(file)))
return -EPERM;
+ if (IS_SWAPFILE(file_inode(file)))
+ return -ETXTBSY;
}
/*
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 596ac98051c5..1ca4ee8c2d60 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -3165,6 +3165,19 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
if (error)
goto bad_swap;
+ /*
+ * Flush any pending IO and dirty mappings before we start using this
+ * swap file.
+ */
+ if (S_ISREG(inode->i_mode)) {
+ inode->i_flags |= S_SWAPFILE;
+ error = inode_drain_writes(inode);
+ if (error) {
+ inode->i_flags &= ~S_SWAPFILE;
+ goto bad_swap;
+ }
+ }
+
mutex_lock(&swapon_mutex);
prio = -1;
if (swap_flags & SWAP_FLAG_PREFER)
@@ -3185,8 +3198,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
atomic_inc(&proc_poll_event);
wake_up_interruptible(&proc_poll_wait);
- if (S_ISREG(inode->i_mode))
- inode->i_flags |= S_SWAPFILE;
error = 0;
goto out;
bad_swap:
next prev parent reply other threads:[~2019-06-26 2:36 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-26 2:32 [PATCH v5 0/5] vfs: make immutable files actually immutable Darrick J. Wong
2019-06-26 2:33 ` [PATCH 1/5] mm/fs: don't allow writes to immutable files Darrick J. Wong
2019-06-26 2:33 ` [PATCH 2/5] vfs: flush and wait for io when setting the immutable flag via SETFLAGS Darrick J. Wong
2019-06-26 2:33 ` [PATCH 3/5] vfs: flush and wait for io when setting the immutable flag via FSSETXATTR Darrick J. Wong
2019-06-26 2:33 ` [PATCH 4/5] vfs: don't allow most setxattr to immutable files Darrick J. Wong
2019-06-26 2:33 ` Darrick J. Wong [this message]
2019-06-26 3:51 ` [PATCH 5/5] vfs: don't allow writes to swap files Al Viro
2019-06-26 16:28 ` Darrick J. Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=156151641177.2283603.7806026378321236401.stgit@magnolia \
--to=darrick.wong@oracle.com \
--cc=adilger.kernel@dilger.ca \
--cc=ard.biesheuvel@linaro.org \
--cc=clm@fb.com \
--cc=devel@lists.orangefs.org \
--cc=dsterba@suse.com \
--cc=hch@infradead.org \
--cc=jack@suse.com \
--cc=jaegeuk@kernel.org \
--cc=jk@ozlabs.org \
--cc=josef@toxicpanda.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-efi@vger.kernel.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-mtd@lists.infradead.org \
--cc=linux-nilfs@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=matthew.garrett@nebula.com \
--cc=ocfs2-devel@oss.oracle.com \
--cc=reiserfs-devel@vger.kernel.org \
--cc=tytso@mit.edu \
--cc=viro@zeniv.linux.org.uk \
--cc=yuchao0@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).