我一直在进行一些 shell 编写,并遇到了使用 access()
来检查文件是否存在、是否可以读取等的建议。似乎非常容易实现,并且比 更快>stat()
。当我开始查看它的手册页时,我注意到不建议使用它,因为它可能会导致安全漏洞。手册页是这样说的:
Using access() to check if a user is authorized to e.g. open a file before actually doing so using open(2) creates a security hole, because the user might exploit the short time interval between checking and opening the file to manipulate it.
有谁知道如何利用此漏洞,或者它是否仅适用于在检查文件后使用 open()
?我知道很多人说使用 stat()
来代替,但是 access()
非常容易实现,特别是对于我使用它的 shell。
最佳答案
这是一场 TOCTOU 竞赛(检查时间到更新时间)。恶意用户可以在 access()
和 open()
调用之间将他有权访问的文件替换为他无权访问的文件的符号链接(symbolic link)。使用faccessat()
或fstat()
。一般来说,打开一个文件一次,然后对其使用 f*()
函数(例如:fchown()
,...)。
关于c - access() 安全漏洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7925177/