[sgen] Document descriptor types in comment.
[mono.git] / mono / metadata / security-core-clr.c
index 769e4af79e4ce8e522244721f16a703fb4907dec..1be63ffc2240dbfcce52661320765620449a384d 100644 (file)
 
 gboolean mono_security_core_clr_test = FALSE;
 
+static MonoSecurityCoreCLROptions security_core_clr_options = MONO_SECURITY_CORE_CLR_OPTIONS_DEFAULT;
+
+/**
+ * mono_security_core_clr_set_options:
+ * @options: the new options for the coreclr system to use
+ *
+ * By default, the CoreCLRs security model forbids execution trough reflection of methods not visible from the calling code.
+ * Even if the method being called is not in a platform assembly. For non moonlight CoreCLR users this restriction does not
+ * make a lot of sense, since the author could have just changed the non platform assembly to allow the method to be called.
+ * This function allows specific relaxations from the default behaviour to be set.
+ *
+ * Use MONO_SECURITY_CORE_CLR_OPTIONS_DEFAULT for the default coreclr coreclr behaviour as used in Moonlight.
+ *
+ * Use MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION to allow transparent code to execute methods and access 
+ * fields that are not in platformcode, even if those methods and fields are private or otherwise not visible to the calling code.
+ *
+ * Use MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_DELEGATE to allow delegates to be created that point at methods that are not in
+ * platformcode even if those methods and fields are private or otherwise not visible to the calling code.
+ *
+ */
+void
+mono_security_core_clr_set_options (MonoSecurityCoreCLROptions options) {
+       security_core_clr_options = options;
+}
+
+/**
+ * mono_security_core_clr_get_options:
+ *
+ * Retrieves the current options used by the coreclr system.
+ */
+
+MonoSecurityCoreCLROptions
+mono_security_core_clr_get_options ()
+{
+       return security_core_clr_options;
+}
+
+/*
+ * default_platform_check:
+ *
+ *     Default platform check. Always TRUE for current corlib (minimum 
+ *     trust-able subset) otherwise return FALSE. Any real CoreCLR host
+ *     should provide its own callback to define platform code (i.e.
+ *     this default is meant for test only).
+ */
+static gboolean
+default_platform_check (const char *image_name)
+{
+       if (mono_defaults.corlib) {
+               return (strcmp (mono_defaults.corlib->name, image_name) == 0);
+       } else {
+               /* this can get called even before we load corlib (e.g. the EXE itself) */
+               const char *corlib = "mscorlib.dll";
+               int ilen = strlen (image_name);
+               int clen = strlen (corlib);
+               return ((ilen >= clen) && (strcmp ("mscorlib.dll", image_name + ilen - clen) == 0));
+       }
+}
+
+static MonoCoreClrPlatformCB platform_callback = default_platform_check;
+
+/*
+ * mono_security_core_clr_determine_platform_image:
+ *
+ *  Call the supplied callback (from mono_security_set_core_clr_platform_callback) 
+ *  to determine if this image represents platform code.
+ */
+gboolean
+mono_security_core_clr_determine_platform_image (MonoImage *image)
+{
+       return platform_callback (image->name);
+}
+
+/*
+ * mono_security_set_core_clr_platform_callback:
+ *
+ *  Set the callback function that will be used to determine if an image
+ *  is part, or not, of the platform code.
+ */
+void
+mono_security_set_core_clr_platform_callback (MonoCoreClrPlatformCB callback)
+{
+       platform_callback = callback;
+}
+
+/*
+ * mono_security_core_clr_is_platform_image:
+ *
+ *   Return the (cached) boolean value indicating if this image represent platform code
+ */
+gboolean
+mono_security_core_clr_is_platform_image (MonoImage *image)
+{
+       return image->core_clr_platform_code;
+}
+
+/* Note: The above functions are outside this guard so that the public API isn't affected. */
+
+#ifndef DISABLE_SECURITY
+
 static MonoClass*
 security_critical_attribute (void)
 {
@@ -148,10 +248,11 @@ get_default_ctor (MonoClass *klass)
  *     Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
  *
  *     Furthermore a class MUST have a default constructor if its base 
- *     class has a non-transparent default constructor. The same 
- *     inheritance rule applies to both default constructors.
+ *     class has a non-transparent, public or protected, default constructor. 
+ *     The same inheritance rule applies to both default constructors.
  *
  *     Reference: message from a SecurityException in SL4RC
+ *     Reference: fxcop CA2132 rule
  */
 void
 mono_security_core_clr_check_inheritance (MonoClass *class)
@@ -170,12 +271,15 @@ mono_security_core_clr_check_inheritance (MonoClass *class)
                        "Inheritance failure for type %s. Parent class %s is more restricted.",
                        class);
        } else {
-               class_level = mono_security_core_clr_method_level (get_default_ctor (class), FALSE);
-               parent_level = mono_security_core_clr_method_level (get_default_ctor (parent), FALSE);
-               if (class_level < parent_level) {
-                       set_type_load_exception_type (
-                               "Inheritance failure for type %s. Default constructor security mismatch with %s.",
-                               class);
+               MonoMethod *parent_ctor = get_default_ctor (parent);
+               if (parent_ctor && ((parent_ctor->flags & METHOD_ATTRIBUTE_PUBLIC) != 0)) {
+                       class_level = mono_security_core_clr_method_level (get_default_ctor (class), FALSE);
+                       parent_level = mono_security_core_clr_method_level (parent_ctor, FALSE);
+                       if (class_level < parent_level) {
+                               set_type_load_exception_type (
+                                       "Inheritance failure for type %s. Default constructor security mismatch with %s.",
+                                       class);
+                       }
                }
        }
 }
@@ -404,6 +508,7 @@ mono_security_core_clr_require_elevated_permissions (void)
        return (mono_security_core_clr_method_level (cookie.caller, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT);
 }
 
+
 /*
  * check_field_access:
  *
@@ -538,6 +643,11 @@ mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
        if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
                return;
 
+       if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION) {
+               if (!mono_security_core_clr_is_platform_image (mono_field_get_parent(field)->image))
+                       return;
+       }
+
        /* Transparent code cannot [get|set]value on Critical fields */
        if (mono_security_core_clr_class_level (mono_field_get_parent (field)) == MONO_SECURITY_CORE_CLR_CRITICAL) {
                mono_raise_exception (get_field_access_exception (
@@ -570,6 +680,11 @@ mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
        if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
                return;
 
+       if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION) {
+               if (!mono_security_core_clr_is_platform_image (method->klass->image))
+                       return;
+       }
+
        /* Transparent code cannot invoke, even using reflection, Critical code */
        if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
                mono_raise_exception (get_method_access_exception (
@@ -652,7 +767,12 @@ mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean th
                        "Transparent method %s cannot create a delegate on Critical method %s.", 
                        caller, method));
        }
-       
+
+       if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_DELEGATE) {
+               if (!mono_security_core_clr_is_platform_image (method->klass->image))
+                       return TRUE;
+       }
+
        /* also it cannot create the delegate on a method that is not visible from it's (caller) point of view */
        if (!check_method_access (caller, method)) {
                mono_raise_exception (get_method_access_exception (
@@ -875,73 +995,90 @@ mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_lev
 }
 
 /*
- * mono_security_core_clr_is_platform_image:
+ * mono_security_enable_core_clr:
  *
- *   Return the (cached) boolean value indicating if this image represent platform code
+ *   Enable the verifier and the CoreCLR security model
  */
-gboolean
-mono_security_core_clr_is_platform_image (MonoImage *image)
+void
+mono_security_enable_core_clr ()
 {
-       return image->core_clr_platform_code;
+       mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
+       mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
 }
 
-/*
- * default_platform_check:
- *
- *     Default platform check. Always TRUE for current corlib (minimum 
- *     trust-able subset) otherwise return FALSE. Any real CoreCLR host
- *     should provide its own callback to define platform code (i.e.
- *     this default is meant for test only).
- */
-static gboolean
-default_platform_check (const char *image_name)
+#else
+
+void
+mono_security_core_clr_check_inheritance (MonoClass *class)
 {
-       if (mono_defaults.corlib) {
-               return (strcmp (mono_defaults.corlib->name, image_name) == 0);
-       } else {
-               /* this can get called even before we load corlib (e.g. the EXE itself) */
-               const char *corlib = "mscorlib.dll";
-               int ilen = strlen (image_name);
-               int clen = strlen (corlib);
-               return ((ilen >= clen) && (strcmp ("mscorlib.dll", image_name + ilen - clen) == 0));
-       }
 }
 
-static MonoCoreClrPlatformCB platform_callback = default_platform_check;
+void
+mono_security_core_clr_check_override (MonoClass *class, MonoMethod *override, MonoMethod *base)
+{
+}
 
-/*
- * mono_security_core_clr_determine_platform_image:
- *
- *     Call the supplied callback (from mono_security_set_core_clr_platform_callback) 
- *     to determine if this image represents platform code.
- */
 gboolean
-mono_security_core_clr_determine_platform_image (MonoImage *image)
+mono_security_core_clr_require_elevated_permissions (void)
 {
-       return platform_callback (image->name);
+       return FALSE;
 }
 
-/*
- * mono_security_enable_core_clr:
- *
- *   Enable the verifier and the CoreCLR security model
- */
 void
-mono_security_enable_core_clr ()
+mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
 {
-       mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
-       mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
 }
 
-/*
- * mono_security_set_core_clr_platform_callback:
- *
- *     Set the callback function that will be used to determine if an image
- *     is part, or not, of the platform code.
- */
 void
-mono_security_set_core_clr_platform_callback (MonoCoreClrPlatformCB callback)
+mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
+{
+}
+
+gboolean
+mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean throwOnBindFailure)
+{
+       return TRUE;
+}
+
+MonoException*
+mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
+{
+       return NULL;
+}
+
+gboolean
+mono_security_core_clr_can_access_internals (MonoImage *accessing, MonoImage* accessed)
+{
+       return TRUE;
+}
+
+MonoException*
+mono_security_core_clr_is_field_access_allowed (MonoMethod *caller, MonoClassField *field)
+{
+       return NULL;
+}
+
+MonoException*
+mono_security_core_clr_is_call_allowed (MonoMethod *caller, MonoMethod *callee)
+{
+       return NULL;
+}
+
+MonoSecurityCoreCLRLevel
+mono_security_core_clr_class_level (MonoClass *class)
+{
+       return MONO_SECURITY_CORE_CLR_TRANSPARENT;
+}
+
+MonoSecurityCoreCLRLevel
+mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
+{
+       return MONO_SECURITY_CORE_CLR_TRANSPARENT;
+}
+
+void
+mono_security_enable_core_clr ()
 {
-       platform_callback = callback;
 }
 
+#endif /* DISABLE_SECURITY */