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