Linux-Modules Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] module: fix a memory leak
@ 2017-02-15 11:18 Bartosz Golaszewski
  2017-02-16 16:51 ` Lucas De Marchi
  0 siblings, 1 reply; 2+ messages in thread
From: Bartosz Golaszewski @ 2017-02-15 11:18 UTC (permalink / raw)
  To: linux-modules; +Cc: 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().

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)) {
-- 
2.9.3


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] module: fix a memory leak
  2017-02-15 11:18 [PATCH] module: fix a memory leak Bartosz Golaszewski
@ 2017-02-16 16:51 ` Lucas De Marchi
  0 siblings, 0 replies; 2+ messages in thread
From: Lucas De Marchi @ 2017-02-16 16:51 UTC (permalink / raw)
  To: Bartosz Golaszewski; +Cc: linux-modules

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-15 11:18 [PATCH] module: fix a memory leak Bartosz Golaszewski
2017-02-16 16:51 ` Lucas De Marchi

Linux-Modules Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-modules/0 linux-modules/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-modules linux-modules/ https://lore.kernel.org/linux-modules \
		linux-modules@vger.kernel.org linux-modules@archiver.kernel.org
	public-inbox-index linux-modules


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-modules


AGPL code for this site: git clone https://public-inbox.org/ public-inbox