You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to properly set umask in TarEntry and ZipFileExtensions #69760
This question is related to one of the items in the tar pending feedback issue: #68230
In the initial Tar implementation PR, @tmds pointed out here and here that the code setting the mode when extracting files in Unix was incorrect because the umask is being ignored.
The proposed solution would be to call the SafeFileHandle.Open() static method that takes an Interop.Sys.Permissions argument. The problem is that this method is internal. The only places where we call it are in File, FileStream and FileSystem.Unix.cs, which all reside in System.Private.CoreLib.
I examined that Open method, and when it calls SafeFileHandle.Inithere, that is when we interact with the umask, which we consume here.
Why do we use the hardcoded Interop.Sys.Permissions.Mask in SafeFileHandle, instead of the one desired by the user, which according to various documentation sources (like this one) is usually set in the .profile file or in /etc/profile?
If we all think it is the right thing to use that hardcoded mask, can I simply just do the following for both TarEntry and ZipFileExtensions?:
@tmds had a question here about the if (permisions != 0). This code is identical to the one in ZipFileExtensions. Do we need that protection at all? @eerhardt would there be unexpected behavior if we remove that if?
In the initial Tar implementation PR, @tmds pointed out #67883 (comment) and #67883 (comment) that the code setting the mode when extracting files in Unix was incorrect because the umask is being ignored.
The umask protects the user from unintentional opening up files/directories to other users.
The tar command has a flag to apply the exact permissions (-p, --preserve-permissions).
The proposed solution would be to call the SafeFileHandle.Open() static method that takes an Interop.Sys.Permissions argument. The problem is that this method is internal. The only places where we call it are in File, FileStream and FileSystem.Unix.cs, which all reside in System.Private.CoreLib.
Why do we use the hardcoded Interop.Sys.Permissions.Mask in SafeFileHandle, instead of the one desired by the user, which according to various documentation sources (like this one) is usually set in the .profile file or in /etc/profile?
The kernel tracks the process umask. It gets applied automatically to the mask specified by the user on specific calls like open and mkdir.
It doesn't get used for calls like chmod and fchmod.
If we all think it is the right thing to use that hardcoded mask, can I simply just do the following for both TarEntry and ZipFileExtensions?:
imo, we should have the same behavior as tar for the same reasons.
@tmds had a question #67883 (comment) about the if (permisions != 0). This code is identical to the one in ZipFileExtensions. Do we need that protection at all? @eerhardt would there be unexpected behavior if we remove that if?
Activity
tmds commentedon May 25, 2022
The umask protects the user from unintentional opening up files/directories to other users.
The
tarcommand has a flag to apply the exact permissions (-p, --preserve-permissions).#67837 will provide a way to do this.
The kernel tracks the process
umask. It gets applied automatically to the mask specified by the user on specific calls likeopenandmkdir.It doesn't get used for calls like
chmodandfchmod.imo, we should have the same behavior as
tarfor the same reasons.The rationale is provided in a comment:
runtime/src/libraries/System.IO.Compression.ZipFile/src/System/IO/Compression/ZipFileExtensions.ZipArchiveEntry.Extract.Unix.cs
Lines 20 to 26 in 0dba0ee
carlossanlop commentedon May 26, 2022
Thanks for the additional explanation, @tmds. I'll wait for the new API #67837 to be added to continue working on this.
carlossanlop commentedon Jul 7, 2022
@tmds I see that you mentioned your PR #71647 would address this, not fix it. Is there anything else we should fix in Tar, in your opinion?
tmds commentedon Jul 8, 2022
I should have used "fixes" in the message. We can close this when that PR is merged.