module: fix a memory leak
diff mbox

Message ID 1487157483-12848-1-git-send-email-bgolaszewski@baylibre.com
State Accepted
Headers show

Commit Message

Bartosz Golaszewski Feb. 15, 2017, 11:18 a.m. UTC
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().

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 libkmod/libkmod-module.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

Comments

Lucas De Marchi Feb. 16, 2017, 4:51 p.m. UTC | #1
On Wed, Feb 15, 2017 at 3:18 AM, Bartosz Golaszewski
<bgolaszewski@baylibre.com> wrote:
> 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().
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> ---
>  libkmod/libkmod-module.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
> index bf6a8d6..57da0a2 100644
> --- a/libkmod/libkmod-module.c
> +++ b/libkmod/libkmod-module.c
> @@ -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)) {
> --

Applied, thanks.

Lucas De Marchi
--
To unsubscribe from this list: send the line "unsubscribe linux-modules" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch
diff mbox

diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
index bf6a8d6..57da0a2 100644
--- a/libkmod/libkmod-module.c
+++ b/libkmod/libkmod-module.c
@@ -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)) {