- Prohibit writing to active swap files and swap partitions.
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEUzaAxoMeQq6m2jMV+H93GTRKtOsFAl1cDHQACgkQ+H93GTRK
 tOs1qw//aqXAQ7bpLDl7jx9CSuAighzKir0mHYFm9HUsnuRT6gLqIOVSeugoi8hY
 tYhPNzcKHL39YDa1QfKo1RKW6uCwsECHT/5TebLxBkTL3vGGAenPchAcjj89SV54
 lQ/h8O6hkDU+KCKC0kmDem7ma7DD5YZmWXDxW/HvnygjCnZ9BFaOeLQt/TPBmOmN
 lozPHcdrxhIuCuSTMjIZRq27Zl6uzj5tr+FkT+FWiYDrGhgWT7is6o397SEm7yYT
 3JqUQ+ZUOY4IwLlrWiVKqi0IqjvWqhaLzmjZaKF+YC8Ni0sdpaDdsE/uPSCyQ7k7
 28qbfypnu7bswakjcekdSX2Dj/SZivFb8AzlqaSIMVlw4STFzjMMYMLib8/OlPES
 z1pAjXHypLjNO3dNBYp/mRll+/BQ2NM6oCtnVVQGKVnlcx3oLo+n6JSRK8t74DTf
 BkYu93aybBpfE49Fb3VQum+9okg9BdShRxvUp023/WTUaa8aUyIbizn3iTrke/sx
 0bC+Vvdr33JZnoO8WKVzSd7COTHOTQ920NodTKAJ9bkF3WKyLM135ctavHrtdAg3
 FHBXpN7AjbOaLovLpiy3eb//ghKJwgyhqbN6VCGTudC/nkaXq7y7M3DPbXxQYxom
 yCA0qMByMg+cL4BtzS52QEda7xK1iiuQ/3jbdQ4lFuBHhwekVKs=
 =Ag/B
 -----END PGP SIGNATURE-----

Merge tag 'vfs-5.4-merge-1' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux

Pull swap access updates from Darrick Wong:
 "Prohibit writing to active swap files and swap partitions.

  There's no non-malicious use case for allowing userspace to scribble
  on storage that the kernel thinks it owns"

* tag 'vfs-5.4-merge-1' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux:
  vfs: don't allow writes to swap files
  mm: set S_SWAPFILE on blockdev swap devices
This commit is contained in:
Linus Torvalds 2019-09-18 17:35:20 -07:00
Родитель b6c0d35772 dc617f29db
Коммит e6bc9de714
6 изменённых файлов: 52 добавлений и 18 удалений

Просмотреть файл

@ -1972,6 +1972,9 @@ ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
if (bdev_read_only(I_BDEV(bd_inode))) if (bdev_read_only(I_BDEV(bd_inode)))
return -EPERM; return -EPERM;
if (IS_SWAPFILE(bd_inode))
return -ETXTBSY;
if (!iov_iter_count(from)) if (!iov_iter_count(from))
return 0; return 0;

Просмотреть файл

@ -3565,4 +3565,15 @@ static inline void simple_fill_fsxattr(struct fsxattr *fa, __u32 xflags)
fa->fsx_xflags = 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 */ #endif /* _LINUX_FS_H */

Просмотреть файл

@ -2988,6 +2988,9 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
loff_t count; loff_t count;
int ret; int ret;
if (IS_SWAPFILE(inode))
return -ETXTBSY;
if (!iov_iter_count(from)) if (!iov_iter_count(from))
return 0; return 0;

Просмотреть файл

@ -2196,6 +2196,10 @@ static vm_fault_t do_page_mkwrite(struct vm_fault *vmf)
vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; 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); ret = vmf->vma->vm_ops->page_mkwrite(vmf);
/* Restore original flags so that caller is not surprised */ /* Restore original flags so that caller is not surprised */
vmf->flags = old_flags; vmf->flags = old_flags;

Просмотреть файл

@ -1483,8 +1483,12 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
case MAP_SHARED_VALIDATE: case MAP_SHARED_VALIDATE:
if (flags & ~flags_mask) if (flags & ~flags_mask)
return -EOPNOTSUPP; return -EOPNOTSUPP;
if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE)) if (prot & PROT_WRITE) {
return -EACCES; 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 * Make sure we don't allow writing to an append-only

Просмотреть файл

@ -2368,9 +2368,8 @@ EXPORT_SYMBOL_GPL(add_swap_extent);
* requirements, they are simply tossed out - we will never use those blocks * requirements, they are simply tossed out - we will never use those blocks
* for swapping. * for swapping.
* *
* For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This * For all swap devices we set S_SWAPFILE across the life of the swapon. This
* prevents root from shooting her foot off by ftruncating an in-use swapfile, * prevents users from writing to the swap device, which will corrupt memory.
* which will scribble on the fs.
* *
* The amount of disk space which a single swap extent represents varies. * 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 * 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; inode = mapping->host;
if (S_ISBLK(inode->i_mode)) { if (S_ISBLK(inode->i_mode)) {
struct block_device *bdev = I_BDEV(inode); struct block_device *bdev = I_BDEV(inode);
set_blocksize(bdev, old_block_size); set_blocksize(bdev, old_block_size);
blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 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); 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; p->flags |= SWP_BLKDEV;
} else if (S_ISREG(inode->i_mode)) { } else if (S_ISREG(inode->i_mode)) {
p->bdev = inode->i_sb->s_bdev; p->bdev = inode->i_sb->s_bdev;
inode_lock(inode); }
if (IS_SWAPFILE(inode))
return -EBUSY; inode_lock(inode);
} else if (IS_SWAPFILE(inode))
return -EINVAL; return -EBUSY;
return 0; return 0;
} }
@ -3275,6 +3275,17 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
if (error) if (error)
goto bad_swap; 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); mutex_lock(&swapon_mutex);
prio = -1; prio = -1;
if (swap_flags & SWAP_FLAG_PREFER) if (swap_flags & SWAP_FLAG_PREFER)
@ -3295,8 +3306,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
atomic_inc(&proc_poll_event); atomic_inc(&proc_poll_event);
wake_up_interruptible(&proc_poll_wait); wake_up_interruptible(&proc_poll_wait);
if (S_ISREG(inode->i_mode))
inode->i_flags |= S_SWAPFILE;
error = 0; error = 0;
goto out; goto out;
bad_swap: bad_swap:
@ -3318,7 +3327,7 @@ bad_swap:
if (inced_nr_rotate_swap) if (inced_nr_rotate_swap)
atomic_dec(&nr_rotate_swap); atomic_dec(&nr_rotate_swap);
if (swap_file) { if (swap_file) {
if (inode && S_ISREG(inode->i_mode)) { if (inode) {
inode_unlock(inode); inode_unlock(inode);
inode = NULL; inode = NULL;
} }
@ -3331,7 +3340,7 @@ out:
} }
if (name) if (name)
putname(name); putname(name);
if (inode && S_ISREG(inode->i_mode)) if (inode)
inode_unlock(inode); inode_unlock(inode);
if (!error) if (!error)
enable_swap_slots_cache(); enable_swap_slots_cache();