Skip to contentSkip to conversation

erofs-utils: lib: fix fd leak in erofs_metamgr_init()

PATCH Superseded Deepak Pathik 2 participants 5 messages Apr 1 – Apr 2 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: fix fd leak in erofs_metamgr_init()

In erofs_metamgr_init(), erofs_tmpfile() returns a file descriptor stored in m2gr->vf.fd. If the subsequent erofs_buffer_init() call fails, the function returns -ENOMEM without closing this file descriptor.

The caller erofs_metadata_init() handles this failure at err_free, which only frees the m2gr struct. The fd is therefore leaked with no remaining reference to close it.

The success path correctly cleans up via erofs_metamgr_exit(), which calls erofs_io_close(&m2gr->vf). Mirror that behaviour on the error path by closing the fd before returning.

---
 lib/metabox.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/metabox.c +3 -1
index 12706aa..d55e787 100644
@@ -32,8 +32,10 @@ static int erofs_metamgr_init(struct erofs_sb_info *sbi,
 
 m2gr->vf = (struct erofs_vfile){ .fd = ret };
 	m2gr->bmgr = erofs_buffer_init(sbi, 0, &m2gr->vf);
- if (!m2gr->bmgr)
+if (!m2gr->bmgr) {
+close(m2gr->vf.fd);
 		return -ENOMEM;
+}
 	return 0;
 }
Signature · 1 line
2.50.1
#1 lore raw

lib/metabox.c static int erofs_metamgr_init(struct erofs_sb_info *sbi, · line 36

Quoted 2 lines · Deepak Pathik
lib/metabox.c +2 -0
+if (!m2gr->bmgr) {
+close(m2gr->vf.fd);

erofs_io_close() does more than close(fd) — it dispatches through vf->ops->close(vf) if ops is set, and resets vf->fd to -1 afterward. Using raw close() here skips both, which is incorrect.

Also, the if block is missing tab indentation.

Suggested fix:

if (!m2gr->bmgr) {
erofs_io_close(&m2gr->vf);
return -ENOMEM;
}

Quoted 39 lines · Deepak Pathik

In erofs_metamgr_init(), erofs_tmpfile() returns a file descriptor stored in m2gr->vf.fd. If the subsequent erofs_buffer_init() call fails, the function returns -ENOMEM without closing this file descriptor.

The caller erofs_metadata_init() handles this failure at err_free, which only frees the m2gr struct. The fd is therefore leaked with no remaining reference to close it.

The success path correctly cleans up via erofs_metamgr_exit(), which calls erofs_io_close(&m2gr->vf). Mirror that behaviour on the error path by closing the fd before returning.

---
 lib/metabox.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/metabox.c +3 -2
index 12706aa..d55e787 100644
@@ -32,8 +32,10 @@ static int erofs_metamgr_init(struct erofs_sb_info *sbi,
 
 m2gr->vf = (struct erofs_vfile){ .fd = ret };
        m2gr->bmgr = erofs_buffer_init(sbi, 0, &m2gr->vf);
- if (!m2gr->bmgr)
+if (!m2gr->bmgr) {
+close(m2gr->vf.fd);
                return -ENOMEM;
+}
        return 0;
 }
--

2.50.1

#2 lore raw

Hi Utkal,

Thanks for the review. You're right on both points — v2 will use erofs_io_close() and fix the indentation.

Thanks, Deepak Pathik

Quoted 60 lines · Utkal Singh

lib/metabox.c static int erofs_metamgr_init(struct erofs_sb_info *sbi, · line 36

Quoted 2 lines · Deepak Pathik
lib/metabox.c +2 -0
+if (!m2gr->bmgr) {
+close(m2gr->vf.fd);

erofs_io_close() does more than close(fd) — it dispatches through vf->ops->close(vf) if ops is set, and resets vf->fd to -1 afterward. Using raw close() here skips both, which is incorrect.

Also, the if block is missing tab indentation.

Suggested fix:

if (!m2gr->bmgr) {
erofs_io_close(&m2gr->vf);
return -ENOMEM;
}

lib/metabox.c static int erofs_metamgr_init(struct erofs_sb_info

Quoted 24 lines · Deepak Pathik

In erofs_metamgr_init(), erofs_tmpfile() returns a file descriptor stored in m2gr->vf.fd. If the subsequent erofs_buffer_init() call fails, the function returns -ENOMEM without closing this file descriptor.

The caller erofs_metadata_init() handles this failure at err_free, which only frees the m2gr struct. The fd is therefore leaked with no remaining reference to close it.

The success path correctly cleans up via erofs_metamgr_exit(), which calls erofs_io_close(&m2gr->vf). Mirror that behaviour on the error path by closing the fd before returning.

---
 lib/metabox.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/metabox.c +0 -0
index 12706aa..d55e787 100644
@@ -32,8 +32,10 @@ static int erofs_metamgr_init(struct erofs_sb_info

*sbi,

Quoted 15 lines
lib/metabox.c +3 -2
 m2gr->vf = (struct erofs_vfile){ .fd = ret };
        m2gr->bmgr = erofs_buffer_init(sbi, 0, &m2gr->vf);
- if (!m2gr->bmgr)
+if (!m2gr->bmgr) {
+close(m2gr->vf.fd);
                return -ENOMEM;
+}
        return 0;
 }
--

2.50.1

#3 lore raw

PATCH v2 1/1 erofs-utils: lib: fix fd leak in erofs_metamgr_init()

In reply to Deepak Pathik

In erofs_metamgr_init(), erofs_tmpfile() returns a file descriptor stored in m2gr->vf.fd. If the subsequent erofs_buffer_init() call fails, the function returns -ENOMEM without closing this file descriptor.

The caller erofs_metadata_init() handles this failure at err_free, which only frees the m2gr struct. The fd is therefore leaked with no remaining reference to close it.

The success path correctly cleans up via erofs_metamgr_exit(), which calls erofs_io_close(&m2gr->vf). Mirror that behaviour on the error path by closing the fd before returning.

---
v2: use erofs_io_close() instead of raw close(); rebased on latest upstream/dev

 lib/metabox.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/metabox.c +3 -1
index d5ce9e3..86a7083 100644
@@ -32,8 +32,10 @@ static int erofs_metamgr_init(struct erofs_sb_info *sbi,
 
 m2gr->vf = (struct erofs_vfile){ .fd = ret };
 	m2gr->bmgr = erofs_buffer_init(sbi, 0, &m2gr->vf);
- if (!m2gr->bmgr)
+if (!m2gr->bmgr) {
+erofs_io_close(&m2gr->vf);
 		return -ENOMEM;
+}
 	return 0;
 }
 
Signature · 1 line
2.53.0
#4 lore raw

PATCH v3 1/1 erofs-utils: lib: fix fd leak in erofs_metamgr_init()

In reply to Deepak Pathik

In erofs_metamgr_init(), erofs_tmpfile() returns a file descriptor stored in m2gr->vf.fd. If the subsequent erofs_buffer_init() call fails, the function returns -ENOMEM without closing this file descriptor.

The caller erofs_metadata_init() handles this failure at err_free, which only frees the m2gr struct. The fd is therefore leaked with no remaining reference to close it.

The success path correctly cleans up via erofs_metamgr_exit(), which calls erofs_io_close(&m2gr->vf). Mirror that behaviour on the error path by closing the fd before returning.

---
 lib/metabox.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/metabox.c +3 -1
index d5ce9e3..86a7083 100644
@@ -32,8 +32,10 @@ static int erofs_metamgr_init(struct erofs_sb_info *sbi,
 
 	m2gr->vf = (struct erofs_vfile){ .fd = ret };
 	m2gr->bmgr = erofs_buffer_init(sbi, 0, &m2gr->vf);
-	if (!m2gr->bmgr)
+	if (!m2gr->bmgr) {
+		erofs_io_close(&m2gr->vf);
 		return -ENOMEM;
+	}
 	return 0;
 }
 
Signature · 1 line
2.53.0