This is a note to let you know that I've just added the patch titled ext4: fix potential out of bound read in ext4_fc_replay_scan() to the 5.15-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: ext4-fix-potential-out-of-bound-read-in-ext4_fc_replay_scan.patch and it can be found in the queue-5.15 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From stable-owner@xxxxxxxxxxxxxxx Thu Jan 5 08:17:15 2023 From: Eric Biggers <ebiggers@xxxxxxxxxx> Date: Wed, 4 Jan 2023 23:13:54 -0800 Subject: ext4: fix potential out of bound read in ext4_fc_replay_scan() To: stable@xxxxxxxxxxxxxxx Cc: linux-ext4@xxxxxxxxxxxxxxx, stable@xxxxxxxxxx, Ye Bin <yebin10@xxxxxxxxxx>, Theodore Ts'o <tytso@xxxxxxx> Message-ID: <20230105071359.257952-6-ebiggers@xxxxxxxxxx> From: Eric Biggers <ebiggers@xxxxxxxxxx> From: Ye Bin <yebin10@xxxxxxxxxx> commit 1b45cc5c7b920fd8bf72e5a888ec7abeadf41e09 upstream. For scan loop must ensure that at least EXT4_FC_TAG_BASE_LEN space. If remain space less than EXT4_FC_TAG_BASE_LEN which will lead to out of bound read when mounting corrupt file system image. ADD_RANGE/HEAD/TAIL is needed to add extra check when do journal scan, as this three tags will read data during scan, tag length couldn't less than data length which will read. Cc: stable@xxxxxxxxxx Signed-off-by: Ye Bin <yebin10@xxxxxxxxxx> Link: https://lore.kernel.org/r/20220924075233.2315259-4-yebin10@xxxxxxxxxx Signed-off-by: Theodore Ts'o <tytso@xxxxxxx> Signed-off-by: Eric Biggers <ebiggers@xxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- fs/ext4/fast_commit.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) --- a/fs/ext4/fast_commit.c +++ b/fs/ext4/fast_commit.c @@ -1907,6 +1907,34 @@ void ext4_fc_replay_cleanup(struct super kfree(sbi->s_fc_replay_state.fc_modified_inodes); } +static inline bool ext4_fc_tag_len_isvalid(struct ext4_fc_tl *tl, + u8 *val, u8 *end) +{ + if (val + tl->fc_len > end) + return false; + + /* Here only check ADD_RANGE/TAIL/HEAD which will read data when do + * journal rescan before do CRC check. Other tags length check will + * rely on CRC check. + */ + switch (tl->fc_tag) { + case EXT4_FC_TAG_ADD_RANGE: + return (sizeof(struct ext4_fc_add_range) == tl->fc_len); + case EXT4_FC_TAG_TAIL: + return (sizeof(struct ext4_fc_tail) <= tl->fc_len); + case EXT4_FC_TAG_HEAD: + return (sizeof(struct ext4_fc_head) == tl->fc_len); + case EXT4_FC_TAG_DEL_RANGE: + case EXT4_FC_TAG_LINK: + case EXT4_FC_TAG_UNLINK: + case EXT4_FC_TAG_CREAT: + case EXT4_FC_TAG_INODE: + case EXT4_FC_TAG_PAD: + default: + return true; + } +} + /* * Recovery Scan phase handler * @@ -1963,10 +1991,15 @@ static int ext4_fc_replay_scan(journal_t } state->fc_replay_expected_off++; - for (cur = start; cur < end; + for (cur = start; cur < end - EXT4_FC_TAG_BASE_LEN; cur = cur + EXT4_FC_TAG_BASE_LEN + tl.fc_len) { ext4_fc_get_tl(&tl, cur); val = cur + EXT4_FC_TAG_BASE_LEN; + if (!ext4_fc_tag_len_isvalid(&tl, val, end)) { + ret = state->fc_replay_num_tags ? + JBD2_FC_REPLAY_STOP : -ECANCELED; + goto out_err; + } ext4_debug("Scan phase, tag:%s, blk %lld\n", tag2str(tl.fc_tag), bh->b_blocknr); switch (tl.fc_tag) { @@ -2077,7 +2110,7 @@ static int ext4_fc_replay(journal_t *jou start = (u8 *)bh->b_data; end = (__u8 *)bh->b_data + journal->j_blocksize - 1; - for (cur = start; cur < end; + for (cur = start; cur < end - EXT4_FC_TAG_BASE_LEN; cur = cur + EXT4_FC_TAG_BASE_LEN + tl.fc_len) { ext4_fc_get_tl(&tl, cur); val = cur + EXT4_FC_TAG_BASE_LEN; @@ -2087,6 +2120,7 @@ static int ext4_fc_replay(journal_t *jou ext4_fc_set_bitmaps_and_counters(sb); break; } + ext4_debug("Replay phase, tag:%s\n", tag2str(tl.fc_tag)); state->fc_replay_num_tags--; switch (tl.fc_tag) { Patches currently in stable-queue which might be from stable-owner@xxxxxxxxxxxxxxx are queue-5.15/ext4-remove-unused-enum-ext4_fc_commit_failed.patch queue-5.15/ext4-fix-unaligned-memory-access-in-ext4_fc_reserve_space.patch queue-5.15/ext4-disable-fast-commit-of-encrypted-dir-operations.patch queue-5.15/ext4-introduce-ext4_fc_tag_base_len-helper.patch queue-5.15/ext4-fix-potential-out-of-bound-read-in-ext4_fc_replay_scan.patch queue-5.15/ext4-use-ext4_debug-instead-of-jbd_debug.patch queue-5.15/ext4-factor-out-ext4_fc_get_tl.patch queue-5.15/ext4-add-missing-validation-of-fast-commit-record-lengths.patch queue-5.15/ext4-fix-off-by-one-errors-in-fast-commit-block-filling.patch queue-5.15/ext4-don-t-set-up-encryption-key-during-jbd2-transaction.patch