I am writing this function:
int main(int argc, char const *argv[]) {
char* hack[];
hack[0] = "/bin/sh";
hack[1] = NULL;
execve(hack[0], &hack, &hack[1]);
return 0;
}
my question is, what is the difference between writing the line:
execve(hack[0], &hack, &hack[1]);
instead of:
execve(hack[0], hack, NULL);
if there is any difference which is the best way? Writing the first type of expression I get a warning saying:
warning: passing argument 2 of ‘execve’ from incompatible pointer type [-Wincompatible-pointer-types]
note: expected ‘char * const*’ but argument is of type ‘char * (*)[3]’
what does this mean?
EDIT: I will start with an explanation about
execve
to make it a bit clearer.execve
will run the program pathname, replacing the current process, stack frame and heap. For this it takes three arguments:char*
with the path to your program.argv
passed to your ownmain
: get command line arguments, with the first such argument being thepathname
to the program.Both argv and envp are
char **
, meaning they are lists ofchar *
(null-terminated strings), with the last element being NULL. A program can then expect to iterate them until they find a NULL pointer to gather its arguments and environment.--
There are two differences between your two statements:
The second argument (argv):
hack
array, meaning you have achar***
, not compatible with the declaration which expects achar**
hack
array itself, which is achar **
(AKA a string array)The third argument (envp):
hack
array, which translates to "A string array whose first element is NULL".To quote the execve(2) man page:
In conclusion, in your case, the proper call is