1 year ago
#250763
parvathy priya
why is popen inside chroot is not working in C++ program
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
c++
popen
chroot
0 Answers
Your Answer