vfs: make active swap files unwritable
Subsystems (6)
v3 cover vfs: make active swap files unwritable
Hi all,
I discovered that it's possible for userspace to write to active swap files and swap devices. While activated, the kernel effectively holds an irrevocable (except by swapoff) longterm lease on the storage associated with the swap device, so we need to shut down this vector for memory corruption of userspace programs.
Nothing new for v3 other than rebasing against 5.3-rc4.
If you're going to start using this mess, you probably ought to just pull from my git trees, which are linked below.
This has been lightly tested with fstests. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=immutable-swapfiles
fstests git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfstests-dev.git/log/?h=immutable-swapfiles
PATCH v3 1/2 mm: set S_SWAPFILE on blockdev swap devices
From: Darrick J. Wong <[email protected]>
Set S_SWAPFILE on block device inodes so that they have the same protections as a swap flie.
--- mm/swapfile.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-)
mm/swapfile.c +15 -16
@@ -2368,9 +2368,8 @@ EXPORT_SYMBOL_GPL(add_swap_extent);* requirements, they are simply tossed out - we will never use those blocks * for swapping. * -* For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This -* prevents root from shooting her foot off by ftruncating an in-use swapfile, -* which will scribble on the fs. +* For all swap devices we set S_SWAPFILE across the life of the swapon. This +* prevents users from writing to the swap device, which will corrupt memory. * * The amount of disk space which a single swap extent represents varies. * Typically it is in the 1-4 megabyte range. So we can have hundreds of
@@ -2661,13 +2660,14 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) inode = mapping->host;if (S_ISBLK (inode->i_mode)) {struct block_device *bdev =I_BDEV (inode); +set_blocksize (bdev, old_block_size);blkdev_put (bdev,FMODE_READ |FMODE_WRITE |FMODE_EXCL ); - }else { -inode_lock (inode); - inode->i_flags &= ~S_SWAPFILE ; -inode_unlock (inode); } + +inode_lock (inode); + inode->i_flags &= ~S_SWAPFILE ; +inode_unlock (inode);filp_close (swap_file,NULL );/*
@@ -2890,11 +2890,11 @@ static int claim_swapfile(struct swap_info_struct *p, struct inode *inode) p->flags |=SWP_BLKDEV ; }else if (S_ISREG (inode->i_mode)) { p->bdev = inode->i_sb->s_bdev; -inode_lock (inode); -if (IS_SWAPFILE (inode)) -return -EBUSY ; - }else -return -EINVAL ; + } + +inode_lock (inode); +if (IS_SWAPFILE (inode)) +return -EBUSY ;return 0 ; }
@@ -3295,8 +3295,7 @@ 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 ; + inode->i_flags |=S_SWAPFILE ; error =0 ; goto out; bad_swap:
@@ -3318,7 +3317,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)if (inced_nr_rotate_swap)atomic_dec (&nr_rotate_swap);if (swap_file) { -if (inode &&S_ISREG (inode->i_mode)) { +if (inode) {inode_unlock (inode); inode =NULL ; }
@@ -3331,7 +3330,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) }if (name)putname (name); -if (inode &&S_ISREG (inode->i_mode)) +if (inode)inode_unlock (inode);if (!error)enable_swap_slots_cache ();
Re: [PATCH 1/2] mm: set S_SWAPFILE on blockdev swap devices
Looks good,
PATCH v3 2/2 vfs: don't allow writes to swap files
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
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.
--- fs/block_dev.c | 3 +++ mm/filemap.c | 3 +++ mm/memory.c | 4 ++++ mm/mmap.c | 8 ++++++-- mm/swapfile.c | 12 +++++++++++- 5 files changed, 27 insertions(+), 3 deletions(-)
fs/block_dev.c +3 -0
@@ -2011,6 +2011,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 ;
mm/filemap.c +3 -0
@@ -2988,6 +2988,9 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)loff_t count;int ret; +if (IS_SWAPFILE (inode)) +return -ETXTBSY ; +if (!iov_iter_count (from))return 0 ;
mm/memory.c +4 -0
@@ -2196,6 +2196,10 @@ 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_SWAPFILE (vmf->vma->vm_file->f_mapping->host)) +return VM_FAULT_SIGBUS ; + ret = vmf->vma->vm_ops->page_mkwrite (vmf);/* Restore original flags so that caller is not surprised */ vmf->flags = old_flags;
mm/mmap.c +6 -2
@@ -1483,8 +1483,12 @@ unsigned long do_mmap(struct file *file, unsigned long addr,case MAP_SHARED_VALIDATE :if (flags & ~flags_mask)return -EOPNOTSUPP ; -if ((prot&PROT_WRITE ) && !(file->f_mode&FMODE_WRITE )) -return -EACCES ; +if (prot &PROT_WRITE ) { +if (!(file->f_mode &FMODE_WRITE )) +return -EACCES ; +if (IS_SWAPFILE (file->f_mapping->host)) +return -ETXTBSY ; + }/* * Make sure we don't allow writing to an append-only
mm/swapfile.c +11 -1
@@ -3275,6 +3275,17 @@ 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 device. +*/ + 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 )
@@ -3295,7 +3306,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)atomic_inc (&proc_poll_event);wake_up_interruptible (&proc_poll_wait); - inode->i_flags |=S_SWAPFILE ; error =0 ; goto out; bad_swap:
Re: [PATCH 2/2] vfs: don't allow writes to swap files
The new checks look fine to me, but where does the inode_drain_writes() function come from, I can't find that in my tree anywhere.
Also what does inode_drain_writes do about existing shared writable mapping? Do we even care about that corner case?
Re: [PATCH 2/2] vfs: don't allow writes to swap files
Quoted 2 lines · Christoph Hellwig
The new checks look fine to me, but where does the
inode_drain_writes()function come from, I can't find that in my tree anywhere.
Doh. Forgot to include that patch in the series. :(
/*
* Flush file data before changing attributes. Caller must hold any locks
* required to prevent further writes to this file until we're done setting
* flags.
*/
static inline int inode_drain_writes(struct inode *inode)
{
inode_dio_wait(inode);
return filemap_write_and_wait(inode->i_mapping);
}Quoted 2 lines
Also what does
inode_drain_writesdo about existing shared writable mapping? Do we even care about that corner case?
We probably ought to flush and invalidate the pagecache for the entire file so that page_mkwrite can bounce off the swapfile.
--D
PATCH v2 2/2 vfs: don't allow writes to swap files
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
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.
---
v2: add missing inode_drain_writes
--- fs/block_dev.c | 3 +++ include/linux/fs.h | 11 +++++++++++ mm/filemap.c | 3 +++ mm/memory.c | 4 ++++ mm/mmap.c | 8 ++++++-- mm/swapfile.c | 12 +++++++++++- 6 files changed, 38 insertions(+), 3 deletions(-)
fs/block_dev.c +3 -0
@@ -1978,6 +1978,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 ;
include/linux/fs.h +11 -0
@@ -3547,4 +3547,15 @@ static inline void simple_fill_fsxattr(struct fsxattr *fa, __u32 xflags) fa->fsx_xflags = xflags; } +/* +* Flush file data before changing attributes. Caller must hold any locks +* required to prevent further writes to this file until we're done setting +* flags. +*/ +static inline int inode_drain_writes (struct inode *inode) +{ +inode_dio_wait (inode); +return filemap_write_and_wait (inode->i_mapping); +} +#endif /* _LINUX_FS_H */
mm/filemap.c +3 -0
@@ -2989,6 +2989,9 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)loff_t count;int ret; +if (IS_SWAPFILE (inode)) +return -ETXTBSY ; +if (!iov_iter_count (from))return 0 ;
mm/memory.c +4 -0
@@ -2196,6 +2196,10 @@ 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_SWAPFILE (vmf->vma->vm_file->f_mapping->host)) +return VM_FAULT_SIGBUS ; + ret = vmf->vma->vm_ops->page_mkwrite (vmf);/* Restore original flags so that caller is not surprised */ vmf->flags = old_flags;
mm/mmap.c +6 -2
@@ -1483,8 +1483,12 @@ unsigned long do_mmap(struct file *file, unsigned long addr,case MAP_SHARED_VALIDATE :if (flags & ~flags_mask)return -EOPNOTSUPP ; -if ((prot&PROT_WRITE ) && !(file->f_mode&FMODE_WRITE )) -return -EACCES ; +if (prot &PROT_WRITE ) { +if (!(file->f_mode &FMODE_WRITE )) +return -EACCES ; +if (IS_SWAPFILE (file->f_mapping->host)) +return -ETXTBSY ; + }/* * Make sure we don't allow writing to an append-only
mm/swapfile.c +11 -1
@@ -3275,6 +3275,17 @@ 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 device. +*/ + 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 )
@@ -3295,7 +3306,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)atomic_inc (&proc_poll_event);wake_up_interruptible (&proc_poll_wait); - inode->i_flags |=S_SWAPFILE ; error =0 ; goto out; bad_swap:
Re: [PATCH v2 2/2] vfs: don't allow writes to swap files
Quoted 7 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
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 <[email protected]>
Looks good,
[PATCH RFC 3/2] fstests: check that we can't write to swap files
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
While active, the media backing a swap file is leased to the kernel. Userspace has no business writing to it. Make sure we can't do this.
--- tests/generic/717 | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/generic/717.out | 7 ++++++ tests/generic/718 | 46 ++++++++++++++++++++++++++++++++++++++ tests/generic/718.out | 5 ++++ tests/generic/group | 2 ++ 5 files changed, 120 insertions(+) create mode 100755 tests/generic/717 create mode 100644 tests/generic/717.out create mode 100755 tests/generic/718 create mode 100644 tests/generic/718.out
tests/generic/717 new file +60 -0
@@ -0,0 +1,60 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0-or-newer +# Copyright (c) 2019, Oracle and/or its affiliates. All Rights Reserved. +# +# FS QA Test No. 717 +# +# Check that we can't modify a file that's an active swap file. + +seq=`basename $0` +seqres=$RESULT_DIR/$seq +echo "QA output created by $seq" + +here=`pwd` +tmp=/tmp/$$ +status=1 # failure is the default! +trap "_cleanup; exit \$status" 0 1 2 3 15 + +_cleanup() +{ + cd / + swapoff $testfile + rm -rf $tmp.* $testfile +} + +# get standard environment, filters and checks +. ./common/rc +. ./common/filter + +# real QA test starts here +_supported_os Linux +_supported_fs generic +_require_scratch_swapfile + +rm -f $seqres.full + +_scratch_mkfs > $seqres.full 2>&1 +_scratch_mount >> $seqres.full 2>&1 + +testfile=$SCRATCH_MNT/$seq.swap + +_format_swapfile $testfile 20m +swapon $testfile 2>&1 | _filter_scratch + +# Can we write to it? +$XFS_IO_PROG -c 'pwrite -S 0x59 64k 64k' $testfile +$XFS_IO_PROG -d -c 'pwrite -S 0x60 64k 64k' $testfile +$XFS_IO_PROG -c 'mmap -rw 64k 64k' -c 'mwrite -S 0x61 64k 64k' $testfile + +# Can we change the file size? +$XFS_IO_PROG -c 'truncate 18m' $testfile + +# Can you fallocate the file? +$XFS_IO_PROG -c 'falloc 0 32m' $testfile + +# We test that you can't reflink, dedupe, or copy_file_range into a swapfile +# in other tests. + +# success, all done +status=0 +exit
tests/generic/717.out new file +7 -0
@@ -0,0 +1,7 @@ +QA output created by 717 +pwrite: Text file busy +pwrite: Text file busy +mmap: Text file busy +no mapped regions, try 'help mmap' +ftruncate: Text file busy +fallocate: Text file busy
tests/generic/718 new file +46 -0
46 lines not on this page · raw message
tests/generic/718.out new file +5 -0
@@ -0,0 +1,5 @@ +QA output created by 718 +pwrite: Text file busy +pwrite: Text file busy +mmap: Text file busy +no mapped regions, try 'help mmap'
tests/generic/group +2 -0
@@ -570,3 +570,5 @@ 565 auto quick copy_range 715 auto quick rw 716 auto quick rw +717 auto quick rw swap +718 auto quick rw swap
Andrew Morton
maintainerRe: [PATCH RFC 3/2] fstests: check that we can't write to swap files
Quoted 2 lines · On Thu, 15 Aug 2019 09:34:34 -0700 "Darrick J. Wong" <[email protected]>…
While active, the media backing a swap file is leased to the kernel. Userspace has no business writing to it. Make sure we can't do this.
I don't think this tests the case where a file was already open for writing and someone does swapon(that file)?
And then does swapoff(that file), when writes should start working again?
Ditto all the above, with s/open/mmap/.
Do we handle (and test!) the case where there's unwritten dirty pagecache at the time of swapon()? Ditto pte-dirty MAP_SHARED pages?
Re: [PATCH RFC 3/2] fstests: check that we can't write to swap files
Quoted 11 lines · Andrew Morton
Quoted 2 lines · On Thu, 15 Aug 2019 09:34:34 -0700 "Darrick J. Wong" <[email protected]>…
While active, the media backing a swap file is leased to the kernel. Userspace has no business writing to it. Make sure we can't do this.
I don't think this tests the case where a file was already open for writing and someone does swapon(that file)?
And then does swapoff(that file), when writes should start working again?
Ditto all the above, with s/open/mmap/.
Heh, ok. I'll start working on a C program to do that.
Quoted 2 lines
Do we handle (and test!) the case where there's unwritten dirty pagecache at the time of
swapon()? Ditto pte-dirtyMAP_SHAREDpages?
One of the tests I wrote for iomap_swapfile_activate way back when checks that. The iomap version calls vfs_fsync, but AFAICT generic_swapfile_activate doesn't do that.
--D
[PATCH v2 RFC 3/2] fstests: check that we can't write to swap files
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
While active, the media backing a swap file is leased to the kernel. Userspace has no business writing to it. Make sure we can't do this.
---
v2: add tests for writable fds after swapon
--- src/swapon.c | 135 ++++++++++++++++++++++++++++++++++++++++++++++++- tests/generic/717 | 70 +++++++++++++++++++++++++ tests/generic/717.out | 14 +++++ tests/generic/718 | 55 ++++++++++++++++++++ tests/generic/718.out | 12 ++++ tests/generic/group | 2 + 6 files changed, 284 insertions(+), 4 deletions(-) create mode 100755 tests/generic/717 create mode 100644 tests/generic/717.out create mode 100755 tests/generic/718 create mode 100644 tests/generic/718.out
src/swapon.c +131 -4
@@ -3,22 +3,149 @@#include <stdio.h> #include <stdlib.h> #include <unistd.h> +#include <string.h> #include <sys/swap.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <fcntl.h> +#include <signal.h> + +static void usage (const char *prog) +{ +fprintf (stderr,"usage: %s [-v verb] PATH\n" , prog); +exit (EXIT_FAILURE ); +} + +enum verbs { +TEST_SWAPON =0 , +TEST_WRITE , +TEST_MWRITE_AFTER , +TEST_MWRITE_BEFORE_AND_MWRITE_AFTER , +TEST_MWRITE_BEFORE , +MAX_TEST_VERBS , +}; + +#define BUF_SIZE 262144 +static char buf[BUF_SIZE ]; + +static void handle_signal (int signal) +{ +fprintf (stderr,"Caught signal %d, terminating...\n" , signal); +exit (EXIT_FAILURE ); +}int main (int argc,char **argv) { -int ret; +struct sigaction act = { + .sa_handler = handle_signal, + }; +enum verbs verb =TEST_SWAPON ; +void *p; +ssize_t sz; +int fd =-1 ; +int ret, c; + +memset (buf,0x58 ,BUF_SIZE ); + +while ((c =getopt (argc, argv,"v:" )) !=-1 ) { +switch (c) { +case 'v' : + verb =atoi (optarg); +if (verb <TEST_SWAPON || verb >=MAX_TEST_VERBS ) { +fprintf (stderr,"Verbs must be 0-%d.\n" , +MAX_TEST_VERBS -1 ); +usage (argv[0 ]); + } +break ; +default : +usage (argv[0 ]); +break ; + } + } -if (argc !=2 ) { -fprintf (stderr,"usage: %s PATH\n" , argv[0 ]); + ret =sigaction (SIGSEGV , &act,NULL ); +if (ret) { +perror ("sigsegv action" );return EXIT_FAILURE ; } - ret =swapon (argv[1 ],0 ); + ret =sigaction (SIGBUS , &act,NULL ); +if (ret) { +perror ("sigbus action" ); +return EXIT_FAILURE ; + } + +switch (verb) { +case TEST_WRITE : +case TEST_MWRITE_AFTER : +case TEST_MWRITE_BEFORE_AND_MWRITE_AFTER : +case TEST_MWRITE_BEFORE : + fd =open (argv[optind],O_RDWR ); +if (fd <0 ) { +perror (argv[optind]); +return EXIT_FAILURE ; + } +break ; +default : +break ; + } + +switch (verb) { +case TEST_MWRITE_BEFORE_AND_MWRITE_AFTER : +case TEST_MWRITE_BEFORE : + p =mmap (NULL ,BUF_SIZE ,PROT_WRITE |PROT_READ ,MAP_SHARED , + fd,65536 ); +if (p ==MAP_FAILED ) { +perror ("mmap" ); +return EXIT_FAILURE ; + } +memcpy (p, buf,BUF_SIZE ); +break ; +default : +break ; + } + +if (optind != argc -1 ) +usage (argv[0 ]); + + ret =swapon (argv[optind],0 );if (ret) {perror ("swapon" );return EXIT_FAILURE ; } +switch (verb) { +case TEST_WRITE : + sz =pwrite (fd, buf,BUF_SIZE ,65536 ); +if (sz <0 ) { +perror ("pwrite" ); +return EXIT_FAILURE ; + } +break ; +case TEST_MWRITE_AFTER : + p =mmap (NULL ,BUF_SIZE ,PROT_WRITE |PROT_READ ,MAP_SHARED , + fd,65536 ); +if (p ==MAP_FAILED ) { +perror ("mmap" ); +return EXIT_FAILURE ; + } +/* fall through */ +case TEST_MWRITE_BEFORE_AND_MWRITE_AFTER : +memcpy (p, buf,BUF_SIZE ); +break ; +default : +break ; + } + +if (fd >=0 ) { + ret =fsync (fd); +if (ret) +perror ("fsync" ); + ret =close (fd); +if (ret) +perror ("close" ); + } +return EXIT_SUCCESS ; }
tests/generic/717 new file +70 -0
70 lines not on this page · raw message
tests/generic/717.out new file +14 -0
@@ -0,0 +1,14 @@ +QA output created by 717 +verb 1 +pwrite: Text file busy +verb 2 +mmap: Text file busy +verb 3 +Caught signal 7, terminating... +verb 4 +pwrite: Text file busy +pwrite: Text file busy +mmap: Text file busy +no mapped regions, try 'help mmap' +ftruncate: Text file busy +fallocate: Text file busy
tests/generic/718 new file +55 -0
55 lines not on this page · raw message
tests/generic/718.out new file +12 -0
@@ -0,0 +1,12 @@ +QA output created by 718 +verb 1 +pwrite: Text file busy +verb 2 +mmap: Text file busy +verb 3 +Caught signal 7, terminating... +verb 4 +pwrite: Text file busy +pwrite: Text file busy +mmap: Text file busy +no mapped regions, try 'help mmap'
tests/generic/group +2 -0
@@ -570,3 +570,5 @@ 565 auto quick copy_range 715 auto quick rw 716 auto quick rw +717 auto quick rw swap +718 auto quick rw swap