Fix two potential memory leak
cover Fix two potential memory leak
Ye Bin (2):
ext4: fix potential memory leak in ext4_fc_record_regions()
ext4: fix potential memory leak in ext4_fc_record_modified_inode()
fs/ext4/fast_commit.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/2 ext4: fix potential memory leak in ext4_fc_record_regions()
As krealloc may return NULL, in this case 'state->fc_regions' may not be freed by krealloc, but 'state->fc_regions' already set NULL. Then will lead to 'state->fc_regions' memory leak.
--- fs/ext4/fast_commit.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
fs/ext4/fast_commit.c +8 -6
@@ -1677,15 +1677,17 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,if (replay && state->fc_regions_used != state->fc_regions_valid) state->fc_regions_used = state->fc_regions_valid;if (state->fc_regions_used == state->fc_regions_size) { +struct ext4_fc_alloc_region *fc_regions; + state->fc_regions_size +=EXT4_FC_REPLAY_REALLOC_INCREMENT ; - state->fc_regions =krealloc ( - state->fc_regions, - state->fc_regions_size * -sizeof (struct ext4_fc_alloc_region ), -GFP_KERNEL ); -if (!state->fc_regions) + fc_regions =krealloc (state->fc_regions, + state->fc_regions_size * +sizeof (struct ext4_fc_alloc_region ), +GFP_KERNEL ); +if (!fc_regions)return -ENOMEM ; + state->fc_regions = fc_regions; } region = &state->fc_regions[state->fc_regions_used++]; region->ino = ino;
Signature · 1 line
2.31.1
Re: [PATCH -next 1/2] ext4: fix potential memory leak in ext4_fc_record_regions()
Quoted 5 lines · Ye Bin
As krealloc may return NULL, in this case '
state->fc_regions' may not be freed by krealloc, but 'state->fc_regions' already set NULL. Then will lead to 'state->fc_regions' memory leak.Signed-off-by Ye Bin <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 35 lines
--- fs/ext4/fast_commit.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)fs/ext4/fast_commit.c +8
-6@@ -1677,15 +1677,17 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,if (replay && state->fc_regions_used != state->fc_regions_valid) state->fc_regions_used = state->fc_regions_valid;if (state->fc_regions_used == state->fc_regions_size) { +struct ext4_fc_alloc_region *fc_regions; + state->fc_regions_size +=EXT4_FC_REPLAY_REALLOC_INCREMENT ; - state->fc_regions =krealloc ( - state->fc_regions, - state->fc_regions_size * -sizeof (struct ext4_fc_alloc_region ), -GFP_KERNEL ); -if (!state->fc_regions) + fc_regions =krealloc (state->fc_regions, + state->fc_regions_size * +sizeof (struct ext4_fc_alloc_region ), +GFP_KERNEL ); +if (!fc_regions)return -ENOMEM ; + state->fc_regions = fc_regions; } region = &state->fc_regions[state->fc_regions_used++]; region->ino = ino;Signature · 1 line
2.31.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH -next 1/2] ext4: fix potential memory leak in ext4_fc_record_regions()
In reply to Ye BinHello,
Le 19/09/2022 à 16:40, Ye Bin a écrit :
Quoted 22 lines
As krealloc may return NULL, in this case '
state->fc_regions' may not be freed by krealloc, but 'state->fc_regions' already set NULL. Then will lead to 'state->fc_regions' memory leak.Signed-off-by Ye Bin <[email protected]>--- fs/ext4/fast_commit.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)fs/ext4/fast_commit.c +2
-1@@ -1677,15 +1677,17 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,if (replay && state->fc_regions_used != state->fc_regions_valid) state->fc_regions_used = state->fc_regions_valid;if (state->fc_regions_used == state->fc_regions_size) { +struct ext4_fc_alloc_region *fc_regions; + state->fc_regions_size +=EXT4_FC_REPLAY_REALLOC_INCREMENT ; - state->fc_regions = krealloc(
fs/ext4/fast_commit.c int ext4_fc_record_regions(struct super_block *sb, int ino, · line 1688
Quoted 10 lines
fs/ext4/fast_commit.c +5
-5- state->fc_regions, - state->fc_regions_size * -sizeof (struct ext4_fc_alloc_region ), - GFP_KERNEL); -if (!state->fc_regions) + fc_regions =krealloc (state->fc_regions, + state->fc_regions_size * +sizeof (struct ext4_fc_alloc_region ), +GFP_KERNEL ); +if (!fc_regions)
Would it not be safer to restore state->fc_regions_size to its previous value in that case to keep consistency between size value and allocated size (or to update state->fc_regions_size only after allocation as it is done in second part of this patch) ?
fs/ext4/fast_commit.c int ext4_fc_record_regions(struct super_block *sb, int ino, · line 1693
Quoted 5 lines
fs/ext4/fast_commit.c +1
-0return -ENOMEM ; + state->fc_regions = fc_regions; } region = &state->fc_regions[state->fc_regions_used++]; region->ino = ino;
Regards,
Damien
Re: [PATCH -next 1/2] ext4: fix potential memory leak in ext4_fc_record_regions()
Quoted 30 lines · Damien Guibouret
Hello,
Quoted 27 lines · Ye Bin
As krealloc may return NULL, in this case '
state->fc_regions' may not be freed by krealloc, but 'state->fc_regions' already set NULL. Then will lead to 'state->fc_regions' memory leak.Signed-off-by Ye Bin <[email protected]>--- fs/ext4/fast_commit.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)fs/ext4/fast_commit.c +2
-5@@ -1677,15 +1677,17 @@ int ext4_fc_record_regions(struct super_block *sb, int ino, if (replay && state->fc_regions_used != state->fc_regions_valid) state->fc_regions_used = state->fc_regions_valid;if (state->fc_regions_used == state->fc_regions_size) { +struct ext4_fc_alloc_region *fc_regions; + state->fc_regions_size +=EXT4_FC_REPLAY_REALLOC_INCREMENT ; - state->fc_regions =krealloc ( - state->fc_regions, - state->fc_regions_size * -sizeof (struct ext4_fc_alloc_region ), -GFP_KERNEL );
Quoted 9 lines
fs/ext4/fast_commit.c int ext4_fc_record_regions(struct super_block *sb, int ino, · line 1688
Quoted 6 lines
fs/ext4/fast_commit.c +5
-1-if (!state->fc_regions) + fc_regions =krealloc (state->fc_regions, + state->fc_regions_size * +sizeof (struct ext4_fc_alloc_region ), +GFP_KERNEL ); +if (!fc_regions)Would it not be safer to restore
state->fc_regions_sizeto its previous value in that case to keep consistency between size value and allocated size (or to updatestate->fc_regions_sizeonly after allocation as it is done in second part of this patch) ?
Actually, If 'ext4_fc_record_regions()' return -ENOMEM, then will stop replay journal.
'state->fc_regions_size' will not be used any more, so it's safe.
Quoted 12 lines
Quoted 5 lines
fs/ext4/fast_commit.c +1
-0return -ENOMEM ; + state->fc_regions = fc_regions; } region = &state->fc_regions[state->fc_regions_used++];region->ino = ino;Regards,
Damien
.
Re: [PATCH -next 1/2] ext4: fix potential memory leak in ext4_fc_record_regions()
Hello,
Le 20/09/2022 à 03:07, yebin a écrit :
Quoted 42 lines · Damien Guibouret
Quoted 30 lines
Hello,
Quoted 27 lines · Ye Bin
As krealloc may return NULL, in this case '
state->fc_regions' may not be freed by krealloc, but 'state->fc_regions' already set NULL. Then will lead to 'state->fc_regions' memory leak.Signed-off-by Ye Bin <[email protected]>---
fs/ext4/fast_commit.c| 14 ++++++++------1 file changed, 8 insertions(+), 6 deletions(-)fs/ext4/fast_commit.c +2
-0@@ -1677,15 +1677,17 @@ int ext4_fc_record_regions(struct super_block *sb, int ino, if (replay && state->fc_regions_used != state->fc_regions_valid) state->fc_regions_used = state->fc_regions_valid; if (state->fc_regions_used == state->fc_regions_size) { + struct ext4_fc_alloc_region *fc_regions; +
state->fc_regions_size+=EXT4_FC_REPLAY_REALLOC_INCREMENT; -state->fc_regions= krealloc(
-state->fc_regions,
-state->fc_regions_size*
-sizeof(struct ext4_fc_alloc_region), -GFP_KERNEL);Quoted 9 lines
Quoted 6 lines
- if (!state->fc_regions)
+fc_regions= krealloc(state->fc_regions, +state->fc_regions_size*
+sizeof(struct ext4_fc_alloc_region), +GFP_KERNEL);
+ if (!fc_regions)Would it not be safer to restore
state->fc_regions_sizeto its previous value in that case to keep consistency between size value and allocated size (or to updatestate->fc_regions_sizeonly after allocation as it is done in second part of this patch) ?
Quoted 2 lines
Actually, If '
ext4_fc_record_regions()' return-ENOMEM, then will stop replay journal. 'state->fc_regions_size' will not be used any more, so it's safe.
There are at least two calls in ext4_ext_clear_bb (ext4/extents.c) that do not check for return code of ext4_fc_record_regions. But perhaps these are these calls that should be fixed.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
return
-ENOMEM;
+state->fc_regions=fc_regions;
}
region = &state->fc_regions[state->fc_regions_used++];region->ino= ino;
Regards,
Damien
Re: [PATCH -next 1/2] ext4: fix potential memory leak in ext4_fc_record_regions()
Quoted 45 lines · Damien Guibouret
Hello,
Quoted 42 lines · yebin
Quoted 39 lines · Damien Guibouret
Hello,
Quoted 23 lines · Ye Bin
As krealloc may return NULL, in this case '
state->fc_regions' may not be freed by krealloc, but 'state->fc_regions' already set NULL. Then will lead to 'state->fc_regions' memory leak.Signed-off-by Ye Bin <[email protected]>--- fs/ext4/fast_commit.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)fs/ext4/fast_commit.c +2
-1@@ -1677,15 +1677,17 @@ int ext4_fc_record_regions(struct super_block *sb, int ino, if (replay && state->fc_regions_used != state->fc_regions_valid) state->fc_regions_used = state->fc_regions_valid;if (state->fc_regions_used == state->fc_regions_size) { +struct ext4_fc_alloc_region *fc_regions; + state->fc_regions_size +=EXT4_FC_REPLAY_REALLOC_INCREMENT ; - state->fc_regions = krealloc(fs/ext4/fast_commit.c int ext4_fc_record_regions(struct · line 1689
Quoted 10 lines
fs/ext4/fast_commit.c +5
-5- state->fc_regions, - state->fc_regions_size * -sizeof (struct ext4_fc_alloc_region ), - GFP_KERNEL); -if (!state->fc_regions) + fc_regions =krealloc (state->fc_regions, + state->fc_regions_size * +sizeof (struct ext4_fc_alloc_region ), +GFP_KERNEL ); +if (!fc_regions)Would it not be safer to restore
state->fc_regions_sizeto its previous value in that case to keep consistency between size value and allocated size (or to updatestate->fc_regions_sizeonly after allocation as it is done in second part of this patch) ?
Quoted 5 lines
Quoted 2 lines
Actually, If '
ext4_fc_record_regions()' return-ENOMEM, then will stop replay journal.
'state->fc_regions_size' will not be used any more, so it's safe.There are at least two calls in
ext4_ext_clear_bb(ext4/extents.c) that do not check for return code ofext4_fc_record_regions. But perhaps these are these calls that should be fixed.
Thanks very much,
Indeed, it's better to repair it here.
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
fs/ext4/fast_commit.c +1
-0return -ENOMEM ; + state->fc_regions = fc_regions; } region = &state->fc_regions[state->fc_regions_used++];region->ino = ino;Regards,
Damien
.
PATCH 2/2 ext4: fix potential memory leak in ext4_fc_record_modified_inode()
In reply to Ye BinAs krealloc may return NULL, in this case 'state->fc_modified_inodes' may not be freed by krealloc, but 'state->fc_modified_inodes' already set NULL. Then will lead to 'state->fc_modified_inodes' memory leak.
--- fs/ext4/fast_commit.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
fs/ext4/fast_commit.c +5 -3
@@ -1486,13 +1486,15 @@ static int ext4_fc_record_modified_inode(struct super_block *sb, int ino)if (state->fc_modified_inodes[i] == ino)return 0 ;if (state->fc_modified_inodes_used == state->fc_modified_inodes_size) { - state->fc_modified_inodes =krealloc ( - state->fc_modified_inodes, +int *fc_modified_inodes; + + fc_modified_inodes =krealloc (state->fc_modified_inodes,sizeof (int ) * (state->fc_modified_inodes_size +EXT4_FC_REPLAY_REALLOC_INCREMENT ),GFP_KERNEL ); -if (!state->fc_modified_inodes) +if (!fc_modified_inodes)return -ENOMEM ; + state->fc_modified_inodes = fc_modified_inodes; state->fc_modified_inodes_size +=EXT4_FC_REPLAY_REALLOC_INCREMENT ; }
Signature · 1 line
2.31.1
Re: [PATCH -next 2/2] ext4: fix potential memory leak in ext4_fc_record_modified_inode()
Quoted 5 lines · Ye Bin
As krealloc may return NULL, in this case '
state->fc_modified_inodes' may not be freed by krealloc, but 'state->fc_modified_inodes' already set NULL. Then will lead to 'state->fc_modified_inodes' memory leak.Signed-off-by Ye Bin <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 30 lines
--- fs/ext4/fast_commit.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)fs/ext4/fast_commit.c +5
-3@@ -1486,13 +1486,15 @@ static int ext4_fc_record_modified_inode(struct super_block *sb, int ino)if (state->fc_modified_inodes[i] == ino)return 0 ;if (state->fc_modified_inodes_used == state->fc_modified_inodes_size) { - state->fc_modified_inodes =krealloc ( - state->fc_modified_inodes, +int *fc_modified_inodes; + + fc_modified_inodes =krealloc (state->fc_modified_inodes,sizeof (int ) * (state->fc_modified_inodes_size +EXT4_FC_REPLAY_REALLOC_INCREMENT ),GFP_KERNEL ); -if (!state->fc_modified_inodes) +if (!fc_modified_inodes)return -ENOMEM ; + state->fc_modified_inodes = fc_modified_inodes; state->fc_modified_inodes_size +=EXT4_FC_REPLAY_REALLOC_INCREMENT ; }Signature · 1 line
2.31.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR