2 * security.c: Security internal calls
5 * Sebastien Pouliot <sebastien@ximian.com>
7 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #include <mono/metadata/assembly.h>
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/image.h>
18 #include <mono/metadata/exception.h>
19 #include <mono/metadata/object-internals.h>
20 #include <mono/metadata/metadata-internals.h>
21 #include <mono/metadata/security.h>
22 #include <mono/io-layer/io-layer.h>
23 #include <mono/utils/strenc.h>
35 #include <sys/types.h>
42 #ifndef HAVE_GETGRGID_R
43 #warning Non-thread safe getgrgid being used!
45 #ifndef HAVE_GETGRNAM_R
46 #warning Non-thread safe getgrnam being used!
48 #ifndef HAVE_GETPWNAM_R
49 #warning Non-thread safe getpwnam being used!
51 #ifndef HAVE_GETPWUID_R
52 #warning Non-thread safe getpwuid being used!
55 #endif /* defined(__GNUC__) */
56 #endif /* !HOST_WIN32 */
58 /* internal functions - reuse driven */
60 /* ask a server to translate a SID into a textual representation */
62 #define MONO_SYSCONF_DEFAULT_SIZE ((size_t) 1024)
65 * Ensure we always get a valid (usable) value from sysconf.
66 * In case of error, we return the default value.
68 static size_t mono_sysconf (int name)
70 size_t size = (size_t) sysconf (name);
72 return (size == -1) ? MONO_SYSCONF_DEFAULT_SIZE : size;
76 GetTokenName (uid_t uid)
80 #ifdef HAVE_GETPWUID_R
86 struct passwd *p = NULL;
89 #ifdef HAVE_GETPWUID_R
90 #ifdef _SC_GETPW_R_SIZE_MAX
91 fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
93 fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
95 fbuf = (gchar *)g_malloc0 (fbufsize);
96 retval = getpwuid_r (uid, &pwd, fbuf, fbufsize, &p);
97 result = ((retval == 0) && (p == &pwd));
99 /* default to non thread-safe but posix compliant function */
101 result = (p != NULL);
105 uname = g_strdup (p->pw_name);
108 #ifdef HAVE_GETPWUID_R
116 IsMemberInList (uid_t user, struct group *g)
118 gboolean result = FALSE;
119 gchar *utf8_username = GetTokenName (user);
125 gchar **users = g->gr_mem;
129 if (strcmp (utf8_username, u) == 0) {
137 g_free (utf8_username);
142 IsDefaultGroup (uid_t user, gid_t group)
144 #ifdef HAVE_GETPWUID_R
150 struct passwd *p = NULL;
153 #ifdef HAVE_GETPWUID_R
154 #ifdef _SC_GETPW_R_SIZE_MAX
155 fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
157 fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
160 fbuf = (gchar *)g_malloc0 (fbufsize);
161 retval = getpwuid_r (user, &pwd, fbuf, fbufsize, &p);
162 result = ((retval == 0) && (p == &pwd));
164 /* default to non thread-safe but posix compliant function */
166 result = (p != NULL);
170 result = (p->pw_gid == group);
173 #ifdef HAVE_GETPWUID_R
181 IsMemberOf (gid_t user, struct group *g)
186 /* is it the user default group */
187 if (IsDefaultGroup (user, g->gr_gid))
190 /* is the user in the group list */
191 return IsMemberInList (user, g);
193 #endif /* !HOST_WIN32 */
197 /* System.Security.Principal.WindowsIdentity */
201 ves_icall_System_Security_Principal_WindowsIdentity_GetCurrentToken (void)
203 return GINT_TO_POINTER (geteuid ());
207 internal_get_token_name (gpointer token, gunichar2 ** uniname)
211 gchar *uname = GetTokenName ((uid_t) GPOINTER_TO_INT (token));
214 size = strlen (uname);
215 *uniname = g_utf8_to_utf16 (uname, size, NULL, NULL, NULL);
223 ves_icall_System_Security_Principal_WindowsIdentity_GetTokenName (gpointer token)
226 MonoString *result = NULL;
227 gunichar2 *uniname = NULL;
230 mono_error_init (&error);
232 size = internal_get_token_name (token, &uniname);
235 result = mono_string_new_utf16_checked (mono_domain_get (), uniname, size, &error);
238 result = mono_string_new (mono_domain_get (), "");
243 mono_error_set_pending_exception (&error);
246 #endif /* !HOST_WIN32 */
250 ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoString *username)
252 #ifdef HAVE_GETPWNAM_R
258 gpointer token = (gpointer) -2;
263 utf8_name = mono_unicode_to_external (mono_string_chars (username));
265 #ifdef HAVE_GETPWNAM_R
266 #ifdef _SC_GETPW_R_SIZE_MAX
267 fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
269 fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
272 fbuf = (gchar *)g_malloc0 (fbufsize);
273 retval = getpwnam_r (utf8_name, &pwd, fbuf, fbufsize, &p);
274 result = ((retval == 0) && (p == &pwd));
276 /* default to non thread-safe but posix compliant function */
277 p = getpwnam (utf8_name);
278 result = (p != NULL);
282 token = GINT_TO_POINTER (p->pw_uid);
285 #ifdef HAVE_GETPWNAM_R
292 #endif /* HOST_WIN32 */
294 /* http://www.dotnet247.com/247reference/msgs/39/195403.aspx
295 // internal static string[] WindowsIdentity._GetRoles (IntPtr token)
300 ves_icall_System_Security_Principal_WindowsIdentity_GetRoles (gpointer token)
303 MonoArray *array = NULL;
304 MonoDomain *domain = mono_domain_get ();
306 /* POSIX-compliant systems should use IsMemberOfGroupId or IsMemberOfGroupName */
307 g_warning ("WindowsIdentity._GetRoles should never be called on POSIX");
310 /* return empty array of string, i.e. string [0] */
311 array = mono_array_new_checked (domain, mono_get_string_class (), 0, &error);
312 mono_error_set_pending_exception (&error);
316 #endif /* !HOST_WIN32 */
318 /* System.Security.Principal.WindowsImpersonationContext */
322 ves_icall_System_Security_Principal_WindowsImpersonationContext_CloseToken (gpointer token)
326 #endif /* !HOST_WIN32 */
330 ves_icall_System_Security_Principal_WindowsImpersonationContext_DuplicateToken (gpointer token)
334 #endif /* !HOST_WIN32 */
336 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
338 ves_icall_System_Security_Principal_WindowsImpersonationContext_SetCurrentToken (gpointer token)
340 /* Posix version implemented in /mono/mono/io-layer/security.c */
341 return (ImpersonateLoggedOnUser (token) != 0);
345 ves_icall_System_Security_Principal_WindowsImpersonationContext_RevertToSelf (void)
347 /* Posix version implemented in /mono/mono/io-layer/security.c */
348 return (RevertToSelf () != 0);
350 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
352 /* System.Security.Principal.WindowsPrincipal */
356 ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupId (gpointer user, gpointer group)
358 gboolean result = FALSE;
360 #ifdef HAVE_GETGRGID_R
366 struct group *g = NULL;
368 #ifdef HAVE_GETGRGID_R
369 #ifdef _SC_GETGR_R_SIZE_MAX
370 fbufsize = mono_sysconf (_SC_GETGR_R_SIZE_MAX);
372 fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
374 fbuf = (gchar *)g_malloc0 (fbufsize);
375 retval = getgrgid_r ((gid_t) GPOINTER_TO_INT (group), &grp, fbuf, fbufsize, &g);
376 result = ((retval == 0) && (g == &grp));
378 /* default to non thread-safe but posix compliant function */
379 g = getgrgid ((gid_t) GPOINTER_TO_INT (group));
380 result = (g != NULL);
384 result = IsMemberOf ((uid_t) GPOINTER_TO_INT (user), g);
387 #ifdef HAVE_GETGRGID_R
395 ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupName (gpointer user, MonoString *group)
397 gboolean result = FALSE;
398 gchar *utf8_groupname;
400 utf8_groupname = mono_unicode_to_external (mono_string_chars (group));
401 if (utf8_groupname) {
402 struct group *g = NULL;
403 #ifdef HAVE_GETGRNAM_R
407 #ifdef _SC_GETGR_R_SIZE_MAX
408 size_t fbufsize = mono_sysconf (_SC_GETGR_R_SIZE_MAX);
410 size_t fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
412 fbuf = (gchar *)g_malloc0 (fbufsize);
413 retval = getgrnam_r (utf8_groupname, &grp, fbuf, fbufsize, &g);
414 result = ((retval == 0) && (g == &grp));
416 /* default to non thread-safe but posix compliant function */
417 g = getgrnam (utf8_groupname);
418 result = (g != NULL);
422 result = IsMemberOf ((uid_t) GPOINTER_TO_INT (user), g);
425 #ifdef HAVE_GETGRNAM_R
428 g_free (utf8_groupname);
433 #endif /* !HOST_WIN32 */
435 /* Mono.Security.Cryptography IO related internal calls */
439 IsProtected (MonoString *path, gint32 protection)
441 gboolean result = FALSE;
442 gchar *utf8_name = mono_unicode_to_external (mono_string_chars (path));
445 if (stat (utf8_name, &st) == 0) {
446 result = (((st.st_mode & 0777) & protection) == 0);
455 Protect (MonoString *path, gint32 file_mode, gint32 add_dir_mode)
457 gboolean result = FALSE;
458 gchar *utf8_name = mono_unicode_to_external (mono_string_chars (path));
461 if (stat (utf8_name, &st) == 0) {
462 int mode = file_mode;
463 if (st.st_mode & S_IFDIR)
464 mode |= add_dir_mode;
465 result = (chmod (utf8_name, mode) == 0);
473 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_CanSecure (MonoString *root)
475 /* we assume some kind of security is applicable outside Windows */
480 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_IsMachineProtected (MonoString *path)
482 gboolean ret = FALSE;
484 /* no one, but the owner, should have write access to the directory */
485 ret = IsProtected (path, (S_IWGRP | S_IWOTH));
486 return (MonoBoolean)ret;
490 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_IsUserProtected (MonoString *path)
492 gboolean ret = FALSE;
494 /* no one, but the user, should have access to the directory */
495 ret = IsProtected (path, (S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH));
496 return (MonoBoolean)ret;
500 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_ProtectMachine (MonoString *path)
502 gboolean ret = FALSE;
504 /* read/write to owner, read to everyone else */
505 ret = Protect (path, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), (S_IXUSR | S_IXGRP | S_IXOTH));
506 return (MonoBoolean)ret;
510 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_ProtectUser (MonoString *path)
512 gboolean ret = FALSE;
514 /* read/write to user, no access to everyone else */
515 ret = Protect (path, (S_IRUSR | S_IWUSR), S_IXUSR);
516 return (MonoBoolean)ret;
518 #endif /* !HOST_WIN32 */
521 * Returns TRUE if there is "something" where the Authenticode signature is
522 * normally located. Returns FALSE is data directory is empty.
524 * Note: Neither the structure nor the signature is verified by this function.
527 ves_icall_System_Security_Policy_Evidence_IsAuthenticodePresent (MonoReflectionAssembly *refass)
529 if (refass && refass->assembly && refass->assembly->image) {
530 return (MonoBoolean)mono_image_has_authenticode_entry (refass->assembly->image);
536 /* System.Security.SecureString related internal calls */
538 static MonoImage *system_security_assembly = NULL;
541 ves_icall_System_Security_SecureString_DecryptInternal (MonoArray *data, MonoObject *scope)
544 invoke_protected_memory_method (data, scope, FALSE, &error);
545 mono_error_set_pending_exception (&error);
548 ves_icall_System_Security_SecureString_EncryptInternal (MonoArray* data, MonoObject *scope)
551 invoke_protected_memory_method (data, scope, TRUE, &error);
552 mono_error_set_pending_exception (&error);
555 void invoke_protected_memory_method (MonoArray *data, MonoObject *scope, gboolean encrypt, MonoError *error)
561 mono_error_init (error);
563 if (system_security_assembly == NULL) {
564 system_security_assembly = mono_image_loaded ("System.Security");
565 if (!system_security_assembly) {
566 MonoAssembly *sa = mono_assembly_open ("System.Security.dll", NULL);
568 g_assert_not_reached ();
569 system_security_assembly = mono_assembly_get_image (sa);
573 klass = mono_class_load_from_name (system_security_assembly,
574 "System.Security.Cryptography", "ProtectedMemory");
575 method = mono_class_get_method_from_name (klass, encrypt ? "Protect" : "Unprotect", 2);
577 params [1] = scope; /* MemoryProtectionScope.SameProcess */
579 mono_runtime_invoke_checked (method, NULL, params, error);