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