[Lucid,01/10] kmod: fix resource leak in call_usermodehelper_pipe()

Submitted by Tim Gardner on March 26, 2012, 6:01 p.m.

Details

Message ID 1332784903-75063-2-git-send-email-tim.gardner@canonical.com
State New
Headers show

Commit Message

Tim Gardner March 26, 2012, 6:01 p.m.
From: Masami Hiramatsu <mhiramat@redhat.com>

BugLink: http://bugs.launchpad.net/bugs/963685

Fix resource (write-pipe file) leak in call_usermodehelper_pipe().

When call_usermodehelper_exec() fails, write-pipe file is opened and
call_usermodehelper_pipe() just returns an error.  Since it is hard for
caller to determine whether the error occured when opening the pipe or
executing the helper, the caller cannot close the pipe by themselves.

I've found this resoruce leak when testing coredump.  You can check how
the resource leaks as below;

$ echo "|nocommand" > /proc/sys/kernel/core_pattern
$ ulimit -c unlimited
$ while [ 1 ]; do ./segv; done &> /dev/null &
$ cat /proc/meminfo (<- repeat it)

where segv.c is;
//-----
int main () {
        char *p = 0;
        *p = 1;
}
//-----

This patch closes write-pipe file if call_usermodehelper_exec() failed.

Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
(cherry picked from commit 8767ba2796a1c894e6d9524584a26a8224f0543d)

Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
---
 kernel/kmod.c |   12 +++++++-----
 1 files changed, 7 insertions(+), 5 deletions(-)

Patch hide | download patch | download mbox

diff --git a/kernel/kmod.c b/kernel/kmod.c
index a061472..9e38576 100644
--- a/kernel/kmod.c
+++ b/kernel/kmod.c
@@ -553,13 +553,15 @@  int call_usermodehelper_pipe(char *path, char **argv, char **envp,
 		return -ENOMEM;
 
 	ret = call_usermodehelper_stdinpipe(sub_info, filp);
-	if (ret < 0)
-		goto out;
+	if (ret < 0) {
+		call_usermodehelper_freeinfo(sub_info);
+		return ret;
+	}
 
-	return call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
+	ret = call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
+	if (ret < 0)	/* Failed to execute helper, close pipe */
+		filp_close(*filp, NULL);
 
-  out:
-	call_usermodehelper_freeinfo(sub_info);
 	return ret;
 }
 EXPORT_SYMBOL(call_usermodehelper_pipe);