I have a problem with the execution of shell commands inside a chroot jail. Here is an exemple:
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
int main()
{
if (geteuid() == 0) // check root privileges
{
chroot("/bin");
chdir("/");
execl("/ls", "ls", "-l", (char *) NULL); // "/ls" should be equivalent to "/bin/ls"
perror(strerror(errno));
}
else
printf("Permission denied\n");
return 0;
}
The problem is the exec: according to errno, the error is “No such file or directory”.
The same error appears if I use exec(“/bin/ls”, …)
I think that “ls” cannot use the shared libraries he needs, because of chroot jail.
Any suggestion to solve this problem?
You’re probably right regarding shared libraries being inaccessible. Setting up a chroot jail typically involves copying parts of
/bin,/usr/bin,/lib, and/usr/libinto a parallel directory structure.A simpler alternative is to use only statically linked executables. On many linux systems there will be a statically linked executable called
busyboxthat provides the base functionality of many Unix commands includingls. Invoking it likebusybox ls -lprovides similar output to the regularlsprogram without needed to access addition shared libraries outside the chroot jail.