3 * Windows security support.
5 * Copyright 2016 Microsoft
6 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11 #if defined(HOST_WIN32)
14 #include "mono/metadata/mono-security-windows-internals.h"
16 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
21 #ifndef PROTECTED_DACL_SECURITY_INFORMATION
22 #define PROTECTED_DACL_SECURITY_INFORMATION 0x80000000L
25 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
27 GetSidName (gunichar2 *server, PSID sid, gint32 *size)
29 gunichar2 *uniname = NULL;
32 SID_NAME_USE peUse; /* out */
34 LookupAccountSid (server, sid, NULL, &cchName, NULL,
37 if ((cchName > 0) && (cchDomain > 0)) {
38 gunichar2 *user = g_malloc0 ((cchName + 1) * 2);
39 gunichar2 *domain = g_malloc0 ((cchDomain + 1) * 2);
41 LookupAccountSid (server, sid, user, &cchName, domain,
46 /* domain/machine name included (+ sepearator) */
47 *size = cchName + cchDomain + 1;
48 uniname = g_malloc0 ((*size + 1) * 2);
49 memcpy (uniname, domain, cchDomain * 2);
50 *(uniname + cchDomain) = '\\';
51 memcpy (uniname + cchDomain + 1, user, cchName * 2);
55 /* no domain / machine */
61 /* nothing -> return NULL */
72 ves_icall_System_Security_Principal_WindowsIdentity_GetCurrentToken (void)
74 gpointer token = NULL;
76 /* Note: This isn't a copy of the Token - we must not close it!!!
77 * http://www.develop.com/kbrown/book/html/whatis_windowsprincipal.html
80 /* thread may be impersonating somebody */
81 if (OpenThreadToken (GetCurrentThread (), MAXIMUM_ALLOWED, 1, &token) == 0) {
82 /* if not take the process identity */
83 OpenProcessToken (GetCurrentProcess (), MAXIMUM_ALLOWED, &token);
90 mono_security_win_get_token_name (gpointer token, gunichar2 ** uniname)
94 GetTokenInformation (token, TokenUser, NULL, size, (PDWORD)&size);
96 TOKEN_USER *tu = g_malloc0 (size);
97 if (GetTokenInformation (token, TokenUser, tu, size, (PDWORD)&size)) {
98 *uniname = GetSidName (NULL, tu->User.Sid, &size);
105 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
108 ves_icall_System_Security_Principal_WindowsIdentity_GetTokenName (gpointer token)
111 MonoString *result = NULL;
112 gunichar2 *uniname = NULL;
117 size = mono_security_win_get_token_name (token, &uniname);
120 result = mono_string_new_utf16_checked (mono_domain_get (), uniname, size, &error);
123 result = mono_string_new_checked (mono_domain_get (), "", &error);
128 mono_error_set_pending_exception (&error);
133 ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoString *username)
135 gpointer token = NULL;
137 /* TODO: MS has something like this working in Windows 2003 (client and
138 * server) but works only for domain accounts (so it's quite limiting).
139 * http://www.develop.com/kbrown/book/html/howto_logonuser.html
141 g_warning ("Unsupported on Win32 (anyway requires W2K3 minimum)");
145 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
147 ves_icall_System_Security_Principal_WindowsIdentity_GetRoles (gpointer token)
150 MonoArray *array = NULL;
151 MonoDomain *domain = mono_domain_get ();
155 GetTokenInformation (token, TokenGroups, NULL, size, (PDWORD)&size);
157 TOKEN_GROUPS *tg = g_malloc0 (size);
158 if (GetTokenInformation (token, TokenGroups, tg, size, (PDWORD)&size)) {
160 int num = tg->GroupCount;
162 array = mono_array_new_checked (domain, mono_get_string_class (), num, &error);
163 if (mono_error_set_pending_exception (&error)) {
168 for (i=0; i < num; i++) {
170 gunichar2 *uniname = GetSidName (NULL, tg->Groups [i].Sid, &size);
173 MonoString *str = mono_string_new_utf16_checked (domain, uniname, size, &error);
174 if (!is_ok (&error)) {
177 mono_error_set_pending_exception (&error);
180 mono_array_setref (array, i, str);
189 /* return empty array of string, i.e. string [0] */
190 array = mono_array_new_checked (domain, mono_get_string_class (), 0, &error);
191 mono_error_set_pending_exception (&error);
195 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
198 ves_icall_System_Security_Principal_WindowsImpersonationContext_CloseToken (gpointer token)
200 gboolean result = TRUE;
201 result = (CloseHandle (token) != 0);
205 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
207 ves_icall_System_Security_Principal_WindowsImpersonationContext_DuplicateToken (gpointer token)
209 gpointer dupe = NULL;
211 if (DuplicateToken (token, SecurityImpersonation, &dupe) == 0) {
216 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
219 ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupId (gpointer user, gpointer group)
221 gboolean result = FALSE;
223 /* The convertion from an ID to a string is done in managed code for Windows */
224 g_warning ("IsMemberOfGroupId should never be called on Win32");
229 ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupName (gpointer user, MonoString *group)
231 gboolean result = FALSE;
233 /* Windows version use a cache built using WindowsIdentity._GetRoles */
234 g_warning ("IsMemberOfGroupName should never be called on Win32");
238 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
240 GetAdministratorsSid (void)
242 SID_IDENTIFIER_AUTHORITY admins = { SECURITY_NT_AUTHORITY };
244 if (!AllocateAndInitializeSid (&admins, 2, SECURITY_BUILTIN_DOMAIN_RID,
245 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pSid))
247 /* Note: this SID must be freed with FreeSid () */
252 GetEveryoneSid (void)
254 SID_IDENTIFIER_AUTHORITY everyone = { SECURITY_WORLD_SID_AUTHORITY };
256 if (!AllocateAndInitializeSid (&everyone, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pSid))
258 /* Note: this SID must be freed with FreeSid () */
263 GetCurrentUserSid (void)
267 gpointer token = ves_icall_System_Security_Principal_WindowsIdentity_GetCurrentToken ();
269 GetTokenInformation (token, TokenUser, NULL, size, (PDWORD)&size);
271 TOKEN_USER *tu = g_malloc0 (size);
272 if (GetTokenInformation (token, TokenUser, tu, size, (PDWORD)&size)) {
273 DWORD length = GetLengthSid (tu->User.Sid);
274 sid = (PSID) g_malloc0 (length);
275 if (!CopySid (length, sid, tu->User.Sid)) {
282 /* Note: this SID must be freed with g_free () */
287 GetRightsFromSid (PSID sid, PACL acl)
289 ACCESS_MASK rights = 0;
292 BuildTrusteeWithSidW (&trustee, sid);
293 if (GetEffectiveRightsFromAcl (acl, &trustee, &rights) != ERROR_SUCCESS)
300 mono_security_win_is_machine_protected (gunichar2 *path)
302 gboolean success = FALSE;
304 PSECURITY_DESCRIPTOR pSD = NULL;
305 PSID pEveryoneSid = NULL;
307 DWORD dwRes = GetNamedSecurityInfoW (path, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, &pDACL, NULL, &pSD);
308 if (dwRes != ERROR_SUCCESS)
311 /* We check that Everyone is still limited to READ-ONLY -
312 but not if new entries have been added by an Administrator */
314 pEveryoneSid = GetEveryoneSid ();
316 ACCESS_MASK rights = GetRightsFromSid (pEveryoneSid, pDACL);
317 /* http://msdn.microsoft.com/library/en-us/security/security/generic_access_rights.asp?frame=true */
318 success = (rights == (READ_CONTROL | SYNCHRONIZE | FILE_READ_DATA | FILE_READ_EA | FILE_READ_ATTRIBUTES));
319 FreeSid (pEveryoneSid);
321 /* Note: we don't need to check our own access -
322 we'll know soon enough when reading the file */
331 mono_security_win_is_user_protected (gunichar2 *path)
333 gboolean success = FALSE;
335 PSID pEveryoneSid = NULL;
336 PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL;
338 DWORD dwRes = GetNamedSecurityInfoW (path, SE_FILE_OBJECT,
339 DACL_SECURITY_INFORMATION, NULL, NULL, &pDACL, NULL, &pSecurityDescriptor);
340 if (dwRes != ERROR_SUCCESS)
343 /* We check that our original entries in the ACL are in place -
344 but not if new entries have been added by the user */
346 /* Everyone should be denied */
347 pEveryoneSid = GetEveryoneSid ();
349 ACCESS_MASK rights = GetRightsFromSid (pEveryoneSid, pDACL);
350 success = (rights == 0);
351 FreeSid (pEveryoneSid);
353 /* Note: we don't need to check our own access -
354 we'll know soon enough when reading the file */
356 if (pSecurityDescriptor)
357 LocalFree (pSecurityDescriptor);
363 mono_security_win_protect_machine (gunichar2 *path)
365 PSID pEveryoneSid = GetEveryoneSid ();
366 PSID pAdminsSid = GetAdministratorsSid ();
369 if (pEveryoneSid && pAdminsSid) {
371 EXPLICIT_ACCESS ea [2];
372 ZeroMemory (&ea, 2 * sizeof (EXPLICIT_ACCESS));
374 /* grant all access to the BUILTIN\Administrators group */
375 BuildTrusteeWithSidW (&ea [0].Trustee, pAdminsSid);
376 ea [0].grfAccessPermissions = GENERIC_ALL;
377 ea [0].grfAccessMode = SET_ACCESS;
378 ea [0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
379 ea [0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
380 ea [0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
382 /* read-only access everyone */
383 BuildTrusteeWithSidW (&ea [1].Trustee, pEveryoneSid);
384 ea [1].grfAccessPermissions = GENERIC_READ;
385 ea [1].grfAccessMode = SET_ACCESS;
386 ea [1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
387 ea [1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
388 ea [1].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
390 retval = SetEntriesInAcl (2, ea, NULL, &pDACL);
391 if (retval == ERROR_SUCCESS) {
392 /* with PROTECTED_DACL_SECURITY_INFORMATION we */
393 /* remove any existing ACL (like inherited ones) */
394 retval = SetNamedSecurityInfo (path, SE_FILE_OBJECT,
395 DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
396 NULL, NULL, pDACL, NULL);
403 FreeSid (pEveryoneSid);
405 FreeSid (pAdminsSid);
406 return (retval == ERROR_SUCCESS);
410 mono_security_win_protect_user (gunichar2 *path)
414 PSID pCurrentSid = GetCurrentUserSid ();
418 ZeroMemory (&ea, sizeof (EXPLICIT_ACCESS));
420 /* grant exclusive access to the current user */
421 BuildTrusteeWithSidW (&ea.Trustee, pCurrentSid);
422 ea.grfAccessPermissions = GENERIC_ALL;
423 ea.grfAccessMode = SET_ACCESS;
424 ea.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
425 ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
426 ea.Trustee.TrusteeType = TRUSTEE_IS_USER;
428 retval = SetEntriesInAcl (1, &ea, NULL, &pDACL);
429 if (retval == ERROR_SUCCESS) {
430 /* with PROTECTED_DACL_SECURITY_INFORMATION we
431 remove any existing ACL (like inherited ones) */
432 retval = SetNamedSecurityInfo (path, SE_FILE_OBJECT,
433 DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
434 NULL, NULL, pDACL, NULL);
439 g_free (pCurrentSid); /* g_malloc0 */
442 return (retval == ERROR_SUCCESS);
444 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
447 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_CanSecure (MonoString *root)
451 /* ACL are nice... unless you have FAT or other uncivilized filesystem */
452 if (!GetVolumeInformation (mono_string_chars (root), NULL, 0, NULL, NULL, (LPDWORD)&flags, NULL, 0))
454 return ((flags & FS_PERSISTENT_ACLS) == FS_PERSISTENT_ACLS);
458 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_IsMachineProtected (MonoString *path)
460 gboolean ret = FALSE;
462 /* no one, but the owner, should have write access to the directory */
463 ret = mono_security_win_is_machine_protected (mono_string_chars (path));
464 return (MonoBoolean)ret;
468 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_IsUserProtected (MonoString *path)
470 gboolean ret = FALSE;
472 /* no one, but the user, should have access to the directory */
473 ret = mono_security_win_is_user_protected (mono_string_chars (path));
474 return (MonoBoolean)ret;
478 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_ProtectMachine (MonoString *path)
480 gboolean ret = FALSE;
482 /* read/write to owner, read to everyone else */
483 ret = mono_security_win_protect_machine (mono_string_chars (path));
484 return (MonoBoolean)ret;
488 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_ProtectUser (MonoString *path)
490 gboolean ret = FALSE;
492 /* read/write to user, no access to everyone else */
493 ret = mono_security_win_protect_user (mono_string_chars (path));
494 return (MonoBoolean)ret;
496 #endif /* HOST_WIN32 */