From: William Chuang <founder@logarcheon.com>
To: Theodore Ts'o <tytso@mit.edu>
Cc: Andreas Dilger <adilger.kernel@dilger.ca>,
Jan Kara <jack@suse.cz>, Ojaswin Mujoo <ojaswin@linux.ibm.com>,
Ritesh Harjani <ritesh.list@gmail.com>,
linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3] ext4: reject a non-regular boot loader inode in EXT4_IOC_SWAP_BOOT
Date: Tue, 17 Nov 2026 22:33:59 +0000 [thread overview]
Message-ID: <20261117223350.6920-1-founder@logarcheon.com> (raw)
In-Reply-To: <20261117065645.4409-1-founder@logarcheon.com>
swap_inode_boot_loader() gets the boot loader inode (inode 5) with
EXT4_IGET_BAD. On a new filesystem that inode is unused, its mode is
0, and ext4_iget() returns it as a bad inode, which make_bad_inode()
sets to S_IFREG. On a corrupted or crafted filesystem, ext4_iget()
can instead return inode 5 as a directory or another non-regular type,
fully set up with that type's inode and file operations.
Such an inode is first passed to lock_two_nondirectories(), which
warns if it is a directory:
WARNING: fs/inode.c:1271 at lock_two_nondirectories
swap_inode_boot_loader
__ext4_ioctl
The "never used as a boot loader" branch then sets inode_bl->i_mode
to S_IFREG, changing the type of a live in-core inode while it keeps
the operations of its old type.
Check the type right after ext4_iget(): report the corruption and
return -EFSCORRUPTED unless the inode is a regular file. Leave the
cached inode's type unchanged, so that a concurrent caller holding the
same inode rejects it too. ext4_iget() lets inode 5 through with
i_nlink == 0; in that case set i_nlink to 1 in memory, under the inode
lock, so that the final iput() does not send the reserved inode down
the deletion path of ext4_evict_inode().
With non-regular types rejected up front, the later
"is_bad_inode(inode_bl) || !S_ISREG(inode_bl->i_mode)" test only needs
is_bad_inode(). The unused
case is unaffected, since make_bad_inode() has already made it
S_IFREG. This is the check Ted Ts'o suggested when the warning was
first reported.
Tested on UML at 666539eec: inode 5 set with debugfs to a directory
(with i_links_count 1, and with 0) or a character device, then
EXT4_IOC_SWAP_BOOT issued twice. Before this patch, the directory
cases warn, and all cases succeed and leave inode 5 as a regular file
on disk. After it, all return EFSCORRUPTED, the error is logged,
inode 5 is unchanged, and no attempt is made to free it. Two
processes issuing the ioctl concurrently 500 times each against a
directory inode 5 never succeed. On a
new filesystem the ioctl still succeeds, twice. Not tested: KASAN
builds and syzbot's 2023 reproducer.
Fixes: 991ed014de08 ("ext4: fix bug_on in __es_tree_search caused by bad boot loader inode")
Suggested-by: Theodore Ts'o <tytso@mit.edu>
Link: https://lore.kernel.org/all/tencent_DABB2333139E8D1BCF4B5D1B2725FABA9108@qq.com/T/
Link: https://sashiko.dev/#/bug/linux-8cfc8e22-8259-4c89-8a8b-64c16e4481a5
Signed-off-by: William Chuang <founder@logarcheon.com>
---
v3: following Sashiko's review of v2:
- do not call make_bad_inode() on the rejected inode: it changes the
type of a cached inode that a concurrent caller may hold, which
then passes the regular-file check and is swapped. Instead, when
i_nlink is 0, set it to 1 in memory under the inode lock, so the
final iput() does not try to delete inode 5;
- drop the now redundant !S_ISREG() from the later is_bad_inode()
test.
The concurrent case was run on single-CPU UML, where the v2 race did
not show up either; v3 avoids it by not changing the inode's type.
v2: https://lore.kernel.org/all/20261117065645.4409-1-founder@logarcheon.com/
mark the rejected inode bad before iput() (Sashiko review of v1).
v1: https://lore.kernel.org/all/20261117061822.3826-1-founder@logarcheon.com/
fs/ext4/ioctl.c | 26 +++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index e8e579498..41e8934a7 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -390,6 +390,30 @@ static long swap_inode_boot_loader(struct super_block *sb,
return PTR_ERR(inode_bl);
ei_bl = EXT4_I(inode_bl);
+ /*
+ * An unused boot loader inode comes back from ext4_iget() as a bad
+ * inode, which make_bad_inode() leaves as S_IFREG. Any other type
+ * means the on-disk inode is corrupted; its type cannot be changed
+ * in place below.
+ */
+ if (!S_ISREG(inode_bl->i_mode)) {
+ EXT4_ERROR_INODE(inode_bl,
+ "boot loader inode is not a regular file (mode 0%o)",
+ inode_bl->i_mode);
+ /*
+ * Leave the type alone: other callers may hold this cached
+ * inode, and must reject it too. ext4_iget() lets inode 5
+ * through with i_nlink == 0, so keep the last iput() from
+ * treating the reserved inode as deleted.
+ */
+ inode_lock(inode_bl);
+ if (!inode_bl->i_nlink)
+ set_nlink(inode_bl, 1);
+ inode_unlock(inode_bl);
+ iput(inode_bl);
+ return -EFSCORRUPTED;
+ }
+
/* Protect orig inodes against a truncate and make sure,
* that only 1 swap_inode_boot_loader is running. */
lock_two_nondirectories(inode, inode_bl);
@@ -435,7 +459,7 @@ static long swap_inode_boot_loader(struct super_block *sb,
/* Protect extent tree against block allocations via delalloc */
ext4_double_down_write_data_sem(inode, inode_bl);
- if (is_bad_inode(inode_bl) || !S_ISREG(inode_bl->i_mode)) {
+ if (is_bad_inode(inode_bl)) {
/* this inode has never been used as a BOOT_LOADER */
set_nlink(inode_bl, 1);
i_uid_write(inode_bl, 0);
--
2.43.0
prev parent reply other threads:[~2026-11-17 22:34 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 6:18 [PATCH] " William Chuang
2026-11-17 6:56 ` [PATCH v2] " William Chuang
2026-11-17 22:33 ` William Chuang [this message]
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=20261117223350.6920-1-founder@logarcheon.com \
--to=founder@logarcheon.com \
--cc=adilger.kernel@dilger.ca \
--cc=jack@suse.cz \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ojaswin@linux.ibm.com \
--cc=ritesh.list@gmail.com \
--cc=tytso@mit.edu \
/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
all inboxes | Powered by JetHome®