ext4: reject a non-regular boot loader inode in EXT4_IOC_SWAP_BOOT
PATCH 1/1 ext4: reject a non-regular boot loader inode in EXT4_IOC_SWAP_BOOT
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. 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 to a directory or a character device with debugfs, then EXT4_IOC_SWAP_BOOT issued twice. Before this patch, the directory case warns, and both cases succeed and leave inode 5 as a regular file on disk. After it, both return EFSCORRUPTED, the error is logged, and inode 5 is unchanged. On a new filesystem the ioctl still succeeds, twice. Not tested: KASAN builds and syzbot's 2023 reproducer.
--- fs/ext4/ioctl.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
fs/ext4/ioctl.c +14 -0
@@ -390,6 +390,20 @@ 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); +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);
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] ext4: missing make_bad_inode() before iput() in swap_inode_boot_loader() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] ext4: missing make_bad_inode() before iput() in swap_inode_boot_loader()
--
commit f760f0ea1cf04164f03ebca7f4659e4130a0b1ed
Author: William Chuang <[email protected]>
ext4: reject a non-regular boot loader inode in EXT4_IOC_SWAP_BOOT
This commit rejects a boot loader inode (inode 5) in swap_inode_boot_loader() with -EFSCORRUPTED if its mode after ext4_iget() is not S_IFREG.
Quoted 11 lines
fs/ext4/ioctl.c +3
-0@@ -390,6 +390,20 @@ 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
fs/ext4/ioctl.c static long swap_inode_boot_loader(struct super_block *sb, · line 405
Quoted 10 lines
fs/ext4/ioctl.c +10
-0+* 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); +iput (inode_bl); +return -EFSCORRUPTED ; + }
[Severity: Medium]
Should this error path call make_bad_inode(inode_bl) before iput(inode_bl), similar to ext4_get_journal_inode()?
__ext4_iget() exempts EXT4_BOOT_LOADER_INO from the i_nlink == 0 check and only marks it bad when i_mode is not a recognized file type:
fs/ext4/inode.c:__ext4_iget() {
...
if (inode->i_nlink == 0) {
if ((inode->i_mode == 0 || flags & EXT4_IGET_SPECIAL ||
!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
ino != EXT4_BOOT_LOADER_INO) {
...
}When a corrupted inode 5 has a valid non-regular i_mode (such as S_IFDIR or S_IFCHR) and i_nlink == 0, __ext4_iget() returns a live, non-bad inode. Under errors=continue, iput(inode_bl) then enters ext4_evict_inode(), which only skips truncation and on-disk deletion when is_bad_inode(inode) is true, causing it to truncate inode 5, dirty it on disk, and call ext4_free_inode() on the reserved inode. If i_nlink > 0, the corrupted non-regular inode stays hashed in the inode cache instead of being evicted.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 ext4: reject a non-regular boot loader inode in EXT4_IOC_SWAP_BOOT
In reply to William Chuangswap_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. Mark the rejected inode bad before iput(), as ext4_get_journal_inode() does, so that a corrupted inode 5 with i_nlink == 0 is not passed to the deletion path of ext4_evict_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. On a new filesystem the ioctl still succeeds, twice. Not tested: KASAN builds and syzbot's 2023 reproducer.
---
v2: mark the rejected inode bad before iput(), as ext4_get_journal_inode()
does (Sashiko review). Without it, a corrupted inode 5 with i_links_count 0 went to the deletion path on iput(), which logged "ext4_free_inode: reserved or nonexistent inode 5". Added that case to the tests.
v1: https://lore.kernel.org/all/[email protected]/
fs/ext4/ioctl.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
fs/ext4/ioctl.c +20 -0
@@ -390,6 +390,26 @@ 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); +/* +* Drop it as a bad inode, as ext4_get_journal_inode() does: +* with i_nlink == 0, a plain iput() would try to delete the +* reserved inode, and otherwise it would stay cached. +*/ +make_bad_inode (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);
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] ext4: unlocked make_bad_inode() in swap_inode_boot_loader() races with iget - [Low] ext4…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] ext4: unlocked make_bad_inode() in swap_inode_boot_loader() races with iget
- [Low] ext4: redundant !S_ISREG() check left in swap_inode_boot_loader()
--
commit 92e1647560c592675448976b5391a26f9a8b9d29
Author: William Chuang <[email protected]>
ext4: reject a non-regular boot loader inode in EXT4_IOC_SWAP_BOOT
This commit rejects EXT4_IOC_SWAP_BOOT when the boot loader inode is not a regular file, marking the inode bad before dropping the reference.
Quoted 21 lines
fs/ext4/ioctl.c +13
-0@@ -390,6 +390,26 @@ 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); +/* +* Drop it as a bad inode, as ext4_get_journal_inode() does: +* with i_nlink == 0, a plain iput() would try to delete the
fs/ext4/ioctl.c static long swap_inode_boot_loader(struct super_block *sb, · line 415
Quoted 10 lines
fs/ext4/ioctl.c +7
-0+* reserved inode, and otherwise it would stay cached. +*/ +make_bad_inode (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);
[Severity: Medium]
Can calling make_bad_inode(inode_bl) on a live, unlocked inode race with a concurrent swap_inode_boot_loader() caller and trick it into treating the corrupted inode as an unused boot loader inode?
When the on-disk boot loader inode has a valid non-regular mode, __ext4_iget() unlocks and returns it while it is hashed in the inode cache. make_bad_inode() overwrites inode_bl->i_mode with S_IFREG and sets inode_bl->i_op to &bad_inode_ops, which is the same state swap_inode_boot_loader() uses to identify an unused boot loader inode:
CPU0 CPU1swap_inode_boot_loader() swap_inode_boot_loader()
inode_bl = ext4_iget(...)
!S_ISREG(inode_bl->i_mode)
EXT4_ERROR_INODE(...)
inode_bl = ext4_iget(...)
/* gets same cached inode_bl */
make_bad_inode(inode_bl)
inode_bl->i_mode = S_IFREG
inode_bl->i_op = &bad_inode_ops
iput(inode_bl)
!S_ISREG(inode_bl->i_mode) /* false */
...
if (is_bad_inode(inode_bl) || ...)
/* reinitializes & swaps inode_bl */CPU1 then bypasses the !S_ISREG(inode_bl->i_mode) check, enters the is_bad_inode(inode_bl) branch, and swaps the corrupted inode (preserving its stale i_blocks, i_bytes, and union fields) with the caller's file.
[Severity: Low]
This isn't a bug, but now that non-regular boot loader inodes are rejected upfront here, is the !S_ISREG(inode_bl->i_mode) check added by 991ed014de08 ("ext4: fix bug_on in __es_tree_search caused by bad boot loader inode") further down in swap_inode_boot_loader() now redundant?
fs/ext4/ioctl.c:swap_inode_boot_loader() {
...
if (is_bad_inode(inode_bl) || !S_ISREG(inode_bl->i_mode)) {
/* this inode has never been used as a BOOT_LOADER */
...
}Dropping !S_ISREG(inode_bl->i_mode) from that condition would keep the two checks consistent.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v3 1/1 ext4: reject a non-regular boot loader inode in EXT4_IOC_SWAP_BOOT
In reply to William Chuangswap_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.
---
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/[email protected]/
mark the rejected inode bad before iput() (Sashiko review of v1).
v1: https://lore.kernel.org/all/[email protected]/
fs/ext4/ioctl.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
fs/ext4/ioctl.c +25 -1
@@ -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 );
Signature · 1 line
2.43.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 14 lines
swap_inode_boot_loader()gets the boot loader inode (inode 5) withEXT4_IGET_BAD. On a new filesystem that inode is unused, its mode is 0, andext4_iget()returns it as a bad inode, whichmake_bad_inode()sets toS_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
Quoted 7 lines
The "never used as a boot loader" branch then sets
inode_bl->i_mode[ ... ]Fixes 991ed014de08 ("ext4: fix bug_on in __es_tree_search caused by bad boot loader inode")Suggested-by Theodore Ts'o <[email protected]>Signed-off-by William Chuang <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 44 lines · William Chuang
swap_inode_boot_loader()gets the boot loader inode (inode 5) withEXT4_IGET_BAD. On a new filesystem that inode is unused, its mode is 0, andext4_iget()returns it as a bad inode, whichmake_bad_inode()sets toS_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_ioctlThe "never used as a boot loader" branch then sets
inode_bl->i_modetoS_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-EFSCORRUPTEDunless 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 withi_nlink== 0; in that case seti_nlinkto 1 in memory, under the inode lock, so that the finaliput()does not send the reserved inode down the deletion path ofext4_evict_inode().With non-regular types rejected up front, the later "
is_bad_inode(inode_bl)|| !S_ISREG(inode_bl->i_mode)" test only needsis_bad_inode(). The unused
case is unaffected, sincemake_bad_inode()has already made itS_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 (withi_links_count1, and with 0) or a character device, thenEXT4_IOC_SWAP_BOOTissued 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.
Quoted 5 lines
Fixes 991ed014de08 ("ext4: fix bug_on in __es_tree_search caused by bad boot loader inode")Suggested-by Theodore Ts'o <[email protected]>Signed-off-by William Chuang <[email protected]>
Yeah, this looks sane. Feel free to add:
Honza
Quoted 66 lines
---
v3: following Sashiko's review of v2:
- do not callmake_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, wheni_nlinkis 0, set it to 1 in memory under the inode lock, so the finaliput()does not try to delete inode 5;
- drop the now redundant !S_ISREG() from the lateris_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/[email protected]/
mark the rejected inode bad beforeiput()(Sashiko review of v1).
v1: https://lore.kernel.org/all/[email protected]/fs/ext4/ioctl.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)fs/ext4/ioctl.c +25
-1@@ -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 );Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR