implement a relaxed mode of CoreCLR for coreclr users that do not require to be like...
authorLucas Meijer <lucas@unity3d.com>
Wed, 9 Mar 2011 09:04:00 +0000 (10:04 +0100)
committerSebastien Pouliot <sebastien@ximian.com>
Thu, 31 Mar 2011 14:37:46 +0000 (10:37 -0400)
mono/metadata/security-core-clr.c
mono/metadata/security-core-clr.h

index dd70925072d947c37e5c7b1cae76133237b4ea83..82068daacf115074d67f0fd3c4ab0c96de5b48ae 100644 (file)
@@ -408,6 +408,29 @@ mono_security_core_clr_require_elevated_permissions (void)
        return (mono_security_core_clr_method_level (cookie.caller, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT);
 }
 
+
+static MonoSecurityCoreCLRBehaviour security_core_clr_behaviour = MONO_SECURITY_CORE_CLR_BEHAVIOUR_MOONLIGHT;
+
+/*
+ * mono_security_core_clr_set_behaviour
+ *
+ *      Moonlight's 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. 
+ */
+
+void 
+mono_security_core_clr_set_behaviour (MonoSecurityCoreCLRBehaviour behaviour) {
+       security_core_clr_behaviour = behaviour;
+}
+
+MonoSecurityCoreCLRBehaviour
+mono_security_core_clr_get_behaviour ()
+{
+       return security_core_clr_behaviour;
+}
+
+
 /*
  * check_field_access:
  *
@@ -542,6 +565,12 @@ 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_behaviour() == MONO_SECURITY_CORE_CLR_BEHAVIOUR_RELAXED)
+       {
+               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 (
@@ -574,6 +603,12 @@ 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_behaviour() == MONO_SECURITY_CORE_CLR_BEHAVIOUR_RELAXED)
+       {
+               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 (
@@ -657,6 +692,12 @@ mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean th
                        caller, method));
        }
        
+       if (mono_security_core_clr_get_behaviour() == MONO_SECURITY_CORE_CLR_BEHAVIOUR_RELAXED)
+       {
+               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 (
index 323aeb28c0d397bd8063b79f2b1d8f30417ceb40..281a7dce7c71cfaa2310f2f80f9b2cd2c7fe3f6b 100644 (file)
@@ -20,6 +20,11 @@ typedef enum {
        MONO_SECURITY_CORE_CLR_CRITICAL
 } MonoSecurityCoreCLRLevel;
 
+typedef enum {
+       MONO_SECURITY_CORE_CLR_BEHAVIOUR_MOONLIGHT = 0,
+       MONO_SECURITY_CORE_CLR_BEHAVIOUR_RELAXED
+} MonoSecurityCoreCLRBehaviour;
+
 extern gboolean mono_security_core_clr_test;
 
 extern void mono_security_core_clr_check_inheritance (MonoClass *class) MONO_INTERNAL;
@@ -43,4 +48,7 @@ extern gboolean mono_security_core_clr_determine_platform_image (MonoImage *imag
 
 extern gboolean mono_security_core_clr_require_elevated_permissions (void);
 
+extern void mono_security_core_clr_set_behaviour (MonoSecurityCoreCLRBehaviour behaviour);
+extern MonoSecurityCoreCLRBehaviour mono_security_core_clr_get_behaviour (void);
+
 #endif /* _MONO_METADATA_SECURITY_CORE_CLR_H_ */