2 * security-core-clr.c: CoreCLR security
5 * Mark Probst <mark.probst@gmail.com>
6 * Sebastien Pouliot <sebastien@ximian.com>
8 * Copyright 2007-2010 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>
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/utils/mono-logger-internal.h>
21 #include "security-core-clr.h"
23 gboolean mono_security_core_clr_test = FALSE;
25 static MonoSecurityCoreCLROptions security_core_clr_options = MONO_SECURITY_CORE_CLR_OPTIONS_DEFAULT;
28 * mono_security_core_clr_set_options:
29 * @options: the new options for the coreclr system to use
31 * By default, the CoreCLRs security model forbids execution trough reflection of methods not visible from the calling code.
32 * Even if the method being called is not in a platform assembly. For non moonlight CoreCLR users this restriction does not
33 * make a lot of sense, since the author could have just changed the non platform assembly to allow the method to be called.
34 * This function allows specific relaxations from the default behaviour to be set.
36 * Use MONO_SECURITY_CORE_CLR_OPTIONS_DEFAULT for the default coreclr coreclr behaviour as used in Moonlight.
38 * Use MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION to allow transparent code to execute methods and access
39 * fields that are not in platformcode, even if those methods and fields are private or otherwise not visible to the calling code.
41 * Use MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_DELEGATE to allow delegates to be created that point at methods that are not in
42 * platformcode even if those methods and fields are private or otherwise not visible to the calling code.
46 mono_security_core_clr_set_options (MonoSecurityCoreCLROptions options) {
47 security_core_clr_options = options;
51 * mono_security_core_clr_get_options:
53 * Retrieves the current options used by the coreclr system.
56 MonoSecurityCoreCLROptions
57 mono_security_core_clr_get_options ()
59 return security_core_clr_options;
63 * default_platform_check:
65 * Default platform check. Always TRUE for current corlib (minimum
66 * trust-able subset) otherwise return FALSE. Any real CoreCLR host
67 * should provide its own callback to define platform code (i.e.
68 * this default is meant for test only).
71 default_platform_check (const char *image_name)
73 if (mono_defaults.corlib) {
74 return (strcmp (mono_defaults.corlib->name, image_name) == 0);
76 /* this can get called even before we load corlib (e.g. the EXE itself) */
77 const char *corlib = "mscorlib.dll";
78 int ilen = strlen (image_name);
79 int clen = strlen (corlib);
80 return ((ilen >= clen) && (strcmp ("mscorlib.dll", image_name + ilen - clen) == 0));
84 static MonoCoreClrPlatformCB platform_callback = default_platform_check;
87 * mono_security_core_clr_determine_platform_image:
89 * Call the supplied callback (from mono_security_set_core_clr_platform_callback)
90 * to determine if this image represents platform code.
93 mono_security_core_clr_determine_platform_image (MonoImage *image)
95 return platform_callback (image->name);
99 * mono_security_set_core_clr_platform_callback:
101 * Set the callback function that will be used to determine if an image
102 * is part, or not, of the platform code.
105 mono_security_set_core_clr_platform_callback (MonoCoreClrPlatformCB callback)
107 platform_callback = callback;
111 * mono_security_core_clr_is_platform_image:
113 * Return the (cached) boolean value indicating if this image represent platform code
116 mono_security_core_clr_is_platform_image (MonoImage *image)
118 return image->core_clr_platform_code;
121 /* Note: The above functions are outside this guard so that the public API isn't affected. */
123 #ifndef DISABLE_SECURITY
126 security_critical_attribute (void)
128 static MonoClass *class = NULL;
131 class = mono_class_from_name (mono_defaults.corlib, "System.Security",
132 "SecurityCriticalAttribute");
139 security_safe_critical_attribute (void)
141 static MonoClass *class = NULL;
144 class = mono_class_from_name (mono_defaults.corlib, "System.Security",
145 "SecuritySafeCriticalAttribute");
151 /* sometime we get a NULL (not found) caller (e.g. get_reflection_caller) */
153 get_method_full_name (MonoMethod * method)
155 return method ? mono_method_full_name (method, TRUE) : g_strdup ("'no caller found'");
159 * set_type_load_exception_type
161 * Set MONO_EXCEPTION_TYPE_LOAD on the specified 'class' and provide
162 * a descriptive message for the exception. This message is also,
163 * optionally, being logged (export MONO_LOG_MASK="security") for
164 * debugging purposes.
167 set_type_load_exception_type (const char *format, MonoClass *class)
169 char *type_name = mono_type_get_full_name (class);
170 char *parent_name = mono_type_get_full_name (class->parent);
171 char *message = g_strdup_printf (format, type_name, parent_name);
173 g_free (parent_name);
176 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
177 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, message);
178 // note: do not free string given to mono_class_set_failure
182 * set_type_load_exception_methods
184 * Set MONO_EXCEPTION_TYPE_LOAD on the 'override' class and provide
185 * a descriptive message for the exception. This message is also,
186 * optionally, being logged (export MONO_LOG_MASK="security") for
187 * debugging purposes.
190 set_type_load_exception_methods (const char *format, MonoMethod *override, MonoMethod *base)
192 char *method_name = get_method_full_name (override);
193 char *base_name = get_method_full_name (base);
194 char *message = g_strdup_printf (format, method_name, base_name);
197 g_free (method_name);
199 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
200 mono_class_set_failure (override->klass, MONO_EXCEPTION_TYPE_LOAD, message);
201 // note: do not free string given to mono_class_set_failure
204 /* MonoClass is not fully initialized (inited is not yet == 1) when we
205 * check the inheritance rules so we need to look for the default ctor
206 * ourselve to avoid recursion (and aborting)
209 get_default_ctor (MonoClass *klass)
213 mono_class_setup_methods (klass);
217 for (i = 0; i < klass->method.count; ++i) {
218 MonoMethodSignature *sig;
219 MonoMethod *method = klass->methods [i];
224 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) == 0)
226 if ((method->name[0] != '.') || strcmp (".ctor", method->name))
228 sig = mono_method_signature (method);
229 if (sig && (sig->param_count == 0))
237 * mono_security_core_clr_check_inheritance:
239 * Determine if the specified class can inherit from its parent using
240 * the CoreCLR inheritance rules.
242 * Base Type Allow Derived Type
243 * ------------ ------------------
244 * Transparent Transparent, SafeCritical, Critical
245 * SafeCritical SafeCritical, Critical
248 * Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
250 * Furthermore a class MUST have a default constructor if its base
251 * class has a non-transparent, public or protected, default constructor.
252 * The same inheritance rule applies to both default constructors.
254 * Reference: message from a SecurityException in SL4RC
255 * Reference: fxcop CA2132 rule
258 mono_security_core_clr_check_inheritance (MonoClass *class)
260 MonoSecurityCoreCLRLevel class_level, parent_level;
261 MonoClass *parent = class->parent;
266 class_level = mono_security_core_clr_class_level (class);
267 parent_level = mono_security_core_clr_class_level (parent);
269 if (class_level < parent_level) {
270 set_type_load_exception_type (
271 "Inheritance failure for type %s. Parent class %s is more restricted.",
274 MonoMethod *parent_ctor = get_default_ctor (parent);
275 if (parent_ctor && ((parent_ctor->flags & METHOD_ATTRIBUTE_PUBLIC) != 0)) {
276 class_level = mono_security_core_clr_method_level (get_default_ctor (class), FALSE);
277 parent_level = mono_security_core_clr_method_level (parent_ctor, FALSE);
278 if (class_level < parent_level) {
279 set_type_load_exception_type (
280 "Inheritance failure for type %s. Default constructor security mismatch with %s.",
288 * mono_security_core_clr_check_override:
290 * Determine if the specified override can "legally" override the
291 * specified base method using the CoreCLR inheritance rules.
293 * Base (virtual/interface) Allowed override
294 * ------------------------ -------------------------
295 * Transparent Transparent, SafeCritical
296 * SafeCritical Transparent, SafeCritical
299 * Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
302 mono_security_core_clr_check_override (MonoClass *class, MonoMethod *override, MonoMethod *base)
304 MonoSecurityCoreCLRLevel base_level = mono_security_core_clr_method_level (base, FALSE);
305 MonoSecurityCoreCLRLevel override_level = mono_security_core_clr_method_level (override, FALSE);
306 /* if the base method is decorated with [SecurityCritical] then the overrided method MUST be too */
307 if (base_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
308 if (override_level != MONO_SECURITY_CORE_CLR_CRITICAL) {
309 set_type_load_exception_methods (
310 "Override failure for %s over %s. Override MUST be [SecurityCritical].",
314 /* base is [SecuritySafeCritical] or [SecurityTransparent], override MUST NOT be [SecurityCritical] */
315 if (override_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
316 set_type_load_exception_methods (
317 "Override failure for %s over %s. Override must NOT be [SecurityCritical].",
324 * get_caller_no_reflection_related:
326 * Find the first managed caller that is either:
327 * (a) located outside the platform code assemblies; or
328 * (b) not related to reflection and delegates
330 * Returns TRUE to stop the stackwalk, FALSE to continue to the next frame.
333 get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
335 MonoMethod **dest = data;
338 /* skip unmanaged frames */
342 if (m->wrapper_type != MONO_WRAPPER_NONE)
345 /* quick out (any namespace not starting with an 'S' */
346 ns = m->klass->name_space;
347 if (!ns || (*ns != 'S')) {
352 /* stop if the method is not part of platform code */
353 if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
358 /* any number of calls inside System.Reflection are allowed */
359 if (strcmp (ns, "System.Reflection") == 0)
362 /* any number of calls inside System.Reflection are allowed */
363 if (strcmp (ns, "System.Reflection.Emit") == 0)
366 /* calls from System.Delegate are also possible and allowed */
367 if (strcmp (ns, "System") == 0) {
368 const char *kname = m->klass->name;
369 if ((*kname == 'A') && (strcmp (kname, "Activator") == 0))
372 /* unlike most Invoke* cases InvokeMember is not inside System.Reflection[.Emit] but is SecuritySafeCritical */
373 if (((*kname == 'T') && (strcmp (kname, "Type") == 0)) ||
374 ((*kname == 'M') && (strcmp (kname, "MonoType")) == 0)) {
376 /* if calling InvokeMember then we can't stop the stackwalk here and need to look at the caller */
377 if (strcmp (m->name, "InvokeMember") == 0)
381 /* the security check on the delegate is made at creation time, not at invoke time */
382 if (((*kname == 'D') && (strcmp (kname, "Delegate") == 0)) ||
383 ((*kname == 'M') && (strcmp (kname, "MulticastDelegate")) == 0)) {
385 /* if we're invoking then we can stop our stack walk */
386 if (strcmp (m->name, "DynamicInvoke") != 0)
401 * get_reflection_caller:
403 * Walk to the first managed method outside:
404 * - System.Reflection* namespaces
405 * - System.[Multicast]Delegate or Activator type
407 * and return a pointer to its MonoMethod.
409 * This is required since CoreCLR checks needs to be done on this "real" caller.
412 get_reflection_caller (void)
414 MonoMethod *m = NULL;
415 mono_stack_walk_no_il (get_caller_no_reflection_related, &m);
416 if (G_UNLIKELY (!m)) {
417 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, "No caller outside reflection was found");
425 } ElevatedTrustCookie;
428 * get_caller_of_elevated_trust_code
430 * Stack walk to find who is calling code requiring Elevated Trust.
431 * If a critical method is found then the caller is platform code
432 * and has elevated trust, otherwise (transparent) a check needs to
433 * be done (on the managed side) to determine if the application is
434 * running with elevated permissions.
437 get_caller_of_elevated_trust_code (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
439 ElevatedTrustCookie *cookie = data;
441 /* skip unmanaged frames and wrappers */
442 if (!managed || (m->wrapper_type != MONO_WRAPPER_NONE))
445 /* end stack walk if we find ourselves outside platform code (we won't find critical code anymore) */
446 if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
451 switch (cookie->depth) {
452 /* while depth == 0 look for SecurityManager::[Check|Ensure]ElevatedPermissions */
454 if (strcmp (m->klass->name_space, "System.Security"))
456 if (strcmp (m->klass->name, "SecurityManager"))
458 if ((strcmp (m->name, "EnsureElevatedPermissions")) && strcmp (m->name, "CheckElevatedPermissions"))
462 /* while depth == 1 look for the caller to SecurityManager::[Check|Ensure]ElevatedPermissions */
464 /* this frame is [SecuritySafeCritical] because it calls [SecurityCritical] [Check|Ensure]ElevatedPermissions */
465 /* the next frame will contain the caller(s) we want to check */
468 /* while depth >= 2 look for [safe]critical caller, end stack walk if we find it */
471 /* if the caller is transparent then we continue the stack walk */
472 if (mono_security_core_clr_method_level (m, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT)
475 /* Security[Safe]Critical code is always allowed to call elevated-trust code */
484 * mono_security_core_clr_require_elevated_permissions:
486 * Return TRUE if the caller of the current method (the code who
487 * called SecurityManager.get_RequiresElevatedPermissions) needs
488 * elevated trust to perform an action.
490 * A stack walk is done to find the callers. If one of the callers
491 * is either [SecurityCritical] or [SecuritySafeCritical] then the
492 * action is needed for platform code (i.e. no restriction).
493 * Otherwise (transparent) the requested action needs elevated trust
496 mono_security_core_clr_require_elevated_permissions (void)
498 ElevatedTrustCookie cookie;
500 cookie.caller = NULL;
501 mono_stack_walk_no_il (get_caller_of_elevated_trust_code, &cookie);
503 /* return TRUE if the stack walk did not reach far enough or did not find callers */
504 if (!cookie.caller || cookie.depth < 3)
507 /* return TRUE if the caller is transparent, i.e. if elevated trust is required to continue executing the method */
508 return (mono_security_core_clr_method_level (cookie.caller, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT);
513 * check_field_access:
515 * Return TRUE if the caller method can access the specified field, FALSE otherwise.
518 check_field_access (MonoMethod *caller, MonoClassField *field)
520 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
525 /* this check can occur before the field's type is resolved (and that can fail) */
526 mono_field_get_type_checked (field, &error);
527 if (!mono_error_ok (&error)) {
528 mono_error_cleanup (&error);
532 klass = (mono_field_get_flags (field) & FIELD_ATTRIBUTE_STATIC) ? NULL : mono_field_get_parent (field);
533 return mono_method_can_access_field_full (caller, field, klass);
539 * check_method_access:
541 * Return TRUE if the caller method can access the specified callee method, FALSE otherwise.
544 check_method_access (MonoMethod *caller, MonoMethod *callee)
546 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
548 MonoClass *klass = (callee->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : callee->klass;
549 return mono_method_can_access_method_full (caller, callee, klass);
555 * get_argument_exception
557 * Helper function to create an MonoException (ArgumentException in
558 * managed-land) and provide a descriptive message for it. This
559 * message is also, optionally, being logged (export
560 * MONO_LOG_MASK="security") for debugging purposes.
562 static MonoException*
563 get_argument_exception (const char *format, MonoMethod *caller, MonoMethod *callee)
566 char *caller_name = get_method_full_name (caller);
567 char *callee_name = get_method_full_name (callee);
568 char *message = g_strdup_printf (format, caller_name, callee_name);
569 g_free (callee_name);
570 g_free (caller_name);
572 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
573 ex = mono_get_exception_argument ("method", message);
580 * get_field_access_exception
582 * Helper function to create an MonoException (FieldAccessException
583 * in managed-land) and provide a descriptive message for it. This
584 * message is also, optionally, being logged (export
585 * MONO_LOG_MASK="security") for debugging purposes.
587 static MonoException*
588 get_field_access_exception (const char *format, MonoMethod *caller, MonoClassField *field)
591 char *caller_name = get_method_full_name (caller);
592 char *field_name = mono_field_full_name (field);
593 char *message = g_strdup_printf (format, caller_name, field_name);
595 g_free (caller_name);
597 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
598 ex = mono_get_exception_field_access_msg (message);
605 * get_method_access_exception
607 * Helper function to create an MonoException (MethodAccessException
608 * in managed-land) and provide a descriptive message for it. This
609 * message is also, optionally, being logged (export
610 * MONO_LOG_MASK="security") for debugging purposes.
612 static MonoException*
613 get_method_access_exception (const char *format, MonoMethod *caller, MonoMethod *callee)
616 char *caller_name = get_method_full_name (caller);
617 char *callee_name = get_method_full_name (callee);
618 char *message = g_strdup_printf (format, caller_name, callee_name);
619 g_free (callee_name);
620 g_free (caller_name);
622 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
623 ex = mono_get_exception_method_access_msg (message);
630 * mono_security_core_clr_ensure_reflection_access_field:
632 * Ensure that the specified field can be used with reflection since
633 * Transparent code cannot access to Critical fields and can only use
634 * them if they are visible from it's point of view.
636 * A FieldAccessException is thrown if the field is cannot be accessed.
639 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
641 MonoMethod *caller = get_reflection_caller ();
642 /* CoreCLR restrictions applies to Transparent code/caller */
643 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
646 if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION) {
647 if (!mono_security_core_clr_is_platform_image (mono_field_get_parent(field)->image))
651 /* Transparent code cannot [get|set]value on Critical fields */
652 if (mono_security_core_clr_class_level (mono_field_get_parent (field)) == MONO_SECURITY_CORE_CLR_CRITICAL) {
653 mono_raise_exception (get_field_access_exception (
654 "Transparent method %s cannot get or set Critical field %s.",
658 /* also it cannot access a fields that is not visible from it's (caller) point of view */
659 if (!check_field_access (caller, field)) {
660 mono_raise_exception (get_field_access_exception (
661 "Transparent method %s cannot get or set private/internal field %s.",
667 * mono_security_core_clr_ensure_reflection_access_method:
669 * Ensure that the specified method can be used with reflection since
670 * Transparent code cannot call Critical methods and can only call them
671 * if they are visible from it's point of view.
673 * A MethodAccessException is thrown if the field is cannot be accessed.
676 mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
678 MonoMethod *caller = get_reflection_caller ();
679 /* CoreCLR restrictions applies to Transparent code/caller */
680 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
683 if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION) {
684 if (!mono_security_core_clr_is_platform_image (method->klass->image))
688 /* Transparent code cannot invoke, even using reflection, Critical code */
689 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
690 mono_raise_exception (get_method_access_exception (
691 "Transparent method %s cannot invoke Critical method %s.",
695 /* also it cannot invoke a method that is not visible from it's (caller) point of view */
696 if (!check_method_access (caller, method)) {
697 mono_raise_exception (get_method_access_exception (
698 "Transparent method %s cannot invoke private/internal method %s.",
704 * can_avoid_corlib_reflection_delegate_optimization:
706 * Mono's mscorlib use delegates to optimize PropertyInfo and EventInfo
707 * reflection calls. This requires either a bunch of additional, and not
708 * really required, [SecuritySafeCritical] in the class libraries or
709 * (like this) a way to skip them. As a bonus we also avoid the stack
710 * walk to find the caller.
712 * Return TRUE if we can skip this "internal" delegate creation, FALSE
716 can_avoid_corlib_reflection_delegate_optimization (MonoMethod *method)
718 if (!mono_security_core_clr_is_platform_image (method->klass->image))
721 if (strcmp (method->klass->name_space, "System.Reflection") != 0)
724 if (strcmp (method->klass->name, "MonoProperty") == 0) {
725 if ((strcmp (method->name, "GetterAdapterFrame") == 0) || strcmp (method->name, "StaticGetterAdapterFrame") == 0)
727 } else if (strcmp (method->klass->name, "EventInfo") == 0) {
728 if ((strcmp (method->name, "AddEventFrame") == 0) || strcmp (method->name, "StaticAddEventAdapterFrame") == 0)
736 * mono_security_core_clr_ensure_delegate_creation:
738 * Return TRUE if a delegate can be created on the specified method.
739 * CoreCLR also affect the binding, so throwOnBindFailure must be
740 * FALSE to let this function return (FALSE) normally, otherwise (if
741 * throwOnBindFailure is TRUE) it will throw an ArgumentException.
743 * A MethodAccessException is thrown if the specified method is not
744 * visible from the caller point of view.
747 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean throwOnBindFailure)
751 /* note: mscorlib creates delegates to avoid reflection (optimization), we ignore those cases */
752 if (can_avoid_corlib_reflection_delegate_optimization (method))
755 caller = get_reflection_caller ();
756 /* if the "real" caller is not Transparent then it do can anything */
757 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
760 /* otherwise it (as a Transparent caller) cannot create a delegate on a Critical method... */
761 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
762 /* but this throws only if 'throwOnBindFailure' is TRUE */
763 if (!throwOnBindFailure)
766 mono_raise_exception (get_argument_exception (
767 "Transparent method %s cannot create a delegate on Critical method %s.",
771 if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_DELEGATE) {
772 if (!mono_security_core_clr_is_platform_image (method->klass->image))
776 /* also it cannot create the delegate on a method that is not visible from it's (caller) point of view */
777 if (!check_method_access (caller, method)) {
778 mono_raise_exception (get_method_access_exception (
779 "Transparent method %s cannot create a delegate on private/internal method %s.",
787 * mono_security_core_clr_ensure_dynamic_method_resolved_object:
789 * Called from mono_reflection_create_dynamic_method (reflection.c) to add some extra checks required for CoreCLR.
790 * Dynamic methods needs to check to see if the objects being used (e.g. methods, fields) comes from platform code
791 * and do an accessibility check in this case. Otherwise (i.e. user/application code) can be used without this extra
792 * accessbility check.
795 mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
797 /* XXX find/create test cases for other handle_class XXX */
798 if (handle_class == mono_defaults.fieldhandle_class) {
799 MonoClassField *field = (MonoClassField*) ref;
800 MonoClass *klass = mono_field_get_parent (field);
801 /* fields coming from platform code have extra protection (accessibility check) */
802 if (mono_security_core_clr_is_platform_image (klass->image)) {
803 MonoMethod *caller = get_reflection_caller ();
804 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
805 if (!check_field_access (caller, field)) {
806 return get_field_access_exception (
807 "Dynamic method %s cannot create access private/internal field %s.",
811 } else if (handle_class == mono_defaults.methodhandle_class) {
812 MonoMethod *method = (MonoMethod*) ref;
813 /* methods coming from platform code have extra protection (accessibility check) */
814 if (mono_security_core_clr_is_platform_image (method->klass->image)) {
815 MonoMethod *caller = get_reflection_caller ();
816 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
817 if (!check_method_access (caller, method)) {
818 return get_method_access_exception (
819 "Dynamic method %s cannot create access private/internal method %s.",
828 * mono_security_core_clr_can_access_internals
830 * Check if we allow [InternalsVisibleTo] to work between two images.
833 mono_security_core_clr_can_access_internals (MonoImage *accessing, MonoImage* accessed)
835 /* are we trying to access internals of a platform assembly ? if not this is acceptable */
836 if (!mono_security_core_clr_is_platform_image (accessed))
839 /* we can't let everyone with the right name and public key token access the internals of platform code.
840 * (Silverlight can rely on the strongname signature of the assemblies, but Mono does not verify them)
841 * However platform code is fully trusted so it can access the internals of other platform code assemblies */
842 if (mono_security_core_clr_is_platform_image (accessing))
845 /* catch-22: System.Xml needs access to mscorlib's internals (e.g. ArrayList) but is not considered platform code.
846 * Promoting it to platform code would create another issue since (both Mono/Moonlight or MS version of)
847 * System.Xml.Linq.dll (an SDK, not platform, assembly) needs access to System.Xml.dll internals (either ).
848 * The solution is to trust, even transparent code, in the plugin directory to access platform code internals */
849 if (!accessed->assembly->basedir || !accessing->assembly->basedir)
851 return (strcmp (accessed->assembly->basedir, accessing->assembly->basedir) == 0);
855 * mono_security_core_clr_is_field_access_allowed
857 * Return a MonoException (FieldccessException in managed-land) if
858 * the access from "caller" to "field" is not valid under CoreCLR -
859 * i.e. a [SecurityTransparent] method calling a [SecurityCritical]
863 mono_security_core_clr_is_field_access_allowed (MonoMethod *caller, MonoClassField *field)
865 /* there's no restriction to access Transparent or SafeCritical fields, so we only check calls to Critical methods */
866 if (mono_security_core_clr_class_level (mono_field_get_parent (field)) != MONO_SECURITY_CORE_CLR_CRITICAL)
869 /* caller is Critical! only SafeCritical and Critical callers can access the field, so we throw if caller is Transparent */
870 if (!caller || (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT))
873 return get_field_access_exception (
874 "Transparent method %s cannot call use Critical field %s.",
879 * mono_security_core_clr_is_call_allowed
881 * Return a MonoException (MethodAccessException in managed-land) if
882 * the call from "caller" to "callee" is not valid under CoreCLR -
883 * i.e. a [SecurityTransparent] method calling a [SecurityCritical]
887 mono_security_core_clr_is_call_allowed (MonoMethod *caller, MonoMethod *callee)
889 /* there's no restriction to call Transparent or SafeCritical code, so we only check calls to Critical methods */
890 if (mono_security_core_clr_method_level (callee, TRUE) != MONO_SECURITY_CORE_CLR_CRITICAL)
893 /* callee is Critical! only SafeCritical and Critical callers can call it, so we throw if the caller is Transparent */
894 if (!caller || (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT))
897 return get_method_access_exception (
898 "Transparent method %s cannot call Critical method %s.",
903 * mono_security_core_clr_level_from_cinfo:
905 * Return the MonoSecurityCoreCLRLevel that match the attribute located
906 * in the specified custom attributes. If no attribute is present it
907 * defaults to MONO_SECURITY_CORE_CLR_TRANSPARENT, which is the default
908 * level for all code under the CoreCLR.
910 static MonoSecurityCoreCLRLevel
911 mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo *cinfo, MonoImage *image)
913 int level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
915 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_safe_critical_attribute ()))
916 level = MONO_SECURITY_CORE_CLR_SAFE_CRITICAL;
917 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_critical_attribute ()))
918 level = MONO_SECURITY_CORE_CLR_CRITICAL;
924 * mono_security_core_clr_class_level_no_platform_check:
926 * Return the MonoSecurityCoreCLRLevel for the specified class, without
927 * checking for platform code. This help us avoid multiple redundant
929 * - a check for the method and one for the class;
930 * - a check for the class and outer class(es) ...
932 static MonoSecurityCoreCLRLevel
933 mono_security_core_clr_class_level_no_platform_check (MonoClass *class)
935 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
936 MonoCustomAttrInfo *cinfo = mono_custom_attrs_from_class (class);
938 level = mono_security_core_clr_level_from_cinfo (cinfo, class->image);
939 mono_custom_attrs_free (cinfo);
942 if (level == MONO_SECURITY_CORE_CLR_TRANSPARENT && class->nested_in)
943 level = mono_security_core_clr_class_level_no_platform_check (class->nested_in);
949 * mono_security_core_clr_class_level:
951 * Return the MonoSecurityCoreCLRLevel for the specified class.
953 MonoSecurityCoreCLRLevel
954 mono_security_core_clr_class_level (MonoClass *class)
956 /* non-platform code is always Transparent - whatever the attributes says */
957 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (class->image))
958 return MONO_SECURITY_CORE_CLR_TRANSPARENT;
960 return mono_security_core_clr_class_level_no_platform_check (class);
964 * mono_security_core_clr_method_level:
966 * Return the MonoSecurityCoreCLRLevel for the specified method.
967 * If with_class_level is TRUE then the type (class) will also be
968 * checked, otherwise this will only report the information about
971 MonoSecurityCoreCLRLevel
972 mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
974 MonoCustomAttrInfo *cinfo;
975 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
977 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
981 /* non-platform code is always Transparent - whatever the attributes says */
982 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (method->klass->image))
985 cinfo = mono_custom_attrs_from_method (method);
987 level = mono_security_core_clr_level_from_cinfo (cinfo, method->klass->image);
988 mono_custom_attrs_free (cinfo);
991 if (with_class_level && level == MONO_SECURITY_CORE_CLR_TRANSPARENT)
992 level = mono_security_core_clr_class_level (method->klass);
998 * mono_security_enable_core_clr:
1000 * Enable the verifier and the CoreCLR security model
1003 mono_security_enable_core_clr ()
1005 mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
1006 mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
1012 mono_security_core_clr_check_inheritance (MonoClass *class)
1017 mono_security_core_clr_check_override (MonoClass *class, MonoMethod *override, MonoMethod *base)
1022 mono_security_core_clr_require_elevated_permissions (void)
1028 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
1033 mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
1038 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean throwOnBindFailure)
1044 mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
1050 mono_security_core_clr_can_access_internals (MonoImage *accessing, MonoImage* accessed)
1056 mono_security_core_clr_is_field_access_allowed (MonoMethod *caller, MonoClassField *field)
1062 mono_security_core_clr_is_call_allowed (MonoMethod *caller, MonoMethod *callee)
1067 MonoSecurityCoreCLRLevel
1068 mono_security_core_clr_class_level (MonoClass *class)
1070 return MONO_SECURITY_CORE_CLR_TRANSPARENT;
1073 MonoSecurityCoreCLRLevel
1074 mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
1076 return MONO_SECURITY_CORE_CLR_TRANSPARENT;
1080 mono_security_enable_core_clr ()
1084 #endif /* DISABLE_SECURITY */