0

I am trying to use basic linux command "ls" inside chroot,I have required library for ls is present in the folder,i could acess ls manually,but not able to use vi cpp program. Find the below program

#include<iostream>
#include <stdio.h>
#include <unistd.h>
#include <filesystem>
using namespace std;

int Privsep_Chroot(const char *path)
{
    char buffer[127];
    string result = "";
    if (chdir(path) < 0) {
        return (1);
    }
   system("ls");
   FILE *pipe=popen("/bin/ls","r"); 
   if (chroot(path) < 0) {
        return (1);
    }
    

    if (chdir("/") < 0) {
        return (1);
   }
    putenv("PATH=$PATH:/usr/bin");
    int n=system("ls");
    FILE *pipe=popen("/bin/ls","r");
    if(pipe)
        cout<<"execute";
    while (!feof(pipe)) {
         if (fgets(buffer, 128, pipe) != NULL)
            result += buffer;
      }
    char tmp[10];
    getcwd(tmp, 256);
    cout << "Current working directory: " << tmp << endl;
    return (0);
}
int main()
{
    cout<<Privsep_Chroot("/root/jail");
} 

Lib and Bin in /root/jail:

root@parvathy-12527:/var/ossec# cd /root/jail/
root@parvathy-12527:~/jail# ls
bin  lib  lib64 
root@parvathy-12527:~/jail# ls bin/
bash  ls
root@parvathy-12527:~/jail# ls lib
ld-linux-x86-64.so.2  libc.so.6  libdl.so.2  libpcre2-8.so.0  libpthread.so.0  libselinux.so.1  libtinfo.so.6
root@parvathy-12527:~/jail# ls lib64
ld-linux-x86-64.so.2  libc.so.6  libdl.so.2  libpcre2-8.so.0  libpthread.so.0  libselinux.so.1  libtinfo.so.6
root@parvathy-12527:~/jail# 

screenshot of manually using ls inside chroot

In above program, system("ls") returns -1 with linux errno 2 (ENOENT-No such file or directory) popen return null pointer.

can Someone shed light on the above issuse

  • `ls` is not a linux command. In most cases of running Linux, it's rather a GNU program or maybe from Busybox, just for your info. As a new user here, please also take the [tour] and read [ask]. – Ulrich Eckhardt Mar 04 '22 at 07:11
  • 1
    `putenv("PATH=$PATH:/usr/bin");`: `$PATH` will not be expanded. This is not a shell. So, `/bin` is not going to be in your PATH. – user17732522 Mar 04 '22 at 07:13

0 Answers0