* [PATCH v2] mm/swap: reject swapon() on filesystem-level encrypted files
@ 2026-08-03 18:04 Eric Biggers
2026-08-03 18:41 ` Usama Anjum
2026-08-04 0:54 ` Darrick J. Wong
0 siblings, 2 replies; 3+ messages in thread
From: Eric Biggers @ 2026-08-03 18:04 UTC (permalink / raw)
To: linux-mm, Andrew Morton, Chris Li, Kairui Song
Cc: linux-ext4, linux-f2fs-devel, linux-fscrypt, linux-kernel,
Kemeng Shi, Nhat Pham, Baoquan He, Barry Song, Youngjun Park,
Eric Biggers, stable
ext4 and f2fs don't prevent filesystem-level encrypted files from being
set up directly as swap files. In this case, encryption is bypassed.
No one should be doing this, vs. the methods of encrypted swap that
actually do work (such as swapping to a dm-crypt device, or swapping to
a loopback device on top of a filesystem-level encrypted file).
Nevertheless, to prevent user error, make swapon() explicitly reject
this case. Document this behavior in fscrypt.rst as well.
Fixes: 9bd8212f981e ("ext4 crypto: add encryption policy and password salt support")
Fixes: f424f664f0e8 ("f2fs crypto: add encryption policy and password salt support")
Cc: stable@vger.kernel.org
Reviewed-by: Baoquan He <baoquan.he@linux.dev>
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
Changed in v2:
- Added a pr_warn_once
- Removed unnecessary word from fscrypt.rst
- Added Reviewed-by
Documentation/filesystems/fscrypt.rst | 4 ++++
mm/swapfile.c | 7 +++++++
2 files changed, 11 insertions(+)
diff --git a/Documentation/filesystems/fscrypt.rst b/Documentation/filesystems/fscrypt.rst
index c0dd35f1af12..e4882b73120e 100644
--- a/Documentation/filesystems/fscrypt.rst
+++ b/Documentation/filesystems/fscrypt.rst
@@ -1238,6 +1238,10 @@ astute users may notice some differences in behavior:
- DAX (Direct Access) is not supported on encrypted files.
+- Encrypted files cannot be used directly as swap files. To swap to
+ an encrypted file, set up a loopback device on top of it.
+ Alternatively, encrypted swap can use a dm-crypt device.
+
- The maximum length of an encrypted symlink is 2 bytes shorter than
the maximum length of an unencrypted symlink. For example, on an
EXT4 filesystem with a 4K block size, unencrypted symlinks can be up
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 78b49b0658ad..8538659acb9e 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -3650,6 +3650,13 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
error = -EBUSY;
goto bad_swap_unlock_inode;
}
+ if (IS_ENCRYPTED(inode)) {
+ pr_warn_once(
+ "Filesystem-level encrypted swapfile '%s' is unsupported. Create a loop device over it, or use dm-crypt\n",
+ name->name);
+ error = -EINVAL;
+ goto bad_swap_unlock_inode;
+ }
/*
* The swap subsystem needs a major overhaul to support this.
base-commit: f5098b6bae761e346ebcd9da7f95622c04733cff
--
2.55.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2] mm/swap: reject swapon() on filesystem-level encrypted files
2026-08-03 18:04 [PATCH v2] mm/swap: reject swapon() on filesystem-level encrypted files Eric Biggers
@ 2026-08-03 18:41 ` Usama Anjum
2026-08-04 0:54 ` Darrick J. Wong
1 sibling, 0 replies; 3+ messages in thread
From: Usama Anjum @ 2026-08-03 18:41 UTC (permalink / raw)
To: Eric Biggers
Cc: usama.anjum, linux-ext4, linux-f2fs-devel, linux-fscrypt,
linux-kernel, Kemeng Shi, Nhat Pham, Baoquan He, Barry Song,
Youngjun Park, stable, linux-mm, Andrew Morton, Chris Li,
Kairui Song
On 03/08/2026 7:04 pm, Eric Biggers wrote:
> ext4 and f2fs don't prevent filesystem-level encrypted files from being
> set up directly as swap files. In this case, encryption is bypassed.
>
> No one should be doing this, vs. the methods of encrypted swap that
> actually do work (such as swapping to a dm-crypt device, or swapping to
> a loopback device on top of a filesystem-level encrypted file).
>
> Nevertheless, to prevent user error, make swapon() explicitly reject
> this case. Document this behavior in fscrypt.rst as well.
>
> Fixes: 9bd8212f981e ("ext4 crypto: add encryption policy and password salt support")
> Fixes: f424f664f0e8 ("f2fs crypto: add encryption policy and password salt support")
> Cc: stable@vger.kernel.org
> Reviewed-by: Baoquan He <baoquan.he@linux.dev>
> Signed-off-by: Eric Biggers <ebiggers@kernel.org>
In the past, I've certainly setup swap wrong. It'll help:
Reviewed-by: Muhammad Usama Anjum <usama.anjum@arm.com>
> ---
>
> Changed in v2:
> - Added a pr_warn_once
> - Removed unnecessary word from fscrypt.rst
> - Added Reviewed-by
>
> Documentation/filesystems/fscrypt.rst | 4 ++++
> mm/swapfile.c | 7 +++++++
> 2 files changed, 11 insertions(+)
>
> diff --git a/Documentation/filesystems/fscrypt.rst b/Documentation/filesystems/fscrypt.rst
> index c0dd35f1af12..e4882b73120e 100644
> --- a/Documentation/filesystems/fscrypt.rst
> +++ b/Documentation/filesystems/fscrypt.rst
> @@ -1238,6 +1238,10 @@ astute users may notice some differences in behavior:
>
> - DAX (Direct Access) is not supported on encrypted files.
>
> +- Encrypted files cannot be used directly as swap files. To swap to
> + an encrypted file, set up a loopback device on top of it.
> + Alternatively, encrypted swap can use a dm-crypt device.
> +
> - The maximum length of an encrypted symlink is 2 bytes shorter than
> the maximum length of an unencrypted symlink. For example, on an
> EXT4 filesystem with a 4K block size, unencrypted symlinks can be up
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 78b49b0658ad..8538659acb9e 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -3650,6 +3650,13 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
> error = -EBUSY;
> goto bad_swap_unlock_inode;
> }
> + if (IS_ENCRYPTED(inode)) {
> + pr_warn_once(
> + "Filesystem-level encrypted swapfile '%s' is unsupported. Create a loop device over it, or use dm-crypt\n",
> + name->name);
> + error = -EINVAL;
> + goto bad_swap_unlock_inode;
> + }
>
> /*
> * The swap subsystem needs a major overhaul to support this.
>
> base-commit: f5098b6bae761e346ebcd9da7f95622c04733cff
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] mm/swap: reject swapon() on filesystem-level encrypted files
2026-08-03 18:04 [PATCH v2] mm/swap: reject swapon() on filesystem-level encrypted files Eric Biggers
2026-08-03 18:41 ` Usama Anjum
@ 2026-08-04 0:54 ` Darrick J. Wong
1 sibling, 0 replies; 3+ messages in thread
From: Darrick J. Wong @ 2026-08-04 0:54 UTC (permalink / raw)
To: Eric Biggers
Cc: linux-mm, Andrew Morton, Chris Li, Kairui Song, linux-ext4,
linux-f2fs-devel, linux-fscrypt, linux-kernel, Kemeng Shi,
Nhat Pham, Baoquan He, Barry Song, Youngjun Park, stable
On Mon, Aug 03, 2026 at 11:04:26AM -0700, Eric Biggers wrote:
> ext4 and f2fs don't prevent filesystem-level encrypted files from being
> set up directly as swap files. In this case, encryption is bypassed.
>
> No one should be doing this, vs. the methods of encrypted swap that
> actually do work (such as swapping to a dm-crypt device, or swapping to
> a loopback device on top of a filesystem-level encrypted file).
>
> Nevertheless, to prevent user error, make swapon() explicitly reject
> this case. Document this behavior in fscrypt.rst as well.
>
> Fixes: 9bd8212f981e ("ext4 crypto: add encryption policy and password salt support")
> Fixes: f424f664f0e8 ("f2fs crypto: add encryption policy and password salt support")
> Cc: stable@vger.kernel.org
> Reviewed-by: Baoquan He <baoquan.he@linux.dev>
> Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Looks good to me!
Reviewed-by: "Darrick J. Wong" <djwong@kernel.org>
--D
> ---
>
> Changed in v2:
> - Added a pr_warn_once
> - Removed unnecessary word from fscrypt.rst
> - Added Reviewed-by
>
> Documentation/filesystems/fscrypt.rst | 4 ++++
> mm/swapfile.c | 7 +++++++
> 2 files changed, 11 insertions(+)
>
> diff --git a/Documentation/filesystems/fscrypt.rst b/Documentation/filesystems/fscrypt.rst
> index c0dd35f1af12..e4882b73120e 100644
> --- a/Documentation/filesystems/fscrypt.rst
> +++ b/Documentation/filesystems/fscrypt.rst
> @@ -1238,6 +1238,10 @@ astute users may notice some differences in behavior:
>
> - DAX (Direct Access) is not supported on encrypted files.
>
> +- Encrypted files cannot be used directly as swap files. To swap to
> + an encrypted file, set up a loopback device on top of it.
> + Alternatively, encrypted swap can use a dm-crypt device.
> +
> - The maximum length of an encrypted symlink is 2 bytes shorter than
> the maximum length of an unencrypted symlink. For example, on an
> EXT4 filesystem with a 4K block size, unencrypted symlinks can be up
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 78b49b0658ad..8538659acb9e 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -3650,6 +3650,13 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
> error = -EBUSY;
> goto bad_swap_unlock_inode;
> }
> + if (IS_ENCRYPTED(inode)) {
> + pr_warn_once(
> + "Filesystem-level encrypted swapfile '%s' is unsupported. Create a loop device over it, or use dm-crypt\n",
> + name->name);
> + error = -EINVAL;
> + goto bad_swap_unlock_inode;
> + }
>
> /*
> * The swap subsystem needs a major overhaul to support this.
>
> base-commit: f5098b6bae761e346ebcd9da7f95622c04733cff
> --
> 2.55.0
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-08-04 0:54 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-03 18:04 [PATCH v2] mm/swap: reject swapon() on filesystem-level encrypted files Eric Biggers
2026-08-03 18:41 ` Usama Anjum
2026-08-04 0:54 ` Darrick J. Wong
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox