2 * security-core-clr.c: CoreCLR security
5 * Mark Probst <mark.probst@gmail.com>
6 * Sebastien Pouliot <sebastien@ximian.com>
8 * Copyright 2007-2009 Novell, Inc (http://www.novell.com)
11 #include <mono/metadata/class-internals.h>
12 #include <mono/metadata/security-manager.h>
13 #include <mono/metadata/assembly.h>
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/verify-internals.h>
16 #include <mono/metadata/object.h>
17 #include <mono/metadata/exception.h>
19 #include "security-core-clr.h"
21 gboolean mono_security_core_clr_test = FALSE;
24 security_critical_attribute (void)
26 static MonoClass *class = NULL;
29 class = mono_class_from_name (mono_defaults.corlib, "System.Security",
30 "SecurityCriticalAttribute");
37 security_safe_critical_attribute (void)
39 static MonoClass *class = NULL;
42 class = mono_class_from_name (mono_defaults.corlib, "System.Security",
43 "SecuritySafeCriticalAttribute");
50 * mono_security_core_clr_check_inheritance:
52 * Determine if the specified class can inherit from its parent using
53 * the CoreCLR inheritance rules.
55 * Base Type Allow Derived Type
56 * ------------ ------------------
57 * Transparent Transparent, SafeCritical, Critical
58 * SafeCritical SafeCritical, Critical
61 * Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
64 mono_security_core_clr_check_inheritance (MonoClass *class)
66 MonoSecurityCoreCLRLevel class_level, parent_level;
67 MonoClass *parent = class->parent;
72 class_level = mono_security_core_clr_class_level (class);
73 parent_level = mono_security_core_clr_class_level (parent);
75 if (class_level < parent_level)
76 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
80 * mono_security_core_clr_check_override:
82 * Determine if the specified override can "legally" override the
83 * specified base method using the CoreCLR inheritance rules.
85 * Base (virtual/interface) Allowed override
86 * ------------------------ -------------------------
87 * Transparent Transparent, SafeCritical
88 * SafeCritical Transparent, SafeCritical
91 * Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
94 mono_security_core_clr_check_override (MonoClass *class, MonoMethod *override, MonoMethod *base)
96 MonoSecurityCoreCLRLevel base_level = mono_security_core_clr_method_level (base, FALSE);
97 MonoSecurityCoreCLRLevel override_level = mono_security_core_clr_method_level (override, FALSE);
98 /* if the base method is decorated with [SecurityCritical] then the overrided method MUST be too */
99 if (base_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
100 if (override_level != MONO_SECURITY_CORE_CLR_CRITICAL)
101 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
103 /* base is [SecuritySafeCritical] or [SecurityTransparent], override MUST NOT be [SecurityCritical] */
104 if (override_level == MONO_SECURITY_CORE_CLR_CRITICAL)
105 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
110 * get_caller_no_reflection_related:
112 * Find the first managed caller that is either:
113 * (a) located outside the platform code assemblies; or
114 * (b) not related to reflection and delegates
116 * Returns TRUE to stop the stackwalk, FALSE to continue to the next frame.
119 get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
121 MonoMethod **dest = data;
124 /* skip unmanaged frames */
128 if (m->wrapper_type != MONO_WRAPPER_NONE)
131 /* quick out (any namespace not starting with an 'S' */
132 ns = m->klass->name_space;
133 if (!ns || (*ns != 'S')) {
138 /* stop if the method is not part of platform code */
139 if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
144 /* any number of calls inside System.Reflection are allowed */
145 if (strcmp (ns, "System.Reflection") == 0)
148 /* any number of calls inside System.Reflection are allowed */
149 if (strcmp (ns, "System.Reflection.Emit") == 0)
152 /* calls from System.Delegate are also possible and allowed */
153 if (strcmp (ns, "System") == 0) {
154 const char *kname = m->klass->name;
155 if ((*kname == 'D') && (strcmp (kname, "Delegate") == 0))
157 if ((*kname == 'M') && (strcmp (kname, "MulticastDelegate")) == 0)
159 if ((*kname == 'A') && (strcmp (kname, "Activator") == 0))
173 * get_reflection_caller:
175 * Walk to the first managed method outside:
176 * - System.Reflection* namespaces
177 * - System.[MulticastDelegate]Delegate or Activator type
179 * and return a pointer to its MonoMethod.
181 * This is required since CoreCLR checks needs to be done on this "real" caller.
184 get_reflection_caller (void)
186 MonoMethod *m = NULL;
187 mono_stack_walk_no_il (get_caller_no_reflection_related, &m);
189 g_warning ("could not find a caller outside reflection");
194 * check_field_access:
196 * Return TRUE if the caller method can access the specified field, FALSE otherwise.
199 check_field_access (MonoMethod *caller, MonoClassField *field)
201 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
203 MonoClass *klass = (mono_field_get_flags (field) & FIELD_ATTRIBUTE_STATIC) ? NULL : mono_field_get_parent (field);
204 return mono_method_can_access_field_full (caller, field, klass);
210 * check_method_access:
212 * Return TRUE if the caller method can access the specified callee method, FALSE otherwise.
215 check_method_access (MonoMethod *caller, MonoMethod *callee)
217 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
219 MonoClass *klass = (callee->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : callee->klass;
220 return mono_method_can_access_method_full (caller, callee, klass);
226 * mono_security_core_clr_ensure_reflection_access_field:
228 * Ensure that the specified field can be used with reflection since
229 * Transparent code cannot access to Critical fields and can only use
230 * them if they are visible from it's point of view.
232 * A FieldAccessException is thrown if the field is cannot be accessed.
235 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
237 MonoMethod *caller = get_reflection_caller ();
238 /* CoreCLR restrictions applies to Transparent code/caller */
239 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
242 /* Transparent code cannot [get|set]value on Critical fields */
243 if (mono_security_core_clr_class_level (mono_field_get_parent (field)) == MONO_SECURITY_CORE_CLR_CRITICAL)
244 mono_raise_exception (mono_get_exception_field_access ());
246 /* also it cannot access a fields that is not visible from it's (caller) point of view */
247 if (!check_field_access (caller, field))
248 mono_raise_exception (mono_get_exception_field_access ());
252 * mono_security_core_clr_ensure_reflection_access_method:
254 * Ensure that the specified method can be used with reflection since
255 * Transparent code cannot call Critical methods and can only call them
256 * if they are visible from it's point of view.
258 * A MethodAccessException is thrown if the field is cannot be accessed.
261 mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
263 MonoMethod *caller = get_reflection_caller ();
264 /* CoreCLR restrictions applies to Transparent code/caller */
265 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
268 /* Transparent code cannot invoke, even using reflection, Critical code */
269 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL)
270 mono_raise_exception (mono_get_exception_method_access ());
272 /* also it cannot invoke a method that is not visible from it's (caller) point of view */
273 if (!check_method_access (caller, method))
274 mono_raise_exception (mono_get_exception_method_access ());
278 * can_avoid_corlib_reflection_delegate_optimization:
280 * Mono's mscorlib use delegates to optimize PropertyInfo and EventInfo
281 * reflection calls. This requires either a bunch of additional, and not
282 * really required, [SecuritySafeCritical] in the class libraries or
283 * (like this) a way to skip them. As a bonus we also avoid the stack
284 * walk to find the caller.
286 * Return TRUE if we can skip this "internal" delegate creation, FALSE
290 can_avoid_corlib_reflection_delegate_optimization (MonoMethod *method)
292 if (!mono_security_core_clr_is_platform_image (method->klass->image))
295 if (strcmp (method->klass->name_space, "System.Reflection") != 0)
298 if (strcmp (method->klass->name, "MonoProperty") == 0) {
299 if ((strcmp (method->name, "GetterAdapterFrame") == 0) || strcmp (method->name, "StaticGetterAdapterFrame") == 0)
301 } else if (strcmp (method->klass->name, "EventInfo") == 0) {
302 if ((strcmp (method->name, "AddEventFrame") == 0) || strcmp (method->name, "StaticAddEventAdapterFrame") == 0)
310 * mono_security_core_clr_ensure_delegate_creation:
312 * Return TRUE if a delegate can be created on the specified method.
313 * CoreCLR also affect the binding, so throwOnBindFailure must be
314 * FALSE to let this function return (FALSE) normally, otherwise (if
315 * throwOnBindFailure is TRUE) itwill throw an ArgumentException.
317 * A MethodAccessException is thrown if the specified method is not
318 * visible from the caller point of view.
321 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean throwOnBindFailure)
325 /* note: mscorlib creates delegates to avoid reflection (optimization), we ignore those cases */
326 if (can_avoid_corlib_reflection_delegate_optimization (method))
329 caller = get_reflection_caller ();
330 /* if the "real" caller is not Transparent then it do can anything */
331 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
334 /* otherwise it (as a Transparent caller) cannot create a delegate on a Critical method... */
335 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
336 /* but this throws only if 'throwOnBindFailure' is TRUE */
337 if (!throwOnBindFailure)
340 mono_raise_exception (mono_get_exception_argument ("method", "Transparent code cannot call Critical code"));
343 /* also it cannot create the delegate on a method that is not visible from it's (caller) point of view */
344 if (!check_method_access (caller, method))
345 mono_raise_exception (mono_get_exception_method_access ());
351 * mono_security_core_clr_ensure_dynamic_method_resolved_object:
353 * Called from mono_reflection_create_dynamic_method (reflection.c) to add some extra checks required for CoreCLR.
354 * Dynamic methods needs to check to see if the objects being used (e.g. methods, fields) comes from platform code
355 * and do an accessibility check in this case. Otherwise (i.e. user/application code) can be used without this extra
356 * accessbility check.
359 mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
361 /* XXX find/create test cases for other handle_class XXX */
362 if (handle_class == mono_defaults.fieldhandle_class) {
363 MonoClassField *field = (MonoClassField*) ref;
364 MonoClass *klass = mono_field_get_parent (field);
365 /* fields coming from platform code have extra protection (accessibility check) */
366 if (mono_security_core_clr_is_platform_image (klass->image)) {
367 MonoMethod *caller = get_reflection_caller ();
368 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
369 if (!check_field_access (caller, field))
370 return mono_get_exception_field_access ();
372 } else if (handle_class == mono_defaults.methodhandle_class) {
373 MonoMethod *method = (MonoMethod*) ref;
374 /* methods coming from platform code have extra protection (accessibility check) */
375 if (mono_security_core_clr_is_platform_image (method->klass->image)) {
376 MonoMethod *caller = get_reflection_caller ();
377 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
378 if (!check_method_access (caller, method))
379 return mono_get_exception_method_access ();
386 * mono_security_core_clr_level_from_cinfo:
388 * Return the MonoSecurityCoreCLRLevel that match the attribute located
389 * in the specified custom attributes. If no attribute is present it
390 * defaults to MONO_SECURITY_CORE_CLR_TRANSPARENT, which is the default
391 * level for all code under the CoreCLR.
393 static MonoSecurityCoreCLRLevel
394 mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo *cinfo, MonoImage *image)
396 int level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
398 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_safe_critical_attribute ()))
399 level = MONO_SECURITY_CORE_CLR_SAFE_CRITICAL;
400 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_critical_attribute ()))
401 level = MONO_SECURITY_CORE_CLR_CRITICAL;
407 * mono_security_core_clr_class_level:
409 * Return the MonoSecurityCoreCLRLevel for the specified class.
411 MonoSecurityCoreCLRLevel
412 mono_security_core_clr_class_level (MonoClass *class)
414 MonoCustomAttrInfo *cinfo;
415 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
417 /* non-platform code is always Transparent - whatever the attributes says */
418 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (class->image))
421 cinfo = mono_custom_attrs_from_class (class);
423 level = mono_security_core_clr_level_from_cinfo (cinfo, class->image);
424 mono_custom_attrs_free (cinfo);
427 if (level == MONO_SECURITY_CORE_CLR_TRANSPARENT && class->nested_in)
428 level = mono_security_core_clr_class_level (class->nested_in);
434 * mono_security_core_clr_method_level:
436 * Return the MonoSecurityCoreCLRLevel for the specified method.
437 * If with_class_level is TRUE then the type (class) will also be
438 * checked, otherwise this will only report the information about
441 MonoSecurityCoreCLRLevel
442 mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
444 MonoCustomAttrInfo *cinfo;
445 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
447 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
451 /* non-platform code is always Transparent - whatever the attributes says */
452 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (method->klass->image))
455 cinfo = mono_custom_attrs_from_method (method);
457 level = mono_security_core_clr_level_from_cinfo (cinfo, method->klass->image);
458 mono_custom_attrs_free (cinfo);
461 if (with_class_level && level == MONO_SECURITY_CORE_CLR_TRANSPARENT)
462 level = mono_security_core_clr_class_level (method->klass);
468 * mono_security_core_clr_is_platform_image:
470 * Return the (cached) boolean value indicating if this image represent platform code
473 mono_security_core_clr_is_platform_image (MonoImage *image)
475 return image->core_clr_platform_code;
479 * default_platform_check:
481 * Default platform check. Always return FALSE.
484 default_platform_check (const char *image_name)
489 static MonoCoreClrPlatformCB platform_callback = default_platform_check;
492 * mono_security_core_clr_determine_platform_image:
494 * Call the supplied callback (from mono_security_set_core_clr_platform_callback)
495 * to determine if this image represents platform code.
498 mono_security_core_clr_determine_platform_image (MonoImage *image)
500 return platform_callback (image->name);
504 * mono_security_enable_core_clr:
506 * Enable the verifier and the CoreCLR security model
509 mono_security_enable_core_clr ()
511 mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
512 mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
516 * mono_security_set_core_clr_platform_callback:
518 * Set the callback function that will be used to determine if an image
519 * is part, or not, of the platform code.
522 mono_security_set_core_clr_platform_callback (MonoCoreClrPlatformCB callback)
524 platform_callback = callback;