Skip to content

How to properly set umask in TarEntry and ZipFileExtensions #69760

Closed
@carlossanlop

Description

@carlossanlop

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.Init here, that is when we interact with the umask, which we consume here.

I also noticed that the code in ZipFileExtensions.ZipArchiveEntry.Extract.Unix.cs has the same problem of ignoring the umask (cc @eerhardt).

So I have the following questions:

  1. 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?

  2. 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?:

Interop.CheckIo(
    Interop.Sys.FChMod(fs.SafeFileHandle, permissions & (int)Interop.Sys.Permissions.Mask),
    fs.Name);
  1. @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?

Metadata

Metadata

Assignees

Labels

area-System.IO.CompressionquestionAnswer questions and provide assistance, not an issue with source code or documentation.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions