1 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
2 .\" and Copyright (c) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Modified by Michael Haardt <michael@moria.de>
7 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
8 .\" Modified 1994-08-21 by Michael Chastain <mec@shell.portal.com>:
9 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
10 .\" Modified 1999-11-12 by Urs Thuermann <urs@isnogud.escape.de>
11 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
12 .\" 2006-09-04 Michael Kerrisk <mtk.manpages@gmail.com>
13 .\" Added list of process attributes that are not preserved on exec().
14 .\" 2007-09-14 Ollie Wild <aaw@google.com>, mtk
15 .\" Add text describing limits on command-line arguments + environment
17 .TH EXECVE 2 2021-08-27 "Linux man-pages (unreleased)"
19 execve \- execute program
22 .RI ( libc ", " \-lc )
25 .B #include <unistd.h>
27 .BI "int execve(const char *" pathname ", char *const " argv [],
28 .BI " char *const " envp []);
32 executes the program referred to by \fIpathname\fP.
33 This causes the program that is currently being run by the calling process
34 to be replaced with a new program, with newly initialized stack, heap,
35 and (initialized and uninitialized) data segments.
37 \fIpathname\fP must be either a binary executable, or a script
38 starting with a line of the form:
42 \fB#!\fP\fIinterpreter \fP[optional-arg]
46 For details of the latter case, see "Interpreter scripts" below.
49 is an array of pointers to strings passed to the new program
50 as its command-line arguments.
51 By convention, the first of these strings (i.e.,
53 should contain the filename associated with the file being executed.
56 array must be terminated by a NULL pointer.
57 (Thus, in the new program,
62 is an array of pointers to strings, conventionally of the form
64 which are passed as the environment of the new program.
67 array must be terminated by a NULL pointer.
69 This manual page describes the Linux system call in detail;
70 for an overview of the nomenclature and the many, often preferable,
71 standardised variants of this function provided by libc,
72 including ones that search the
74 environment variable, see
77 The argument vector and environment can be accessed by the
78 new program's main function, when it is defined as:
82 int main(int argc, char *argv[], char *envp[])
86 Note, however, that the use of a third argument to the main function
87 is not specified in POSIX.1;
89 the environment should be accessed via the external variable
93 does not return on success, and the text, initialized data,
94 uninitialized data (bss), and stack of the calling process are overwritten
95 according to the contents of the newly loaded program.
97 If the current program is being ptraced, a \fBSIGTRAP\fP signal is sent to it
101 If the set-user-ID bit is set on the program file referred to by
103 then the effective user ID of the calling process is changed
104 to that of the owner of the program file.
105 Similarly, if the set-group-ID bit is set on the program file,
106 then the effective group ID of the calling
107 process is set to the group of the program file.
109 The aforementioned transformations of the effective IDs are
111 performed (i.e., the set-user-ID and set-group-ID bits are ignored)
112 if any of the following is true:
116 attribute is set for the calling thread (see
119 the underlying filesystem is mounted
127 the calling process is being ptraced.
129 The capabilities of the program file (see
130 .BR capabilities (7))
131 are also ignored if any of the above are true.
133 The effective user ID of the process is copied to the saved set-user-ID;
134 similarly, the effective group ID is copied to the saved set-group-ID.
135 This copying takes place after any effective ID changes that occur
136 because of the set-user-ID and set-group-ID mode bits.
138 The process's real UID and real GID, as well as its supplementary group IDs,
139 are unchanged by a call to
142 If the executable is an a.out dynamically linked
143 binary executable containing
144 shared-library stubs, the Linux dynamic linker
146 is called at the start of execution to bring
147 needed shared objects into memory
148 and link the executable with them.
150 If the executable is a dynamically linked ELF executable, the
151 interpreter named in the PT_INTERP segment is used to load the needed
153 This interpreter is typically
154 .I /lib/ld\-linux.so.2
155 for binaries linked with glibc (see
156 .BR ld\-linux.so (8)).
158 .SS Effect on process attributes
159 All process attributes are preserved during an
161 except the following:
163 The dispositions of any signals that are being caught are
167 Any alternate signal stack is not preserved
168 .RB ( sigaltstack (2)).
170 Memory mappings are not preserved
173 Attached System\ V shared memory segments are detached
176 POSIX shared memory regions are unmapped
179 Open POSIX message queue descriptors are closed
180 .RB ( mq_overview (7)).
182 Any open POSIX named semaphores are closed
183 .RB ( sem_overview (7)).
185 POSIX timers are not preserved
186 .RB ( timer_create (2)).
188 Any open directory streams are closed
191 Memory locks are not preserved
195 Exit handlers are not preserved
199 The floating-point environment is reset to the default (see
202 The process attributes in the preceding list are all specified
204 The following Linux-specific process attributes are also
205 not preserved during an
208 The process's "dumpable" attribute is set to the value 1,
209 unless a set-user-ID program, a set-group-ID program,
210 or a program with capabilities is being executed,
211 in which case the dumpable flag may instead be reset to the value in
212 .IR /proc/sys/fs/suid_dumpable ,
213 in the circumstances described under
217 Note that changes to the "dumpable" attribute may cause ownership
218 of files in the process's
220 directory to change to
230 (Since Linux 2.4.36 / 2.6.23)
231 If a set-user-ID or set-group-ID program is being executed,
232 then the parent death signal set by
237 The process name, as set by
241 .IR "ps\ \-o comm" ),
242 is reset to the name of the new executable file.
249 .BR capabilities (7).
251 The termination signal is reset to
256 The file descriptor table is unshared, undoing the effect of the
261 Note the following further points:
263 All threads other than the calling thread are destroyed during an
265 Mutexes, condition variables, and other pthreads objects are not preserved.
267 The equivalent of \fIsetlocale(LC_ALL, "C")\fP
268 is executed at program start-up.
270 POSIX.1 specifies that the dispositions of any signals that
271 are ignored or set to the default are left unchanged.
272 POSIX.1 specifies one exception: if
275 then an implementation may leave the disposition unchanged or
276 reset it to the default; Linux does the former.
278 Any outstanding asynchronous I/O operations are canceled
282 For the handling of capabilities during
285 .BR capabilities (7).
287 By default, file descriptors remain open across an
289 File descriptors that are marked close-on-exec are closed;
290 see the description of
294 (If a file descriptor is closed, this will cause the release
295 of all record locks obtained on the underlying file by this process.
299 POSIX.1 says that if file descriptors 0, 1, and 2 would
300 otherwise be closed after a successful
302 and the process would gain privilege because the set-user-ID or
303 set-group-ID mode bit was set on the executed file,
304 then the system may open an unspecified file for each of these
306 As a general principle, no portable program, whether privileged or not,
307 can assume that these three file descriptors will remain
310 .\" On Linux it appears that these file descriptors are
311 .\" always open after an execve(), and it looks like
312 .\" Solaris 8 and FreeBSD 6.1 are the same. -- mtk, 30 Apr 2007
313 .SS Interpreter scripts
314 An interpreter script is a text file that has execute
315 permission enabled and whose first line is of the form:
319 \fB#!\fP\fIinterpreter \fP[optional-arg]
325 must be a valid pathname for an executable file.
331 specifies an interpreter script, then
333 will be invoked with the following arguments:
337 \fIinterpreter\fP [optional-arg] \fIpathname\fP arg...
343 is the pathname of the file specified as the first argument of
347 is the series of words pointed to by the
353 Note that there is no way to get the
355 that was passed to the
358 .\" See the P - preserve-argv[0] option.
359 .\" Documentation/admin-guide/binfmt-misc.rst
360 .\" https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html
364 should either be absent, or be specified as a single word (i.e., it
365 should not contain white space); see NOTES below.
368 .\" commit bf2a9a39639b8b51377905397a5005f444e9a892
369 the kernel permits the interpreter of a script to itself be a script.
370 This permission is recursive, up to a limit of four recursions,
371 so that the interpreter may be a script which is interpreted by a script,
373 .SS Limits on size of arguments and environment
374 Most UNIX implementations impose some limit on the total size
375 of the command-line argument
379 strings that may be passed to a new program.
380 POSIX.1 allows an implementation to advertise this limit using the
382 constant (either defined in
384 or available at run time using the call
385 .IR "sysconf(_SC_ARG_MAX)" ).
387 On Linux prior to kernel 2.6.23, the memory used to store the
388 environment and argument strings was limited to 32 pages
389 (defined by the kernel constant
391 On architectures with a 4-kB page size,
392 this yields a maximum size of 128\ kB.
394 On kernel 2.6.23 and later, most architectures support a size limit
395 derived from the soft
399 that is in force at the time of the
402 (Architectures with no memory management unit are excepted:
403 they maintain the limit that was in effect before kernel 2.6.23.)
404 This change allows programs to have a much larger
405 argument and/or environment list.
406 .\" For some background on the changes to ARG_MAX in kernels 2.6.23 and
408 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=5786
409 .\" http://bugzilla.kernel.org/show_bug.cgi?id=10095
410 .\" http://thread.gmane.org/gmane.linux.kernel/646709/focus=648101,
411 .\" checked into 2.6.25 as commit a64e715fc74b1a7dcc5944f848acc38b2c4d4ee2.
412 For these architectures, the total size is limited to 1/4 of the allowed
414 (Imposing the 1/4-limit
415 ensures that the new program always has some stack space.)
416 .\" Ollie: That doesn't include the lists of pointers, though,
417 .\" so the actual usage is a bit higher (1 pointer per argument).
418 Additionally, the total size is limited to 3/4 of the value
419 of the kernel constant
423 the kernel also places a floor of 32 pages on this size limit,
427 applications are guaranteed to have at least as much argument and
428 environment space as was provided by Linux 2.6.22 and earlier.
429 (This guarantee was not provided in Linux 2.6.23 and 2.6.24.)
430 Additionally, the limit per string is 32 pages (the kernel constant
431 .BR MAX_ARG_STRLEN ),
432 and the maximum number of strings is 0x7FFFFFFF.
436 does not return, on error \-1 is returned, and
438 is set to indicate the error.
442 The total number of bytes in the environment
449 Search permission is denied on a component of the path prefix of
451 or the name of a script interpreter.
453 .BR path_resolution (7).)
456 The file or a script interpreter is not a regular file.
459 Execute permission is denied for the file or a script or ELF interpreter.
462 The filesystem is mounted
465 .BR EAGAIN " (since Linux 3.1)"
466 .\" commit 72fa59970f8698023045ab0713d66f3f4f96945c
467 Having changed its real UID using one of the
469 calls, the caller was\(emand is now still\(emabove its
473 For a more detailed explanation of this error, see NOTES.
477 or one of the pointers in the vectors
481 points outside your accessible address space.
484 An ELF executable had more than one PT_INTERP segment (i.e., tried to
485 name more than one interpreter).
488 An I/O error occurred.
491 An ELF interpreter was a directory.
494 An ELF interpreter was not in a recognized format.
497 Too many symbolic links were encountered in resolving
499 or the name of a script or ELF interpreter.
502 The maximum recursion limit was reached during recursive script
503 interpretation (see "Interpreter scripts", above).
505 .\" commit d740269867021faf4ce38a449353d2b986c34a67
506 the error produced for this case was
510 The per-process limit on the number of open file descriptors has been reached.
517 The system-wide limit on the total number of open files has been reached.
522 or a script or ELF interpreter does not exist.
525 An executable is not in a recognized format, is for the wrong
526 architecture, or has some other format error that means it cannot be
530 Insufficient kernel memory was available.
533 A component of the path prefix of
535 or a script or ELF interpreter is not a directory.
538 The filesystem is mounted
540 the user is not the superuser,
541 and the file has the set-user-ID or set-group-ID bit set.
544 The process is being traced, the user is not the superuser and the
545 file has the set-user-ID or set-group-ID bit set.
548 A "capability-dumb" applications would not obtain the full set of
549 permitted capabilities granted by the executable file.
551 .BR capabilities (7).
554 The specified executable was open for writing by one or more processes.
556 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
557 POSIX does not document the #! behavior, but it exists
558 (with some variations) on other UNIX systems.
559 .\" SVr4 documents additional error
560 .\" conditions EAGAIN, EINTR, ELIBACC, ENOLINK, EMULTIHOP; POSIX does not
561 .\" document ETXTBSY, EPERM, EFAULT, ELOOP, EIO, ENFILE, EMFILE, EINVAL,
562 .\" EISDIR or ELIBBAD error conditions.
566 (and the related functions described in
568 described as "executing a
570 process" (or similar).
571 This is a highly misleading description:
572 there is no new process;
573 many attributes of the calling process remain unchanged
574 (in particular, its PID).
577 does is arrange for an existing process (the calling process)
578 to execute a new program.
580 Set-user-ID and set-group-ID processes can not be
583 The result of mounting a filesystem
585 varies across Linux kernel versions:
586 some will refuse execution of set-user-ID and set-group-ID
587 executables when this would
588 give the user powers they did not have already (and return
590 some will just ignore the set-user-ID and set-group-ID bits and
598 can be specified as NULL.
599 In both cases, this has the same effect as specifying the argument
600 as a pointer to a list containing a single null pointer.
601 .B "Do not take advantage of this nonstandard and nonportable misfeature!"
602 On many other UNIX systems, specifying
604 as NULL will result in an error
607 other UNIX systems treat the
609 case the same as Linux.
610 .\" e.g., EFAULT on Solaris 8 and FreeBSD 6.1; but
611 .\" HP-UX 11 is like Linux -- mtk, Apr 2007
612 .\" Bug filed 30 Apr 2007: http://bugzilla.kernel.org/show_bug.cgi?id=8408
613 .\" Bug rejected (because fix would constitute an ABI change).
616 POSIX.1 says that values returned by
618 should be invariant over the lifetime of a process.
619 However, since Linux 2.6.23, if the
621 resource limit changes, then the value reported by
624 to reflect the fact that the limit on space for holding
625 command-line arguments and environment variables has changed.
629 fails, control returns to the original executable image,
632 can then handle the error.
633 However, in (rare) cases (typically caused by resource exhaustion),
634 failure may occur past the point of no return:
635 the original executable image has been torn down,
636 but the new image could not be completely built.
637 In such cases, the kernel kills the process with a
638 .\" commit 19d860a140beac48a1377f179e693abe86a9dac9
644 .SS Interpreter scripts
645 The kernel imposes a maximum length on the text that follows the
646 "#!" characters at the start of a script;
647 characters beyond the limit are ignored.
648 Before Linux 5.1, the limit is 127 characters.
650 .\" commit 6eb3c3d0a52dca337e327ae8868ca1f44a712e02
651 the limit is 255 characters.
655 argument of an interpreter script vary across implementations.
656 On Linux, the entire string following the
658 name is passed as a single argument to the interpreter,
659 and this string can include white space.
660 However, behavior differs on some other systems.
663 use the first white space to terminate
666 .\" e.g., FreeBSD before 6.0, but not FreeBSD 6.0 onward
667 an interpreter script can have multiple arguments,
670 are used to delimit the arguments.
672 Linux (like most other modern UNIX systems)
673 ignores the set-user-ID and set-group-ID bits on scripts.
676 .\" Some Linux versions have failed to check permissions on ELF
677 .\" interpreters. This is a security hole, because it allows users to
678 .\" open any file, such as a rewinding tape device, for reading. Some
679 .\" Linux versions have also had other security holes in
681 .\" that could be exploited for denial of service by a suitably crafted
682 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
683 .SS execve() and EAGAIN
684 A more detailed explanation of the
686 error that can occur (since Linux 3.1) when calling
692 error can occur when a
699 caused the real user ID of the process to change,
700 and that change caused the process to exceed its
702 resource limit (i.e., the number of processes belonging
703 to the new real UID exceeds the resource limit).
704 From Linux 2.6.0 to 3.0, this caused the
708 .\" commit 909cc4ae86f3380152a18e2a3c44523893ee11c4
709 the resource limit was not imposed on processes that
710 changed their user IDs.)
712 Since Linux 3.1, the scenario just described no longer causes the
715 because it too often led to security holes where buggy applications
716 didn't check the return status and assumed
717 that\(emif the caller had root privileges\(emthe call would always succeed.
720 calls now successfully change the real UID,
721 but the kernel sets an internal flag, named
722 .BR PF_NPROC_EXCEEDED ,
725 resource limit has been exceeded.
728 flag is set and the resource limit is still
729 exceeded at the time of a subsequent
731 call, that call fails with the error
733 This kernel logic ensures that the
735 resource limit is still enforced for the
736 common privileged daemon workflow\(emnamely,
743 If the resource limit was not still exceeded at the time of the
746 (because other processes belonging to this real UID terminated between the
752 call succeeds and the kernel clears the
755 The flag is also cleared if a subsequent call to
757 by this process succeeds.
759 With UNIX\ V6, the argument list of an
762 while the argument list of
765 Thus, this argument list was not directly usable in a further
768 Since UNIX\ V7, both are NULL.
771 .\" Some Linux versions have failed to check permissions on ELF
772 .\" interpreters. This is a security hole, because it allows users to
773 .\" open any file, such as a rewinding tape device, for reading. Some
774 .\" Linux versions have also had other security holes in
776 .\" that could be exploited for denial of service by a suitably crafted
777 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
779 The following program is designed to be execed by the second program below.
780 It just echoes its command-line arguments, one per line.
783 .\" SRC BEGIN (myecho.c)
791 main(int argc, char *argv[])
793 for (int j = 0; j < argc; j++)
794 printf("argv[%d]: %s\en", j, argv[j]);
802 This program can be used to exec the program named in its command-line
806 .\" SRC BEGIN (execve.c)
815 main(int argc, char *argv[])
817 static char *newargv[] = { NULL, "hello", "world", NULL };
818 static char *newenviron[] = { NULL };
821 fprintf(stderr, "Usage: %s <file\-to\-exec>\en", argv[0]);
825 newargv[0] = argv[1];
827 execve(argv[1], newargv, newenviron);
828 perror("execve"); /* execve() returns only on error */
835 We can use the second program to exec the first as follows:
839 .RB "$" " cc myecho.c \-o myecho"
840 .RB "$" " cc execve.c \-o execve"
841 .RB "$" " ./execve ./myecho"
848 We can also use these programs to demonstrate the use of a script
850 To do this we create a script whose "interpreter" is our
856 .RB "$" " cat > script"
857 .B #!./myecho script\-arg
859 .RB "$" " chmod +x script"
863 We can then use our program to exec the script:
867 .RB "$" " ./execve ./script"
879 .BR get_robust_list (2),
886 .BR capabilities (7),
889 .BR path_resolution (7),