/proc/今天了解了目录,尤其是我对半公开提供有关进程的所有信息的安全性感兴趣,因此我编写了一个简单的程序,该程序执行一些简单的操作,从而使我能够探索/proc/目录的某些属性:
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
using namespace std;
extern char** environ;
void is_linux() {
#ifdef __linux
cout << "this is running on linux" << endl;
#endif
}
int main(int argc, char* argv[]) {
is_linux();
cout << "hello world" << endl;
int fd = open("afile.txt", O_RDONLY | O_CREAT, 0600);
cout << "afile.txt open on: " << fd << endl;
cout << "current pid: " << getpid() << endl;;
cout << "launch arguments: " << endl;
for (int index = 0; index != argc; ++index) {
cout << argv[index] << endl;
}
cout << "program environment: " << endl;
for (char** entry = environ; *entry; ++entry) {
cout << *entry << endl;
}
pause();
}
Run Code Online (Sandbox Code Playgroud)
但是有趣的是(无论如何对我来说),当我检查文件描述符文件夹(/pid/<PID#>/fd)时,我看到了:
root@excalibur-VirtualBox:/proc/1546/fd# ls -l
total 0
lrwx------ 1 root root 64 Nov 7 09:12 0 -> /dev/null
lrwx------ 1 root root 64 Nov 7 09:12 1 -> /dev/null
lrwx------ 1 root root 64 Nov 7 09:12 2 -> /dev/null
lrwx------ 1 root root 64 Nov 7 09:12 3 -> socket:[11050]
Run Code Online (Sandbox Code Playgroud)
为什么文件描述符指向/dev/null?这是为了防止用户能够在不实际成为进程本身的情况下将内容注入文件中,还是基于此?更奇怪的是,为什么打开的文件的文件描述符指向套接字?这似乎很奇怪。如果有人能为我阐明这一点,我将非常感谢。谢谢!
您肯定在查看错误的/proc目录(对于其他 PID 或在另一台计算机上)。/proc/<pid>/fd您的程序的内容应如下所示:
lrwx------ 1 user group 64 Nov 7 22:15 0 -> /dev/pts/4
lrwx------ 1 user group 64 Nov 7 22:15 1 -> /dev/pts/4
lrwx------ 1 user group 64 Nov 7 22:15 2 -> /dev/pts/4
lr-x------ 1 user group 64 Nov 7 22:15 3 -> /tmp/afile.txt
Run Code Online (Sandbox Code Playgroud)
在这里我们可以看到文件描述符 0、1 和 2 显示为指向运行程序的伪终端的符号链接。/dev/null如果您使用输入、输出和错误重定向来启动程序,则可能是这样。文件描述符#3 指向afile.txt当前打开的文件。
| 归档时间: |
|
| 查看次数: |
2660 次 |
| 最近记录: |