Blame man3/fexecve.3

Packit 7cfc04
.\" Copyright (c) 2006, 2014, Michael Kerrisk
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH FEXECVE 3 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
fexecve \- execute program specified via file descriptor
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <unistd.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int fexecve(int " fd ", char *const " argv "[], char *const " envp []);
Packit 7cfc04
.fi
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR fexecve ():
Packit 7cfc04
.PD 0
Packit 7cfc04
.ad l
Packit 7cfc04
.RS 4
Packit 7cfc04
.TP 4
Packit 7cfc04
Since glibc 2.10:
Packit 7cfc04
_POSIX_C_SOURCE\ >=\ 200809L
Packit 7cfc04
.TP
Packit 7cfc04
Before glibc 2.10:
Packit 7cfc04
_GNU_SOURCE
Packit 7cfc04
.RE
Packit 7cfc04
.ad
Packit 7cfc04
.PD
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.BR fexecve ()
Packit 7cfc04
performs the same task as
Packit 7cfc04
.BR execve (2),
Packit 7cfc04
with the difference that the file to be executed
Packit 7cfc04
is specified via a file descriptor,
Packit 7cfc04
.IR fd ,
Packit 7cfc04
rather than via a pathname.
Packit 7cfc04
The file descriptor
Packit 7cfc04
.I fd
Packit 7cfc04
must be opened read-only
Packit 7cfc04
.RB ( O_RDONLY )
Packit 7cfc04
or with the
Packit 7cfc04
.B O_PATH
Packit 7cfc04
flag
Packit 7cfc04
and the caller must have permission to execute the file that it refers to.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
A successful call to
Packit 7cfc04
.BR fexecve ()
Packit 7cfc04
never returns.
Packit 7cfc04
On error, the function does return, with a result value of \-1, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
Errors are as for
Packit 7cfc04
.BR execve (2),
Packit 7cfc04
with the following additions:
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I fd
Packit 7cfc04
is not a valid file descriptor, or
Packit 7cfc04
.I argv
Packit 7cfc04
is NULL, or
Packit 7cfc04
.I envp
Packit 7cfc04
is NULL.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOSYS
Packit 7cfc04
The
Packit 7cfc04
.I /proc
Packit 7cfc04
filesystem could not be accessed.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
.BR fexecve ()
Packit 7cfc04
is implemented since glibc 2.3.2.
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lb lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR fexecve ()
Packit 7cfc04
T}	Thread safety	MT-Safe
Packit 7cfc04
.TE
Packit 7cfc04
.sp 1
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2008.
Packit 7cfc04
This function is not specified in POSIX.1-2001,
Packit 7cfc04
and is not widely available on other systems.
Packit 7cfc04
It is specified in POSIX.1-2008.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
On Linux with glibc versions 2.26 and earlier,
Packit 7cfc04
.BR fexecve ()
Packit 7cfc04
is implemented using the
Packit 7cfc04
.BR proc (5)
Packit 7cfc04
filesystem, so
Packit 7cfc04
.I /proc
Packit 7cfc04
needs to be mounted and available at the time of the call.
Packit 7cfc04
Since glibc 2.27,
Packit 7cfc04
.\" glibc commit 43ffc53a352a67672210c9dd4959f6c6b7407e60
Packit 7cfc04
if the underlying kernel supports the
Packit 7cfc04
.BR execveat (2)
Packit 7cfc04
system call, then
Packit 7cfc04
.BR fexecve ()
Packit 7cfc04
is implemented using that system call, with the benefit that
Packit 7cfc04
.IR /proc
Packit 7cfc04
does not need to be mounted.
Packit 7cfc04
.PP
Packit 7cfc04
The idea behind
Packit 7cfc04
.BR fexecve ()
Packit 7cfc04
is to allow the caller to verify (checksum) the contents of
Packit 7cfc04
an executable before executing it.
Packit 7cfc04
Simply opening the file, checksumming the contents, and then doing an
Packit 7cfc04
.BR execve (2)
Packit 7cfc04
would not suffice, since, between the two steps, the filename,
Packit 7cfc04
or a directory prefix of the pathname, could have been exchanged
Packit 7cfc04
(by, for example, modifying the target of a symbolic link).
Packit 7cfc04
.BR fexecve ()
Packit 7cfc04
does not mitigate the problem that the
Packit 7cfc04
.I contents
Packit 7cfc04
of a file could be changed between the checksumming and the call to
Packit 7cfc04
.BR fexecve ();
Packit 7cfc04
for that, the solution is to ensure that the permissions on the file
Packit 7cfc04
prevent it from being modified by malicious users.
Packit 7cfc04
.PP
Packit 7cfc04
The natural idiom when using
Packit 7cfc04
.BR fexecve ()
Packit 7cfc04
is to set the close-on-exec flag on
Packit 7cfc04
.IR fd ,
Packit 7cfc04
so that the file descriptor does not leak through to the program
Packit 7cfc04
that is executed.
Packit 7cfc04
This approach is natural for two reasons.
Packit 7cfc04
First, it prevents file descriptors being consumed unnecessarily.
Packit 7cfc04
(The executed program normally has no need of a file descriptor
Packit 7cfc04
that refers to the program itself.)
Packit 7cfc04
Second, if
Packit 7cfc04
.BR fexecve ()
Packit 7cfc04
is used recursively,
Packit 7cfc04
employing the close-on-exec flag prevents the file descriptor exhaustion
Packit 7cfc04
that would result from the fact that each step in the recursion would
Packit 7cfc04
cause one more file descriptor to be passed to the new program.
Packit 7cfc04
(But see BUGS.)
Packit 7cfc04
.SH BUGS
Packit 7cfc04
If
Packit 7cfc04
.I fd
Packit 7cfc04
refers to a script (i.e., it is an executable text file that names
Packit 7cfc04
a script interpreter with a first line that begins with the characters
Packit 7cfc04
.IR #! )
Packit 7cfc04
and the close-on-exec flag has been set for
Packit 7cfc04
.IR fd ,
Packit 7cfc04
then
Packit 7cfc04
.BR fexecve ()
Packit 7cfc04
fails with the error
Packit 7cfc04
.BR ENOENT .
Packit 7cfc04
This error occurs because,
Packit 7cfc04
by the time the script interpreter is executed,
Packit 7cfc04
.I fd
Packit 7cfc04
has already been closed because of the close-on-exec flag.
Packit 7cfc04
Thus, the close-on-exec flag can't be set on
Packit 7cfc04
.I fd
Packit 7cfc04
if it refers to a script, leading to the problems described in NOTES.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR execve (2),
Packit 7cfc04
.BR execveat (2)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.