/*
* 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)
*/
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:
+ *
+ * Find the first managed caller that is either:
+ * (a) located outside the platform code assemblies; or
+ * (b) not related to reflection and delegates
+ *
+ * Returns TRUE to stop the stackwalk, FALSE to continue to the next frame.
+ */
static gboolean
get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
{
return TRUE;
}
-/* walk to the first managed method outside:
- * - System.Reflection* namespaces
- * - System.[MulticastDelegate]Delegate or Activator type
- * - platform code
- * and return its value, since CoreCLR checks needs to be done on this "real" caller.
+/*
+ * get_reflection_caller:
+ *
+ * Walk to the first managed method outside:
+ * - System.Reflection* namespaces
+ * - System.[MulticastDelegate]Delegate or Activator type
+ * - platform code
+ * and return a pointer to its MonoMethod.
+ *
+ * This is required since CoreCLR checks needs to be done on this "real" caller.
*/
static MonoMethod*
get_reflection_caller (void)
return mono_method_can_access_method_full (caller, callee, klass);
}
+/*
+ * mono_security_core_clr_ensure_reflection_access_field:
+ *
+ * Ensure that the specified field can be used with reflection since
+ * Transparent code cannot access to Critical fields and can only use
+ * them if they are visible from it's point of view.
+ *
+ * A FieldAccessException is thrown if the field is cannot be accessed.
+ */
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 ());
}
+/*
+ * mono_security_core_clr_ensure_reflection_access_method:
+ *
+ * Ensure that the specified method can be used with reflection since
+ * Transparent code cannot call Critical methods and can only call them
+ * if they are visible from it's point of view.
+ *
+ * A MethodAccessException is thrown if the field is cannot be accessed.
+ */
void
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"))
+ return TRUE;
+ } else if (strcmp (method->klass->name, "EvenInfo") == 0) {
+ if ((strcmp (method->name, "AddEventFrame") == 0) || strcmp (method->name, "StaticAddEventAdapterFrame"))
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
/*
* mono_security_core_clr_ensure_delegate_creation:
*
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;
return NULL;
}
+/*
+ * mono_security_core_clr_level_from_cinfo:
+ *
+ * Return the MonoSecurityCoreCLRLevel that match the attribute located
+ * in the specified custom attributes. If no attribute is present it
+ * defaults to MONO_SECURITY_CORE_CLR_TRANSPARENT, which is the default
+ * level for all code under the CoreCLR.
+ */
static MonoSecurityCoreCLRLevel
mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo *cinfo, MonoImage *image)
{
return level;
}
+/*
+ * mono_security_core_clr_class_level:
+ *
+ * Return the MonoSecurityCoreCLRLevel for the specified class.
+ */
MonoSecurityCoreCLRLevel
mono_security_core_clr_class_level (MonoClass *class)
{
return level;
}
+/*
+ * mono_security_core_clr_method_level:
+ *
+ * Return the MonoSecurityCoreCLRLevel for the specified method.
+ * If with_class_level is TRUE then the type (class) will also be
+ * checked, otherwise this will only report the information about
+ * the method itself.
+ */
MonoSecurityCoreCLRLevel
mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
{
return level;
}
+/*
+ * 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)
{
- const char *prefix = mono_assembly_getrootdir ();
- int prefix_len = strlen (prefix);
- static const char subprefix[] = "/mono/2.1/";
- int subprefix_len = strlen (subprefix);
+ return image->core_clr_platform_code;
+}
- if (!image->name)
- return FALSE;
- if (strncmp (prefix, image->name, prefix_len) != 0)
- return FALSE;
- if (strncmp (subprefix, image->name + prefix_len, subprefix_len) != 0)
- return FALSE;
- if (strchr (image->name + prefix_len + subprefix_len, '/'))
- return FALSE;
- return TRUE;
+/*
+ * default_platform_check:
+ *
+ * Default platform check. Always return FALSE.
+ */
+static gboolean
+default_platform_check (const char *image_name)
+{
+ return FALSE;
+}
+
+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_enable_core_clr:
+ *
+ * Enable the verifier and the CoreCLR security model
+ */
void
mono_security_enable_core_clr ()
{
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)
+{
+ platform_callback = callback;
+}
+