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)
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12 #include <mono/metadata/class-internals.h>
13 #include <mono/metadata/security-manager.h>
14 #include <mono/metadata/assembly.h>
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/verify-internals.h>
17 #include <mono/metadata/object.h>
18 #include <mono/metadata/exception.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/utils/mono-logger-internals.h>
22 #include "security-core-clr.h"
24 gboolean mono_security_core_clr_test = FALSE;
26 static MonoSecurityCoreCLROptions security_core_clr_options = MONO_SECURITY_CORE_CLR_OPTIONS_DEFAULT;
29 * mono_security_core_clr_set_options:
30 * @options: the new options for the coreclr system to use
32 * By default, the CoreCLRs security model forbids execution trough reflection of methods not visible from the calling code.
33 * Even if the method being called is not in a platform assembly. For non moonlight CoreCLR users this restriction does not
34 * make a lot of sense, since the author could have just changed the non platform assembly to allow the method to be called.
35 * This function allows specific relaxations from the default behaviour to be set.
37 * Use MONO_SECURITY_CORE_CLR_OPTIONS_DEFAULT for the default coreclr coreclr behaviour as used in Moonlight.
39 * Use MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION to allow transparent code to execute methods and access
40 * fields that are not in platformcode, even if those methods and fields are private or otherwise not visible to the calling code.
42 * Use MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_DELEGATE to allow delegates to be created that point at methods that are not in
43 * platformcode even if those methods and fields are private or otherwise not visible to the calling code.
47 mono_security_core_clr_set_options (MonoSecurityCoreCLROptions options) {
48 security_core_clr_options = options;
52 * mono_security_core_clr_get_options:
54 * Retrieves the current options used by the coreclr system.
57 MonoSecurityCoreCLROptions
58 mono_security_core_clr_get_options ()
60 return security_core_clr_options;
64 * default_platform_check:
66 * Default platform check. Always TRUE for current corlib (minimum
67 * trust-able subset) otherwise return FALSE. Any real CoreCLR host
68 * should provide its own callback to define platform code (i.e.
69 * this default is meant for test only).
72 default_platform_check (const char *image_name)
74 if (mono_defaults.corlib) {
75 return (strcmp (mono_defaults.corlib->name, image_name) == 0);
77 /* this can get called even before we load corlib (e.g. the EXE itself) */
78 const char *corlib = "mscorlib.dll";
79 int ilen = strlen (image_name);
80 int clen = strlen (corlib);
81 return ((ilen >= clen) && (strcmp ("mscorlib.dll", image_name + ilen - clen) == 0));
85 static MonoCoreClrPlatformCB platform_callback = default_platform_check;
88 * mono_security_core_clr_determine_platform_image:
90 * Call the supplied callback (from mono_security_set_core_clr_platform_callback)
91 * to determine if this image represents platform code.
94 mono_security_core_clr_determine_platform_image (MonoImage *image)
96 return platform_callback (image->name);
100 * mono_security_set_core_clr_platform_callback:
102 * Set the callback function that will be used to determine if an image
103 * is part, or not, of the platform code.
106 mono_security_set_core_clr_platform_callback (MonoCoreClrPlatformCB callback)
108 platform_callback = callback;
112 * mono_security_core_clr_is_platform_image:
114 * Return the (cached) boolean value indicating if this image represent platform code
117 mono_security_core_clr_is_platform_image (MonoImage *image)
119 return image->core_clr_platform_code;
122 /* Note: The above functions are outside this guard so that the public API isn't affected. */
124 #ifndef DISABLE_SECURITY
126 /* Class lazy loading functions */
127 static GENERATE_GET_CLASS_WITH_CACHE (security_critical, System.Security, SecurityCriticalAttribute)
128 static GENERATE_GET_CLASS_WITH_CACHE (security_safe_critical, System.Security, SecuritySafeCriticalAttribute)
131 security_critical_attribute (void)
133 return mono_class_get_security_critical_class ();
137 security_safe_critical_attribute (void)
139 return mono_class_get_security_safe_critical_class ();
143 /* sometime we get a NULL (not found) caller (e.g. get_reflection_caller) */
145 get_method_full_name (MonoMethod * method)
147 return method ? mono_method_full_name (method, TRUE) : g_strdup ("'no caller found'");
151 * set_type_load_exception_type
153 * Set MONO_EXCEPTION_TYPE_LOAD on the specified 'class' and provide
154 * a descriptive message for the exception. This message is also,
155 * optionally, being logged (export MONO_LOG_MASK="security") for
156 * debugging purposes.
159 set_type_load_exception_type (const char *format, MonoClass *klass)
161 char *type_name = mono_type_get_full_name (klass);
162 char *parent_name = mono_type_get_full_name (klass->parent);
163 char *message = mono_image_strdup_printf (klass->image, format, type_name, parent_name);
165 g_free (parent_name);
168 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, "%s", message);
169 mono_class_set_type_load_failure (klass, "%s", message);
170 // note: do not free string given to mono_class_set_failure
174 * set_type_load_exception_methods
176 * Set MONO_EXCEPTION_TYPE_LOAD on the 'override' class and provide
177 * a descriptive message for the exception. This message is also,
178 * optionally, being logged (export MONO_LOG_MASK="security") for
179 * debugging purposes.
182 set_type_load_exception_methods (const char *format, MonoMethod *override, MonoMethod *base)
184 char *method_name = get_method_full_name (override);
185 char *base_name = get_method_full_name (base);
186 char *message = mono_image_strdup_printf (override->klass->image, format, method_name, base_name);
189 g_free (method_name);
191 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, "%s", message);
192 mono_class_set_type_load_failure (override->klass, "%s", message);
193 // note: do not free string given to mono_class_set_failure
196 /* MonoClass is not fully initialized (inited is not yet == 1) when we
197 * check the inheritance rules so we need to look for the default ctor
198 * ourselve to avoid recursion (and aborting)
201 get_default_ctor (MonoClass *klass)
205 mono_class_setup_methods (klass);
209 for (i = 0; i < klass->method.count; ++i) {
210 MonoMethodSignature *sig;
211 MonoMethod *method = klass->methods [i];
216 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) == 0)
218 if ((method->name[0] != '.') || strcmp (".ctor", method->name))
220 sig = mono_method_signature (method);
221 if (sig && (sig->param_count == 0))
229 * mono_security_core_clr_check_inheritance:
231 * Determine if the specified class can inherit from its parent using
232 * the CoreCLR inheritance rules.
234 * Base Type Allow Derived Type
235 * ------------ ------------------
236 * Transparent Transparent, SafeCritical, Critical
237 * SafeCritical SafeCritical, Critical
240 * Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
242 * Furthermore a class MUST have a default constructor if its base
243 * class has a non-transparent, public or protected, default constructor.
244 * The same inheritance rule applies to both default constructors.
246 * Reference: message from a SecurityException in SL4RC
247 * Reference: fxcop CA2132 rule
250 mono_security_core_clr_check_inheritance (MonoClass *klass)
252 MonoSecurityCoreCLRLevel class_level, parent_level;
253 MonoClass *parent = klass->parent;
258 class_level = mono_security_core_clr_class_level (klass);
259 parent_level = mono_security_core_clr_class_level (parent);
261 if (class_level < parent_level) {
262 set_type_load_exception_type (
263 "Inheritance failure for type %s. Parent class %s is more restricted.",
266 MonoMethod *parent_ctor = get_default_ctor (parent);
267 if (parent_ctor && ((parent_ctor->flags & METHOD_ATTRIBUTE_PUBLIC) != 0)) {
268 class_level = mono_security_core_clr_method_level (get_default_ctor (klass), FALSE);
269 parent_level = mono_security_core_clr_method_level (parent_ctor, FALSE);
270 if (class_level < parent_level) {
271 set_type_load_exception_type (
272 "Inheritance failure for type %s. Default constructor security mismatch with %s.",
280 * mono_security_core_clr_check_override:
282 * Determine if the specified override can "legally" override the
283 * specified base method using the CoreCLR inheritance rules.
285 * Base (virtual/interface) Allowed override
286 * ------------------------ -------------------------
287 * Transparent Transparent, SafeCritical
288 * SafeCritical Transparent, SafeCritical
291 * Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
294 mono_security_core_clr_check_override (MonoClass *klass, MonoMethod *override, MonoMethod *base)
296 MonoSecurityCoreCLRLevel base_level = mono_security_core_clr_method_level (base, FALSE);
297 MonoSecurityCoreCLRLevel override_level = mono_security_core_clr_method_level (override, FALSE);
298 /* if the base method is decorated with [SecurityCritical] then the overrided method MUST be too */
299 if (base_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
300 if (override_level != MONO_SECURITY_CORE_CLR_CRITICAL) {
301 set_type_load_exception_methods (
302 "Override failure for %s over %s. Override MUST be [SecurityCritical].",
306 /* base is [SecuritySafeCritical] or [SecurityTransparent], override MUST NOT be [SecurityCritical] */
307 if (override_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
308 set_type_load_exception_methods (
309 "Override failure for %s over %s. Override must NOT be [SecurityCritical].",
316 * get_caller_no_reflection_related:
318 * Find the first managed caller that is either:
319 * (a) located outside the platform code assemblies; or
320 * (b) not related to reflection and delegates
322 * Returns TRUE to stop the stackwalk, FALSE to continue to the next frame.
325 get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
327 MonoMethod **dest = (MonoMethod **)data;
330 /* skip unmanaged frames */
334 if (m->wrapper_type != MONO_WRAPPER_NONE)
337 /* quick out (any namespace not starting with an 'S' */
338 ns = m->klass->name_space;
339 if (!ns || (*ns != 'S')) {
344 /* stop if the method is not part of platform code */
345 if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
350 /* any number of calls inside System.Reflection are allowed */
351 if (strcmp (ns, "System.Reflection") == 0)
354 /* any number of calls inside System.Reflection are allowed */
355 if (strcmp (ns, "System.Reflection.Emit") == 0)
358 /* calls from System.Delegate are also possible and allowed */
359 if (strcmp (ns, "System") == 0) {
360 const char *kname = m->klass->name;
361 if ((*kname == 'A') && (strcmp (kname, "Activator") == 0))
364 /* unlike most Invoke* cases InvokeMember is not inside System.Reflection[.Emit] but is SecuritySafeCritical */
365 if (((*kname == 'T') && (strcmp (kname, "Type") == 0)) ||
366 ((*kname == 'R') && (strcmp (kname, "RuntimeType")) == 0)) {
368 /* if calling InvokeMember then we can't stop the stackwalk here and need to look at the caller */
369 if (strcmp (m->name, "InvokeMember") == 0)
373 /* the security check on the delegate is made at creation time, not at invoke time */
374 if (((*kname == 'D') && (strcmp (kname, "Delegate") == 0)) ||
375 ((*kname == 'M') && (strcmp (kname, "MulticastDelegate")) == 0)) {
377 /* if we're invoking then we can stop our stack walk */
378 if (strcmp (m->name, "DynamicInvoke") != 0)
393 * get_reflection_caller:
395 * Walk to the first managed method outside:
396 * - System.Reflection* namespaces
397 * - System.[Multicast]Delegate or Activator type
399 * and return a pointer to its MonoMethod.
401 * This is required since CoreCLR checks needs to be done on this "real" caller.
404 get_reflection_caller (void)
406 MonoMethod *m = NULL;
407 mono_stack_walk_no_il (get_caller_no_reflection_related, &m);
408 if (G_UNLIKELY (!m)) {
409 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, "No caller outside reflection was found");
417 } ElevatedTrustCookie;
420 * get_caller_of_elevated_trust_code
422 * Stack walk to find who is calling code requiring Elevated Trust.
423 * If a critical method is found then the caller is platform code
424 * and has elevated trust, otherwise (transparent) a check needs to
425 * be done (on the managed side) to determine if the application is
426 * running with elevated permissions.
429 get_caller_of_elevated_trust_code (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
431 ElevatedTrustCookie *cookie = (ElevatedTrustCookie *)data;
433 /* skip unmanaged frames and wrappers */
434 if (!managed || (m->wrapper_type != MONO_WRAPPER_NONE))
437 /* end stack walk if we find ourselves outside platform code (we won't find critical code anymore) */
438 if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
443 switch (cookie->depth) {
444 /* while depth == 0 look for SecurityManager::[Check|Ensure]ElevatedPermissions */
446 if (strcmp (m->klass->name_space, "System.Security"))
448 if (strcmp (m->klass->name, "SecurityManager"))
450 if ((strcmp (m->name, "EnsureElevatedPermissions")) && strcmp (m->name, "CheckElevatedPermissions"))
454 /* while depth == 1 look for the caller to SecurityManager::[Check|Ensure]ElevatedPermissions */
456 /* this frame is [SecuritySafeCritical] because it calls [SecurityCritical] [Check|Ensure]ElevatedPermissions */
457 /* the next frame will contain the caller(s) we want to check */
460 /* while depth >= 2 look for [safe]critical caller, end stack walk if we find it */
463 /* if the caller is transparent then we continue the stack walk */
464 if (mono_security_core_clr_method_level (m, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT)
467 /* Security[Safe]Critical code is always allowed to call elevated-trust code */
476 * mono_security_core_clr_require_elevated_permissions:
478 * Return TRUE if the caller of the current method (the code who
479 * called SecurityManager.get_RequiresElevatedPermissions) needs
480 * elevated trust to perform an action.
482 * A stack walk is done to find the callers. If one of the callers
483 * is either [SecurityCritical] or [SecuritySafeCritical] then the
484 * action is needed for platform code (i.e. no restriction).
485 * Otherwise (transparent) the requested action needs elevated trust
488 mono_security_core_clr_require_elevated_permissions (void)
490 ElevatedTrustCookie cookie;
492 cookie.caller = NULL;
493 mono_stack_walk_no_il (get_caller_of_elevated_trust_code, &cookie);
495 /* return TRUE if the stack walk did not reach far enough or did not find callers */
496 if (!cookie.caller || cookie.depth < 3)
499 /* return TRUE if the caller is transparent, i.e. if elevated trust is required to continue executing the method */
500 return (mono_security_core_clr_method_level (cookie.caller, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT);
505 * check_field_access:
507 * Return TRUE if the caller method can access the specified field, FALSE otherwise.
510 check_field_access (MonoMethod *caller, MonoClassField *field)
512 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
517 /* this check can occur before the field's type is resolved (and that can fail) */
518 mono_field_get_type_checked (field, &error);
519 if (!mono_error_ok (&error)) {
520 mono_error_cleanup (&error);
524 klass = (mono_field_get_flags (field) & FIELD_ATTRIBUTE_STATIC) ? NULL : mono_field_get_parent (field);
525 return mono_method_can_access_field_full (caller, field, klass);
531 * check_method_access:
533 * Return TRUE if the caller method can access the specified callee method, FALSE otherwise.
536 check_method_access (MonoMethod *caller, MonoMethod *callee)
538 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
540 MonoClass *klass = (callee->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : callee->klass;
541 return mono_method_can_access_method_full (caller, callee, klass);
547 * get_argument_exception
549 * Helper function to create an MonoException (ArgumentException in
550 * managed-land) and provide a descriptive message for it. This
551 * message is also, optionally, being logged (export
552 * MONO_LOG_MASK="security") for debugging purposes.
554 static MonoException*
555 get_argument_exception (const char *format, MonoMethod *caller, MonoMethod *callee)
558 char *caller_name = get_method_full_name (caller);
559 char *callee_name = get_method_full_name (callee);
560 char *message = g_strdup_printf (format, caller_name, callee_name);
561 g_free (callee_name);
562 g_free (caller_name);
564 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, "%s", message);
565 ex = mono_get_exception_argument ("method", message);
572 * get_field_access_exception
574 * Helper function to create an MonoException (FieldAccessException
575 * in managed-land) and provide a descriptive message for it. This
576 * message is also, optionally, being logged (export
577 * MONO_LOG_MASK="security") for debugging purposes.
579 static MonoException*
580 get_field_access_exception (const char *format, MonoMethod *caller, MonoClassField *field)
583 char *caller_name = get_method_full_name (caller);
584 char *field_name = mono_field_full_name (field);
585 char *message = g_strdup_printf (format, caller_name, field_name);
587 g_free (caller_name);
589 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, "%s", message);
590 ex = mono_get_exception_field_access_msg (message);
597 * get_method_access_exception
599 * Helper function to create an MonoException (MethodAccessException
600 * in managed-land) and provide a descriptive message for it. This
601 * message is also, optionally, being logged (export
602 * MONO_LOG_MASK="security") for debugging purposes.
604 static MonoException*
605 get_method_access_exception (const char *format, MonoMethod *caller, MonoMethod *callee)
608 char *caller_name = get_method_full_name (caller);
609 char *callee_name = get_method_full_name (callee);
610 char *message = g_strdup_printf (format, caller_name, callee_name);
611 g_free (callee_name);
612 g_free (caller_name);
614 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, "%s", message);
615 ex = mono_get_exception_method_access_msg (message);
622 * mono_security_core_clr_ensure_reflection_access_field:
624 * Ensure that the specified field can be used with reflection since
625 * Transparent code cannot access to Critical fields and can only use
626 * them if they are visible from it's point of view.
628 * Returns TRUE if acess is allowed. Otherwise returns FALSE and sets @error to a FieldAccessException if the field is cannot be accessed.
631 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field, MonoError *error)
633 mono_error_init (error);
634 MonoMethod *caller = get_reflection_caller ();
635 /* CoreCLR restrictions applies to Transparent code/caller */
636 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
639 if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION) {
640 if (!mono_security_core_clr_is_platform_image (mono_field_get_parent(field)->image))
644 /* Transparent code cannot [get|set]value on Critical fields */
645 if (mono_security_core_clr_class_level (mono_field_get_parent (field)) == MONO_SECURITY_CORE_CLR_CRITICAL) {
646 mono_error_set_exception_instance (error, get_field_access_exception (
647 "Transparent method %s cannot get or set Critical field %s.",
652 /* also it cannot access a fields that is not visible from it's (caller) point of view */
653 if (!check_field_access (caller, field)) {
654 mono_error_set_exception_instance (error, get_field_access_exception (
655 "Transparent method %s cannot get or set private/internal field %s.",
663 * mono_security_core_clr_ensure_reflection_access_method:
665 * Ensure that the specified method can be used with reflection since
666 * Transparent code cannot call Critical methods and can only call them
667 * if they are visible from it's point of view.
669 * If access is allowed returns TRUE. Returns FALSE and sets @error to a MethodAccessException if the field is cannot be accessed.
672 mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method, MonoError *error)
674 mono_error_init (error);
675 MonoMethod *caller = get_reflection_caller ();
676 /* CoreCLR restrictions applies to Transparent code/caller */
677 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
680 if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION) {
681 if (!mono_security_core_clr_is_platform_image (method->klass->image))
685 /* Transparent code cannot invoke, even using reflection, Critical code */
686 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
687 mono_error_set_exception_instance (error, get_method_access_exception (
688 "Transparent method %s cannot invoke Critical method %s.",
693 /* also it cannot invoke a method that is not visible from it's (caller) point of view */
694 if (!check_method_access (caller, method)) {
695 mono_error_set_exception_instance (error, get_method_access_exception (
696 "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
739 * method. CoreCLR can also affect the binding, this function may
740 * return (FALSE) and set @error to an ArgumentException.
742 * @error is set to a MethodAccessException if the specified method is not
743 * visible from the caller point of view.
746 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, MonoError *error)
750 mono_error_init (error);
752 /* note: mscorlib creates delegates to avoid reflection (optimization), we ignore those cases */
753 if (can_avoid_corlib_reflection_delegate_optimization (method))
756 caller = get_reflection_caller ();
757 /* if the "real" caller is not Transparent then it do can anything */
758 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
761 /* otherwise it (as a Transparent caller) cannot create a delegate on a Critical method... */
762 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
763 mono_error_set_exception_instance (error, get_argument_exception (
764 "Transparent method %s cannot create a delegate on Critical method %s.",
769 if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_DELEGATE) {
770 if (!mono_security_core_clr_is_platform_image (method->klass->image))
774 /* also it cannot create the delegate on a method that is not visible from it's (caller) point of view */
775 if (!check_method_access (caller, method)) {
776 mono_error_set_exception_instance (error, get_method_access_exception (
777 "Transparent method %s cannot create a delegate on private/internal method %s.",
786 * mono_security_core_clr_ensure_dynamic_method_resolved_object:
788 * Called from mono_reflection_create_dynamic_method (reflection.c) to add some extra checks required for CoreCLR.
789 * Dynamic methods needs to check to see if the objects being used (e.g. methods, fields) comes from platform code
790 * and do an accessibility check in this case. Otherwise (i.e. user/application code) can be used without this extra
791 * accessbility check.
794 mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
796 /* XXX find/create test cases for other handle_class XXX */
797 if (handle_class == mono_defaults.fieldhandle_class) {
798 MonoClassField *field = (MonoClassField*) ref;
799 MonoClass *klass = mono_field_get_parent (field);
800 /* fields coming from platform code have extra protection (accessibility check) */
801 if (mono_security_core_clr_is_platform_image (klass->image)) {
802 MonoMethod *caller = get_reflection_caller ();
803 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
804 if (!check_field_access (caller, field)) {
805 return get_field_access_exception (
806 "Dynamic method %s cannot create access private/internal field %s.",
810 } else if (handle_class == mono_defaults.methodhandle_class) {
811 MonoMethod *method = (MonoMethod*) ref;
812 /* methods coming from platform code have extra protection (accessibility check) */
813 if (mono_security_core_clr_is_platform_image (method->klass->image)) {
814 MonoMethod *caller = get_reflection_caller ();
815 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
816 if (!check_method_access (caller, method)) {
817 return get_method_access_exception (
818 "Dynamic method %s cannot create access private/internal method %s.",
827 * mono_security_core_clr_can_access_internals
829 * Check if we allow [InternalsVisibleTo] to work between two images.
832 mono_security_core_clr_can_access_internals (MonoImage *accessing, MonoImage* accessed)
834 /* are we trying to access internals of a platform assembly ? if not this is acceptable */
835 if (!mono_security_core_clr_is_platform_image (accessed))
838 /* we can't let everyone with the right name and public key token access the internals of platform code.
839 * (Silverlight can rely on the strongname signature of the assemblies, but Mono does not verify them)
840 * However platform code is fully trusted so it can access the internals of other platform code assemblies */
841 if (mono_security_core_clr_is_platform_image (accessing))
844 /* catch-22: System.Xml needs access to mscorlib's internals (e.g. ArrayList) but is not considered platform code.
845 * Promoting it to platform code would create another issue since (both Mono/Moonlight or MS version of)
846 * System.Xml.Linq.dll (an SDK, not platform, assembly) needs access to System.Xml.dll internals (either ).
847 * The solution is to trust, even transparent code, in the plugin directory to access platform code internals */
848 if (!accessed->assembly->basedir || !accessing->assembly->basedir)
850 return (strcmp (accessed->assembly->basedir, accessing->assembly->basedir) == 0);
854 * mono_security_core_clr_is_field_access_allowed
856 * Return a MonoException (FieldccessException in managed-land) if
857 * the access from "caller" to "field" is not valid under CoreCLR -
858 * i.e. a [SecurityTransparent] method calling a [SecurityCritical]
862 mono_security_core_clr_is_field_access_allowed (MonoMethod *caller, MonoClassField *field)
864 /* there's no restriction to access Transparent or SafeCritical fields, so we only check calls to Critical methods */
865 if (mono_security_core_clr_class_level (mono_field_get_parent (field)) != MONO_SECURITY_CORE_CLR_CRITICAL)
868 /* caller is Critical! only SafeCritical and Critical callers can access the field, so we throw if caller is Transparent */
869 if (!caller || (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT))
872 return get_field_access_exception (
873 "Transparent method %s cannot call use Critical field %s.",
878 * mono_security_core_clr_is_call_allowed
880 * Return a MonoException (MethodAccessException in managed-land) if
881 * the call from "caller" to "callee" is not valid under CoreCLR -
882 * i.e. a [SecurityTransparent] method calling a [SecurityCritical]
886 mono_security_core_clr_is_call_allowed (MonoMethod *caller, MonoMethod *callee)
888 /* there's no restriction to call Transparent or SafeCritical code, so we only check calls to Critical methods */
889 if (mono_security_core_clr_method_level (callee, TRUE) != MONO_SECURITY_CORE_CLR_CRITICAL)
892 /* callee is Critical! only SafeCritical and Critical callers can call it, so we throw if the caller is Transparent */
893 if (!caller || (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT))
896 return get_method_access_exception (
897 "Transparent method %s cannot call Critical method %s.",
902 * mono_security_core_clr_level_from_cinfo:
904 * Return the MonoSecurityCoreCLRLevel that match the attribute located
905 * in the specified custom attributes. If no attribute is present it
906 * defaults to MONO_SECURITY_CORE_CLR_TRANSPARENT, which is the default
907 * level for all code under the CoreCLR.
909 static MonoSecurityCoreCLRLevel
910 mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo *cinfo, MonoImage *image)
912 int level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
914 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_safe_critical_attribute ()))
915 level = MONO_SECURITY_CORE_CLR_SAFE_CRITICAL;
916 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_critical_attribute ()))
917 level = MONO_SECURITY_CORE_CLR_CRITICAL;
919 return (MonoSecurityCoreCLRLevel)level;
923 * mono_security_core_clr_class_level_no_platform_check:
925 * Return the MonoSecurityCoreCLRLevel for the specified class, without
926 * checking for platform code. This help us avoid multiple redundant
928 * - a check for the method and one for the class;
929 * - a check for the class and outer class(es) ...
931 static MonoSecurityCoreCLRLevel
932 mono_security_core_clr_class_level_no_platform_check (MonoClass *klass)
934 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
935 MonoCustomAttrInfo *cinfo = mono_custom_attrs_from_class (klass);
937 level = mono_security_core_clr_level_from_cinfo (cinfo, klass->image);
938 mono_custom_attrs_free (cinfo);
941 if (level == MONO_SECURITY_CORE_CLR_TRANSPARENT && klass->nested_in)
942 level = mono_security_core_clr_class_level_no_platform_check (klass->nested_in);
948 * mono_security_core_clr_class_level:
950 * Return the MonoSecurityCoreCLRLevel for the specified class.
952 MonoSecurityCoreCLRLevel
953 mono_security_core_clr_class_level (MonoClass *klass)
955 /* non-platform code is always Transparent - whatever the attributes says */
956 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (klass->image))
957 return MONO_SECURITY_CORE_CLR_TRANSPARENT;
959 return mono_security_core_clr_class_level_no_platform_check (klass);
963 * mono_security_core_clr_field_level:
965 * Return the MonoSecurityCoreCLRLevel for the specified field.
966 * If with_class_level is TRUE then the type (class) will also be
967 * checked, otherwise this will only report the information about
970 MonoSecurityCoreCLRLevel
971 mono_security_core_clr_field_level (MonoClassField *field, gboolean with_class_level)
973 MonoCustomAttrInfo *cinfo;
974 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
976 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
980 /* non-platform code is always Transparent - whatever the attributes says */
981 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (field->parent->image))
984 cinfo = mono_custom_attrs_from_field (field->parent, field);
986 level = mono_security_core_clr_level_from_cinfo (cinfo, field->parent->image);
987 mono_custom_attrs_free (cinfo);
990 if (with_class_level && level == MONO_SECURITY_CORE_CLR_TRANSPARENT)
991 level = mono_security_core_clr_class_level (field->parent);
997 * mono_security_core_clr_method_level:
999 * Return the MonoSecurityCoreCLRLevel for the specified method.
1000 * If with_class_level is TRUE then the type (class) will also be
1001 * checked, otherwise this will only report the information about
1002 * the method itself.
1004 MonoSecurityCoreCLRLevel
1005 mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
1007 MonoCustomAttrInfo *cinfo;
1008 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
1010 /* if get_reflection_caller returns NULL then we assume the caller has NO privilege */
1014 /* non-platform code is always Transparent - whatever the attributes says */
1015 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (method->klass->image))
1018 cinfo = mono_custom_attrs_from_method (method);
1020 level = mono_security_core_clr_level_from_cinfo (cinfo, method->klass->image);
1021 mono_custom_attrs_free (cinfo);
1024 if (with_class_level && level == MONO_SECURITY_CORE_CLR_TRANSPARENT)
1025 level = mono_security_core_clr_class_level (method->klass);
1031 * mono_security_enable_core_clr:
1033 * Enable the verifier and the CoreCLR security model
1036 mono_security_enable_core_clr ()
1038 mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
1039 mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
1045 mono_security_core_clr_check_inheritance (MonoClass *klass)
1050 mono_security_core_clr_check_override (MonoClass *klass, MonoMethod *override, MonoMethod *base)
1055 mono_security_core_clr_require_elevated_permissions (void)
1061 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field, MonoError *error)
1063 mono_error_init (error);
1068 mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method, MonoError *error)
1070 mono_error_init (error);
1075 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, MonoError *error)
1077 mono_error_init (error);
1082 mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
1088 mono_security_core_clr_can_access_internals (MonoImage *accessing, MonoImage* accessed)
1094 mono_security_core_clr_is_field_access_allowed (MonoMethod *caller, MonoClassField *field)
1100 mono_security_core_clr_is_call_allowed (MonoMethod *caller, MonoMethod *callee)
1105 MonoSecurityCoreCLRLevel
1106 mono_security_core_clr_class_level (MonoClass *klass)
1108 return MONO_SECURITY_CORE_CLR_TRANSPARENT;
1111 MonoSecurityCoreCLRLevel
1112 mono_security_core_clr_field_level (MonoClassField *field, gboolean with_class_level)
1114 return MONO_SECURITY_CORE_CLR_TRANSPARENT;
1117 MonoSecurityCoreCLRLevel
1118 mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
1120 return MONO_SECURITY_CORE_CLR_TRANSPARENT;
1124 mono_security_enable_core_clr ()
1128 #endif /* DISABLE_SECURITY */