Skip to contentSkip to conversation

genl: Fix descriptor leak in get_genl_kind()

PATCH In review Maks Mishin 2 participants 2 messages Feb 7, 2024 – Feb 8, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 genl: Fix descriptor leak in get_genl_kind()

---
 genl/genl.c | 3 +++
 1 file changed, 3 insertions(+)
genl/genl.c +3 -0
index 85cc73bb..74100dad 100644
@@ -71,6 +71,9 @@ static struct genl_util *get_genl_kind(const char *str)
 	snprintf(buf, sizeof(buf), "%s_genl_util", str);
 
 	f = dlsym(dlh, buf);
+	if (dlh != NULL)
+		dlclose(dlh);
+
 	if (f == NULL)
 		goto noexist;
 reg:
Signature · 1 line
2.30.2
#1 lore raw
Quoted 1 line · Maks Mishin

The subject should say [PATCH iproute2] or [PATCH iproute2-next] since it targets that project.

Quoted 9 lines
---
 genl/genl.c | 3 +++
 1 file changed, 3 insertions(+)
genl/genl.c +0 -0
index 85cc73bb..74100dad 100644

genl/genl.c static struct genl_util *get_genl_kind(const char *str) · line 75

Quoted 5 lines
genl/genl.c +2 -0
@@ -71,6 +71,9 @@ static struct genl_util *get_genl_kind(const char *str)
 	snprintf(buf, sizeof(buf), "%s_genl_util", str);
 
 	f = dlsym(dlh, buf);
+	if (dlh != NULL)
+		dlclose(dlh);

This is broken. If the earlier dlopen() actually loaded a .so then this dlclose() will close it again, before f gets used. When f gets dereferenced later, the program will crash. If this works at all, it is because dlopen(NULL, ...) returns a handle to the main program, so dlclose() doesn't unload it.

My assumption is that the author is leaving resources to be released at program exit. It is a short-lived command line utility after all.

Quoted 4 lines
genl/genl.c +1 -0
+
 	if (f == NULL)
 		goto noexist;
 reg: