Home > Software engineering >  What does "/bin/sh" stand for in execlp command?
What does "/bin/sh" stand for in execlp command?

Time:11-08

I want to execute a shell command with execlp, I tried with the following instruction :

execlp("sh", "sh", "-c", p_command, (char*)NULL);

p_command is a pointer to a const char representing a shell command line.

My minimal test tells me the program succeded as expected. I first choose to use "/bin/sh" instead of "sh" but I've learned that p(ath) in execlp allows us to avoid writing the full path, as if exec will complete the path for us ; so I removed "/bin/". My concern is that I never saw a code using execlp with only "sh", as it effectively does for exemple for ls we can directly use "ls" instead of "/bin/ls".

As a beginner I am wondering what "/bin/sh" stands for, what is the difference between "sh" and "/bin/sh" in this situation and why we have to write the full path for execlp to execute a shell ?

CodePudding user response:

When the path passed to execlp is sh, execlp searches for it in the directories listed in the PATH environment variable. If an attacker is able to modify the PATH variable in the environment that runs your program, they can set it to list a directory of their choosing, and they can place their own program named sh in that directory. Then your program will execute their program instead of executing the system sh program. In some cases (depending on a bit in the file’s mode bits), programs are executed with the permissions of their owners rather than the permissions of the user executing the program. Such programs must be written carefully to avoid situations like this, where an attacker would be able to exploit the program.

When the path passed to execlp is /bin/sh, execlp looks for it in the path that is /bin/sh starting from the root of the file system, called /. This will always use the sh program that the system administrator has put in the /bin directory (usually done as part of system installation).

CodePudding user response:

Every executable is run through bash. execlp is a part of exec family where lp stands for list of args and path. So whenever you pass a command or execute a file , bin/sh or bash forks and performs the task in child process and returns. when ls is run a bash is allocated to it.

  • Related