diff options
author | Jiyoung Yun <jy910.yun@samsung.com> | 2016-12-27 16:46:08 +0900 |
---|---|---|
committer | Jiyoung Yun <jy910.yun@samsung.com> | 2016-12-27 16:46:08 +0900 |
commit | db20f3f1bb8595633a7e16c8900fd401a453a6b5 (patch) | |
tree | e5435159cd1bf0519276363a6fe1663d1721bed3 /src/mscorlib/src/System/Security/PermissionToken.cs | |
parent | 4b4aad7217d3292650e77eec2cf4c198ea9c3b4b (diff) | |
download | coreclr-db20f3f1bb8595633a7e16c8900fd401a453a6b5.tar.gz coreclr-db20f3f1bb8595633a7e16c8900fd401a453a6b5.tar.bz2 coreclr-db20f3f1bb8595633a7e16c8900fd401a453a6b5.zip |
Imported Upstream version 1.0.0.9127upstream/1.0.0.9127
Diffstat (limited to 'src/mscorlib/src/System/Security/PermissionToken.cs')
-rw-r--r-- | src/mscorlib/src/System/Security/PermissionToken.cs | 202 |
1 files changed, 11 insertions, 191 deletions
diff --git a/src/mscorlib/src/System/Security/PermissionToken.cs b/src/mscorlib/src/System/Security/PermissionToken.cs index e78c0f1a93..5c6a322c1c 100644 --- a/src/mscorlib/src/System/Security/PermissionToken.cs +++ b/src/mscorlib/src/System/Security/PermissionToken.cs @@ -2,7 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -namespace System.Security { +namespace System.Security +{ using System; using System.Security.Util; using System.Security.Permissions; @@ -11,6 +12,7 @@ namespace System.Security { using System.Threading; using System.Globalization; using System.Runtime.CompilerServices; + using System.Diagnostics; using System.Diagnostics.Contracts; [Flags] @@ -34,7 +36,6 @@ namespace System.Security { _info = CultureInfo.InvariantCulture.TextInfo; } - [System.Security.SecuritySafeCritical] // auto-generated public int Compare(Object a, Object b) { String strA = a as String; @@ -64,7 +65,7 @@ namespace System.Security { // The data structure consuming this will be responsible for dealing with null objects as keys. public int GetHashCode(Object obj) { - if (obj == null) throw new ArgumentNullException("obj"); + if (obj == null) throw new ArgumentNullException(nameof(obj)); Contract.EndContractBlock(); String str = obj as String; @@ -90,20 +91,13 @@ namespace System.Security { internal sealed class PermissionToken : ISecurityEncodable { private static readonly PermissionTokenFactory s_theTokenFactory; -#if FEATURE_CAS_POLICY - private static volatile ReflectionPermission s_reflectPerm = null; -#endif // FEATURE_CAS_POLICY - private const string c_mscorlibName = System.CoreLib.Name; internal int m_index; internal volatile PermissionTokenType m_type; -#if FEATURE_CAS_POLICY - internal String m_strTypeName; -#endif // FEATURE_CAS_POLICY static internal TokenBasedSet s_tokenSet = new TokenBasedSet(); internal static bool IsMscorlibClassName (string className) { - Contract.Assert( c_mscorlibName == ((RuntimeAssembly)Assembly.GetExecutingAssembly()).GetSimpleName(), + Debug.Assert( c_mscorlibName == ((RuntimeAssembly)Assembly.GetExecutingAssembly()).GetSimpleName(), System.CoreLib.Name+" name mismatch" ); // If the class name does not look like a fully qualified name, we cannot simply determine if it's @@ -119,11 +113,7 @@ namespace System.Security { // Search for the string 'mscorlib' in the classname. If we find it, we will conservatively assume it's an mscorlib.dll type and load it. for (int i = index; i < className.Length; i++) { -#if FEATURE_CORECLR - if (className[i] == 's' || className[i] == 'S') -#else - if (className[i] == 'm' || className[i] == 'M') -#endif + if (className[i] == 's' || className[i] == 'S') { if (String.Compare(className, i, c_mscorlibName, 0, c_mscorlibName.Length, StringComparison.OrdinalIgnoreCase) == 0) return true; @@ -145,39 +135,14 @@ namespace System.Security { { m_index = index; m_type = type; -#if FEATURE_CAS_POLICY - m_strTypeName = strTypeName; -#endif // FEATURE_CAS_POLICY } - [System.Security.SecurityCritical] // auto-generated public static PermissionToken GetToken(Type cls) { if (cls == null) return null; - -#if FEATURE_CAS_POLICY - if (cls.GetInterface( "System.Security.Permissions.IBuiltInPermission" ) != null) - { - if (s_reflectPerm == null) - s_reflectPerm = new ReflectionPermission(PermissionState.Unrestricted); - s_reflectPerm.Assert(); - MethodInfo method = cls.GetMethod( "GetTokenIndex", BindingFlags.Static | BindingFlags.NonPublic ); - Contract.Assert( method != null, "IBuiltInPermission types should have a static method called 'GetTokenIndex'" ); - - // GetTokenIndex needs to be invoked without any security checks, since doing a security check - // will involve a ReflectionTargetDemand which creates a CompressedStack and attempts to get the - // token. - RuntimeMethodInfo getTokenIndex = method as RuntimeMethodInfo; - Contract.Assert(getTokenIndex != null, "method is not a RuntimeMethodInfo"); - int token = (int)getTokenIndex.UnsafeInvoke(null, BindingFlags.Default, null, null, null); - return s_theTokenFactory.BuiltInGetToken(token, null, cls); - } - else -#endif // FEATURE_CAS_POLICY - { - return s_theTokenFactory.GetToken(cls, null); - } + + return s_theTokenFactory.GetToken(cls, null); } public static PermissionToken GetToken(IPermission perm) @@ -193,82 +158,6 @@ namespace System.Security { return s_theTokenFactory.GetToken(perm.GetType(), perm); } -#if FEATURE_CAS_POLICY - public static PermissionToken GetToken(String typeStr) - { - return GetToken( typeStr, false ); - } - -#if _DEBUG - [System.Security.SecuritySafeCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable - private static void GetTokenHelper(String typeStr) - { - new PermissionSet(PermissionState.Unrestricted).Assert(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - Type type = RuntimeTypeHandle.GetTypeByName( typeStr.Trim().Replace( '\'', '\"' ), ref stackMark); - Contract.Assert( (type == null) || (type.Module.Assembly != System.Reflection.Assembly.GetExecutingAssembly()) || (typeStr.IndexOf("mscorlib", StringComparison.Ordinal) < 0), - "We should not go through this path for mscorlib based permissions" ); - } -#endif - - public static PermissionToken GetToken(String typeStr, bool bCreateMscorlib) - { - if (typeStr == null) - return null; - - if (IsMscorlibClassName( typeStr )) - { - if (!bCreateMscorlib) - { - return null; - } - else - { - return FindToken( Type.GetType( typeStr ) ); - } - } - else - { - PermissionToken token = s_theTokenFactory.GetToken(typeStr); -#if _DEBUG - GetTokenHelper(typeStr); -#endif - return token; - } - } - - [SecuritySafeCritical] - public static PermissionToken FindToken( Type cls ) - { - if (cls == null) - return null; - -#if FEATURE_CAS_POLICY - if (cls.GetInterface( "System.Security.Permissions.IBuiltInPermission" ) != null) - { - if (s_reflectPerm == null) - s_reflectPerm = new ReflectionPermission(PermissionState.Unrestricted); - s_reflectPerm.Assert(); - MethodInfo method = cls.GetMethod( "GetTokenIndex", BindingFlags.Static | BindingFlags.NonPublic ); - Contract.Assert( method != null, "IBuiltInPermission types should have a static method called 'GetTokenIndex'" ); - - // GetTokenIndex needs to be invoked without any security checks, since doing a security check - // will involve a ReflectionTargetDemand which creates a CompressedStack and attempts to get the - // token. - RuntimeMethodInfo getTokenIndex = method as RuntimeMethodInfo; - Contract.Assert(getTokenIndex != null, "method is not a RuntimeMethodInfo"); - int token = (int)getTokenIndex.UnsafeInvoke(null, BindingFlags.Default, null, null, null); - return s_theTokenFactory.BuiltInGetToken(token, null, cls); - } - else -#endif // FEATURE_CAS_POLICY - { - return s_theTokenFactory.FindToken( cls ); - } - } -#endif // FEATURE_CAS_POLICY - public static PermissionToken FindTokenByIndex( int i ) { return s_theTokenFactory.FindTokenByIndex( i ); @@ -289,41 +178,6 @@ namespace System.Security { return true; } - -#if FEATURE_CAS_POLICY - public SecurityElement ToXml() - { - Contract.Assert( (m_type & PermissionTokenType.DontKnow) == 0, "Should have valid token type when ToXml is called" ); - SecurityElement elRoot = new SecurityElement( "PermissionToken" ); - if ((m_type & PermissionTokenType.BuiltIn) != 0) - elRoot.AddAttribute( "Index", "" + this.m_index ); - else - elRoot.AddAttribute( "Name", SecurityElement.Escape( m_strTypeName ) ); - elRoot.AddAttribute("Type", m_type.ToString("F")); - return elRoot; - } - - public void FromXml(SecurityElement elRoot) - { - // For the most part there is no parameter checking here since this is an - // internal class and the serialization/deserialization path is controlled. - - if (!elRoot.Tag.Equals( "PermissionToken" )) - Contract.Assert( false, "Tried to deserialize non-PermissionToken element here" ); - - String strName = elRoot.Attribute( "Name" ); - PermissionToken realToken; - if (strName != null) - realToken = GetToken( strName, true ); - else - realToken = FindTokenByIndex( Int32.Parse( elRoot.Attribute( "Index" ), CultureInfo.InvariantCulture ) ); - - this.m_index = realToken.m_index; - this.m_type = (PermissionTokenType) Enum.Parse(typeof(PermissionTokenType), elRoot.Attribute("Type")); - Contract.Assert((this.m_type & PermissionTokenType.DontKnow) == 0, "Should have valid token type when FromXml is called."); - this.m_strTypeName = realToken.m_strTypeName; - } -#endif // FEATURE_CAS_POLICY } // Package access only @@ -357,33 +211,6 @@ namespace System.Security { m_indexTable = new Hashtable(size); } -#if FEATURE_CAS_POLICY - [SecuritySafeCritical] - internal PermissionToken FindToken( Type cls ) - { - IntPtr typePtr = cls.TypeHandle.Value; - PermissionToken tok = (PermissionToken)m_handleTable[typePtr]; - - if (tok != null) - return tok; - - if (m_tokenTable == null) - return null; - - tok = (PermissionToken)m_tokenTable[cls.AssemblyQualifiedName]; - - if (tok != null) - { - lock (this) - { - m_handleTable.Add(typePtr, tok); - } - } - - return tok; - } -#endif // FEATURE_CAS_POLICY - internal PermissionToken FindTokenByIndex( int i ) { PermissionToken token; @@ -400,10 +227,9 @@ namespace System.Security { return token; } - [SecuritySafeCritical] internal PermissionToken GetToken(Type cls, IPermission perm) { - Contract.Assert( cls != null, "Must pass in valid type" ); + Debug.Assert( cls != null, "Must pass in valid type" ); IntPtr typePtr = cls.TypeHandle.Value; object tok = m_handleTable[typePtr]; @@ -459,22 +285,16 @@ namespace System.Security { { if (perm != null) { - Contract.Assert( !(perm is IBuiltInPermission), "This should not be called for built-ins" ); + Debug.Assert( !(perm is IBuiltInPermission), "This should not be called for built-ins" ); ((PermissionToken)tok).m_type = PermissionTokenType.IUnrestricted; -#if FEATURE_CAS_POLICY - ((PermissionToken)tok).m_strTypeName = perm.GetType().AssemblyQualifiedName; -#endif // FEATURE_CAS_POLICY } else { - Contract.Assert( cls.GetInterface( "System.Security.Permissions.IBuiltInPermission" ) == null, "This shoudl not be called for built-ins" ); + Debug.Assert( cls.GetInterface( "System.Security.Permissions.IBuiltInPermission" ) == null, "This shoudl not be called for built-ins" ); if (cls.GetInterface(s_unrestrictedPermissionInferfaceName) != null) ((PermissionToken)tok).m_type = PermissionTokenType.IUnrestricted; else ((PermissionToken)tok).m_type = PermissionTokenType.Normal; -#if FEATURE_CAS_POLICY - ((PermissionToken)tok).m_strTypeName = cls.AssemblyQualifiedName; -#endif // FEATURE_CAS_POLICY } } |