| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Runtime.InteropServices; |
| | | 8 | | using System.Runtime.Versioning; |
| | | 9 | | using System.Security.Principal; |
| | | 10 | | using System.Text; |
| | | 11 | | using CoreWCF.Runtime; |
| | | 12 | | using CoreWCF.Security; |
| | | 13 | | using Microsoft.IdentityModel.Tokens; |
| | | 14 | | using Microsoft.Win32.SafeHandles; |
| | | 15 | | using static CoreWCF.Security.UnsafeNativeMethods; |
| | | 16 | | |
| | | 17 | | namespace CoreWCF.Channels |
| | | 18 | | { |
| | | 19 | | [SupportedOSPlatform("windows")] |
| | | 20 | | internal static class AppContainerInfo |
| | | 21 | | { |
| | 0 | 22 | | private static readonly object s_lock = new object(); |
| | | 23 | | private static volatile bool s_isRunningInAppContainerSet; |
| | | 24 | | private static bool s_isRunningInAppContainer; |
| | | 25 | | private static volatile SecurityIdentifier s_currentAppContainerSid; |
| | | 26 | | |
| | | 27 | | internal static bool IsRunningInAppContainer |
| | | 28 | | { |
| | | 29 | | get |
| | | 30 | | { |
| | 0 | 31 | | if (!s_isRunningInAppContainerSet) |
| | | 32 | | { |
| | 0 | 33 | | lock (s_lock) |
| | | 34 | | { |
| | 0 | 35 | | if (!s_isRunningInAppContainerSet) |
| | | 36 | | { |
| | 0 | 37 | | s_isRunningInAppContainer = RunningInAppContainer(); |
| | 0 | 38 | | s_isRunningInAppContainerSet = true; |
| | | 39 | | } |
| | 0 | 40 | | } |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | return s_isRunningInAppContainer; |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | internal static SecurityIdentifier GetCurrentAppContainerSid() |
| | | 48 | | { |
| | 0 | 49 | | if (s_currentAppContainerSid == null) |
| | | 50 | | { |
| | 0 | 51 | | lock (s_lock) |
| | | 52 | | { |
| | 0 | 53 | | if (s_currentAppContainerSid == null) |
| | | 54 | | { |
| | 0 | 55 | | s_currentAppContainerSid = GetAppContainerSid(); |
| | | 56 | | } |
| | 0 | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | return s_currentAppContainerSid; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | private static bool RunningInAppContainer() |
| | | 64 | | { |
| | 0 | 65 | | uint tokenInfoLength = sizeof(int); |
| | 0 | 66 | | byte[] tokenInformation = new byte[tokenInfoLength]; |
| | 0 | 67 | | if (!UnsafeNativeMethods.GetTokenInformation(GetCurrentProcessToken(), |
| | 0 | 68 | | UnsafeNativeMethods.TOKEN_INFORMATION_CLASS.TokenIsAppContainer, |
| | 0 | 69 | | tokenInformation, tokenInfoLength, out _)) |
| | | 70 | | { |
| | 0 | 71 | | int error = Marshal.GetLastWin32Error(); |
| | 0 | 72 | | throw new Win32Exception(error); |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | return BitConverter.ToInt32(tokenInformation, 0) != 0; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | private static unsafe SecurityIdentifier GetAppContainerSid() |
| | | 79 | | { |
| | | 80 | | // First call to get the required buffer length |
| | 0 | 81 | | uint returnLength = UnsafeNativeMethods.GetTokenInformationLength( |
| | 0 | 82 | | GetCurrentProcessToken(), |
| | 0 | 83 | | TOKEN_INFORMATION_CLASS.TokenAppContainerSid); |
| | | 84 | | |
| | 0 | 85 | | byte[] tokenInformation = new byte[returnLength]; |
| | 0 | 86 | | fixed (byte* pTokenInformation = tokenInformation) |
| | | 87 | | { |
| | 0 | 88 | | if (!UnsafeNativeMethods.GetTokenInformation( |
| | 0 | 89 | | GetCurrentProcessToken(), |
| | 0 | 90 | | TOKEN_INFORMATION_CLASS.TokenAppContainerSid, |
| | 0 | 91 | | tokenInformation, |
| | 0 | 92 | | returnLength, |
| | 0 | 93 | | out returnLength)) |
| | | 94 | | { |
| | 0 | 95 | | int errorCode = Marshal.GetLastWin32Error(); |
| | 0 | 96 | | throw Fx.Exception.AsError(new Win32Exception(errorCode)); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | // TOKEN_APPCONTAINER_INFORMATION contains a single PSID field |
| | 0 | 100 | | IntPtr sidPtr = Marshal.ReadIntPtr(tokenInformation, 0); |
| | 0 | 101 | | return new SecurityIdentifier(sidPtr); |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | // Shortcut for when you only need to a token to query information. |
| | | 106 | | // See https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentproc |
| | 0 | 107 | | private static IntPtr GetCurrentProcessToken() => (IntPtr)(-4); |
| | | 108 | | } |
| | | 109 | | } |