我想知道是否有人可以协助我,我试图找出如何处理检查时间,使用时间问题以及在不需要它们时的放弃特权,例如,如果它是到服务器的符号链接可以更改为影子文件的文件。假定在调用过程以提升的特权运行时调用了以下功能。
int
updatefile(char *file)
{
int fd;
if (access(file, R_OK|W_OK)) {
perror("access()");
return (-1);
}
fd = open(file, O_RDWR);
/*
* file is written to here.
*/
printf("Updated %s on...\n", file);
system("date");
/*
* elevated privileges are required here...
*/
return (0);
}
假设您的access
函数检查文件类型并确定用户是否具有适当的权限来操作文件,您担心到access
和之间存在潜在的TOCTTOU错误open
。
避免这种情况的典型方法是:
int updatefile(char *file)
{
int fd = -1;
if(-1 != (fd = open(file, R_OK | W_OK)))
{
struct stat buf;
if(0 == fstat(fd, &buf))
{
/* perform any necessary check on the here */
/* do what ever else you need to do */
/* write to the file here */
/* gain elevated permissions here */
/* do privileged task */
/* drop back to normal permissions here */
close(fd);
}
else
{
/* handle error stating the file */
}
}
else
{
/* handle error for not opening file */
}
}
之所以起作用,是因为我们将对文件进行的所有检查推迟到获得文件的“句柄”之后。我们可以通过errno
外部else块中的值来判断用户是否没有打开文件的权限;
如果我们能够获取文件的“句柄”,那么我们就可以进行所需的检查。因为我们从打开文件的那一刻到执行检查直到使用文件的最后一刻都维护着“句柄”;恶意软件将无法在检查和使用之间修改文件系统。
希望这对T有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句