From: Eric Biggers <ebiggers@kernel.org>
To: "Darrick J. Wong" <djwong@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
linux-mm@kvack.org, Chris Li <chrisl@kernel.org>,
Kairui Song <kasong@tencent.com>,
linux-ext4@vger.kernel.org,
linux-f2fs-devel@lists.sourceforge.net,
linux-fscrypt@vger.kernel.org, linux-kernel@vger.kernel.org,
Kemeng Shi <shikemeng@huaweicloud.com>,
Nhat Pham <nphamcs@gmail.com>, Baoquan He <baoquan.he@linux.dev>,
Barry Song <baohua@kernel.org>,
Youngjun Park <youngjun.park@lge.com>,
stable@vger.kernel.org
Subject: Re: [PATCH] mm/swap: reject swapon() on filesystem-level encrypted files
Date: Thu, 30 Jul 2026 21:04:38 -0700 [thread overview]
Message-ID: <20260731040438.GA57540@sol> (raw)
In-Reply-To: <20260731034931.GA3560112@frogsfrogsfrogs>
On Thu, Jul 30, 2026 at 08:49:31PM -0700, Darrick J. Wong wrote:
> On Thu, Jul 30, 2026 at 08:30:03PM +0000, Eric Biggers wrote:
> > On Thu, Jul 30, 2026 at 12:53:27PM -0700, Andrew Morton wrote:
> > > On Thu, 30 Jul 2026 11:48:53 -0700 Eric Biggers <ebiggers@kernel.org> 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")
> > >
> > > Ouch. We are going to break ten year old setups? I don't think we can
> > > do this!
> >
> > That assumes anyone is actually depending on this bug, which they
> > probably aren't. Encrypted swap is done in other ways.
>
> Hmm. swapon() seems to read the swapfile header via read_mapping_folio,
> which means that fscrypt will read the encrypted block 0, decrypt it,
> and hand the mm a valid swapfile header, right?
>
> For blockdev filesystem, the setup_swap_extents code will set up a file
> range -> LBA mapping, and the swap code issues bios directly to the
> bdev. Ondisk contents aren't encrypted, and read()ing the swapfile will
> produce garbage because we're decrypting something that isn't encrypted.
>
> So I guess the bug here is that the ondisk swapfile contents aren't
> encrypted? Can we fix the swap code to call fscrypt_set_bio_crypt_ctx?
>
> Though, you probably don't want anyone to read the non-header swapfile
> contents, so can one create an ephemeral encryption key for the swap
> code? Or do you not want crypto engines in the swap path?
>
> (Apologies for my unfiltered idiot thoughts :P)
Well, the direct swapfile implementation bypasses the filesystem after
the header has been read and the on-disk blocks have been queried. That
seems to be very much what it's designed to do. It seems out of scope
for it to start trying to implement filesystem features like fscrypt
file contents encryption. (Note that there's more to it than calling
fscrypt_set_bio_crypt_ctx(), especially in current mainline.) After
all, we also wouldn't expect it to understand and implement the
compression, checksums, etc. features of various filesystems.
Yes, when people encrypt their swap, they of course use an ephemeral
key. However, they use either dm-crypt, or a loop device on top of an
fscrypt file. (Note that if they use zram with a backing file, they
*have* to use a block device anyway.)
Fortunately, I've never heard of someone actually using the direct
swapon() with an fscrypt file, which is what this patch pertains to.
This case was just found by an LLM review. I don't think we need to add
a new feature here; we should just remove the footgun just in case.
- Eric
next prev parent reply other threads:[~2026-07-31 4:06 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-30 18:48 [PATCH] mm/swap: reject swapon() on filesystem-level encrypted files Eric Biggers
2026-07-30 19:53 ` Andrew Morton
2026-07-30 20:30 ` Eric Biggers
2026-07-31 3:49 ` Darrick J. Wong
2026-07-31 4:04 ` Eric Biggers [this message]
2026-07-31 15:11 ` Baoquan He
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260731040438.GA57540@sol \
--to=ebiggers@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baoquan.he@linux.dev \
--cc=chrisl@kernel.org \
--cc=djwong@kernel.org \
--cc=kasong@tencent.com \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-fscrypt@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=shikemeng@huaweicloud.com \
--cc=stable@vger.kernel.org \
--cc=youngjun.park@lge.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox