c++ - 为什么我的代码在没有 chroot 功能的情况下工作,但在 chroot 功能下失败?
问题描述
我试图让我的代码在 下工作chroot('/root/test1')
,但它不能正常工作。
但是当我删除chroot('/root/test1')
并修改execl("/test2", "test2", NULL)
为 时execl("/root/test1/test2", "test2", NULL)
,它会按预期工作得很好。这是为什么?
另外我想问一下,如果我把fp
redirect设置为stdin
,然后用execl
函数来运行另一个程序,子程序fp
是否会得到输入?
'/root/test1/' 中的文件:
test2
test2.cpp
test3
test3.cpp
execl 函数的返回值为 -1,errno 为 2。
测试3.cpp
int main() {
FILE *fp;
errno = 0;
fp = fopen("log.txt", "r");
dup2(fileno(fp), fileno(stdin));
cout << chdir("/root/test1") << endl;
cout << chroot("/root/test1") << endl;
DIR *dir = opendir("/");
dirent *list;
while ((list = readdir(dir)) != NULL) {
cout << list -> d_name << " ";
}
cout << endl;
closedir(dir);
errno = 0;
cout << execl("/test2", "test2", NULL) << endl;
cout << errno << endl;
cout << strerror(errno) << endl;
return 0;
}
测试2.cpp
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int a,b;
cin >> a;
scanf("%d",&b);
cout << a+b << endl;
printf("%d",a+b);
return 0;
}
日志.txt
111 222
输出*
0
0
. test3.cpp test3 .. test2 test2.cpp log.txt
-1
2
No such file or directory
解决方案
将 /usr /lib /lib64 和 /bin/bash 复制到 /root/test1
推荐阅读
- mysql - 如何在同一个表的同一个查询中使用 SUM 和 COUNT 创建 SQL
- css - Bootstrap 4 无法在两列之间添加装订线
- c++ - asio::async_write 性能限制
- google-sheets-api - Google 表格:从保护中排除当前日期行
- sql - Conditional CHECK constraint
- reactjs - useContext in React Native
- java - 列出所有可以由用户输入字符组成的单词(Java 数组)
- javascript - How to filter an array based on the grand child array's values?
- c# - 检查 PDF 文件是否正确创建
- javascript - Firestore分页以错误的顺序附加数组