EXECVE(2)

πŸ’… EXECVE β€” the "become a whole new program" syscall ✨

okay so bestie, this is the EXECVE(2) page β€” it lives in the System Calls Manual (section 2, the syscall girlies 🫢) and it covers execve and fexecve. Buckle up, it's iconic.


✨ NAME ✨

execve, fexecve β€” execute a file πŸƒβ™€οΈπŸ’¨

πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc) πŸŽ€

🌸 SYNOPSIS 🌸

#include <unistd.h>

int
execve(const char *path, char *const argv[], char *const envp[]);

int
fexecve(int fd, char *const argv[], char *const envp[]);

πŸ¦‹ DESCRIPTION πŸ¦‹

So execve() is a system call and what it does is transform the calling process into a new process. Like a full glow-up, except the old you genuinely does not survive it. The new process gets constructed from an ordinary file whose name is pointed to by path β€” the docs call that the new process file. And fexecve() is equivalent to execve() except the file to be executed is determined by the file descriptor fd instead of a path. fd girlies stay winning πŸ’…

This file is either an executable object file, or a file of data for an interpreter. An executable object file is made of an identifying header, followed by pages of data representing the initial program (text) and initialized data pages. Extra pages can be specified by the header to be initialized with zero data β€” see elf(5) and a.out(5).

An interpreter file starts with a line of this exact form:

#! interpreter [arg]

When an interpreter file gets execve'd, the system actually execve's the specified interpreter. If that optional arg is specified, it becomes the first argument to the interpreter, and the name of the originally execve'd file becomes the second argument. If arg is not specified, the name of the originally execve'd file becomes the first argument. Either way the original arguments are shifted over to become the subsequent arguments, and the zeroth argument is set to the specified interpreter. periodt. πŸ”₯

The argument argv is a pointer to a null-terminated array of character pointers to null-terminated character strings. These strings construct the argument list made available to the new process. At least one argument must be present in the array; and by custom, the first element should be the name of the executed program (for example, the last component of path).

The argument envp is also a pointer to a null-terminated array of character pointers to null-terminated strings. A pointer to this array is normally stored in the global variable environ. These strings pass information to the new process that isn't directly an argument to the command β€” see environ(7).

File descriptors open in the calling process image remain open in the new process image, except for those with the close-on-exec flag set (see close(2) and fcntl(2)). Descriptors that remain open are unaffected by execve(). Now here's the spicy part: if any of the standard descriptors (0, 1, and/or 2) are closed at the time execve() is called, and the process will gain privilege as a result of set-id semantics, those descriptors will be re-opened automatically. And like β€” no programs, whether privileged or not, should assume that these descriptors will remain closed across a call to execve(). πŸ’€

Signals set to be ignored in the calling process are set to be ignored in the new process. Signals set to be caught in the calling process image are set to default action in the new process image. Blocked signals remain blocked regardless of changes to the signal action. The signal stack is reset to be undefined (see sigaction(2) for more info).

Now, the set-ID section πŸ‘‘ β€” if the set-user-ID mode bit of the new process image file is set (see chmod(2)), the effective user ID of the new process image is set to the owner ID of the new process image file. If the set-group-ID mode bit of the new process image file is set, the effective group ID of the new process image is set to the group ID of the new process image file. (The effective group ID is the first element of the group list.) The real user ID, real group ID and other group IDs of the new process image remain the same as the calling process image. After any set-user-ID and set-group-ID processing, the effective user ID is recorded as the saved set-user-ID, and the effective group ID is recorded as the saved set-group-ID. These values may be used in changing the effective IDs later (see setuid(2)).

The set-ID bits are not honored if the respective file system has the nosuid option enabled, or if the new process file is an interpreter file. Syscall tracing is disabled if effective IDs are changed.

The new process also inherits these attributes from the calling process:

When a program is executed as a result of an execve() system call, it is entered like this:

main(argc, argv, envp)
int argc;
char **argv, **envp;

where argc is the number of elements in argv (the ``arg count'') and argv points to the array of character pointers to the arguments themselves.

And for fexecve() specifically: it ignores the file offset of fd. Since execute permission is checked by fexecve(), the file descriptor fd doesn't need to have been opened with the O_EXEC flag. However β€” if the file to be executed denies read permission for the process preparing to do the exec, the only way to provide the fd to fexecve() is to use the O_EXEC flag when opening fd. And note that the file to be executed can not be open for writing. 🚫✍️

🫢 RETURN VALUES 🫢

Here's the thing bestie: since execve() overlays the current process image with a new process image, a successful call has no process to return to. There is literally nobody left to return to. 😭

If execve() does return to the calling process, an error has occurred β€” the return value will be -1 and the global variable errno is set to indicate the error.

πŸ’€ ERRORS πŸ’€

The execve() system call will fail and return to the calling process if:

  • [ENOTDIR] β€” A component of the path prefix is not a directory.
  • [ENAMETOOLONG] β€” A component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters.
  • [ENOEXEC] β€” When invoking an interpreted script, the length of the first line, inclusive of the #! prefix and terminating newline, exceeds MAXSHELLCMDLEN characters.
  • [ENOENT] β€” The new process file does not exist.
  • [ELOOP] β€” Too many symbolic links were encountered in translating the pathname.
  • [EACCES] β€” Search permission is denied for a component of the path prefix.
  • [EACCES] β€” The new process file is not an ordinary file.
  • [EACCES] β€” The new process file mode denies execute permission.
  • [ENOEXEC] β€” The new process file has the appropriate access permission, but has an invalid magic number in its header.
  • [ETXTBSY] β€” The new process file is a pure procedure (shared text) file that is currently open for writing by some process.
  • [ENOMEM] β€” The new process requires more virtual memory than is allowed by the imposed maximum (getrlimit(2)).
  • [E2BIG] β€” The number of bytes in the new process' argument list is larger than the system-imposed limit. This limit is specified by the sysctl(3) MIB variable KERN_ARGMAX.
  • [EFAULT] β€” The new process file is not as long as indicated by the size values in its header.
  • [EFAULT] β€” The path, argv, or envp arguments point to an illegal address.
  • [EIO] β€” An I/O error occurred while reading from the file system.
  • [EINTEGRITY] β€” Corrupted data was detected while reading from the file system.

In addition, fexecve() will fail and return to the calling process if:

  • [EBADF] β€” The fd argument is not a valid file descriptor open for executing.

🌈 SEE ALSO 🌈

ktrace(1), _exit(2), fork(2), open(2), execl(3), exit(3), sysctl(3), a.out(5), elf(5), fdescfs(5), environ(7), mount(8)

πŸ‘‘ STANDARDS πŸ‘‘

The execve() system call conforms to IEEE Std 1003.1-2001 (β€œPOSIX.1”), with the exception of reopening descriptors 0, 1, and/or 2 in certain circumstances. A future update of the Standard is expected to require this behavior, and it may become the default for non-privileged processes as well. The support for executing interpreted programs is an extension. The fexecve() system call conforms to The Open Group Extended API Set 2 specification. (spec girlies, we love a compliant queen πŸ’œ)

πŸŽ€ HISTORY πŸŽ€

execve() appeared in 4.2BSD. fexecve() appeared in FreeBSD 8.0. glow-ups across the decades fr fr.

⚠️ CAVEATS ⚠️

If a program is setuid to a non-super-user, but is executed when the real uid is ``root'', then the program has some of the powers of a super-user as well. πŸ’€ be careful bestie.
When executing an interpreted program through fexecve(), the kernel supplies /dev/fd/n as a second argument to the interpreter, where n is the file descriptor passed in the fd argument to fexecve(). For this construction to work correctly, the fdescfs(5) filesystem shall be mounted on /dev/fd.

and that's the whole page, no cap πŸ’– this one's from Debian, dated March 30, 2020 β€” so it's serving vintage but make it forever. bye bestie, go exec something iconic πŸŒŸπŸ‘‘

πŸ“¦ where this page comes from