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