Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Don't DllImport System.Native libraries #26919

@eerhardt

Description

@eerhardt

Describe the bug

Today we are p/invoking into the System.Native library in dotnet/runtime to use the lstat function:

[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:

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.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions