-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Describe the bug
Today we are p/invoking into the System.Native library in dotnet/runtime to use the lstat function:
sdk/src/Cli/dotnet/StatInterop.cs
Lines 70 to 71 in 4536ed2
| [DllImport("libSystem.Native", EntryPoint = "SystemNative_LStat", SetLastError = true)] | |
| internal static extern int LStat(string path, out FileStatus output); |
This isn't recommended because this library is a private implementation detail of the runtime. We should find another way to implement the required functionality here.
One possibility is to add the necessary APIs to .NET to read the current owner of a file:
sdk/src/Cli/dotnet/SudoEnvironmentDirectoryOverride.cs
Lines 111 to 135 in 4536ed2
| private static bool TempHomeIsOnlyRootWritable(string path) | |
| { | |
| if (StatInterop.LStat(path, out StatInterop.FileStatus fileStat) != 0) | |
| { | |
| return false; | |
| } | |
| return IsOwnedByRoot(fileStat) && GroupCannotWrite(fileStat) && | |
| OtherUserCannotWrite(fileStat); | |
| } | |
| private static bool OtherUserCannotWrite(StatInterop.FileStatus fileStat) | |
| { | |
| return (fileStat.Mode & (int) StatInterop.Permissions.S_IWOTH) == 0; | |
| } | |
| private static bool GroupCannotWrite(StatInterop.FileStatus fileStat) | |
| { | |
| return (fileStat.Mode & (int) StatInterop.Permissions.S_IWGRP) == 0; | |
| } | |
| private static bool IsOwnedByRoot(StatInterop.FileStatus fileStat) | |
| { | |
| return fileStat.Uid == 0; | |
| } |
.NET 7 added APIs to check the file's mode, but this code also needs to check the file's owner.
To Reproduce
This is the root cause of dotnet/runtime#72308. In .NET 7 the runtime changed the signature of this native function and broke the SDK on arm64.