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
3535 bool ready;
3636 bool platform_support;
3737 bool free_bitmaps;
38+ struct inode *bd_inode;
3839 } snapshot_state;
3940
41+int is_hibernate_resume_dev(const struct inode *bd_inode)
42+{
43+ return hibernation_available() && snapshot_state.bd_inode == bd_inode;
44+}
45+
4046 static int snapshot_open(struct inode *inode, struct file *filp)
4147 {
4248 struct snapshot_data *data;
@@ -95,6 +101,7 @@ static int snapshot_open(struct inode *inode, struct file *filp) · 52 unmodified lines
95101 data->frozen = false;
96102 data->ready = false;
97103 data->platform_support = false;
104+ data->bd_inode = NULL;
98105
99106 Unlock:
100107 unlock_system_sleep();
@@ -110,6 +117,7 @@ static int snapshot_release(struct inode *inode, struct file *filp) · 9 unmodified lines
110117
111118 swsusp_free();
112119 data = filp->private_data;
120+ data->bd_inode = NULL;
113121 free_all_swap_pages(data->swap);
114122 if (data->frozen) {
115123 pm_restore_gfp_mask();
@@ -202,6 +210,7 @@ struct compat_resume_swap_area { · 86 unmodified lines
202210 static int snapshot_set_swap_area(struct snapshot_data *data,
203211 void __user *argp)
204212 {
213+ struct block_device *bdev;
205214 sector_t offset;
206215 dev_t swdev;
207216
@@ -232,9 +241,12 @@ static int snapshot_set_swap_area(struct snapshot_data *data, · 24 unmodified lines
232241 data->swap = -1;
233242 return -EINVAL;
234243 }
235− data->swap = swap_type_of(swdev, offset, NULL);
244+ data->swap = swap_type_of(swdev, offset, &bdev);
236245 if (data->swap < 0)
237246 return -ENODEV;
247+
248+ data->bd_inode = bdev->bd_inode;
249+ bdput(bdev);
238250 return 0;
239251 }
240252

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