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

Showing the changes to kernel (1 of 3 files). Show all

Unified · Split

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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t