freebsd-dev/lib/libc/gen/exec.c

307 lines
7.0 KiB
C
Raw Normal View History

1994-05-27 05:00:24 +00:00
/*-
* SPDX-License-Identifier: BSD-3-Clause
*
1994-05-27 05:00:24 +00:00
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1994-05-27 05:00:24 +00:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__SCCSID("@(#)exec.c 8.1 (Berkeley) 6/4/93");
__FBSDID("$FreeBSD$");
1994-05-27 05:00:24 +00:00
#include "namespace.h"
1994-05-27 05:00:24 +00:00
#include <sys/param.h>
#include <sys/stat.h>
1994-05-27 05:00:24 +00:00
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <paths.h>
#include <stdarg.h>
#include "un-namespace.h"
#include "libc_private.h"
1994-05-27 05:00:24 +00:00
extern char **environ;
static const char execvPe_err_preamble[] = "execvP: ";
static const char execvPe_err_trailer[] = ": path too long\n";
1994-05-27 05:00:24 +00:00
int
execl(const char *name, const char *arg, ...)
{
va_list ap;
const char **argv;
int n;
1994-05-27 05:00:24 +00:00
va_start(ap, arg);
n = 1;
while (va_arg(ap, char *) != NULL)
n++;
va_end(ap);
argv = alloca((n + 1) * sizeof(*argv));
if (argv == NULL) {
errno = ENOMEM;
return (-1);
}
1994-05-27 05:00:24 +00:00
va_start(ap, arg);
n = 1;
argv[0] = arg;
while ((argv[n] = va_arg(ap, char *)) != NULL)
n++;
1994-05-27 05:00:24 +00:00
va_end(ap);
return (_execve(name, __DECONST(char **, argv), environ));
1994-05-27 05:00:24 +00:00
}
int
execle(const char *name, const char *arg, ...)
{
va_list ap;
const char **argv;
char **envp;
int n;
1994-05-27 05:00:24 +00:00
va_start(ap, arg);
n = 1;
while (va_arg(ap, char *) != NULL)
n++;
1994-05-27 05:00:24 +00:00
va_end(ap);
argv = alloca((n + 1) * sizeof(*argv));
if (argv == NULL) {
errno = ENOMEM;
return (-1);
}
va_start(ap, arg);
n = 1;
argv[0] = arg;
while ((argv[n] = va_arg(ap, char *)) != NULL)
n++;
envp = va_arg(ap, char **);
va_end(ap);
return (_execve(name, __DECONST(char **, argv), envp));
1994-05-27 05:00:24 +00:00
}
int
execlp(const char *name, const char *arg, ...)
{
va_list ap;
const char **argv;
int n;
1994-05-27 05:00:24 +00:00
va_start(ap, arg);
n = 1;
while (va_arg(ap, char *) != NULL)
n++;
1994-05-27 05:00:24 +00:00
va_end(ap);
argv = alloca((n + 1) * sizeof(*argv));
if (argv == NULL) {
errno = ENOMEM;
return (-1);
}
va_start(ap, arg);
n = 1;
argv[0] = arg;
while ((argv[n] = va_arg(ap, char *)) != NULL)
n++;
va_end(ap);
return (execvp(name, __DECONST(char **, argv)));
1994-05-27 05:00:24 +00:00
}
int
execv(const char *name, char * const *argv)
1994-05-27 05:00:24 +00:00
{
(void)_execve(name, argv, environ);
1994-05-27 05:00:24 +00:00
return (-1);
}
int
2003-07-01 12:30:03 +00:00
execvp(const char *name, char * const *argv)
{
return (_execvpe(name, argv, environ));
}
static int
execvPe(const char *name, const char *path, char * const *argv,
char * const *envp)
1994-05-27 05:00:24 +00:00
{
const char **memp;
size_t cnt, lp, ln;
int eacces, save_errno;
char buf[MAXPATHLEN];
const char *bp, *np, *op, *p;
struct stat sb;
1994-05-27 05:00:24 +00:00
eacces = 0;
1994-05-27 05:00:24 +00:00
/* If it's an absolute or relative path name, it's easy. */
if (strchr(name, '/')) {
bp = name;
op = NULL;
1994-05-27 05:00:24 +00:00
goto retry;
}
bp = buf;
/* If it's an empty path name, fail in the usual POSIX way. */
if (*name == '\0') {
errno = ENOENT;
return (-1);
}
op = path;
ln = strlen(name);
while (op != NULL) {
np = strchrnul(op, ':');
1994-05-27 05:00:24 +00:00
/*
* It's a SHELL path -- double, leading and trailing colons
* mean the current directory.
*/
if (np == op) {
/* Empty component. */
1994-05-27 05:00:24 +00:00
p = ".";
lp = 1;
} else {
/* Non-empty component. */
p = op;
lp = np - op;
}
/* Advance to the next component or terminate after this. */
if (*np == '\0')
op = NULL;
else
op = np + 1;
1994-05-27 05:00:24 +00:00
/*
* If the path is too long complain. This is a possible
* security issue; given a way to make the path too long
* the user may execute the wrong program.
*/
if (lp + ln + 2 > sizeof(buf)) {
(void)_write(STDERR_FILENO, execvPe_err_preamble,
sizeof(execvPe_err_preamble) - 1);
(void)_write(STDERR_FILENO, p, lp);
(void)_write(STDERR_FILENO, execvPe_err_trailer,
sizeof(execvPe_err_trailer) - 1);
1994-05-27 05:00:24 +00:00
continue;
}
bcopy(p, buf, lp);
buf[lp] = '/';
bcopy(name, buf + lp + 1, ln);
buf[lp + ln + 1] = '\0';
retry: (void)_execve(bp, argv, envp);
2003-07-01 12:30:03 +00:00
switch (errno) {
case E2BIG:
goto done;
case ELOOP:
case ENAMETOOLONG:
1994-05-27 05:00:24 +00:00
case ENOENT:
break;
case ENOEXEC:
for (cnt = 0; argv[cnt]; ++cnt)
;
execvp: fix up the ENOEXEC fallback If execve fails with ENOEXEC, execvp is expected to rebuild the command with /bin/sh instead and try again. The previous version did this, but overlooked two details: argv[0] can conceivably be NULL, in which case memp would never get terminated. We must allocate no less than three * sizeof(char *) so we can properly terminate at all times. For the non-NULL argv standard case, we count all the non-NULL elements and actually skip the first argument, so we end up capturing the NULL terminator in our bcopy(). The second detail is that the spec is actually worded such that we should have been preserving argv[0] as passed to execvp: "[...] executed command shall be as if the process invoked the sh utility using execl() as follows: execl(<shell path>, arg0, file, arg1, ..., (char *)0); where <shell path> is an unspecified pathname for the sh utility, file is the process image file, and for execvp(), where arg0, arg1, and so on correspond to the values passed to execvp() in argv[0], argv[1], and so on." So we make this change at this time as well, while we're already touching it. We decidedly can't preserve a NULL argv[0] as this would be incredibly, incredibly fragile, so we retain our legacy behavior of using "sh" for argv[] in this specific instance. Some light tests are added to try and detect some components of handling the ENOEXEC fallback; posix_spawnp_enoexec_fallback_null_argv0 is likely not 100% reliable, but it at least won't raise false-alarms and it did result in useful failures with pre-change libc on my machine. This is a secondary change in D25038. Reported by: Andrew Gierth <andrew_tao173.riddles.org.uk> Reviewed by: jilles, kib, Andrew Gierth MFC after: 1 week
2020-06-10 01:30:37 +00:00
/*
* cnt may be 0 above; always allocate at least
* 3 entries so that we can at least fit "sh", bp, and
* the NULL terminator. We can rely on cnt to take into
* account the NULL terminator in all other scenarios,
* as we drop argv[0].
*/
memp = alloca(MAX(3, cnt + 2) * sizeof(char *));
if (memp == NULL) {
/* errno = ENOMEM; XXX override ENOEXEC? */
goto done;
}
execvp: fix up the ENOEXEC fallback If execve fails with ENOEXEC, execvp is expected to rebuild the command with /bin/sh instead and try again. The previous version did this, but overlooked two details: argv[0] can conceivably be NULL, in which case memp would never get terminated. We must allocate no less than three * sizeof(char *) so we can properly terminate at all times. For the non-NULL argv standard case, we count all the non-NULL elements and actually skip the first argument, so we end up capturing the NULL terminator in our bcopy(). The second detail is that the spec is actually worded such that we should have been preserving argv[0] as passed to execvp: "[...] executed command shall be as if the process invoked the sh utility using execl() as follows: execl(<shell path>, arg0, file, arg1, ..., (char *)0); where <shell path> is an unspecified pathname for the sh utility, file is the process image file, and for execvp(), where arg0, arg1, and so on correspond to the values passed to execvp() in argv[0], argv[1], and so on." So we make this change at this time as well, while we're already touching it. We decidedly can't preserve a NULL argv[0] as this would be incredibly, incredibly fragile, so we retain our legacy behavior of using "sh" for argv[] in this specific instance. Some light tests are added to try and detect some components of handling the ENOEXEC fallback; posix_spawnp_enoexec_fallback_null_argv0 is likely not 100% reliable, but it at least won't raise false-alarms and it did result in useful failures with pre-change libc on my machine. This is a secondary change in D25038. Reported by: Andrew Gierth <andrew_tao173.riddles.org.uk> Reviewed by: jilles, kib, Andrew Gierth MFC after: 1 week
2020-06-10 01:30:37 +00:00
if (cnt > 0) {
memp[0] = argv[0];
memp[1] = bp;
bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
} else {
memp[0] = "sh";
memp[1] = bp;
memp[2] = NULL;
}
(void)_execve(_PATH_BSHELL,
__DECONST(char **, memp), envp);
1994-05-27 05:00:24 +00:00
goto done;
case ENOMEM:
goto done;
case ENOTDIR:
break;
1994-05-27 05:00:24 +00:00
case ETXTBSY:
/*
* We used to retry here, but sh(1) doesn't.
*/
goto done;
1994-05-27 05:00:24 +00:00
default:
/*
* EACCES may be for an inaccessible directory or
* a non-executable file. Call stat() to decide
* which. This also handles ambiguities for EFAULT
* and EIO, and undocumented errors like ESTALE.
* We hope that the race for a stat() is unimportant.
*/
save_errno = errno;
if (stat(bp, &sb) != 0)
break;
if (save_errno == EACCES) {
eacces = 1;
continue;
}
errno = save_errno;
1994-05-27 05:00:24 +00:00
goto done;
}
}
if (eacces)
errno = EACCES;
else
1994-05-27 05:00:24 +00:00
errno = ENOENT;
done:
1994-05-27 05:00:24 +00:00
return (-1);
}
int
execvP(const char *name, const char *path, char * const argv[])
{
return execvPe(name, path, argv, environ);
}
int
_execvpe(const char *name, char * const argv[], char * const envp[])
{
const char *path;
/* Get the path we're searching. */
if ((path = getenv("PATH")) == NULL)
path = _PATH_DEFPATH;
return (execvPe(name, path, argv, envp));
}