2009-11-18 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mono / metadata / security-core-clr.c
index f2200c491e94bba221caef40aedef026b8578d5b..f30d5f184337ca8b147b433a3e69a50f6a986dbd 100644 (file)
@@ -1,8 +1,9 @@
 /*
  * security-core-clr.c: CoreCLR security
  *
- * Author:
+ * Authors:
  *     Mark Probst <mark.probst@gmail.com>
+ *     Sebastien Pouliot  <sebastien@ximian.com>
  *
  * Copyright 2007-2009 Novell, Inc (http://www.novell.com)
  */
@@ -45,6 +46,66 @@ security_safe_critical_attribute (void)
        return class;
 }
 
+/*
+ * mono_security_core_clr_check_inheritance:
+ *
+ *     Determine if the specified class can inherit from its parent using 
+ *     the CoreCLR inheritance rules.
+ *
+ *     Base Type       Allow Derived Type
+ *     ------------    ------------------
+ *     Transparent     Transparent, SafeCritical, Critical
+ *     SafeCritical    SafeCritical, Critical
+ *     Critical        Critical
+ *
+ *     Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
+ */
+void
+mono_security_core_clr_check_inheritance (MonoClass *class)
+{
+       MonoSecurityCoreCLRLevel class_level, parent_level;
+       MonoClass *parent = class->parent;
+
+       if (!parent)
+               return;
+
+       class_level = mono_security_core_clr_class_level (class);
+       parent_level = mono_security_core_clr_class_level (parent);
+
+       if (class_level < parent_level)
+               mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
+}
+
+/*
+ * mono_security_core_clr_check_override:
+ *
+ *     Determine if the specified override can "legally" override the 
+ *     specified base method using the CoreCLR inheritance rules.
+ *
+ *     Base (virtual/interface)        Allowed override
+ *     ------------------------        -------------------------
+ *     Transparent                     Transparent, SafeCritical
+ *     SafeCritical                    Transparent, SafeCritical
+ *     Critical                        Critical
+ *
+ *     Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
+ */
+void
+mono_security_core_clr_check_override (MonoClass *class, MonoMethod *override, MonoMethod *base)
+{
+       MonoSecurityCoreCLRLevel base_level = mono_security_core_clr_method_level (base, FALSE);
+       MonoSecurityCoreCLRLevel override_level = mono_security_core_clr_method_level (override, FALSE);
+       /* if the base method is decorated with [SecurityCritical] then the overrided method MUST be too */
+       if (base_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
+               if (override_level != MONO_SECURITY_CORE_CLR_CRITICAL)
+                       mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
+       } else {
+               /* base is [SecuritySafeCritical] or [SecurityTransparent], override MUST NOT be [SecurityCritical] */
+               if (override_level == MONO_SECURITY_CORE_CLR_CRITICAL)
+                       mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
+       }
+}
+
 /*
  * get_caller_no_reflection_related:
  *
@@ -91,12 +152,26 @@ 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;
+
+               /* unlike most Invoke* cases InvokeMember is not inside System.Reflection[.Emit] but is SecuritySafeCritical */
+               if (((*kname == 'T') && (strcmp (kname, "Type") == 0)) || 
+                       ((*kname == 'M') && (strcmp (kname, "MonoType")) == 0)) {
+
+                       /* if calling InvokeMember then we can't stop the stackwalk here and need to look at the caller */
+                       if (strcmp (m->name, "InvokeMember") == 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) {
@@ -122,8 +197,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;
 }
 
@@ -135,8 +212,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;
 }
 
 /*
@@ -147,8 +228,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;
 }
 
 /*
@@ -163,18 +248,17 @@ check_method_access (MonoMethod *caller, MonoMethod *callee)
 void
 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
 {
-       MonoClass *klass = mono_field_get_parent (field);
-
-       /* under CoreCLR you cannot use the value (get/set) of the reflected field: */
        MonoMethod *caller = get_reflection_caller ();
+       /* CoreCLR restrictions applies to Transparent code/caller */
+       if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
+               return;
 
-       /* (a) of a Critical type when called from a Transparent caller */
-       if (mono_security_core_clr_class_level (klass) == MONO_SECURITY_CORE_CLR_CRITICAL) {
-               if (mono_security_core_clr_method_level (caller, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT)
-                       mono_raise_exception (mono_get_exception_field_access ());
-       }
-       /* (b) that are not accessible from the caller pov */
-       if (!mono_method_can_access_field_full (caller, field, klass))
+       /* 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 (mono_get_exception_field_access ());
+
+       /* also it cannot access a fields that is not visible from it's (caller) point of view */
+       if (!check_field_access (caller, field))
                mono_raise_exception (mono_get_exception_field_access ());
 }
 
@@ -200,17 +284,49 @@ mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
                mono_raise_exception (mono_get_exception_method_access ());
 
        /* also it cannot invoke a method that is not visible from it's (caller) point of view */
-       if (!mono_method_can_access_method_full (caller, method, (method->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : method->klass))
+       if (!check_method_access (caller, method))
                mono_raise_exception (mono_get_exception_method_access ());
 }
 
+/*
+ * can_avoid_corlib_reflection_delegate_optimization:
+ *
+ *     Mono's mscorlib use delegates to optimize PropertyInfo and EventInfo
+ *     reflection calls. This requires either a bunch of additional, and not
+ *     really required, [SecuritySafeCritical] in the class libraries or 
+ *     (like this) a way to skip them. As a bonus we also avoid the stack
+ *     walk to find the caller.
+ *
+ *     Return TRUE if we can skip this "internal" delegate creation, FALSE
+ *     otherwise.
+ */
+static gboolean
+can_avoid_corlib_reflection_delegate_optimization (MonoMethod *method)
+{
+       if (!mono_security_core_clr_is_platform_image (method->klass->image))
+               return FALSE;
+
+       if (strcmp (method->klass->name_space, "System.Reflection") != 0)
+               return FALSE;
+
+       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, "EventInfo") == 0) {
+               if ((strcmp (method->name, "AddEventFrame") == 0) || strcmp (method->name, "StaticAddEventAdapterFrame") == 0)
+                       return TRUE;
+       }
+
+       return FALSE;
+}
+
 /*
  * mono_security_core_clr_ensure_delegate_creation:
  *
  *     Return TRUE if a delegate can be created on the specified method. 
  *     CoreCLR also affect the binding, so throwOnBindFailure must be 
  *     FALSE to let this function return (FALSE) normally, otherwise (if
- *     throwOnBindFailure is TRUE) itwill throw an ArgumentException.
+ *     throwOnBindFailure is TRUE) it will throw an ArgumentException.
  *
  *     A MethodAccessException is thrown if the specified method is not
  *     visible from the caller point of view.
@@ -218,8 +334,13 @@ mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
 gboolean
 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean throwOnBindFailure)
 {
+       MonoMethod *caller;
+
        /* note: mscorlib creates delegates to avoid reflection (optimization), we ignore those cases */
-       MonoMethod *caller = get_reflection_caller ();
+       if (can_avoid_corlib_reflection_delegate_optimization (method))
+               return TRUE;
+
+       caller = get_reflection_caller ();
        /* if the "real" caller is not Transparent then it do can anything */
        if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
                return TRUE;
@@ -275,6 +396,31 @@ mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, Mono
        return NULL;
 }
 
+/*
+ * mono_security_core_clr_can_access_internals
+ *
+ *     Check if we allow [InternalsVisibleTo] to work between two images.
+ */
+gboolean
+mono_security_core_clr_can_access_internals (MonoImage *accessing, MonoImage* accessed)
+{
+       /* are we trying to access internals of a platform assembly ? if not this is acceptable */
+       if (!mono_security_core_clr_is_platform_image (accessed))
+               return TRUE;
+
+       /* we can't let everyone with the right name and public key token access the internals of platform code.
+        * (Silverlight can rely on the strongname signature of the assemblies, but Mono does not verify them)
+        * However platform code is fully trusted so it can access the internals of other platform code assemblies */
+       if (mono_security_core_clr_is_platform_image (accessing))
+               return TRUE;
+
+       /* catch-22: System.Xml needs access to mscorlib's internals (e.g. ArrayList) but is not considered platform code.
+        * Promoting it to platform code would create another issue since (both Mono/Moonlight or MS version of) 
+        * System.Xml.Linq.dll (an SDK, not platform, assembly) needs access to System.Xml.dll internals (either ). 
+        * The solution is to trust, even transparent code, in the plugin directory to access platform code internals */
+       return (strcmp (accessed->assembly->basedir, accessing->assembly->basedir) == 0);
+}
+
 /*
  * mono_security_core_clr_level_from_cinfo:
  *
@@ -297,32 +443,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:
  *
@@ -337,6 +496,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;
@@ -367,12 +530,23 @@ mono_security_core_clr_is_platform_image (MonoImage *image)
 /*
  * default_platform_check:
  *
- *     Default platform check. Always return FALSE.
+ *     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)
 {
-       return FALSE;
+       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;