Skip to content

Commit 62e46e0

Browse files
riteshharjanigregkh
authored andcommitted
ext4: fix error handling in ext4_fc_record_modified_inode()
commit cdce59a upstream. 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 <luo.penghao@zte.com.cn> Suggested-by: Lukas Czerner <lczerner@redhat.com> Signed-off-by: Ritesh Harjani <riteshh@linux.ibm.com> Reviewed-by: Jan Kara <jack@suse.cz> Link: https://lore.kernel.org/r/62e8b6a1cce9359682051deb736a3c0953c9d1e9.1642416995.git.riteshh@linux.ibm.com Signed-off-by: Theodore Ts'o <tytso@mit.edu> Cc: stable@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 764793b commit 62e46e0

1 file changed

Lines changed: 29 additions & 35 deletions

File tree

fs/ext4/fast_commit.c

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,14 +1388,15 @@ static int ext4_fc_record_modified_inode(struct super_block *sb, int ino)
13881388
if (state->fc_modified_inodes[i] == ino)
13891389
return 0;
13901390
if (state->fc_modified_inodes_used == state->fc_modified_inodes_size) {
1391-
state->fc_modified_inodes_size +=
1392-
EXT4_FC_REPLAY_REALLOC_INCREMENT;
13931391
state->fc_modified_inodes = krealloc(
1394-
state->fc_modified_inodes, sizeof(int) *
1395-
state->fc_modified_inodes_size,
1396-
GFP_KERNEL);
1392+
state->fc_modified_inodes,
1393+
sizeof(int) * (state->fc_modified_inodes_size +
1394+
EXT4_FC_REPLAY_REALLOC_INCREMENT),
1395+
GFP_KERNEL);
13971396
if (!state->fc_modified_inodes)
13981397
return -ENOMEM;
1398+
state->fc_modified_inodes_size +=
1399+
EXT4_FC_REPLAY_REALLOC_INCREMENT;
13991400
}
14001401
state->fc_modified_inodes[state->fc_modified_inodes_used++] = ino;
14011402
return 0;
@@ -1427,7 +1428,9 @@ static int ext4_fc_replay_inode(struct super_block *sb, struct ext4_fc_tl *tl,
14271428
}
14281429
inode = NULL;
14291430

1430-
ext4_fc_record_modified_inode(sb, ino);
1431+
ret = ext4_fc_record_modified_inode(sb, ino);
1432+
if (ret)
1433+
goto out;
14311434

14321435
raw_fc_inode = (struct ext4_inode *)
14331436
(val + offsetof(struct ext4_fc_inode, fc_raw_inode));
@@ -1626,6 +1629,8 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
16261629
}
16271630

16281631
ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
1632+
if (ret)
1633+
goto out;
16291634

16301635
start = le32_to_cpu(ex->ee_block);
16311636
start_pblk = ext4_ext_pblock(ex);
@@ -1643,18 +1648,14 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
16431648
map.m_pblk = 0;
16441649
ret = ext4_map_blocks(NULL, inode, &map, 0);
16451650

1646-
if (ret < 0) {
1647-
iput(inode);
1648-
return 0;
1649-
}
1651+
if (ret < 0)
1652+
goto out;
16501653

16511654
if (ret == 0) {
16521655
/* Range is not mapped */
16531656
path = ext4_find_extent(inode, cur, NULL, 0);
1654-
if (IS_ERR(path)) {
1655-
iput(inode);
1656-
return 0;
1657-
}
1657+
if (IS_ERR(path))
1658+
goto out;
16581659
memset(&newex, 0, sizeof(newex));
16591660
newex.ee_block = cpu_to_le32(cur);
16601661
ext4_ext_store_pblock(
@@ -1668,10 +1669,8 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
16681669
up_write((&EXT4_I(inode)->i_data_sem));
16691670
ext4_ext_drop_refs(path);
16701671
kfree(path);
1671-
if (ret) {
1672-
iput(inode);
1673-
return 0;
1674-
}
1672+
if (ret)
1673+
goto out;
16751674
goto next;
16761675
}
16771676

@@ -1684,10 +1683,8 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
16841683
ret = ext4_ext_replay_update_ex(inode, cur, map.m_len,
16851684
ext4_ext_is_unwritten(ex),
16861685
start_pblk + cur - start);
1687-
if (ret) {
1688-
iput(inode);
1689-
return 0;
1690-
}
1686+
if (ret)
1687+
goto out;
16911688
/*
16921689
* Mark the old blocks as free since they aren't used
16931690
* anymore. We maintain an array of all the modified
@@ -1707,10 +1704,8 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
17071704
ext4_ext_is_unwritten(ex), map.m_pblk);
17081705
ret = ext4_ext_replay_update_ex(inode, cur, map.m_len,
17091706
ext4_ext_is_unwritten(ex), map.m_pblk);
1710-
if (ret) {
1711-
iput(inode);
1712-
return 0;
1713-
}
1707+
if (ret)
1708+
goto out;
17141709
/*
17151710
* We may have split the extent tree while toggling the state.
17161711
* Try to shrink the extent tree now.
@@ -1722,6 +1717,7 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
17221717
}
17231718
ext4_ext_replay_shrink_inode(inode, i_size_read(inode) >>
17241719
sb->s_blocksize_bits);
1720+
out:
17251721
iput(inode);
17261722
return 0;
17271723
}
@@ -1751,6 +1747,8 @@ ext4_fc_replay_del_range(struct super_block *sb, struct ext4_fc_tl *tl,
17511747
}
17521748

17531749
ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
1750+
if (ret)
1751+
goto out;
17541752

17551753
jbd_debug(1, "DEL_RANGE, inode %ld, lblk %d, len %d\n",
17561754
inode->i_ino, le32_to_cpu(lrange.fc_lblk),
@@ -1760,10 +1758,8 @@ ext4_fc_replay_del_range(struct super_block *sb, struct ext4_fc_tl *tl,
17601758
map.m_len = remaining;
17611759

17621760
ret = ext4_map_blocks(NULL, inode, &map, 0);
1763-
if (ret < 0) {
1764-
iput(inode);
1765-
return 0;
1766-
}
1761+
if (ret < 0)
1762+
goto out;
17671763
if (ret > 0) {
17681764
remaining -= ret;
17691765
cur += ret;
@@ -1778,15 +1774,13 @@ ext4_fc_replay_del_range(struct super_block *sb, struct ext4_fc_tl *tl,
17781774
ret = ext4_ext_remove_space(inode, lrange.fc_lblk,
17791775
lrange.fc_lblk + lrange.fc_len - 1);
17801776
up_write(&EXT4_I(inode)->i_data_sem);
1781-
if (ret) {
1782-
iput(inode);
1783-
return 0;
1784-
}
1777+
if (ret)
1778+
goto out;
17851779
ext4_ext_replay_shrink_inode(inode,
17861780
i_size_read(inode) >> sb->s_blocksize_bits);
17871781
ext4_mark_inode_dirty(NULL, inode);
1782+
out:
17881783
iput(inode);
1789-
17901784
return 0;
17911785
}
17921786

0 commit comments

Comments
 (0)