Skip to contentSkip to conversation

usermodehelper: Fix -ENOMEM return logic

PATCH In review Lucas De Marchi 3 participants 13 messages Feb 25, 2013 – Mar 7, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usermodehelper: Fix -ENOMEM return logic

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.

---
 kernel/kmod.c               |  9 +++++++--
 security/keys/request_key.c | 10 +++++++---
 2 files changed, 14 insertions(+), 5 deletions(-)
kernel/kmod.c +7 -2
index 56dd349..c4fec52 100644
@@ -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);
 }
security/keys/request_key.c +7 -3
index 4bd6bdb..22dc7a4 100644
@@ -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;
 }
 
 /*
Signature · 1 line
1.8.1.4
maintainer
#1 lore raw
Quoted 5 lines · 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.

Quoted 8 lines

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.

I suspect it's probably better to always call the cleanup function from call_usermodehelper_fns() rather than have the cleanup done by the caller in some circumstances and not others - would it make sense to change the cleanup function to take the pointer to the caller data rather than to take the subprocess_info struct?

David

#2 lore raw
Quoted 7 lines · David Howells
Quoted 3 lines · Lucas De Marchi

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.

Quoted 5 lines

I suspect it's probably better to always call the cleanup function from call_usermodehelper_fns() rather than have the cleanup done by the caller in some circumstances and not others - would it make sense to change the cleanup function to take the pointer to the caller data rather than to take the subprocess_info struct?

I this this will comlicate the logic even more, the "caller data" has to be kmalloced/kfreed as well.

Btw. __orderly_poweroff() doesn't need any tricks, afaics. I'll send a simple cleanup in a minute.

Oleg.

#3 lore raw
In reply to Lucas De Marchi
Quoted 5 lines · 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.

Yes, this is confusing.

Quoted 1 line

However call_usermodehelper_exec() may also return -ENOMEM,

Yes, and we can't distinguish this case from info == NULL case,

Quoted 4 lines

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.

In short: every user of call_usermodehelper_fns(cleanup != NULL) is buggy. Thanks.

But I am not sure I agree with the patch...

Review

Quoted 7 lines

static void call_usermodehelper_freeinfo(struct subprocess_info *info) {

(quoted diff) +1 -1
-	if (info->cleanup)
+	if (info->cleanup && info->retval != -ENOMEM)
 		(*info->cleanup)(info);
 	kfree(info);
 }

This doesn't look very clean/robust. And in general, personally I dislike the fact that ENOMEM has the special meaning. IOW, I think we should cleanup this logic, not to complicate it more.

And in fact I do not think this is right, at least in UMH_NO_WAIT case, shouldn't avoid ->cleanup() if, say, prepare_kernel_cred() fails in ____call_usermodehelper()...

I think we should extract call_usermodehelper_setup() + call_usermodehelper_setfns() into the new helper and export it. And export call_usermodehelper_exec() as well.

call_usermodehelper_setfns() as a separate function makes no sense.

Then we can fix call_modprobe/orderly_poweroff, something like below.

What do you think?

Oleg.

kernel/kmod.c +8 -2
@@ -98,8 +98,14 @@ static int call_modprobe(char *module_na
 	argv[3] = module_name;	/* check free_modprobe_argv() */
 	argv[4] = NULL;
 
-	return call_usermodehelper_fns(modprobe_path, argv, envp,
-		wait | UMH_KILLABLE, NULL, free_modprobe_argv, NULL);
+	info = call_usermodehelper_setup(...); // better name, please...
+	if (!info)
+		goto free_modname;
+
+	return call_usermodehelper_exec(info, wait);
+
+free_modname:
+	kfree(module_name);
 free_argv:
 	kfree(argv);
 out:
#4 lore raw
Quoted 44 lines · Oleg Nesterov
Quoted 5 lines · 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.

Yes, this is confusing.

Quoted 1 line

However call_usermodehelper_exec() may also return -ENOMEM,

Yes, and we can't distinguish this case from info == NULL case,

Quoted 4 lines

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.

In short: every user of call_usermodehelper_fns(cleanup != NULL) is buggy. Thanks.

But I am not sure I agree with the patch...

Review

Quoted 7 lines

static void call_usermodehelper_freeinfo(struct subprocess_info *info) {

(quoted diff) +1 -1
-     if (info->cleanup)
+     if (info->cleanup && info->retval != -ENOMEM)
              (*info->cleanup)(info);
      kfree(info);
 }

This doesn't look very clean/robust. And in general, personally I dislike the fact that ENOMEM has the special meaning. IOW, I think we should cleanup this logic, not to complicate it more.

And in fact I do not think this is right, at least in UMH_NO_WAIT case, shouldn't avoid ->cleanup() if, say, prepare_kernel_cred() fails in ____call_usermodehelper()...

I think we should extract call_usermodehelper_setup() + call_usermodehelper_setfns() into the new helper and export it. And export call_usermodehelper_exec() as well.

Quoted 5 lines

call_usermodehelper_setfns() as a separate function makes no sense.

Then we can fix call_modprobe/orderly_poweroff, something like below.

What do you think?

Yep. The current interface is confusing. I agree that a separate setup() + exec() would make more sense.

Quoted 6 lines

Oleg.

kernel/kmod.c static int call_modprobe(char *module_na · line 105

Quoted 10 lines
kernel/kmod.c +5 -2
@@ -98,8 +98,14 @@ static int call_modprobe(char *module_na
        argv[3] = module_name;  /* check free_modprobe_argv() */
        argv[4] = NULL;
 
-       return call_usermodehelper_fns(modprobe_path, argv, envp,
-               wait | UMH_KILLABLE, NULL, free_modprobe_argv, NULL);
+       info = call_usermodehelper_setup(...); // better name, please...
+       if (!info)
+               goto free_modname;
+
+       return call_usermodehelper_exec(info, wait);

I'd say that in these cases the "call_" prefix has no meaning, and we could just use a "usermodehelper" as the namespace.

Lucas De Marchi

#5 lore raw
Quoted 3 lines · Lucas De Marchi

Yep. The current interface is confusing. I agree that a separate setup() + exec() would make more sense.

Great,

Quoted 4 lines
Quoted 4 lines
kernel/kmod.c +0 -0
@@ -98,8 +98,14 @@ static int call_modprobe(char *module_na
        argv[3] = module_name;  /* check free_modprobe_argv() */
        argv[4] = NULL;
 
Quoted 10 lines

kernel/kmod.c static int call_modprobe(char *module_na · line 105

Quoted 7 lines
kernel/kmod.c +5 -2
-       return call_usermodehelper_fns(modprobe_path, argv, envp,
-               wait | UMH_KILLABLE, NULL, free_modprobe_argv, NULL);
+       info = call_usermodehelper_setup(...); // better name, please...
+       if (!info)
+               goto free_modname;
+
+       return call_usermodehelper_exec(info, wait);

I'd say that in these cases the "call_" prefix has no meaning, and we could just use a "usermodehelper" as the namespace.

Oh, I agree with any naming.

So, I hope you will send v2. I'd suggest to split the fixes. 1/3 should create/export the new helpers, and 2-3 fix should call_modprobe() and call_usermodehelper_keys(). But this is up to you, I won't insist.

Oleg.

#6 lore raw

Hi Oleg,

Quoted 22 lines · Oleg Nesterov
Quoted 3 lines · Lucas De Marchi

Yep. The current interface is confusing. I agree that a separate setup() + exec() would make more sense.

Great,

Quoted 4 lines
Quoted 4 lines
(quoted diff) +0 -0
@@ -98,8 +98,14 @@ static int call_modprobe(char *module_na
        argv[3] = module_name;  /* check free_modprobe_argv() */
        argv[4] = NULL;
 
Quoted 10 lines

Review static int call_modprobe(char *module_na · line 105

Quoted 7 lines
(quoted diff) +5 -2
-       return call_usermodehelper_fns(modprobe_path, argv, envp,
-               wait | UMH_KILLABLE, NULL, free_modprobe_argv, NULL);
+       info = call_usermodehelper_setup(...); // better name, please...
+       if (!info)
+               goto free_modname;
+
+       return call_usermodehelper_exec(info, wait);

I'd say that in these cases the "call_" prefix has no meaning, and we could just use a "usermodehelper" as the namespace.

Quoted 5 lines

Oh, I agree with any naming.

So, I hope you will send v2. I'd suggest to split the fixes. 1/3 should create/export the new helpers, and 2-3 fix should call_modprobe() and call_usermodehelper_keys(). But this is up to you, I won't insist.

I was implementing this today, but looking into call_modprobe(), it is never called with UMH_NO_WAIT. Doesn't it mean we can use a similar trick as you used in __orderly_poweroff()? Then something similar to this would be sufficient in this case (whitespace damaged):

kernel/kmod.c +0 -0
index b39f240..e4d6f6f 100644
@@ -69,12 +69,6 @@ static DECLARE_RWSEM(umhelper_sem);

 */
 char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";

-static void free_modprobe_argv(struct subprocess_info *info) -{
- kfree(info->argv[3]); /* check call_modprobe() */
- kfree(info->argv);
-}
-
 static int call_modprobe(char *module_name, int wait)
 {
  static char *envp[] = {
@@ -83,6 +77,7 @@ static int call_modprobe(char *module_name, int wait) "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  NULL
  };
+ int ret = -ENOMEM;

  char **argv = kmalloc(sizeof(char *[5]), GFP_KERNEL); if (!argv)
@@ -98,12 +93,13 @@ 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,
- wait | UMH_KILLABLE, NULL, free_modprobe_argv, NULL);
+ ret = call_usermodehelper(modprobe_path, argv, envp,
+  wait | UMH_KILLABLE);
+ kfree(module_name);
 free_argv:
  kfree(argv);
 out:
- return -ENOMEM;
+ return ret;
 }

 /**

Lucas De Marchi

#7 lore raw

Hi Lucas,

Quoted 9 lines · Lucas De Marchi
Quoted 4 lines · Oleg Nesterov

So, I hope you will send v2. I'd suggest to split the fixes. 1/3 should create/export the new helpers, and 2-3 fix should call_modprobe() and call_usermodehelper_keys(). But this is up to you, I won't insist.

I was implementing this today, but looking into call_modprobe(), it is never called with UMH_NO_WAIT.

wait == T means UMH_WAIT_PROC, so we can't simply rely on CLONE_VFORK. But probably we can rely on sys_wait4.

However,

Review

Quoted 9 lines
(quoted diff) +0 -0
@@ -98,12 +93,13 @@ static int call_modprobe(char *module_name, int wait)

  argv[3] = module_name; /* check free_modprobe_argv() */
  argv[4] = NULL;

(quoted diff) +3 -2
-	return call_usermodehelper_fns(modprobe_path, argv, envp,
-		wait | UMH_KILLABLE, NULL, free_modprobe_argv, NULL);
+	ret = call_usermodehelper(modprobe_path, argv, envp,
+				  wait | UMH_KILLABLE);
+	kfree(module_name);

Please note UMH_KILLABLE. call_usermodehelper() can be interrupted and even UMH_WAIT_EXEC case is not safe. If call_modprobe() is killed we can return while the workqueue thread still tries to clone/exec/etc.

Oleg.

#8 lore raw
Quoted 10 lines · Oleg Nesterov

Hi Lucas,

Quoted 7 lines · Lucas De Marchi
Quoted 4 lines · Oleg Nesterov

So, I hope you will send v2. I'd suggest to split the fixes. 1/3 should create/export the new helpers, and 2-3 fix should call_modprobe() and call_usermodehelper_keys(). But this is up to you, I won't insist.

Quoted 5 lines
Quoted 2 lines

I was implementing this today, but looking into call_modprobe(), it is never called with UMH_NO_WAIT.

wait == T means UMH_WAIT_PROC, so we can't simply rely on CLONE_VFORK. But probably we can rely on sys_wait4.

yep, I was thinking about relying on sys_wait4.

Quoted 7 lines

However,

Quoted 4 lines
(quoted diff) +0 -0
@@ -98,12 +93,13 @@ static int call_modprobe(char *module_name, int wait)
      argv[3] = module_name;  /* check free_modprobe_argv() */
      argv[4] = NULL;
 
Quoted 9 lines

Review static int call_modprobe(char *module_name, int wait) · line 98

Quoted 5 lines
(quoted diff) +3 -2
-     return call_usermodehelper_fns(modprobe_path, argv, envp,
-             wait | UMH_KILLABLE, NULL, free_modprobe_argv, NULL);
+     ret = call_usermodehelper(modprobe_path, argv, envp,
+                               wait | UMH_KILLABLE);
+     kfree(module_name);

Please note UMH_KILLABLE. call_usermodehelper() can be interrupted and even UMH_WAIT_EXEC case is not safe. If call_modprobe() is killed we can return while the workqueue thread still tries to clone/exec/etc.

Even if it's killed, we would just free the resource we allocated before. It would not be safe if we allocated in the init function and freed in the cleanup. Or am I missing something?

thanks
Lucas De Marchi

#9 lore raw
Quoted 17 lines · Lucas De Marchi
Quoted 5 lines · Oleg Nesterov
Quoted 5 lines
(quoted diff) +0 -0
@@ -98,12 +93,13 @@ static int call_modprobe(char *module_name, int wait)
      argv[3] = module_name;  /* check free_modprobe_argv() */
      argv[4] = NULL;
 
Quoted 9 lines

Review static int call_modprobe(char *module_name, int wait) · line 98

Quoted 5 lines
(quoted diff) +3 -2
-     return call_usermodehelper_fns(modprobe_path, argv, envp,
-             wait | UMH_KILLABLE, NULL, free_modprobe_argv, NULL);
+     ret = call_usermodehelper(modprobe_path, argv, envp,
+                               wait | UMH_KILLABLE);
+     kfree(module_name);

Please note UMH_KILLABLE. call_usermodehelper() can be interrupted and even UMH_WAIT_EXEC case is not safe. If call_modprobe() is killed we can return while the workqueue thread still tries to clone/exec/etc.

Quoted 2 lines

Even if it's killed, we would just free the resource we allocated before.

Yes, and after that ____call_usermodehelper() can do do_execve(module_name) ?

Quoted 2 lines

It would not be safe if we allocated in the init function and freed in the cleanup.

But we do? We free this memory in cleanup ? And I was allocated by us.

sub_info itself can't go away (if you meant this), but sub_info->path/argv/envp can.

Quoted 1 line

Or am I missing something?

Or me...

Oleg.

#10 lore raw
Quoted 24 lines · Oleg Nesterov
Quoted 17 lines · Lucas De Marchi
Quoted 5 lines · Oleg Nesterov
Quoted 5 lines
(quoted diff) +0 -0
@@ -98,12 +93,13 @@ static int call_modprobe(char *module_name, int wait)
      argv[3] = module_name;  /* check free_modprobe_argv() */
      argv[4] = NULL;
 
Quoted 9 lines

Review static int call_modprobe(char *module_name, int wait) · line 98

Quoted 5 lines
(quoted diff) +3 -2
-     return call_usermodehelper_fns(modprobe_path, argv, envp,
-             wait | UMH_KILLABLE, NULL, free_modprobe_argv, NULL);
+     ret = call_usermodehelper(modprobe_path, argv, envp,
+                               wait | UMH_KILLABLE);
+     kfree(module_name);

Please note UMH_KILLABLE. call_usermodehelper() can be interrupted and even UMH_WAIT_EXEC case is not safe. If call_modprobe() is killed we can return while the workqueue thread still tries to clone/exec/etc.

Quoted 2 lines

Even if it's killed, we would just free the resource we allocated before.

Yes, and after that ____call_usermodehelper() can do do_execve(module_name) ?

Quoted 7 lines
Quoted 2 lines

It would not be safe if we allocated in the init function and freed in the cleanup.

But we do? We free this memory in cleanup ? And I was allocated by us.

sub_info itself can't go away (if you meant this), but sub_info->path/argv/envp can.

Oh... you are right - the UMH_KILLABLE is the problem here. Dunno what I was thinking :-/. I will fix my pending the patches.

thanks
Lucas De Marchi

#11 lore raw

cover usermodehelper: cleanup/fix __orderly_poweroff() && argv_free()

In reply to Lucas De Marchi
Quoted 3 lines · Lucas De Marchi

it was risking running the cleanup twice (like kernel/sys.c:orderly_poweroff()) and if not, a leak could happen.

As for kernel/sys.c:orderly_poweroff(), it simply doesn't need argv_cleanup() and -ENOMEM check, see 1/1.

Oleg.

#12 lore raw

PATCH 1/1 usermodehelper: cleanup/fix __orderly_poweroff() && argv_free()

__orderly_poweroff() does argv_free() if call_usermodehelper_fns() returns -ENOMEM. As Lucas pointed out, this can be wrong if -ENOMEM was not triggered by the failing call_usermodehelper_setup(), in this case both __orderly_poweroff() and argv_cleanup() can do kfree().

Kill argv_cleanup() and change __orderly_poweroff() to call argv_free() unconditionally like do_coredump() does. This info->cleanup() is not needed (and wrong) since 6c0c0d4d "fix bug in orderly_poweroff() which did the UMH_NO_WAIT => UMH_WAIT_EXEC change, we can rely on the fact that CLONE_VFORK can't return until do_execve() succeeds/fails.

kernel/sys.c +2 -8
@@ -2184,11 +2184,6 @@ SYSCALL_DEFINE3(getcpu, unsigned __user 
 
 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
 
-static void argv_cleanup(struct subprocess_info *info)
-{
-	argv_free(info->argv);
-}
-
 static int __orderly_poweroff(void)
 {
 	int argc;
@@ -2208,9 +2203,8 @@ static int __orderly_poweroff(void)
 	}
 
 	ret = call_usermodehelper_fns(argv[0], argv, envp, UMH_WAIT_EXEC,
-				      NULL, argv_cleanup, NULL);
-	if (ret == -ENOMEM)
-		argv_free(argv);
+				      NULL, NULL, NULL);
+	argv_free(argv);
 
 	return ret;
 }