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 ENOENT The close-on-exec flag is set on fd, and fd refers to a script.
39 See BUGS.
40
41 ENOSYS The kernel does not provide the execveat(2) system call, and the
42 /proc filesystem could not be accessed.
43
45 fexecve() is implemented since glibc 2.3.2.
46
48 For an explanation of the terms used in this section, see at‐
49 tributes(7).
50
51 ┌──────────┬───────────────┬─────────┐
52 │Interface │ Attribute │ Value │
53 ├──────────┼───────────────┼─────────┤
54 │fexecve() │ Thread safety │ MT-Safe │
55 └──────────┴───────────────┴─────────┘
56
58 POSIX.1-2008. This function is not specified in POSIX.1-2001, and is
59 not widely available on other systems. It is specified in
60 POSIX.1-2008.
61
63 On Linux with glibc versions 2.26 and earlier, fexecve() is implemented
64 using the proc(5) filesystem, so /proc needs to be mounted and avail‐
65 able at the time of the call. Since glibc 2.27, if the underlying ker‐
66 nel supports the execveat(2) system call, then fexecve() is implemented
67 using that system call, with the benefit that /proc does not need to be
68 mounted.
69
70 The idea behind fexecve() is to allow the caller to verify (checksum)
71 the contents of an executable before executing it. Simply opening the
72 file, checksumming the contents, and then doing an execve(2) would not
73 suffice, since, between the two steps, the filename, or a directory
74 prefix of the pathname, could have been exchanged (by, for example,
75 modifying the target of a symbolic link). fexecve() does not mitigate
76 the problem that the contents of a file could be changed between the
77 checksumming and the call to fexecve(); for that, the solution is to
78 ensure that the permissions on the file prevent it from being modified
79 by malicious users.
80
81 The natural idiom when using fexecve() is to set the close-on-exec flag
82 on fd, so that the file descriptor does not leak through to the program
83 that is executed. This approach is natural for two reasons. First, it
84 prevents file descriptors being consumed unnecessarily. (The executed
85 program normally has no need of a file descriptor that refers to the
86 program itself.) Second, if fexecve() is used recursively, employing
87 the close-on-exec flag prevents the file descriptor exhaustion that
88 would result from the fact that each step in the recursion would cause
89 one more file descriptor to be passed to the new program. (But see
90 BUGS.)
91
93 If fd refers to a script (i.e., it is an executable text file that
94 names a script interpreter with a first line that begins with the char‐
95 acters #!) and the close-on-exec flag has been set for fd, then fex‐
96 ecve() fails with the error ENOENT. This error occurs because, by the
97 time the script interpreter is executed, fd has already been closed be‐
98 cause of the close-on-exec flag. Thus, the close-on-exec flag can't be
99 set on fd if it refers to a script, leading to the problems described
100 in NOTES.
101
103 execve(2), execveat(2)
104
106 This page is part of release 5.10 of the Linux man-pages project. A
107 description of the project, information about reporting bugs, and the
108 latest version of this page, can be found at
109 https://www.kernel.org/doc/man-pages/.
110
111
112
113Linux 2019-10-10 FEXECVE(3)