提交 af2457f4 编写于 作者: D Darrick J. Wong 提交者: Cheng Jian

mm: set S_SWAPFILE on blockdev swap devices

mainline inclusion
from mainline-5.4-rc1
commit 1638045c
category: bugfix
bugzilla: 50612
CVE: NA
---------------------------

Set S_SWAPFILE on block device inodes so that they have the same
protections as a swap flie.
Signed-off-by: NDarrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: NChristoph Hellwig <hch@lst.de>
Signed-off-by: Nzhangyi (F) <yi.zhang@huawei.com>
Reviewed-by: Nyangerkun <yangerkun@huawei.com>
Signed-off-by: NYang Yingliang <yangyingliang@huawei.com>
Signed-off-by: NCheng Jian <cj.chengjian@huawei.com>
上级 c6d8e7da
...@@ -2474,9 +2474,8 @@ add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, ...@@ -2474,9 +2474,8 @@ add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
* 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
...@@ -2767,13 +2766,14 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) ...@@ -2767,13 +2766,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);
/* /*
...@@ -2999,11 +2999,11 @@ static int claim_swapfile(struct swap_info_struct *p, struct inode *inode) ...@@ -2999,11 +2999,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;
} }
...@@ -3404,8 +3404,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) ...@@ -3404,8 +3404,7 @@ 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;
inode->i_flags |= S_SWAPFILE;
error = 0; error = 0;
goto out; goto out;
bad_swap: bad_swap:
...@@ -3427,7 +3426,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) ...@@ -3427,7 +3426,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
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;
} }
...@@ -3440,7 +3439,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) ...@@ -3440,7 +3439,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
} }
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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册