为什么 chroot 中的 popen 在 C++ 中不起作用?程序
我正在尝试在 chroot 中使用基本的 linux 命令“ls”,文件夹中存在 ls 所需的库,我可以手动访问 ls,但无法使用 vi cpp 程序。 在/root/jail中找到以下程序
#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和Bin:
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#
在上面的程序中, system("ls") 返回 -1 并显示 linux errno 2 (ENOENT-没有这样的文件或目录) popen 返回空指针。
有人可以阐明上述问题吗
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论