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");
52 * set_type_load_exception_type
54 * Set MONO_EXCEPTION_TYPE_LOAD on the specified 'class' and provide
55 * a descriptive message for the exception. This message is also,
56 * optionally, being logged (export MONO_LOG_MASK="security") for
60 set_type_load_exception_type (const char *format, MonoClass *class)
62 char *type_name = mono_type_get_full_name (class);
63 char *parent_name = mono_type_get_full_name (class->parent);
64 char *message = g_strdup_printf (format, type_name, parent_name);
69 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
70 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, message);
71 // note: do not free string given to mono_class_set_failure
75 * set_type_load_exception_methods
77 * Set MONO_EXCEPTION_TYPE_LOAD on the 'override' class and provide
78 * a descriptive message for the exception. This message is also,
79 * optionally, being logged (export MONO_LOG_MASK="security") for
83 set_type_load_exception_methods (const char *format, MonoMethod *override, MonoMethod *base)
85 char *method_name = mono_method_full_name (override, TRUE);
86 char *base_name = mono_method_full_name (base, TRUE);
87 char *message = g_strdup_printf (format, method_name, base_name);
92 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
93 mono_class_set_failure (override->klass, MONO_EXCEPTION_TYPE_LOAD, message);
94 // note: do not free string given to mono_class_set_failure
97 /* MonoClass is not fully initialized (inited is not yet == 1) when we
98 * check the inheritance rules so we need to look for the default ctor
99 * ourselve to avoid recursion (and aborting)
102 get_default_ctor (MonoClass *klass)
106 mono_class_setup_methods (klass);
110 for (i = 0; i < klass->method.count; ++i) {
111 MonoMethodSignature *sig;
112 MonoMethod *method = klass->methods [i];
114 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) == 0)
116 if ((method->name[0] != '.') || strcmp (".ctor", method->name))
118 sig = mono_method_signature (method);
119 if (sig && (sig->param_count == 0))
127 * mono_security_core_clr_check_inheritance:
129 * Determine if the specified class can inherit from its parent using
130 * the CoreCLR inheritance rules.
132 * Base Type Allow Derived Type
133 * ------------ ------------------
134 * Transparent Transparent, SafeCritical, Critical
135 * SafeCritical SafeCritical, Critical
138 * Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
140 * Furthermore a class MUST have a default constructor if its base
141 * class has a non-transparent default constructor. The same
142 * inheritance rule applies to both default constructors.
144 * Reference: message from a SecurityException in SL4RC
147 mono_security_core_clr_check_inheritance (MonoClass *class)
149 MonoSecurityCoreCLRLevel class_level, parent_level;
150 MonoClass *parent = class->parent;
155 class_level = mono_security_core_clr_class_level (class);
156 parent_level = mono_security_core_clr_class_level (parent);
158 if (class_level < parent_level) {
159 set_type_load_exception_type (
160 "Inheritance failure for type %s. Parent class %s is more restricted.",
163 class_level = mono_security_core_clr_method_level (get_default_ctor (class), FALSE);
164 parent_level = mono_security_core_clr_method_level (get_default_ctor (parent), FALSE);
165 if (class_level < parent_level) {
166 set_type_load_exception_type (
167 "Inheritance failure for type %s. Default constructor security mismatch with %s.",
174 * mono_security_core_clr_check_override:
176 * Determine if the specified override can "legally" override the
177 * specified base method using the CoreCLR inheritance rules.
179 * Base (virtual/interface) Allowed override
180 * ------------------------ -------------------------
181 * Transparent Transparent, SafeCritical
182 * SafeCritical Transparent, SafeCritical
185 * Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
188 mono_security_core_clr_check_override (MonoClass *class, MonoMethod *override, MonoMethod *base)
190 MonoSecurityCoreCLRLevel base_level = mono_security_core_clr_method_level (base, FALSE);
191 MonoSecurityCoreCLRLevel override_level = mono_security_core_clr_method_level (override, FALSE);
192 /* if the base method is decorated with [SecurityCritical] then the overrided method MUST be too */
193 if (base_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
194 if (override_level != MONO_SECURITY_CORE_CLR_CRITICAL) {
195 set_type_load_exception_methods (
196 "Override failure for %s over %s. Override MUST be [SecurityCritical].",
200 /* base is [SecuritySafeCritical] or [SecurityTransparent], override MUST NOT be [SecurityCritical] */
201 if (override_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
202 set_type_load_exception_methods (
203 "Override failure for %s over %s. Override must NOT be [SecurityCritical].",
210 * get_caller_no_reflection_related:
212 * Find the first managed caller that is either:
213 * (a) located outside the platform code assemblies; or
214 * (b) not related to reflection and delegates
216 * Returns TRUE to stop the stackwalk, FALSE to continue to the next frame.
219 get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
221 MonoMethod **dest = data;
224 /* skip unmanaged frames */
228 if (m->wrapper_type != MONO_WRAPPER_NONE)
231 /* quick out (any namespace not starting with an 'S' */
232 ns = m->klass->name_space;
233 if (!ns || (*ns != 'S')) {
238 /* stop if the method is not part of platform code */
239 if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
244 /* any number of calls inside System.Reflection are allowed */
245 if (strcmp (ns, "System.Reflection") == 0)
248 /* any number of calls inside System.Reflection are allowed */
249 if (strcmp (ns, "System.Reflection.Emit") == 0)
252 /* calls from System.Delegate are also possible and allowed */
253 if (strcmp (ns, "System") == 0) {
254 const char *kname = m->klass->name;
255 if ((*kname == 'A') && (strcmp (kname, "Activator") == 0))
258 /* unlike most Invoke* cases InvokeMember is not inside System.Reflection[.Emit] but is SecuritySafeCritical */
259 if (((*kname == 'T') && (strcmp (kname, "Type") == 0)) ||
260 ((*kname == 'M') && (strcmp (kname, "MonoType")) == 0)) {
262 /* if calling InvokeMember then we can't stop the stackwalk here and need to look at the caller */
263 if (strcmp (m->name, "InvokeMember") == 0)
267 /* the security check on the delegate is made at creation time, not at invoke time */
268 if (((*kname == 'D') && (strcmp (kname, "Delegate") == 0)) ||
269 ((*kname == 'M') && (strcmp (kname, "MulticastDelegate")) == 0)) {
271 /* if we're invoking then we can stop our stack walk */
272 if (strcmp (m->name, "DynamicInvoke") != 0)
287 * get_reflection_caller:
289 * Walk to the first managed method outside:
290 * - System.Reflection* namespaces
291 * - System.[Multicast]Delegate or Activator type
293 * and return a pointer to its MonoMethod.
295 * This is required since CoreCLR checks needs to be done on this "real" caller.
298 get_reflection_caller (void)
300 MonoMethod *m = NULL;
301 mono_stack_walk_no_il (get_caller_no_reflection_related, &m);
302 if (G_UNLIKELY (!m)) {
303 mono_trace (G_LOG_LEVEL_ERROR, MONO_TRACE_SECURITY, "No caller outside reflection was found");
309 * check_field_access:
311 * Return TRUE if the caller method can access the specified field, FALSE otherwise.
314 check_field_access (MonoMethod *caller, MonoClassField *field)
316 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
318 MonoClass *klass = (mono_field_get_flags (field) & FIELD_ATTRIBUTE_STATIC) ? NULL : mono_field_get_parent (field);
319 return mono_method_can_access_field_full (caller, field, klass);
325 * check_method_access:
327 * Return TRUE if the caller method can access the specified callee method, FALSE otherwise.
330 check_method_access (MonoMethod *caller, MonoMethod *callee)
332 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
334 MonoClass *klass = (callee->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : callee->klass;
335 return mono_method_can_access_method_full (caller, callee, klass);
341 * get_argument_exception
343 * Helper function to create an MonoException (ArgumentException in
344 * managed-land) and provide a descriptive message for it. This
345 * message is also, optionally, being logged (export
346 * MONO_LOG_MASK="security") for debugging purposes.
348 static MonoException*
349 get_argument_exception (const char *format, MonoMethod *caller, MonoMethod *callee)
352 char *caller_name = mono_method_full_name (caller, TRUE);
353 char *callee_name = mono_method_full_name (callee, TRUE);
354 char *message = g_strdup_printf (format, caller_name, callee_name);
355 g_free (callee_name);
356 g_free (caller_name);
358 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
359 ex = mono_get_exception_argument ("method", message);
366 * get_field_access_exception
368 * Helper function to create an MonoException (FieldAccessException
369 * in managed-land) and provide a descriptive message for it. This
370 * message is also, optionally, being logged (export
371 * MONO_LOG_MASK="security") for debugging purposes.
373 static MonoException*
374 get_field_access_exception (const char *format, MonoMethod *caller, MonoClassField *field)
377 char *caller_name = mono_method_full_name (caller, TRUE);
378 char *field_name = mono_field_full_name (field);
379 char *message = g_strdup_printf (format, caller_name, field_name);
381 g_free (caller_name);
383 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
384 ex = mono_get_exception_field_access_msg (message);
391 * get_method_access_exception
393 * Helper function to create an MonoException (MethodAccessException
394 * in managed-land) and provide a descriptive message for it. This
395 * message is also, optionally, being logged (export
396 * MONO_LOG_MASK="security") for debugging purposes.
398 static MonoException*
399 get_method_access_exception (const char *format, MonoMethod *caller, MonoMethod *callee)
402 char *caller_name = mono_method_full_name (caller, TRUE);
403 char *callee_name = mono_method_full_name (callee, TRUE);
404 char *message = g_strdup_printf (format, caller_name, callee_name);
405 g_free (callee_name);
406 g_free (caller_name);
408 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, message);
409 ex = mono_get_exception_method_access_msg (message);
416 * mono_security_core_clr_ensure_reflection_access_field:
418 * Ensure that the specified field can be used with reflection since
419 * Transparent code cannot access to Critical fields and can only use
420 * them if they are visible from it's point of view.
422 * A FieldAccessException is thrown if the field is cannot be accessed.
425 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
427 MonoMethod *caller = get_reflection_caller ();
428 /* CoreCLR restrictions applies to Transparent code/caller */
429 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
432 /* Transparent code cannot [get|set]value on Critical fields */
433 if (mono_security_core_clr_class_level (mono_field_get_parent (field)) == MONO_SECURITY_CORE_CLR_CRITICAL) {
434 mono_raise_exception (get_field_access_exception (
435 "Transparent method %s cannot get or set Critical field %s.",
439 /* also it cannot access a fields that is not visible from it's (caller) point of view */
440 if (!check_field_access (caller, field)) {
441 mono_raise_exception (get_field_access_exception (
442 "Transparent method %s cannot get or set private/internal field %s.",
448 * mono_security_core_clr_ensure_reflection_access_method:
450 * Ensure that the specified method can be used with reflection since
451 * Transparent code cannot call Critical methods and can only call them
452 * if they are visible from it's point of view.
454 * A MethodAccessException is thrown if the field is cannot be accessed.
457 mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
459 MonoMethod *caller = get_reflection_caller ();
460 /* CoreCLR restrictions applies to Transparent code/caller */
461 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
464 /* Transparent code cannot invoke, even using reflection, Critical code */
465 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
466 mono_raise_exception (get_method_access_exception (
467 "Transparent method %s cannot invoke Critical method %s.",
471 /* also it cannot invoke a method that is not visible from it's (caller) point of view */
472 if (!check_method_access (caller, method)) {
473 mono_raise_exception (get_method_access_exception (
474 "Transparent method %s cannot invoke private/internal method %s.",
480 * can_avoid_corlib_reflection_delegate_optimization:
482 * Mono's mscorlib use delegates to optimize PropertyInfo and EventInfo
483 * reflection calls. This requires either a bunch of additional, and not
484 * really required, [SecuritySafeCritical] in the class libraries or
485 * (like this) a way to skip them. As a bonus we also avoid the stack
486 * walk to find the caller.
488 * Return TRUE if we can skip this "internal" delegate creation, FALSE
492 can_avoid_corlib_reflection_delegate_optimization (MonoMethod *method)
494 if (!mono_security_core_clr_is_platform_image (method->klass->image))
497 if (strcmp (method->klass->name_space, "System.Reflection") != 0)
500 if (strcmp (method->klass->name, "MonoProperty") == 0) {
501 if ((strcmp (method->name, "GetterAdapterFrame") == 0) || strcmp (method->name, "StaticGetterAdapterFrame") == 0)
503 } else if (strcmp (method->klass->name, "EventInfo") == 0) {
504 if ((strcmp (method->name, "AddEventFrame") == 0) || strcmp (method->name, "StaticAddEventAdapterFrame") == 0)
512 * mono_security_core_clr_ensure_delegate_creation:
514 * Return TRUE if a delegate can be created on the specified method.
515 * CoreCLR also affect the binding, so throwOnBindFailure must be
516 * FALSE to let this function return (FALSE) normally, otherwise (if
517 * throwOnBindFailure is TRUE) it will throw an ArgumentException.
519 * A MethodAccessException is thrown if the specified method is not
520 * visible from the caller point of view.
523 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean throwOnBindFailure)
527 /* note: mscorlib creates delegates to avoid reflection (optimization), we ignore those cases */
528 if (can_avoid_corlib_reflection_delegate_optimization (method))
531 caller = get_reflection_caller ();
532 /* if the "real" caller is not Transparent then it do can anything */
533 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
536 /* otherwise it (as a Transparent caller) cannot create a delegate on a Critical method... */
537 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
538 /* but this throws only if 'throwOnBindFailure' is TRUE */
539 if (!throwOnBindFailure)
542 mono_raise_exception (get_argument_exception (
543 "Transparent method %s cannot create a delegate on Critical method %s.",
547 /* also it cannot create the delegate on a method that is not visible from it's (caller) point of view */
548 if (!check_method_access (caller, method)) {
549 mono_raise_exception (get_method_access_exception (
550 "Transparent method %s cannot create a delegate on private/internal method %s.",
558 * mono_security_core_clr_ensure_dynamic_method_resolved_object:
560 * Called from mono_reflection_create_dynamic_method (reflection.c) to add some extra checks required for CoreCLR.
561 * Dynamic methods needs to check to see if the objects being used (e.g. methods, fields) comes from platform code
562 * and do an accessibility check in this case. Otherwise (i.e. user/application code) can be used without this extra
563 * accessbility check.
566 mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
568 /* XXX find/create test cases for other handle_class XXX */
569 if (handle_class == mono_defaults.fieldhandle_class) {
570 MonoClassField *field = (MonoClassField*) ref;
571 MonoClass *klass = mono_field_get_parent (field);
572 /* fields coming from platform code have extra protection (accessibility check) */
573 if (mono_security_core_clr_is_platform_image (klass->image)) {
574 MonoMethod *caller = get_reflection_caller ();
575 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
576 if (!check_field_access (caller, field)) {
577 return get_field_access_exception (
578 "Dynamic method %s cannot create access private/internal field %s.",
582 } else if (handle_class == mono_defaults.methodhandle_class) {
583 MonoMethod *method = (MonoMethod*) ref;
584 /* methods coming from platform code have extra protection (accessibility check) */
585 if (mono_security_core_clr_is_platform_image (method->klass->image)) {
586 MonoMethod *caller = get_reflection_caller ();
587 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
588 if (!check_method_access (caller, method)) {
589 return get_method_access_exception (
590 "Dynamic method %s cannot create access private/internal method %s.",
599 * mono_security_core_clr_can_access_internals
601 * Check if we allow [InternalsVisibleTo] to work between two images.
604 mono_security_core_clr_can_access_internals (MonoImage *accessing, MonoImage* accessed)
606 /* are we trying to access internals of a platform assembly ? if not this is acceptable */
607 if (!mono_security_core_clr_is_platform_image (accessed))
610 /* we can't let everyone with the right name and public key token access the internals of platform code.
611 * (Silverlight can rely on the strongname signature of the assemblies, but Mono does not verify them)
612 * However platform code is fully trusted so it can access the internals of other platform code assemblies */
613 if (mono_security_core_clr_is_platform_image (accessing))
616 /* catch-22: System.Xml needs access to mscorlib's internals (e.g. ArrayList) but is not considered platform code.
617 * Promoting it to platform code would create another issue since (both Mono/Moonlight or MS version of)
618 * System.Xml.Linq.dll (an SDK, not platform, assembly) needs access to System.Xml.dll internals (either ).
619 * The solution is to trust, even transparent code, in the plugin directory to access platform code internals */
620 if (!accessed->assembly->basedir || !accessing->assembly->basedir)
622 return (strcmp (accessed->assembly->basedir, accessing->assembly->basedir) == 0);
626 * mono_security_core_clr_is_field_access_allowed
628 * Return a MonoException (FieldccessException in managed-land) if
629 * the access from "caller" to "field" is not valid under CoreCLR -
630 * i.e. a [SecurityTransparent] method calling a [SecurityCritical]
634 mono_security_core_clr_is_field_access_allowed (MonoMethod *caller, MonoClassField *field)
636 /* there's no restriction to access Transparent or SafeCritical fields, so we only check calls to Critical methods */
637 if (mono_security_core_clr_class_level (mono_field_get_parent (field)) != MONO_SECURITY_CORE_CLR_CRITICAL)
640 /* caller is Critical! only SafeCritical and Critical callers can access the field, so we throw if caller is Transparent */
641 if (!caller || (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT))
644 return get_field_access_exception (
645 "Transparent method %s cannot call use Critical field %s.",
650 * mono_security_core_clr_is_call_allowed
652 * Return a MonoException (MethodAccessException in managed-land) if
653 * the call from "caller" to "callee" is not valid under CoreCLR -
654 * i.e. a [SecurityTransparent] method calling a [SecurityCritical]
658 mono_security_core_clr_is_call_allowed (MonoMethod *caller, MonoMethod *callee)
660 /* there's no restriction to call Transparent or SafeCritical code, so we only check calls to Critical methods */
661 if (mono_security_core_clr_method_level (callee, TRUE) != MONO_SECURITY_CORE_CLR_CRITICAL)
664 /* callee is Critical! only SafeCritical and Critical callers can call it, so we throw if the caller is Transparent */
665 if (!caller || (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT))
668 return get_method_access_exception (
669 "Transparent method %s cannot call Critical method %s.",
674 * mono_security_core_clr_level_from_cinfo:
676 * Return the MonoSecurityCoreCLRLevel that match the attribute located
677 * in the specified custom attributes. If no attribute is present it
678 * defaults to MONO_SECURITY_CORE_CLR_TRANSPARENT, which is the default
679 * level for all code under the CoreCLR.
681 static MonoSecurityCoreCLRLevel
682 mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo *cinfo, MonoImage *image)
684 int level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
686 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_safe_critical_attribute ()))
687 level = MONO_SECURITY_CORE_CLR_SAFE_CRITICAL;
688 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_critical_attribute ()))
689 level = MONO_SECURITY_CORE_CLR_CRITICAL;
695 * mono_security_core_clr_class_level_no_platform_check:
697 * Return the MonoSecurityCoreCLRLevel for the specified class, without
698 * checking for platform code. This help us avoid multiple redundant
700 * - a check for the method and one for the class;
701 * - a check for the class and outer class(es) ...
703 static MonoSecurityCoreCLRLevel
704 mono_security_core_clr_class_level_no_platform_check (MonoClass *class)
706 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
707 MonoCustomAttrInfo *cinfo = mono_custom_attrs_from_class (class);
709 level = mono_security_core_clr_level_from_cinfo (cinfo, class->image);
710 mono_custom_attrs_free (cinfo);
713 if (level == MONO_SECURITY_CORE_CLR_TRANSPARENT && class->nested_in)
714 level = mono_security_core_clr_class_level_no_platform_check (class->nested_in);
720 * mono_security_core_clr_class_level:
722 * Return the MonoSecurityCoreCLRLevel for the specified class.
724 MonoSecurityCoreCLRLevel
725 mono_security_core_clr_class_level (MonoClass *class)
727 /* non-platform code is always Transparent - whatever the attributes says */
728 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (class->image))
729 return MONO_SECURITY_CORE_CLR_TRANSPARENT;
731 return mono_security_core_clr_class_level_no_platform_check (class);
735 * mono_security_core_clr_method_level:
737 * Return the MonoSecurityCoreCLRLevel for the specified method.
738 * If with_class_level is TRUE then the type (class) will also be
739 * checked, otherwise this will only report the information about
742 MonoSecurityCoreCLRLevel
743 mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
745 MonoCustomAttrInfo *cinfo;
746 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
748 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
752 /* non-platform code is always Transparent - whatever the attributes says */
753 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (method->klass->image))
756 cinfo = mono_custom_attrs_from_method (method);
758 level = mono_security_core_clr_level_from_cinfo (cinfo, method->klass->image);
759 mono_custom_attrs_free (cinfo);
762 if (with_class_level && level == MONO_SECURITY_CORE_CLR_TRANSPARENT)
763 level = mono_security_core_clr_class_level (method->klass);
769 * mono_security_core_clr_is_platform_image:
771 * Return the (cached) boolean value indicating if this image represent platform code
774 mono_security_core_clr_is_platform_image (MonoImage *image)
776 return image->core_clr_platform_code;
780 * default_platform_check:
782 * Default platform check. Always TRUE for current corlib (minimum
783 * trust-able subset) otherwise return FALSE. Any real CoreCLR host
784 * should provide its own callback to define platform code (i.e.
785 * this default is meant for test only).
788 default_platform_check (const char *image_name)
790 if (mono_defaults.corlib) {
791 return (strcmp (mono_defaults.corlib->name, image_name) == 0);
793 /* this can get called even before we load corlib (e.g. the EXE itself) */
794 const char *corlib = "mscorlib.dll";
795 int ilen = strlen (image_name);
796 int clen = strlen (corlib);
797 return ((ilen >= clen) && (strcmp ("mscorlib.dll", image_name + ilen - clen) == 0));
801 static MonoCoreClrPlatformCB platform_callback = default_platform_check;
804 * mono_security_core_clr_determine_platform_image:
806 * Call the supplied callback (from mono_security_set_core_clr_platform_callback)
807 * to determine if this image represents platform code.
810 mono_security_core_clr_determine_platform_image (MonoImage *image)
812 return platform_callback (image->name);
816 * mono_security_enable_core_clr:
818 * Enable the verifier and the CoreCLR security model
821 mono_security_enable_core_clr ()
823 mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
824 mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
828 * mono_security_set_core_clr_platform_callback:
830 * Set the callback function that will be used to determine if an image
831 * is part, or not, of the platform code.
834 mono_security_set_core_clr_platform_callback (MonoCoreClrPlatformCB callback)
836 platform_callback = callback;