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;
26 security_critical_attribute (void)
28 static MonoClass *class = NULL;
31 class = mono_class_from_name (mono_defaults.corlib, "System.Security",
32 "SecurityCriticalAttribute");
39 security_safe_critical_attribute (void)
41 static MonoClass *class = NULL;
44 class = mono_class_from_name (mono_defaults.corlib, "System.Security",
45 "SecuritySafeCriticalAttribute");
51 /* sometime we get a NULL (not found) caller (e.g. get_reflection_caller) */
53 get_method_full_name (MonoMethod * method)
55 return method ? mono_method_full_name (method, TRUE) : g_strdup ("'no caller found'");
59 * set_type_load_exception_type
61 * Set MONO_EXCEPTION_TYPE_LOAD on the specified 'class' and provide
62 * a descriptive message for the exception. This message is also,
63 * optionally, being logged (export MONO_LOG_MASK="security") for
67 set_type_load_exception_type (const char *format, MonoClass *class)
69 char *type_name = mono_type_get_full_name (class);
70 char *parent_name = mono_type_get_full_name (class->parent);
71 char *message = g_strdup_printf (format, type_name, parent_name);
76 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
77 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, message);
78 // note: do not free string given to mono_class_set_failure
82 * set_type_load_exception_methods
84 * Set MONO_EXCEPTION_TYPE_LOAD on the 'override' class and provide
85 * a descriptive message for the exception. This message is also,
86 * optionally, being logged (export MONO_LOG_MASK="security") for
90 set_type_load_exception_methods (const char *format, MonoMethod *override, MonoMethod *base)
92 char *method_name = get_method_full_name (override);
93 char *base_name = get_method_full_name (base);
94 char *message = g_strdup_printf (format, method_name, base_name);
99 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
100 mono_class_set_failure (override->klass, MONO_EXCEPTION_TYPE_LOAD, message);
101 // note: do not free string given to mono_class_set_failure
104 /* MonoClass is not fully initialized (inited is not yet == 1) when we
105 * check the inheritance rules so we need to look for the default ctor
106 * ourselve to avoid recursion (and aborting)
109 get_default_ctor (MonoClass *klass)
113 mono_class_setup_methods (klass);
117 for (i = 0; i < klass->method.count; ++i) {
118 MonoMethodSignature *sig;
119 MonoMethod *method = klass->methods [i];
124 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) == 0)
126 if ((method->name[0] != '.') || strcmp (".ctor", method->name))
128 sig = mono_method_signature (method);
129 if (sig && (sig->param_count == 0))
137 * mono_security_core_clr_check_inheritance:
139 * Determine if the specified class can inherit from its parent using
140 * the CoreCLR inheritance rules.
142 * Base Type Allow Derived Type
143 * ------------ ------------------
144 * Transparent Transparent, SafeCritical, Critical
145 * SafeCritical SafeCritical, Critical
148 * Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
150 * Furthermore a class MUST have a default constructor if its base
151 * class has a non-transparent, public or protected, default constructor.
152 * The same inheritance rule applies to both default constructors.
154 * Reference: message from a SecurityException in SL4RC
155 * Reference: fxcop CA2132 rule
158 mono_security_core_clr_check_inheritance (MonoClass *class)
160 MonoSecurityCoreCLRLevel class_level, parent_level;
161 MonoClass *parent = class->parent;
166 class_level = mono_security_core_clr_class_level (class);
167 parent_level = mono_security_core_clr_class_level (parent);
169 if (class_level < parent_level) {
170 set_type_load_exception_type (
171 "Inheritance failure for type %s. Parent class %s is more restricted.",
174 MonoMethod *parent_ctor = get_default_ctor (parent);
175 if (parent_ctor && ((parent_ctor->flags & METHOD_ATTRIBUTE_PUBLIC) != 0)) {
176 class_level = mono_security_core_clr_method_level (get_default_ctor (class), FALSE);
177 parent_level = mono_security_core_clr_method_level (parent_ctor, FALSE);
178 if (class_level < parent_level) {
179 set_type_load_exception_type (
180 "Inheritance failure for type %s. Default constructor security mismatch with %s.",
188 * mono_security_core_clr_check_override:
190 * Determine if the specified override can "legally" override the
191 * specified base method using the CoreCLR inheritance rules.
193 * Base (virtual/interface) Allowed override
194 * ------------------------ -------------------------
195 * Transparent Transparent, SafeCritical
196 * SafeCritical Transparent, SafeCritical
199 * Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
202 mono_security_core_clr_check_override (MonoClass *class, MonoMethod *override, MonoMethod *base)
204 MonoSecurityCoreCLRLevel base_level = mono_security_core_clr_method_level (base, FALSE);
205 MonoSecurityCoreCLRLevel override_level = mono_security_core_clr_method_level (override, FALSE);
206 /* if the base method is decorated with [SecurityCritical] then the overrided method MUST be too */
207 if (base_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
208 if (override_level != MONO_SECURITY_CORE_CLR_CRITICAL) {
209 set_type_load_exception_methods (
210 "Override failure for %s over %s. Override MUST be [SecurityCritical].",
214 /* base is [SecuritySafeCritical] or [SecurityTransparent], override MUST NOT be [SecurityCritical] */
215 if (override_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
216 set_type_load_exception_methods (
217 "Override failure for %s over %s. Override must NOT be [SecurityCritical].",
224 * get_caller_no_reflection_related:
226 * Find the first managed caller that is either:
227 * (a) located outside the platform code assemblies; or
228 * (b) not related to reflection and delegates
230 * Returns TRUE to stop the stackwalk, FALSE to continue to the next frame.
233 get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
235 MonoMethod **dest = data;
238 /* skip unmanaged frames */
242 if (m->wrapper_type != MONO_WRAPPER_NONE)
245 /* quick out (any namespace not starting with an 'S' */
246 ns = m->klass->name_space;
247 if (!ns || (*ns != 'S')) {
252 /* stop if the method is not part of platform code */
253 if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
258 /* any number of calls inside System.Reflection are allowed */
259 if (strcmp (ns, "System.Reflection") == 0)
262 /* any number of calls inside System.Reflection are allowed */
263 if (strcmp (ns, "System.Reflection.Emit") == 0)
266 /* calls from System.Delegate are also possible and allowed */
267 if (strcmp (ns, "System") == 0) {
268 const char *kname = m->klass->name;
269 if ((*kname == 'A') && (strcmp (kname, "Activator") == 0))
272 /* unlike most Invoke* cases InvokeMember is not inside System.Reflection[.Emit] but is SecuritySafeCritical */
273 if (((*kname == 'T') && (strcmp (kname, "Type") == 0)) ||
274 ((*kname == 'M') && (strcmp (kname, "MonoType")) == 0)) {
276 /* if calling InvokeMember then we can't stop the stackwalk here and need to look at the caller */
277 if (strcmp (m->name, "InvokeMember") == 0)
281 /* the security check on the delegate is made at creation time, not at invoke time */
282 if (((*kname == 'D') && (strcmp (kname, "Delegate") == 0)) ||
283 ((*kname == 'M') && (strcmp (kname, "MulticastDelegate")) == 0)) {
285 /* if we're invoking then we can stop our stack walk */
286 if (strcmp (m->name, "DynamicInvoke") != 0)
301 * get_reflection_caller:
303 * Walk to the first managed method outside:
304 * - System.Reflection* namespaces
305 * - System.[Multicast]Delegate or Activator type
307 * and return a pointer to its MonoMethod.
309 * This is required since CoreCLR checks needs to be done on this "real" caller.
312 get_reflection_caller (void)
314 MonoMethod *m = NULL;
315 mono_stack_walk_no_il (get_caller_no_reflection_related, &m);
316 if (G_UNLIKELY (!m)) {
317 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, "No caller outside reflection was found");
325 } ElevatedTrustCookie;
328 * get_caller_of_elevated_trust_code
330 * Stack walk to find who is calling code requiring Elevated Trust.
331 * If a critical method is found then the caller is platform code
332 * and has elevated trust, otherwise (transparent) a check needs to
333 * be done (on the managed side) to determine if the application is
334 * running with elevated permissions.
337 get_caller_of_elevated_trust_code (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
339 ElevatedTrustCookie *cookie = data;
341 /* skip unmanaged frames and wrappers */
342 if (!managed || (m->wrapper_type != MONO_WRAPPER_NONE))
345 /* end stack walk if we find ourselves outside platform code (we won't find critical code anymore) */
346 if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
351 switch (cookie->depth) {
352 /* while depth == 0 look for SecurityManager::[Check|Ensure]ElevatedPermissions */
354 if (strcmp (m->klass->name_space, "System.Security"))
356 if (strcmp (m->klass->name, "SecurityManager"))
358 if ((strcmp (m->name, "EnsureElevatedPermissions")) && strcmp (m->name, "CheckElevatedPermissions"))
362 /* while depth == 1 look for the caller to SecurityManager::[Check|Ensure]ElevatedPermissions */
364 /* this frame is [SecuritySafeCritical] because it calls [SecurityCritical] [Check|Ensure]ElevatedPermissions */
365 /* the next frame will contain the caller(s) we want to check */
368 /* while depth >= 2 look for [safe]critical caller, end stack walk if we find it */
371 /* if the caller is transparent then we continue the stack walk */
372 if (mono_security_core_clr_method_level (m, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT)
375 /* Security[Safe]Critical code is always allowed to call elevated-trust code */
384 * mono_security_core_clr_require_elevated_permissions:
386 * Return TRUE if the caller of the current method (the code who
387 * called SecurityManager.get_RequiresElevatedPermissions) needs
388 * elevated trust to perform an action.
390 * A stack walk is done to find the callers. If one of the callers
391 * is either [SecurityCritical] or [SecuritySafeCritical] then the
392 * action is needed for platform code (i.e. no restriction).
393 * Otherwise (transparent) the requested action needs elevated trust
396 mono_security_core_clr_require_elevated_permissions (void)
398 ElevatedTrustCookie cookie;
400 cookie.caller = NULL;
401 mono_stack_walk_no_il (get_caller_of_elevated_trust_code, &cookie);
403 /* return TRUE if the stack walk did not reach far enough or did not find callers */
404 if (!cookie.caller || cookie.depth < 3)
407 /* return TRUE if the caller is transparent, i.e. if elevated trust is required to continue executing the method */
408 return (mono_security_core_clr_method_level (cookie.caller, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT);
412 static MonoSecurityCoreCLROptions security_core_clr_options = MONO_SECURITY_CORE_CLR_OPTIONS_DEFAULT;
415 * mono_security_core_clr_set_options
417 * By default, the CoreCLRs security model forbids execution trough reflection of methods not visible from the calling code.
418 * Even if the method being called is not in a platform assembly. For non moonlight CoreCLR users this restriction does not
419 * make a lot of sense, since the author could have just changed the non platform assembly to allow the method to be called.
420 * this function allows specific relaxations from the default behaviour to be set.
424 mono_security_core_clr_set_options (MonoSecurityCoreCLROptions options) {
425 security_core_clr_options = options;
428 MonoSecurityCoreCLROptions
429 mono_security_core_clr_get_options ()
431 return security_core_clr_options;
436 * check_field_access:
438 * Return TRUE if the caller method can access the specified field, FALSE otherwise.
441 check_field_access (MonoMethod *caller, MonoClassField *field)
443 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
448 /* this check can occur before the field's type is resolved (and that can fail) */
449 mono_field_get_type_checked (field, &error);
450 if (!mono_error_ok (&error)) {
451 mono_error_cleanup (&error);
455 klass = (mono_field_get_flags (field) & FIELD_ATTRIBUTE_STATIC) ? NULL : mono_field_get_parent (field);
456 return mono_method_can_access_field_full (caller, field, klass);
462 * check_method_access:
464 * Return TRUE if the caller method can access the specified callee method, FALSE otherwise.
467 check_method_access (MonoMethod *caller, MonoMethod *callee)
469 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
471 MonoClass *klass = (callee->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : callee->klass;
472 return mono_method_can_access_method_full (caller, callee, klass);
478 * get_argument_exception
480 * Helper function to create an MonoException (ArgumentException in
481 * managed-land) and provide a descriptive message for it. This
482 * message is also, optionally, being logged (export
483 * MONO_LOG_MASK="security") for debugging purposes.
485 static MonoException*
486 get_argument_exception (const char *format, MonoMethod *caller, MonoMethod *callee)
489 char *caller_name = get_method_full_name (caller);
490 char *callee_name = get_method_full_name (callee);
491 char *message = g_strdup_printf (format, caller_name, callee_name);
492 g_free (callee_name);
493 g_free (caller_name);
495 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
496 ex = mono_get_exception_argument ("method", message);
503 * get_field_access_exception
505 * Helper function to create an MonoException (FieldAccessException
506 * in managed-land) and provide a descriptive message for it. This
507 * message is also, optionally, being logged (export
508 * MONO_LOG_MASK="security") for debugging purposes.
510 static MonoException*
511 get_field_access_exception (const char *format, MonoMethod *caller, MonoClassField *field)
514 char *caller_name = get_method_full_name (caller);
515 char *field_name = mono_field_full_name (field);
516 char *message = g_strdup_printf (format, caller_name, field_name);
518 g_free (caller_name);
520 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
521 ex = mono_get_exception_field_access_msg (message);
528 * get_method_access_exception
530 * Helper function to create an MonoException (MethodAccessException
531 * in managed-land) and provide a descriptive message for it. This
532 * message is also, optionally, being logged (export
533 * MONO_LOG_MASK="security") for debugging purposes.
535 static MonoException*
536 get_method_access_exception (const char *format, MonoMethod *caller, MonoMethod *callee)
539 char *caller_name = get_method_full_name (caller);
540 char *callee_name = get_method_full_name (callee);
541 char *message = g_strdup_printf (format, caller_name, callee_name);
542 g_free (callee_name);
543 g_free (caller_name);
545 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
546 ex = mono_get_exception_method_access_msg (message);
553 * mono_security_core_clr_ensure_reflection_access_field:
555 * Ensure that the specified field can be used with reflection since
556 * Transparent code cannot access to Critical fields and can only use
557 * them if they are visible from it's point of view.
559 * A FieldAccessException is thrown if the field is cannot be accessed.
562 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
564 MonoMethod *caller = get_reflection_caller ();
565 /* CoreCLR restrictions applies to Transparent code/caller */
566 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
569 if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION) {
570 if (!mono_security_core_clr_is_platform_image (mono_field_get_parent(field)->image))
574 /* Transparent code cannot [get|set]value on Critical fields */
575 if (mono_security_core_clr_class_level (mono_field_get_parent (field)) == MONO_SECURITY_CORE_CLR_CRITICAL) {
576 mono_raise_exception (get_field_access_exception (
577 "Transparent method %s cannot get or set Critical field %s.",
581 /* also it cannot access a fields that is not visible from it's (caller) point of view */
582 if (!check_field_access (caller, field)) {
583 mono_raise_exception (get_field_access_exception (
584 "Transparent method %s cannot get or set private/internal field %s.",
590 * mono_security_core_clr_ensure_reflection_access_method:
592 * Ensure that the specified method can be used with reflection since
593 * Transparent code cannot call Critical methods and can only call them
594 * if they are visible from it's point of view.
596 * A MethodAccessException is thrown if the field is cannot be accessed.
599 mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
601 MonoMethod *caller = get_reflection_caller ();
602 /* CoreCLR restrictions applies to Transparent code/caller */
603 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
606 if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION) {
607 if (!mono_security_core_clr_is_platform_image (method->klass->image))
611 /* Transparent code cannot invoke, even using reflection, Critical code */
612 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
613 mono_raise_exception (get_method_access_exception (
614 "Transparent method %s cannot invoke Critical method %s.",
618 /* also it cannot invoke a method that is not visible from it's (caller) point of view */
619 if (!check_method_access (caller, method)) {
620 mono_raise_exception (get_method_access_exception (
621 "Transparent method %s cannot invoke private/internal method %s.",
627 * can_avoid_corlib_reflection_delegate_optimization:
629 * Mono's mscorlib use delegates to optimize PropertyInfo and EventInfo
630 * reflection calls. This requires either a bunch of additional, and not
631 * really required, [SecuritySafeCritical] in the class libraries or
632 * (like this) a way to skip them. As a bonus we also avoid the stack
633 * walk to find the caller.
635 * Return TRUE if we can skip this "internal" delegate creation, FALSE
639 can_avoid_corlib_reflection_delegate_optimization (MonoMethod *method)
641 if (!mono_security_core_clr_is_platform_image (method->klass->image))
644 if (strcmp (method->klass->name_space, "System.Reflection") != 0)
647 if (strcmp (method->klass->name, "MonoProperty") == 0) {
648 if ((strcmp (method->name, "GetterAdapterFrame") == 0) || strcmp (method->name, "StaticGetterAdapterFrame") == 0)
650 } else if (strcmp (method->klass->name, "EventInfo") == 0) {
651 if ((strcmp (method->name, "AddEventFrame") == 0) || strcmp (method->name, "StaticAddEventAdapterFrame") == 0)
659 * mono_security_core_clr_ensure_delegate_creation:
661 * Return TRUE if a delegate can be created on the specified method.
662 * CoreCLR also affect the binding, so throwOnBindFailure must be
663 * FALSE to let this function return (FALSE) normally, otherwise (if
664 * throwOnBindFailure is TRUE) it will throw an ArgumentException.
666 * A MethodAccessException is thrown if the specified method is not
667 * visible from the caller point of view.
670 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean throwOnBindFailure)
674 /* note: mscorlib creates delegates to avoid reflection (optimization), we ignore those cases */
675 if (can_avoid_corlib_reflection_delegate_optimization (method))
678 caller = get_reflection_caller ();
679 /* if the "real" caller is not Transparent then it do can anything */
680 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
683 /* otherwise it (as a Transparent caller) cannot create a delegate on a Critical method... */
684 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
685 /* but this throws only if 'throwOnBindFailure' is TRUE */
686 if (!throwOnBindFailure)
689 mono_raise_exception (get_argument_exception (
690 "Transparent method %s cannot create a delegate on Critical method %s.",
694 if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_DELEGATE) {
695 if (!mono_security_core_clr_is_platform_image (method->klass->image))
699 /* also it cannot create the delegate on a method that is not visible from it's (caller) point of view */
700 if (!check_method_access (caller, method)) {
701 mono_raise_exception (get_method_access_exception (
702 "Transparent method %s cannot create a delegate on private/internal method %s.",
710 * mono_security_core_clr_ensure_dynamic_method_resolved_object:
712 * Called from mono_reflection_create_dynamic_method (reflection.c) to add some extra checks required for CoreCLR.
713 * Dynamic methods needs to check to see if the objects being used (e.g. methods, fields) comes from platform code
714 * and do an accessibility check in this case. Otherwise (i.e. user/application code) can be used without this extra
715 * accessbility check.
718 mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
720 /* XXX find/create test cases for other handle_class XXX */
721 if (handle_class == mono_defaults.fieldhandle_class) {
722 MonoClassField *field = (MonoClassField*) ref;
723 MonoClass *klass = mono_field_get_parent (field);
724 /* fields coming from platform code have extra protection (accessibility check) */
725 if (mono_security_core_clr_is_platform_image (klass->image)) {
726 MonoMethod *caller = get_reflection_caller ();
727 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
728 if (!check_field_access (caller, field)) {
729 return get_field_access_exception (
730 "Dynamic method %s cannot create access private/internal field %s.",
734 } else if (handle_class == mono_defaults.methodhandle_class) {
735 MonoMethod *method = (MonoMethod*) ref;
736 /* methods coming from platform code have extra protection (accessibility check) */
737 if (mono_security_core_clr_is_platform_image (method->klass->image)) {
738 MonoMethod *caller = get_reflection_caller ();
739 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
740 if (!check_method_access (caller, method)) {
741 return get_method_access_exception (
742 "Dynamic method %s cannot create access private/internal method %s.",
751 * mono_security_core_clr_can_access_internals
753 * Check if we allow [InternalsVisibleTo] to work between two images.
756 mono_security_core_clr_can_access_internals (MonoImage *accessing, MonoImage* accessed)
758 /* are we trying to access internals of a platform assembly ? if not this is acceptable */
759 if (!mono_security_core_clr_is_platform_image (accessed))
762 /* we can't let everyone with the right name and public key token access the internals of platform code.
763 * (Silverlight can rely on the strongname signature of the assemblies, but Mono does not verify them)
764 * However platform code is fully trusted so it can access the internals of other platform code assemblies */
765 if (mono_security_core_clr_is_platform_image (accessing))
768 /* catch-22: System.Xml needs access to mscorlib's internals (e.g. ArrayList) but is not considered platform code.
769 * Promoting it to platform code would create another issue since (both Mono/Moonlight or MS version of)
770 * System.Xml.Linq.dll (an SDK, not platform, assembly) needs access to System.Xml.dll internals (either ).
771 * The solution is to trust, even transparent code, in the plugin directory to access platform code internals */
772 if (!accessed->assembly->basedir || !accessing->assembly->basedir)
774 return (strcmp (accessed->assembly->basedir, accessing->assembly->basedir) == 0);
778 * mono_security_core_clr_is_field_access_allowed
780 * Return a MonoException (FieldccessException in managed-land) if
781 * the access from "caller" to "field" is not valid under CoreCLR -
782 * i.e. a [SecurityTransparent] method calling a [SecurityCritical]
786 mono_security_core_clr_is_field_access_allowed (MonoMethod *caller, MonoClassField *field)
788 /* there's no restriction to access Transparent or SafeCritical fields, so we only check calls to Critical methods */
789 if (mono_security_core_clr_class_level (mono_field_get_parent (field)) != MONO_SECURITY_CORE_CLR_CRITICAL)
792 /* caller is Critical! only SafeCritical and Critical callers can access the field, so we throw if caller is Transparent */
793 if (!caller || (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT))
796 return get_field_access_exception (
797 "Transparent method %s cannot call use Critical field %s.",
802 * mono_security_core_clr_is_call_allowed
804 * Return a MonoException (MethodAccessException in managed-land) if
805 * the call from "caller" to "callee" is not valid under CoreCLR -
806 * i.e. a [SecurityTransparent] method calling a [SecurityCritical]
810 mono_security_core_clr_is_call_allowed (MonoMethod *caller, MonoMethod *callee)
812 /* there's no restriction to call Transparent or SafeCritical code, so we only check calls to Critical methods */
813 if (mono_security_core_clr_method_level (callee, TRUE) != MONO_SECURITY_CORE_CLR_CRITICAL)
816 /* callee is Critical! only SafeCritical and Critical callers can call it, so we throw if the caller is Transparent */
817 if (!caller || (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT))
820 return get_method_access_exception (
821 "Transparent method %s cannot call Critical method %s.",
826 * mono_security_core_clr_level_from_cinfo:
828 * Return the MonoSecurityCoreCLRLevel that match the attribute located
829 * in the specified custom attributes. If no attribute is present it
830 * defaults to MONO_SECURITY_CORE_CLR_TRANSPARENT, which is the default
831 * level for all code under the CoreCLR.
833 static MonoSecurityCoreCLRLevel
834 mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo *cinfo, MonoImage *image)
836 int level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
838 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_safe_critical_attribute ()))
839 level = MONO_SECURITY_CORE_CLR_SAFE_CRITICAL;
840 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_critical_attribute ()))
841 level = MONO_SECURITY_CORE_CLR_CRITICAL;
847 * mono_security_core_clr_class_level_no_platform_check:
849 * Return the MonoSecurityCoreCLRLevel for the specified class, without
850 * checking for platform code. This help us avoid multiple redundant
852 * - a check for the method and one for the class;
853 * - a check for the class and outer class(es) ...
855 static MonoSecurityCoreCLRLevel
856 mono_security_core_clr_class_level_no_platform_check (MonoClass *class)
858 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
859 MonoCustomAttrInfo *cinfo = mono_custom_attrs_from_class (class);
861 level = mono_security_core_clr_level_from_cinfo (cinfo, class->image);
862 mono_custom_attrs_free (cinfo);
865 if (level == MONO_SECURITY_CORE_CLR_TRANSPARENT && class->nested_in)
866 level = mono_security_core_clr_class_level_no_platform_check (class->nested_in);
872 * mono_security_core_clr_class_level:
874 * Return the MonoSecurityCoreCLRLevel for the specified class.
876 MonoSecurityCoreCLRLevel
877 mono_security_core_clr_class_level (MonoClass *class)
879 /* non-platform code is always Transparent - whatever the attributes says */
880 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (class->image))
881 return MONO_SECURITY_CORE_CLR_TRANSPARENT;
883 return mono_security_core_clr_class_level_no_platform_check (class);
887 * mono_security_core_clr_method_level:
889 * Return the MonoSecurityCoreCLRLevel for the specified method.
890 * If with_class_level is TRUE then the type (class) will also be
891 * checked, otherwise this will only report the information about
894 MonoSecurityCoreCLRLevel
895 mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
897 MonoCustomAttrInfo *cinfo;
898 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
900 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
904 /* non-platform code is always Transparent - whatever the attributes says */
905 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (method->klass->image))
908 cinfo = mono_custom_attrs_from_method (method);
910 level = mono_security_core_clr_level_from_cinfo (cinfo, method->klass->image);
911 mono_custom_attrs_free (cinfo);
914 if (with_class_level && level == MONO_SECURITY_CORE_CLR_TRANSPARENT)
915 level = mono_security_core_clr_class_level (method->klass);
921 * mono_security_core_clr_is_platform_image:
923 * Return the (cached) boolean value indicating if this image represent platform code
926 mono_security_core_clr_is_platform_image (MonoImage *image)
928 return image->core_clr_platform_code;
932 * default_platform_check:
934 * Default platform check. Always TRUE for current corlib (minimum
935 * trust-able subset) otherwise return FALSE. Any real CoreCLR host
936 * should provide its own callback to define platform code (i.e.
937 * this default is meant for test only).
940 default_platform_check (const char *image_name)
942 if (mono_defaults.corlib) {
943 return (strcmp (mono_defaults.corlib->name, image_name) == 0);
945 /* this can get called even before we load corlib (e.g. the EXE itself) */
946 const char *corlib = "mscorlib.dll";
947 int ilen = strlen (image_name);
948 int clen = strlen (corlib);
949 return ((ilen >= clen) && (strcmp ("mscorlib.dll", image_name + ilen - clen) == 0));
953 static MonoCoreClrPlatformCB platform_callback = default_platform_check;
956 * mono_security_core_clr_determine_platform_image:
958 * Call the supplied callback (from mono_security_set_core_clr_platform_callback)
959 * to determine if this image represents platform code.
962 mono_security_core_clr_determine_platform_image (MonoImage *image)
964 return platform_callback (image->name);
968 * mono_security_enable_core_clr:
970 * Enable the verifier and the CoreCLR security model
973 mono_security_enable_core_clr ()
975 mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
976 mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
980 * mono_security_set_core_clr_platform_callback:
982 * Set the callback function that will be used to determine if an image
983 * is part, or not, of the platform code.
986 mono_security_set_core_clr_platform_callback (MonoCoreClrPlatformCB callback)
988 platform_callback = callback;