Using the following code to run the ls command via /bin/shnworks fine:
#include <unistd.h>
int main(int argc, char **argv, char **envp) {
execle("/bin/sh", "sh", "-c", "ls", (char*)NULL, envp);
}
However if I launch the shell in an empty environment, changing the execle line to read like this:
execle("/bin/sh", "sh", "-c", "ls", (char*)NULL, NULL);
It works too.
How does the shell know the path to ls even though I didn’t pass any enviroment?
Lets re-write your program as following:
Now, once you compile and run with ltrace, you’ll find the following snippet in the output:
As you can see, it’s clearly looking for the right path before doing the fork() with
'/bin/ls'which is the right path for'ls'. If there was$PATHvariable given,shwould try those paths to find the location ofls. Since there is no$PATHprovided in this case, plausible paths (e.g./bin,/usr/bin,/sbin) are tried nevertheless.From
execleman-page: