Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix a possible memory leak if execvp fails in execCommand #536

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions popt/popt.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ static int execCommand(poptContext con)
argv[argc] = s;
} else
argv[argc] = findProgramPath(item->argv[0]);
if (argv[argc++] == NULL) return POPT_ERROR_NOARG;
if (argv[argc++] == NULL) goto fail_noarg;

if (item->argc > 1) {
memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1));
Expand All @@ -434,9 +434,9 @@ static int execCommand(poptContext con)
{
#ifdef __hpux
int rc = setresgid(getgid(), getgid(),-1);
if (rc) return POPT_ERROR_ERRNO;
if (rc) goto fail_errno;
rc = setresuid(getuid(), getuid(),-1);
if (rc) return POPT_ERROR_ERRNO;
if (rc) goto fail_errno;
#else
/*
* XXX " ... on BSD systems setuid() should be preferred over setreuid()"
Expand All @@ -445,22 +445,22 @@ static int execCommand(poptContext con)
*/
#if defined(HAVE_SETUID)
int rc = setgid(getgid());
if (rc) return POPT_ERROR_ERRNO;
if (rc) goto fail_errno;
rc = setuid(getuid());
if (rc) return POPT_ERROR_ERRNO;
if (rc) goto fail_errno;
#elif defined (HAVE_SETREUID)
int rc = setregid(getgid(), getgid());
if (rc) return POPT_ERROR_ERRNO;
if (rc) goto fail_errno;
rc = setreuid(getuid(), getuid());
if (rc) return POPT_ERROR_ERRNO;
if (rc) goto fail_errno;
#else
; /* Can't drop privileges */
#endif
#endif
}

if (argv[0] == NULL)
return POPT_ERROR_NOARG;
goto fail_noarg;

#ifdef MYDEBUG
if (_popt_debug)
Expand All @@ -474,7 +474,12 @@ if (_popt_debug)

execvp(argv[0], (char *const *)argv);

fail_errno:
_free(argv);
return POPT_ERROR_ERRNO;
fail_noarg:
_free(argv);
return POPT_ERROR_NOARG;
}
/*@=bounds =boundswrite @*/

Expand Down