2009-07-26 Miguel de Icaza <miguel@novell.com>
[mono.git] / mono / metadata / security-core-clr.c
index 192427d78efd23e1817a941a973e51d324880599..59b5a7c11561cc910ee121b211eb9879c83703c1 100644 (file)
@@ -152,12 +152,17 @@ get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean
        /* calls from System.Delegate are also possible and allowed */
        if (strcmp (ns, "System") == 0) {
                const char *kname = m->klass->name;
-               if ((*kname == 'D') && (strcmp (kname, "Delegate") == 0))
-                       return FALSE;
-               if ((*kname == 'M') && (strcmp (kname, "MulticastDelegate")) == 0)
-                       return FALSE;
                if ((*kname == 'A') && (strcmp (kname, "Activator") == 0))
                        return FALSE;
+
+               // the security check on the delegate is made at creation time, not at invoke time
+               if (((*kname == 'D') && (strcmp (kname, "Delegate") == 0)) || 
+                       ((*kname == 'M') && (strcmp (kname, "MulticastDelegate")) == 0)) {
+
+                       // if we're invoking then we can stop our stack walk
+                       if (strcmp (m->name, "DynamicInvoke") != 0)
+                               return FALSE;
+               }
        }
 
        if (m == *dest) {
@@ -183,8 +188,10 @@ get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean
 static MonoMethod*
 get_reflection_caller (void)
 {
-       MonoMethod *m = mono_method_get_last_managed ();
+       MonoMethod *m = NULL;
        mono_stack_walk_no_il (get_caller_no_reflection_related, &m);
+       if (!m)
+               g_warning ("could not find a caller outside reflection");
        return m;
 }
 
@@ -196,8 +203,12 @@ get_reflection_caller (void)
 static gboolean
 check_field_access (MonoMethod *caller, MonoClassField *field)
 {
-       MonoClass *klass = (mono_field_get_flags (field) & FIELD_ATTRIBUTE_STATIC) ? NULL : mono_field_get_parent (field);
-       return mono_method_can_access_field_full (caller, field, klass);
+       /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
+       if (caller) {
+               MonoClass *klass = (mono_field_get_flags (field) & FIELD_ATTRIBUTE_STATIC) ? NULL : mono_field_get_parent (field);
+               return mono_method_can_access_field_full (caller, field, klass);
+       }
+       return FALSE;
 }
 
 /*
@@ -208,8 +219,12 @@ check_field_access (MonoMethod *caller, MonoClassField *field)
 static gboolean
 check_method_access (MonoMethod *caller, MonoMethod *callee)
 {
-       MonoClass *klass = (callee->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : callee->klass;
-       return mono_method_can_access_method_full (caller, callee, klass);
+       /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
+       if (caller) {
+               MonoClass *klass = (callee->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : callee->klass;
+               return mono_method_can_access_method_full (caller, callee, klass);
+       }
+       return FALSE;
 }
 
 /*
@@ -288,7 +303,7 @@ can_avoid_corlib_reflection_delegate_optimization (MonoMethod *method)
        if (strcmp (method->klass->name, "MonoProperty") == 0) {
                if ((strcmp (method->name, "GetterAdapterFrame") == 0) || strcmp (method->name, "StaticGetterAdapterFrame") == 0)
                        return TRUE;
-       } else if (strcmp (method->klass->name, "EvenInfo") == 0) {
+       } else if (strcmp (method->klass->name, "EventInfo") == 0) {
                if ((strcmp (method->name, "AddEventFrame") == 0) || strcmp (method->name, "StaticAddEventAdapterFrame") == 0)
                        return TRUE;
        }
@@ -394,32 +409,45 @@ mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo *cinfo, MonoImage *i
 }
 
 /*
- * mono_security_core_clr_class_level:
+ * mono_security_core_clr_class_level_no_platform_check:
  *
- *     Return the MonoSecurityCoreCLRLevel for the specified class.
+ *     Return the MonoSecurityCoreCLRLevel for the specified class, without 
+ *     checking for platform code. This help us avoid multiple redundant 
+ *     checks, e.g.
+ *     - a check for the method and one for the class;
+ *     - a check for the class and outer class(es) ...
  */
-MonoSecurityCoreCLRLevel
-mono_security_core_clr_class_level (MonoClass *class)
+static MonoSecurityCoreCLRLevel
+mono_security_core_clr_class_level_no_platform_check (MonoClass *class)
 {
-       MonoCustomAttrInfo *cinfo;
        MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
-
-       /* non-platform code is always Transparent - whatever the attributes says */
-       if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (class->image))
-               return level;
-
-       cinfo = mono_custom_attrs_from_class (class);
+       MonoCustomAttrInfo *cinfo = mono_custom_attrs_from_class (class);
        if (cinfo) {
                level = mono_security_core_clr_level_from_cinfo (cinfo, class->image);
                mono_custom_attrs_free (cinfo);
        }
 
        if (level == MONO_SECURITY_CORE_CLR_TRANSPARENT && class->nested_in)
-               level = mono_security_core_clr_class_level (class->nested_in);
+               level = mono_security_core_clr_class_level_no_platform_check (class->nested_in);
 
        return level;
 }
 
+/*
+ * mono_security_core_clr_class_level:
+ *
+ *     Return the MonoSecurityCoreCLRLevel for the specified class.
+ */
+MonoSecurityCoreCLRLevel
+mono_security_core_clr_class_level (MonoClass *class)
+{
+       /* non-platform code is always Transparent - whatever the attributes says */
+       if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (class->image))
+               return MONO_SECURITY_CORE_CLR_TRANSPARENT;
+
+       return mono_security_core_clr_class_level_no_platform_check (class);
+}
+
 /*
  * mono_security_core_clr_method_level:
  *
@@ -434,6 +462,10 @@ mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_lev
        MonoCustomAttrInfo *cinfo;
        MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
 
+       /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
+       if (!method)
+               return level;
+
        /* non-platform code is always Transparent - whatever the attributes says */
        if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (method->klass->image))
                return level;