3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // HostProtectionPermission.cs
8 // <OWNER>Microsoft</OWNER>
11 namespace System
.Security
.Permissions
15 using System
.Security
.Util
;
17 using System
.Threading
;
18 using System
.Runtime
.Remoting
;
19 using System
.Security
;
20 using System
.Runtime
.Serialization
;
21 using System
.Reflection
;
22 using System
.Globalization
;
23 using System
.Diagnostics
.Contracts
;
25 // Keep this enum in sync with tools\ngen\ngen.cpp and inc\mscoree.idl
29 [System
.Runtime
.InteropServices
.ComVisible(true)]
30 public enum HostProtectionResource
33 //--------------------------------
34 Synchronization
= 0x1,
36 ExternalProcessMgmt
= 0x4,
37 SelfAffectingProcessMgmt
= 0x8,
38 ExternalThreading
= 0x10,
39 SelfAffectingThreading
= 0x20,
40 SecurityInfrastructure
= 0x40,
42 MayLeakOnAbort
= 0x100,
43 //---------------------------------
47 [AttributeUsage(AttributeTargets
.Method
| AttributeTargets
.Constructor
| AttributeTargets
.Class
| AttributeTargets
.Struct
| AttributeTargets
.Assembly
| AttributeTargets
.Delegate
, AllowMultiple
= true, Inherited
= false )]
48 [System
.Runtime
.InteropServices
.ComVisible(true)]
51 // This needs to be in the asmmeta to enable SecAnnotate to successfully resolve and run the security rules. It gets marked
52 // as internal by BCLRewriter so we are simply marking it as FriendAccessAllowed so it stays in the asmmeta.
53 [System
.Runtime
.CompilerServices
.FriendAccessAllowedAttribute
]
54 #endif // FEATURE_CORECLR
55 #pragma warning disable 618
56 sealed public class HostProtectionAttribute
: CodeAccessSecurityAttribute
57 #pragma warning restore 618
59 private HostProtectionResource m_resources
= HostProtectionResource
.None
;
61 public HostProtectionAttribute()
62 #pragma warning disable 618
63 : base( SecurityAction
.LinkDemand
)
64 #pragma warning restore 618
68 #pragma warning disable 618
69 public HostProtectionAttribute( SecurityAction action
)
70 #pragma warning restore 618
73 #pragma warning disable 618
74 if (action
!= SecurityAction
.LinkDemand
)
75 #pragma warning restore 618
76 throw new ArgumentException(Environment
.GetResourceString("Argument_InvalidFlag"));
77 Contract
.EndContractBlock();
80 public HostProtectionResource Resources
{
81 get { return m_resources; }
82 set { m_resources = value; }
85 public bool Synchronization
{
86 get { return (m_resources & HostProtectionResource.Synchronization) != 0; }
87 set { m_resources = (value ? m_resources | HostProtectionResource.Synchronization : m_resources & ~HostProtectionResource.Synchronization); }
90 public bool SharedState
{
91 get { return (m_resources & HostProtectionResource.SharedState) != 0; }
92 set { m_resources = (value ? m_resources | HostProtectionResource.SharedState : m_resources & ~HostProtectionResource.SharedState); }
95 public bool ExternalProcessMgmt
{
96 get { return (m_resources & HostProtectionResource.ExternalProcessMgmt) != 0; }
97 set { m_resources = (value ? m_resources | HostProtectionResource.ExternalProcessMgmt : m_resources & ~HostProtectionResource.ExternalProcessMgmt); }
100 public bool SelfAffectingProcessMgmt
{
101 get { return (m_resources & HostProtectionResource.SelfAffectingProcessMgmt) != 0; }
102 set { m_resources = (value ? m_resources | HostProtectionResource.SelfAffectingProcessMgmt : m_resources & ~HostProtectionResource.SelfAffectingProcessMgmt); }
105 public bool ExternalThreading
{
106 get { return (m_resources & HostProtectionResource.ExternalThreading) != 0; }
107 set { m_resources = (value ? m_resources | HostProtectionResource.ExternalThreading : m_resources & ~HostProtectionResource.ExternalThreading); }
110 public bool SelfAffectingThreading
{
111 get { return (m_resources & HostProtectionResource.SelfAffectingThreading) != 0; }
112 set { m_resources = (value ? m_resources | HostProtectionResource.SelfAffectingThreading : m_resources & ~HostProtectionResource.SelfAffectingThreading); }
115 [System
.Runtime
.InteropServices
.ComVisible(true)]
116 public bool SecurityInfrastructure
{
117 get { return (m_resources & HostProtectionResource.SecurityInfrastructure) != 0; }
118 set { m_resources = (value ? m_resources | HostProtectionResource.SecurityInfrastructure : m_resources & ~HostProtectionResource.SecurityInfrastructure); }
122 get { return (m_resources & HostProtectionResource.UI) != 0; }
123 set { m_resources = (value ? m_resources | HostProtectionResource.UI : m_resources & ~HostProtectionResource.UI); }
126 public bool MayLeakOnAbort
{
127 get { return (m_resources & HostProtectionResource.MayLeakOnAbort) != 0; }
128 set { m_resources = (value ? m_resources | HostProtectionResource.MayLeakOnAbort : m_resources & ~HostProtectionResource.MayLeakOnAbort); }
131 public override IPermission
CreatePermission()
135 return new HostProtectionPermission( PermissionState
.Unrestricted
);
139 return new HostProtectionPermission( m_resources
);
145 sealed internal class HostProtectionPermission
: CodeAccessPermission
, IUnrestrictedPermission
, IBuiltInPermission
147 //------------------------------------------------------
151 //------------------------------------------------------
153 // This value is set by PermissionSet.FilterHostProtectionPermissions. It is only used for
154 // constructing a HostProtectionException object. Changing it will not affect HostProtection.
155 internal static volatile HostProtectionResource protectedResources
= HostProtectionResource
.None
;
157 //------------------------------------------------------
161 //------------------------------------------------------
162 private HostProtectionResource m_resources
;
164 //------------------------------------------------------
168 //------------------------------------------------------
169 public HostProtectionPermission(PermissionState state
)
171 if (state
== PermissionState
.Unrestricted
)
172 Resources
= HostProtectionResource
.All
;
173 else if (state
== PermissionState
.None
)
174 Resources
= HostProtectionResource
.None
;
176 throw new ArgumentException(Environment
.GetResourceString("Argument_InvalidPermissionState"));
179 public HostProtectionPermission(HostProtectionResource resources
)
181 Resources
= resources
;
184 //------------------------------------------------------
186 // IPermission interface implementation
188 //------------------------------------------------------
189 public bool IsUnrestricted()
191 return Resources
== HostProtectionResource
.All
;
194 //------------------------------------------------------
198 //------------------------------------------------------
199 public HostProtectionResource Resources
203 if(value < HostProtectionResource
.None
|| value > HostProtectionResource
.All
)
204 throw new ArgumentException(Environment
.GetResourceString("Arg_EnumIllegalVal", (int)value));
205 Contract
.EndContractBlock();
215 //------------------------------------------------------
217 // IPermission interface implementation
219 //------------------------------------------------------
220 public override bool IsSubsetOf(IPermission target
)
223 return m_resources
== HostProtectionResource
.None
;
224 if(this.GetType() != target
.GetType())
225 throw new ArgumentException( Environment
.GetResourceString("Argument_WrongType", this.GetType().FullName
) );
226 return ((uint)this.m_resources
& (uint)((HostProtectionPermission
)target
).m_resources
) == (uint)this.m_resources
;
229 public override IPermission
Union(IPermission target
)
233 if(this.GetType() != target
.GetType())
234 throw new ArgumentException( Environment
.GetResourceString("Argument_WrongType", this.GetType().FullName
) );
235 HostProtectionResource newResources
= (HostProtectionResource
)((uint)this.m_resources
| (uint)((HostProtectionPermission
)target
).m_resources
);
236 return new HostProtectionPermission(newResources
);
239 public override IPermission
Intersect(IPermission target
)
243 if(this.GetType() != target
.GetType())
244 throw new ArgumentException( Environment
.GetResourceString("Argument_WrongType", this.GetType().FullName
) );
245 HostProtectionResource newResources
= (HostProtectionResource
)((uint)this.m_resources
& (uint)((HostProtectionPermission
)target
).m_resources
);
246 if(newResources
== HostProtectionResource
.None
)
248 return new HostProtectionPermission(newResources
);
251 public override IPermission
Copy()
253 return new HostProtectionPermission(m_resources
);
256 #if FEATURE_CAS_POLICY
257 //------------------------------------------------------
261 //------------------------------------------------------
262 public override SecurityElement
ToXml()
264 SecurityElement esd
= CodeAccessPermission
.CreatePermissionElement( this, this.GetType().FullName
);
266 esd
.AddAttribute( "Unrestricted", "true" );
268 esd
.AddAttribute( "Resources", XMLUtil
.BitFieldEnumToString( typeof( HostProtectionResource
), Resources
) );
272 public override void FromXml(SecurityElement esd
)
274 CodeAccessPermission
.ValidateElement( esd
, this );
275 if (XMLUtil
.IsUnrestricted( esd
))
276 Resources
= HostProtectionResource
.All
;
279 String resources
= esd
.Attribute( "Resources" );
280 if (resources
== null)
281 Resources
= HostProtectionResource
.None
;
283 Resources
= (HostProtectionResource
)Enum
.Parse( typeof( HostProtectionResource
), resources
);
286 #endif // FEATURE_CAS_POLICY
288 //------------------------------------------------------
292 //------------------------------------------------------
295 int IBuiltInPermission
.GetTokenIndex()
297 return HostProtectionPermission
.GetTokenIndex();
300 internal static int GetTokenIndex()
302 return BuiltInPermissionIndex
.HostProtectionPermissionIndex
;