TARGET_J2EE/JVM fixes
[mono.git] / mcs / class / corlib / System.Security / SecurityManager.cs
index b7b48a74c4334435de2b42a2817c44d4001658d6..6016ddeb37fd7bff1eea28c0e4abd7f0a9fa6078 100644 (file)
@@ -62,9 +62,9 @@ namespace System.Security {
 #endif
                private static object _lockObject;
                private static ArrayList _hierarchy;
-               private static PermissionSet _fullTrust; // for [AllowPartiallyTrustedCallers]
                private static IPermission _unmanagedCode;
                private static Hashtable _declsecCache;
+               private static PolicyLevel _level;
 
                static SecurityManager () 
                {
@@ -80,7 +80,7 @@ namespace System.Security {
                        get;
 
                        [MethodImplAttribute (MethodImplOptions.InternalCall)]
-                       [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+                       [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                        set;
                }
 
@@ -89,7 +89,7 @@ namespace System.Security {
                        get;
 
                        [MethodImplAttribute (MethodImplOptions.InternalCall)]
-                       [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+                       [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                        set;
                }
 
@@ -99,7 +99,7 @@ namespace System.Security {
                // NOTE: This method doesn't show in the class library status page because
                // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
                // But it's there!
-               [MonoTODO]
+               [MonoTODO ("works for fulltrust (empty), documentation doesn't really make sense, type wise")]
                [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey = "0x00000000000000000400000000000000")]
                public static void GetZoneAndOrigin (out ArrayList zone, out ArrayList origin) 
                {
@@ -120,42 +120,80 @@ namespace System.Security {
                        // - Not affected by overrides (like Assert, Deny and PermitOnly)
                        // - calls IsSubsetOf even for non CAS permissions
                        //   (i.e. it does call Demand so any code there won't be executed)
+#if NET_2_0
+                       // with 2.0 identity permission are unrestrictable
                        return IsGranted (Assembly.GetCallingAssembly (), perm);
+#else
+                       if (perm is IUnrestrictedPermission)
+                               return IsGranted (Assembly.GetCallingAssembly (), perm);
+                       else
+                               return IsGrantedRestricted (Assembly.GetCallingAssembly (), perm);
+#endif
                }
 
-               internal static bool IsGranted (Assembly a, IPermission perm)
+#if !NET_2_0
+               // only for permissions that do not implement IUnrestrictedPermission
+               internal static bool IsGrantedRestricted (Assembly a, IPermission perm)
                {
-                       CodeAccessPermission grant = null;
-
-                       if (a.GrantedPermissionSet != null) {
-                               grant = (CodeAccessPermission) a.GrantedPermissionSet.GetPermission (perm.GetType ());
-                               if (grant == null) {
-                                       if (!a.GrantedPermissionSet.IsUnrestricted () || !(perm is IUnrestrictedPermission)) {
-                                               return perm.IsSubsetOf (null);
-                                       }
-                               } else if (!perm.IsSubsetOf (grant)) {
+                       PermissionSet granted = a.GrantedPermissionSet;\r
+                       if (granted != null) {\r
+                               CodeAccessPermission grant = (CodeAccessPermission) granted.GetPermission (perm.GetType ());
+                               if (!perm.IsSubsetOf (grant)) {
                                        return false;
                                }
                        }
 
-                       if (a.DeniedPermissionSet != null) {
+                       PermissionSet denied = a.DeniedPermissionSet;
+                       if (denied != null) {\r
                                CodeAccessPermission refuse = (CodeAccessPermission) a.DeniedPermissionSet.GetPermission (perm.GetType ());
                                if ((refuse != null) && perm.IsSubsetOf (refuse))
                                        return false;
                        }
                        return true;
                }
-
-               internal static bool IsGranted (Assembly a, PermissionSet ps, bool noncas)
+#endif
+               // note: in 2.0 *all* permissions (including identity permissions) support unrestricted
+               internal static bool IsGranted (Assembly a, IPermission perm)
+               {
+                       PermissionSet granted = a.GrantedPermissionSet;\r
+                       if ((granted != null) && !granted.IsUnrestricted ()) {\r
+                               CodeAccessPermission grant = (CodeAccessPermission) granted.GetPermission (perm.GetType ());\r
+                               if (!perm.IsSubsetOf (grant)) {\r
+                                       return false;\r
+                               }\r
+                       }\r
+\r
+                       PermissionSet denied = a.DeniedPermissionSet;\r
+                       if ((denied != null) && !denied.IsEmpty ()) {\r
+                               if (denied.IsUnrestricted ())\r
+                                       return false;\r
+                               CodeAccessPermission refuse = (CodeAccessPermission) a.DeniedPermissionSet.GetPermission (perm.GetType ());\r
+                               if ((refuse != null) && perm.IsSubsetOf (refuse))\r
+                                       return false;\r
+                       }\r
+                       return true;\r
+               }
+
+               internal static IPermission CheckPermissionSet (Assembly a, PermissionSet ps, bool noncas)
                {
                        if (ps.IsEmpty ())
-                               return true;
+                               return null;
 
                        foreach (IPermission p in ps) {
                                // note: this may contains non CAS permissions
                                if ((!noncas) && (p is CodeAccessPermission)) {
-                                       if (!SecurityManager.IsGranted (a, p))
-                                               return false;
+#if NET_2_0
+                                       if (!IsGranted (a, p))
+                                               return p;
+#else
+                                       if (p is IUnrestrictedPermission) {
+                                               if (!IsGranted (a, p))
+                                                       return p;
+                                       } else {
+                                               if (!IsGrantedRestricted (a, p))
+                                                       return p;
+                                       }
+#endif
                                } else {
                                        // but non-CAS will throw on failure...
                                        try {
@@ -163,31 +201,34 @@ namespace System.Security {
                                        }
                                        catch (SecurityException) {
                                                // ... so we catch
-                                               return false;
+                                               return p;
                                        }
                                }
                        }
-                       return true;
+                       return null;
                }
 
-               internal static bool IsGranted (AppDomain ad, PermissionSet ps)
+               internal static IPermission CheckPermissionSet (AppDomain ad, PermissionSet ps)
                {
                        if ((ps == null) || ps.IsEmpty ())
-                               return true;
+                               return null;
 
                        PermissionSet granted = ad.GrantedPermissionSet;
                        if (granted == null)
-                               return true;
+                               return null;
+                       if ((ad.GrantedPermissionSet.Count == 0) && ad.GrantedPermissionSet.IsUnrestricted ())
+                               return null;
 
                        foreach (IPermission p in ps) {
                                if (p is CodeAccessPermission) {
                                        CodeAccessPermission grant = (CodeAccessPermission) granted.GetPermission (p.GetType ());
                                        if (grant == null) {
                                                if (!granted.IsUnrestricted () || !(p is IUnrestrictedPermission)) {
-                                                       return p.IsSubsetOf (null);
+                                                       if (!p.IsSubsetOf (null))
+                                                               return p;
                                                }
                                        } else if (!p.IsSubsetOf (grant)) {
-                                               return false;
+                                               return p;
                                        }
                                } else {
                                        // but non-CAS will throw on failure...
@@ -196,14 +237,14 @@ namespace System.Security {
                                        }
                                        catch (SecurityException) {
                                                // ... so we catch
-                                               return false;
+                                               return p;
                                        }
                                }
                        }
-                       return true;
+                       return null;
                }
 
-               [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+               [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                public static PolicyLevel LoadPolicyLevelFromFile (string path, PolicyLevelType type)
                {
                        if (path == null)
@@ -220,7 +261,7 @@ namespace System.Security {
                        return pl;
                }
 
-               [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+               [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                public static PolicyLevel LoadPolicyLevelFromString (string str, PolicyLevelType type)
                {
                        if (null == str)
@@ -237,7 +278,7 @@ namespace System.Security {
                        return pl;
                }
 
-               [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+               [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                public static IEnumerator PolicyHierarchy ()
                {
                        return Hierarchy;
@@ -260,11 +301,12 @@ namespace System.Security {
                        }
 
                        ResolveIdentityPermissions (ps, evidence);
+
                        return ps;
                }
 
 #if NET_2_0
-               [MonoTODO ("more tests are needed")]
+               [MonoTODO ("(2.0) more tests are needed")]
                public static PermissionSet ResolvePolicy (Evidence[] evidences)
                {
                        if ((evidences == null) || (evidences.Length == 0) ||
@@ -304,7 +346,6 @@ namespace System.Security {
 
                static private SecurityPermission _execution = new SecurityPermission (SecurityPermissionFlag.Execution);
 
-               [MonoTODO()]
                public static PermissionSet ResolvePolicy (Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, out PermissionSet denied)
                {
                        PermissionSet resolved = ResolvePolicy (evidence);
@@ -313,17 +354,26 @@ namespace System.Security {
                                throw new PolicyException (Locale.GetText (
                                        "Policy doesn't grant the minimal permissions required to execute the assembly."));
                        }
-                       // do we have the right to execute ?
+
+                       // do we check for execution rights ?
                        if (CheckExecutionRights) {
-                               // unless we have "Full Trust"...
-                               if (!resolved.IsUnrestricted ()) {
-                                       // ... we need to find a SecurityPermission
-                                       IPermission security = resolved.GetPermission (typeof (SecurityPermission));
-                                       if (!_execution.IsSubsetOf (security)) {
-                                               throw new PolicyException (Locale.GetText (
-                                                       "Policy doesn't grant the right to execute to the assembly."));
+                               bool execute = false;
+                               // an empty permissionset doesn't include Execution
+                               if (resolved != null) {
+                                       // unless we have "Full Trust"...
+                                       if (resolved.IsUnrestricted ()) {
+                                               execute = true;
+                                       } else {
+                                               // ... we need to find a SecurityPermission
+                                               IPermission security = resolved.GetPermission (typeof (SecurityPermission));
+                                               execute = _execution.IsSubsetOf (security);
                                        }
                                }
+
+                               if (!execute) {
+                                       throw new PolicyException (Locale.GetText (
+                                               "Policy doesn't grant the right to execute the assembly."));
+                               }
                        }
 
                        denied = denyPset;
@@ -346,7 +396,7 @@ namespace System.Security {
                        return al.GetEnumerator ();
                }
 
-               [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+               [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                public static void SavePolicy () 
                {
                        IEnumerator e = Hierarchy;
@@ -356,7 +406,7 @@ namespace System.Security {
                        }
                }
 
-               [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+               [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
                public static void SavePolicyLevel (PolicyLevel level) 
                {
                        // Yes this will throw a NullReferenceException, just like MS (see FDBK13121)
@@ -367,12 +417,9 @@ namespace System.Security {
 
                private static IEnumerator Hierarchy {
                        get {
-                               // double-lock pattern
-                               if (_hierarchy == null) {
-                                       lock (_lockObject) {
-                                               if (_hierarchy == null)
-                                                       InitializePolicyHierarchy ();
-                                       }
+                               lock (_lockObject) {
+                                       if (_hierarchy == null)
+                                               InitializePolicyHierarchy ();
                                }
                                return _hierarchy.GetEnumerator ();
                        }
@@ -384,17 +431,25 @@ namespace System.Security {
                        // note: use InternalGetFolderPath to avoid recursive policy initialization
                        string userPolicyPath = Path.Combine (Environment.InternalGetFolderPath (Environment.SpecialFolder.ApplicationData), "mono");
 
-                       ArrayList al = new ArrayList ();
-                       al.Add (new PolicyLevel ("Enterprise", PolicyLevelType.Enterprise,
-                               Path.Combine (machinePolicyPath, "enterprisesec.config")));
+                       PolicyLevel enterprise = new PolicyLevel ("Enterprise", PolicyLevelType.Enterprise);
+                       _level = enterprise;
+                       enterprise.LoadFromFile (Path.Combine (machinePolicyPath, "enterprisesec.config"));
+
+                       PolicyLevel machine = new PolicyLevel ("Machine", PolicyLevelType.Machine);
+                       _level = machine;
+                       machine.LoadFromFile (Path.Combine (machinePolicyPath, "security.config"));
 
-                       al.Add (new PolicyLevel ("Machine", PolicyLevelType.Machine,
-                               Path.Combine (machinePolicyPath, "security.config")));
+                       PolicyLevel user = new PolicyLevel ("User", PolicyLevelType.User);
+                       _level = user;
+                       user.LoadFromFile (Path.Combine (userPolicyPath, "security.config"));
 
-                       al.Add (new PolicyLevel ("User", PolicyLevelType.User,
-                               Path.Combine (userPolicyPath, "security.config")));
+                       ArrayList al = new ArrayList ();
+                       al.Add (enterprise);
+                       al.Add (machine);
+                       al.Add (user);
 
                        _hierarchy = ArrayList.Synchronized (al);
+                       _level = null;
                }
 
                internal static bool ResolvePolicyLevel (ref PermissionSet ps, PolicyLevel pl, Evidence evidence)
@@ -418,9 +473,13 @@ namespace System.Security {
                        return false;
                }
 
-               // TODO: this changes in 2.0 as identity permissions can now be unrestricted
                internal static void ResolveIdentityPermissions (PermissionSet ps, Evidence evidence)
                {
+#if NET_2_0
+                       // in 2.0 identity permissions can now be unrestricted
+                       if (ps.IsUnrestricted ())
+                               return;
+#endif
                        // Only host evidence are used for policy resolution
                        IEnumerator ee = evidence.GetHostEnumerator ();
                        while (ee.MoveNext ()) {
@@ -432,21 +491,22 @@ namespace System.Security {
                        }
                }
 
+               internal static PolicyLevel ResolvingPolicyLevel {
+                       get { return _level; }
+                       set { _level = value; }
+               }
+
                internal static PermissionSet Decode (IntPtr permissions, int length)
                {
                        // Permission sets from the runtime (declarative security) can be cached
                        // for performance as they can never change (i.e. they are read-only).
+                       PermissionSet ps = null;
 
-                       if (_declsecCache == null) {
-                               lock (_lockObject) {
-                                       if (_declsecCache == null) {
-                                               _declsecCache = new Hashtable ();
-                                       }
+                       lock (_lockObject) {
+                               if (_declsecCache == null) {
+                                       _declsecCache = new Hashtable ();
                                }
-                       }
 
-                       PermissionSet ps = null;
-                       lock (_lockObject) {
                                object key = (object) (int) permissions;
                                ps = (PermissionSet) _declsecCache [key];
                                if (ps == null) {
@@ -529,12 +589,9 @@ namespace System.Security {
 
                private static IPermission UnmanagedCode {
                        get {
-                               // double-lock pattern
-                               if (_unmanagedCode == null) {
-                                       lock (_lockObject) {
-                                               if (_unmanagedCode == null)
-                                                       _unmanagedCode = new SecurityPermission (SecurityPermissionFlag.UnmanagedCode);
-                                       }
+                               lock (_lockObject) {
+                                       if (_unmanagedCode == null)
+                                               _unmanagedCode = new SecurityPermission (SecurityPermissionFlag.UnmanagedCode);
                                }
                                return _unmanagedCode;
                        }
@@ -615,20 +672,20 @@ namespace System.Security {
                                bool result = true;
                                if (klass->cas.size > 0) {
                                        ps = Decode (klass->cas.blob, klass->cas.size);
-                                       result = SecurityManager.IsGranted (a, ps, false);
+                                       result = (SecurityManager.CheckPermissionSet (a, ps, false) == null);
                                }
                                if (result && (klass->noncas.size > 0)) {
                                        ps = Decode (klass->noncas.blob, klass->noncas.size);
-                                       result = SecurityManager.IsGranted (a, ps, true);
+                                       result = (SecurityManager.CheckPermissionSet (a, ps, true) == null);
                                }
 
                                if (result && (method->cas.size > 0)) {
                                        ps = Decode (method->cas.blob, method->cas.size);
-                                       result = SecurityManager.IsGranted (a, ps, false);
+                                       result = (SecurityManager.CheckPermissionSet (a, ps, false) == null);
                                }
                                if (result && (method->noncas.size > 0)) {
                                        ps = Decode (method->noncas.blob, method->noncas.size);
-                                       result = SecurityManager.IsGranted (a, ps, true);
+                                       result = (SecurityManager.CheckPermissionSet (a, ps, true) == null);
                                }
 #if NET_2_0
                                // success if one of the permission is granted
@@ -637,7 +694,7 @@ namespace System.Security {
                                        if (psc.Count > 0) {
                                                result = false;
                                                foreach (PermissionSet pset in psc) {
-                                                       if (SecurityManager.IsGranted (a, pset, false)) {
+                                                       if (SecurityManager.CheckPermissionSet (a, pset, false) == null) {
                                                                result = true;
                                                                break;
                                                        }
@@ -649,7 +706,7 @@ namespace System.Security {
                                        if (psc.Count > 0) {
                                                result = false;
                                                foreach (PermissionSet pset in psc) {
-                                                       if (SecurityManager.IsGranted (a, pset, false)) {
+                                                       if (SecurityManager.CheckPermissionSet (a, pset, false) == null) {
                                                                result = true;
                                                                break;
                                                        }
@@ -666,24 +723,22 @@ namespace System.Security {
 
                private static bool LinkDemandFullTrust (Assembly a)
                {
-                       // double-lock pattern
-                       if (_fullTrust == null) {
-                               lock (_lockObject) {
-                                       if (_fullTrust == null)
-                                               _fullTrust = new NamedPermissionSet ("FullTrust");
-                               }
-                       }
+                       // FullTrust is immutable (and means Unrestricted) 
+                       // so we can skip the subset operations and jump to IsUnrestricted.
+                       PermissionSet granted = a.GrantedPermissionSet;
+                       if ((granted != null) && !granted.IsUnrestricted ())
+                               return false;
 
-                       try {
-                               return SecurityManager.IsGranted (a, _fullTrust, false);
-                       }
-                       catch (SecurityException) {
+                       PermissionSet denied = a.DeniedPermissionSet;
+                       if ((denied != null) && !denied.IsEmpty ())
                                return false;
-                       }
+
+                       return true;
                }
 
                private static bool LinkDemandUnmanaged (Assembly a)
                {
+                       // note: we know that UnmanagedCode (SecurityPermission) implements IUnrestrictedPermission
                        return IsGranted (a, UnmanagedCode);
                }
 
@@ -709,7 +764,7 @@ namespace System.Security {
                                break;
                        case 2: // MONO_JIT_LINKDEMAND_APTC
                                message = Locale.GetText ("Partially trusted callers aren't allowed to call into this assembly.");
-                               demanded = (object) _fullTrust;
+                               demanded = (object) DefaultPolicies.FullTrust; // immutable
                                break;
                        case 4: // MONO_JIT_LINKDEMAND_ECMA
                                message = Locale.GetText ("Calling internal calls is restricted to ECMA signed assemblies.");
@@ -767,11 +822,11 @@ namespace System.Security {
                                bool result = true;
                                if (actions->cas.size > 0) {
                                        ps = Decode (actions->cas.blob, actions->cas.size);
-                                       result = SecurityManager.IsGranted (a, ps, false);
+                                       result = (SecurityManager.CheckPermissionSet (a, ps, false) == null);
                                }
                                if (actions->noncas.size > 0) {
                                        ps = Decode (actions->noncas.blob, actions->noncas.size);
-                                       result = SecurityManager.IsGranted (a, ps, true);
+                                       result = (SecurityManager.CheckPermissionSet (a, ps, true) == null);
                                }
 #if NET_2_0
                                // success if one of the permission is granted
@@ -780,7 +835,7 @@ namespace System.Security {
                                        if (psc.Count > 0) {
                                                result = false;
                                                foreach (PermissionSet pset in psc) {
-                                                       if (SecurityManager.IsGranted (a, pset, false)) {
+                                                       if (SecurityManager.CheckPermissionSet (a, pset, false) == null) {
                                                                result = true;
                                                                break;
                                                        }