summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Security/Permissions/HostProtectionPermission.cs
blob: c4facbb67e58244d8960644b280df1e41d155be8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Licensed to the .NET Foundation under one or more agreements.
// 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.Permissions
{
    using System;
    using System.IO;
    using System.Security.Util;
    using System.Text;
    using System.Threading;
    using System.Runtime.Remoting;
    using System.Security;
    using System.Runtime.Serialization;
    using System.Reflection;
    using System.Globalization;
    using System.Diagnostics.Contracts;

    // Keep this enum in sync with tools\ngen\ngen.cpp and inc\mscoree.idl

[Serializable]
    [Flags]
    [System.Runtime.InteropServices.ComVisible(true)]
    public enum HostProtectionResource
    {
        None                        = 0x0,
        //--------------------------------
        Synchronization             = 0x1,
        SharedState                 = 0x2,
        ExternalProcessMgmt         = 0x4,
        SelfAffectingProcessMgmt    = 0x8,
        ExternalThreading           = 0x10,
        SelfAffectingThreading      = 0x20,
        SecurityInfrastructure      = 0x40,
        UI                          = 0x80,
        MayLeakOnAbort              = 0x100,
        //---------------------------------
        All                         = 0x1ff,
    }

    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly | AttributeTargets.Delegate, AllowMultiple = true, Inherited = false )] 
    [System.Runtime.InteropServices.ComVisible(true)]
    [Serializable]
    // This needs to be in the asmmeta to enable SecAnnotate to successfully resolve and run the security rules. It gets marked
    // as internal by BCLRewriter so we are simply marking it as FriendAccessAllowed so it stays in the asmmeta.
    [System.Runtime.CompilerServices.FriendAccessAllowedAttribute]
#pragma warning disable 618
    sealed public class HostProtectionAttribute : CodeAccessSecurityAttribute
#pragma warning restore 618
    {
        private HostProtectionResource m_resources = HostProtectionResource.None;

        public HostProtectionAttribute()
#pragma warning disable 618
            : base( SecurityAction.LinkDemand )
#pragma warning restore 618
        {
        }

#pragma warning disable 618
        public HostProtectionAttribute( SecurityAction action )
#pragma warning restore 618
            : base( action )
        {
#pragma warning disable 618
            if (action != SecurityAction.LinkDemand)
#pragma warning restore 618
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"));
            Contract.EndContractBlock();
        }

        public HostProtectionResource Resources {
            get { return m_resources; }
            set { m_resources = value; }
        }

        public bool Synchronization {
            get { return (m_resources & HostProtectionResource.Synchronization) != 0; }
            set { m_resources = (value ? m_resources | HostProtectionResource.Synchronization : m_resources & ~HostProtectionResource.Synchronization); }
        }

        public bool SharedState {
            get { return (m_resources & HostProtectionResource.SharedState) != 0; }
            set { m_resources = (value ? m_resources | HostProtectionResource.SharedState : m_resources & ~HostProtectionResource.SharedState); }
        }

        public bool ExternalProcessMgmt {
            get { return (m_resources & HostProtectionResource.ExternalProcessMgmt) != 0; }
            set { m_resources = (value ? m_resources | HostProtectionResource.ExternalProcessMgmt : m_resources & ~HostProtectionResource.ExternalProcessMgmt); }
        }

        public bool SelfAffectingProcessMgmt {
            get { return (m_resources & HostProtectionResource.SelfAffectingProcessMgmt) != 0; }
            set { m_resources = (value ? m_resources | HostProtectionResource.SelfAffectingProcessMgmt : m_resources & ~HostProtectionResource.SelfAffectingProcessMgmt); }
        }

        public bool ExternalThreading {
            get { return (m_resources & HostProtectionResource.ExternalThreading) != 0; }
            set { m_resources = (value ? m_resources | HostProtectionResource.ExternalThreading : m_resources & ~HostProtectionResource.ExternalThreading); }
        }

        public bool SelfAffectingThreading {
            get { return (m_resources & HostProtectionResource.SelfAffectingThreading) != 0; }
            set { m_resources = (value ? m_resources | HostProtectionResource.SelfAffectingThreading : m_resources & ~HostProtectionResource.SelfAffectingThreading); }
        }

[System.Runtime.InteropServices.ComVisible(true)]
        public bool SecurityInfrastructure {
            get { return (m_resources & HostProtectionResource.SecurityInfrastructure) != 0; }
            set { m_resources = (value ? m_resources | HostProtectionResource.SecurityInfrastructure : m_resources & ~HostProtectionResource.SecurityInfrastructure); }
        }

        public bool UI {
            get { return (m_resources & HostProtectionResource.UI) != 0; }
            set { m_resources = (value ? m_resources | HostProtectionResource.UI : m_resources & ~HostProtectionResource.UI); }
        }

        public bool MayLeakOnAbort {
            get { return (m_resources & HostProtectionResource.MayLeakOnAbort) != 0; }
            set { m_resources = (value ? m_resources | HostProtectionResource.MayLeakOnAbort : m_resources & ~HostProtectionResource.MayLeakOnAbort); }
        }

        public override IPermission CreatePermission()
        {
            if (m_unrestricted)
            {
                return new HostProtectionPermission( PermissionState.Unrestricted );
            }
            else
            {
                return new HostProtectionPermission( m_resources );
            }
        }
    }

    [Serializable]
    sealed internal class HostProtectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission
    {
        //------------------------------------------------------
        //
        // GLOBALS
        //
        //------------------------------------------------------

        // This value is set by PermissionSet.FilterHostProtectionPermissions.  It is only used for
        // constructing a HostProtectionException object.  Changing it will not affect HostProtection.
        internal static volatile HostProtectionResource protectedResources = HostProtectionResource.None;

        //------------------------------------------------------
        //
        // MEMBERS
        //
        //------------------------------------------------------
        private HostProtectionResource m_resources;

        //------------------------------------------------------
        //
        // CONSTRUCTORS
        //
        //------------------------------------------------------
        public HostProtectionPermission(PermissionState state)
        {
            if (state == PermissionState.Unrestricted)
                Resources = HostProtectionResource.All;
            else if (state == PermissionState.None)
                Resources = HostProtectionResource.None;
            else
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
        }

        public HostProtectionPermission(HostProtectionResource resources)
        {
            Resources = resources;
        }

        //------------------------------------------------------
        //
        // IPermission interface implementation
        //
        //------------------------------------------------------
        public bool IsUnrestricted()
        {
            return Resources == HostProtectionResource.All;
        }

        //------------------------------------------------------
        //
        // Properties
        //
        //------------------------------------------------------
        public HostProtectionResource Resources
        {
            set
            {
                if(value < HostProtectionResource.None || value > HostProtectionResource.All)
                    throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)value));
                Contract.EndContractBlock();
                m_resources = value;
            }

            get
            {
                return m_resources;
            }
        }

        //------------------------------------------------------
        //
        // IPermission interface implementation
        //
        //------------------------------------------------------
        public override bool IsSubsetOf(IPermission target)
        {
            if (target == null)
                return m_resources == HostProtectionResource.None;
            if(this.GetType() != target.GetType())
                throw new ArgumentException( Environment.GetResourceString("Argument_WrongType", this.GetType().FullName) );
            return ((uint)this.m_resources & (uint)((HostProtectionPermission)target).m_resources) == (uint)this.m_resources;
        }

        public override IPermission Union(IPermission target)
        {
            if (target == null)
                return(this.Copy());
            if(this.GetType() != target.GetType())
                throw new ArgumentException( Environment.GetResourceString("Argument_WrongType", this.GetType().FullName) );
            HostProtectionResource newResources = (HostProtectionResource)((uint)this.m_resources | (uint)((HostProtectionPermission)target).m_resources);
            return new HostProtectionPermission(newResources);
        }

        public override IPermission Intersect(IPermission target)
        {
            if (target == null)
                return null;
            if(this.GetType() != target.GetType())
                throw new ArgumentException( Environment.GetResourceString("Argument_WrongType", this.GetType().FullName) );
            HostProtectionResource newResources = (HostProtectionResource)((uint)this.m_resources & (uint)((HostProtectionPermission)target).m_resources);
            if(newResources == HostProtectionResource.None)
                return null;
            return new HostProtectionPermission(newResources);
        }

        public override IPermission Copy()
        {
            return new HostProtectionPermission(m_resources);
        }

        //------------------------------------------------------
        //
        // OBJECT OVERRIDES
        //
        //------------------------------------------------------

        /// <internalonly/>
        int IBuiltInPermission.GetTokenIndex()
        {
            return HostProtectionPermission.GetTokenIndex();
        }

        internal static int GetTokenIndex()
        {
            return BuiltInPermissionIndex.HostProtectionPermissionIndex;
        }
    }
}