WSL: Access WslGetDistributionConfiguration from Managed Code returns E_ACCESSDENIED

Environment

Windows build number: Version 10.0.19041.450
Your Distribution version: n/a.
Whether the issue is on WSL 2 and/or WSL 1: WSL 2

Steps to reproduce

WSL logs: None needed

I am trying to implement WslGetDistributionConfiguration API function from wslapi and keep getting E_ACCESSDENIED Currently I am using .NET managed code to call , either from C# and VB.net i get the same results

 <DllImport("wslapi.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
  Shared Function WslGetDistributionConfiguration(ByVal distributionName As String, ByRef distributionVersion As ULong, ByRef defaultUID As ULong, ByRef wslDistributionFlags As WSL_DISTRIBUTION_FLAGS, ByRef defaultEnvironmentVariables As String, ByRef defaultEnvironmentVariableCount As ULong) As UInteger
  End Function

  <Flags()> Enum WSL_DISTRIBUTION_FLAGS As UInteger
    WSL_DISTRIBUTION_FLAGS_NONE = 0                  ' No flags are being supplied.
    WSL_DISTRIBUTION_FLAGS_ENABLE_INTEROP = 1        ' Allow the distribution to interoperate with Windows processes (for example, the user can invoke "cmd.exe" or "notepad.exe" from within a WSL session).
    WSL_DISTRIBUTION_FLAGS_APPEND_NT_PATH = 2        ' Add the Windows %PATH% environment variable values to WSL sessions.
    WSL_DISTRIBUTION_FLAGS_ENABLE_DRIVE_MOUNTING = 3 ' Automatically mount Windows drives inside of WSL sessions (for example, "C:" will be available under "/mnt/c").
  End Enum

Would like to know if this is a know issue or if there is any implementation of this API that works properly.

I am following the documentation from: https://docs.microsoft.com/en-us/windows/win32/api/wslapi/nf-wslapi-wslgetdistributionconfiguration

Expected behavior

Function returns proper S_OK

Actual behavior

Function returns error HRESULT = E_ACCESSDENIED | General access denied error | 0x80070005

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 19 (1 by maintainers)

Most upvoted comments

I create a simple C# console app, and you can build it with x64 configuration, and I didn’t see problem. https://pastebin.com/98AnCbst

You are missing CharSet = CharSet.Unicode, which will result in 0x80040302. And maybe else.

Beat me by 8 minutes. I should have had dinner. Seems to take here.

The reason for that is lxss manager is a service, and the wslapi will access lxss manager via COM. In lxss service, it used impersonate token to get user’s configurations. C# initialized the security without IMPERSONATE flag, so it doesn’t work. If you want to know more, please refer security in COM. (actually I spent half an day to read the document, but I still confused.)

You missed the undocumented CoInitializeSecurity! You can find it’s C# version here Because CoInitializeSecurity can only be initialized once, that means you need to set it in the earliest initialization of your application. If it’s WPF application, you can set it in App::App

C# example:

      CoInitializeSecurity(IntPtr.Zero, -1, IntPtr.Zero, IntPtr.Zero, RpcAuthnLevel.Default, RpcImpLevel.Impersonate, IntPtr.Zero, EoAuthnCap.StaticCloaking, IntPtr.Zero);

Here is the C++ example.

CoInitializeSecurity(nullptr,
                                         -1,
                                         nullptr,
                                         nullptr,
                                         RPC_C_AUTHN_LEVEL_DEFAULT,
                                         RPC_C_IMP_LEVEL_IMPERSONATE,
                                         NULL,
                                         EOAC_STATIC_CLOAKING,
                                         0));