Skip to content

Commit cdce59a

Browse files
riteshharjanitytso
authored andcommitted
ext4: fix error handling in ext4_fc_record_modified_inode()
Current code does not fully takes care of krealloc() error case, which could lead to silent memory corruption or a kernel bug. This patch fixes that. Also it cleans up some duplicated error handling logic from various functions in fast_commit.c file. Reported-by: luo penghao <[email protected]> Suggested-by: Lukas Czerner <[email protected]> Signed-off-by: Ritesh Harjani <[email protected]> Reviewed-by: Jan Kara <[email protected]> Link: https://lore.kernel.org/r/62e8b6a1cce9359682051deb736a3c0953c9d1e9.1642416995.git.riteshh@linux.ibm.com Signed-off-by: Theodore Ts'o <[email protected]> Cc: [email protected]
1 parent 09355d9 commit cdce59a

File tree

1 file changed

+29
-35
lines changed

1 file changed

+29
-35
lines changed

fs/ext4/fast_commit.c

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,14 +1410,15 @@ static int ext4_fc_record_modified_inode(struct super_block *sb, int ino)
14101410
if (state->fc_modified_inodes[i] == ino)
14111411
return 0;
14121412
if (state->fc_modified_inodes_used == state->fc_modified_inodes_size) {
1413-
state->fc_modified_inodes_size +=
1414-
EXT4_FC_REPLAY_REALLOC_INCREMENT;
14151413
state->fc_modified_inodes = krealloc(
1416-
state->fc_modified_inodes, sizeof(int) *
1417-
state->fc_modified_inodes_size,
1418-
GFP_KERNEL);
1414+
state->fc_modified_inodes,
1415+
sizeof(int) * (state->fc_modified_inodes_size +
1416+
EXT4_FC_REPLAY_REALLOC_INCREMENT),
1417+
GFP_KERNEL);
14191418
if (!state->fc_modified_inodes)
14201419
return -ENOMEM;
1420+
state->fc_modified_inodes_size +=
1421+
EXT4_FC_REPLAY_REALLOC_INCREMENT;
14211422
}
14221423
state->fc_modified_inodes[state->fc_modified_inodes_used++] = ino;
14231424
return 0;
@@ -1449,7 +1450,9 @@ static int ext4_fc_replay_inode(struct super_block *sb, struct ext4_fc_tl *tl,
14491450
}
14501451
inode = NULL;
14511452

1452-
ext4_fc_record_modified_inode(sb, ino);
1453+
ret = ext4_fc_record_modified_inode(sb, ino);
1454+
if (ret)
1455+
goto out;
14531456

14541457
raw_fc_inode = (struct ext4_inode *)
14551458
(val + offsetof(struct ext4_fc_inode, fc_raw_inode));
@@ -1649,6 +1652,8 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
16491652
}
16501653

16511654
ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
1655+
if (ret)
1656+
goto out;
16521657

16531658
start = le32_to_cpu(ex->ee_block);
16541659
start_pblk = ext4_ext_pblock(ex);
@@ -1666,18 +1671,14 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
16661671
map.m_pblk = 0;
16671672
ret = ext4_map_blocks(NULL, inode, &map, 0);
16681673

1669-
if (ret < 0) {
1670-
iput(inode);
1671-
return 0;
1672-
}
1674+
if (ret < 0)
1675+
goto out;
16731676

16741677
if (ret == 0) {
16751678
/* Range is not mapped */
16761679
path = ext4_find_extent(inode, cur, NULL, 0);
1677-
if (IS_ERR(path)) {
1678-
iput(inode);
1679-
return 0;
1680-
}
1680+
if (IS_ERR(path))
1681+
goto out;
16811682
memset(&newex, 0, sizeof(newex));
16821683
newex.ee_block = cpu_to_le32(cur);
16831684
ext4_ext_store_pblock(
@@ -1691,10 +1692,8 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
16911692
up_write((&EXT4_I(inode)->i_data_sem));
16921693
ext4_ext_drop_refs(path);
16931694
kfree(path);
1694-
if (ret) {
1695-
iput(inode);
1696-
return 0;
1697-
}
1695+
if (ret)
1696+
goto out;
16981697
goto next;
16991698
}
17001699

@@ -1707,10 +1706,8 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
17071706
ret = ext4_ext_replay_update_ex(inode, cur, map.m_len,
17081707
ext4_ext_is_unwritten(ex),
17091708
start_pblk + cur - start);
1710-
if (ret) {
1711-
iput(inode);
1712-
return 0;
1713-
}
1709+
if (ret)
1710+
goto out;
17141711
/*
17151712
* Mark the old blocks as free since they aren't used
17161713
* anymore. We maintain an array of all the modified
@@ -1730,10 +1727,8 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
17301727
ext4_ext_is_unwritten(ex), map.m_pblk);
17311728
ret = ext4_ext_replay_update_ex(inode, cur, map.m_len,
17321729
ext4_ext_is_unwritten(ex), map.m_pblk);
1733-
if (ret) {
1734-
iput(inode);
1735-
return 0;
1736-
}
1730+
if (ret)
1731+
goto out;
17371732
/*
17381733
* We may have split the extent tree while toggling the state.
17391734
* Try to shrink the extent tree now.
@@ -1745,6 +1740,7 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
17451740
}
17461741
ext4_ext_replay_shrink_inode(inode, i_size_read(inode) >>
17471742
sb->s_blocksize_bits);
1743+
out:
17481744
iput(inode);
17491745
return 0;
17501746
}
@@ -1774,6 +1770,8 @@ ext4_fc_replay_del_range(struct super_block *sb, struct ext4_fc_tl *tl,
17741770
}
17751771

17761772
ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
1773+
if (ret)
1774+
goto out;
17771775

17781776
jbd_debug(1, "DEL_RANGE, inode %ld, lblk %d, len %d\n",
17791777
inode->i_ino, le32_to_cpu(lrange.fc_lblk),
@@ -1783,10 +1781,8 @@ ext4_fc_replay_del_range(struct super_block *sb, struct ext4_fc_tl *tl,
17831781
map.m_len = remaining;
17841782

17851783
ret = ext4_map_blocks(NULL, inode, &map, 0);
1786-
if (ret < 0) {
1787-
iput(inode);
1788-
return 0;
1789-
}
1784+
if (ret < 0)
1785+
goto out;
17901786
if (ret > 0) {
17911787
remaining -= ret;
17921788
cur += ret;
@@ -1801,15 +1797,13 @@ ext4_fc_replay_del_range(struct super_block *sb, struct ext4_fc_tl *tl,
18011797
ret = ext4_ext_remove_space(inode, lrange.fc_lblk,
18021798
lrange.fc_lblk + lrange.fc_len - 1);
18031799
up_write(&EXT4_I(inode)->i_data_sem);
1804-
if (ret) {
1805-
iput(inode);
1806-
return 0;
1807-
}
1800+
if (ret)
1801+
goto out;
18081802
ext4_ext_replay_shrink_inode(inode,
18091803
i_size_read(inode) >> sb->s_blocksize_bits);
18101804
ext4_mark_inode_dirty(NULL, inode);
1805+
out:
18111806
iput(inode);
1812-
18131807
return 0;
18141808
}
18151809

0 commit comments

Comments
 (0)