Skip to content

Commit 1b45cc5

Browse files
Ye Bintytso
authored andcommitted
ext4: fix potential out of bound read in ext4_fc_replay_scan()
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: [email protected] Signed-off-by: Ye Bin <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent dcc5827 commit 1b45cc5

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

fs/ext4/fast_commit.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,34 @@ void ext4_fc_replay_cleanup(struct super_block *sb)
19761976
kfree(sbi->s_fc_replay_state.fc_modified_inodes);
19771977
}
19781978

1979+
static inline bool ext4_fc_tag_len_isvalid(struct ext4_fc_tl *tl,
1980+
u8 *val, u8 *end)
1981+
{
1982+
if (val + tl->fc_len > end)
1983+
return false;
1984+
1985+
/* Here only check ADD_RANGE/TAIL/HEAD which will read data when do
1986+
* journal rescan before do CRC check. Other tags length check will
1987+
* rely on CRC check.
1988+
*/
1989+
switch (tl->fc_tag) {
1990+
case EXT4_FC_TAG_ADD_RANGE:
1991+
return (sizeof(struct ext4_fc_add_range) == tl->fc_len);
1992+
case EXT4_FC_TAG_TAIL:
1993+
return (sizeof(struct ext4_fc_tail) <= tl->fc_len);
1994+
case EXT4_FC_TAG_HEAD:
1995+
return (sizeof(struct ext4_fc_head) == tl->fc_len);
1996+
case EXT4_FC_TAG_DEL_RANGE:
1997+
case EXT4_FC_TAG_LINK:
1998+
case EXT4_FC_TAG_UNLINK:
1999+
case EXT4_FC_TAG_CREAT:
2000+
case EXT4_FC_TAG_INODE:
2001+
case EXT4_FC_TAG_PAD:
2002+
default:
2003+
return true;
2004+
}
2005+
}
2006+
19792007
/*
19802008
* Recovery Scan phase handler
19812009
*
@@ -2032,10 +2060,15 @@ static int ext4_fc_replay_scan(journal_t *journal,
20322060
}
20332061

20342062
state->fc_replay_expected_off++;
2035-
for (cur = start; cur < end;
2063+
for (cur = start; cur < end - EXT4_FC_TAG_BASE_LEN;
20362064
cur = cur + EXT4_FC_TAG_BASE_LEN + tl.fc_len) {
20372065
ext4_fc_get_tl(&tl, cur);
20382066
val = cur + EXT4_FC_TAG_BASE_LEN;
2067+
if (!ext4_fc_tag_len_isvalid(&tl, val, end)) {
2068+
ret = state->fc_replay_num_tags ?
2069+
JBD2_FC_REPLAY_STOP : -ECANCELED;
2070+
goto out_err;
2071+
}
20392072
ext4_debug("Scan phase, tag:%s, blk %lld\n",
20402073
tag2str(tl.fc_tag), bh->b_blocknr);
20412074
switch (tl.fc_tag) {
@@ -2146,7 +2179,7 @@ static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh,
21462179
start = (u8 *)bh->b_data;
21472180
end = (__u8 *)bh->b_data + journal->j_blocksize - 1;
21482181

2149-
for (cur = start; cur < end;
2182+
for (cur = start; cur < end - EXT4_FC_TAG_BASE_LEN;
21502183
cur = cur + EXT4_FC_TAG_BASE_LEN + tl.fc_len) {
21512184
ext4_fc_get_tl(&tl, cur);
21522185
val = cur + EXT4_FC_TAG_BASE_LEN;
@@ -2156,6 +2189,7 @@ static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh,
21562189
ext4_fc_set_bitmaps_and_counters(sb);
21572190
break;
21582191
}
2192+
21592193
ext4_debug("Replay phase, tag:%s\n", tag2str(tl.fc_tag));
21602194
state->fc_replay_num_tags--;
21612195
switch (tl.fc_tag) {

0 commit comments

Comments
 (0)