From: "Theodore Ts'o" <tytso@mit.edu>
To: Ext4 Developers List <linux-ext4@vger.kernel.org>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Subject: [PATCH 22/23] ext4: don't allocate over indirect branches that failed to load
Date: Wed, 11 Nov 2026 23:47:11 -0500 [thread overview]
Message-ID: <20261112044712.1577296-23-tytso@mit.edu> (raw)
In-Reply-To: <20261112044712.1577296-1-tytso@mit.edu>
ext4_get_branch() stops walking an indirect block chain either because
it found an unallocated pointer (*err == 0) or because it failed to
load an indirect block whose pointer is non-zero: -EFSCORRUPTED when
the pointer is beyond the end of the filesystem, -ENOMEM when
ext4_sb_getblk() fails, and -EIO when the read fails or when
ext4_check_indirect_blockref() rejects the block contents.
ext4_ind_map_blocks() only checks for -EIO before starting allocation.
For -EFSCORRUPTED and -ENOMEM it treats the still populated slot as a
hole, allocates a new branch and has ext4_splice_branch() store it over
the existing pointer. Every block reachable from the old indirect
block is then silently dropped from the file and leaked, and the
corruption is never reported to the caller.
The bigalloc check that rejects allocation for indirect-mapped inodes
also jumps to "out" instead of "cleanup", leaking the references to the
indirect block buffer_heads that ext4_get_branch() already took.
Bail out on any error from ext4_get_branch() before using the partial
chain on both the lookup and the allocation paths, release the chain on
the bigalloc error path, and return -EFSCORRUPTED rather than -EIO when
an indirect block fails ext4_check_indirect_blockref(), so that it is
reported consistently with other indirect block corruption.
Extend the indirect KUnit suite to cover allocation through a branch
that hits -ENOMEM or -EFSCORRUPTED, and the buffer_head reference
count on the bigalloc rejection path.
Fixes: 860d21e2c585 ("ext4: return ENOMEM if sb_getblk() fails")
Fixes: bab08ab96462 ("ext4: enforce bigalloc restrictions (e.g., no online resizing, etc.)")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
---
fs/ext4/indirect-test.c | 90 ++++++++++++++++++++++++++++++++++++++++-
fs/ext4/indirect.c | 19 ++++++---
2 files changed, 102 insertions(+), 7 deletions(-)
diff --git a/fs/ext4/indirect-test.c b/fs/ext4/indirect-test.c
index add5662adc3b..23094102608e 100644
--- a/fs/ext4/indirect-test.c
+++ b/fs/ext4/indirect-test.c
@@ -78,6 +78,7 @@ struct ind_test_ctx {
unsigned int fail_alloc_on_call;
int fail_alloc_err;
int read_bh_err;
+ ext4_fsblk_t getblk_fail_blk;
ext4_fsblk_t freed_blks[IND_TEST_MAX_FREED_REC];
unsigned int freed_cnt;
bool rwsem_inited;
@@ -230,6 +231,9 @@ static struct buffer_head *ind_sb_getblk_stub(struct super_block *sb,
ctx = test->priv;
if (!ctx || sb != ctx->sb)
return NULL;
+ /* Simulate sb_getblk() failure for one specific block */
+ if (ctx->getblk_fail_blk && block == ctx->getblk_fail_blk)
+ return NULL;
return ind_get_or_create_bh(ctx, block);
}
@@ -953,14 +957,15 @@ static void test_ind_map_blocks_errors_and_alloc_branch_unwind(struct kunit *tes
/*
* 5. Non-uptodate IND block containing invalid blockref (block 1 ==
- * s_first_data_block) fails ext4_check_indirect_blockref() -> -EIO.
+ * s_first_data_block) fails ext4_check_indirect_blockref() ->
+ * -EFSCORRUPTED.
*/
((__le32 *)ind_bh->b_data)[0] = cpu_to_le32(IND_TEST_FIRST_DATA_BLK);
clear_buffer_uptodate(ind_bh);
down_read(&EXT4_I(inode)->i_data_sem);
ret = ext4_ind_map_blocks(NULL, inode, &map, 0);
up_read(&EXT4_I(inode)->i_data_sem);
- KUNIT_EXPECT_EQ(test, ret, -EIO);
+ KUNIT_EXPECT_EQ(test, ret, -EFSCORRUPTED);
brelse(ind_bh);
EXT4_I(inode)->i_data[EXT4_IND_BLOCK] = 0;
@@ -1247,6 +1252,86 @@ static void test_ind_free_corrupt_block_numbers(struct kunit *test)
iput(inode);
}
+/*
+ * Test 8: ext4_ind_map_blocks(EXT4_GET_BLOCKS_CREATE) must fail, rather than
+ * treat the branch as a hole and splice a new one over the existing pointer,
+ * when ext4_get_branch() fails with -ENOMEM or -EFSCORRUPTED, and must not
+ * leak indirect buffer_heads when rejecting allocation on bigalloc.
+ */
+static void test_ind_map_blocks_create_branch_errors(struct kunit *test)
+{
+ struct ind_test_ctx *ctx = test->priv;
+ struct super_block *sb = ctx->sb;
+ struct inode *inode = ind_make_test_inode(test, 19);
+ struct ext4_map_blocks map;
+ struct buffer_head *ind_bh;
+ ext4_fsblk_t bad_blk = IND_TEST_TOTAL_BLKS + 10;
+ handle_t *handle;
+ int count, ret;
+
+ handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 8);
+ KUNIT_ASSERT_FALSE(test, IS_ERR(handle));
+
+ /* Populated IND block 300 mapping IND_BASE -> data block 400 */
+ ind_blk_ptrs(test, 300)[0] = cpu_to_le32(400);
+ EXT4_I(inode)->i_data[EXT4_IND_BLOCK] = cpu_to_le32(300);
+
+ /* 1. sb_getblk() of the IND block fails -> -ENOMEM, IND preserved */
+ ctx->getblk_fail_blk = 300;
+ ctx->alloc_calls = 0;
+ memset(&map, 0, sizeof(map));
+ map.m_lblk = IND_TEST_IND_BASE + 1;
+ map.m_len = 1;
+ down_write(&EXT4_I(inode)->i_data_sem);
+ ret = ext4_ind_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
+ up_write(&EXT4_I(inode)->i_data_sem);
+ ctx->getblk_fail_blk = 0;
+ KUNIT_EXPECT_EQ(test, ret, -ENOMEM);
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(EXT4_I(inode)->i_data[EXT4_IND_BLOCK]),
+ 300U);
+ KUNIT_EXPECT_EQ(test, ctx->alloc_calls, 0U);
+ KUNIT_EXPECT_FALSE(test, map.m_flags & EXT4_MAP_NEW);
+
+ /* 2. Out-of-range IND pointer -> -EFSCORRUPTED, pointer preserved */
+ EXT4_I(inode)->i_data[EXT4_IND_BLOCK] = cpu_to_le32(bad_blk);
+ ctx->alloc_calls = 0;
+ memset(&map, 0, sizeof(map));
+ map.m_lblk = IND_TEST_IND_BASE;
+ map.m_len = 1;
+ down_write(&EXT4_I(inode)->i_data_sem);
+ ret = ext4_ind_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
+ up_write(&EXT4_I(inode)->i_data_sem);
+ KUNIT_EXPECT_EQ(test, ret, -EFSCORRUPTED);
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(EXT4_I(inode)->i_data[EXT4_IND_BLOCK]),
+ (u32)bad_blk);
+ KUNIT_EXPECT_EQ(test, ctx->alloc_calls, 0U);
+
+ /*
+ * 3. bigalloc rejection after ext4_get_branch() has read the IND
+ * block must drop the IND buffer_head reference it holds.
+ */
+ EXT4_I(inode)->i_data[EXT4_IND_BLOCK] = cpu_to_le32(300);
+ ind_bh = ind_lookup_bh(ctx, 300);
+ KUNIT_ASSERT_NOT_NULL(test, ind_bh);
+ count = atomic_read(&ind_bh->b_count);
+ ctx->alloc_calls = 0;
+ ext4_set_feature_bigalloc(sb);
+ memset(&map, 0, sizeof(map));
+ map.m_lblk = IND_TEST_IND_BASE + 1;
+ map.m_len = 1;
+ down_write(&EXT4_I(inode)->i_data_sem);
+ ret = ext4_ind_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
+ up_write(&EXT4_I(inode)->i_data_sem);
+ ext4_clear_feature_bigalloc(sb);
+ KUNIT_EXPECT_EQ(test, ret, -EFSCORRUPTED);
+ KUNIT_EXPECT_EQ(test, atomic_read(&ind_bh->b_count), count);
+ KUNIT_EXPECT_EQ(test, ctx->alloc_calls, 0U);
+
+ EXT4_I(inode)->i_data[EXT4_IND_BLOCK] = 0;
+ ext4_journal_stop(handle);
+ iput(inode);
+}
+
static struct kunit_case ext4_indirect_test_cases[] = {
KUNIT_CASE(test_block_to_path_and_trans_blocks),
KUNIT_CASE(test_ind_map_blocks_lookup_mapped_and_holes),
@@ -1255,6 +1340,7 @@ static struct kunit_case ext4_indirect_test_cases[] = {
KUNIT_CASE(test_ind_truncate_partial_and_full),
KUNIT_CASE(test_ind_remove_space_levels_and_convergence),
KUNIT_CASE(test_ind_free_corrupt_block_numbers),
+ KUNIT_CASE(test_ind_map_blocks_create_branch_errors),
{}
};
diff --git a/fs/ext4/indirect.c b/fs/ext4/indirect.c
index 5b2b2be73a90..c1038af04b05 100644
--- a/fs/ext4/indirect.c
+++ b/fs/ext4/indirect.c
@@ -135,6 +135,10 @@ static int ext4_block_to_path(struct inode *inode,
* (pointer to last triple returned, *@err == 0)
* or when it gets an IO error reading an indirect block
* (ditto, *@err == -EIO)
+ * or when it finds a corrupted indirect block reference
+ * (ditto, *@err == -EFSCORRUPTED)
+ * or when it fails to get a buffer for an indirect block
+ * (ditto, *@err == -ENOMEM)
* or when it reads all @depth-1 indirect blocks successfully and finds
* the whole chain, all way to the data (returns %NULL, *err == 0).
*
@@ -177,6 +181,7 @@ static Indirect *ext4_get_branch(struct inode *inode, int depth,
/* validate block references */
if (ext4_check_indirect_blockref(inode, bh)) {
put_bh(bh);
+ ret = -EFSCORRUPTED;
goto failure;
}
}
@@ -572,6 +577,14 @@ int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
goto got_it;
}
+ /*
+ * Failed to read the indirect branch (-EIO, -ENOMEM, -EFSCORRUPTED).
+ * Never treat this as a hole: allocating here would overwrite the
+ * existing, still referenced, indirect block pointer.
+ */
+ if (err)
+ goto cleanup;
+
/* Next simple case - plain lookup failed */
if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
unsigned epb = inode->i_sb->s_blocksize / sizeof(u32);
@@ -593,10 +606,6 @@ int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
goto cleanup;
}
- /* Failed read of indirect block */
- if (err == -EIO)
- goto cleanup;
-
/*
* Okay, we need to do block allocation.
*/
@@ -604,7 +613,7 @@ int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
EXT4_ERROR_INODE(inode, "Can't allocate blocks for "
"non-extent mapped inodes with bigalloc");
err = -EFSCORRUPTED;
- goto out;
+ goto cleanup;
}
/* Set up for the direct block allocation */
--
2.56.0.542.g3ef89b7020-goog
next prev parent reply other threads:[~2026-11-12 4:47 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 4:46 [PATCH 00/23] ext4: lots of misc bug fixes Theodore Ts'o
2026-11-12 4:46 ` [PATCH 01/23] ext4: only update s_mount_opts when EXT4_TUNE_FL_MOUNT_OPTS is set Theodore Ts'o
2026-11-12 4:46 ` [PATCH 02/23] ext4: return -EFSBADCRC when skipping a corrupt backup superblock Theodore Ts'o
2026-11-12 4:46 ` [PATCH 03/23] ext4: don't allow enabling casefold via EXT4_IOC_SET_TUNE_SB_PARAM Theodore Ts'o
2026-11-12 4:46 ` [PATCH 04/23] ext4: persist the htree hash seed when enabling dir_index online Theodore Ts'o
2026-11-12 4:46 ` [PATCH 05/23] ext4: enforce feature prerequisites when tuning a mounted fs Theodore Ts'o
2026-11-12 4:46 ` [PATCH 06/23] ext4: handle s_lastcheck_hi in the get/set tune superblock ioctls Theodore Ts'o
2026-11-12 4:46 ` [PATCH 07/23] ext4: pass missing ioctls through ext4_compat_ioctl Theodore Ts'o
2026-11-12 4:46 ` [PATCH 08/23] ext4: fix inverted err_report_sec check in ext4_update_super() Theodore Ts'o
2026-11-12 4:46 ` [PATCH 09/23] ext4: fix err_report_sec_store() timer and value handling Theodore Ts'o
2026-11-12 4:46 ` [PATCH 10/23] ext4: fix inode table accounting in count_overhead() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 11/23] ext4: handle flex groups straddling s_first_meta_bg in online resize Theodore Ts'o
2026-11-12 4:47 ` [PATCH 12/23] ext4: reject EXT4_IOC_GROUP_ADD inputs larger than a block group Theodore Ts'o
2026-11-12 4:47 ` [PATCH 13/23] ext4: propagate ext4_alloc_group_tables() errors from ext4_resize_fs() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 14/23] ext4: reject online shrink before probing the device size Theodore Ts'o
2026-11-12 4:47 ` [PATCH 15/23] ext4: cap migrated extents at EXT_INIT_MAX_LEN Theodore Ts'o
2026-11-12 4:47 ` [PATCH 16/23] ext4: don't treat a handle restart as an error in finish_range() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 17/23] ext4: don't clobber the migration result in ext4_ext_migrate() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 18/23] ext4: fix EOF clamping of the range in mext_check_adjust_range() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 19/23] ext4: bound the MMP update and check intervals Theodore Ts'o
2026-11-12 4:47 ` [PATCH 20/23] ext4: fix MMP claim of a cleanly unmounted filesystem Theodore Ts'o
2026-11-12 4:47 ` [PATCH 21/23] ext4: fix device comparison overflow in ext4_getfsmap_dev_compare() Theodore Ts'o
2026-11-12 4:47 ` Theodore Ts'o [this message]
2026-11-12 4:47 ` [PATCH 23/23] ext4: don't stop EXT4_IOC_GET_ES_CACHE at uncached ranges Theodore Ts'o
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=20261112044712.1577296-23-tytso@mit.edu \
--to=tytso@mit.edu \
--cc=linux-ext4@vger.kernel.org \
/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