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