Skip to contentSkip to conversation

erofs-utils: misc: Fix potential memory leak in realloc failure path

PATCH In review Sandeep Dhavale via Linux-erofs 2 participants 4 messages Jul 18, 2024 – Jul 19, 2024 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: misc: Fix potential memory leak in realloc failure path

As realloc returns NULL on failure, the original value will be overwritten if it is used as lvalue. Fix this by using a temporary variable to hold the return value and exit with -ENOMEM in case of failure. This patch fixes 2 of the realloc blocks with similar fix.

---
 fsck/main.c | 9 +++++++--
 lib/data.c  | 5 +++--
 2 files changed, 10 insertions(+), 4 deletions(-)
fsck/main.c +7 -2
index 8ec9486..75950b6 100644
@@ -508,8 +508,13 @@ static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
 		if (compressed) {
 			if (map.m_llen > buffer_size) {
 				buffer_size = map.m_llen;
-				buffer = realloc(buffer, buffer_size);
-				BUG_ON(!buffer);
+				char *newbuffer = realloc(buffer, buffer_size);
+
+				if (!newbuffer) {
+					ret = -ENOMEM;
+					goto out;
+				}
+				buffer = newbuffer;
 			}
 			ret = z_erofs_read_one_data(inode, &map, raw, buffer,
 						    0, map.m_llen, false);
lib/data.c +3 -2
index a8402ed..0fc013e 100644
@@ -338,11 +338,12 @@ static int z_erofs_read_data(struct erofs_inode *inode, char *buffer,
 
 		if (map.m_plen > bufsize) {
 			bufsize = map.m_plen;
-			raw = realloc(raw, bufsize);
-			if (!raw) {
+			char *newraw = realloc(raw, bufsize);
+			if (!newraw) {
 				ret = -ENOMEM;
 				break;
 			}
+			raw = newraw;
 		}
 
 		ret = z_erofs_read_one_data(inode, &map, raw,
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
#1 lore raw

Hi Sandeep,

Quoted 4 lines · Sandeep Dhavale

As realloc returns NULL on failure, the original value will be overwritten if it is used as lvalue. Fix this by using a temporary variable to hold the return value and exit with -ENOMEM in case of failure. This patch fixes 2 of the realloc blocks with similar fix.

Thanks for the patch!

Is it a lint issue? Although I think currently userspace malloc failures is rare, it'd be better to handle them.

Quoted 14 lines
---
  fsck/main.c | 9 +++++++--
  lib/data.c  | 5 +++--
  2 files changed, 10 insertions(+), 4 deletions(-)
fsck/main.c +0 -0
index 8ec9486..75950b6 100644
@@ -508,8 +508,13 @@ static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
  		if (compressed) {
  			if (map.m_llen > buffer_size) {

fsck/main.c static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd) · line 517

Quoted 10 lines
fsck/main.c +7 -2
  				buffer_size = map.m_llen;
-				buffer = realloc(buffer, buffer_size);
-				BUG_ON(!buffer);
+				char *newbuffer = realloc(buffer, buffer_size);
+
+				if (!newbuffer) {
+					ret = -ENOMEM;
+					goto out;
+				}
+				buffer = newbuffer;

I update this as since the variable definition would be better at the beginning of a block...

fsck/main.c +3 -2
index 75950b6..fb66967 100644
@@ -507,9 +507,10 @@ static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
 
                if (compressed) {
                        if (map.m_llen > buffer_size) {
-                               buffer_size = map.m_llen;
-                               char *newbuffer = realloc(buffer, buffer_size);
+                               char *newbuffer;
 
+                               buffer_size = map.m_llen;
+                               newbuffer = realloc(buffer, buffer_size);
                                if (!newbuffer) {
                                        ret = -ENOMEM;
                                        goto out;

Thanks,
Gao Xiang

#2 lore raw

Hi Gao,

Quoted 2 lines

Is it a lint issue? Although I think currently userspace malloc failures is rare, it'd be better to handle them.

Previously I have used scan-build as a static checker. However today, one of the realloc code blocks, I stumbled upon while going through code on another performance problem.
Then I just grepped and found another one so I just fixed them in one go.

Quoted 2 lines

I update this as since the variable definition would be better at the beginning of a block...

The edit looks better to me, so feel free to update while applying. As always, thanks for the review.

Thanks,
Sandeep.

#3 lore raw
Quoted 9 lines · Sandeep Dhavale

Hi Gao,

Quoted 2 lines

Is it a lint issue? Although I think currently userspace malloc failures is rare, it'd be better to handle them.

Previously I have used scan-build as a static checker. However today, one of the realloc code blocks, I stumbled upon while going through code on another performance problem.
Then I just grepped and found another one so I just fixed them in one go.

Okay, thanks for the info.

Quoted 6 lines
Quoted 3 lines

I update this as since the variable definition would be better at the beginning of a block...

The edit looks better to me, so feel free to update while applying. As always, thanks for the review.

Let me apply the patch soon..

Thanks,
Gao Xiang

Quoted 3 lines

Thanks,
Sandeep.