-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible race-conditions between file creation and Fchownat #327
Comments
Good find! The problem with seteuid is that affects all threads. We could create the file with mode 000, chown it, and then chmod to the requested mode. Filtering O_creat is a good idea. About mknod, the kernel checks if the user can do that before sending us the request. User should get permission denied from the kernel |
Actually, i'm not sure if we can get o_create in open, but blindly filtering it out does no harm. |
I have been thinking about this issue a bit more. Apparently, it is possible to change the effective permissions for a single thread on Linux, this is just a limitation of the POSIX api. See http://man7.org/linux/man-pages/man2/setresuid.2.html:
Unfortunately, with Go things are a bit more complicated. Since Go routines do not necessarily correspond 1:1 to kernel threads, it seems dangerous to change user/group in the program. It would be necessary to lock for basically all file system operations, so this is not really feasible. I was wondering what Getting the
|
Quick question about the patch, why 0600 permissions instead of 0000? |
For regular files, it probably doesn't really matter if we use |
To clarify: For read-only files, commands like |
What about using It would have the advantage that we could use it for all file related syscalls where the owner/group is important. Note that there is also a (probably difficult to exploit, but still) security issue related to |
On macOS there is apparently |
The lockosthread thing sounds too brittle for my taste. If at all possible i'd like to avoid it. |
It certainly would be less portable. Then (at least for now) let's stick to a |
Reported by @slackner at #327 : Possible race-conditions between file creation and Fchownat * Assume a system contains a gocryptfs mount as root user with -allow_other * As a regular user create a new file with mode containing the SUID flag and write access for other users * Before gocryptfs executes the Fchownat call, try to open the file again, write some exploit code to it, and try to run it. For a short time, the file is owned by root and has the SUID flag, so this is pretty dangerous.
Commited as b22cc03 (slightly adjusted), thanks! |
Sorry, I did not do it this time, but do you mind if I add Signed-off-by: Sebastian Lackner [email protected] in cases like this in the future? When you propose a patch but don't make a PR. Faking the git author would be another option, but that feels kinda wrong |
Thanks! Now we carefully have to think if we need similar logic for other file objects aswell: What about directories, symlinks, or device files? Could it be exploited in any way if they are created with the wrong owner? The only risk I see would be that a user opens the corresponding file objects before we have set the correct owner. Is that actually possible (and could it be exploited)? Regarding adding |
Works for me as well, just want to give credit! |
Since Mknod can also be used to create regular files, it suffers from exactly the same problems as Create(). See rfjakob#327 for more details.
Make sure that the directory belongs to the correct owner before users can access it. For directories with SUID/SGID mode, there is a risk of race-conditions when files are created before the correct owner is set. They will then inherit the wrong user and/or group. See rfjakob#327 for more details.
Make sure that the directory belongs to the correct owner before users can access it. For directories with SUID/SGID mode, there is a risk of race-conditions when files are created before the correct owner is set. They will then inherit the wrong user and/or group. See #327 for more details.
Fixed by 03b9d65 , Fchownat is gone |
The method would work as follows:
-allow_other
Fchownat
call, try to open the file again, write some exploit code to it, and try to run it.For a short time, the file is owned by root and has the SUID flag, so this is pretty dangerous. To mitigate this issue, we could temporarily change the effective UID/GID and skip the
Fchown
step.Some more thoughts:
In
Open
, we might want to explicitly filterO_CREAT
from the flags - if it is ever used for file creation, it skips thePreserveOwner
andLongName
creation code.The same issue might also be relevant for
Mknod
. Should a regular user really be able to create block devices with arbitrary modes? This probably needs further investigations.The text was updated successfully, but these errors were encountered: