简体   繁体   中英

How to execute shell command inside a chroot jail

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/lib into 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 busybox that provides the base functionality of many Unix commands including ls . Invoking it like busybox ls -l provides similar output to the regular ls program without needed to access addition shared libraries outside the chroot jail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM