public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usermodehelper: Fix -ENOMEM return logic
@ 2013-02-25 14:25 Lucas De Marchi
  2013-02-25 15:05 ` David Howells
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Lucas De Marchi @ 2013-02-25 14:25 UTC (permalink / raw)
  To: linux-kernel
  Cc: David Howells, James Morris, Andrew Morton, Oleg Nesterov,
	Lucas De Marchi

Callers of call_usermodehelper_fns() should check the return value and
free themselves the data passed if the return is -ENOMEM. This is
because the subprocess_info is allocated in this function, and if the
allocation fail, the cleanup function cannot be called.

However call_usermodehelper_exec() may also return -ENOMEM, in which
case the cleanup function is called. This means that if the caller
checked the return code, it was risking running the cleanup twice (like
kernel/sys.c:orderly_poweroff()) and if not, a leak could happen.

This patch fixes both call_usermodehelper_fns() to never call the
cleanup function in case retval == -ENOMEM and also the callers to
actually check the return value of this function.

Signed-off-by: Lucas De Marchi <lucas.demarchi@profusion.mobi>
---
 kernel/kmod.c               |  9 +++++++--
 security/keys/request_key.c | 10 +++++++---
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/kernel/kmod.c b/kernel/kmod.c
index 56dd349..c4fec52 100644
--- a/kernel/kmod.c
+++ b/kernel/kmod.c
@@ -77,6 +77,7 @@ static void free_modprobe_argv(struct subprocess_info *info)
 
 static int call_modprobe(char *module_name, int wait)
 {
+	int ret;
 	static char *envp[] = {
 		"HOME=/",
 		"TERM=linux",
@@ -98,8 +99,12 @@ static int call_modprobe(char *module_name, int wait)
 	argv[3] = module_name;	/* check free_modprobe_argv() */
 	argv[4] = NULL;
 
-	return call_usermodehelper_fns(modprobe_path, argv, envp,
+	ret = call_usermodehelper_fns(modprobe_path, argv, envp,
 		wait | UMH_KILLABLE, NULL, free_modprobe_argv, NULL);
+	if (ret != -ENOMEM)
+		return ret;
+
+	kfree(module_name);
 free_argv:
 	kfree(argv);
 out:
@@ -249,7 +254,7 @@ static int call_helper(void *data)
 
 static void call_usermodehelper_freeinfo(struct subprocess_info *info)
 {
-	if (info->cleanup)
+	if (info->cleanup && info->retval != -ENOMEM)
 		(*info->cleanup)(info);
 	kfree(info);
 }
diff --git a/security/keys/request_key.c b/security/keys/request_key.c
index 4bd6bdb..22dc7a4 100644
--- a/security/keys/request_key.c
+++ b/security/keys/request_key.c
@@ -93,9 +93,13 @@ static void umh_keys_cleanup(struct subprocess_info *info)
 static int call_usermodehelper_keys(char *path, char **argv, char **envp,
 					struct key *session_keyring, int wait)
 {
-	return call_usermodehelper_fns(path, argv, envp, wait,
-				       umh_keys_init, umh_keys_cleanup,
-				       key_get(session_keyring));
+	int ret = call_usermodehelper_fns(path, argv, envp, wait,
+					  umh_keys_init, umh_keys_cleanup,
+					  key_get(session_keyring));
+	if (ret == -ENOMEM)
+		key_put(session_keyring);
+
+	return ret;
 }
 
 /*
-- 
1.8.1.4


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

end of thread, other threads:[~2013-03-07 21:36 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-25 14:25 [PATCH] usermodehelper: Fix -ENOMEM return logic Lucas De Marchi
2013-02-25 15:05 ` David Howells
2013-02-25 16:51   ` Oleg Nesterov
2013-02-25 16:06 ` Oleg Nesterov
2013-02-25 17:38   ` Lucas De Marchi
2013-02-25 18:08     ` Oleg Nesterov
2013-03-07  2:05       ` Lucas De Marchi
2013-03-07 19:37         ` Oleg Nesterov
2013-03-07 19:47           ` Lucas De Marchi
2013-03-07 20:07             ` Oleg Nesterov
2013-03-07 21:35               ` Lucas De Marchi
2013-02-25 17:10 ` [PATCH 0/1] usermodehelper: cleanup/fix __orderly_poweroff() && argv_free() Oleg Nesterov
2013-02-25 17:11   ` [PATCH 1/1] " Oleg Nesterov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox