【发布时间】:2018-03-14 02:07:40
【问题描述】:
我在 linux (Ubuntu 12.0.4) 中运行以下 C 脚本作为设置根 UID 脚本 (chmod 4755)
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void main()
{ int fd;
/* Assume that /etc/zzz is an important system file,
* and it is owned by root with permission 0644.
* Before running this program, you should create
* the file /etc/zzz first. */
fd = open("/etc/zzz", O_RDWR | O_APPEND);
if (fd == -1) {
printf("Cannot open /etc/zzz\n");
exit(0);
}
/* Simulate the tasks conducted by the program */
sleep(1);
/* After the task, the root privileges are no longer needed, it’s time to relinquish the root privileges
permanently. */
setgroups(0, NULL);
setregid(getgid());
setreuid(getuid()); /* getuid() returns the real uid */
if(setregid(getgid()) == 0){
printf("Still root GID!\n");
exit(0);
} if(setreuid(getuid()) ==0){
printf("Still root UID\n");
exit(0);
if (fork()) { /* In the parent process */
close (fd);
exit(0);
} else { /* in the child process */
/* Now, assume that the child process is compromised,
malicious attackers have injected the following
statements into this process */
write (fd, "Malicious Data\n", 15);
close (fd);
}
}
据我所见,它应该将权限设置回真实用户(ID 1000),但我收到“Still root”错误。
我已经尝试插入 setuid(1000) 和 setuid(0) 来删除任何已保存的 UID 问题,但这只是允许它绕过 if 语句,但仍然允许写入“恶意数据”。
我还尝试在删除权限之前关闭文件close(fd),因为我不确定在以 root 身份打开的文件仍处于打开状态时您是否无法编辑权限。但我仍然遇到同样的问题
关于我在这里做错了什么有什么想法吗?为什么它不起作用?
【问题讨论】:
标签: c linux permissions user-permissions