Skip to contentSkip to conversation

module: fix a memory leak

PATCH Applied Bartosz Golaszewski 2 participants 2 messages Feb 15, 2017 – Feb 16, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 module: fix a memory leak

When a module is removed and re-inserted without unrefing, the kmod_file is unconditionally re-opened. This results in a memory and file descriptor leak.

Fix it by checking if the file is already open in
kmod_module_insert_module().

---
 libkmod/libkmod-module.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
libkmod/libkmod-module.c +6 -4
index bf6a8d6..57da0a2 100644
@@ -833,10 +833,12 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
 		return -ENOENT;
 	}
 
-	mod->file = kmod_file_open(mod->ctx, path);
-	if (mod->file == NULL) {
-		err = -errno;
-		return err;
+	if (!mod->file) {
+		mod->file = kmod_file_open(mod->ctx, path);
+		if (mod->file == NULL) {
+			err = -errno;
+			return err;
+		}
 	}
 
 	if (kmod_file_get_direct(mod->file)) {
Signature · 1 line
2.9.3
#1 lore raw
Quoted 24 lines · Bartosz Golaszewski

When a module is removed and re-inserted without unrefing, the kmod_file is unconditionally re-opened. This results in a memory and file descriptor leak.

Fix it by checking if the file is already open in
kmod_module_insert_module().

---
 libkmod/libkmod-module.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
libkmod/libkmod-module.c +0 -4
index bf6a8d6..57da0a2 100644
@@ -833,10 +833,12 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
                return -ENOENT;
        }
 
-       mod->file = kmod_file_open(mod->ctx, path);
-       if (mod->file == NULL) {
-               err = -errno;
-               return err;

libkmod/libkmod-module.c KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,

Quoted 10 lines
libkmod/libkmod-module.c +6 -1
+       if (!mod->file) {
+               mod->file = kmod_file_open(mod->ctx, path);
+               if (mod->file == NULL) {
+                       err = -errno;
+                       return err;
+               }
        }
 
        if (kmod_file_get_direct(mod->file)) {
--

Applied, thanks.

Lucas De Marchi