起因
在写httpser 想要切换目录记得有个函数chroot 于是就用了 , 然后神奇的事情就出现了 , 之后想要输出当前工作目录等一系列的操作都无效,我们来了解其功能。
查阅资料 :
chroot()用来改变根目录为参数path所指定的目录. 只有超级用户才允许改变根目录, 子进程将继承新的根目录. 调用成功则返回0,
失败则返-1, 错误代码存于errno. 错误代码
实例
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
/* 将根目录改为/tmp ,并将工作目录切换至/tmp */
char buf[50];
// chroot("/home/dra");
chdir("/home/dra/C/fff");
getcwd(buf , 50);
printf("%s\n",buf);
return 0;
}
首先有路径
程序运行结果
对代码做出一下修改
chroot("/home/dra");
chdir("/C/fff");
由此可知,chroot 就是同描述一般 , 修改了根目录 所有路径操作都以chroot改变以后新路劲来操作。
那用户可以跳出这个root目录吗
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
/* 将根目录改为/tmp ,并将工作目录切换至/tmp */
char buf[50];
int a,b;
a=chroot("/home/dra/C");
b=chdir("/../pwn");
getcwd(buf , 50);
printf("%s %d %d\n",buf,a,b);
return 0;
}
答案告诉我们这个函数还是安全的。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
/* 将根目录改为/tmp ,并将工作目录切换至/tmp */
char buf[50];
int a,b;
a=chroot("/home/dra/");
b=chdir("/../pwn");
getcwd(buf , 50);
printf("%s %d %d\n",buf,a,b);
return 0;
}
似乎是因为函数忽略了/… , 这里C和pwn 是同一级目录。