execve(2) System Calls Manual execve(2) execve - LIBRARY Standard C library (libc, -lc) #include int execve(const char *pathname, char *const _Nullable argv[], char *const _Nullable envp[]); execve , pathname. , , , , ( ). pathname , : #! [ ] << >>. argv is an array of pointers to strings passed to the new program as its command-line arguments. By convention, the first of these strings (i.e., argv[0]) should contain the filename associated with the file being executed. The argv array must be terminated by a null pointer. (Thus, in the new program, argv[argc] will be a null pointer.) envp is an array of pointers to strings, conventionally of the form key=value, which are passed as the environment of the new program. The envp array must be terminated by a null pointer. This manual page describes the Linux system call in detail; for an overview of the nomenclature and the many, often preferable, standardised variants of this function provided by libc, including ones that search the PATH environment variable, see exec(3). The argument vector and environment can be accessed by the new program's main function, when it is defined as: int main(int argc, char *argv[], char *envp[]) , POSIX.1; POSIX.1, environ(7). execve() , , , (bss) . ptrace, execve() SIGTRAP. If the set-user-ID bit is set on the program file referred to by pathname, then the effective user ID of the calling process is changed to that of the owner of the program file. Similarly, if the set-group-ID bit is set on the program file, then the effective group ID of the calling process is set to the group of the program file. IDs (. ., set-user-ID set-group-ID ), - : o no_new_privs ( prctl(2)); o nosuid ( MS_NOSUID mount(2)); o ptrace. ( capabilities(7)), - . (set-user-ID), (set-group-ID). , - set-user-ID set-group-ID. The process's real UID and real GID, as well as its supplementary group IDs, are unchanged by a call to execve(). - a.out, , Linux ld.so(8), . ELF, , PT_INTERP. , glibc, /lib/ld-linux.so.2 ( ld-linux.so(8)). Effect on process attributes execve() , : o (signal(7)). o (sigaltstack(2)). o (mmap(2)). o System V (shmat(2)). o POSIX (shm_open(3)). o POSIX (mq_overview(7)). o POSIX (sem_overview(7)). o POSIX (timer_create(2)). o (directory streams) (opendir(3)). o (mlock(2), mlockall(2)). o (exit handlers) (atexit(3), on_exit(3)). o (fenv(3)). POSIX.1 . , Linux, execve(): o The process's "dumpable" attribute is set to the value 1, unless a set-user-ID program, a set-group-ID program, or a program with capabilities is being executed, in which case the dumpable flag may instead be reset to the value in /proc/sys/fs/suid_dumpable, in the circumstances described under PR_SET_DUMPABLE in prctl(2). Note that changes to the "dumpable" attribute may cause ownership of files in the process's /proc/pid directory to change to root:root, as described in proc(5). o PR_SET_KEEPCAPS (prctl(2)) . o ( Linux 2.4.36 / 2.6.23) set-user-ID set-group-ID, , prctl(2) PR_SET_PDEATHSIG, . o , prctl(2) PR_SET_NAME ( ps -o comm), . o SECBIT_KEEP_CAPS securebits . capabilities(7). o (termination signal) SIGCHLD (clone(2)). o , CLONE_FILES clone(2). : o (threads), , execve(). , pthreads . o setlocale(LC_ALL, "C"). o POSIX.1 , , . POSIX.1 : SIGCHLD , (disposition) ; Linux . o - (aio_read(3), aio_write(3)). o (capabilities) execve(), . capabilities(7). o , execve() . , close-on-exec ( ), ; FD_CLOEXEC fcntl(2) ( , , . fcntl(2)). POSIX.1 , 0, 1 2 execve(), - set-user-ID set-group-ID , . , , , , execve(). -- , : #! [ ] . pathname execve() , : [ ] pathname where pathname is the pathname of the file specified as the first argument of execve(), and arg... is the series of words pointed to by the argv argument of execve(), starting at argv[1]. Note that there is no way to get the argv[0] that was passed to the execve() call. , , (.., ); . . Linux 2.6.28 . ( ), , . . UNIX (argv) (envp), . POSIX.1 ARG_MAX ( sysconf(_SC_ARG_MAX)). Before Linux 2.6.23, the memory used to store the environment and argument strings was limited to 32 pages (defined by the kernel constant MAX_ARG_PAGES). On architectures with a 4-kB page size, this yields a maximum size of 128 kB. On Linux 2.6.23 and later, most architectures support a size limit derived from the soft RLIMIT_STACK resource limit (see getrlimit(2)) that is in force at the time of the execve() call. (Architectures with no memory management unit are excepted: they maintain the limit that was in effect before Linux 2.6.23.) This change allows programs to have a much larger argument and/or environment list. For these architectures, the total size is limited to 1/4 of the allowed stack size. (Imposing the 1/4-limit ensures that the new program always has some stack space.) Additionally, the total size is limited to 3/4 of the value of the kernel constant _STK_LIM (8 MiB). Since Linux 2.6.25, the kernel also places a floor of 32 pages on this size limit, so that, even when RLIMIT_STACK is set very low, applications are guaranteed to have at least as much argument and environment space as was provided by Linux 2.6.22 and earlier. (This guarantee was not provided in Linux 2.6.23 and 2.6.24.) Additionally, the limit per string is 32 pages (the kernel constant MAX_ARG_STRLEN), and the maximum number of strings is 0x7FFFFFFF. On success, execve() does not return, on error -1 is returned, and errno is set to indicate the error. E2BIG The total number of bytes in the environment (envp) and argument list (argv) is too large, an argument or environment string is too long, or the full pathname of the executable is too long. The terminating null byte is counted as part of the string length. EACCES pathname ( path_resolution(7)). EACCES . EACCES ELF. EACCES noexec. EAGAIN ( Linux 3.1) Having changed its real UID using one of the set*uid() calls, the caller was--and is now still--above its RLIMIT_NPROC resource limit (see setrlimit(2)). For a more detailed explanation of this error, see NOTES. EFAULT pathname argv envp . EINVAL ELF- PT_INTERP (.., ). EIO -. EISDIR ELF . ELIBBAD ELF. ELOOP pathname, ELF . ELOOP ( << >> ). Linux 3.8 ENOEXEC. EMFILE . ENAMETOOLONG pathname. ENFILE . ENOENT The file pathname or a script or ELF interpreter does not exist. ENOEXEC , , , - . ENOMEM . ENOTDIR pathname, ELF . EPERM nosuid, , set-user-ID set-group-ID. EPERM , , set-user-ID set-group-ID. EPERM << >> (capability-dumb) , . capabilities(7). ETXTBSY . POSIX does not document the #! behavior, but it exists (with some variations) on other UNIX systems. Linux argv envp NULL. , , null. ! UNIX argv NULL (EFAULT). UNIX envp==NULL Linux. POSIX.1 , , sysconf(3), . , Linux 2.6.23, RLIMIT_STACK, , _SC_ARG_MAX, , , . <<#!>> ; . Linux 5.1 127 . Linux 5.1 255 . . Linux, , . , . . , , . Linux ( UNIX) set-user-ID set-group-ID. POSIX.1-2008. POSIX.1-2001, SVr4, 4.3BSD. UNIX V6 exec() 0, main -1. , exec(). UNIX V7 NULL. , execve() ( , exec(3)) , << >>. -- ; ( , PID). , execve(2), ( ) . set-user-ID set-group-ID ptrace(2). nosuid Linux: set-user-ID set-group-ID, ( EPERM), set-user-ID set-group-ID exec(). In most cases where execve() fails, control returns to the original executable image, and the caller of execve() can then handle the error. However, in (rare) cases (typically caused by resource exhaustion), failure may occur past the point of no return: the original executable image has been torn down, but the new image could not be completely built. In such cases, the kernel kills the process with a SIGSEGV (SIGKILL until Linux 3.17) signal. execve() EAGAIN EAGAIN, ( Linux 3.1) execve(). The EAGAIN error can occur when a preceding call to setuid(2), setreuid(2), or setresuid(2) caused the real user ID of the process to change, and that change caused the process to exceed its RLIMIT_NPROC resource limit (i.e., the number of processes belonging to the new real UID exceeds the resource limit). From Linux 2.6.0 to Linux 3.0, this caused the set*uid() call to fail. (Before Linux 2.6, the resource limit was not imposed on processes that changed their user IDs.) Since Linux 3.1, the scenario just described no longer causes the set*uid() call to fail, because it too often led to security holes where buggy applications didn't check the return status and assumed that--if the caller had root privileges--the call would always succeed. Instead, the set*uid() calls now successfully change the real UID, but the kernel sets an internal flag, named PF_NPROC_EXCEEDED, to note that the RLIMIT_NPROC resource limit has been exceeded. If the PF_NPROC_EXCEEDED flag is set and the resource limit is still exceeded at the time of a subsequent execve() call, that call fails with the error EAGAIN. This kernel logic ensures that the RLIMIT_NPROC resource limit is still enforced for the common privileged daemon workflow--namely, fork(2) + set*uid() + execve(). execve() ( , UID set*uid() execve()), execve() PF_NPROC_EXCEEDED . , fork(2). , . . /* myecho.c */ #include #include int main(int argc, char *argv[]) { for (size_t j = 0; j < argc; j++) printf("argv[%zu]: %s\n", j, argv[j]); exit(EXIT_SUCCESS); } , . /* execve.c */ #include #include #include int main(int argc, char *argv[]) { static char *newargv[] = { NULL, "hello", "world", NULL }; static char *newenviron[] = { NULL }; if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } newargv[0] = argv[1]; execve(argv[1], newargv, newenviron); perror("execve"); /* execve() returns only on error */ exit(EXIT_FAILURE); } : $ cc myecho.c -o myecho $ cc execve.c -o execve $ ./execve ./myecho argv[0]: ./myecho argv[1]: hello argv[2]: world . , "" myecho: $ cat > script #!./myecho script-arg ^D $ chmod +x script : $ ./execve ./script argv[0]: ./myecho argv[1]: script-arg argv[2]: ./script argv[3]: hello argv[4]: world . chmod(2), execveat(2), fork(2), get_robust_list(2), ptrace(2), exec(3), fexecve(3), getauxval(3), getopt(3), system(3), capabilities(7), credentials(7), environ(7), path_resolution(7), ld.so(8) Azamat Hackimov , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 1 2023 . execve(2)