1994-05-27 05:00:24 +00:00
|
|
|
.\" Copyright (c) 1980, 1991, 1993
|
|
|
|
.\" The Regents of the University of California. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
.\" 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 05:00:24 +00:00
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
1997-03-11 11:35:56 +00:00
|
|
|
.\" @(#)execve.2 8.5 (Berkeley) 6/1/94
|
1994-05-27 05:00:24 +00:00
|
|
|
.\"
|
2022-01-25 22:47:23 +00:00
|
|
|
.Dd January 26, 2022
|
1994-05-27 05:00:24 +00:00
|
|
|
.Dt EXECVE 2
|
2001-07-10 13:41:46 +00:00
|
|
|
.Os
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh NAME
|
2008-04-16 13:03:12 +00:00
|
|
|
.Nm execve ,
|
|
|
|
.Nm fexecve
|
1994-05-27 05:00:24 +00:00
|
|
|
.Nd execute a file
|
2000-04-21 09:42:15 +00:00
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libc
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In unistd.h
|
1994-05-27 05:00:24 +00:00
|
|
|
.Ft int
|
|
|
|
.Fn execve "const char *path" "char *const argv[]" "char *const envp[]"
|
2008-04-16 13:03:12 +00:00
|
|
|
.Ft int
|
|
|
|
.Fn fexecve "int fd" "char *const argv[]" "char *const envp[]"
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh DESCRIPTION
|
2002-07-15 20:59:12 +00:00
|
|
|
The
|
|
|
|
.Fn execve
|
2002-12-18 09:22:32 +00:00
|
|
|
system call
|
1994-05-27 05:00:24 +00:00
|
|
|
transforms the calling process into a new process.
|
|
|
|
The new process is constructed from an ordinary file,
|
|
|
|
whose name is pointed to by
|
|
|
|
.Fa path ,
|
|
|
|
called the
|
|
|
|
.Em new process file .
|
2008-04-16 13:03:12 +00:00
|
|
|
The
|
|
|
|
.Fn fexecve
|
|
|
|
system call is equivalent to
|
|
|
|
.Fn execve
|
|
|
|
except that the file to be executed is determined by the file
|
|
|
|
descriptor
|
|
|
|
.Fa fd
|
|
|
|
instead of a
|
|
|
|
.Fa path .
|
1994-05-27 05:00:24 +00:00
|
|
|
This file is either an executable object file,
|
|
|
|
or a file of data for an interpreter.
|
|
|
|
An executable object file consists of an identifying header,
|
|
|
|
followed by pages of data representing the initial program (text)
|
2004-07-02 23:52:20 +00:00
|
|
|
and initialized data pages.
|
|
|
|
Additional pages may be specified
|
2004-07-03 22:30:10 +00:00
|
|
|
by the header to be initialized with zero data; see
|
2000-11-27 15:18:25 +00:00
|
|
|
.Xr elf 5
|
|
|
|
and
|
1994-05-27 05:00:24 +00:00
|
|
|
.Xr a.out 5 .
|
|
|
|
.Pp
|
|
|
|
An interpreter file begins with a line of the form:
|
|
|
|
.Pp
|
2001-02-07 13:45:30 +00:00
|
|
|
.Bd -ragged -offset indent -compact
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sy \&#!
|
|
|
|
.Em interpreter
|
|
|
|
.Bq Em arg
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
When an interpreter file is
|
1997-03-12 14:49:41 +00:00
|
|
|
.Sy execve Ap d ,
|
1997-07-08 18:27:38 +00:00
|
|
|
the system actually
|
1997-03-12 14:49:41 +00:00
|
|
|
.Sy execve Ap s
|
1997-07-08 18:27:38 +00:00
|
|
|
the specified
|
1994-05-27 05:00:24 +00:00
|
|
|
.Em interpreter .
|
|
|
|
If the optional
|
|
|
|
.Em arg
|
|
|
|
is specified, it becomes the first argument to the
|
|
|
|
.Em interpreter ,
|
|
|
|
and the name of the originally
|
1997-03-12 14:49:41 +00:00
|
|
|
.Sy execve Ap d
|
1994-05-27 05:00:24 +00:00
|
|
|
file becomes the second argument;
|
|
|
|
otherwise, the name of the originally
|
1997-03-12 14:49:41 +00:00
|
|
|
.Sy execve Ap d
|
2004-07-02 23:52:20 +00:00
|
|
|
file becomes the first argument.
|
|
|
|
The original arguments are shifted over to
|
2000-08-05 16:38:24 +00:00
|
|
|
become the subsequent arguments.
|
|
|
|
The zeroth argument is set to the specified
|
|
|
|
.Em interpreter .
|
1994-05-27 05:00:24 +00:00
|
|
|
.Pp
|
|
|
|
The argument
|
|
|
|
.Fa argv
|
|
|
|
is a pointer to a null-terminated array of
|
|
|
|
character pointers to null-terminated character strings.
|
|
|
|
These strings construct the argument list to be made available to the new
|
2004-07-02 23:52:20 +00:00
|
|
|
process.
|
|
|
|
At least one argument must be present in
|
1994-05-27 05:00:24 +00:00
|
|
|
the array; by custom, the first element should be
|
|
|
|
the name of the executed program (for example, the last component of
|
|
|
|
.Fa path ) .
|
|
|
|
.Pp
|
|
|
|
The argument
|
|
|
|
.Fa envp
|
|
|
|
is also a pointer to a null-terminated array of
|
|
|
|
character pointers to null-terminated strings.
|
|
|
|
A pointer to this array is normally stored in the global variable
|
2001-02-01 16:38:02 +00:00
|
|
|
.Va environ .
|
1994-05-27 05:00:24 +00:00
|
|
|
These strings pass information to the
|
|
|
|
new process that is not directly an argument to the command (see
|
|
|
|
.Xr environ 7 ) .
|
|
|
|
.Pp
|
|
|
|
File descriptors open in the calling process image remain open in
|
|
|
|
the new process image, except for those for which the close-on-exec
|
|
|
|
flag is set (see
|
|
|
|
.Xr close 2
|
|
|
|
and
|
|
|
|
.Xr fcntl 2 ) .
|
|
|
|
Descriptors that remain open are unaffected by
|
|
|
|
.Fn execve .
|
2002-08-07 18:03:30 +00:00
|
|
|
If any of the standard descriptors (0, 1, and/or 2) are closed at the
|
|
|
|
time
|
|
|
|
.Fn execve
|
|
|
|
is called, and the process will gain privilege as a result of set-id
|
|
|
|
semantics, those descriptors will be re-opened automatically.
|
|
|
|
No programs, whether privileged or not, should assume that these descriptors
|
|
|
|
will remain closed across a call to
|
|
|
|
.Fn execve .
|
1994-05-27 05:00:24 +00:00
|
|
|
.Pp
|
|
|
|
Signals set to be ignored in the calling process are set to be ignored in
|
|
|
|
the
|
2000-03-02 09:14:21 +00:00
|
|
|
new process.
|
|
|
|
Signals which are set to be caught in the calling process image
|
1994-05-27 05:00:24 +00:00
|
|
|
are set to default action in the new process image.
|
|
|
|
Blocked signals remain blocked regardless of changes to the signal action.
|
|
|
|
The signal stack is reset to be undefined (see
|
|
|
|
.Xr sigaction 2
|
|
|
|
for more information).
|
|
|
|
.Pp
|
|
|
|
If the set-user-ID mode bit of the new process image file is set
|
|
|
|
(see
|
|
|
|
.Xr chmod 2 ) ,
|
|
|
|
the effective user ID of the new process image is set to the owner ID
|
|
|
|
of the new process image file.
|
|
|
|
If the set-group-ID mode bit of the new process image file is set,
|
|
|
|
the effective group ID of the new process image is set to the group ID
|
|
|
|
of the new process image file.
|
|
|
|
(The effective group ID is the first element of the group list.)
|
|
|
|
The real user ID, real group ID and
|
|
|
|
other group IDs of the new process image remain the same as the calling
|
|
|
|
process image.
|
|
|
|
After any set-user-ID and set-group-ID processing,
|
|
|
|
the effective user ID is recorded as the saved set-user-ID,
|
|
|
|
and the effective group ID is recorded as the saved set-group-ID.
|
|
|
|
These values may be used in changing the effective IDs later (see
|
|
|
|
.Xr setuid 2 ) .
|
|
|
|
.Pp
|
1999-04-27 03:56:10 +00:00
|
|
|
The set-ID bits are not honored if the respective file system has the
|
2002-12-18 13:33:04 +00:00
|
|
|
.Cm nosuid
|
2004-07-02 23:52:20 +00:00
|
|
|
option enabled or if the new process file is an interpreter file.
|
|
|
|
Syscall
|
1999-04-27 03:56:10 +00:00
|
|
|
tracing is disabled if effective IDs are changed.
|
|
|
|
.Pp
|
1994-05-27 05:00:24 +00:00
|
|
|
The new process also inherits the following attributes from
|
|
|
|
the calling process:
|
|
|
|
.Pp
|
|
|
|
.Bl -column parent_process_ID -offset indent -compact
|
|
|
|
.It process ID Ta see Xr getpid 2
|
|
|
|
.It parent process ID Ta see Xr getppid 2
|
|
|
|
.It process group ID Ta see Xr getpgrp 2
|
|
|
|
.It access groups Ta see Xr getgroups 2
|
|
|
|
.It working directory Ta see Xr chdir 2
|
|
|
|
.It root directory Ta see Xr chroot 2
|
|
|
|
.It control terminal Ta see Xr termios 4
|
|
|
|
.It resource usages Ta see Xr getrusage 2
|
|
|
|
.It interval timers Ta see Xr getitimer 2
|
|
|
|
.It resource limits Ta see Xr getrlimit 2
|
|
|
|
.It file mode mask Ta see Xr umask 2
|
2010-05-06 22:49:54 +00:00
|
|
|
.It signal mask Ta see Xr sigaction 2 ,
|
|
|
|
.Xr sigprocmask 2
|
1994-05-27 05:00:24 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
When a program is executed as a result of an
|
|
|
|
.Fn execve
|
2002-12-18 09:22:32 +00:00
|
|
|
system call, it is entered as follows:
|
1994-05-27 05:00:24 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
main(argc, argv, envp)
|
|
|
|
int argc;
|
|
|
|
char **argv, **envp;
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
where
|
|
|
|
.Fa argc
|
|
|
|
is the number of elements in
|
|
|
|
.Fa argv
|
|
|
|
(the ``arg count'')
|
|
|
|
and
|
|
|
|
.Fa argv
|
|
|
|
points to the array of character pointers
|
|
|
|
to the arguments themselves.
|
2008-04-16 13:03:12 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn fexecve
|
|
|
|
ignores the file offset of
|
|
|
|
.Fa fd .
|
|
|
|
Since execute permission is checked by
|
|
|
|
.Fn fexecve ,
|
|
|
|
the file descriptor
|
|
|
|
.Fa fd
|
2009-07-05 20:20:09 +00:00
|
|
|
need not have been opened with the
|
2008-04-16 13:03:12 +00:00
|
|
|
.Dv O_EXEC
|
|
|
|
flag.
|
2009-07-05 20:20:09 +00:00
|
|
|
However, if the file to be executed denies read permission for the process
|
|
|
|
preparing to do the exec, the only way to provide the
|
2008-04-16 13:03:12 +00:00
|
|
|
.Fa fd
|
|
|
|
to
|
|
|
|
.Fn fexecve
|
|
|
|
is to use the
|
|
|
|
.Dv O_EXEC
|
|
|
|
flag when opening
|
|
|
|
.Fa fd .
|
2009-07-05 20:20:09 +00:00
|
|
|
Note that the file to be executed can not be open for writing.
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh RETURN VALUES
|
|
|
|
As the
|
|
|
|
.Fn execve
|
2002-12-18 09:22:32 +00:00
|
|
|
system call overlays the current process image
|
1994-05-27 05:00:24 +00:00
|
|
|
with a new process image the successful call
|
|
|
|
has no process to return to.
|
|
|
|
If
|
|
|
|
.Fn execve
|
|
|
|
does return to the calling process an error has occurred; the
|
|
|
|
return value will be -1 and the global variable
|
|
|
|
.Va errno
|
|
|
|
is set to indicate the error.
|
|
|
|
.Sh ERRORS
|
2002-07-15 20:59:12 +00:00
|
|
|
The
|
|
|
|
.Fn execve
|
2002-12-18 09:22:32 +00:00
|
|
|
system call
|
1994-05-27 05:00:24 +00:00
|
|
|
will fail and return to the calling process if:
|
2000-05-04 13:09:25 +00:00
|
|
|
.Bl -tag -width Er
|
1994-05-27 05:00:24 +00:00
|
|
|
.It Bq Er ENOTDIR
|
|
|
|
A component of the path prefix is not a directory.
|
|
|
|
.It Bq Er ENAMETOOLONG
|
|
|
|
A component of a pathname exceeded 255 characters,
|
|
|
|
or an entire path name exceeded 1023 characters.
|
Fix exec_imgact_shell()'s handling of two error cases: (1) Previously, if
the first line of a script exceeded MAXSHELLCMDLEN characters, then
exec_imgact_shell() silently truncated the line and passed on the truncated
interpreter name or argument. Now, exec_imgact_shell() will fail and return
ENOEXEC, which is the commonly used errno among Unix variants for this type
of error. (2) Previously, exec_imgact_shell()'s check on the length of the
interpreter's name was ineffective. In other words, exec_imgact_shell()
could not possibly fail and return ENAMETOOLONG. The reason being that the
length of the interpreter name had to exceed MAXSHELLCMDLEN characters in
order that ENAMETOOLONG be returned. But, the search for the end of the
interpreter name stops after at most MAXSHELLCMDLEN - 2 characters are
scanned. (In the end, this particular error is eventually discovered
outside of exec_imgact_shell() and ENAMETOOLONG is returned. So, the real
effect of this second change is that the error is detected earlier, in
exec_imgact_shell().)
Update the definition of MAXINTERP to the actual limit on the size of
the interpreter name that has been in effect since r142453 (from
2005).
In collaboration with: kib
2010-09-21 16:24:51 +00:00
|
|
|
.It Bq Er ENOEXEC
|
|
|
|
When invoking an interpreted script, the length of the first line,
|
|
|
|
inclusive of the
|
|
|
|
.Sy \&#!
|
|
|
|
prefix and terminating newline, exceeds
|
2001-12-04 16:09:27 +00:00
|
|
|
.Dv MAXSHELLCMDLEN
|
|
|
|
characters.
|
1994-05-27 05:00:24 +00:00
|
|
|
.It Bq Er ENOENT
|
|
|
|
The new process file does not exist.
|
|
|
|
.It Bq Er ELOOP
|
|
|
|
Too many symbolic links were encountered in translating the pathname.
|
|
|
|
.It Bq Er EACCES
|
|
|
|
Search permission is denied for a component of the path prefix.
|
|
|
|
.It Bq Er EACCES
|
|
|
|
The new process file is not an ordinary file.
|
|
|
|
.It Bq Er EACCES
|
|
|
|
The new process file mode denies execute permission.
|
2022-01-25 22:47:23 +00:00
|
|
|
.It Bq Er EINVAL
|
|
|
|
.Fa argv
|
|
|
|
did not contain at least one element.
|
1994-05-27 05:00:24 +00:00
|
|
|
.It Bq Er ENOEXEC
|
|
|
|
The new process file has the appropriate access
|
|
|
|
permission, but has an invalid magic number in its header.
|
|
|
|
.It Bq Er ETXTBSY
|
|
|
|
The new process file is a pure procedure (shared text)
|
2009-07-05 20:20:09 +00:00
|
|
|
file that is currently open for writing by some process.
|
1994-05-27 05:00:24 +00:00
|
|
|
.It Bq Er ENOMEM
|
|
|
|
The new process requires more virtual memory than
|
|
|
|
is allowed by the imposed maximum
|
|
|
|
.Pq Xr getrlimit 2 .
|
|
|
|
.It Bq Er E2BIG
|
1998-01-11 21:43:38 +00:00
|
|
|
The number of bytes in the new process' argument list
|
1994-05-27 05:00:24 +00:00
|
|
|
is larger than the system-imposed limit.
|
1998-01-11 21:43:38 +00:00
|
|
|
This limit is specified by the
|
|
|
|
.Xr sysctl 3
|
|
|
|
MIB variable
|
|
|
|
.Dv KERN_ARGMAX .
|
1994-05-27 05:00:24 +00:00
|
|
|
.It Bq Er EFAULT
|
|
|
|
The new process file is not as long as indicated by
|
|
|
|
the size values in its header.
|
|
|
|
.It Bq Er EFAULT
|
2002-12-19 09:40:28 +00:00
|
|
|
The
|
|
|
|
.Fa path ,
|
1994-05-27 05:00:24 +00:00
|
|
|
.Fa argv ,
|
|
|
|
or
|
|
|
|
.Fa envp
|
2002-12-19 09:40:28 +00:00
|
|
|
arguments
|
1994-05-27 05:00:24 +00:00
|
|
|
point
|
|
|
|
to an illegal address.
|
|
|
|
.It Bq Er EIO
|
|
|
|
An I/O error occurred while reading from the file system.
|
2020-03-30 21:44:00 +00:00
|
|
|
.It Bq Er EINTEGRITY
|
|
|
|
Corrupted data was detected while reading from the file system.
|
1994-05-27 05:00:24 +00:00
|
|
|
.El
|
2008-04-16 13:03:12 +00:00
|
|
|
.Pp
|
|
|
|
In addition, the
|
|
|
|
.Fn fexecve
|
|
|
|
will fail and return to the calling process if:
|
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EBADF
|
|
|
|
The
|
|
|
|
.Fa fd
|
|
|
|
argument is not a valid file descriptor open for executing.
|
|
|
|
.El
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh SEE ALSO
|
1999-08-15 09:51:25 +00:00
|
|
|
.Xr ktrace 1 ,
|
2001-07-06 16:46:48 +00:00
|
|
|
.Xr _exit 2 ,
|
2004-07-04 20:55:50 +00:00
|
|
|
.Xr fork 2 ,
|
2008-04-16 13:03:12 +00:00
|
|
|
.Xr open 2 ,
|
1994-05-27 05:00:24 +00:00
|
|
|
.Xr execl 3 ,
|
1996-02-11 22:38:05 +00:00
|
|
|
.Xr exit 3 ,
|
1998-01-11 21:43:38 +00:00
|
|
|
.Xr sysctl 3 ,
|
2000-11-27 15:18:25 +00:00
|
|
|
.Xr a.out 5 ,
|
|
|
|
.Xr elf 5 ,
|
2008-08-26 10:53:32 +00:00
|
|
|
.Xr fdescfs 5 ,
|
1999-08-15 09:51:25 +00:00
|
|
|
.Xr environ 7 ,
|
|
|
|
.Xr mount 8
|
2002-08-07 18:03:30 +00:00
|
|
|
.Sh STANDARDS
|
|
|
|
The
|
|
|
|
.Fn execve
|
2002-12-18 09:22:32 +00:00
|
|
|
system call conforms to
|
2002-08-07 18:03:30 +00:00
|
|
|
.St -p1003.1-2001 ,
|
|
|
|
with the exception of reopening descriptors 0, 1, and/or 2 in certain
|
|
|
|
circumstances.
|
|
|
|
A future update of the Standard is expected to require this behavior,
|
|
|
|
and it may become the default for non-privileged processes as well.
|
|
|
|
.\" NB: update this caveat when TC1 is blessed.
|
|
|
|
The support for executing interpreted programs is an extension.
|
2008-04-16 13:03:12 +00:00
|
|
|
The
|
|
|
|
.Fn fexecve
|
|
|
|
system call conforms to The Open Group Extended API Set 2 specification.
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh HISTORY
|
|
|
|
The
|
1996-08-22 22:05:59 +00:00
|
|
|
.Fn execve
|
2002-12-18 09:22:32 +00:00
|
|
|
system call appeared in
|
2020-12-19 14:54:28 +00:00
|
|
|
.At v7 .
|
2008-04-16 13:03:12 +00:00
|
|
|
The
|
|
|
|
.Fn fexecve
|
|
|
|
system call appeared in
|
|
|
|
.Fx 8.0 .
|
2010-05-13 12:07:55 +00:00
|
|
|
.Sh CAVEATS
|
|
|
|
If a program is
|
|
|
|
.Em setuid
|
|
|
|
to a non-super-user, but is executed when
|
|
|
|
the real
|
|
|
|
.Em uid
|
|
|
|
is ``root'', then the program has some of the powers
|
|
|
|
of a super-user as well.
|
|
|
|
.Pp
|
|
|
|
When executing an interpreted program through
|
|
|
|
.Fn fexecve ,
|
|
|
|
kernel supplies
|
|
|
|
.Pa /dev/fd/n
|
|
|
|
as a second argument to the interpreter,
|
|
|
|
where
|
|
|
|
.Ar n
|
|
|
|
is the file descriptor passed in the
|
|
|
|
.Fa fd
|
|
|
|
argument to
|
|
|
|
.Fn fexecve .
|
|
|
|
For this construction to work correctly, the
|
|
|
|
.Xr fdescfs 5
|
|
|
|
filesystem shall be mounted on
|
|
|
|
.Pa /dev/fd .
|