Skip to contentSkip to conversation

Fix two potential memory leak

PATCH Superseded Ye Bin 3 participants 9 messages Sep 19, 2022 – Sep 21, 2022 ext4 file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ye Bin

author
#0 lore raw

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
author
#1 lore raw

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
index 9217a588afd1..cc8c8db075ba 100644
@@ -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
reviewer Reviewed-by on v1 1/2
#2 lore raw

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.

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 9217a588afd1..cc8c8db075ba 100644
@@ -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
#3 lore raw

Re: [PATCH -next 1/2] ext4: fix potential memory leak in ext4_fc_record_regions()

In reply to Ye Bin

Hello,

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.

---
  fs/ext4/fast_commit.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
fs/ext4/fast_commit.c +2 -1
index 9217a588afd1..cc8c8db075ba 100644
@@ -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 -0
  			return -ENOMEM;
+		state->fc_regions = fc_regions;
  	}
  	region = &state->fc_regions[state->fc_regions_used++];
  	region->ino = ino;

Regards,

Damien

author
#4 lore raw

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.

---
  fs/ext4/fast_commit.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
fs/ext4/fast_commit.c +2 -5
index 9217a588afd1..cc8c8db075ba 100644
@@ -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_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) ?

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 -0
              return -ENOMEM;
+        state->fc_regions = fc_regions;
      }
      region = &state->fc_regions[state->fc_regions_used++];
      region->ino = ino;

Regards,

Damien

.

#5 lore raw

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.

---
  fs/ext4/fast_commit.c | 14 ++++++++------

  1 file changed, 8 insertions(+), 6 deletions(-)
fs/ext4/fast_commit.c +2 -0
index 9217a588afd1..cc8c8db075ba 100644
@@ -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_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) ?

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

author
#6 lore raw

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.

---
  fs/ext4/fast_commit.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
fs/ext4/fast_commit.c +2 -1
index 9217a588afd1..cc8c8db075ba 100644
@@ -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_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) ?

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 of ext4_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 -0
              return -ENOMEM;
+        state->fc_regions = fc_regions;
      }
      region = &state->fc_regions[state->fc_regions_used++];
      region->ino = ino;

Regards,

Damien

.

author
#7 lore raw

PATCH 2/2 ext4: fix potential memory leak in ext4_fc_record_modified_inode()

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

---
 fs/ext4/fast_commit.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/ext4/fast_commit.c +5 -3
index cc8c8db075ba..5ab58cb4ce8d 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 v1 2/2
#8 lore raw

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.

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
index cc8c8db075ba..5ab58cb4ce8d 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