access() Security Hole

你。 提交于 2019-11-29 15:44:58

That is a TOCTOU race (Time of Check to Time of Update). A malicious user could substitute a file he has access to for a symlink to something he doesn't have access to between the access() and the open() calls. Use faccessat() or fstat(). In general, open a file once, and use f*() functions on it (e.g: fchown(), ...).

One thing I can think of, although it seems weak - access() uses the real rather than effective uid and gid. This supposedly allows a setuid program (one which a regular user executes but which gains permissions of the owner) to check whether the invoking user can read the file, to prevent inadvertently giving that user access to a file that they should be unable to read, perhaps by using some symbolic link or hard link trickery. I can't find any evidence that this is possible, or that this isn't possible with stat(), but imagine this scenario:

user executes program
program is setuid, immediately gets all privs of root
program checks file1 to ensure that user has access
file1 is a hardlink to file2, which user has access to
user changes file1 to hardlink to file3 (/etc/shadow or something like that)
program reads file1 and does something to it (print, convert, whatever)
user now has access to a file they shouldn't

The pattern seems to be calling access() or stat() to determine whether you can open a file, and then opening it if you have permission.

Instead, it's usually better just to go ahead and try to open it, and then check whether the attempt succeeded (and if not, why). This avoids the time interval between the check and the attempt to open the file.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!