2 * security-core-clr.c: CoreCLR security
5 * Mark Probst <mark.probst@gmail.com>
7 * Copyright 2007-2009 Novell, Inc (http://www.novell.com)
10 #include <mono/metadata/class-internals.h>
11 #include <mono/metadata/security-manager.h>
12 #include <mono/metadata/assembly.h>
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/verify-internals.h>
15 #include <mono/metadata/object.h>
16 #include <mono/metadata/exception.h>
18 #include "security-core-clr.h"
20 gboolean mono_security_core_clr_test = FALSE;
23 security_critical_attribute (void)
25 static MonoClass *class = NULL;
28 class = mono_class_from_name (mono_defaults.corlib, "System.Security",
29 "SecurityCriticalAttribute");
36 security_safe_critical_attribute (void)
38 static MonoClass *class = NULL;
41 class = mono_class_from_name (mono_defaults.corlib, "System.Security",
42 "SecuritySafeCriticalAttribute");
49 * mono_security_core_clr_check_inheritance:
51 * Determine if the specified class can inherit from its parent using
52 * the CoreCLR inheritance rules.
54 * Base Type Allow Derived Type
55 * ------------ ------------------
56 * Transparent Transparent, SafeCritical, Critical
57 * SafeCritical SafeCritical, Critical
60 * Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
63 mono_security_core_clr_check_inheritance (MonoClass *class)
65 MonoSecurityCoreCLRLevel class_level, parent_level;
66 MonoClass *parent = class->parent;
71 class_level = mono_security_core_clr_class_level (class);
72 parent_level = mono_security_core_clr_class_level (parent);
74 if (class_level < parent_level)
75 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
79 * mono_security_core_clr_check_override:
81 * Determine if the specified override can "legally" override the
82 * specified base method using the CoreCLR inheritance rules.
84 * Base (virtual/interface) Allowed override
85 * ------------------------ -------------------------
86 * Transparent Transparent, SafeCritical
87 * SafeCritical Transparent, SafeCritical
90 * Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
93 mono_security_core_clr_check_override (MonoClass *class, MonoMethod *override, MonoMethod *base)
95 MonoSecurityCoreCLRLevel base_level = mono_security_core_clr_method_level (base, FALSE);
96 MonoSecurityCoreCLRLevel override_level = mono_security_core_clr_method_level (override, FALSE);
97 /* if the base method is decorated with [SecurityCritical] then the overrided method MUST be too */
98 if (base_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
99 if (override_level != MONO_SECURITY_CORE_CLR_CRITICAL)
100 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
102 /* base is [SecuritySafeCritical] or [SecurityTransparent], override MUST NOT be [SecurityCritical] */
103 if (override_level == MONO_SECURITY_CORE_CLR_CRITICAL)
104 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
109 * get_caller_no_reflection_related:
111 * Find the first managed caller that is either:
112 * (a) located outside the platform code assemblies; or
113 * (b) not related to reflection and delegates
115 * Returns TRUE to stop the stackwalk, FALSE to continue to the next frame.
118 get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
120 MonoMethod **dest = data;
123 /* skip unmanaged frames */
127 if (m->wrapper_type != MONO_WRAPPER_NONE)
130 /* quick out (any namespace not starting with an 'S' */
131 ns = m->klass->name_space;
132 if (!ns || (*ns != 'S')) {
137 /* stop if the method is not part of platform code */
138 if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
143 /* any number of calls inside System.Reflection are allowed */
144 if (strcmp (ns, "System.Reflection") == 0)
147 /* any number of calls inside System.Reflection are allowed */
148 if (strcmp (ns, "System.Reflection.Emit") == 0)
151 /* calls from System.Delegate are also possible and allowed */
152 if (strcmp (ns, "System") == 0) {
153 const char *kname = m->klass->name;
154 if ((*kname == 'D') && (strcmp (kname, "Delegate") == 0))
156 if ((*kname == 'M') && (strcmp (kname, "MulticastDelegate")) == 0)
158 if ((*kname == 'A') && (strcmp (kname, "Activator") == 0))
172 * get_reflection_caller:
174 * Walk to the first managed method outside:
175 * - System.Reflection* namespaces
176 * - System.[MulticastDelegate]Delegate or Activator type
178 * and return a pointer to its MonoMethod.
180 * This is required since CoreCLR checks needs to be done on this "real" caller.
183 get_reflection_caller (void)
185 MonoMethod *m = mono_method_get_last_managed ();
186 mono_stack_walk_no_il (get_caller_no_reflection_related, &m);
191 * check_field_access:
193 * Return TRUE if the caller method can access the specified field, FALSE otherwise.
196 check_field_access (MonoMethod *caller, MonoClassField *field)
198 MonoClass *klass = (mono_field_get_flags (field) & FIELD_ATTRIBUTE_STATIC) ? NULL : mono_field_get_parent (field);
199 return mono_method_can_access_field_full (caller, field, klass);
203 * check_method_access:
205 * Return TRUE if the caller method can access the specified callee method, FALSE otherwise.
208 check_method_access (MonoMethod *caller, MonoMethod *callee)
210 MonoClass *klass = (callee->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : callee->klass;
211 return mono_method_can_access_method_full (caller, callee, klass);
215 * mono_security_core_clr_ensure_reflection_access_field:
217 * Ensure that the specified field can be used with reflection since
218 * Transparent code cannot access to Critical fields and can only use
219 * them if they are visible from it's point of view.
221 * A FieldAccessException is thrown if the field is cannot be accessed.
224 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
226 MonoClass *klass = mono_field_get_parent (field);
228 /* under CoreCLR you cannot use the value (get/set) of the reflected field: */
229 MonoMethod *caller = get_reflection_caller ();
231 /* (a) of a Critical type when called from a Transparent caller */
232 if (mono_security_core_clr_class_level (klass) == MONO_SECURITY_CORE_CLR_CRITICAL) {
233 if (mono_security_core_clr_method_level (caller, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT)
234 mono_raise_exception (mono_get_exception_field_access ());
236 /* (b) that are not accessible from the caller pov */
237 if (!mono_method_can_access_field_full (caller, field, klass))
238 mono_raise_exception (mono_get_exception_field_access ());
242 * mono_security_core_clr_ensure_reflection_access_method:
244 * Ensure that the specified method can be used with reflection since
245 * Transparent code cannot call Critical methods and can only call them
246 * if they are visible from it's point of view.
248 * A MethodAccessException is thrown if the field is cannot be accessed.
251 mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
253 MonoMethod *caller = get_reflection_caller ();
254 /* CoreCLR restrictions applies to Transparent code/caller */
255 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
258 /* Transparent code cannot invoke, even using reflection, Critical code */
259 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL)
260 mono_raise_exception (mono_get_exception_method_access ());
262 /* also it cannot invoke a method that is not visible from it's (caller) point of view */
263 if (!mono_method_can_access_method_full (caller, method, (method->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : method->klass))
264 mono_raise_exception (mono_get_exception_method_access ());
268 * mono_security_core_clr_ensure_delegate_creation:
270 * Return TRUE if a delegate can be created on the specified method.
271 * CoreCLR also affect the binding, so throwOnBindFailure must be
272 * FALSE to let this function return (FALSE) normally, otherwise (if
273 * throwOnBindFailure is TRUE) itwill throw an ArgumentException.
275 * A MethodAccessException is thrown if the specified method is not
276 * visible from the caller point of view.
279 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean throwOnBindFailure)
281 /* note: mscorlib creates delegates to avoid reflection (optimization), we ignore those cases */
282 MonoMethod *caller = get_reflection_caller ();
283 /* if the "real" caller is not Transparent then it do can anything */
284 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
287 /* otherwise it (as a Transparent caller) cannot create a delegate on a Critical method... */
288 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
289 /* but this throws only if 'throwOnBindFailure' is TRUE */
290 if (!throwOnBindFailure)
293 mono_raise_exception (mono_get_exception_argument ("method", "Transparent code cannot call Critical code"));
296 /* also it cannot create the delegate on a method that is not visible from it's (caller) point of view */
297 if (!check_method_access (caller, method))
298 mono_raise_exception (mono_get_exception_method_access ());
304 * mono_security_core_clr_ensure_dynamic_method_resolved_object:
306 * Called from mono_reflection_create_dynamic_method (reflection.c) to add some extra checks required for CoreCLR.
307 * Dynamic methods needs to check to see if the objects being used (e.g. methods, fields) comes from platform code
308 * and do an accessibility check in this case. Otherwise (i.e. user/application code) can be used without this extra
309 * accessbility check.
312 mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
314 /* XXX find/create test cases for other handle_class XXX */
315 if (handle_class == mono_defaults.fieldhandle_class) {
316 MonoClassField *field = (MonoClassField*) ref;
317 MonoClass *klass = mono_field_get_parent (field);
318 /* fields coming from platform code have extra protection (accessibility check) */
319 if (mono_security_core_clr_is_platform_image (klass->image)) {
320 MonoMethod *caller = get_reflection_caller ();
321 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
322 if (!check_field_access (caller, field))
323 return mono_get_exception_field_access ();
325 } else if (handle_class == mono_defaults.methodhandle_class) {
326 MonoMethod *method = (MonoMethod*) ref;
327 /* methods coming from platform code have extra protection (accessibility check) */
328 if (mono_security_core_clr_is_platform_image (method->klass->image)) {
329 MonoMethod *caller = get_reflection_caller ();
330 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
331 if (!check_method_access (caller, method))
332 return mono_get_exception_method_access ();
339 * mono_security_core_clr_level_from_cinfo:
341 * Return the MonoSecurityCoreCLRLevel that match the attribute located
342 * in the specified custom attributes. If no attribute is present it
343 * defaults to MONO_SECURITY_CORE_CLR_TRANSPARENT, which is the default
344 * level for all code under the CoreCLR.
346 static MonoSecurityCoreCLRLevel
347 mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo *cinfo, MonoImage *image)
349 int level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
351 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_safe_critical_attribute ()))
352 level = MONO_SECURITY_CORE_CLR_SAFE_CRITICAL;
353 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_critical_attribute ()))
354 level = MONO_SECURITY_CORE_CLR_CRITICAL;
360 * mono_security_core_clr_class_level:
362 * Return the MonoSecurityCoreCLRLevel for the specified class.
364 MonoSecurityCoreCLRLevel
365 mono_security_core_clr_class_level (MonoClass *class)
367 MonoCustomAttrInfo *cinfo;
368 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
370 /* non-platform code is always Transparent - whatever the attributes says */
371 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (class->image))
374 cinfo = mono_custom_attrs_from_class (class);
376 level = mono_security_core_clr_level_from_cinfo (cinfo, class->image);
377 mono_custom_attrs_free (cinfo);
380 if (level == MONO_SECURITY_CORE_CLR_TRANSPARENT && class->nested_in)
381 level = mono_security_core_clr_class_level (class->nested_in);
387 * mono_security_core_clr_method_level:
389 * Return the MonoSecurityCoreCLRLevel for the specified method.
390 * If with_class_level is TRUE then the type (class) will also be
391 * checked, otherwise this will only report the information about
394 MonoSecurityCoreCLRLevel
395 mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
397 MonoCustomAttrInfo *cinfo;
398 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
400 /* non-platform code is always Transparent - whatever the attributes says */
401 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (method->klass->image))
404 cinfo = mono_custom_attrs_from_method (method);
406 level = mono_security_core_clr_level_from_cinfo (cinfo, method->klass->image);
407 mono_custom_attrs_free (cinfo);
410 if (with_class_level && level == MONO_SECURITY_CORE_CLR_TRANSPARENT)
411 level = mono_security_core_clr_class_level (method->klass);
417 * mono_security_core_clr_is_platform_image:
419 * Return the (cached) boolean value indicating if this image represent platform code
422 mono_security_core_clr_is_platform_image (MonoImage *image)
424 return image->core_clr_platform_code;
428 * default_platform_check:
430 * Default platform check. Always return FALSE.
433 default_platform_check (const char *image_name)
438 static MonoCoreClrPlatformCB platform_callback = default_platform_check;
441 * mono_security_core_clr_determine_platform_image:
443 * Call the supplied callback (from mono_security_set_core_clr_platform_callback)
444 * to determine if this image represents platform code.
447 mono_security_core_clr_determine_platform_image (MonoImage *image)
449 return platform_callback (image->name);
453 * mono_security_enable_core_clr:
455 * Enable the verifier and the CoreCLR security model
458 mono_security_enable_core_clr ()
460 mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
461 mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
465 * mono_security_set_core_clr_platform_callback:
467 * Set the callback function that will be used to determine if an image
468 * is part, or not, of the platform code.
471 mono_security_set_core_clr_platform_callback (MonoCoreClrPlatformCB callback)
473 platform_callback = callback;