Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / coreclr-security.cs
1 using System.Security;
2 using System;
3 using System.Runtime.InteropServices;
4 using System.Reflection;
5
6 [SecurityCriticalAttribute]
7 public class CClass
8 {
9         public CClass ()
10         {
11                 //Console.WriteLine ("c ctor");
12         }
13
14         public virtual void Method ()
15         {
16                 //Console.WriteLine ("c class");
17         }
18
19         public static void StaticMethod ()
20         {
21                 //Console.WriteLine ("c class static");
22         }
23 }
24
25 [SecuritySafeCriticalAttribute]
26 public class SCClass
27 {
28         public SCClass ()
29         {
30                 //Console.WriteLine ("sc ctor");
31         }
32
33         public virtual void Method ()
34         {
35                 //Console.WriteLine ("sc class");
36                 CClass cc = new CClass ();
37                 cc.Method ();
38         }
39 }
40
41 public class SCDevClass : SCClass
42 {
43         public SCDevClass ()
44         {
45                 Test.error ("safe-critical-derived class instantiated");
46         }
47
48         public override void Method ()
49         {
50                 //base.Method ();
51                 Test.error ("safe-critical-derived method called");
52         }
53 }
54
55 public class CMethodClass
56 {
57         public CMethodClass ()
58         {
59                 //Console.WriteLine ("cmethod ctor");
60         }
61
62         [SecurityCriticalAttribute]
63         public virtual void Method ()
64         {
65                 //Console.WriteLine ("cmethod");
66         }
67 }
68
69 public class CMethodDevClass : CMethodClass
70 {
71         public CMethodDevClass ()
72         {
73                 Test.error ("critical-derived constructor called");
74         }
75
76         public override void Method ()
77         {
78                 //base.Method();
79                 Test.error ("critical-derived method called");
80         }
81 }
82
83 public interface CMethodInterface {
84         [SecurityCriticalAttribute]
85         void Method ();
86 }
87
88 public class CInterfaceClass : CMethodInterface {
89         public CInterfaceClass () { }
90
91         public void Method ()
92         {
93                 Test.error ("security-critical-interface-derived method called");
94         }
95 }
96
97 [SecurityCriticalAttribute]
98 public class CriticalClass {
99
100         public class NestedClassInsideCritical {
101
102                 static public void Method ()
103                 {
104                         Test.error ("critical inner class method called");
105                 }
106         }
107 }
108
109 public class TransparentBaseClass {
110         public virtual void TransparentMethod ()
111         {
112         }
113
114         [SecuritySafeCritical]
115         public virtual void SafeCriticalMethod ()
116         {
117         }
118
119         [SecurityCritical]
120         public virtual void CriticalMethod ()
121         {
122         }
123 }
124
125 public class BadTransparentOverrideClass : TransparentBaseClass {
126         [SecurityCritical]
127         public override void TransparentMethod ()
128         {
129                 Test.error ("this method is critical and cannot override its base (transparent)");
130         }
131 }
132
133 public class BadSafeCriticalOverrideClass : TransparentBaseClass {
134         [SecurityCritical]
135         public override void SafeCriticalMethod ()
136         {
137                 Test.error ("this method is critical and cannot override its base (safe critical)");
138         }
139 }
140
141 public class BadCriticalOverrideClass : TransparentBaseClass {
142         public override void CriticalMethod ()
143         {
144                 Test.error ("this method is NOT critical and cannot override its base (critical)");
145         }
146 }
147
148 public delegate void MethodDelegate ();
149
150 public delegate Object InvokeDelegate (Object obj, Object[] parms);
151
152 // the 0.1% case from http://blogs.msdn.com/shawnfa/archive/2007/05/11/silverlight-security-iii-inheritance.aspx
153 public class TransparentClassWithSafeCriticalDefaultConstructor {
154
155         [SecuritySafeCritical]
156         public TransparentClassWithSafeCriticalDefaultConstructor ()
157         {
158         }
159 }
160
161 public class TransparentInheritFromSafeCriticalDefaultConstructor : TransparentClassWithSafeCriticalDefaultConstructor {
162
163         public TransparentInheritFromSafeCriticalDefaultConstructor ()
164         {
165         }
166 }
167
168 public class SafeInheritFromSafeCriticalDefaultConstructor : TransparentClassWithSafeCriticalDefaultConstructor {
169
170         [SecuritySafeCritical]
171         public SafeInheritFromSafeCriticalDefaultConstructor ()
172         {
173         }
174 }
175
176 public class Test
177 {
178         static bool haveError = false;
179
180         public static void error (string text)
181         {
182                 Console.WriteLine (text);
183                 haveError = true;
184         }
185
186         [SecurityCriticalAttribute]
187         static void CMethod ()
188         {
189                 //Console.WriteLine ("c");
190         }
191
192         [SecuritySafeCriticalAttribute]
193         static void SCMethod ()
194         {
195                 //Console.WriteLine ("sc");
196                 CMethod ();
197         }
198
199         static void doSCDev ()
200         {
201                 SCDevClass scdev = new SCDevClass ();
202                 scdev.Method ();
203         }
204
205         static void doCMethodDev ()
206         {
207                 CMethodDevClass cmdev = new CMethodDevClass ();
208                 error ("critical-derived object instantiated");
209                 cmdev.Method ();
210                 Console.WriteLine ("critical-derived method called");
211         }
212
213         static void doSCInterfaceDev ()
214         {
215                 CMethodInterface mi = new CInterfaceClass ();
216                 error ("safe-critical-interface-derived object instantiated");
217                 mi.Method ();
218                 error ("safe-critical-interface-derived method called");
219         }
220
221         /*
222         static unsafe void unsafeMethod ()
223         {
224                 byte *p = null;
225                 error ("unsafe method called");
226         }
227         */
228
229         static void doBadTransparentOverrideClass ()
230         {
231                 new BadTransparentOverrideClass ();
232         }
233
234         static void doBadSafeCriticalOverrideClass ()
235         {
236                 new BadSafeCriticalOverrideClass ();
237         }
238
239         static void doBadCriticalOverrideClass ()
240         {
241                 new BadCriticalOverrideClass ();
242         }
243
244         public static void TransparentReflectionCMethod ()
245         {
246         }
247
248         [SecurityCriticalAttribute]
249         public static void ReflectionCMethod ()
250         {
251                 error ("method called via reflection");
252         }
253
254         [SecurityCriticalAttribute]
255         public static unsafe void StringTest ()
256         {
257                 string str = "blabla";
258                 char [] arr = str.ToCharArray ();
259                 string r;
260
261                 fixed (char *tarr = arr) {
262                         int ss = 1, l = 3;
263                         r = new string (tarr, ss, l - ss);
264                 }
265         }
266
267         [SecuritySafeCriticalAttribute]
268         public static void CallStringTest ()
269         {
270                 StringTest ();
271         }
272
273         [DllImport ("/lib64/libc.so.6")]
274         static extern int getpid ();
275
276
277         static void ArraysCreatedByTransparentCaller ()
278         {
279                 // Transparent creating an array of a Critical type
280                 // using Class[] (rank == 1) throws a TypeLoadException on SL2 - but that looks like a bug
281                 // reported as https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=490406
282                 CClass[] c_array = new CClass [0];
283                 // Transparent creating an array of a SafeCritical type
284                 SCClass[] sc_array = new SCClass [0];
285
286                 // Transparent creating a multidimentional array of a Critical type
287                 CClass[,] c_multi = new CClass [0,0];
288                 // Transparent creating a multidimentional array of a SafeCritical type
289                 SCClass[,] sc_multi = new SCClass [0,0];
290
291                 // Transparent creating a jagged array of a Critical type
292                 CClass[][] c_jagged = new CClass [0][];
293                 // Transparent creating a jagged array of a Critical type
294                 SCClass[][] sc_jagged = new SCClass [0][];
295         }
296
297         [SecuritySafeCritical]
298         static void ArraysCreatedBySafeCriticalCaller ()
299         {
300                 // SafeCritical creating an array of a Critical type
301                 CClass[] c_array = new CClass [0];
302                 // SafeCritical creating an array of a SafeCritical type
303                 SCClass[] sc_array = new SCClass [0];
304
305                 // SafeCritical creating a multidimentional array of a Critical type
306                 CClass[,] c_multi = new CClass [0,0];
307                 // SafeCritical creating a multidimentional array of a SafeCritical type
308                 SCClass[,] sc_multi = new SCClass [0,0];
309
310                 // SafeCritical creating a jagged array of a Critical type
311                 CClass[][] c_jagged = new CClass [0][];
312                 // SafeCritical creating a jagged array of a Critical type
313                 SCClass[][] sc_jagged = new SCClass [0][];
314
315                 // Transparent Main could not call a critical method by itself
316                 ArraysCreatedByCriticalCaller ();
317         }
318
319         [SecurityCritical]
320         static void ArraysCreatedByCriticalCaller ()
321         {
322                 // Critical creating an array of a Critical type
323                 CClass[] c_array = new CClass [0];
324                 // Critical creating an array of a SafeCritical type
325                 SCClass[] sc_array = new SCClass [0];
326
327                 // Critical creating a multidimentional array of a Critical type
328                 CClass[,] c_multi = new CClass [0,0];
329                 // Critical creating a multidimentional array of a SafeCritical type
330                 SCClass[,] sc_multi = new SCClass [0,0];
331
332                 // Critical creating a jagged array of a Critical type
333                 CClass[][] c_jagged = new CClass [0][];
334                 // Critical creating a jagged array of a Critical type
335                 SCClass[][] sc_jagged = new SCClass [0][];
336         }
337
338         public static int Main ()
339         {
340                 SCMethod ();
341
342                 try {
343                         CMethod ();
344                         error ("static critical method called");
345                 } catch (MethodAccessException) {
346                 }
347
348                 SCClass sc = new SCClass ();
349                 sc.Method ();
350
351                 try {
352                         CClass c = new CClass (); // Illegal
353                         error ("critical object instantiated");
354                         c.Method ();    // Illegal
355                         error ("critical method called");
356                 } catch (MethodAccessException) {
357                 }
358
359                 try {
360                         doSCDev ();
361                         error ("security-critical-derived class error");
362                 } catch (TypeLoadException) {
363                 }
364
365                 try {
366                         doCMethodDev ();
367                 } catch (TypeLoadException) {
368                 }
369
370                 try {
371                         getpid ();
372                         error ("pinvoke called");
373                 } catch (MethodAccessException) {
374                 }
375
376                 try {
377                         MethodDelegate md = new MethodDelegate (CClass.StaticMethod);
378                         md ();
379                         error ("critical method called via delegate");
380                 } catch (MethodAccessException) {
381                 }
382
383                 try {
384                         CriticalClass.NestedClassInsideCritical.Method ();
385                 } catch (MethodAccessException) {
386                 }
387
388                 try {
389                         doSCInterfaceDev ();
390                 } catch (TypeLoadException) {
391                 }
392
393                 /*
394                 try {
395                         unsafeMethod ();
396                 } catch (VerificationException) {
397                 }
398                 */
399
400                 try {
401                         Type type = Type.GetType ("Test");
402                         MethodInfo method = type.GetMethod ("TransparentReflectionCMethod");
403
404                         method.Invoke(null, null);
405                 } catch (MethodAccessException) {
406                         error ("transparent method not called via reflection");
407                 }
408
409                 try {
410                         Type type = Type.GetType ("Test");
411                         MethodInfo method = type.GetMethod ("ReflectionCMethod");
412
413                         method.Invoke(null, null);
414                 } catch (MethodAccessException) {
415                 }
416
417                 try {
418                         Type type = Type.GetType ("Test");
419                         MethodInfo method = type.GetMethod ("TransparentReflectionCMethod");
420                         InvokeDelegate id = new InvokeDelegate (method.Invoke);
421
422                         id (null, null);
423                 } catch (MethodAccessException) {
424                         error ("transparent method not called via reflection delegate");
425                 }
426
427                 try {
428                         Type type = Type.GetType ("Test");
429                         MethodInfo method = type.GetMethod ("ReflectionCMethod");
430                         InvokeDelegate id = new InvokeDelegate (method.Invoke);
431
432                         id (null, null);
433                 } catch (MethodAccessException) {
434                 }
435
436
437                 // wrapper 7
438                 try {
439                         CallStringTest ();
440                 } catch (MethodAccessException) {
441                         error ("string test failed");
442                 }
443
444                 try {
445                         doBadTransparentOverrideClass ();
446                         error ("BadTransparentOverrideClass error");
447                 } catch (TypeLoadException) {
448                 }
449
450                 try {
451                         doBadSafeCriticalOverrideClass ();
452                         error ("BadSafeCriticalOverrideClass error");
453                 } catch (TypeLoadException) {
454                 }
455
456                 try {
457                         doBadCriticalOverrideClass ();
458                         error ("BadCriticalOverrideClass error");
459                 } catch (TypeLoadException) {
460                 }
461
462                 new TransparentClassWithSafeCriticalDefaultConstructor ();
463                 try {
464                         new TransparentInheritFromSafeCriticalDefaultConstructor ();
465                 } catch (TypeLoadException) {
466                 }
467                 new SafeInheritFromSafeCriticalDefaultConstructor ();
468
469                 // arrays creation tests
470                 ArraysCreatedByTransparentCaller ();
471                 ArraysCreatedBySafeCriticalCaller ();
472                 // the above also calls ArraysCreatedBySafeCriticalCaller since (Transparent) Main cannot call it directly
473
474                 if (haveError)
475                         return 1;
476
477 //              Console.WriteLine ("ok");
478                 return 0;
479         }
480 }