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