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>
14 #include "security-core-clr.h"
16 gboolean mono_security_core_clr_test
= FALSE
;
19 security_critical_attribute (void)
21 static MonoClass
*class = NULL
;
24 class = mono_class_from_name (mono_defaults
.corlib
, "System.Security",
25 "SecurityCriticalAttribute");
32 security_safe_critical_attribute (void)
34 static MonoClass
*class = NULL
;
37 class = mono_class_from_name (mono_defaults
.corlib
, "System.Security",
38 "SecuritySafeCriticalAttribute");
44 MonoSecurityCoreCLRLevel
45 mono_security_core_clr_level_from_cinfo (MonoCustomAttrInfo
*cinfo
, MonoImage
*image
)
47 int level
= MONO_SECURITY_CORE_CLR_TRANSPARENT
;
49 if (!mono_security_core_clr_test
&& !mono_security_core_clr_is_platform_image (image
))
52 if (cinfo
&& mono_custom_attrs_has_attr (cinfo
, security_safe_critical_attribute ()))
53 level
= MONO_SECURITY_CORE_CLR_SAFE_CRITICAL
;
54 if (cinfo
&& mono_custom_attrs_has_attr (cinfo
, security_critical_attribute ()))
55 level
= MONO_SECURITY_CORE_CLR_CRITICAL
;
60 MonoSecurityCoreCLRLevel
61 mono_security_core_clr_class_level (MonoClass
*class)
63 MonoCustomAttrInfo
*cinfo
= mono_custom_attrs_from_class (class);
64 MonoSecurityCoreCLRLevel lvl
= mono_security_core_clr_level_from_cinfo (cinfo
, class->image
);
67 mono_custom_attrs_free (cinfo
);
69 if (lvl
== MONO_SECURITY_CORE_CLR_TRANSPARENT
&& class->nested_in
)
70 return mono_security_core_clr_class_level (class->nested_in
);
75 MonoSecurityCoreCLRLevel
76 mono_security_core_clr_method_level (MonoMethod
*method
, gboolean with_class_level
)
78 MonoCustomAttrInfo
*cinfo
= mono_custom_attrs_from_method (method
);
79 MonoSecurityCoreCLRLevel level
= mono_security_core_clr_level_from_cinfo (cinfo
, method
->klass
->image
);
81 if (with_class_level
&& level
== MONO_SECURITY_CORE_CLR_TRANSPARENT
)
82 level
= mono_security_core_clr_class_level (method
->klass
);
85 mono_custom_attrs_free (cinfo
);
91 mono_security_core_clr_is_platform_image (MonoImage
*image
)
93 const char *prefix
= mono_assembly_getrootdir ();
94 int prefix_len
= strlen (prefix
);
95 static const char subprefix
[] = "/mono/2.1/";
96 int subprefix_len
= strlen (subprefix
);
100 if (strncmp (prefix
, image
->name
, prefix_len
) != 0)
102 if (strncmp (subprefix
, image
->name
+ prefix_len
, subprefix_len
) != 0)
104 if (strchr (image
->name
+ prefix_len
+ subprefix_len
, '/'))