2009-03-27 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  * get_caller_no_reflection_related:
50  *
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
54  *
55  *      Returns TRUE to stop the stackwalk, FALSE to continue to the next frame.
56  */
57 static gboolean
58 get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
59 {
60         MonoMethod **dest = data;
61         const char *ns;
62
63         /* skip unmanaged frames */
64         if (!managed)
65                 return FALSE;
66
67         if (m->wrapper_type != MONO_WRAPPER_NONE)
68                 return FALSE;
69
70         /* quick out (any namespace not starting with an 'S' */
71         ns = m->klass->name_space;
72         if (!ns || (*ns != 'S')) {
73                 *dest = m;
74                 return TRUE;
75         }
76
77         /* stop if the method is not part of platform code */
78         if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
79                 *dest = m;
80                 return TRUE;
81         }
82
83         /* any number of calls inside System.Reflection are allowed */
84         if (strcmp (ns, "System.Reflection") == 0)
85                 return FALSE;
86
87         /* any number of calls inside System.Reflection are allowed */
88         if (strcmp (ns, "System.Reflection.Emit") == 0)
89                 return FALSE;
90
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))
95                         return FALSE;
96                 if ((*kname == 'M') && (strcmp (kname, "MulticastDelegate")) == 0)
97                         return FALSE;
98                 if ((*kname == 'A') && (strcmp (kname, "Activator") == 0))
99                         return FALSE;
100         }
101
102         if (m == *dest) {
103                 *dest = NULL;
104                 return FALSE;
105         }
106
107         *dest = m;
108         return TRUE;
109 }
110
111 /*
112  * get_reflection_caller:
113  * 
114  *      Walk to the first managed method outside:
115  *      - System.Reflection* namespaces
116  *      - System.[MulticastDelegate]Delegate or Activator type
117  *      - platform code
118  *      and return a pointer to its MonoMethod.
119  *
120  *      This is required since CoreCLR checks needs to be done on this "real" caller.
121  */
122 static MonoMethod*
123 get_reflection_caller (void)
124 {
125         MonoMethod *m = mono_method_get_last_managed ();
126         mono_stack_walk_no_il (get_caller_no_reflection_related, &m);
127         return m;
128 }
129
130 /*
131  * check_field_access:
132  *
133  *      Return TRUE if the caller method can access the specified field, FALSE otherwise.
134  */
135 static gboolean
136 check_field_access (MonoMethod *caller, MonoClassField *field)
137 {
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);
140 }
141
142 /*
143  * check_method_access:
144  *
145  *      Return TRUE if the caller method can access the specified callee method, FALSE otherwise.
146  */
147 static gboolean
148 check_method_access (MonoMethod *caller, MonoMethod *callee)
149 {
150         MonoClass *klass = (callee->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : callee->klass;
151         return mono_method_can_access_method_full (caller, callee, klass);
152 }
153
154 /*
155  * mono_security_core_clr_ensure_reflection_access_field:
156  *
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.
160  *
161  *      A FieldAccessException is thrown if the field is cannot be accessed.
162  */
163 void
164 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
165 {
166         MonoClass *klass = mono_field_get_parent (field);
167
168         /* under CoreCLR you cannot use the value (get/set) of the reflected field: */
169         MonoMethod *caller = get_reflection_caller ();
170
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 ());
175         }
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 ());
179 }
180
181 /*
182  * mono_security_core_clr_ensure_reflection_access_method:
183  *
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.
187  *
188  *      A MethodAccessException is thrown if the field is cannot be accessed.
189  */
190 void
191 mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
192 {
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)
196                 return;
197
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 ());
201
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 ());
205 }
206
207 /*
208  * mono_security_core_clr_ensure_delegate_creation:
209  *
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.
214  *
215  *      A MethodAccessException is thrown if the specified method is not
216  *      visible from the caller point of view.
217  */
218 gboolean
219 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean throwOnBindFailure)
220 {
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)
225                 return TRUE;
226
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)
231                         return FALSE;
232
233                 mono_raise_exception (mono_get_exception_argument ("method", "Transparent code cannot call Critical code"));
234         }
235         
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 ());
239
240         return TRUE;
241 }
242
243 /*
244  * mono_security_core_clr_ensure_dynamic_method_resolved_object:
245  *
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.
250  */
251 MonoException*
252 mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
253 {
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 ();
264                 }
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 ();
273                 }
274         }
275         return NULL;
276 }
277
278 /*
279  * mono_security_core_clr_level_from_cinfo:
280  *
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.
285  */
286 static MonoSecurityCoreCLRLevel
287 mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo *cinfo, MonoImage *image)
288 {
289         int level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
290
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;
295
296         return level;
297 }
298
299 /*
300  * mono_security_core_clr_class_level:
301  *
302  *      Return the MonoSecurityCoreCLRLevel for the specified class.
303  */
304 MonoSecurityCoreCLRLevel
305 mono_security_core_clr_class_level (MonoClass *class)
306 {
307         MonoCustomAttrInfo *cinfo;
308         MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
309
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))
312                 return level;
313
314         cinfo = mono_custom_attrs_from_class (class);
315         if (cinfo) {
316                 level = mono_security_core_clr_level_from_cinfo (cinfo, class->image);
317                 mono_custom_attrs_free (cinfo);
318         }
319
320         if (level == MONO_SECURITY_CORE_CLR_TRANSPARENT && class->nested_in)
321                 level = mono_security_core_clr_class_level (class->nested_in);
322
323         return level;
324 }
325
326 /*
327  * mono_security_core_clr_method_level:
328  *
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
332  *      the method itself.
333  */
334 MonoSecurityCoreCLRLevel
335 mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
336 {
337         MonoCustomAttrInfo *cinfo;
338         MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
339
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))
342                 return level;
343
344         cinfo = mono_custom_attrs_from_method (method);
345         if (cinfo) {
346                 level = mono_security_core_clr_level_from_cinfo (cinfo, method->klass->image);
347                 mono_custom_attrs_free (cinfo);
348         }
349
350         if (with_class_level && level == MONO_SECURITY_CORE_CLR_TRANSPARENT)
351                 level = mono_security_core_clr_class_level (method->klass);
352
353         return level;
354 }
355
356 /*
357  * mono_security_core_clr_is_platform_image:
358  *
359  *   Return the (cached) boolean value indicating if this image represent platform code
360  */
361 gboolean
362 mono_security_core_clr_is_platform_image (MonoImage *image)
363 {
364         return image->core_clr_platform_code;
365 }
366
367 /*
368  * default_platform_check:
369  *
370  *      Default platform check. Always return FALSE.
371  */
372 static gboolean
373 default_platform_check (const char *image_name)
374 {
375         return FALSE;
376 }
377
378 static MonoCoreClrPlatformCB platform_callback = default_platform_check;
379
380 /*
381  * mono_security_core_clr_determine_platform_image:
382  *
383  *      Call the supplied callback (from mono_security_set_core_clr_platform_callback) 
384  *      to determine if this image represents platform code.
385  */
386 gboolean
387 mono_security_core_clr_determine_platform_image (MonoImage *image)
388 {
389         return platform_callback (image->name);
390 }
391
392 /*
393  * mono_security_enable_core_clr:
394  *
395  *   Enable the verifier and the CoreCLR security model
396  */
397 void
398 mono_security_enable_core_clr ()
399 {
400         mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
401         mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
402 }
403
404 /*
405  * mono_security_set_core_clr_platform_callback:
406  *
407  *      Set the callback function that will be used to determine if an image
408  *      is part, or not, of the platform code.
409  */
410 void
411 mono_security_set_core_clr_platform_callback (MonoCoreClrPlatformCB callback)
412 {
413         platform_callback = callback;
414 }
415