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 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified by Michael Haardt <michael@moria.de>
27 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1994-08-21 by Michael Chastain <mec@shell.portal.com>:
29 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified 1999-11-12 by Urs Thuermann <urs@isnogud.escape.de>
31 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
32 .\" 2006-09-04 Michael Kerrisk <mtk.manpages@gmail.com>
33 .\" Added list of process attributes that are not preserved on exec().
34 .\" 2007-09-14 Ollie Wild <aaw@google.com>, mtk
35 .\" Add text describing limits on command-line arguments + environment
37 .TH EXECVE 2 2021-08-27 "Linux" "Linux Programmer's Manual"
39 execve \- execute program
42 .B #include <unistd.h>
44 .BI "int execve(const char *" pathname ", char *const " argv [],
45 .BI " char *const " envp []);
49 executes the program referred to by \fIpathname\fP.
50 This causes the program that is currently being run by the calling process
51 to be replaced with a new program, with newly initialized stack, heap,
52 and (initialized and uninitialized) data segments.
54 \fIpathname\fP must be either a binary executable, or a script
55 starting with a line of the form:
59 \fB#!\fP\fIinterpreter \fP[optional-arg]
63 For details of the latter case, see "Interpreter scripts" below.
65 \fIargv\fP is an array of pointers to strings passed to the new program
66 as its command-line arguments.
67 By convention, the first of these strings (i.e.,
69 should contain the filename associated with the file being executed.
72 array must be terminated by a NULL pointer.
73 (Thus, in the new program,
77 \fIenvp\fP is an array of pointers to strings, conventionally of the form
78 \fBkey=value\fP, which are passed as the environment of the new program.
81 array must be terminated by a NULL pointer.
83 The argument vector and environment can be accessed by the
84 new program's main function, when it is defined as:
88 int main(int argc, char *argv[], char *envp[])
92 Note, however, that the use of a third argument to the main function
93 is not specified in POSIX.1;
95 the environment should be accessed via the external variable
99 does not return on success, and the text, initialized data,
100 uninitialized data (bss), and stack of the calling process are overwritten
101 according to the contents of the newly loaded program.
103 If the current program is being ptraced, a \fBSIGTRAP\fP signal is sent to it
107 If the set-user-ID bit is set on the program file referred to by
109 then the effective user ID of the calling process is changed
110 to that of the owner of the program file.
111 Similarly, if the set-group-ID bit is set on the program file,
112 then the effective group ID of the calling
113 process is set to the group of the program file.
115 The aforementioned transformations of the effective IDs are
117 performed (i.e., the set-user-ID and set-group-ID bits are ignored)
118 if any of the following is true:
122 attribute is set for the calling thread (see
125 the underlying filesystem is mounted
133 the calling process is being ptraced.
135 The capabilities of the program file (see
136 .BR capabilities (7))
137 are also ignored if any of the above are true.
139 The effective user ID of the process is copied to the saved set-user-ID;
140 similarly, the effective group ID is copied to the saved set-group-ID.
141 This copying takes place after any effective ID changes that occur
142 because of the set-user-ID and set-group-ID mode bits.
144 The process's real UID and real GID, as well as its supplementary group IDs,
145 are unchanged by a call to
148 If the executable is an a.out dynamically linked
149 binary executable containing
150 shared-library stubs, the Linux dynamic linker
152 is called at the start of execution to bring
153 needed shared objects into memory
154 and link the executable with them.
156 If the executable is a dynamically linked ELF executable, the
157 interpreter named in the PT_INTERP segment is used to load the needed
159 This interpreter is typically
160 .I /lib/ld\-linux.so.2
161 for binaries linked with glibc (see
162 .BR ld\-linux.so (8)).
164 .SS Effect on process attributes
165 All process attributes are preserved during an
167 except the following:
169 The dispositions of any signals that are being caught are
173 Any alternate signal stack is not preserved
174 .RB ( sigaltstack (2)).
176 Memory mappings are not preserved
179 Attached System\ V shared memory segments are detached
182 POSIX shared memory regions are unmapped
185 Open POSIX message queue descriptors are closed
186 .RB ( mq_overview (7)).
188 Any open POSIX named semaphores are closed
189 .RB ( sem_overview (7)).
191 POSIX timers are not preserved
192 .RB ( timer_create (2)).
194 Any open directory streams are closed
197 Memory locks are not preserved
201 Exit handlers are not preserved
205 The floating-point environment is reset to the default (see
208 The process attributes in the preceding list are all specified
210 The following Linux-specific process attributes are also
211 not preserved during an
214 The process's "dumpable" attribute is set to the value 1,
215 unless a set-user-ID program, a set-group-ID program,
216 or a program with capabilities is being executed,
217 in which case the dumpable flag may instead be reset to the value in
218 .IR /proc/sys/fs/suid_dumpable ,
219 in the circumstances described under
223 Note that changes to the "dumpable" attribute may cause ownership
224 of files in the process's
226 directory to change to
236 (Since Linux 2.4.36 / 2.6.23)
237 If a set-user-ID or set-group-ID program is being executed,
238 then the parent death signal set by
243 The process name, as set by
247 .IR "ps\ \-o comm" ),
248 is reset to the name of the new executable file.
255 .BR capabilities (7).
257 The termination signal is reset to
262 The file descriptor table is unshared, undoing the effect of the
267 Note the following further points:
269 All threads other than the calling thread are destroyed during an
271 Mutexes, condition variables, and other pthreads objects are not preserved.
273 The equivalent of \fIsetlocale(LC_ALL, "C")\fP
274 is executed at program start-up.
276 POSIX.1 specifies that the dispositions of any signals that
277 are ignored or set to the default are left unchanged.
278 POSIX.1 specifies one exception: if
281 then an implementation may leave the disposition unchanged or
282 reset it to the default; Linux does the former.
284 Any outstanding asynchronous I/O operations are canceled
288 For the handling of capabilities during
291 .BR capabilities (7).
293 By default, file descriptors remain open across an
295 File descriptors that are marked close-on-exec are closed;
296 see the description of
300 (If a file descriptor is closed, this will cause the release
301 of all record locks obtained on the underlying file by this process.
305 POSIX.1 says that if file descriptors 0, 1, and 2 would
306 otherwise be closed after a successful
308 and the process would gain privilege because the set-user-ID or
309 set-group-ID mode bit was set on the executed file,
310 then the system may open an unspecified file for each of these
312 As a general principle, no portable program, whether privileged or not,
313 can assume that these three file descriptors will remain
316 .\" On Linux it appears that these file descriptors are
317 .\" always open after an execve(), and it looks like
318 .\" Solaris 8 and FreeBSD 6.1 are the same. -- mtk, 30 Apr 2007
319 .SS Interpreter scripts
320 An interpreter script is a text file that has execute
321 permission enabled and whose first line is of the form:
325 \fB#!\fP\fIinterpreter \fP[optional-arg]
331 must be a valid pathname for an executable file.
337 specifies an interpreter script, then
339 will be invoked with the following arguments:
343 \fIinterpreter\fP [optional-arg] \fIpathname\fP arg...
349 is the pathname of the file specified as the first argument of
353 is the series of words pointed to by the
359 Note that there is no way to get the
361 that was passed to the
364 .\" See the P - preserve-argv[0] option.
365 .\" Documentation/admin-guide/binfmt-misc.rst
366 .\" https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html
370 should either be absent, or be specified as a single word (i.e., it
371 should not contain white space); see NOTES below.
374 .\" commit bf2a9a39639b8b51377905397a5005f444e9a892
375 the kernel permits the interpreter of a script to itself be a script.
376 This permission is recursive, up to a limit of four recursions,
377 so that the interpreter may be a script which is interpreted by a script,
379 .SS Limits on size of arguments and environment
380 Most UNIX implementations impose some limit on the total size
381 of the command-line argument
385 strings that may be passed to a new program.
386 POSIX.1 allows an implementation to advertise this limit using the
388 constant (either defined in
390 or available at run time using the call
391 .IR "sysconf(_SC_ARG_MAX)" ).
393 On Linux prior to kernel 2.6.23, the memory used to store the
394 environment and argument strings was limited to 32 pages
395 (defined by the kernel constant
397 On architectures with a 4-kB page size,
398 this yields a maximum size of 128\ kB.
400 On kernel 2.6.23 and later, most architectures support a size limit
401 derived from the soft
405 that is in force at the time of the
408 (Architectures with no memory management unit are excepted:
409 they maintain the limit that was in effect before kernel 2.6.23.)
410 This change allows programs to have a much larger
411 argument and/or environment list.
412 .\" For some background on the changes to ARG_MAX in kernels 2.6.23 and
414 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=5786
415 .\" http://bugzilla.kernel.org/show_bug.cgi?id=10095
416 .\" http://thread.gmane.org/gmane.linux.kernel/646709/focus=648101,
417 .\" checked into 2.6.25 as commit a64e715fc74b1a7dcc5944f848acc38b2c4d4ee2.
418 For these architectures, the total size is limited to 1/4 of the allowed
420 (Imposing the 1/4-limit
421 ensures that the new program always has some stack space.)
422 .\" Ollie: That doesn't include the lists of pointers, though,
423 .\" so the actual usage is a bit higher (1 pointer per argument).
424 Additionally, the total size is limited to 3/4 of the value
425 of the kernel constant
429 the kernel also places a floor of 32 pages on this size limit,
433 applications are guaranteed to have at least as much argument and
434 environment space as was provided by Linux 2.6.22 and earlier.
435 (This guarantee was not provided in Linux 2.6.23 and 2.6.24.)
436 Additionally, the limit per string is 32 pages (the kernel constant
437 .BR MAX_ARG_STRLEN ),
438 and the maximum number of strings is 0x7FFFFFFF.
442 does not return, on error \-1 is returned, and
444 is set to indicate the error.
448 The total number of bytes in the environment
455 Search permission is denied on a component of the path prefix of
457 or the name of a script interpreter.
459 .BR path_resolution (7).)
462 The file or a script interpreter is not a regular file.
465 Execute permission is denied for the file or a script or ELF interpreter.
468 The filesystem is mounted
471 .BR EAGAIN " (since Linux 3.1)"
472 .\" commit 72fa59970f8698023045ab0713d66f3f4f96945c
473 Having changed its real UID using one of the
475 calls, the caller was\(emand is now still\(emabove its
479 For a more detailed explanation of this error, see NOTES.
483 or one of the pointers in the vectors
487 points outside your accessible address space.
490 An ELF executable had more than one PT_INTERP segment (i.e., tried to
491 name more than one interpreter).
494 An I/O error occurred.
497 An ELF interpreter was a directory.
500 An ELF interpreter was not in a recognized format.
503 Too many symbolic links were encountered in resolving
505 or the name of a script or ELF interpreter.
508 The maximum recursion limit was reached during recursive script
509 interpretation (see "Interpreter scripts", above).
511 .\" commit d740269867021faf4ce38a449353d2b986c34a67
512 the error produced for this case was
516 The per-process limit on the number of open file descriptors has been reached.
523 The system-wide limit on the total number of open files has been reached.
528 or a script or ELF interpreter does not exist.
531 An executable is not in a recognized format, is for the wrong
532 architecture, or has some other format error that means it cannot be
536 Insufficient kernel memory was available.
539 A component of the path prefix of
541 or a script or ELF interpreter is not a directory.
544 The filesystem is mounted
546 the user is not the superuser,
547 and the file has the set-user-ID or set-group-ID bit set.
550 The process is being traced, the user is not the superuser and the
551 file has the set-user-ID or set-group-ID bit set.
554 A "capability-dumb" applications would not obtain the full set of
555 permitted capabilities granted by the executable file.
557 .BR capabilities (7).
560 The specified executable was open for writing by one or more processes.
562 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
563 POSIX does not document the #! behavior, but it exists
564 (with some variations) on other UNIX systems.
565 .\" SVr4 documents additional error
566 .\" conditions EAGAIN, EINTR, ELIBACC, ENOLINK, EMULTIHOP; POSIX does not
567 .\" document ETXTBSY, EPERM, EFAULT, ELOOP, EIO, ENFILE, EMFILE, EINVAL,
568 .\" EISDIR or ELIBBAD error conditions.
572 (and the related functions described in
574 described as "executing a
576 process" (or similar).
577 This is a highly misleading description:
578 there is no new process;
579 many attributes of the calling process remain unchanged
580 (in particular, its PID).
583 does is arrange for an existing process (the calling process)
584 to execute a new program.
586 Set-user-ID and set-group-ID processes can not be
589 The result of mounting a filesystem
591 varies across Linux kernel versions:
592 some will refuse execution of set-user-ID and set-group-ID
593 executables when this would
594 give the user powers they did not have already (and return
596 some will just ignore the set-user-ID and set-group-ID bits and
604 can be specified as NULL.
605 In both cases, this has the same effect as specifying the argument
606 as a pointer to a list containing a single null pointer.
607 .B "Do not take advantage of this nonstandard and nonportable misfeature!"
608 On many other UNIX systems, specifying
610 as NULL will result in an error
613 other UNIX systems treat the
615 case the same as Linux.
616 .\" e.g., EFAULT on Solaris 8 and FreeBSD 6.1; but
617 .\" HP-UX 11 is like Linux -- mtk, Apr 2007
618 .\" Bug filed 30 Apr 2007: http://bugzilla.kernel.org/show_bug.cgi?id=8408
619 .\" Bug rejected (because fix would constitute an ABI change).
622 POSIX.1 says that values returned by
624 should be invariant over the lifetime of a process.
625 However, since Linux 2.6.23, if the
627 resource limit changes, then the value reported by
630 to reflect the fact that the limit on space for holding
631 command-line arguments and environment variables has changed.
635 fails, control returns to the original executable image,
638 can then handle the error.
639 However, in (rare) cases (typically caused by resource exhaustion),
640 failure may occur past the point of no return:
641 the original executable image has been torn down,
642 but the new image could not be completely built.
643 In such cases, the kernel kills the process with a
644 .\" commit 19d860a140beac48a1377f179e693abe86a9dac9
650 .SS Interpreter scripts
651 The kernel imposes a maximum length on the text that follows the
652 "#!" characters at the start of a script;
653 characters beyond the limit are ignored.
654 Before Linux 5.1, the limit is 127 characters.
656 .\" commit 6eb3c3d0a52dca337e327ae8868ca1f44a712e02
657 the limit is 255 characters.
661 argument of an interpreter script vary across implementations.
662 On Linux, the entire string following the
664 name is passed as a single argument to the interpreter,
665 and this string can include white space.
666 However, behavior differs on some other systems.
669 use the first white space to terminate
672 .\" e.g., FreeBSD before 6.0, but not FreeBSD 6.0 onward
673 an interpreter script can have multiple arguments,
676 are used to delimit the arguments.
678 Linux (like most other modern UNIX systems)
679 ignores the set-user-ID and set-group-ID bits on scripts.
682 .\" Some Linux versions have failed to check permissions on ELF
683 .\" interpreters. This is a security hole, because it allows users to
684 .\" open any file, such as a rewinding tape device, for reading. Some
685 .\" Linux versions have also had other security holes in
687 .\" that could be exploited for denial of service by a suitably crafted
688 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
689 .SS execve() and EAGAIN
690 A more detailed explanation of the
692 error that can occur (since Linux 3.1) when calling
698 error can occur when a
705 caused the real user ID of the process to change,
706 and that change caused the process to exceed its
708 resource limit (i.e., the number of processes belonging
709 to the new real UID exceeds the resource limit).
710 From Linux 2.6.0 to 3.0, this caused the
714 .\" commit 909cc4ae86f3380152a18e2a3c44523893ee11c4
715 the resource limit was not imposed on processes that
716 changed their user IDs.)
718 Since Linux 3.1, the scenario just described no longer causes the
721 because it too often led to security holes where buggy applications
722 didn't check the return status and assumed
723 that\(emif the caller had root privileges\(emthe call would always succeed.
726 calls now successfully change the real UID,
727 but the kernel sets an internal flag, named
728 .BR PF_NPROC_EXCEEDED ,
731 resource limit has been exceeded.
733 .BR PF_NPROC_EXCEEDED
734 flag is set and the resource limit is still
735 exceeded at the time of a subsequent
737 call, that call fails with the error
739 This kernel logic ensures that the
741 resource limit is still enforced for the
742 common privileged daemon workflow\(emnamely,
749 If the resource limit was not still exceeded at the time of the
752 (because other processes belonging to this real UID terminated between the
758 call succeeds and the kernel clears the
759 .BR PF_NPROC_EXCEEDED
761 The flag is also cleared if a subsequent call to
763 by this process succeeds.
765 With UNIX\ V6, the argument list of an
768 while the argument list of
771 Thus, this argument list was not directly usable in a further
774 Since UNIX\ V7, both are NULL.
777 .\" Some Linux versions have failed to check permissions on ELF
778 .\" interpreters. This is a security hole, because it allows users to
779 .\" open any file, such as a rewinding tape device, for reading. Some
780 .\" Linux versions have also had other security holes in
782 .\" that could be exploited for denial of service by a suitably crafted
783 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
785 The following program is designed to be execed by the second program below.
786 It just echoes its command-line arguments, one per line.
796 main(int argc, char *argv[])
798 for (int j = 0; j < argc; j++)
799 printf("argv[%d]: %s\en", j, argv[j]);
806 This program can be used to exec the program named in its command-line
818 main(int argc, char *argv[])
820 char *newargv[] = { NULL, "hello", "world", NULL };
821 char *newenviron[] = { NULL };
824 fprintf(stderr, "Usage: %s <file\-to\-exec>\en", argv[0]);
828 newargv[0] = argv[1];
830 execve(argv[1], newargv, newenviron);
831 perror("execve"); /* execve() returns only on error */
837 We can use the second program to exec the first as follows:
841 .RB "$" " cc myecho.c \-o myecho"
842 .RB "$" " cc execve.c \-o execve"
843 .RB "$" " ./execve ./myecho"
850 We can also use these programs to demonstrate the use of a script
852 To do this we create a script whose "interpreter" is our
858 .RB "$" " cat > script"
859 .B #!./myecho script\-arg
861 .RB "$" " chmod +x script"
865 We can then use our program to exec the script:
869 .RB "$" " ./execve ./script"
881 .BR get_robust_list (2),
888 .BR capabilities (7),
891 .BR path_resolution (7),