History

PM: hibernate: Restrict writes to the resume device

Hibernation via snapshot device requires write permission to the swap block device, the one that more often (but not necessarily) is used to store the hibernation image. With this patch, such permissions are granted iff: 1) snapshot device config option is enabled 2) swap partition is used as resume device In other circumstances the swap device is not writable from userspace. In order to achieve this, every write attempt to a swap device is checked against the device configured as part of the uswsusp API [0] using a pointer to the inode struct in memory. If the swap device being written was not configured for resuming, the write request is denied. NOTE: this implementation works only for swap block devices, where the inode configured by swapon (which sets S_SWAPFILE) is the same used by SNAPSHOT_SET_SWAP_AREA. In case of swap file, SNAPSHOT_SET_SWAP_AREA indeed receives the inode of the block device containing the filesystem where the swap file is located (+ offset in it) which is never passed to swapon and then has not set S_SWAPFILE. As result, the swap file itself (as a file) has never an option to be written from userspace. Instead it remains writable if accessed directly from the containing block device, which is always writeable from root. [0] Documentation/power/userland-swsusp.rst v2: - rename is_hibernate_snapshot_dev() to is_hibernate_resume_dev() - fix description so to correctly refer to the resume device

Domenico Andreoli committed 6 years ago

Commit ad1e4f71 parent c4f39a63 files · +20 −3Browse files

Unified · Split

3 files
  1. fs/block_dev.c +1 −2
  2. include/linux/suspend.h +6 −0
  3. kernel/power/user.c +13 −1
fs/block_dev.c+1−2
@@ -2023,8 +2023,7 @@ ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from) · 2022 unmodified lines
2023 if (bdev_read_only(I_BDEV(bd_inode)))2023 if (bdev_read_only(I_BDEV(bd_inode)))
2024 return -EPERM;2024 return -EPERM;
2025 2025
2026 /* uswsusp needs write permission to the swap */2026 if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode))
2027 if (IS_SWAPFILE(bd_inode) && !hibernation_available())
2028 return -ETXTBSY;2027 return -ETXTBSY;
2029 2028
2030 if (!iov_iter_count(from))2029 if (!iov_iter_count(from))
include/linux/suspend.h+6−0
@@ -466,6 +466,12 @@ static inline bool system_entering_hibernation(void) { return false; } · 465 unmodified lines
466static inline bool hibernation_available(void) { return false; }466static inline bool hibernation_available(void) { return false; }
467#endif /* CONFIG_HIBERNATION */467#endif /* CONFIG_HIBERNATION */
468 468
469#ifdef CONFIG_HIBERNATION_SNAPSHOT_DEV
470int is_hibernate_resume_dev(const struct inode *);
471#else
472static inline int is_hibernate_resume_dev(const struct inode *i) { return 0; }
473#endif
474
469/* Hibernation and suspend events */475/* Hibernation and suspend events */
470#define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */476#define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
471#define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */477#define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
kernel/power/user.c+13−1
@@ -35,8 +35,14 @@ static struct snapshot_data { · 34 unmodified lines
35 bool ready;35 bool ready;
36 bool platform_support;36 bool platform_support;
37 bool free_bitmaps;37 bool free_bitmaps;
38 struct inode *bd_inode;
38} snapshot_state;39} snapshot_state;
39 40
41int is_hibernate_resume_dev(const struct inode *bd_inode)
42{
43 return hibernation_available() && snapshot_state.bd_inode == bd_inode;
44}
45
40static int snapshot_open(struct inode *inode, struct file *filp)46static int snapshot_open(struct inode *inode, struct file *filp)
41{47{
42 struct snapshot_data *data;48 struct snapshot_data *data;
@@ -95,6 +101,7 @@ static int snapshot_open(struct inode *inode, struct file *filp) · 52 unmodified lines
95 data->frozen = false;101 data->frozen = false;
96 data->ready = false;102 data->ready = false;
97 data->platform_support = false;103 data->platform_support = false;
104 data->bd_inode = NULL;
98 105
99 Unlock:106 Unlock:
100 unlock_system_sleep();107 unlock_system_sleep();
@@ -110,6 +117,7 @@ static int snapshot_release(struct inode *inode, struct file *filp) · 9 unmodified lines
110 117
111 swsusp_free();118 swsusp_free();
112 data = filp->private_data;119 data = filp->private_data;
120 data->bd_inode = NULL;
113 free_all_swap_pages(data->swap);121 free_all_swap_pages(data->swap);
114 if (data->frozen) {122 if (data->frozen) {
115 pm_restore_gfp_mask();123 pm_restore_gfp_mask();
@@ -202,6 +210,7 @@ struct compat_resume_swap_area { · 86 unmodified lines
202static int snapshot_set_swap_area(struct snapshot_data *data,210static int snapshot_set_swap_area(struct snapshot_data *data,
203 void __user *argp)211 void __user *argp)
204{212{
213 struct block_device *bdev;
205 sector_t offset;214 sector_t offset;
206 dev_t swdev;215 dev_t swdev;
207 216
@@ -232,9 +241,12 @@ static int snapshot_set_swap_area(struct snapshot_data *data, · 24 unmodified lines
232 data->swap = -1;241 data->swap = -1;
233 return -EINVAL;242 return -EINVAL;
234 }243 }
235 data->swap = swap_type_of(swdev, offset, NULL);244 data->swap = swap_type_of(swdev, offset, &bdev);
236 if (data->swap < 0)245 if (data->swap < 0)
237 return -ENODEV;246 return -ENODEV;
247
248 data->bd_inode = bdev->bd_inode;
249 bdput(bdev);
238 return 0;250 return 0;
239}251}
240 252