2009-01-09 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / security.c
index 6601b5f4080b3fcf04c1d64b8c6248586a38f77d..f621f319eb881c2b96bb52cc20bd50bf615e1e16 100644 (file)
@@ -11,6 +11,7 @@
 #include <config.h>
 #endif
 
+#include <mono/metadata/assembly.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/image.h>
 #include <mono/metadata/exception.h>
@@ -114,6 +115,19 @@ GetSidName (gunichar2 *server, PSID sid, gint32 *size)
 
 #else /* not PLATFORM_WIN32 */
 
+#define MONO_SYSCONF_DEFAULT_SIZE      ((size_t) 1024)
+
+/*
+ * Ensure we always get a valid (usable) value from sysconf.
+ * In case of error, we return the default value.
+ */
+static size_t mono_sysconf (int name)
+{
+       size_t size = (size_t) sysconf (name);
+       /* default value */
+       return (size == -1) ? MONO_SYSCONF_DEFAULT_SIZE : size;
+}
+
 
 static gchar*
 GetTokenName (uid_t uid)
@@ -131,9 +145,9 @@ GetTokenName (uid_t uid)
 
 #ifdef HAVE_GETPWUID_R
 #ifdef _SC_GETPW_R_SIZE_MAX
-       fbufsize = (size_t) sysconf (_SC_GETPW_R_SIZE_MAX);
+       fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
 #else
-       fbufsize = (size_t) 1024;
+       fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
 #endif
        fbuf = g_malloc0 (fbufsize);
        retval = getpwuid_r (uid, &pwd, fbuf, fbufsize, &p);
@@ -197,9 +211,9 @@ IsDefaultGroup (uid_t user, gid_t group)
 
 #ifdef HAVE_GETPWUID_R
 #ifdef _SC_GETPW_R_SIZE_MAX
-       fbufsize = (size_t) sysconf (_SC_GETPW_R_SIZE_MAX);
+       fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
 #else
-       fbufsize = (size_t) 1024;
+       fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
 #endif
 
        fbuf = g_malloc0 (fbufsize);
@@ -277,7 +291,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetCurrentToken (void)
                OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token);
        }
 #else
-       token = (gpointer) geteuid ();
+       token = GINT_TO_POINTER (geteuid ());
 #endif
        return token;
 }
@@ -302,7 +316,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetTokenName (gpointer token
                g_free (tu);
        }
 #else 
-       gchar *uname = GetTokenName ((uid_t) token);
+       gchar *uname = GetTokenName ((uid_t) GPOINTER_TO_INT (token));
 
        MONO_ARCH_SAVE_REGS;
 
@@ -359,9 +373,9 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoString *us
 
 #ifdef HAVE_GETPWNAM_R
 #ifdef _SC_GETPW_R_SIZE_MAX
-       fbufsize = (size_t) sysconf (_SC_GETPW_R_SIZE_MAX);
+       fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
 #else
-       fbufsize = (size_t) 1024;
+       fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
 #endif
 
        fbuf = g_malloc0 (fbufsize);
@@ -374,7 +388,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoString *us
 #endif
 
        if (result) {
-               token = (gpointer) p->pw_uid;
+               token = GINT_TO_POINTER (p->pw_uid);
        }
 
 #ifdef HAVE_GETPWNAM_R
@@ -414,7 +428,7 @@ ves_icall_System_Security_Principal_WindowsIdentity_GetRoles (gpointer token)
 
                                if (uniname) {
                                        MonoString *str = mono_string_new_utf16 (domain, uniname, size);
-                                       mono_array_set (array, MonoString *, i, str);
+                                       mono_array_setref (array, i, str);
                                        g_free (uniname);
                                }
                        }
@@ -515,21 +529,21 @@ ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupId (gpointer
 
 #ifdef HAVE_GETGRGID_R
 #ifdef _SC_GETGR_R_SIZE_MAX
-       fbufsize = (size_t) sysconf (_SC_GETGR_R_SIZE_MAX);
+       fbufsize = mono_sysconf (_SC_GETGR_R_SIZE_MAX);
 #else
-       fbufsize = (size_t) 1024;
+       fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
 #endif
        fbuf = g_malloc0 (fbufsize);
-       retval = getgrgid_r ((gid_t) group, &grp, fbuf, fbufsize, &g);
+       retval = getgrgid_r ((gid_t) GPOINTER_TO_INT (group), &grp, fbuf, fbufsize, &g);
        result = ((retval == 0) && (g == &grp));
 #else
        /* default to non thread-safe but posix compliant function */
-       g = getgrgid ((gid_t) group);
+       g = getgrgid ((gid_t) GPOINTER_TO_INT (group));
        result = (g != NULL);
 #endif
 
        if (result) {
-               result = IsMemberOf ((uid_t) user, g);
+               result = IsMemberOf ((uid_t) GPOINTER_TO_INT (user), g);
        }
 
 #ifdef HAVE_GETGRGID_R
@@ -567,9 +581,9 @@ ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupName (gpoint
                gchar *fbuf;
                gint32 retval;
 #ifdef _SC_GETGR_R_SIZE_MAX
-               size_t fbufsize = (size_t) sysconf (_SC_GETGR_R_SIZE_MAX);
+               size_t fbufsize = mono_sysconf (_SC_GETGR_R_SIZE_MAX);
 #else
-               size_t fbufsize = (size_t) 1024;
+               size_t fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
 #endif
                fbuf = g_malloc0 (fbufsize);
                retval = getgrnam_r (utf8_groupname, &grp, fbuf, fbufsize, &g);
@@ -581,7 +595,7 @@ ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupName (gpoint
 #endif
 
                if (result) {
-                       result = IsMemberOf ((uid_t) user, g);
+                       result = IsMemberOf ((uid_t) GPOINTER_TO_INT (user), g);
                }
 
 #ifdef HAVE_GETGRNAM_R
@@ -951,3 +965,45 @@ ves_icall_System_Security_Policy_Evidence_IsAuthenticodePresent (MonoReflectionA
        }
        return FALSE;
 }
+
+
+/* System.Security.SecureString related internal calls */
+
+static MonoImage *system_security_assembly = NULL;
+
+void
+ves_icall_System_Security_SecureString_DecryptInternal (MonoArray *data, MonoObject *scope)
+{
+       invoke_protected_memory_method (data, scope, FALSE);
+}
+void
+ves_icall_System_Security_SecureString_EncryptInternal (MonoArray* data, MonoObject *scope)
+{
+       invoke_protected_memory_method (data, scope, TRUE);
+}
+
+void invoke_protected_memory_method (MonoArray *data, MonoObject *scope, gboolean encrypt)
+{
+       MonoClass *klass;
+       MonoMethod *method;
+       void *params [2];
+
+       MONO_ARCH_SAVE_REGS;
+
+       if (system_security_assembly == NULL) {
+               system_security_assembly = mono_image_loaded ("System.Security");
+               if (!system_security_assembly) {
+                       MonoAssembly *sa = mono_assembly_open ("System.Security.dll", NULL);
+                       if (!sa)
+                               g_assert_not_reached ();
+                       system_security_assembly = mono_assembly_get_image (sa);
+               }
+       }
+
+       klass = mono_class_from_name (system_security_assembly,
+                                                                 "System.Security.Cryptography", "ProtectedMemory");
+       method = mono_class_get_method_from_name (klass, encrypt ? "Protect" : "Unprotect", 2);
+       params [0] = data;
+       params [1] = scope; /* MemoryProtectionScope.SameProcess */
+       mono_runtime_invoke (method, NULL, params, NULL);
+}