Skip to contentSkip to conversation

erofs-utils: fuse: add missing return on getattr error

PATCH Superseded Ajay Rajera 2 participants 6 messages Mar 21 Library code

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 erofs-utils: fuse: add missing return on getattr error

erofsfuse_getattr() calls fuse_reply_err() when erofs_read_inode_from_disk() fails, but does not return afterwards. This causes the function to fall through to erofsfuse_fill_stat() with uninitialized inode data and then call fuse_reply_attr(), sending a second reply to the same FUSE request.

Sending two replies to a single FUSE request is undefined behavior in libfuse and typically triggers an assertion failure or crash. The uninitialized inode data may also expose garbage values to userspace.

Fix by adding the missing return after fuse_reply_err().

---
 fuse/main.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fuse/main.c +3 -1
index 82aca8c..b634782 100644
@@ -265,8 +265,10 @@ static void erofsfuse_getattr(fuse_req_t req, fuse_ino_t ino,
 	struct erofs_inode vi = { .sbi = &g_sbi, .nid = erofsfuse_to_nid(ino) };
 
 	ret = erofs_read_inode_from_disk(&vi);
-	if (ret < 0)
+	if (ret < 0) {
 		fuse_reply_err(req, -ret);
+		return;
+	}
 
 	erofsfuse_fill_stat(&vi, &stbuf);
 	stbuf.st_ino = ino;
Signature · 1 line
2.51.0.windows.1
#1 lore raw

PATCH v2 2/2 erofs-utils: lib: fix memory leak in erofs_gzran_builder_init error path

When inflateInit2() fails, erofs_gzran_builder_init() returns an ERR_PTR(-EFAULT) but forgets to free the previously allocated erofs_gzran_builder struct (gb), resulting in a memory leak.

Fix by calling free(gb) before returning the error.

---
 lib/gzran.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/gzran.c +3 -1
index dffb20a..8a01825 100644
@@ -50,8 +50,10 @@ struct erofs_gzran_builder *erofs_gzran_builder_init(struct erofs_vfile *vf,
 	strm->avail_in = 0;
 	strm->next_in = Z_NULL;
 	ret = inflateInit2(strm, 47);	/* automatic zlib or gzip decoding */
-	if (ret != Z_OK)
+	if (ret != Z_OK) {
+		free(gb);
 		return ERR_PTR(-EFAULT);
+	}
 	gb->vf = vf;
 	gb->span_size = span_size;
 	gb->totout = gb->totin = 0;
Signature · 1 line
2.51.0.windows.1
#2 lore raw

Re: [PATCH v2 2/2] erofs-utils: lib: fix memory leak in erofs_gzran_builder_init error path

Quoted 7 lines · Ajay Rajera

When inflateInit2() fails, erofs_gzran_builder_init() returns an ERR_PTR(-EFAULT) but forgets to free the previously allocated erofs_gzran_builder struct (gb), resulting in a memory leak.

Fix by calling free(gb) before returning the error.

The same issue.

#3 lore raw

Re: [PATCH v2 2/2] erofs-utils: lib: fix memory leak in erofs_gzran_builder_init error path

yeah, I just sent patch v3.
Thanks, Ajay.

Quoted 13 lines · Gao Xiang
Quoted 7 lines · Ajay Rajera

When inflateInit2() fails, erofs_gzran_builder_init() returns an ERR_PTR(-EFAULT) but forgets to free the previously allocated erofs_gzran_builder struct (gb), resulting in a memory leak.

Fix by calling free(gb) before returning the error.

The same issue.

#4 lore raw
In reply to Ajay Rajera
Quoted 10 lines · Ajay Rajera

erofsfuse_getattr() calls fuse_reply_err() when erofs_read_inode_from_disk() fails, but does not return afterwards. This causes the function to fall through to erofsfuse_fill_stat() with uninitialized inode data and then call fuse_reply_attr(), sending a second reply to the same FUSE request.

Sending two replies to a single FUSE request is undefined behavior in libfuse and typically triggers an assertion failure or crash. The uninitialized inode data may also expose garbage values to userspace.

Fix by adding the missing return after fuse_reply_err().

Each line of the commit message should not exceed 72 chars.

Quoted 22 lines
---
  fuse/main.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
fuse/main.c +3 -1
index 82aca8c..b634782 100644
@@ -265,8 +265,10 @@ static void erofsfuse_getattr(fuse_req_t req, fuse_ino_t ino,
  	struct erofs_inode vi = { .sbi = &g_sbi, .nid = erofsfuse_to_nid(ino) };
 
  	ret = erofs_read_inode_from_disk(&vi);
-	if (ret < 0)
+	if (ret < 0) {
  		fuse_reply_err(req, -ret);
+		return;
+	}
 
  	erofsfuse_fill_stat(&vi, &stbuf);
  	stbuf.st_ino = ino;
#5 lore raw

Yeah, I apologize for the mistake.
I just sent patch v3
Thanks, Ajay.

Quoted 40 lines · Gao Xiang
Quoted 10 lines · Ajay Rajera

erofsfuse_getattr() calls fuse_reply_err() when erofs_read_inode_from_disk() fails, but does not return afterwards. This causes the function to fall through to erofsfuse_fill_stat() with uninitialized inode data and then call fuse_reply_attr(), sending a second reply to the same FUSE request.

Sending two replies to a single FUSE request is undefined behavior in libfuse and typically triggers an assertion failure or crash. The uninitialized inode data may also expose garbage values to userspace.

Fix by adding the missing return after fuse_reply_err().

Each line of the commit message should not exceed 72 chars.

Quoted 22 lines
---
  fuse/main.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
fuse/main.c +3 -1
index 82aca8c..b634782 100644
@@ -265,8 +265,10 @@ static void erofsfuse_getattr(fuse_req_t req, fuse_ino_t ino,
      struct erofs_inode vi = { .sbi = &g_sbi, .nid = erofsfuse_to_nid(ino) };
 
      ret = erofs_read_inode_from_disk(&vi);
-     if (ret < 0)
+     if (ret < 0) {
              fuse_reply_err(req, -ret);
+             return;
+     }
 
      erofsfuse_fill_stat(&vi, &stbuf);
      stbuf.st_ino = ino;