2009-03-28 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mono / metadata / security-core-clr.c
1 /*
2  * security-core-clr.c: CoreCLR security
3  *
4  * Author:
5  *      Mark Probst <mark.probst@gmail.com>
6  *
7  * Copyright 2007-2009 Novell, Inc (http://www.novell.com)
8  */
9
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>
17
18 #include "security-core-clr.h"
19
20 gboolean mono_security_core_clr_test = FALSE;
21
22 static MonoClass*
23 security_critical_attribute (void)
24 {
25         static MonoClass *class = NULL;
26
27         if (!class) {
28                 class = mono_class_from_name (mono_defaults.corlib, "System.Security", 
29                         "SecurityCriticalAttribute");
30         }
31         g_assert (class);
32         return class;
33 }
34
35 static MonoClass*
36 security_safe_critical_attribute (void)
37 {
38         static MonoClass *class = NULL;
39
40         if (!class) {
41                 class = mono_class_from_name (mono_defaults.corlib, "System.Security", 
42                         "SecuritySafeCriticalAttribute");
43         }
44         g_assert (class);
45         return class;
46 }
47
48 /*
49  * mono_security_core_clr_check_inheritance:
50  *
51  *      Determine if the specified class can inherit from its parent using 
52  *      the CoreCLR inheritance rules.
53  *
54  *      Base Type       Allow Derived Type
55  *      ------------    ------------------
56  *      Transparent     Transparent, SafeCritical, Critical
57  *      SafeCritical    SafeCritical, Critical
58  *      Critical        Critical
59  *
60  *      Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
61  */
62 void
63 mono_security_core_clr_check_inheritance (MonoClass *class)
64 {
65         MonoSecurityCoreCLRLevel class_level, parent_level;
66         MonoClass *parent = class->parent;
67
68         if (!parent)
69                 return;
70
71         class_level = mono_security_core_clr_class_level (class);
72         parent_level = mono_security_core_clr_class_level (parent);
73
74         if (class_level < parent_level)
75                 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
76 }
77
78 /*
79  * mono_security_core_clr_check_override:
80  *
81  *      Determine if the specified override can "legally" override the 
82  *      specified base method using the CoreCLR inheritance rules.
83  *
84  *      Base (virtual/interface)        Allowed override
85  *      ------------------------        -------------------------
86  *      Transparent                     Transparent, SafeCritical
87  *      SafeCritical                    Transparent, SafeCritical
88  *      Critical                        Critical
89  *
90  *      Reference: http://msdn.microsoft.com/en-us/magazine/cc765416.aspx#id0190030
91  */
92 void
93 mono_security_core_clr_check_override (MonoClass *class, MonoMethod *override, MonoMethod *base)
94 {
95         MonoSecurityCoreCLRLevel base_level = mono_security_core_clr_method_level (base, FALSE);
96         MonoSecurityCoreCLRLevel override_level = mono_security_core_clr_method_level (override, FALSE);
97         /* if the base method is decorated with [SecurityCritical] then the overrided method MUST be too */
98         if (base_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
99                 if (override_level != MONO_SECURITY_CORE_CLR_CRITICAL)
100                         mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
101         } else {
102                 /* base is [SecuritySafeCritical] or [SecurityTransparent], override MUST NOT be [SecurityCritical] */
103                 if (override_level == MONO_SECURITY_CORE_CLR_CRITICAL)
104                         mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
105         }
106 }
107
108 /*
109  * get_caller_no_reflection_related:
110  *
111  *      Find the first managed caller that is either:
112  *      (a) located outside the platform code assemblies; or
113  *      (b) not related to reflection and delegates
114  *
115  *      Returns TRUE to stop the stackwalk, FALSE to continue to the next frame.
116  */
117 static gboolean
118 get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
119 {
120         MonoMethod **dest = data;
121         const char *ns;
122
123         /* skip unmanaged frames */
124         if (!managed)
125                 return FALSE;
126
127         if (m->wrapper_type != MONO_WRAPPER_NONE)
128                 return FALSE;
129
130         /* quick out (any namespace not starting with an 'S' */
131         ns = m->klass->name_space;
132         if (!ns || (*ns != 'S')) {
133                 *dest = m;
134                 return TRUE;
135         }
136
137         /* stop if the method is not part of platform code */
138         if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
139                 *dest = m;
140                 return TRUE;
141         }
142
143         /* any number of calls inside System.Reflection are allowed */
144         if (strcmp (ns, "System.Reflection") == 0)
145                 return FALSE;
146
147         /* any number of calls inside System.Reflection are allowed */
148         if (strcmp (ns, "System.Reflection.Emit") == 0)
149                 return FALSE;
150
151         /* calls from System.Delegate are also possible and allowed */
152         if (strcmp (ns, "System") == 0) {
153                 const char *kname = m->klass->name;
154                 if ((*kname == 'D') && (strcmp (kname, "Delegate") == 0))
155                         return FALSE;
156                 if ((*kname == 'M') && (strcmp (kname, "MulticastDelegate")) == 0)
157                         return FALSE;
158                 if ((*kname == 'A') && (strcmp (kname, "Activator") == 0))
159                         return FALSE;
160         }
161
162         if (m == *dest) {
163                 *dest = NULL;
164                 return FALSE;
165         }
166
167         *dest = m;
168         return TRUE;
169 }
170
171 /*
172  * get_reflection_caller:
173  * 
174  *      Walk to the first managed method outside:
175  *      - System.Reflection* namespaces
176  *      - System.[MulticastDelegate]Delegate or Activator type
177  *      - platform code
178  *      and return a pointer to its MonoMethod.
179  *
180  *      This is required since CoreCLR checks needs to be done on this "real" caller.
181  */
182 static MonoMethod*
183 get_reflection_caller (void)
184 {
185         MonoMethod *m = mono_method_get_last_managed ();
186         mono_stack_walk_no_il (get_caller_no_reflection_related, &m);
187         return m;
188 }
189
190 /*
191  * check_field_access:
192  *
193  *      Return TRUE if the caller method can access the specified field, FALSE otherwise.
194  */
195 static gboolean
196 check_field_access (MonoMethod *caller, MonoClassField *field)
197 {
198         MonoClass *klass = (mono_field_get_flags (field) & FIELD_ATTRIBUTE_STATIC) ? NULL : mono_field_get_parent (field);
199         return mono_method_can_access_field_full (caller, field, klass);
200 }
201
202 /*
203  * check_method_access:
204  *
205  *      Return TRUE if the caller method can access the specified callee method, FALSE otherwise.
206  */
207 static gboolean
208 check_method_access (MonoMethod *caller, MonoMethod *callee)
209 {
210         MonoClass *klass = (callee->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : callee->klass;
211         return mono_method_can_access_method_full (caller, callee, klass);
212 }
213
214 /*
215  * mono_security_core_clr_ensure_reflection_access_field:
216  *
217  *      Ensure that the specified field can be used with reflection since 
218  *      Transparent code cannot access to Critical fields and can only use
219  *      them if they are visible from it's point of view.
220  *
221  *      A FieldAccessException is thrown if the field is cannot be accessed.
222  */
223 void
224 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
225 {
226         MonoClass *klass = mono_field_get_parent (field);
227
228         /* under CoreCLR you cannot use the value (get/set) of the reflected field: */
229         MonoMethod *caller = get_reflection_caller ();
230
231         /* (a) of a Critical type when called from a Transparent caller */
232         if (mono_security_core_clr_class_level (klass) == MONO_SECURITY_CORE_CLR_CRITICAL) {
233                 if (mono_security_core_clr_method_level (caller, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT)
234                         mono_raise_exception (mono_get_exception_field_access ());
235         }
236         /* (b) that are not accessible from the caller pov */
237         if (!mono_method_can_access_field_full (caller, field, klass))
238                 mono_raise_exception (mono_get_exception_field_access ());
239 }
240
241 /*
242  * mono_security_core_clr_ensure_reflection_access_method:
243  *
244  *      Ensure that the specified method can be used with reflection since
245  *      Transparent code cannot call Critical methods and can only call them
246  *      if they are visible from it's point of view.
247  *
248  *      A MethodAccessException is thrown if the field is cannot be accessed.
249  */
250 void
251 mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
252 {
253         MonoMethod *caller = get_reflection_caller ();
254         /* CoreCLR restrictions applies to Transparent code/caller */
255         if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
256                 return;
257
258         /* Transparent code cannot invoke, even using reflection, Critical code */
259         if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL)
260                 mono_raise_exception (mono_get_exception_method_access ());
261
262         /* also it cannot invoke a method that is not visible from it's (caller) point of view */
263         if (!mono_method_can_access_method_full (caller, method, (method->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : method->klass))
264                 mono_raise_exception (mono_get_exception_method_access ());
265 }
266
267 /*
268  * mono_security_core_clr_ensure_delegate_creation:
269  *
270  *      Return TRUE if a delegate can be created on the specified method. 
271  *      CoreCLR also affect the binding, so throwOnBindFailure must be 
272  *      FALSE to let this function return (FALSE) normally, otherwise (if
273  *      throwOnBindFailure is TRUE) itwill throw an ArgumentException.
274  *
275  *      A MethodAccessException is thrown if the specified method is not
276  *      visible from the caller point of view.
277  */
278 gboolean
279 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean throwOnBindFailure)
280 {
281         /* note: mscorlib creates delegates to avoid reflection (optimization), we ignore those cases */
282         MonoMethod *caller = get_reflection_caller ();
283         /* if the "real" caller is not Transparent then it do can anything */
284         if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
285                 return TRUE;
286
287         /* otherwise it (as a Transparent caller) cannot create a delegate on a Critical method... */
288         if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
289                 /* but this throws only if 'throwOnBindFailure' is TRUE */
290                 if (!throwOnBindFailure)
291                         return FALSE;
292
293                 mono_raise_exception (mono_get_exception_argument ("method", "Transparent code cannot call Critical code"));
294         }
295         
296         /* also it cannot create the delegate on a method that is not visible from it's (caller) point of view */
297         if (!check_method_access (caller, method))
298                 mono_raise_exception (mono_get_exception_method_access ());
299
300         return TRUE;
301 }
302
303 /*
304  * mono_security_core_clr_ensure_dynamic_method_resolved_object:
305  *
306  *      Called from mono_reflection_create_dynamic_method (reflection.c) to add some extra checks required for CoreCLR.
307  *      Dynamic methods needs to check to see if the objects being used (e.g. methods, fields) comes from platform code
308  *      and do an accessibility check in this case. Otherwise (i.e. user/application code) can be used without this extra
309  *      accessbility check.
310  */
311 MonoException*
312 mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
313 {
314         /* XXX find/create test cases for other handle_class XXX */
315         if (handle_class == mono_defaults.fieldhandle_class) {
316                 MonoClassField *field = (MonoClassField*) ref;
317                 MonoClass *klass = mono_field_get_parent (field);
318                 /* fields coming from platform code have extra protection (accessibility check) */
319                 if (mono_security_core_clr_is_platform_image (klass->image)) {
320                         MonoMethod *caller = get_reflection_caller ();
321                         /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
322                         if (!check_field_access (caller, field))
323                                 return mono_get_exception_field_access ();
324                 }
325         } else if (handle_class == mono_defaults.methodhandle_class) {
326                 MonoMethod *method = (MonoMethod*) ref;
327                 /* methods coming from platform code have extra protection (accessibility check) */
328                 if (mono_security_core_clr_is_platform_image (method->klass->image)) {
329                         MonoMethod *caller = get_reflection_caller ();
330                         /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
331                         if (!check_method_access (caller, method))
332                                 return mono_get_exception_method_access ();
333                 }
334         }
335         return NULL;
336 }
337
338 /*
339  * mono_security_core_clr_level_from_cinfo:
340  *
341  *      Return the MonoSecurityCoreCLRLevel that match the attribute located
342  *      in the specified custom attributes. If no attribute is present it 
343  *      defaults to MONO_SECURITY_CORE_CLR_TRANSPARENT, which is the default
344  *      level for all code under the CoreCLR.
345  */
346 static MonoSecurityCoreCLRLevel
347 mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo *cinfo, MonoImage *image)
348 {
349         int level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
350
351         if (cinfo && mono_custom_attrs_has_attr (cinfo, security_safe_critical_attribute ()))
352                 level = MONO_SECURITY_CORE_CLR_SAFE_CRITICAL;
353         if (cinfo && mono_custom_attrs_has_attr (cinfo, security_critical_attribute ()))
354                 level = MONO_SECURITY_CORE_CLR_CRITICAL;
355
356         return level;
357 }
358
359 /*
360  * mono_security_core_clr_class_level:
361  *
362  *      Return the MonoSecurityCoreCLRLevel for the specified class.
363  */
364 MonoSecurityCoreCLRLevel
365 mono_security_core_clr_class_level (MonoClass *class)
366 {
367         MonoCustomAttrInfo *cinfo;
368         MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
369
370         /* non-platform code is always Transparent - whatever the attributes says */
371         if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (class->image))
372                 return level;
373
374         cinfo = mono_custom_attrs_from_class (class);
375         if (cinfo) {
376                 level = mono_security_core_clr_level_from_cinfo (cinfo, class->image);
377                 mono_custom_attrs_free (cinfo);
378         }
379
380         if (level == MONO_SECURITY_CORE_CLR_TRANSPARENT && class->nested_in)
381                 level = mono_security_core_clr_class_level (class->nested_in);
382
383         return level;
384 }
385
386 /*
387  * mono_security_core_clr_method_level:
388  *
389  *      Return the MonoSecurityCoreCLRLevel for the specified method.
390  *      If with_class_level is TRUE then the type (class) will also be
391  *      checked, otherwise this will only report the information about
392  *      the method itself.
393  */
394 MonoSecurityCoreCLRLevel
395 mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
396 {
397         MonoCustomAttrInfo *cinfo;
398         MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
399
400         /* non-platform code is always Transparent - whatever the attributes says */
401         if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (method->klass->image))
402                 return level;
403
404         cinfo = mono_custom_attrs_from_method (method);
405         if (cinfo) {
406                 level = mono_security_core_clr_level_from_cinfo (cinfo, method->klass->image);
407                 mono_custom_attrs_free (cinfo);
408         }
409
410         if (with_class_level && level == MONO_SECURITY_CORE_CLR_TRANSPARENT)
411                 level = mono_security_core_clr_class_level (method->klass);
412
413         return level;
414 }
415
416 /*
417  * mono_security_core_clr_is_platform_image:
418  *
419  *   Return the (cached) boolean value indicating if this image represent platform code
420  */
421 gboolean
422 mono_security_core_clr_is_platform_image (MonoImage *image)
423 {
424         return image->core_clr_platform_code;
425 }
426
427 /*
428  * default_platform_check:
429  *
430  *      Default platform check. Always return FALSE.
431  */
432 static gboolean
433 default_platform_check (const char *image_name)
434 {
435         return FALSE;
436 }
437
438 static MonoCoreClrPlatformCB platform_callback = default_platform_check;
439
440 /*
441  * mono_security_core_clr_determine_platform_image:
442  *
443  *      Call the supplied callback (from mono_security_set_core_clr_platform_callback) 
444  *      to determine if this image represents platform code.
445  */
446 gboolean
447 mono_security_core_clr_determine_platform_image (MonoImage *image)
448 {
449         return platform_callback (image->name);
450 }
451
452 /*
453  * mono_security_enable_core_clr:
454  *
455  *   Enable the verifier and the CoreCLR security model
456  */
457 void
458 mono_security_enable_core_clr ()
459 {
460         mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
461         mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
462 }
463
464 /*
465  * mono_security_set_core_clr_platform_callback:
466  *
467  *      Set the callback function that will be used to determine if an image
468  *      is part, or not, of the platform code.
469  */
470 void
471 mono_security_set_core_clr_platform_callback (MonoCoreClrPlatformCB callback)
472 {
473         platform_callback = callback;
474 }
475