Debian Codegraph @

upstream/debian-trixie/man2/execve.2

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