Skip to contentSkip to conversation

hibernate: unlock swap bdev for writing when uswsusp is active

PATCH In review Darrick J. Wong 5 participants 16 messages Feb 29, 2020 – Apr 21, 2020

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hibernate: unlock swap bdev for writing when uswsusp is active

From: Darrick J. Wong <[email protected]>

It turns out that there /is/ one use case for programs being able to write to swap devices, and that is the userspace hibernation code. The uswsusp ioctls allow userspace to lease parts of swap devices, so turn S_SWAPFILE off when invoking suspend.

Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices")
---
 include/linux/swap.h |    1 +
 kernel/power/user.c  |   11 ++++++++++-
 mm/swapfile.c        |   26 ++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 1 deletion(-)
include/linux/swap.h +1 -0
index 1e99f7ac1d7e..add93e205850 100644
@@ -458,6 +458,7 @@ extern void swap_free(swp_entry_t);
 extern void swapcache_free_entries(swp_entry_t *entries, int n);
 extern int free_swap_and_cache(swp_entry_t);
 extern int swap_type_of(dev_t, sector_t, struct block_device **);
+extern void swap_relockall(void);
 extern unsigned int count_swap_pages(int, int);
 extern sector_t map_swap_page(struct page *, struct block_device **);
 extern sector_t swapdev_block(int, pgoff_t);
kernel/power/user.c +10 -1
index 77438954cc2b..b11f7037ce5e 100644
@@ -271,6 +271,8 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 			break;
 		}
 		error = hibernation_restore(data->platform_support);
+		if (!error)
+			swap_relockall();
 		break;
 
 	case SNAPSHOT_FREE:
@@ -372,10 +374,17 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 			 */
 			swdev = new_decode_dev(swap_area.dev);
 			if (swdev) {
+				struct block_device *bd;
+
 				offset = swap_area.offset;
-				data->swap = swap_type_of(swdev, offset, NULL);
+				data->swap = swap_type_of(swdev, offset, &bd);
 				if (data->swap < 0)
 					error = -ENODEV;
+
+				inode_lock(bd->bd_inode);
+				bd->bd_inode->i_flags &= ~S_SWAPFILE;
+				inode_unlock(bd->bd_inode);
+				bdput(bd);
 			} else {
 				data->swap = -1;
 				error = -EINVAL;
mm/swapfile.c +26 -0
index b2a2e45c9a36..439bfb7263d3 100644
@@ -1799,6 +1799,32 @@ int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
 	return -ENODEV;
 }
 
+/* Re-lock swap devices after resuming from userspace suspend. */
+void swap_relockall(void)
+{
+	int type;
+
+	spin_lock(&swap_lock);
+	for (type = 0; type < nr_swapfiles; type++) {
+		struct swap_info_struct *sis = swap_info[type];
+		struct block_device *bdev = bdgrab(sis->bdev);
+
+		/*
+		 * uswsusp only knows how to suspend to block devices, so we
+		 * can skip swap files.
+		 */
+		if (!(sis->flags & SWP_WRITEOK) ||
+		    !(sis->flags & SWP_BLKDEV))
+			continue;
+
+		inode_lock(bdev->bd_inode);
+		bdev->bd_inode->i_flags |= S_SWAPFILE;
+		inode_unlock(bdev->bd_inode);
+		bdput(bdev);
+	}
+	spin_unlock(&swap_lock);
+}
+
 /*
  * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
  * corresponding to given index in swap_info (swap type).
#1 lore raw
Quoted 10 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

It turns out that there /is/ one use case for programs being able to write to swap devices, and that is the userspace hibernation code. The uswsusp ioctls allow userspace to lease parts of swap devices, so turn S_SWAPFILE off when invoking suspend.

Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices")

I also tested it yesterday but was not satisfied, unfortunately I did not come with my comment in time.

Yes, I confirm that the uswsusp works again but also checked that swap_relockall() is not triggered at all and therefore after the first hibernation cycle the S_SWAPFILE bit remains cleared and the whole swap_relockall() is useless.

I'm not sure this patch should be merged in the current form.

Regards,
Domenico

Quoted 89 lines
---
 include/linux/swap.h |    1 +
 kernel/power/user.c  |   11 ++++++++++-
 mm/swapfile.c        |   26 ++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 1 deletion(-)
include/linux/swap.h +1 -0
index 1e99f7ac1d7e..add93e205850 100644
@@ -458,6 +458,7 @@ extern void swap_free(swp_entry_t);
 extern void swapcache_free_entries(swp_entry_t *entries, int n);
 extern int free_swap_and_cache(swp_entry_t);
 extern int swap_type_of(dev_t, sector_t, struct block_device **);
+extern void swap_relockall(void);
 extern unsigned int count_swap_pages(int, int);
 extern sector_t map_swap_page(struct page *, struct block_device **);
 extern sector_t swapdev_block(int, pgoff_t);
kernel/power/user.c +10 -1
index 77438954cc2b..b11f7037ce5e 100644
@@ -271,6 +271,8 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 			break;
 		}
 		error = hibernation_restore(data->platform_support);
+		if (!error)
+			swap_relockall();
 		break;
 
 	case SNAPSHOT_FREE:
@@ -372,10 +374,17 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 			 */
 			swdev = new_decode_dev(swap_area.dev);
 			if (swdev) {
+				struct block_device *bd;
+
 				offset = swap_area.offset;
-				data->swap = swap_type_of(swdev, offset, NULL);
+				data->swap = swap_type_of(swdev, offset, &bd);
 				if (data->swap < 0)
 					error = -ENODEV;
+
+				inode_lock(bd->bd_inode);
+				bd->bd_inode->i_flags &= ~S_SWAPFILE;
+				inode_unlock(bd->bd_inode);
+				bdput(bd);
 			} else {
 				data->swap = -1;
 				error = -EINVAL;
mm/swapfile.c +26 -0
index b2a2e45c9a36..439bfb7263d3 100644
@@ -1799,6 +1799,32 @@ int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
 	return -ENODEV;
 }
 
+/* Re-lock swap devices after resuming from userspace suspend. */
+void swap_relockall(void)
+{
+	int type;
+
+	spin_lock(&swap_lock);
+	for (type = 0; type < nr_swapfiles; type++) {
+		struct swap_info_struct *sis = swap_info[type];
+		struct block_device *bdev = bdgrab(sis->bdev);
+
+		/*
+		 * uswsusp only knows how to suspend to block devices, so we
+		 * can skip swap files.
+		 */
+		if (!(sis->flags & SWP_WRITEOK) ||
+		    !(sis->flags & SWP_BLKDEV))
+			continue;
+
+		inode_lock(bdev->bd_inode);
+		bdev->bd_inode->i_flags |= S_SWAPFILE;
+		inode_unlock(bdev->bd_inode);
+		bdput(bdev);
+	}
+	spin_unlock(&swap_lock);
+}
+
 /*
  * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
  * corresponding to given index in swap_info (swap type).
Signature · 2 lines
rsa4096: 3B10 0CA1 8674 ACBA B4FE  FCD2 CE5B CF17 9960 DE13
ed25519: FFB4 0CC3 7F2E 091D F7DA  356E CC79 2832 ED38 CB05
#2 lore raw
Quoted 15 lines · Domenico Andreoli
Quoted 10 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

It turns out that there /is/ one use case for programs being able to write to swap devices, and that is the userspace hibernation code. The uswsusp ioctls allow userspace to lease parts of swap devices, so turn S_SWAPFILE off when invoking suspend.

Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices")

I also tested it yesterday but was not satisfied, unfortunately I did not come with my comment in time.

Quoted 6 lines

Yes, I confirm that the uswsusp works again but also checked that swap_relockall() is not triggered at all and therefore after the first hibernation cycle the S_SWAPFILE bit remains cleared and the whole swap_relockall() is useless.

I'm not sure this patch should be merged in the current form.

NNGGHHGGHGH /me is rapidly losing his sanity and will soon just revert the whole security feature because I'm getting fed up with people yelling at me *while I'm on vacation* trying to *restore* my sanity. I really don't want to be QAing userspace-directed hibernation right now.

...right, the patch is broken because we have to relock the swapfiles in whatever code executes after we jump back to the restored kernel, not in the one that's doing the restoring. Does this help?

OTOH, maybe we should just leave the swapfiles unlocked after resume. Userspace has clearly demonstrated the one usecase for writing to the swapfile, which means anyone could have jumped in while uswsusp was running and written whatever crap they wanted to the parts of the swap file that weren't leased for the hibernate image.

--D

From: Darrick J. Wong <[email protected]>
Subject: [PATCH] hibernate: unlock swap bdev for writing when uswsusp is active

It turns out that there /is/ one use case for programs being able to write to swap devices, and that is the userspace hibernation code. The uswsusp ioctls allow userspace to lease parts of swap devices, so turn S_SWAPFILE off when invoking suspend.

Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices")
---
 include/linux/swap.h     |    1 +
 kernel/power/hibernate.c |    4 ++++
 kernel/power/user.c      |    9 ++++++++-
 mm/swapfile.c            |   26 ++++++++++++++++++++++++++
 4 files changed, 39 insertions(+), 1 deletion(-)
include/linux/swap.h +1 -0
index 1e99f7ac1d7e..add93e205850 100644
@@ -458,6 +458,7 @@ extern void swap_free(swp_entry_t);
 extern void swapcache_free_entries(swp_entry_t *entries, int n);
 extern int free_swap_and_cache(swp_entry_t);
 extern int swap_type_of(dev_t, sector_t, struct block_device **);
+extern void swap_relockall(void);
 extern unsigned int count_swap_pages(int, int);
 extern sector_t map_swap_page(struct page *, struct block_device **);
 extern sector_t swapdev_block(int, pgoff_t);
kernel/power/hibernate.c +4 -0
index 6dbeedb7354c..aa5a6701614d 100644
@@ -31,6 +31,7 @@
 #include <linux/genhd.h>
 #include <linux/ktime.h>
 #include <linux/security.h>
+#include <linux/swap.h>
 #include <trace/events/power.h>
 
 #include "power.h"
@@ -399,6 +400,9 @@ int hibernation_snapshot(int platform_mode)
 	 * image creation has failed and (2) after a successful restore.
 	 */
 
+	/* Lock the swap files, just in case uswsusp was active. */
+	swap_relockall();
+
 	/* We may need to release the preallocated image pages here. */
 	if (error || !in_suspend)
 		swsusp_free();
kernel/power/user.c +8 -1
index 77438954cc2b..a3ae9cbbfcf0 100644
@@ -372,10 +372,17 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
 			 */
 			swdev = new_decode_dev(swap_area.dev);
 			if (swdev) {
+				struct block_device *bd;
+
 				offset = swap_area.offset;
-				data->swap = swap_type_of(swdev, offset, NULL);
+				data->swap = swap_type_of(swdev, offset, &bd);
 				if (data->swap < 0)
 					error = -ENODEV;
+
+				inode_lock(bd->bd_inode);
+				bd->bd_inode->i_flags &= ~S_SWAPFILE;
+				inode_unlock(bd->bd_inode);
+				bdput(bd);
 			} else {
 				data->swap = -1;
 				error = -EINVAL;
mm/swapfile.c +26 -0
index b2a2e45c9a36..439bfb7263d3 100644
@@ -1799,6 +1799,32 @@ int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
 	return -ENODEV;
 }
 
+/* Re-lock swap devices after resuming from userspace suspend. */
+void swap_relockall(void)
+{
+	int type;
+
+	spin_lock(&swap_lock);
+	for (type = 0; type < nr_swapfiles; type++) {
+		struct swap_info_struct *sis = swap_info[type];
+		struct block_device *bdev = bdgrab(sis->bdev);
+
+		/*
+		 * uswsusp only knows how to suspend to block devices, so we
+		 * can skip swap files.
+		 */
+		if (!(sis->flags & SWP_WRITEOK) ||
+		    !(sis->flags & SWP_BLKDEV))
+			continue;
+
+		inode_lock(bdev->bd_inode);
+		bdev->bd_inode->i_flags |= S_SWAPFILE;
+		inode_unlock(bdev->bd_inode);
+		bdput(bdev);
+	}
+	spin_unlock(&swap_lock);
+}
+
 /*
  * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
  * corresponding to given index in swap_info (swap type).
#3 lore raw
Quoted 21 lines · Darrick J. Wong
Quoted 20 lines · Domenico Andreoli
Quoted 10 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

It turns out that there /is/ one use case for programs being able to write to swap devices, and that is the userspace hibernation code. The uswsusp ioctls allow userspace to lease parts of swap devices, so turn S_SWAPFILE off when invoking suspend.

Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices")

I also tested it yesterday but was not satisfied, unfortunately I did not come with my comment in time.

Yes, I confirm that the uswsusp works again but also checked that swap_relockall() is not triggered at all and therefore after the first hibernation cycle the S_SWAPFILE bit remains cleared and the whole swap_relockall() is useless.

Quoted 6 lines
Quoted 1 line

I'm not sure this patch should be merged in the current form.

NNGGHHGGHGH /me is rapidly losing his sanity and will soon just revert the whole security feature because I'm getting fed up with people yelling at me *while I'm on vacation* trying to *restore* my sanity. I really don't want to be QAing userspace-directed hibernation right now.

Maybe we could proceed with the first patch to amend the regression and postpone the improved fix to a later patch? Don't loose sanity for this.

Quoted 3 lines

...right, the patch is broken because we have to relock the swapfiles in whatever code executes after we jump back to the restored kernel, not in the one that's doing the restoring. Does this help?

I made a few unsuccessful attempts in kernel/power/hibernate.c and eventually I'm switching to qemu to speed up the test cycle.

Quoted 5 lines

OTOH, maybe we should just leave the swapfiles unlocked after resume. Userspace has clearly demonstrated the one usecase for writing to the swapfile, which means anyone could have jumped in while uswsusp was running and written whatever crap they wanted to the parts of the swap file that weren't leased for the hibernate image.

Essentially, if the hibernation is supported the swapfile is not totally safe. Maybe user-space hibernation should be a separate option.

Quoted 2 lines

--D

Signature · 2 lines
rsa4096: 3B10 0CA1 8674 ACBA B4FE  FCD2 CE5B CF17 9960 DE13
ed25519: FFB4 0CC3 7F2E 091D F7DA  356E CC79 2832 ED38 CB05
#4 lore raw
Quoted 25 lines · Domenico Andreoli
Quoted 23 lines · Darrick J. Wong
Quoted 21 lines · Domenico Andreoli
Quoted 10 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

It turns out that there /is/ one use case for programs being able to write to swap devices, and that is the userspace hibernation code. The uswsusp ioctls allow userspace to lease parts of swap devices, so turn S_SWAPFILE off when invoking suspend.

Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices")

I also tested it yesterday but was not satisfied, unfortunately I did not come with my comment in time.

Yes, I confirm that the uswsusp works again but also checked that swap_relockall() is not triggered at all and therefore after the first hibernation cycle the S_SWAPFILE bit remains cleared and the whole swap_relockall() is useless.

I'm not sure this patch should be merged in the current form.

Quoted 7 lines
Quoted 4 lines

NNGGHHGGHGH /me is rapidly losing his sanity and will soon just revert the whole security feature because I'm getting fed up with people yelling at me *while I'm on vacation* trying to *restore* my sanity. I really don't want to be QAing userspace-directed hibernation right now.

Maybe we could proceed with the first patch to amend the regression and postpone the improved fix to a later patch? Don't loose sanity for this.

I would concur here.

Quoted 7 lines
Quoted 3 lines

...right, the patch is broken because we have to relock the swapfiles in whatever code executes after we jump back to the restored kernel, not in the one that's doing the restoring. Does this help?

I made a few unsuccessful attempts in kernel/power/hibernate.c and eventually I'm switching to qemu to speed up the test cycle.

Quoted 8 lines
Quoted 5 lines

OTOH, maybe we should just leave the swapfiles unlocked after resume. Userspace has clearly demonstrated the one usecase for writing to the swapfile, which means anyone could have jumped in while uswsusp was running and written whatever crap they wanted to the parts of the swap file that weren't leased for the hibernate image.

Essentially, if the hibernation is supported the swapfile is not totally safe.

But that's only the case with the userspace variant, isn't it?

Quoted 1 line

Maybe user-space hibernation should be a separate option.

That actually is not a bad idea at all in my view.

Thanks!

#5 lore raw

Hi Darrick

If security is a concern, maybe it should in kernel config ( CONFIG_ENABLE_HIBERNATION = Y/N )
For the security hardened server systems with no hibernation need you simply configure it to N.

Also the concern the other process can hijack s2disk is unlikely. You have to realized all processors except for one
are down (See dmesg bellow.) by the time the snapshot image is being written. So you can disable scheduler time sharing on this only one processor when you get snapshot request and no other process can jump in.

I think you can allow (unlock) writing ONLY to device specified in kernel parameter
resume=/dev/<swap_device> and
only when CONFIG_ENABLE_HIBERNATION = Y and there is only one CPU active (CPU0) and
time sharing scheduler is down and user group from another kernel parameter snapshot_gid invoked snapshot.
For me secure enough. If any rogue program pretended to be legitimate user space hibernation
program it would have to go via actual hibernation cycle (powering off computer) and that would be obvious to user if that was not triggered by him
or configured by him to trigger automatically.
It is no way a program secretly could write to resume/swap device.

For normal users often the security is less of concern as they know who works with their laptop , etc.

[ 1243.100159] Disabling non-boot CPUs ...
[ 1243.101448] smpboot: CPU 1 is now offline
[ 1243.103291] smpboot: CPU 2 is now offline
[ 1243.104851] smpboot: CPU 3 is now offline
[ 1243.106522] smpboot: CPU 4 is now offline
[ 1243.108200] smpboot: CPU 5 is now offline
[ 1243.109928] smpboot: CPU 6 is now offline
[ 1243.111501] smpboot: CPU 7 is now offline
[ 1243.113364] PM: Creating hibernation image:
[ 1243.597752] PM: Need to copy 161991 pages
[ 1243.597756] PM: Normal pages needed: 161991 + 1024, available pages: 3967507
[ 1244.202907] PM: Hibernation image created (161991 pages copied)
Quoted 61 lines · Rafael J. Wysocki
Quoted 25 lines · Domenico Andreoli
Quoted 23 lines · Darrick J. Wong
Quoted 21 lines · Domenico Andreoli
Quoted 10 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

It turns out that there /is/ one use case for programs being able to write to swap devices, and that is the userspace hibernation code. The uswsusp ioctls allow userspace to lease parts of swap devices, so turn S_SWAPFILE off when invoking suspend.

Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices")

I also tested it yesterday but was not satisfied, unfortunately I did not come with my comment in time.

Yes, I confirm that the uswsusp works again but also checked that swap_relockall() is not triggered at all and therefore after the first hibernation cycle the S_SWAPFILE bit remains cleared and the whole swap_relockall() is useless.

I'm not sure this patch should be merged in the current form.

Quoted 7 lines
Quoted 4 lines

NNGGHHGGHGH /me is rapidly losing his sanity and will soon just revert the whole security feature because I'm getting fed up with people yelling at me *while I'm on vacation* trying to *restore* my sanity. I really don't want to be QAing userspace-directed hibernation right now.

Maybe we could proceed with the first patch to amend the regression and postpone the improved fix to a later patch? Don't loose sanity for this.

I would concur here.

Quoted 7 lines
Quoted 3 lines

...right, the patch is broken because we have to relock the swapfiles in whatever code executes after we jump back to the restored kernel, not in the one that's doing the restoring. Does this help?

I made a few unsuccessful attempts in kernel/power/hibernate.c and eventually I'm switching to qemu to speed up the test cycle.

Quoted 8 lines
Quoted 5 lines

OTOH, maybe we should just leave the swapfiles unlocked after resume. Userspace has clearly demonstrated the one usecase for writing to the swapfile, which means anyone could have jumped in while uswsusp was running and written whatever crap they wanted to the parts of the swap file that weren't leased for the hibernate image.

Essentially, if the hibernation is supported the swapfile is not totally safe.

But that's only the case with the userspace variant, isn't it?

Quoted 1 line

Maybe user-space hibernation should be a separate option.

That actually is not a bad idea at all in my view.

Thanks!

#6 lore raw
In reply to Rafael J. Wysocki
Quoted 50 lines · Rafael J. Wysocki
Quoted 25 lines · Domenico Andreoli
Quoted 23 lines · Darrick J. Wong
Quoted 21 lines · Domenico Andreoli
Quoted 10 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

It turns out that there /is/ one use case for programs being able to write to swap devices, and that is the userspace hibernation code. The uswsusp ioctls allow userspace to lease parts of swap devices, so turn S_SWAPFILE off when invoking suspend.

Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices")

I also tested it yesterday but was not satisfied, unfortunately I did not come with my comment in time.

Yes, I confirm that the uswsusp works again but also checked that swap_relockall() is not triggered at all and therefore after the first hibernation cycle the S_SWAPFILE bit remains cleared and the whole swap_relockall() is useless.

I'm not sure this patch should be merged in the current form.

Quoted 7 lines
Quoted 4 lines

NNGGHHGGHGH /me is rapidly losing his sanity and will soon just revert the whole security feature because I'm getting fed up with people yelling at me *while I'm on vacation* trying to *restore* my sanity. I really don't want to be QAing userspace-directed hibernation right now.

Maybe we could proceed with the first patch to amend the regression and postpone the improved fix to a later patch? Don't loose sanity for this.

I would concur here.

Quoted 13 lines
Quoted 3 lines

...right, the patch is broken because we have to relock the swapfiles in whatever code executes after we jump back to the restored kernel, not in the one that's doing the restoring. Does this help?

I made a few unsuccessful attempts in kernel/power/hibernate.c and eventually I'm switching to qemu to speed up the test cycle.

Quoted 5 lines

OTOH, maybe we should just leave the swapfiles unlocked after resume. Userspace has clearly demonstrated the one usecase for writing to the swapfile, which means anyone could have jumped in while uswsusp was running and written whatever crap they wanted to the parts of the swap file that weren't leased for the hibernate image.

Quoted 4 lines
Quoted 2 lines

Essentially, if the hibernation is supported the swapfile is not totally safe.

But that's only the case with the userspace variant, isn't it?

Yes.

Quoted 3 lines
Quoted 1 line

Maybe user-space hibernation should be a separate option.

That actually is not a bad idea at all in my view.

The trouble with kconfig options is that the distros will be pressued into setting CONFIG_HIBERNATE_USERSPACE=y to avoid regressing their uswsusp users, which makes the added security code pointless. As this has clearly sucked me into a conflict that I don't have the resources to pursue, I'm going to revert the write patch checks and move on with life.

--D

Quoted 1 line

Thanks!

#7 lore raw
Quoted 80 lines · Darrick J. Wong
Quoted 5 lines · Rafael J. Wysocki
Quoted 3 lines · Domenico Andreoli
Quoted 1 line · Darrick J. Wong

On Sat, Feb 29, 2020 at 07:07:16PM +0100, Domenico Andreoli

Quoted 1 line · wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line

On Sat, Feb 29, 2020 at 09:08:25AM -0800, Darrick J. Wong

Quoted 3 lines · wrote:
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

From: Darrick J. Wong <[email protected]>

It turns out that there /is/ one use case for programs being

able to

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

write to swap devices, and that is the userspace hibernation

code. The

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

uswsusp ioctls allow userspace to lease parts of swap

devices, so turn

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

S_SWAPFILE off when invoking suspend.

Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap

devices")

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

I also tested it yesterday but was not satisfied, unfortunately

I did

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

not come with my comment in time.

Yes, I confirm that the uswsusp works again but also checked

that

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

swap_relockall() is not triggered at all and therefore after

the first

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

hibernation cycle the S_SWAPFILE bit remains cleared and the

whole

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

swap_relockall() is useless.

I'm not sure this patch should be merged in the current form.

NNGGHHGGHGH /me is rapidly losing his sanity and will soon just

revert

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

the whole security feature because I'm getting fed up with people yelling at me *while I'm on vacation* trying to *restore* my

sanity. I

Quoted 1 line
Quoted 1 line
Quoted 1 line

really don't want to be QAing userspace-directed hibernation

right now.

Quoted 2 lines
Quoted 2 lines

Maybe we could proceed with the first patch to amend the regression

and

Quoted 1 line
Quoted 1 line

postpone the improved fix to a later patch? Don't loose sanity for

this.

Quoted 4 lines

I would concur here.

Quoted 1 line
Quoted 1 line

...right, the patch is broken because we have to relock the

swapfiles in

Quoted 1 line
Quoted 1 line
Quoted 1 line

whatever code executes after we jump back to the restored kernel,

not in

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

the one that's doing the restoring. Does this help?

I made a few unsuccessful attempts in kernel/power/hibernate.c and eventually I'm switching to qemu to speed up the test cycle.

Quoted 1 line

OTOH, maybe we should just leave the swapfiles unlocked after

resume.

Quoted 1 line
Quoted 1 line
Quoted 1 line

Userspace has clearly demonstrated the one usecase for writing to

the

Quoted 1 line
Quoted 1 line
Quoted 1 line

swapfile, which means anyone could have jumped in while uswsusp

was

Quoted 1 line
Quoted 1 line
Quoted 1 line

running and written whatever crap they wanted to the parts of the

swap

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

file that weren't leased for the hibernate image.

Essentially, if the hibernation is supported the swapfile is not

totally

Quoted 3 lines
Quoted 1 line

safe.

But that's only the case with the userspace variant, isn't it?

Yes.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Maybe user-space hibernation should be a separate option.

That actually is not a bad idea at all in my view.

The trouble with kconfig options is that the distros will be pressued into setting CONFIG_HIBERNATE_USERSPACE=y to avoid regressing their uswsusp users, which makes the added security code pointless. As this

True but there are not only distros otherwise the kernel would not have any option at all.

It's actually very nice that if hibernation is disabled no userspace is ever allowed to write to the swap.

Quoted 4 lines

has clearly sucked me into a conflict that I don't have the resources to
pursue, I'm going to revert the write patch checks and move on with life.

I don't see the need of reverting anything, I can deal with these issues if you are busy on something else.

Quoted 4 lines

--D

Quoted 1 line

Thanks!

#8 lore raw
Quoted 97 lines · Domenico Andreoli
Quoted 80 lines · Darrick J. Wong
Quoted 5 lines · Rafael J. Wysocki
Quoted 3 lines · Domenico Andreoli
Quoted 1 line · Darrick J. Wong

On Sat, Feb 29, 2020 at 07:07:16PM +0100, Domenico Andreoli

Quoted 1 line · wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line

On Sat, Feb 29, 2020 at 09:08:25AM -0800, Darrick J. Wong

Quoted 3 lines · wrote:
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

From: Darrick J. Wong <[email protected]>

It turns out that there /is/ one use case for programs being

able to

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

write to swap devices, and that is the userspace hibernation

code. The

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

uswsusp ioctls allow userspace to lease parts of swap

devices, so turn

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

S_SWAPFILE off when invoking suspend.

Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap

devices")

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

I also tested it yesterday but was not satisfied, unfortunately

I did

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

not come with my comment in time.

Yes, I confirm that the uswsusp works again but also checked

that

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

swap_relockall() is not triggered at all and therefore after

the first

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

hibernation cycle the S_SWAPFILE bit remains cleared and the

whole

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

swap_relockall() is useless.

I'm not sure this patch should be merged in the current form.

NNGGHHGGHGH /me is rapidly losing his sanity and will soon just

revert

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

the whole security feature because I'm getting fed up with people yelling at me *while I'm on vacation* trying to *restore* my

sanity. I

Quoted 1 line
Quoted 1 line
Quoted 1 line

really don't want to be QAing userspace-directed hibernation

right now.

Quoted 2 lines
Quoted 2 lines

Maybe we could proceed with the first patch to amend the regression

and

Quoted 1 line
Quoted 1 line

postpone the improved fix to a later patch? Don't loose sanity for

this.

Quoted 4 lines

I would concur here.

Quoted 1 line
Quoted 1 line

...right, the patch is broken because we have to relock the

swapfiles in

Quoted 1 line
Quoted 1 line
Quoted 1 line

whatever code executes after we jump back to the restored kernel,

not in

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

the one that's doing the restoring. Does this help?

I made a few unsuccessful attempts in kernel/power/hibernate.c and eventually I'm switching to qemu to speed up the test cycle.

Quoted 1 line

OTOH, maybe we should just leave the swapfiles unlocked after

resume.

Quoted 1 line
Quoted 1 line
Quoted 1 line

Userspace has clearly demonstrated the one usecase for writing to

the

Quoted 1 line
Quoted 1 line
Quoted 1 line

swapfile, which means anyone could have jumped in while uswsusp

was

Quoted 1 line
Quoted 1 line
Quoted 1 line

running and written whatever crap they wanted to the parts of the

swap

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

file that weren't leased for the hibernate image.

Essentially, if the hibernation is supported the swapfile is not

totally

Quoted 3 lines
Quoted 1 line

safe.

But that's only the case with the userspace variant, isn't it?

Yes.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Maybe user-space hibernation should be a separate option.

That actually is not a bad idea at all in my view.

The trouble with kconfig options is that the distros will be pressued into setting CONFIG_HIBERNATE_USERSPACE=y to avoid regressing their uswsusp users, which makes the added security code pointless. As this

True but there are not only distros otherwise the kernel would not have any option at all.

It's actually very nice that if hibernation is disabled no userspace is ever allowed to write to the swap.

Quoted 7 lines
Quoted 4 lines

has clearly sucked me into a conflict that I don't have the resources to
pursue, I'm going to revert the write patch checks and move on with life.

I don't see the need of reverting anything, I can deal with these issues if you are busy on something else.

If you want to work on the patch, please do! Starting from the revert patch I sent earlier, I /think/ only the first chunk (the one that touches blkdev_write_iter) of that patch actually has to be applied to re-enable uswsusp. That could probably be turned into:

	if (IS_SWAPFILE(...) && !IS_ENABLED(HIBERNATION))
		return -ETXTBSY;

Though perhaps a better thing to check here rather than the Kconfig option is whether or not the system is locked out against hibernation? e.g.,

	if (IS_SWAPFILE(...) && !hibernation_available())
		return -EXTBSY;

--D

Quoted 4 lines
Quoted 4 lines

--D

Quoted 1 line

Thanks!

#9 lore raw

PATCH 1/1 hibernate: Allow uswsusp to write to swap

From: Domenico Andreoli <[email protected]>

It turns out that there is one use case for programs being able to write to swap devices, and that is the userspace hibernation code.

Quick fix: disable the S_SWAPFILE check if hibernation is configured.

Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices")
---
 fs/block_dev.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/block_dev.c +2 -1
Index: b/fs/block_dev.c
===================================================================
@@ -2001,7 +2001,8 @@ ssize_t blkdev_write_iter(struct kiocb *
 	if (bdev_read_only(I_BDEV(bd_inode)))
 		return -EPERM;
 
-	if (IS_SWAPFILE(bd_inode))
+	/* uswsusp needs to write to the swap */
+	if (IS_SWAPFILE(bd_inode) && !IS_ENABLED(CONFIG_HIBERNATION))
 		return -ETXTBSY;
 
 	if (!iov_iter_count(from))
#10 lore raw

Re: [PATCH] hibernate: Allow uswsusp to write to swap

Quoted 4 lines · Domenico Andreoli

From: Domenico Andreoli <[email protected]>

It turns out that there is one use case for programs being able to write to swap devices, and that is the userspace hibernation code.

You might want to start a separate thread for this... :)

Quoted 3 lines

Quick fix: disable the S_SWAPFILE check if hibernation is configured.

Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices")

Also, this should be

Fixes dc617f29dbe5 ("vfs: don't allow writes to swap files")

since we still want to set S_SWAPFILE on active swap devices.

Quoted 13 lines
---
 fs/block_dev.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Index: b/fs/block_dev.c
===================================================================

fs/block_dev.c ssize_t blkdev_write_iter(struct kiocb * · line 2005

Quoted 6 lines
fs/block_dev.c +2 -1
@@ -2001,7 +2001,8 @@ ssize_t blkdev_write_iter(struct kiocb *
 	if (bdev_read_only(I_BDEV(bd_inode)))
 		return -EPERM;
 
-	if (IS_SWAPFILE(bd_inode))
+	/* uswsusp needs to write to the swap */
+	if (IS_SWAPFILE(bd_inode) && !IS_ENABLED(CONFIG_HIBERNATION))

...&& hibernation_available() ? That way we can configure this dynamically.

Thanks for taking this on, I'll go self-NAK the revert patch I sent yesterday.

--D

Quoted 3 lines
fs/block_dev.c +0 -0
 		return -ETXTBSY;
 
 	if (!iov_iter_count(from))
#11 lore raw

Re: [PATCH] hibernate: Allow uswsusp to write to swap

Quoted 8 lines · Darrick J. Wong
Quoted 4 lines · Domenico Andreoli

From: Domenico Andreoli <[email protected]>

It turns out that there is one use case for programs being able to write to swap devices, and that is the userspace hibernation code.

You might want to start a separate thread for this... :)

Right

Quoted 8 lines

Quick fix: disable the S_SWAPFILE check if hibernation is configured.

Quoted 2 lines
Fixes 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices")

Also, this should be

Fixes dc617f29dbe5 ("vfs: don't allow writes to swap files")

Of course

since we still want to set S_SWAPFILE on active swap devices.

Quoted 17 lines
Quoted 17 lines
---
 fs/block_dev.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Index: b/fs/block_dev.c
===================================================================

fs/block_dev.c +0 -0
@@ -2001,7 +2001,8 @@ ssize_t blkdev_write_iter(struct kiocb *
      if (bdev_read_only(I_BDEV(bd_inode)))
              return -EPERM;
 
Quoted 7 lines

fs/block_dev.c ssize_t blkdev_write_iter(struct kiocb * · line 2005

Quoted 3 lines
fs/block_dev.c +2 -1
-     if (IS_SWAPFILE(bd_inode))
+     /* uswsusp needs to write to the swap */
+     if (IS_SWAPFILE(bd_inode) && !IS_ENABLED(CONFIG_HIBERNATION))

...&& hibernation_available() ? That way we can configure this dynamically.

Then it would just be "if (IS_SWAPFILE(bd_inode) && !hibernation_available())", right?

Thanks for taking this on, I'll go self-NAK the revert patch I sent

Quoted 2 lines

yesterday.

Thanks,
Dom

#12 lore raw
In reply to Darrick J. Wong
Quoted 5 lines · Darrick J. Wong
Quoted 3 lines · Domenico Andreoli

I don't see the need of reverting anything, I can deal with these issues if you are busy on something else.

Quoted 7 lines

If you want to work on the patch, please do! Starting from the revert patch I sent earlier, I /think/ only the first chunk (the one that touches blkdev_write_iter) of that patch actually has to be applied to re-enable uswsusp. That could probably be turned into:

	if (IS_SWAPFILE(...) && !IS_ENABLED(HIBERNATION))
		return -ETXTBSY;

I've just sent such patch, I don't know how it will play with the whole revert of yesterday and that akpm has already taken in his tree.

Ideally this should go in 5.6-rc and also in stable kernels > 5.2.

Quoted 7 lines

Though perhaps a better thing to check here rather than the Kconfig option is whether or not the system is locked out against hibernation? e.g.,

	if (IS_SWAPFILE(...) && !hibernation_available())
		return -EXTBSY;

This is the kind of improved fix I'm going to prepare for a coming merge window.

Regards,
Domenico

Signature · 2 lines
rsa4096: 3B10 0CA1 8674 ACBA B4FE  FCD2 CE5B CF17 9960 DE13
ed25519: FFB4 0CC3 7F2E 091D F7DA  356E CC79 2832 ED38 CB05
#14 lore raw
Quoted 9 lines · Domenico Andreoli
Quoted 5 lines · Rafael J. Wysocki
Quoted 2 lines · Domenico Andreoli

Maybe user-space hibernation should be a separate option.

That actually is not a bad idea at all in my view.

I prepared a patch for this:
https://lore.kernel.org/linux-pm/[email protected]/

If you succeed in making uswsusp a kconfig option, can you amend the "!hibernation available()" test in blkdev_write_iter so that users of in-kernel hibernate are protected against userspace swap device scribbles, please?

--D

Quoted 6 lines

Regards,
Domenico

Signature · 2 lines
rsa4096: 3B10 0CA1 8674 ACBA B4FE  FCD2 CE5B CF17 9960 DE13
ed25519: FFB4 0CC3 7F2E 091D F7DA  356E CC79 2832 ED38 CB05
#15 lore raw
Quoted 8 lines · Darrick J. Wong
Quoted 7 lines · Domenico Andreoli
Quoted 5 lines · Rafael J. Wysocki
Quoted 2 lines · Domenico Andreoli

Maybe user-space hibernation should be a separate option.

That actually is not a bad idea at all in my view.

Quoted 7 lines
Quoted 2 lines

I prepared a patch for this:
https://lore.kernel.org/linux-pm/[email protected]/

If you succeed in making uswsusp a kconfig option, can you amend the "!hibernation available()" test in blkdev_write_iter so that users of in-kernel hibernate are protected against userspace swap device scribbles, please?

Yes, that's the plan.

Quoted 2 lines

--D

Domenico

Signature · 2 lines
rsa4096: 3B10 0CA1 8674 ACBA B4FE  FCD2 CE5B CF17 9960 DE13
ed25519: FFB4 0CC3 7F2E 091D F7DA  356E CC79 2832 ED38 CB05