Skip to content

Commit 45fec72

Browse files
Eric Biggersgregkh
authored andcommitted
mm/swap: reject swapon() on filesystem-level encrypted files
commit c310a89 upstream. 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. Link: https://lore.kernel.org/20260803180426.3123-1-ebiggers@kernel.org Fixes: 9bd8212 ("ext4 crypto: add encryption policy and password salt support") Fixes: f424f66 ("f2fs crypto: add encryption policy and password salt support") Signed-off-by: Eric Biggers <ebiggers@kernel.org> Reviewed-by: Baoquan He <baoquan.he@linux.dev> Reviewed-by: Muhammad Usama Anjum <usama.anjum@arm.com> Reviewed-by: "Darrick J. Wong" <djwong@kernel.org> Cc: Barry Song <baohua@kernel.org> Cc: Chris Li <chrisl@kernel.org> Cc: Kairui Song <kasong@tencent.com> Cc: Kemeng Shi <shikemeng@huaweicloud.com> Cc: Nhat Pham <nphamcs@gmail.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent df86c0e commit 45fec72

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

Documentation/filesystems/fscrypt.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,10 @@ astute users may notice some differences in behavior:
12161216

12171217
- DAX (Direct Access) is not supported on encrypted files.
12181218

1219+
- Encrypted files cannot be used directly as swap files. To swap to
1220+
an encrypted file, set up a loopback device on top of it.
1221+
Alternatively, encrypted swap can use a dm-crypt device.
1222+
12191223
- The maximum length of an encrypted symlink is 2 bytes shorter than
12201224
the maximum length of an unencrypted symlink. For example, on an
12211225
EXT4 filesystem with a 4K block size, unencrypted symlinks can be up

mm/swapfile.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,6 +3430,13 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
34303430
error = -EBUSY;
34313431
goto bad_swap_unlock_inode;
34323432
}
3433+
if (IS_ENCRYPTED(inode)) {
3434+
pr_warn_once(
3435+
"Filesystem-level encrypted swapfile '%s' is unsupported. Create a loop device over it, or use dm-crypt\n",
3436+
name->name);
3437+
error = -EINVAL;
3438+
goto bad_swap_unlock_inode;
3439+
}
34333440

34343441
/*
34353442
* Read the swap header.

0 commit comments

Comments
 (0)