Skip to contentSkip to conversation

Fix two potential memory leak

PATCH Applied Ye Bin 4 participants 9 messages Sep 21, 2022 – Sep 30, 2022 ext4 file system

lore
Share & tools
lore

Ye Bin

author
#0 lore raw

v2 cover Fix two potential memory leak

Diff v2 vs v1:
Add new patch "ext4: update 'state->fc_regions_size' after successful memory allocation"

Ye Bin (3):
ext4: fix potential memory leak in ext4_fc_record_modified_inode()
ext4: fix potential memory leak in ext4_fc_record_regions()
ext4: update 'state->fc_regions_size' after successful memory
allocation

 fs/ext4/fast_commit.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v2 1/3 ext4: fix potential memory leak in ext4_fc_record_modified_inode()

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.

---
 fs/ext4/fast_commit.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/ext4/fast_commit.c +5 -3
index 88575f3d0dc5..29a22ee0adb8 100644
@@ -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
reviewer Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH -next v2 1/3] 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.

Still looks good to me. I think this didn't change since last time, did it? 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
index 88575f3d0dc5..29a22ee0adb8 100644
@@ -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
author
#3 lore raw

PATCH v2 2/3 ext4: fix potential memory leak in ext4_fc_record_regions()

In reply to 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.

---
 fs/ext4/fast_commit.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
fs/ext4/fast_commit.c +8 -6
index 29a22ee0adb8..f5b0fc50ed47 100644
@@ -1679,15 +1679,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
reviewer Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH -next v2 2/3] 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.

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
index 29a22ee0adb8..f5b0fc50ed47 100644
@@ -1679,15 +1679,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
author
#5 lore raw

PATCH v2 3/3 ext4: update 'state->fc_regions_size' after successful memory allocation

In reply to Ye Bin

To avoid to 'state->fc_regions_size' mismatch with 'state->fc_regions' when fail to reallocate 'fc_reqions',only update 'state->fc_regions_size' after 'state->fc_regions' is allocated successfully.

---
 fs/ext4/fast_commit.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
fs/ext4/fast_commit.c +5 -4
index f5b0fc50ed47..694ab0627395 100644
@@ -1681,14 +1681,15 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,
 	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;
 		fc_regions = krealloc(state->fc_regions,
-				      state->fc_regions_size *
-				      sizeof(struct ext4_fc_alloc_region),
+				      sizeof(struct ext4_fc_alloc_region) *
+				      (state->fc_regions_size +
+				       EXT4_FC_REPLAY_REALLOC_INCREMENT),
 				      GFP_KERNEL);
 		if (!fc_regions)
 			return -ENOMEM;
+		state->fc_regions_size +=
+			EXT4_FC_REPLAY_REALLOC_INCREMENT;
 		state->fc_regions = fc_regions;
 	}
 	region = &state->fc_regions[state->fc_regions_used++];
Signature · 1 line
2.31.1
reviewer Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation

Quoted 5 lines · Ye Bin

To avoid to 'state->fc_regions_size' mismatch with 'state->fc_regions' when fail to reallocate 'fc_reqions',only update 'state->fc_regions_size' after 'state->fc_regions' is allocated successfully.

Looks good. Feel free to add:

Honza

Quoted 31 lines
---
 fs/ext4/fast_commit.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
fs/ext4/fast_commit.c +5 -4
index f5b0fc50ed47..694ab0627395 100644
@@ -1681,14 +1681,15 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,
 	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;
 		fc_regions = krealloc(state->fc_regions,
-				      state->fc_regions_size *
-				      sizeof(struct ext4_fc_alloc_region),
+				      sizeof(struct ext4_fc_alloc_region) *
+				      (state->fc_regions_size +
+				       EXT4_FC_REPLAY_REALLOC_INCREMENT),
 				      GFP_KERNEL);
 		if (!fc_regions)
 			return -ENOMEM;
+		state->fc_regions_size +=
+			EXT4_FC_REPLAY_REALLOC_INCREMENT;
 		state->fc_regions = fc_regions;
 	}
 	region = &state->fc_regions[state->fc_regions_used++];
Signature · 1 line
2.31.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#7 lore raw

Re: [PATCH -next v2 3/3] ext4: update 'state->fc_regions_size' after successful memory allocation

Hello,

That's fine for me.

Regards,

Damien

Le 21/09/2022 à 13:32, Jan Kara a écrit :

Quoted 44 lines · Ye Bin
Quoted 5 lines

To avoid to 'state->fc_regions_size' mismatch with 'state->fc_regions' when fail to reallocate 'fc_reqions',only update 'state->fc_regions_size' after 'state->fc_regions' is allocated successfully.

Looks good. Feel free to add:

Honza

Quoted 31 lines
---
  fs/ext4/fast_commit.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
fs/ext4/fast_commit.c +5 -4
index f5b0fc50ed47..694ab0627395 100644
@@ -1681,14 +1681,15 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,
  	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;
  		fc_regions = krealloc(state->fc_regions,
-				      state->fc_regions_size *
-				      sizeof(struct ext4_fc_alloc_region),
+				      sizeof(struct ext4_fc_alloc_region) *
+				      (state->fc_regions_size +
+				       EXT4_FC_REPLAY_REALLOC_INCREMENT),
  				      GFP_KERNEL);
  		if (!fc_regions)
  			return -ENOMEM;
+		state->fc_regions_size +=
+			EXT4_FC_REPLAY_REALLOC_INCREMENT;
  		state->fc_regions = fc_regions;
  	}
  	region = &state->fc_regions[state->fc_regions_used++];
Signature · 1 line
2.31.1
maintainer Applied
#8 lore raw
In reply to Ye Bin
Quoted 10 lines · Ye Bin

Diff v2 vs v1:
Add new patch "ext4: update 'state->fc_regions_size' after successful memory allocation"

Ye Bin (3):
ext4: fix potential memory leak in ext4_fc_record_modified_inode()
ext4: fix potential memory leak in ext4_fc_record_regions()
ext4: update 'state->fc_regions_size' after successful memory
allocation

[...]

Applied, thanks!

[1/3] ext4: fix potential memory leak in ext4_fc_record_modified_inode()

      commit: 3edc135aaf975c6022f69c47e55f4e55981aff10
[2/3] ext4: fix potential memory leak in ext4_fc_record_regions()
      commit: 87429b6248196338ae578d51ae95f2e3094f8a50
[3/3] ext4: update 'state->fc_regions_size' after successful memory allocation
      commit: 24e597348a4676fb1e573faaea05eb9e6a4b422c

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>