3 * Security internal calls
6 * Sebastien Pouliot <sebastien@ximian.com>
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include <mono/metadata/assembly-internals.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/image.h>
19 #include <mono/metadata/exception.h>
20 #include <mono/metadata/object-internals.h>
21 #include <mono/metadata/metadata-internals.h>
22 #include <mono/metadata/security.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)
82 #ifdef HAVE_GETPWUID_R
88 struct passwd *p = NULL;
91 #ifdef HAVE_GETPWUID_R
92 #ifdef _SC_GETPW_R_SIZE_MAX
93 fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
95 fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
97 fbuf = (gchar *)g_malloc0 (fbufsize);
98 retval = getpwuid_r (uid, &pwd, fbuf, fbufsize, &p);
99 result = ((retval == 0) && (p == &pwd));
101 /* default to non thread-safe but posix compliant function */
103 result = (p != NULL);
107 uname = g_strdup (p->pw_name);
110 #ifdef HAVE_GETPWUID_R
114 #endif /* HAVE_PWD_H */
122 IsMemberInList (uid_t user, struct group *g)
124 gboolean result = FALSE;
125 gchar *utf8_username = GetTokenName (user);
131 gchar **users = g->gr_mem;
135 if (strcmp (utf8_username, u) == 0) {
143 g_free (utf8_username);
147 #endif /* HAVE_GRP_H */
150 IsDefaultGroup (uid_t user, gid_t group)
152 gboolean result = FALSE;
156 #ifdef HAVE_GETPWUID_R
162 struct passwd *p = NULL;
164 #ifdef HAVE_GETPWUID_R
165 #ifdef _SC_GETPW_R_SIZE_MAX
166 fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
168 fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
171 fbuf = (gchar *)g_malloc0 (fbufsize);
172 retval = getpwuid_r (user, &pwd, fbuf, fbufsize, &p);
173 result = ((retval == 0) && (p == &pwd));
175 /* default to non thread-safe but posix compliant function */
177 result = (p != NULL);
181 result = (p->pw_gid == group);
184 #ifdef HAVE_GETPWUID_R
188 #endif /* HAVE_PWD_H */
196 IsMemberOf (gid_t user, struct group *g)
201 /* is it the user default group */
202 if (IsDefaultGroup (user, g->gr_gid))
205 /* is the user in the group list */
206 return IsMemberInList (user, g);
209 #endif /* HAVE_GRP_H */
211 #endif /* !HOST_WIN32 */
215 /* System.Security.Principal.WindowsIdentity */
219 mono_security_principal_windows_identity_get_current_token ()
221 return GINT_TO_POINTER (geteuid ());
225 ves_icall_System_Security_Principal_WindowsIdentity_GetCurrentToken (MonoError *error)
228 return mono_security_principal_windows_identity_get_current_token ();
232 internal_get_token_name (gpointer token, gunichar2 ** uniname)
236 gchar *uname = GetTokenName ((uid_t) GPOINTER_TO_INT (token));
239 size = strlen (uname);
240 *uniname = g_utf8_to_utf16 (uname, size, NULL, NULL, NULL);
248 ves_icall_System_Security_Principal_WindowsIdentity_GetTokenName (gpointer token, MonoError *error)
250 MonoStringHandle result;
251 gunichar2 *uniname = NULL;
256 size = internal_get_token_name (token, &uniname);
259 result = mono_string_new_utf16_handle (mono_domain_get (), uniname, size, error);
262 result = mono_string_new_handle (mono_domain_get (), "", error);
269 #endif /* !HOST_WIN32 */
273 ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoStringHandle username, MonoError *error)
275 gpointer token = (gpointer)-2;
280 #ifdef HAVE_GETPWNAM_R
290 utf8_name = mono_string_handle_to_utf8 (username, error);
291 return_val_if_nok (error, NULL);
293 #ifdef HAVE_GETPWNAM_R
294 #ifdef _SC_GETPW_R_SIZE_MAX
295 fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
297 fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
300 fbuf = (gchar *)g_malloc0 (fbufsize);
301 retval = getpwnam_r (utf8_name, &pwd, fbuf, fbufsize, &p);
302 result = ((retval == 0) && (p == &pwd));
304 /* default to non thread-safe but posix compliant function */
305 p = getpwnam (utf8_name);
306 result = (p != NULL);
310 token = GINT_TO_POINTER (p->pw_uid);
313 #ifdef HAVE_GETPWNAM_R
318 #endif /* HAVE_PWD_H */
322 #endif /* HOST_WIN32 */
324 /* http://www.dotnet247.com/247reference/msgs/39/195403.aspx
325 // internal static string[] WindowsIdentity._GetRoles (IntPtr token)
330 ves_icall_System_Security_Principal_WindowsIdentity_GetRoles (gpointer token)
333 MonoArray *array = NULL;
334 MonoDomain *domain = mono_domain_get ();
336 /* POSIX-compliant systems should use IsMemberOfGroupId or IsMemberOfGroupName */
337 g_warning ("WindowsIdentity._GetRoles should never be called on POSIX");
340 /* return empty array of string, i.e. string [0] */
341 array = mono_array_new_checked (domain, mono_get_string_class (), 0, &error);
342 mono_error_set_pending_exception (&error);
346 #endif /* !HOST_WIN32 */
348 /* System.Security.Principal.WindowsImpersonationContext */
352 ves_icall_System_Security_Principal_WindowsImpersonationContext_CloseToken (gpointer token)
356 #endif /* !HOST_WIN32 */
360 ves_icall_System_Security_Principal_WindowsImpersonationContext_DuplicateToken (gpointer token)
364 #endif /* !HOST_WIN32 */
366 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
368 ves_icall_System_Security_Principal_WindowsImpersonationContext_SetCurrentToken (gpointer token)
371 return (ImpersonateLoggedOnUser (token) != 0);
373 uid_t itoken = (uid_t) GPOINTER_TO_INT (token);
374 #ifdef HAVE_SETRESUID
375 if (setresuid (-1, itoken, getuid ()) < 0)
378 return geteuid () == itoken;
383 ves_icall_System_Security_Principal_WindowsImpersonationContext_RevertToSelf (void)
386 return (RevertToSelf () != 0);
388 #ifdef HAVE_GETRESUID
393 #ifdef HAVE_GETRESUID
394 if (getresuid (&ruid, &euid, &suid) < 0)
397 #ifdef HAVE_SETRESUID
398 if (setresuid (-1, suid, -1) < 0)
403 return geteuid () == suid;
406 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
408 /* System.Security.Principal.WindowsPrincipal */
412 ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupId (gpointer user, gpointer group)
414 gboolean result = FALSE;
418 #ifdef HAVE_GETGRGID_R
424 struct group *g = NULL;
426 #ifdef HAVE_GETGRGID_R
427 #ifdef _SC_GETGR_R_SIZE_MAX
428 fbufsize = mono_sysconf (_SC_GETGR_R_SIZE_MAX);
430 fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
432 fbuf = (gchar *)g_malloc0 (fbufsize);
433 retval = getgrgid_r ((gid_t) GPOINTER_TO_INT (group), &grp, fbuf, fbufsize, &g);
434 result = ((retval == 0) && (g == &grp));
436 /* default to non thread-safe but posix compliant function */
437 g = getgrgid ((gid_t) GPOINTER_TO_INT (group));
438 result = (g != NULL);
442 result = IsMemberOf ((uid_t) GPOINTER_TO_INT (user), g);
445 #ifdef HAVE_GETGRGID_R
449 #endif /* HAVE_GRP_H */
455 ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupName (gpointer user, MonoString *group)
457 gboolean result = FALSE;
461 gchar *utf8_groupname;
463 utf8_groupname = mono_unicode_to_external (mono_string_chars (group));
464 if (utf8_groupname) {
465 struct group *g = NULL;
466 #ifdef HAVE_GETGRNAM_R
470 #ifdef _SC_GETGR_R_SIZE_MAX
471 size_t fbufsize = mono_sysconf (_SC_GETGR_R_SIZE_MAX);
473 size_t fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
475 fbuf = (gchar *)g_malloc0 (fbufsize);
476 retval = getgrnam_r (utf8_groupname, &grp, fbuf, fbufsize, &g);
477 result = ((retval == 0) && (g == &grp));
479 /* default to non thread-safe but posix compliant function */
480 g = getgrnam (utf8_groupname);
481 result = (g != NULL);
485 result = IsMemberOf ((uid_t) GPOINTER_TO_INT (user), g);
488 #ifdef HAVE_GETGRNAM_R
491 g_free (utf8_groupname);
494 #endif /* HAVE_GRP_H */
498 #endif /* !HOST_WIN32 */
500 /* Mono.Security.Cryptography IO related internal calls */
504 IsProtected (MonoString *path, gint32 protection)
506 gboolean result = FALSE;
507 gchar *utf8_name = mono_unicode_to_external (mono_string_chars (path));
510 if (stat (utf8_name, &st) == 0) {
511 result = (((st.st_mode & 0777) & protection) == 0);
520 Protect (MonoString *path, gint32 file_mode, gint32 add_dir_mode)
522 gboolean result = FALSE;
523 gchar *utf8_name = mono_unicode_to_external (mono_string_chars (path));
526 if (stat (utf8_name, &st) == 0) {
527 int mode = file_mode;
528 if (st.st_mode & S_IFDIR)
529 mode |= add_dir_mode;
530 result = (chmod (utf8_name, mode) == 0);
538 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_CanSecure (MonoString *root)
540 /* we assume some kind of security is applicable outside Windows */
545 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_IsMachineProtected (MonoString *path)
547 gboolean ret = FALSE;
549 /* no one, but the owner, should have write access to the directory */
550 ret = IsProtected (path, (S_IWGRP | S_IWOTH));
551 return (MonoBoolean)ret;
555 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_IsUserProtected (MonoString *path)
557 gboolean ret = FALSE;
559 /* no one, but the user, should have access to the directory */
560 ret = IsProtected (path, (S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH));
561 return (MonoBoolean)ret;
565 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_ProtectMachine (MonoString *path)
567 gboolean ret = FALSE;
569 /* read/write to owner, read to everyone else */
570 ret = Protect (path, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), (S_IXUSR | S_IXGRP | S_IXOTH));
571 return (MonoBoolean)ret;
575 ves_icall_Mono_Security_Cryptography_KeyPairPersistence_ProtectUser (MonoString *path)
577 gboolean ret = FALSE;
579 /* read/write to user, no access to everyone else */
580 ret = Protect (path, (S_IRUSR | S_IWUSR), S_IXUSR);
581 return (MonoBoolean)ret;
583 #endif /* !HOST_WIN32 */
586 * Returns TRUE if there is "something" where the Authenticode signature is
587 * normally located. Returns FALSE is data directory is empty.
589 * Note: Neither the structure nor the signature is verified by this function.
592 ves_icall_System_Security_Policy_Evidence_IsAuthenticodePresent (MonoReflectionAssemblyHandle refass, MonoError *error)
595 if (MONO_HANDLE_IS_NULL (refass))
597 MonoAssembly *assembly = MONO_HANDLE_GETVAL (refass, assembly);
598 if (assembly && assembly->image) {
599 return (MonoBoolean)mono_image_has_authenticode_entry (assembly->image);
605 /* System.Security.SecureString related internal calls */
607 static MonoImage *system_security_assembly = NULL;
610 ves_icall_System_Security_SecureString_DecryptInternal (MonoArray *data, MonoObject *scope)
613 invoke_protected_memory_method (data, scope, FALSE, &error);
614 mono_error_set_pending_exception (&error);
617 ves_icall_System_Security_SecureString_EncryptInternal (MonoArray* data, MonoObject *scope)
620 invoke_protected_memory_method (data, scope, TRUE, &error);
621 mono_error_set_pending_exception (&error);
624 void invoke_protected_memory_method (MonoArray *data, MonoObject *scope, gboolean encrypt, MonoError *error)
632 if (system_security_assembly == NULL) {
633 system_security_assembly = mono_image_loaded ("System.Security");
634 if (!system_security_assembly) {
635 MonoAssembly *sa = mono_assembly_open_predicate ("System.Security.dll", FALSE, FALSE, NULL, NULL, NULL);
637 g_assert_not_reached ();
638 system_security_assembly = mono_assembly_get_image (sa);
642 klass = mono_class_load_from_name (system_security_assembly,
643 "System.Security.Cryptography", "ProtectedMemory");
644 method = mono_class_get_method_from_name (klass, encrypt ? "Protect" : "Unprotect", 2);
646 params [1] = scope; /* MemoryProtectionScope.SameProcess */
648 mono_runtime_invoke_checked (method, NULL, params, error);