PM: hibernate: Restrict writes to the resume device
authorDomenico Andreoli <domenico.andreoli@linux.com>
Tue, 19 May 2020 18:14:10 +0000 (20:14 +0200)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Wed, 27 May 2020 15:55:59 +0000 (17:55 +0200)
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

Signed-off-by: Domenico Andreoli <domenico.andreoli@linux.com>
Acked-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
fs/block_dev.c
include/linux/suspend.h
kernel/power/user.c

index 93672c3f1c78c886eaeffaff6746bf2ee2937d99..608a7b9173f4086166194e3519bc240c7359d0c6 100644 (file)
@@ -2023,8 +2023,7 @@ ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
        if (bdev_read_only(I_BDEV(bd_inode)))
                return -EPERM;
 
-       /* uswsusp needs write permission to the swap */
-       if (IS_SWAPFILE(bd_inode) && !hibernation_available())
+       if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode))
                return -ETXTBSY;
 
        if (!iov_iter_count(from))
index 4fcc6fd0cbd63eb1955a7a82dc2a78ae1da3cad5..b960098acfb0de04d925c56e1a69032aa68cd2d6 100644 (file)
@@ -466,6 +466,12 @@ static inline bool system_entering_hibernation(void) { return false; }
 static inline bool hibernation_available(void) { return false; }
 #endif /* CONFIG_HIBERNATION */
 
+#ifdef CONFIG_HIBERNATION_SNAPSHOT_DEV
+int is_hibernate_resume_dev(const struct inode *);
+#else
+static inline int is_hibernate_resume_dev(const struct inode *i) { return 0; }
+#endif
+
 /* Hibernation and suspend events */
 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
 #define PM_POST_HIBERNATION    0x0002 /* Hibernation finished */
index 98548d1cf8a659122998fc10fa56f2229452279d..d5eedc2baa2a10afb2382644f58106e35de0a103 100644 (file)
@@ -35,8 +35,14 @@ static struct snapshot_data {
        bool ready;
        bool platform_support;
        bool free_bitmaps;
+       struct inode *bd_inode;
 } snapshot_state;
 
+int is_hibernate_resume_dev(const struct inode *bd_inode)
+{
+       return hibernation_available() && snapshot_state.bd_inode == bd_inode;
+}
+
 static int snapshot_open(struct inode *inode, struct file *filp)
 {
        struct snapshot_data *data;
@@ -95,6 +101,7 @@ static int snapshot_open(struct inode *inode, struct file *filp)
        data->frozen = false;
        data->ready = false;
        data->platform_support = false;
+       data->bd_inode = NULL;
 
  Unlock:
        unlock_system_sleep();
@@ -110,6 +117,7 @@ static int snapshot_release(struct inode *inode, struct file *filp)
 
        swsusp_free();
        data = filp->private_data;
+       data->bd_inode = NULL;
        free_all_swap_pages(data->swap);
        if (data->frozen) {
                pm_restore_gfp_mask();
@@ -202,6 +210,7 @@ struct compat_resume_swap_area {
 static int snapshot_set_swap_area(struct snapshot_data *data,
                void __user *argp)
 {
+       struct block_device *bdev;
        sector_t offset;
        dev_t swdev;
 
@@ -232,9 +241,12 @@ static int snapshot_set_swap_area(struct snapshot_data *data,
                data->swap = -1;
                return -EINVAL;
        }
-       data->swap = swap_type_of(swdev, offset, NULL);
+       data->swap = swap_type_of(swdev, offset, &bdev);
        if (data->swap < 0)
                return -ENODEV;
+
+       data->bd_inode = bdev->bd_inode;
+       bdput(bdev);
        return 0;
 }