Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / mscorlib / system / security / permissions / hostprotectionpermission.cs
blob837cac3c1e044ea46899ea3b253ed0635299ee7b
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 // HostProtectionPermission.cs
7 //
8 // <OWNER>Microsoft</OWNER>
9 //
11 namespace System.Security.Permissions
13 using System;
14 using System.IO;
15 using System.Security.Util;
16 using System.Text;
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
27 [Serializable]
28 [Flags]
29 [System.Runtime.InteropServices.ComVisible(true)]
30 public enum HostProtectionResource
32 None = 0x0,
33 //--------------------------------
34 Synchronization = 0x1,
35 SharedState = 0x2,
36 ExternalProcessMgmt = 0x4,
37 SelfAffectingProcessMgmt = 0x8,
38 ExternalThreading = 0x10,
39 SelfAffectingThreading = 0x20,
40 SecurityInfrastructure = 0x40,
41 UI = 0x80,
42 MayLeakOnAbort = 0x100,
43 //---------------------------------
44 All = 0x1ff,
47 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly | AttributeTargets.Delegate, AllowMultiple = true, Inherited = false )]
48 [System.Runtime.InteropServices.ComVisible(true)]
49 [Serializable]
50 #if FEATURE_CORECLR
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
71 : base( action )
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); }
121 public bool UI {
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()
133 if (m_unrestricted)
135 return new HostProtectionPermission( PermissionState.Unrestricted );
137 else
139 return new HostProtectionPermission( m_resources );
144 [Serializable]
145 sealed internal class HostProtectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission
147 //------------------------------------------------------
149 // GLOBALS
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 //------------------------------------------------------
159 // MEMBERS
161 //------------------------------------------------------
162 private HostProtectionResource m_resources;
164 //------------------------------------------------------
166 // CONSTRUCTORS
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;
175 else
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 //------------------------------------------------------
196 // Properties
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();
206 m_resources = value;
211 return m_resources;
215 //------------------------------------------------------
217 // IPermission interface implementation
219 //------------------------------------------------------
220 public override bool IsSubsetOf(IPermission target)
222 if (target == null)
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)
231 if (target == null)
232 return(this.Copy());
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)
241 if (target == null)
242 return null;
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)
247 return null;
248 return new HostProtectionPermission(newResources);
251 public override IPermission Copy()
253 return new HostProtectionPermission(m_resources);
256 #if FEATURE_CAS_POLICY
257 //------------------------------------------------------
259 // XML
261 //------------------------------------------------------
262 public override SecurityElement ToXml()
264 SecurityElement esd = CodeAccessPermission.CreatePermissionElement( this, this.GetType().FullName );
265 if(IsUnrestricted())
266 esd.AddAttribute( "Unrestricted", "true" );
267 else
268 esd.AddAttribute( "Resources", XMLUtil.BitFieldEnumToString( typeof( HostProtectionResource ), Resources ) );
269 return esd;
272 public override void FromXml(SecurityElement esd)
274 CodeAccessPermission.ValidateElement( esd, this );
275 if (XMLUtil.IsUnrestricted( esd ))
276 Resources = HostProtectionResource.All;
277 else
279 String resources = esd.Attribute( "Resources" );
280 if (resources == null)
281 Resources = HostProtectionResource.None;
282 else
283 Resources = (HostProtectionResource)Enum.Parse( typeof( HostProtectionResource ), resources );
286 #endif // FEATURE_CAS_POLICY
288 //------------------------------------------------------
290 // OBJECT OVERRIDES
292 //------------------------------------------------------
294 /// <internalonly/>
295 int IBuiltInPermission.GetTokenIndex()
297 return HostProtectionPermission.GetTokenIndex();
300 internal static int GetTokenIndex()
302 return BuiltInPermissionIndex.HostProtectionPermissionIndex;