]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/execve.2
execve.2: tfix
[thirdparty/man-pages.git] / man2 / execve.2
1 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
2 .\" and Copyright (c) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
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
36 .\"
37 .TH EXECVE 2 2019-05-09 "Linux" "Linux Programmer's Manual"
38 .SH NAME
39 execve \- execute program
40 .SH SYNOPSIS
41 .B #include <unistd.h>
42 .PP
43 .BI "int execve(const char *" pathname ", char *const " argv "[], "
44 .br
45 .BI " char *const " envp []);
46 .SH DESCRIPTION
47 .BR execve ()
48 executes the program referred to by \fIpathname\fP.
49 This causes the program that is currently being run by the calling process
50 to be replaced with a new program, with newly initialized stack, heap,
51 and (initialized and uninitialized) data segments.
52 .PP
53 \fIpathname\fP must be either a binary executable, or a script
54 starting with a line of the form:
55 .PP
56 .in +4n
57 .EX
58 \fB#!\fP\fIinterpreter \fP[optional-arg]
59 .EE
60 .in
61 .PP
62 For details of the latter case, see "Interpreter scripts" below.
63 .PP
64 \fIargv\fP is an array of argument strings passed to the new program.
65 By convention, the first of these strings (i.e.,
66 .IR argv[0] )
67 should contain the filename associated with the file being executed.
68 \fIenvp\fP is an array of strings, conventionally of the form
69 \fBkey=value\fP, which are passed as environment to the new program.
70 The \fIargv\fP and \fIenvp\fP arrays must each include a null pointer
71 at the end of the array.
72 .PP
73 The argument vector and environment can be accessed by the
74 called program's main function, when it is defined as:
75 .PP
76 .in +4n
77 .EX
78 int main(int argc, char *argv[], char *envp[])
79 .EE
80 .in
81 .PP
82 Note, however, that the use of a third argument to the main function
83 is not specified in POSIX.1;
84 according to POSIX.1,
85 the environment should be accessed via the external variable
86 .BR environ (7).
87 .PP
88 .BR execve ()
89 does not return on success, and the text, initialized data,
90 uninitialized data (bss), and stack of the calling process are overwritten
91 according to the contents of the newly loaded program.
92 .PP
93 If the current program is being ptraced, a \fBSIGTRAP\fP signal is sent to it
94 after a successful
95 .BR execve ().
96 .PP
97 If the set-user-ID bit is set on the program file referred to by
98 \fIpathname\fP,
99 then the effective user ID of the calling process is changed
100 to that of the owner of the program file.
101 Similarly, when the set-group-ID
102 bit of the program file is set the effective group ID of the calling
103 process is set to the group of the program file.
104 .PP
105 The aforementioned transformations of the effective IDs are
106 .I not
107 performed (i.e., the set-user-ID and set-group-ID bits are ignored)
108 if any of the following is true:
109 .IP * 3
110 the
111 .I no_new_privs
112 attribute is set for the calling thread (see
113 .BR prctl (2));
114 .IP *
115 the underlying filesystem is mounted
116 .I nosuid
117 (the
118 .B MS_NOSUID
119 flag for
120 .BR mount (2));
121 or
122 .IP *
123 the calling process is being ptraced.
124 .PP
125 The capabilities of the program file (see
126 .BR capabilities (7))
127 are also ignored if any of the above are true.
128 .PP
129 The effective user ID of the process is copied to the saved set-user-ID;
130 similarly, the effective group ID is copied to the saved set-group-ID.
131 This copying takes place after any effective ID changes that occur
132 because of the set-user-ID and set-group-ID mode bits.
133 .PP
134 The process's real UID and real GID, as well its supplementary group IDs,
135 are unchanged by a call to
136 .BR execve ().
137 .PP
138 If the executable is an a.out dynamically linked
139 binary executable containing
140 shared-library stubs, the Linux dynamic linker
141 .BR ld.so (8)
142 is called at the start of execution to bring
143 needed shared objects into memory
144 and link the executable with them.
145 .PP
146 If the executable is a dynamically linked ELF executable, the
147 interpreter named in the PT_INTERP segment is used to load the needed
148 shared objects.
149 This interpreter is typically
150 .I /lib/ld-linux.so.2
151 for binaries linked with glibc (see
152 .BR ld-linux.so (8)).
153 .PP
154 All process attributes are preserved during an
155 .BR execve (),
156 except the following:
157 .IP * 3
158 The dispositions of any signals that are being caught are
159 reset to the default
160 .RB ( signal (7)).
161 .IP *
162 Any alternate signal stack is not preserved
163 .RB ( sigaltstack (2)).
164 .IP *
165 Memory mappings are not preserved
166 .RB ( mmap (2)).
167 .IP *
168 Attached System\ V shared memory segments are detached
169 .RB ( shmat (2)).
170 .IP *
171 POSIX shared memory regions are unmapped
172 .RB ( shm_open (3)).
173 .IP *
174 Open POSIX message queue descriptors are closed
175 .RB ( mq_overview (7)).
176 .IP *
177 Any open POSIX named semaphores are closed
178 .RB ( sem_overview (7)).
179 .IP *
180 POSIX timers are not preserved
181 .RB ( timer_create (2)).
182 .IP *
183 Any open directory streams are closed
184 .RB ( opendir (3)).
185 .IP *
186 Memory locks are not preserved
187 .RB ( mlock (2),
188 .BR mlockall (2)).
189 .IP *
190 Exit handlers are not preserved
191 .RB ( atexit (3),
192 .BR on_exit (3)).
193 .IP *
194 The floating-point environment is reset to the default (see
195 .BR fenv (3)).
196 .PP
197 The process attributes in the preceding list are all specified
198 in POSIX.1.
199 The following Linux-specific process attributes are also
200 not preserved during an
201 .BR execve ():
202 .IP * 3
203 The
204 .BR prctl (2)
205 .B PR_SET_DUMPABLE
206 flag is set,
207 unless a set-user-ID or set-group ID program is being executed,
208 in which case it is cleared.
209 .IP *
210 The
211 .BR prctl (2)
212 .B PR_SET_KEEPCAPS
213 flag is cleared.
214 .IP *
215 (Since Linux 2.4.36 / 2.6.23)
216 If a set-user-ID or set-group-ID program is being executed,
217 then the parent death signal set by
218 .BR prctl (2)
219 .B PR_SET_PDEATHSIG
220 flag is cleared.
221 .IP *
222 The process name, as set by
223 .BR prctl (2)
224 .B PR_SET_NAME
225 (and displayed by
226 .IR "ps\ \-o comm" ),
227 is reset to the name of the new executable file.
228 .IP *
229 The
230 .B SECBIT_KEEP_CAPS
231 .I securebits
232 flag is cleared.
233 See
234 .BR capabilities (7).
235 .IP *
236 The termination signal is reset to
237 .B SIGCHLD
238 (see
239 .BR clone (2)).
240 .IP *
241 The file descriptor table is unshared, undoing the effect of the
242 .B CLONE_FILES
243 flag of
244 .BR clone (2).
245 .PP
246 Note the following further points:
247 .IP * 3
248 All threads other than the calling thread are destroyed during an
249 .BR execve ().
250 Mutexes, condition variables, and other pthreads objects are not preserved.
251 .IP *
252 The equivalent of \fIsetlocale(LC_ALL, "C")\fP
253 is executed at program start-up.
254 .IP *
255 POSIX.1 specifies that the dispositions of any signals that
256 are ignored or set to the default are left unchanged.
257 POSIX.1 specifies one exception: if
258 .B SIGCHLD
259 is being ignored,
260 then an implementation may leave the disposition unchanged or
261 reset it to the default; Linux does the former.
262 .IP *
263 Any outstanding asynchronous I/O operations are canceled
264 .RB ( aio_read (3),
265 .BR aio_write (3)).
266 .IP *
267 For the handling of capabilities during
268 .BR execve (),
269 see
270 .BR capabilities (7).
271 .IP *
272 By default, file descriptors remain open across an
273 .BR execve ().
274 File descriptors that are marked close-on-exec are closed;
275 see the description of
276 .B FD_CLOEXEC
277 in
278 .BR fcntl (2).
279 (If a file descriptor is closed, this will cause the release
280 of all record locks obtained on the underlying file by this process.
281 See
282 .BR fcntl (2)
283 for details.)
284 POSIX.1 says that if file descriptors 0, 1, and 2 would
285 otherwise be closed after a successful
286 .BR execve (),
287 and the process would gain privilege because the set-user-ID or
288 set-group_ID mode bit was set on the executed file,
289 then the system may open an unspecified file for each of these
290 file descriptors.
291 As a general principle, no portable program, whether privileged or not,
292 can assume that these three file descriptors will remain
293 closed across an
294 .BR execve ().
295 .\" On Linux it appears that these file descriptors are
296 .\" always open after an execve(), and it looks like
297 .\" Solaris 8 and FreeBSD 6.1 are the same. -- mtk, 30 Apr 2007
298 .SS Interpreter scripts
299 An interpreter script is a text file that has execute
300 permission enabled and whose first line is of the form:
301 .PP
302 .in +4n
303 .EX
304 \fB#!\fP\fIinterpreter \fP[optional-arg]
305 .EE
306 .in
307 .PP
308 The
309 .I interpreter
310 must be a valid pathname for an executable file.
311 .PP
312 If the
313 .I pathname
314 argument of
315 .BR execve ()
316 specifies an interpreter script, then
317 .I interpreter
318 will be invoked with the following arguments:
319 .PP
320 .in +4n
321 .EX
322 \fIinterpreter\fP [optional-arg] \fIpathname\fP arg...
323 .EE
324 .in
325 .PP
326 where
327 .I pathname
328 is the absolute pathname of the file specified as the first argument of
329 .BR execve (),
330 and
331 .I arg...
332 is the series of words pointed to by the
333 .I argv
334 argument of
335 .BR execve (),
336 starting at
337 .IR argv [1].
338 Note that there is no way to get the
339 .IR argv[0]
340 that was passed to the
341 .BR execve ()
342 call.
343 .\" See the P - preserve-argv[0] option.
344 .\" Documentation/admin-guide/binfmt-misc.rst
345 .\" https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html
346 .PP
347 For portable use,
348 .I optional-arg
349 should either be absent, or be specified as a single word (i.e., it
350 should not contain white space); see NOTES below.
351 .PP
352 Since Linux 2.6.28,
353 .\" commit bf2a9a39639b8b51377905397a5005f444e9a892
354 the kernel permits the interpreter of a script to itself be a script.
355 This permission is recursive, up to a limit of four recursions,
356 so that the interpreter may be a script which is interpreted by a script,
357 and so on.
358 .SS Limits on size of arguments and environment
359 Most UNIX implementations impose some limit on the total size
360 of the command-line argument
361 .RI ( argv )
362 and environment
363 .RI ( envp )
364 strings that may be passed to a new program.
365 POSIX.1 allows an implementation to advertise this limit using the
366 .B ARG_MAX
367 constant (either defined in
368 .I <limits.h>
369 or available at run time using the call
370 .IR "sysconf(_SC_ARG_MAX)" ).
371 .PP
372 On Linux prior to kernel 2.6.23, the memory used to store the
373 environment and argument strings was limited to 32 pages
374 (defined by the kernel constant
375 .BR MAX_ARG_PAGES ).
376 On architectures with a 4-kB page size,
377 this yields a maximum size of 128\ kB.
378 .PP
379 On kernel 2.6.23 and later, most architectures support a size limit
380 derived from the soft
381 .B RLIMIT_STACK
382 resource limit (see
383 .BR getrlimit (2))
384 that is in force at the time of the
385 .BR execve ()
386 call.
387 (Architectures with no memory management unit are excepted:
388 they maintain the limit that was in effect before kernel 2.6.23.)
389 This change allows programs to have a much larger
390 argument and/or environment list.
391 .\" For some background on the changes to ARG_MAX in kernels 2.6.23 and
392 .\" 2.6.25, see:
393 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=5786
394 .\" http://bugzilla.kernel.org/show_bug.cgi?id=10095
395 .\" http://thread.gmane.org/gmane.linux.kernel/646709/focus=648101,
396 .\" checked into 2.6.25 as commit a64e715fc74b1a7dcc5944f848acc38b2c4d4ee2.
397 For these architectures, the total size is limited to 1/4 of the allowed
398 stack size.
399 (Imposing the 1/4-limit
400 ensures that the new program always has some stack space.)
401 .\" Ollie: That doesn't include the lists of pointers, though,
402 .\" so the actual usage is a bit higher (1 pointer per argument).
403 Additionally, the total size is limited to 3/4 of the value
404 of the kernel constant
405 .B _STK_LIM
406 (8 Mibibytes).
407 Since Linux 2.6.25,
408 the kernel also places a floor of 32 pages on this size limit,
409 so that, even when
410 .BR RLIMIT_STACK
411 is set very low,
412 applications are guaranteed to have at least as much argument and
413 environment space as was provided by Linux 2.6.23 and earlier.
414 (This guarantee was not provided in Linux 2.6.23 and 2.6.24.)
415 Additionally, the limit per string is 32 pages (the kernel constant
416 .BR MAX_ARG_STRLEN ),
417 and the maximum number of strings is 0x7FFFFFFF.
418 .SH RETURN VALUE
419 On success,
420 .BR execve ()
421 does not return, on error \-1 is returned, and
422 .I errno
423 is set appropriately.
424 .SH ERRORS
425 .TP
426 .B E2BIG
427 The total number of bytes in the environment
428 .RI ( envp )
429 and argument list
430 .RI ( argv )
431 is too large.
432 .TP
433 .B EACCES
434 Search permission is denied on a component of the path prefix of
435 .I pathname
436 or the name of a script interpreter.
437 (See also
438 .BR path_resolution (7).)
439 .TP
440 .B EACCES
441 The file or a script interpreter is not a regular file.
442 .TP
443 .B EACCES
444 Execute permission is denied for the file or a script or ELF interpreter.
445 .TP
446 .B EACCES
447 The filesystem is mounted
448 .IR noexec .
449 .TP
450 .BR EAGAIN " (since Linux 3.1)"
451 .\" commit 72fa59970f8698023045ab0713d66f3f4f96945c
452 Having changed its real UID using one of the
453 .BR set*uid ()
454 calls, the caller was\(emand is now still\(emabove its
455 .BR RLIMIT_NPROC
456 resource limit (see
457 .BR setrlimit (2)).
458 For a more detailed explanation of this error, see NOTES.
459 .TP
460 .B EFAULT
461 .I pathname
462 or one of the pointers in the vectors
463 .I argv
464 or
465 .I envp
466 points outside your accessible address space.
467 .TP
468 .B EINVAL
469 An ELF executable had more than one PT_INTERP segment (i.e., tried to
470 name more than one interpreter).
471 .TP
472 .B EIO
473 An I/O error occurred.
474 .TP
475 .B EISDIR
476 An ELF interpreter was a directory.
477 .TP
478 .B ELIBBAD
479 An ELF interpreter was not in a recognized format.
480 .TP
481 .B ELOOP
482 Too many symbolic links were encountered in resolving
483 .I pathname
484 or the name of a script or ELF interpreter.
485 .TP
486 .B ELOOP
487 The maximum recursion limit was reached during recursive script
488 interpretation (see "Interpreter scripts", above).
489 Before Linux 3.8,
490 .\" commit d740269867021faf4ce38a449353d2b986c34a67
491 the error produced for this case was
492 .BR ENOEXEC .
493 .TP
494 .B EMFILE
495 The per-process limit on the number of open file descriptors has been reached.
496 .TP
497 .B ENAMETOOLONG
498 .I pathname
499 is too long.
500 .TP
501 .B ENFILE
502 The system-wide limit on the total number of open files has been reached.
503 .TP
504 .B ENOENT
505 The file
506 .I pathname
507 or a script or ELF interpreter does not exist, or a shared library
508 .\" FIXME but see http://sourceware.org/bugzilla/show_bug.cgi?id=12241
509 needed for the file or interpreter cannot be found.
510 .TP
511 .B ENOEXEC
512 An executable is not in a recognized format, is for the wrong
513 architecture, or has some other format error that means it cannot be
514 executed.
515 .TP
516 .B ENOMEM
517 Insufficient kernel memory was available.
518 .TP
519 .B ENOTDIR
520 A component of the path prefix of
521 .I pathname
522 or a script or ELF interpreter is not a directory.
523 .TP
524 .B EPERM
525 The filesystem is mounted
526 .IR nosuid ,
527 the user is not the superuser,
528 and the file has the set-user-ID or set-group-ID bit set.
529 .TP
530 .B EPERM
531 The process is being traced, the user is not the superuser and the
532 file has the set-user-ID or set-group-ID bit set.
533 .TP
534 .B EPERM
535 A "capability-dumb" applications would not obtain the full set of
536 permitted capabilities granted by the executable file.
537 See
538 .BR capabilities (7).
539 .TP
540 .B ETXTBSY
541 The specified executable was open for writing by one or more processes.
542 .SH CONFORMING TO
543 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
544 POSIX does not document the #! behavior, but it exists
545 (with some variations) on other UNIX systems.
546 .\" SVr4 documents additional error
547 .\" conditions EAGAIN, EINTR, ELIBACC, ENOLINK, EMULTIHOP; POSIX does not
548 .\" document ETXTBSY, EPERM, EFAULT, ELOOP, EIO, ENFILE, EMFILE, EINVAL,
549 .\" EISDIR or ELIBBAD error conditions.
550 .SH NOTES
551 One sometimes sees
552 .BR execve ()
553 (and the related functions described in
554 .BR exec (3))
555 described as "executing a
556 .I new
557 process" (or similar).
558 This is a highly misleading description:
559 there is no new process;
560 many attributes of the calling process remain unchanged
561 (in particular, its PID).
562 All that
563 .BR execve (2)
564 does is arrange for an existing process (the calling process)
565 to execute a new program.
566 .PP
567 Set-user-ID and set-group-ID processes can not be
568 .BR ptrace (2)d.
569 .PP
570 The result of mounting a filesystem
571 .I nosuid
572 varies across Linux kernel versions:
573 some will refuse execution of set-user-ID and set-group-ID
574 executables when this would
575 give the user powers they did not have already (and return
576 .BR EPERM ),
577 some will just ignore the set-user-ID and set-group-ID bits and
578 .BR exec ()
579 successfully.
580 .PP
581 On Linux,
582 .I argv
583 and
584 .I envp
585 can be specified as NULL.
586 In both cases, this has the same effect as specifying the argument
587 as a pointer to a list containing a single null pointer.
588 .B "Do not take advantage of this nonstandard and nonportable misfeature!"
589 On many other UNIX systems, specifying
590 .I argv
591 as NULL will result in an error
592 .RB ( EFAULT ).
593 .I Some
594 other UNIX systems treat the
595 .I envp==NULL
596 case the same as Linux.
597 .\" e.g., EFAULT on Solaris 8 and FreeBSD 6.1; but
598 .\" HP-UX 11 is like Linux -- mtk, Apr 2007
599 .\" Bug filed 30 Apr 2007: http://bugzilla.kernel.org/show_bug.cgi?id=8408
600 .\" Bug rejected (because fix would constitute an ABI change).
601 .\"
602 .PP
603 POSIX.1 says that values returned by
604 .BR sysconf (3)
605 should be invariant over the lifetime of a process.
606 However, since Linux 2.6.23, if the
607 .BR RLIMIT_STACK
608 resource limit changes, then the value reported by
609 .B _SC_ARG_MAX
610 will also change,
611 to reflect the fact that the limit on space for holding
612 command-line arguments and environment variables has changed.
613 .PP
614 In most cases where
615 .BR execve ()
616 fails, control returns to the original executable image,
617 and the caller of
618 .BR execve ()
619 can then handle the error.
620 However, in (rare) cases (typically caused by resource exhaustion),
621 failure may occur past the point of no return:
622 the original executable image has been torn down,
623 but the new image could not be completely built.
624 In such cases, the kernel kills the process with a
625 .BR SIGKILL
626 signal.
627 .\"
628 .SS Interpreter scripts
629 The kernel imposes a maximum length on the text that follows the
630 "#!" characters at the start of a script;
631 characters beyond the limit are ignored.
632 Before Linux 5.1, the limit is 127 characters.
633 Since Linux 5.1,
634 .\" commit 6eb3c3d0a52dca337e327ae8868ca1f44a712e02
635 the limit is 255 characters.
636 .PP
637 The semantics of the
638 .I optional-arg
639 argument of an interpreter script vary across implementations.
640 On Linux, the entire string following the
641 .I interpreter
642 name is passed as a single argument to the interpreter,
643 and this string can include white space.
644 However, behavior differs on some other systems.
645 Some systems
646 .\" e.g., Solaris 8
647 use the first white space to terminate
648 .IR optional-arg .
649 On some systems,
650 .\" e.g., FreeBSD before 6.0, but not FreeBSD 6.0 onward
651 an interpreter script can have multiple arguments,
652 and white spaces in
653 .I optional-arg
654 are used to delimit the arguments.
655 .PP
656 Linux (like most other modern UNIX systems)
657 ignores the set-user-ID and set-group-ID bits on scripts.
658 .\"
659 .\" .SH BUGS
660 .\" Some Linux versions have failed to check permissions on ELF
661 .\" interpreters. This is a security hole, because it allows users to
662 .\" open any file, such as a rewinding tape device, for reading. Some
663 .\" Linux versions have also had other security holes in
664 .\" .BR execve ()
665 .\" that could be exploited for denial of service by a suitably crafted
666 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
667 .SS execve() and EAGAIN
668 A more detailed explanation of the
669 .BR EAGAIN
670 error that can occur (since Linux 3.1) when calling
671 .BR execve ()
672 is as follows.
673 .PP
674 The
675 .BR EAGAIN
676 error can occur when a
677 .I preceding
678 call to
679 .BR setuid (2),
680 .BR setreuid (2),
681 or
682 .BR setresuid (2)
683 caused the real user ID of the process to change,
684 and that change caused the process to exceed its
685 .BR RLIMIT_NPROC
686 resource limit (i.e., the number of processes belonging
687 to the new real UID exceeds the resource limit).
688 From Linux 2.6.0 to 3.0, this caused the
689 .BR set*uid ()
690 call to fail.
691 (Prior to 2.6,
692 .\" commit 909cc4ae86f3380152a18e2a3c44523893ee11c4
693 the resource limit was not imposed on processes that
694 changed their user IDs.)
695 .PP
696 Since Linux 3.1, the scenario just described no longer causes the
697 .BR set*uid ()
698 call to fail,
699 because it too often led to security holes where buggy applications
700 didn't check the return status and assumed
701 that\(emif the caller had root privileges\(emthe call would always succeed.
702 Instead, the
703 .BR set*uid ()
704 calls now successfully change the real UID,
705 but the kernel sets an internal flag, named
706 .BR PF_NPROC_EXCEEDED ,
707 to note that the
708 .BR RLIMIT_NPROC
709 resource limit has been exceeded.
710 If the
711 .BR PF_NPROC_EXCEEDED
712 flag is set and the resource limit is still
713 exceeded at the time of a subsequent
714 .BR execve ()
715 call, that call fails with the error
716 .BR EAGAIN .
717 This kernel logic ensures that the
718 .BR RLIMIT_NPROC
719 resource limit is still enforced for the
720 common privileged daemon workflow\(emnamely,
721 .BR fork (2)
722 +
723 .BR set*uid ()
724 +
725 .BR execve ().
726 .PP
727 If the resource limit was not still exceeded at the time of the
728 .BR execve ()
729 call
730 (because other processes belonging to this real UID terminated between the
731 .BR set*uid ()
732 call and the
733 .BR execve ()
734 call), then the
735 .BR execve ()
736 call succeeds and the kernel clears the
737 .BR PF_NPROC_EXCEEDED
738 process flag.
739 The flag is also cleared if a subsequent call to
740 .BR fork (2)
741 by this process succeeds.
742 .SS Historical
743 With UNIX\ V6, the argument list of an
744 .BR exec ()
745 call was ended by 0,
746 while the argument list of
747 .I main
748 was ended by \-1.
749 Thus, this argument list was not directly usable in a further
750 .BR exec ()
751 call.
752 Since UNIX\ V7, both are NULL.
753 .\"
754 .\" .SH BUGS
755 .\" Some Linux versions have failed to check permissions on ELF
756 .\" interpreters. This is a security hole, because it allows users to
757 .\" open any file, such as a rewinding tape device, for reading. Some
758 .\" Linux versions have also had other security holes in
759 .\" .BR execve ()
760 .\" that could be exploited for denial of service by a suitably crafted
761 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
762 .SH EXAMPLE
763 The following program is designed to be execed by the second program below.
764 It just echoes its command-line arguments, one per line.
765 .PP
766 .in +4n
767 .EX
768 /* myecho.c */
769
770 #include <stdio.h>
771 #include <stdlib.h>
772
773 int
774 main(int argc, char *argv[])
775 {
776 int j;
777
778 for (j = 0; j < argc; j++)
779 printf("argv[%d]: %s\en", j, argv[j]);
780
781 exit(EXIT_SUCCESS);
782 }
783 .EE
784 .in
785 .PP
786 This program can be used to exec the program named in its command-line
787 argument:
788 .PP
789 .in +4n
790 .EX
791 /* execve.c */
792
793 #include <stdio.h>
794 #include <stdlib.h>
795 #include <unistd.h>
796
797 int
798 main(int argc, char *argv[])
799 {
800 char *newargv[] = { NULL, "hello", "world", NULL };
801 char *newenviron[] = { NULL };
802
803 if (argc != 2) {
804 fprintf(stderr, "Usage: %s <file\-to\-exec>\en", argv[0]);
805 exit(EXIT_FAILURE);
806 }
807
808 newargv[0] = argv[1];
809
810 execve(argv[1], newargv, newenviron);
811 perror("execve"); /* execve() returns only on error */
812 exit(EXIT_FAILURE);
813 }
814 .EE
815 .in
816 .PP
817 We can use the second program to exec the first as follows:
818 .PP
819 .in +4n
820 .EX
821 .RB "$" " cc myecho.c \-o myecho"
822 .RB "$" " cc execve.c \-o execve"
823 .RB "$" " ./execve ./myecho"
824 argv[0]: ./myecho
825 argv[1]: hello
826 argv[2]: world
827 .EE
828 .in
829 .PP
830 We can also use these programs to demonstrate the use of a script
831 interpreter.
832 To do this we create a script whose "interpreter" is our
833 .I myecho
834 program:
835 .PP
836 .in +4n
837 .EX
838 .RB "$" " cat > script"
839 .B #!./myecho script-arg
840 .B ^D
841 .RB "$" " chmod +x script"
842 .EE
843 .in
844 .PP
845 We can then use our program to exec the script:
846 .PP
847 .in +4n
848 .EX
849 .RB "$" " ./execve ./script"
850 argv[0]: ./myecho
851 argv[1]: script-arg
852 argv[2]: ./script
853 argv[3]: hello
854 argv[4]: world
855 .EE
856 .in
857 .SH SEE ALSO
858 .BR chmod (2),
859 .BR execveat (2),
860 .BR fork (2),
861 .BR get_robust_list (2),
862 .BR ptrace (2),
863 .BR exec (3),
864 .BR fexecve (3),
865 .BR getopt (3),
866 .BR system (3),
867 .BR credentials (7),
868 .BR environ (7),
869 .BR path_resolution (7),
870 .BR ld.so (8)