Skip to contentSkip to conversation

erofs-utils: fsck: fix fd leak on failure in erofs_extract_file()

PATCH In review Gao Xiang 3 participants 9 messages Aug 2, 2024 – Aug 4, 2024 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/3 erofs-utils: fsck: fix fd leak on failure in erofs_extract_file()

Ignore the return values as other close()s instead.

Coverity-id: 502331

---
 fsck/main.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fsck/main.c +1 -3
index fb66967..bbef645 100644
@@ -702,11 +702,9 @@ again:
 
 	/* verify data chunk layout */
 	ret = erofs_verify_inode_data(inode, fd);
+	close(fd);
 	if (ret)
 		return ret;
-
-	if (close(fd))
-		return -errno;
 	return ret;
 }
 
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/3 erofs-utils: lib: fix out-of-bounds in erofs_io_xcopy()

Coverity-id: 502334

---
 include/erofs/io.h | 4 ++--
 lib/io.c           | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
include/erofs/io.h +2 -2
index f53abed..d9b33d2 100644
@@ -34,7 +34,7 @@ struct erofs_vfops {
 	off_t (*lseek)(struct erofs_vfile *vf, u64 offset, int whence);
 	int (*fstat)(struct erofs_vfile *vf, struct stat *buf);
 	int (*xcopy)(struct erofs_vfile *vout, off_t pos,
-		     struct erofs_vfile *vin, int len, bool noseek);
+		     struct erofs_vfile *vin, unsigned int len, bool noseek);
 };
 
 /* don't extend this; instead, use payload for any extra information */
@@ -61,7 +61,7 @@ off_t erofs_io_lseek(struct erofs_vfile *vf, u64 offset, int whence);
 ssize_t erofs_copy_file_range(int fd_in, u64 *off_in, int fd_out, u64 *off_out,
 			      size_t length);
 int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos,
-		   struct erofs_vfile *vin, int len, bool noseek);
+		   struct erofs_vfile *vin, unsigned int len, bool noseek);
 
 #ifdef __cplusplus
 }
lib/io.c +2 -2
index 9167321..4937db5 100644
@@ -490,7 +490,7 @@ off_t erofs_io_lseek(struct erofs_vfile *vf, u64 offset, int whence)
 }
 
 int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos,
-		   struct erofs_vfile *vin, int len, bool noseek)
+		   struct erofs_vfile *vin, unsigned int len, bool noseek)
 {
 	if (vout->ops)
 		return vout->ops->xcopy(vout, pos, vin, len, noseek);
@@ -519,7 +519,7 @@ int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos,
 
 	do {
 		char buf[32768];
-		int ret = min_t(int, len, sizeof(buf));
+		int ret = min_t(unsigned int, len, sizeof(buf));
 
 		ret = erofs_io_read(vin, buf, ret);
 		if (ret < 0)
Signature · 1 line
2.43.5
author
#3 lore raw

PATCH 3/3 erofs-utils: lib: fix fd leak on failure in erofs_dev_open()

In reply to Gao Xiang

Coverity-id: 502356

---
 lib/io.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/io.c +3 -1
index 4937db5..6bfae69 100644
@@ -258,8 +258,10 @@ repeat:
 #if defined(HAVE_SYS_STATFS_H) && defined(HAVE_FSTATFS)
 			struct statfs stfs;
 
-			if (again)
+			if (again) {
+				close(fd);
 				return -ENOTEMPTY;
+			}
 
 			/*
 			 * fses like EXT4 and BTRFS will flush dirty blocks
Signature · 1 line
2.43.5
author
#5 lore raw

[PATCH 4/3] erofs-utils: lib: fix potential memory leak in erofs_export_xattr_ibody()

In reply to Gao Xiang

Although it won't happen in reality except for bugs.

Fixes 8f93c2f83962 ("erofs-utils: mkfs: support inline xattr reservation for rootdirs")

Coverity-id: 507395

---
 lib/xattr.c | 1 +
 1 file changed, 1 insertion(+)
lib/xattr.c +1 -0
index f860f2e..651657f 100644
@@ -1020,6 +1020,7 @@ char *erofs_export_xattr_ibody(struct erofs_inode *inode)
 		memset(buf + p, 0, size - p);
 	} else if (__erofs_unlikely(p > size)) {
 		DBG_BUGON(1);
+		free(buf);
 		return ERR_PTR(-EFAULT);
 	}
 	return buf;
Signature · 1 line
2.43.5
Reviewed-by Sandeep Dhavale on v1 1/3
#7 lore raw
In reply to Gao Xiang

Hi Gao,

Quoted 14 lines · Gao Xiang

Ignore the return values as other close()s instead.

Coverity-id: 502331

---
 fsck/main.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fsck/main.c +0 -0
index fb66967..bbef645 100644

fsck/main.c again: · line 707

Quoted 6 lines
fsck/main.c +1 -0
@@ -702,11 +702,9 @@ again:
 
        /* verify data chunk layout */
        ret = erofs_verify_inode_data(inode, fd);
+       close(fd);
        if (ret)
                return ret;

I think we can get rid of this if block and should be just return ret;

Quoted 9 lines
fsck/main.c +0 -4
-
-       if (close(fd))
-               return -errno;
        return ret;
 }
 
--

2.43.5

You can just do that while applying,

Thanks,
Sandeep.

#8 lore raw
Quoted 18 lines · Sandeep Dhavale via Linux-erofs

Hi Gao,

Quoted 15 lines · Gao Xiang

Ignore the return values as other close()s instead.

Coverity-id: 502331

---
 fsck/main.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fsck/main.c +0 -0
index fb66967..bbef645 100644
@@ -702,11 +702,9 @@ again:
 
Quoted 7 lines

fsck/main.c again: · line 707

Quoted 5 lines
fsck/main.c +1 -0
        /* verify data chunk layout */
        ret = erofs_verify_inode_data(inode, fd);
+       close(fd);
        if (ret)
                return ret;

I think we can get rid of this if block and should be just return ret;

Yeah, agreed, let me revise that.

Quoted 6 lines
Quoted 6 lines
fsck/main.c +0 -3
-
-       if (close(fd))
-               return -errno;
        return ret;
 }
 
Quoted 7 lines
Quoted 3 lines

--
2.43.5

You can just do that while applying,

Thanks for your review!

Thanks,
Gao Xiang

Quoted 2 lines

Thanks,
Sandeep.