2009-03-25 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 static gboolean
49 get_caller_no_reflection_related (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
50 {
51         MonoMethod **dest = data;
52         const char *ns;
53
54         /* skip unmanaged frames */
55         if (!managed)
56                 return FALSE;
57
58         if (m->wrapper_type != MONO_WRAPPER_NONE)
59                 return FALSE;
60
61         /* quick out (any namespace not starting with an 'S' */
62         ns = m->klass->name_space;
63         if (!ns || (*ns != 'S')) {
64                 *dest = m;
65                 return TRUE;
66         }
67
68         /* stop if the method is not part of platform code */
69         if (!mono_security_core_clr_is_platform_image (m->klass->image)) {
70                 *dest = m;
71                 return TRUE;
72         }
73
74         /* any number of calls inside System.Reflection are allowed */
75         if (strcmp (ns, "System.Reflection") == 0)
76                 return FALSE;
77
78         /* any number of calls inside System.Reflection are allowed */
79         if (strcmp (ns, "System.Reflection.Emit") == 0)
80                 return FALSE;
81
82         /* calls from System.Delegate are also possible and allowed */
83         if (strcmp (ns, "System") == 0) {
84                 const char *kname = m->klass->name;
85                 if ((*kname == 'D') && (strcmp (kname, "Delegate") == 0))
86                         return FALSE;
87                 if ((*kname == 'M') && (strcmp (kname, "MulticastDelegate")) == 0)
88                         return FALSE;
89                 if ((*kname == 'A') && (strcmp (kname, "Activator") == 0))
90                         return FALSE;
91         }
92
93         if (m == *dest) {
94                 *dest = NULL;
95                 return FALSE;
96         }
97
98         *dest = m;
99         return TRUE;
100 }
101
102 /* walk to the first managed method outside:
103  *  - System.Reflection* namespaces
104  *  - System.[MulticastDelegate]Delegate or Activator type
105  *  - platform code
106  *  and return its value, since CoreCLR checks needs to be done on this "real" caller.
107  */
108 static MonoMethod*
109 get_reflection_caller (void)
110 {
111         MonoMethod *m = mono_method_get_last_managed ();
112         mono_stack_walk_no_il (get_caller_no_reflection_related, &m);
113         return m;
114 }
115
116 /*
117  * check_field_access:
118  *
119  *      Return TRUE if the caller method can access the specified field, FALSE otherwise.
120  */
121 static gboolean
122 check_field_access (MonoMethod *caller, MonoClassField *field)
123 {
124         MonoClass *klass = (mono_field_get_flags (field) & FIELD_ATTRIBUTE_STATIC) ? NULL : mono_field_get_parent (field);
125         return mono_method_can_access_field_full (caller, field, klass);
126 }
127
128 /*
129  * check_method_access:
130  *
131  *      Return TRUE if the caller method can access the specified callee method, FALSE otherwise.
132  */
133 static gboolean
134 check_method_access (MonoMethod *caller, MonoMethod *callee)
135 {
136         MonoClass *klass = (callee->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : callee->klass;
137         return mono_method_can_access_method_full (caller, callee, klass);
138 }
139
140 void
141 mono_security_core_clr_ensure_reflection_access_field (MonoClassField *field)
142 {
143         MonoClass *klass = mono_field_get_parent (field);
144
145         /* under CoreCLR you cannot use the value (get/set) of the reflected field: */
146         MonoMethod *caller = get_reflection_caller ();
147
148         /* (a) of a Critical type when called from a Transparent caller */
149         if (mono_security_core_clr_class_level (klass) == MONO_SECURITY_CORE_CLR_CRITICAL) {
150                 if (mono_security_core_clr_method_level (caller, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT)
151                         mono_raise_exception (mono_get_exception_field_access ());
152         }
153         /* (b) that are not accessible from the caller pov */
154         if (!mono_method_can_access_field_full (caller, field, klass))
155                 mono_raise_exception (mono_get_exception_field_access ());
156 }
157
158 void
159 mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
160 {
161         MonoMethod *caller = get_reflection_caller ();
162         /* CoreCLR restrictions applies to Transparent code/caller */
163         if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
164                 return;
165
166         /* Transparent code cannot invoke, even using reflection, Critical code */
167         if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL)
168                 mono_raise_exception (mono_get_exception_method_access ());
169
170         /* also it cannot invoke a method that is not visible from it's (caller) point of view */
171         if (!mono_method_can_access_method_full (caller, method, (method->flags & METHOD_ATTRIBUTE_STATIC) ? NULL : method->klass))
172                 mono_raise_exception (mono_get_exception_method_access ());
173 }
174
175 /*
176  * mono_security_core_clr_ensure_delegate_creation:
177  *
178  *      Return TRUE if a delegate can be created on the specified method. 
179  *      CoreCLR also affect the binding, so throwOnBindFailure must be 
180  *      FALSE to let this function return (FALSE) normally, otherwise (if
181  *      throwOnBindFailure is TRUE) itwill throw an ArgumentException.
182  *
183  *      A MethodAccessException is thrown if the specified method is not
184  *      visible from the caller point of view.
185  */
186 gboolean
187 mono_security_core_clr_ensure_delegate_creation (MonoMethod *method, gboolean throwOnBindFailure)
188 {
189         /* note: mscorlib creates delegates to avoid reflection (optimization), we ignore those cases */
190         MonoMethod *caller = get_reflection_caller ();
191         /* if the "real" caller is not Transparent then it do can anything */
192         if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
193                 return TRUE;
194
195         /* otherwise it (as a Transparent caller) cannot create a delegate on a Critical method... */
196         if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
197                 /* but this throws only if 'throwOnBindFailure' is TRUE */
198                 if (!throwOnBindFailure)
199                         return FALSE;
200
201                 mono_raise_exception (mono_get_exception_argument ("method", "Transparent code cannot call Critical code"));
202         }
203         
204         /* also it cannot create the delegate on a method that is not visible from it's (caller) point of view */
205         if (!check_method_access (caller, method))
206                 mono_raise_exception (mono_get_exception_method_access ());
207
208         return TRUE;
209 }
210
211 /*
212  * mono_security_core_clr_ensure_dynamic_method_resolved_object:
213  *
214  *      Called from mono_reflection_create_dynamic_method (reflection.c) to add some extra checks required for CoreCLR.
215  *      Dynamic methods needs to check to see if the objects being used (e.g. methods, fields) comes from platform code
216  *      and do an accessibility check in this case. Otherwise (i.e. user/application code) can be used without this extra
217  *      accessbility check.
218  */
219 MonoException*
220 mono_security_core_clr_ensure_dynamic_method_resolved_object (gpointer ref, MonoClass *handle_class)
221 {
222         /* XXX find/create test cases for other handle_class XXX */
223         if (handle_class == mono_defaults.fieldhandle_class) {
224                 MonoClassField *field = (MonoClassField*) ref;
225                 MonoClass *klass = mono_field_get_parent (field);
226                 /* fields coming from platform code have extra protection (accessibility check) */
227                 if (mono_security_core_clr_is_platform_image (klass->image)) {
228                         MonoMethod *caller = get_reflection_caller ();
229                         /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
230                         if (!check_field_access (caller, field))
231                                 return mono_get_exception_field_access ();
232                 }
233         } else if (handle_class == mono_defaults.methodhandle_class) {
234                 MonoMethod *method = (MonoMethod*) ref;
235                 /* methods coming from platform code have extra protection (accessibility check) */
236                 if (mono_security_core_clr_is_platform_image (method->klass->image)) {
237                         MonoMethod *caller = get_reflection_caller ();
238                         /* XXX Critical code probably can do this / need some test cases (safer off otherwise) XXX */
239                         if (!check_method_access (caller, method))
240                                 return mono_get_exception_method_access ();
241                 }
242         }
243         return NULL;
244 }
245
246 static MonoSecurityCoreCLRLevel
247 mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo *cinfo, MonoImage *image)
248 {
249         int level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
250
251         if (cinfo && mono_custom_attrs_has_attr (cinfo, security_safe_critical_attribute ()))
252                 level = MONO_SECURITY_CORE_CLR_SAFE_CRITICAL;
253         if (cinfo && mono_custom_attrs_has_attr (cinfo, security_critical_attribute ()))
254                 level = MONO_SECURITY_CORE_CLR_CRITICAL;
255
256         return level;
257 }
258
259 MonoSecurityCoreCLRLevel
260 mono_security_core_clr_class_level (MonoClass *class)
261 {
262         MonoCustomAttrInfo *cinfo;
263         MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
264
265         /* non-platform code is always Transparent - whatever the attributes says */
266         if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (class->image))
267                 return level;
268
269         cinfo = mono_custom_attrs_from_class (class);
270         if (cinfo) {
271                 level = mono_security_core_clr_level_from_cinfo (cinfo, class->image);
272                 mono_custom_attrs_free (cinfo);
273         }
274
275         if (level == MONO_SECURITY_CORE_CLR_TRANSPARENT && class->nested_in)
276                 level = mono_security_core_clr_class_level (class->nested_in);
277
278         return level;
279 }
280
281 MonoSecurityCoreCLRLevel
282 mono_security_core_clr_method_level (MonoMethod *method, gboolean with_class_level)
283 {
284         MonoCustomAttrInfo *cinfo;
285         MonoSecurityCoreCLRLevel level = MONO_SECURITY_CORE_CLR_TRANSPARENT;
286
287         /* non-platform code is always Transparent - whatever the attributes says */
288         if (!mono_security_core_clr_test && !mono_security_core_clr_is_platform_image (method->klass->image))
289                 return level;
290
291         cinfo = mono_custom_attrs_from_method (method);
292         if (cinfo) {
293                 level = mono_security_core_clr_level_from_cinfo (cinfo, method->klass->image);
294                 mono_custom_attrs_free (cinfo);
295         }
296
297         if (with_class_level && level == MONO_SECURITY_CORE_CLR_TRANSPARENT)
298                 level = mono_security_core_clr_class_level (method->klass);
299
300         return level;
301 }
302
303 gboolean
304 mono_security_core_clr_is_platform_image (MonoImage *image)
305 {
306         const char *prefix = mono_assembly_getrootdir ();
307         int prefix_len = strlen (prefix);
308         static const char subprefix[] = "/mono/2.1/";
309         int subprefix_len = strlen (subprefix);
310
311         if (!image->name)
312                 return FALSE;
313         if (strncmp (prefix, image->name, prefix_len) != 0)
314                 return FALSE;
315         if (strncmp (subprefix, image->name + prefix_len, subprefix_len) != 0)
316                 return FALSE;
317         if (strchr (image->name + prefix_len + subprefix_len, '/'))
318                 return FALSE;
319         return TRUE;
320 }
321
322 void
323 mono_security_enable_core_clr ()
324 {
325         mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
326         mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
327 }