execve() failing to launch program in C

user99545 picture user99545 · Dec 12, 2011 · Viewed 12.5k times · Source

I am trying to spawn a new process using execve() from unistd.h on Linux. I have tried passing it the following parameters execve("/bin/ls", "/bin/ls", NULL); but get no result. I do not get an error either, the program just exits. Is there a reason why this is happening? I have tried launching it as root and regular user. The reason I need to use execve() is because I am trying to get it to work in an assembly call like so

program: db "/bin/ls",0

mov eax, 0xb
mov ebx, program
mov ecx, program
mov edx, 0
int 0x80

Thank you!

Answer

paxdiablo picture paxdiablo · Dec 12, 2011

The arguments that you're passing to execve are wrong. Both the second and third must be an array of char pointers with a NULL sentinel value, not a single pointer.

In other words, something like:

#include <unistd.h>
int main (void) {
    char * const argv[] = {"/bin/ls", NULL};
    char * const envp[] = {NULL};
    int rc = execve ("/bin/ls", argv, envp);
    return rc;
}

When I run that, I do indeed get a list of the files in the current directory.