1FEXECVE(3) Linux Programmer's Manual FEXECVE(3)
2
3
4
6 fexecve - execute program specified via file descriptor
7
9 #include <unistd.h>
10
11 int fexecve(int fd, char *const argv[], char *const envp[]);
12
13 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15 fexecve():
16 Since glibc 2.10:
17 _POSIX_C_SOURCE >= 200809L
18 Before glibc 2.10:
19 _GNU_SOURCE
20
22 fexecve() performs the same task as execve(2), with the difference that
23 the file to be executed is specified via a file descriptor, fd, rather
24 than via a pathname. The file descriptor fd must be opened read-only
25 (O_RDONLY) or with the O_PATH flag and the caller must have permission
26 to execute the file that it refers to.
27
29 A successful call to fexecve() never returns. On error, the function
30 does return, with a result value of -1, and errno is set appropriately.
31
33 Errors are as for execve(2), with the following additions:
34
35 EINVAL fd is not a valid file descriptor, or argv is NULL, or envp is
36 NULL.
37
38 ENOSYS The /proc filesystem could not be accessed.
39
41 fexecve() is implemented since glibc 2.3.2.
42
44 For an explanation of the terms used in this section, see
45 attributes(7).
46
47 ┌──────────┬───────────────┬─────────┐
48 │Interface │ Attribute │ Value │
49 ├──────────┼───────────────┼─────────┤
50 │fexecve() │ Thread safety │ MT-Safe │
51 └──────────┴───────────────┴─────────┘
52
54 POSIX.1-2008. This function is not specified in POSIX.1-2001, and is
55 not widely available on other systems. It is specified in
56 POSIX.1-2008.
57
59 On Linux with glibc versions 2.26 and earlier, fexecve() is implemented
60 using the proc(5) filesystem, so /proc needs to be mounted and avail‐
61 able at the time of the call. Since glibc 2.27, if the underlying ker‐
62 nel supports the execveat(2) system call, then fexecve() is implemented
63 using that system call, with the benefit that /proc does not need to be
64 mounted.
65
66 The idea behind fexecve() is to allow the caller to verify (checksum)
67 the contents of an executable before executing it. Simply opening the
68 file, checksumming the contents, and then doing an execve(2) would not
69 suffice, since, between the two steps, the filename, or a directory
70 prefix of the pathname, could have been exchanged (by, for example,
71 modifying the target of a symbolic link). fexecve() does not mitigate
72 the problem that the contents of a file could be changed between the
73 checksumming and the call to fexecve(); for that, the solution is to
74 ensure that the permissions on the file prevent it from being modified
75 by malicious users.
76
77 The natural idiom when using fexecve() is to set the close-on-exec flag
78 on fd, so that the file descriptor does not leak through to the program
79 that is executed. This approach is natural for two reasons. First, it
80 prevents file descriptors being consumed unnecessarily. (The executed
81 program normally has no need of a file descriptor that refers to the
82 program itself.) Second, if fexecve() is used recursively, employing
83 the close-on-exec flag prevents the file descriptor exhaustion that
84 would result from the fact that each step in the recursion would cause
85 one more file descriptor to be passed to the new program. (But see
86 BUGS.)
87
89 If fd refers to a script (i.e., it is an executable text file that
90 names a script interpreter with a first line that begins with the char‐
91 acters #!) and the close-on-exec flag has been set for fd, then fex‐
92 ecve() fails with the error ENOENT. This error occurs because, by the
93 time the script interpreter is executed, fd has already been closed
94 because of the close-on-exec flag. Thus, the close-on-exec flag can't
95 be set on fd if it refers to a script, leading to the problems
96 described in NOTES.
97
99 execve(2), execveat(2)
100
102 This page is part of release 4.16 of the Linux man-pages project. A
103 description of the project, information about reporting bugs, and the
104 latest version of this page, can be found at
105 https://www.kernel.org/doc/man-pages/.
106
107
108
109Linux 2017-09-15 FEXECVE(3)