为什么 chroot 中的 popen 在 C++ 中不起作用?程序

发布于 2025-01-11 17:35:43 字数 1740 浏览 3 评论 0原文

我正在尝试在 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# 

手动使用的截图ls inside chroot

在上面的程序中, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文