1fexecve(3) Library Functions Manual fexecve(3)
2
3
4
6 fexecve - execute program specified via file descriptor
7
9 Standard C library (libc, -lc)
10
12 #include <unistd.h>
13
14 int fexecve(int fd, char *const argv[], char *const envp[]);
15
16 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18 fexecve():
19 Since glibc 2.10:
20 _POSIX_C_SOURCE >= 200809L
21 Before glibc 2.10:
22 _GNU_SOURCE
23
25 fexecve() performs the same task as execve(2), with the difference that
26 the file to be executed is specified via a file descriptor, fd, rather
27 than via a pathname. The file descriptor fd must be opened read-only
28 (O_RDONLY) or with the O_PATH flag and the caller must have permission
29 to execute the file that it refers to.
30
32 A successful call to fexecve() never returns. On error, the function
33 does return, with a result value of -1, and errno is set to indicate
34 the error.
35
37 Errors are as for execve(2), with the following additions:
38
39 EINVAL fd is not a valid file descriptor, or argv is NULL, or envp is
40 NULL.
41
42 ENOENT The close-on-exec flag is set on fd, and fd refers to a script.
43 See BUGS.
44
45 ENOSYS The kernel does not provide the execveat(2) system call, and the
46 /proc filesystem could not be accessed.
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.
60
62 glibc 2.3.2.
63
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
72 The idea behind fexecve() is to allow the caller to verify (checksum)
73 the contents of an executable before executing it. Simply opening the
74 file, checksumming the contents, and then doing an execve(2) would not
75 suffice, since, between the two steps, the filename, or a directory
76 prefix of the pathname, could have been exchanged (by, for example,
77 modifying the target of a symbolic link). fexecve() does not mitigate
78 the problem that the contents of a file could be changed between the
79 checksumming and the call to fexecve(); for that, the solution is to
80 ensure that the permissions on the file prevent it from being modified
81 by malicious users.
82
83 The natural idiom when using fexecve() is to set the close-on-exec flag
84 on fd, so that the file descriptor does not leak through to the program
85 that is executed. This approach is natural for two reasons. First, it
86 prevents file descriptors being consumed unnecessarily. (The executed
87 program normally has no need of a file descriptor that refers to the
88 program itself.) Second, if fexecve() is used recursively, employing
89 the close-on-exec flag prevents the file descriptor exhaustion that
90 would result from the fact that each step in the recursion would cause
91 one more file descriptor to be passed to the new program. (But see
92 BUGS.)
93
95 If fd refers to a script (i.e., it is an executable text file that
96 names a script interpreter with a first line that begins with the char‐
97 acters #!) and the close-on-exec flag has been set for fd, then fex‐
98 ecve() fails with the error ENOENT. This error occurs because, by the
99 time the script interpreter is executed, fd has already been closed be‐
100 cause of the close-on-exec flag. Thus, the close-on-exec flag can't be
101 set on fd if it refers to a script, leading to the problems described
102 in NOTES.
103
105 execve(2), execveat(2)
106
107
108
109Linux man-pages 6.04 2023-03-30 fexecve(3)