kernel/sys.c: avoid argv_free(NULL)

If argv_split() failed, the code will end up calling argv_free(NULL).  Fix
it up and clean things up a bit.

Addresses Coverity report 703573.

Cc: Cyrill Gorcunov <gorcunov@openvz.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: Serge Hallyn <serge.hallyn@canonical.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: WANG Cong <xiyou.wangcong@gmail.com>
Cc: Alan Cox <alan@linux.intel.com>
Cc: David Rientjes <rientjes@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Andrew Morton 2012-07-30 14:40:03 -07:00 committed by Linus Torvalds
parent 45226e944c
commit b57b44ae69
1 changed files with 33 additions and 27 deletions

View File

@ -2186,6 +2186,32 @@ static void argv_cleanup(struct subprocess_info *info)
argv_free(info->argv); argv_free(info->argv);
} }
static int __orderly_poweroff(void)
{
int argc;
char **argv;
static char *envp[] = {
"HOME=/",
"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
NULL
};
int ret;
argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
if (argv == NULL) {
printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
__func__, poweroff_cmd);
return -ENOMEM;
}
ret = call_usermodehelper_fns(argv[0], argv, envp, UMH_NO_WAIT,
NULL, argv_cleanup, NULL);
if (ret == -ENOMEM)
argv_free(argv);
return ret;
}
/** /**
* orderly_poweroff - Trigger an orderly system poweroff * orderly_poweroff - Trigger an orderly system poweroff
* @force: force poweroff if command execution fails * @force: force poweroff if command execution fails
@ -2195,37 +2221,17 @@ static void argv_cleanup(struct subprocess_info *info)
*/ */
int orderly_poweroff(bool force) int orderly_poweroff(bool force)
{ {
int argc; int ret = __orderly_poweroff();
char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
static char *envp[] = {
"HOME=/",
"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
NULL
};
int ret = -ENOMEM;
if (argv == NULL) { if (ret && force) {
printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
__func__, poweroff_cmd);
goto out;
}
ret = call_usermodehelper_fns(argv[0], argv, envp, UMH_NO_WAIT,
NULL, argv_cleanup, NULL);
out:
if (likely(!ret))
return 0;
if (ret == -ENOMEM)
argv_free(argv);
if (force) {
printk(KERN_WARNING "Failed to start orderly shutdown: " printk(KERN_WARNING "Failed to start orderly shutdown: "
"forcing the issue\n"); "forcing the issue\n");
/* I guess this should try to kick off some daemon to /*
sync and poweroff asap. Or not even bother syncing * I guess this should try to kick off some daemon to sync and
if we're doing an emergency shutdown? */ * poweroff asap. Or not even bother syncing if we're doing an
* emergency shutdown?
*/
emergency_sync(); emergency_sync();
kernel_power_off(); kernel_power_off();
} }