man-pages/man3/fexecve.3.html
2021-03-31 01:06:50 +01:00

293 lines
6.6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of FEXECVE</TITLE>
</HEAD><BODY>
<H1>FEXECVE</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-10-10<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
fexecve - execute program specified via file descriptor
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<B>int fexecve(int </B><I>fd</I><B>, char *const </B><I>argv</I><B>[], char *const </B><I>envp</I><B>[]);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>fexecve</B>():
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2">Since glibc 2.10:<DD>
_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200809L
<DT id="3">Before glibc 2.10:<DD>
_GNU_SOURCE
</DL>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>fexecve</B>()
performs the same task as
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2),
with the difference that the file to be executed
is specified via a file descriptor,
<I>fd</I>,
rather than via a pathname.
The file descriptor
<I>fd</I>
must be opened read-only
(<B>O_RDONLY</B>)
or with the
<B>O_PATH</B>
flag
and the caller must have permission to execute the file that it refers to.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
A successful call to
<B>fexecve</B>()
never returns.
On error, the function does return, with a result value of -1, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
Errors are as for
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2),
with the following additions:
<DL COMPACT>
<DT id="4"><B>EINVAL</B>
<DD>
<I>fd</I>
is not a valid file descriptor, or
<I>argv</I>
is NULL, or
<I>envp</I>
is NULL.
<DT id="5"><B>ENOENT</B>
<DD>
The close-on-exec flag is set on
<I>fd</I>,
and
<I>fd</I>
refers to a script.
See BUGS.
<DT id="6"><B>ENOSYS</B>
<DD>
The kernel does not provide the
<B><A HREF="/cgi-bin/man/man2html?2+execveat">execveat</A></B>(2)
system call, and the
<I>/proc</I>
filesystem could not be accessed.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
<B>fexecve</B>()
is implemented since glibc 2.3.2.
<A NAME="lbAH">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>fexecve</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2008.
This function is not specified in POSIX.1-2001,
and is not widely available on other systems.
It is specified in POSIX.1-2008.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
On Linux with glibc versions 2.26 and earlier,
<B>fexecve</B>()
is implemented using the
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5)
filesystem, so
<I>/proc</I>
needs to be mounted and available at the time of the call.
Since glibc 2.27,
if the underlying kernel supports the
<B><A HREF="/cgi-bin/man/man2html?2+execveat">execveat</A></B>(2)
system call, then
<B>fexecve</B>()
is implemented using that system call, with the benefit that
<I>/proc</I>
does not need to be mounted.
<P>
The idea behind
<B>fexecve</B>()
is to allow the caller to verify (checksum) the contents of
an executable before executing it.
Simply opening the file, checksumming the contents, and then doing an
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2)
would not suffice, since, between the two steps, the filename,
or a directory prefix of the pathname, could have been exchanged
(by, for example, modifying the target of a symbolic link).
<B>fexecve</B>()
does not mitigate the problem that the
<I>contents</I>
of a file could be changed between the checksumming and the call to
<B>fexecve</B>();
for that, the solution is to ensure that the permissions on the file
prevent it from being modified by malicious users.
<P>
The natural idiom when using
<B>fexecve</B>()
is to set the close-on-exec flag on
<I>fd</I>,
so that the file descriptor does not leak through to the program
that is executed.
This approach is natural for two reasons.
First, it prevents file descriptors being consumed unnecessarily.
(The executed program normally has no need of a file descriptor
that refers to the program itself.)
Second, if
<B>fexecve</B>()
is used recursively,
employing the close-on-exec flag prevents the file descriptor exhaustion
that would result from the fact that each step in the recursion would
cause one more file descriptor to be passed to the new program.
(But see BUGS.)
<A NAME="lbAK">&nbsp;</A>
<H2>BUGS</H2>
If
<I>fd</I>
refers to a script (i.e., it is an executable text file that names
a script interpreter with a first line that begins with the characters
<I>#!</I>)
and the close-on-exec flag has been set for
<I>fd</I>,
then
<B>fexecve</B>()
fails with the error
<B>ENOENT</B>.
This error occurs because,
by the time the script interpreter is executed,
<I>fd</I>
has already been closed because of the close-on-exec flag.
Thus, the close-on-exec flag can't be set on
<I>fd</I>
if it refers to a script, leading to the problems described in NOTES.
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+execveat">execveat</A></B>(2)
<A NAME="lbAM">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="7"><A HREF="#lbAB">NAME</A><DD>
<DT id="8"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="9"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="10"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="11"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="12"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="13"><A HREF="#lbAH">ATTRIBUTES</A><DD>
<DT id="14"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="15"><A HREF="#lbAJ">NOTES</A><DD>
<DT id="16"><A HREF="#lbAK">BUGS</A><DD>
<DT id="17"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="18"><A HREF="#lbAM">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:42 GMT, March 31, 2021
</BODY>
</HTML>