execve System Call Explained: How Linux Runs Programs (C and Assembly Examples)

Assembly3 min read
execve system call in C and x86-64 assembly
On this page

execve is the Linux system call that runs a new program. It does not create a new process. It replaces the program running in the current process with a new one: new code, new data, new stack, same process ID. Every time you type a command in a shell, the shell forks a child process and the child calls execve to become that command.

The function signature

#include <unistd.h>

int execve(const char *pathname, char *const argv[], char *const envp[]);
ArgumentMeaning
pathnameFull path of the program, for example /bin/ls
argvArgument list, ending with NULL; argv[0] is usually the program name
envpEnvironment variables as “KEY=value” strings, ending with NULL

On success, execve never returns, because the calling program no longer exists. If it returns, it failed, returning -1 and setting errno (for example ENOENT for a missing file or EACCES for missing permissions).

A simple C example

#include <stdio.h>
#include <unistd.h>

int main(void) {
    char *argv[] = { "ls", "-l", "/tmp", NULL };
    char *envp[] = { "PATH=/bin:/usr/bin", NULL };
    execve("/bin/ls", argv, envp);
    perror("execve");   /* only reached if execve failed */
    return 1;
}

fork plus exec

To run a program and keep your own program running, combine fork and exec:

  1. fork() creates a copy of the current process.
  2. The child calls execve to become the new program.
  3. The parent calls waitpid() to wait for the child to finish.

This is exactly how shells, service managers and most launchers work. You can see the resulting parent and child processes with ps aux and its tree view.

The exec family

The C library offers wrappers that all end in execve:

FunctionArgumentsSearches PATHCustom environment
execlListNoNo
execlpListYesNo
execleListNoYes
execvArrayNoNo
execvpArrayYesNo
execveArrayNoYes (the real system call)

“l” means a list of arguments, “v” a vector (array), “p” searches PATH, “e” passes an environment.

execve in assembly (x86-64)

At the lowest level, execve is system call number 59 on x86-64 Linux:

RegisterValue
rax59 (execve)
rdiPointer to the path string
rsiPointer to the argv array
rdxPointer to the envp array (or NULL)

The syscall instruction then hands control to the kernel. On 32-bit x86 the number is 11 and the call uses int 0x80 with ebx, ecx and edx.

Why execve matters in security

  • Exploitation: classic shellcode ends with an execve of /bin/sh, so an attacker who controls execution gets a shell. Studying it is a standard part of exploit development training.
  • Detection: security tools log execve events (auditd, EDR, eBPF tools) because every program start passes through it. Unusual execve chains, such as a web server spawning a shell, are strong signs of compromise.
  • Forensics: process creation records reconstruct what ran on a system and in what order.

Frequently asked questions

What does execve do?

It replaces the current process image with a new program, keeping the same process ID.

Why does execve not return?

On success the calling program is replaced, so there is nothing to return to. A return value always means failure.

What is the difference between fork and execve?

fork creates a new process as a copy of the current one; execve replaces the program inside a process. They are usually used together.

What is the execve syscall number?

59 on x86-64 Linux and 11 on 32-bit x86 Linux.

Is execve the same as system()?

No. system() runs a command through /bin/sh using fork and exec internally; execve runs a program directly without a shell.

Learn Linux internals and shellcode

Our Assembly and Shellcode Development course covers system calls, registers, process memory and how shellcode uses execve, with hands-on labs.

Written by

Taifun Mamedov

Founder and instructor at Ocsaly Academy.

Courses by Taifun Mamedov

Share this article