2 * security-core-clr.c: CoreCLR security
5 * Mark Probst <mark.probst@gmail.com>
7 * Copyright 2007-2009 Novell, Inc (http://www.novell.com)
10 #include <mono/metadata/class-internals.h>
11 #include <mono/metadata/security-manager.h>
12 #include <mono/metadata/assembly.h>
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/verify-internals.h>
15 #include <mono/metadata/object.h>
16 #include <mono/metadata/exception.h>
18 #include "security-core-clr.h"
20 gboolean mono_security_core_clr_test = FALSE;
23 security_critical_attribute (void)
25 static MonoClass *class = NULL;
28 class = mono_class_from_name (mono_defaults.corlib, "System.Security",
29 "SecurityCriticalAttribute");
36 security_safe_critical_attribute (void)
38 static MonoClass *class = NULL;
41 class = mono_class_from_name (mono_defaults.corlib, "System.Security",
42 "SecuritySafeCriticalAttribute");
49 * get_caller_no_reflection_related:
51 * Find the first managed caller that is either:
52 * (a) located outside the platform code assemblies; or
53 * (b) not related to reflection and delegates
55 * Returns TRUE to stop the stackwalk, FALSE to continue to the next frame.
58 get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
60 MonoMethod **dest = data;
63 /* skip unmanaged frames */
67 if (m->wrapper_type != MONO_WRAPPER_NONE)
70 /* quick out (any namespace not starting with an 'S' */
71 ns = m->klass->name_space;
72 if (!ns || (*ns != 'S')) {
77 /* stop if the method is not part of platform code */
78 if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
83 /* any number of calls inside System.Reflection are allowed */
84 if (strcmp (ns, "System.Reflection") == 0)
87 /* any number of calls inside System.Reflection are allowed */
88 if (strcmp (ns, "System.Reflection.Emit") == 0)
91 /* calls from System.Delegate are also possible and allowed */
92 if (strcmp (ns, "System") == 0) {
93 const char *kname = m->klass->name;
94 if ((*kname == 'D') && (strcmp (kname, "Delegate") == 0))
96 if ((*kname == 'M') && (strcmp (kname, "MulticastDelegate")) == 0)
98 if ((*kname == 'A') && (strcmp (kname, "Activator") == 0))
112 * get_reflection_caller:
114 * Walk to the first managed method outside:
115 * - System.Reflection* namespaces
116 * - System.[MulticastDelegate]Delegate or Activator type
118 * and return a pointer to its MonoMethod.
120 * This is required since CoreCLR checks needs to be done on this "real" caller.
123 get_reflection_caller (void)
125 MonoMethod *m = mono_method_get_last_managed ();
126 mono_stack_walk_no_il (get_caller_no_reflection_related, &m);
131 * check_field_access:
133 * Return TRUE if the caller method can access the specified field, FALSE otherwise.
136 check_field_access (MonoMethod *caller, MonoClassField *field)
138 MonoClass *klass = (mono_field_get_flags (field) & FIELD_ATTRIBUTE_STATIC) ? NULL : mono_field_get_parent (field);
139 return mono_method_can_access_field_full (caller, field, klass);
143 * check_method_access:
145 * Return TRUE if the caller method can access the specified callee method, FALSE otherwise.
148 check_method_access (MonoMethod *caller, MonoMethod *callee)
150 MonoClass *klass = (callee->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : callee->klass;
151 return mono_method_can_access_method_full (caller, callee, klass);
155 * mono_security_core_clr_ensure_reflection_access_field:
157 * Ensure that the specified field can be used with reflection since
158 * Transparent code cannot access to Critical fields and can only use
159 * them if they are visible from it's point of view.
161 * A FieldAccessException is thrown if the field is cannot be accessed.
164 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
166 MonoClass *klass = mono_field_get_parent (field);
168 /* under CoreCLR you cannot use the value (get/set) of the reflected field: */
169 MonoMethod *caller = get_reflection_caller ();
171 /* (a) of a Critical type when called from a Transparent caller */
172 if (mono_security_core_clr_class_level (klass) == MONO_SECURITY_CORE_CLR_CRITICAL) {
173 if (mono_security_core_clr_method_level (caller, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT)
174 mono_raise_exception (mono_get_exception_field_access ());
176 /* (b) that are not accessible from the caller pov */
177 if (!mono_method_can_access_field_full (caller, field, klass))
178 mono_raise_exception (mono_get_exception_field_access ());
182 * mono_security_core_clr_ensure_reflection_access_method:
184 * Ensure that the specified method can be used with reflection since
185 * Transparent code cannot call Critical methods and can only call them
186 * if they are visible from it's point of view.
188 * A MethodAccessException is thrown if the field is cannot be accessed.
191 mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
193 MonoMethod *caller = get_reflection_caller ();
194 /* CoreCLR restrictions applies to Transparent code/caller */
195 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
198 /* Transparent code cannot invoke, even using reflection, Critical code */
199 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL)
200 mono_raise_exception (mono_get_exception_method_access ());
202 /* also it cannot invoke a method that is not visible from it's (caller) point of view */
203 if (!mono_method_can_access_method_full (caller, method, (method->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : method->klass))
204 mono_raise_exception (mono_get_exception_method_access ());
208 * mono_security_core_clr_ensure_delegate_creation:
210 * Return TRUE if a delegate can be created on the specified method.
211 * CoreCLR also affect the binding, so throwOnBindFailure must be
212 * FALSE to let this function return (FALSE) normally, otherwise (if
213 * throwOnBindFailure is TRUE) itwill throw an ArgumentException.
215 * A MethodAccessException is thrown if the specified method is not
216 * visible from the caller point of view.
219 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean throwOnBindFailure)
221 /* note: mscorlib creates delegates to avoid reflection (optimization), we ignore those cases */
222 MonoMethod *caller = get_reflection_caller ();
223 /* if the "real" caller is not Transparent then it do can anything */
224 if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
227 /* otherwise it (as a Transparent caller) cannot create a delegate on a Critical method... */
228 if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
229 /* but this throws only if 'throwOnBindFailure' is TRUE */
230 if (!throwOnBindFailure)
233 mono_raise_exception (mono_get_exception_argument ("method", "Transparent code cannot call Critical code"));
236 /* also it cannot create the delegate on a method that is not visible from it's (caller) point of view */
237 if (!check_method_access (caller, method))
238 mono_raise_exception (mono_get_exception_method_access ());
244 * mono_security_core_clr_ensure_dynamic_method_resolved_object:
246 * Called from mono_reflection_create_dynamic_method (reflection.c) to add some extra checks required for CoreCLR.
247 * Dynamic methods needs to check to see if the objects being used (e.g. methods, fields) comes from platform code
248 * and do an accessibility check in this case. Otherwise (i.e. user/application code) can be used without this extra
249 * accessbility check.
252 mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
254 /* XXX find/create test cases for other handle_class XXX */
255 if (handle_class == mono_defaults.fieldhandle_class) {
256 MonoClassField *field = (MonoClassField*) ref;
257 MonoClass *klass = mono_field_get_parent (field);
258 /* fields coming from platform code have extra protection (accessibility check) */
259 if (mono_security_core_clr_is_platform_image (klass->image)) {
260 MonoMethod *caller = get_reflection_caller ();
261 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
262 if (!check_field_access (caller, field))
263 return mono_get_exception_field_access ();
265 } else if (handle_class == mono_defaults.methodhandle_class) {
266 MonoMethod *method = (MonoMethod*) ref;
267 /* methods coming from platform code have extra protection (accessibility check) */
268 if (mono_security_core_clr_is_platform_image (method->klass->image)) {
269 MonoMethod *caller = get_reflection_caller ();
270 /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
271 if (!check_method_access (caller, method))
272 return mono_get_exception_method_access ();
279 * mono_security_core_clr_level_from_cinfo:
281 * Return the MonoSecurityCoreCLRLevel that match the attribute located
282 * in the specified custom attributes. If no attribute is present it
283 * defaults to MONO_SECURITY_CORE_CLR_TRANSPARENT, which is the default
284 * level for all code under the CoreCLR.
286 static MonoSecurityCoreCLRLevel
287 mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo *cinfo, MonoImage *image)
289 int level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
291 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_safe_critical_attribute ()))
292 level = MONO_SECURITY_CORE_CLR_SAFE_CRITICAL;
293 if (cinfo && mono_custom_attrs_has_attr (cinfo, security_critical_attribute ()))
294 level = MONO_SECURITY_CORE_CLR_CRITICAL;
300 * mono_security_core_clr_class_level:
302 * Return the MonoSecurityCoreCLRLevel for the specified class.
304 MonoSecurityCoreCLRLevel
305 mono_security_core_clr_class_level (MonoClass *class)
307 MonoCustomAttrInfo *cinfo;
308 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
310 /* non-platform code is always Transparent - whatever the attributes says */
311 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (class->image))
314 cinfo = mono_custom_attrs_from_class (class);
316 level = mono_security_core_clr_level_from_cinfo (cinfo, class->image);
317 mono_custom_attrs_free (cinfo);
320 if (level == MONO_SECURITY_CORE_CLR_TRANSPARENT && class->nested_in)
321 level = mono_security_core_clr_class_level (class->nested_in);
327 * mono_security_core_clr_method_level:
329 * Return the MonoSecurityCoreCLRLevel for the specified method.
330 * If with_class_level is TRUE then the type (class) will also be
331 * checked, otherwise this will only report the information about
334 MonoSecurityCoreCLRLevel
335 mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
337 MonoCustomAttrInfo *cinfo;
338 MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
340 /* non-platform code is always Transparent - whatever the attributes says */
341 if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (method->klass->image))
344 cinfo = mono_custom_attrs_from_method (method);
346 level = mono_security_core_clr_level_from_cinfo (cinfo, method->klass->image);
347 mono_custom_attrs_free (cinfo);
350 if (with_class_level && level == MONO_SECURITY_CORE_CLR_TRANSPARENT)
351 level = mono_security_core_clr_class_level (method->klass);
357 * mono_security_core_clr_is_platform_image:
359 * Return the (cached) boolean value indicating if this image represent platform code
362 mono_security_core_clr_is_platform_image (MonoImage *image)
364 return image->core_clr_platform_code;
368 * default_platform_check:
370 * Default platform check. Always return FALSE.
373 default_platform_check (const char *image_name)
378 static MonoCoreClrPlatformCB platform_callback = default_platform_check;
381 * mono_security_core_clr_determine_platform_image:
383 * Call the supplied callback (from mono_security_set_core_clr_platform_callback)
384 * to determine if this image represents platform code.
387 mono_security_core_clr_determine_platform_image (MonoImage *image)
389 return platform_callback (image->name);
393 * mono_security_enable_core_clr:
395 * Enable the verifier and the CoreCLR security model
398 mono_security_enable_core_clr ()
400 mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
401 mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
405 * mono_security_set_core_clr_platform_callback:
407 * Set the callback function that will be used to determine if an image
408 * is part, or not, of the platform code.
411 mono_security_set_core_clr_platform_callback (MonoCoreClrPlatformCB callback)
413 platform_callback = callback;