| | | 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.Runtime.InteropServices; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Threading; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | using Microsoft.Win32.SafeHandles; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Security |
| | | 13 | | { |
| | | 14 | | internal static class UnsafeNativeMethods |
| | | 15 | | { |
| | | 16 | | public const string KERNEL32 = "kernel32.dll"; |
| | | 17 | | public const string ADVAPI32 = "advapi32.dll"; |
| | | 18 | | |
| | | 19 | | public const int ERROR_SUCCESS = 0; |
| | | 20 | | public const int ERROR_FILE_NOT_FOUND = 2; |
| | | 21 | | public const int ERROR_ACCESS_DENIED = 5; |
| | | 22 | | public const int ERROR_INVALID_HANDLE = 6; |
| | | 23 | | public const int ERROR_NOT_ENOUGH_MEMORY = 8; |
| | | 24 | | public const int ERROR_OUTOFMEMORY = 14; |
| | | 25 | | public const int ERROR_SHARING_VIOLATION = 32; |
| | | 26 | | public const int ERROR_HANDLE_EOF = 38; |
| | | 27 | | public const int ERROR_NETNAME_DELETED = 64; |
| | | 28 | | public const int ERROR_INVALID_PARAMETER = 87; |
| | | 29 | | public const int ERROR_BROKEN_PIPE = 109; |
| | | 30 | | public const int ERROR_ALREADY_EXISTS = 183; |
| | | 31 | | public const int ERROR_PIPE_BUSY = 231; |
| | | 32 | | public const int ERROR_NO_DATA = 232; |
| | | 33 | | public const int ERROR_PIPE_NOT_CONNECTED = 233; |
| | | 34 | | public const int ERROR_MORE_DATA = 234; |
| | | 35 | | public const int WAIT_TIMEOUT = 258; |
| | | 36 | | public const int ERROR_PIPE_CONNECTED = 535; |
| | | 37 | | public const int ERROR_OPERATION_ABORTED = 995; |
| | | 38 | | public const int ERROR_IO_PENDING = 997; |
| | | 39 | | public const int ERROR_SERVICE_ALREADY_RUNNING = 1056; |
| | | 40 | | public const int ERROR_SERVICE_DISABLED = 1058; |
| | | 41 | | public const int ERROR_NO_TRACKING_SERVICE = 1172; |
| | | 42 | | public const int ERROR_ALLOTTED_SPACE_EXCEEDED = 1344; |
| | | 43 | | public const int ERROR_NO_SYSTEM_RESOURCES = 1450; |
| | | 44 | | |
| | | 45 | | // When querying for the token length |
| | | 46 | | public const int ERROR_INSUFFICIENT_BUFFER = 122; |
| | | 47 | | |
| | | 48 | | public const int STATUS_PENDING = 0x103; |
| | | 49 | | |
| | | 50 | | // socket errors |
| | | 51 | | public const int WSAACCESS = 10013; |
| | | 52 | | public const int WSAEMFILE = 10024; |
| | | 53 | | public const int WSAEMSGSIZE = 10040; |
| | | 54 | | public const int WSAEADDRINUSE = 10048; |
| | | 55 | | public const int WSAEADDRNOTAVAIL = 10049; |
| | | 56 | | public const int WSAENETDOWN = 10050; |
| | | 57 | | public const int WSAENETUNREACH = 10051; |
| | | 58 | | public const int WSAENETRESET = 10052; |
| | | 59 | | public const int WSAECONNABORTED = 10053; |
| | | 60 | | public const int WSAECONNRESET = 10054; |
| | | 61 | | public const int WSAENOBUFS = 10055; |
| | | 62 | | public const int WSAESHUTDOWN = 10058; |
| | | 63 | | public const int WSAETIMEDOUT = 10060; |
| | | 64 | | public const int WSAECONNREFUSED = 10061; |
| | | 65 | | public const int WSAEHOSTDOWN = 10064; |
| | | 66 | | public const int WSAEHOSTUNREACH = 10065; |
| | | 67 | | |
| | | 68 | | public const int GENERIC_ALL = 0x10000000; |
| | | 69 | | public const int GENERIC_READ = unchecked((int)0x80000000); |
| | | 70 | | public const int GENERIC_WRITE = 0x40000000; |
| | | 71 | | public const int FILE_CREATE_PIPE_INSTANCE = 0x00000004; |
| | | 72 | | public const int FILE_WRITE_ATTRIBUTES = 0x00000100; |
| | | 73 | | public const int FILE_WRITE_DATA = 0x00000002; |
| | | 74 | | public const int FILE_WRITE_EA = 0x00000010; |
| | | 75 | | |
| | | 76 | | public const int FILE_FLAG_OVERLAPPED = 0x40000000; |
| | | 77 | | public const int FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000; |
| | | 78 | | |
| | | 79 | | public const int PIPE_ACCESS_DUPLEX = 3; |
| | | 80 | | public const int PIPE_UNLIMITED_INSTANCES = 255; |
| | | 81 | | public const int PIPE_TYPE_BYTE = 0; |
| | | 82 | | public const int PIPE_TYPE_MESSAGE = 4; |
| | | 83 | | public const int PIPE_READMODE_BYTE = 0; |
| | | 84 | | public const int PIPE_READMODE_MESSAGE = 2; |
| | | 85 | | |
| | | 86 | | // VirtualAlloc constants |
| | | 87 | | public const int PAGE_READWRITE = 4; |
| | | 88 | | |
| | | 89 | | public const int FILE_MAP_WRITE = 2; |
| | | 90 | | public const int FILE_MAP_READ = 4; |
| | | 91 | | |
| | | 92 | | public const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; |
| | | 93 | | public const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; |
| | | 94 | | public const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; |
| | | 95 | | |
| | | 96 | | // ProcessToken constants |
| | | 97 | | public const uint STANDARD_RIGHTS_READ = 0x00020000; |
| | | 98 | | public const uint TOKEN_QUERY = 0x0008; |
| | | 99 | | |
| | | 100 | | // TODO: Try replacing lpSecurityDescriptor with byte[]. I think we can avoid pinning. |
| | | 101 | | [StructLayout(LayoutKind.Sequential)] |
| | | 102 | | internal class SECURITY_ATTRIBUTES |
| | | 103 | | { |
| | | 104 | | internal uint nLength = (uint)Marshal.SizeOf(typeof(SECURITY_ATTRIBUTES)); |
| | | 105 | | internal IntPtr lpSecurityDescriptor = IntPtr.Zero; |
| | | 106 | | internal bool bInheritHandle = false; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] |
| | | 110 | | internal struct SID_AND_ATTRIBUTES |
| | | 111 | | { |
| | | 112 | | internal IntPtr Sid; |
| | | 113 | | internal SidAttribute Attributes; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | [Flags] |
| | | 117 | | internal enum SidAttribute : uint |
| | | 118 | | { |
| | | 119 | | SE_GROUP_MANDATORY = 0x1, // The SID cannot have the SE_GROUP_ENABLED attribute cleared by a call to the Adj |
| | | 120 | | SE_GROUP_ENABLED_BY_DEFAULT = 0x2, // The SID is enabled by default. |
| | | 121 | | SE_GROUP_ENABLED = 0x4, // The SID is enabled for access checks. When the system performs an access check, i |
| | | 122 | | SE_GROUP_OWNER = 0x8, // The SID identifies a group account for which the user of the token is the owner of |
| | | 123 | | SE_GROUP_USE_FOR_DENY_ONLY = 0x10, // |
| | | 124 | | SE_GROUP_RESOURCE = 0x20000000, // The SID identifies a domain-local group.Windows NT: This value is not su |
| | | 125 | | SE_GROUP_LOGON_ID = 0xC0000000, // The SID is a logon SID that identifies the logon session associated with |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | [StructLayout(LayoutKind.Sequential)] |
| | | 129 | | internal struct TOKEN_GROUPS |
| | | 130 | | { |
| | | 131 | | internal int GroupCount; |
| | | 132 | | internal IntPtr Groups; // array of SID_AND_ATTRIBUTES |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | internal enum TOKEN_INFORMATION_CLASS : int |
| | | 136 | | { |
| | | 137 | | TokenUser = 1, // TOKEN_USER structure that contains the user account of the token. = 1, |
| | | 138 | | TokenGroups, // a TOKEN_GROUPS structure that contains the group accounts associated with the token., |
| | | 139 | | TokenPrivileges, // a TOKEN_PRIVILEGES structure that contains the privileges of the token., |
| | | 140 | | TokenOwner, // a TOKEN_OWNER structure that contains the default owner security identifier (SID) for newly c |
| | | 141 | | TokenPrimaryGroup, // a TOKEN_PRIMARY_GROUP structure that contains the default primary group SID for newly |
| | | 142 | | TokenDefaultDacl, // a TOKEN_DEFAULT_DACL structure that contains the default DACL for newly created objects |
| | | 143 | | TokenSource, // a TOKEN_SOURCE structure that contains the source of the token. TOKEN_QUERY_SOURCE access is |
| | | 144 | | TokenType, // a TOKEN_TYPE value that indicates whether the token is a primary or impersonation token., |
| | | 145 | | TokenImpersonationLevel, // a SECURITY_IMPERSONATION_LEVEL value that indicates the impersonation level of t |
| | | 146 | | TokenStatistics, // a TOKEN_STATISTICS structure that contains various token statistics., |
| | | 147 | | TokenRestrictedSids, // a TOKEN_GROUPS structure that contains the list of restricting SIDs in a restricted |
| | | 148 | | TokenSessionId, // a DWORD value that indicates the Terminal Services session identifier that is associated |
| | | 149 | | TokenGroupsAndPrivileges, // a TOKEN_GROUPS_AND_PRIVILEGES structure that contains the user SID, the group a |
| | | 150 | | TokenSessionReference, // Reserved, |
| | | 151 | | TokenSandBoxInert, // a DWORD value that is nonzero if the token includes the SANDBOX_INERT flag., |
| | | 152 | | TokenAuditPolicy, |
| | | 153 | | TokenOrigin, // a TOKEN_ORIGIN value. If the token resulted from a logon that used explicit credentials, su |
| | | 154 | | TokenElevationType, |
| | | 155 | | TokenLinkedToken, |
| | | 156 | | TokenElevation, |
| | | 157 | | TokenHasRestrictions, |
| | | 158 | | TokenAccessInformation, |
| | | 159 | | TokenVirtualizationAllowed, |
| | | 160 | | TokenVirtualizationEnabled, |
| | | 161 | | TokenIntegrityLevel, |
| | | 162 | | TokenUIAccess, |
| | | 163 | | TokenMandatoryPolicy, |
| | | 164 | | TokenLogonSid, |
| | | 165 | | TokenIsAppContainer, |
| | | 166 | | TokenCapabilities, |
| | | 167 | | TokenAppContainerSid, |
| | | 168 | | TokenAppContainerNumber, |
| | | 169 | | TokenUserClaimAttributes, |
| | | 170 | | TokenDeviceClaimAttributes, |
| | | 171 | | TokenRestrictedUserClaimAttributes, |
| | | 172 | | TokenRestrictedDeviceClaimAttributes, |
| | | 173 | | TokenDeviceGroups, |
| | | 174 | | TokenRestrictedDeviceGroups, |
| | | 175 | | MaxTokenInfoClass // MaxTokenInfoClass should always be the last enum |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | [DllImport(KERNEL32)] |
| | | 179 | | internal static extern int CloseHandle(IntPtr handle); |
| | | 180 | | |
| | | 181 | | [DllImport(KERNEL32, CharSet = CharSet.Unicode, SetLastError = true)] |
| | | 182 | | internal static extern SafeFileMappingHandle CreateFileMapping( |
| | | 183 | | IntPtr fileHandle, |
| | | 184 | | SECURITY_ATTRIBUTES securityAttributes, |
| | | 185 | | int protect, |
| | | 186 | | int sizeHigh, |
| | | 187 | | int sizeLow, |
| | | 188 | | string name |
| | | 189 | | ); |
| | | 190 | | |
| | | 191 | | [DllImport(KERNEL32, CharSet = CharSet.Unicode, SetLastError = true)] |
| | | 192 | | internal unsafe static extern SafePipeHandle CreateNamedPipe |
| | | 193 | | ( |
| | | 194 | | string name, |
| | | 195 | | int openMode, |
| | | 196 | | int pipeMode, |
| | | 197 | | int maxInstances, |
| | | 198 | | int outBufSize, |
| | | 199 | | int inBufSize, |
| | | 200 | | int timeout, |
| | | 201 | | SECURITY_ATTRIBUTES securityAttributes |
| | | 202 | | ); |
| | | 203 | | |
| | | 204 | | [DllImport(KERNEL32, CharSet = CharSet.Unicode)] |
| | | 205 | | internal static extern int FormatMessage |
| | | 206 | | ( |
| | | 207 | | int dwFlags, |
| | | 208 | | IntPtr lpSource, |
| | | 209 | | int dwMessageId, |
| | | 210 | | int dwLanguageId, |
| | | 211 | | StringBuilder lpBuffer, |
| | | 212 | | int nSize, |
| | | 213 | | IntPtr arguments |
| | | 214 | | ); |
| | | 215 | | |
| | | 216 | | [DllImport(ADVAPI32, ExactSpelling = true, SetLastError = true)] |
| | | 217 | | internal static extern bool GetTokenInformation(IntPtr token, TOKEN_INFORMATION_CLASS tokenInformationClass, [Ou |
| | | 218 | | |
| | | 219 | | internal static uint GetTokenInformationLength(IntPtr token, TOKEN_INFORMATION_CLASS tokenInformationClass) |
| | | 220 | | { |
| | | 221 | | uint lengthNeeded; |
| | | 222 | | bool success; |
| | | 223 | | if (!(success = GetTokenInformation( |
| | | 224 | | token, |
| | | 225 | | tokenInformationClass, |
| | | 226 | | null, |
| | | 227 | | 0, |
| | | 228 | | out lengthNeeded))) |
| | | 229 | | { |
| | | 230 | | int error = Marshal.GetLastWin32Error(); |
| | | 231 | | if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) |
| | | 232 | | { |
| | | 233 | | throw Fx.Exception.AsError(new Win32Exception(error)); |
| | | 234 | | } |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | Fx.Assert(!success, "Retrieving the length should always fail."); |
| | | 238 | | |
| | | 239 | | return lengthNeeded; |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | [DllImport(KERNEL32, SetLastError = true)] |
| | | 243 | | internal static extern SafeViewOfFileHandle MapViewOfFile |
| | | 244 | | ( |
| | | 245 | | SafeFileMappingHandle handle, |
| | | 246 | | int dwDesiredAccess, |
| | | 247 | | int dwFileOffsetHigh, |
| | | 248 | | int dwFileOffsetLow, |
| | | 249 | | IntPtr dwNumberOfBytesToMap |
| | | 250 | | ); |
| | | 251 | | |
| | | 252 | | [DllImport(KERNEL32, ExactSpelling = true)] |
| | | 253 | | internal static extern int UnmapViewOfFile(IntPtr lpBaseAddress); |
| | | 254 | | |
| | | 255 | | [DllImport(KERNEL32, SetLastError = true)] |
| | | 256 | | internal static unsafe extern int WriteFile |
| | | 257 | | ( |
| | | 258 | | SafePipeHandle handle, |
| | | 259 | | ref byte[] bytes, |
| | | 260 | | int numBytesToWrite, |
| | | 261 | | IntPtr numBytesWritten_mustBeZero, |
| | | 262 | | NativeOverlapped* lpOverlapped |
| | | 263 | | ); |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | internal sealed class SafeFileMappingHandle : SafeHandleZeroOrMinusOneIsInvalid |
| | | 267 | | { |
| | | 268 | | internal SafeFileMappingHandle() : base(true) { } |
| | | 269 | | override protected bool ReleaseHandle() => UnsafeNativeMethods.CloseHandle(handle) != 0; |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | internal sealed class SafeViewOfFileHandle : SafeHandleZeroOrMinusOneIsInvalid |
| | | 273 | | { |
| | 0 | 274 | | internal SafeViewOfFileHandle() : base(true) { } |
| | | 275 | | override protected bool ReleaseHandle() |
| | | 276 | | { |
| | 0 | 277 | | if (UnsafeNativeMethods.UnmapViewOfFile(handle) != 0) |
| | | 278 | | { |
| | 0 | 279 | | handle = IntPtr.Zero; |
| | 0 | 280 | | return true; |
| | | 281 | | } |
| | | 282 | | |
| | 0 | 283 | | return false; |
| | | 284 | | } |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | internal sealed class SafeCloseHandle : SafeHandleZeroOrMinusOneIsInvalid |
| | | 288 | | { |
| | | 289 | | private SafeCloseHandle() : base(true) { } |
| | | 290 | | |
| | | 291 | | internal SafeCloseHandle(IntPtr handle, bool ownsHandle) : base(ownsHandle) |
| | | 292 | | { |
| | | 293 | | DiagnosticUtility.DebugAssert(handle == IntPtr.Zero || !ownsHandle, "Unsafe to create a SafeHandle that owns |
| | | 294 | | SetHandle(handle); |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | protected override bool ReleaseHandle() |
| | | 298 | | { |
| | | 299 | | return UnsafeNativeMethods.CloseHandle(handle) != 0; |
| | | 300 | | } |
| | | 301 | | } |
| | | 302 | | } |