public RuntimeDeclSecurityEntry choice;
}
+#if NET_2_0
+ [ComVisible (true)]
+ public static class SecurityManager {
+#else
public sealed class SecurityManager {
+ private SecurityManager ()
+ {
+ }
+#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 ()
{
_lockObject = new object ();
}
- private SecurityManager ()
- {
- }
-
// properties
extern public static bool CheckExecutionRights {
get;
[MethodImplAttribute (MethodImplOptions.InternalCall)]
- [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+ [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
set;
}
get;
[MethodImplAttribute (MethodImplOptions.InternalCall)]
- [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+ [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
set;
}
// methods
-#if NET_2_0
- [MonoTODO]
+#if NET_1_1
+ // 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 ("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)
{
// - 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 false;
- }
- } 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 {
}
catch (SecurityException) {
// ... so we catch
- return false;
+ return p;
}
}
}
- return true;
+ return null;
}
- [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+ internal static IPermission CheckPermissionSet (AppDomain ad, PermissionSet ps)
+ {
+ if ((ps == null) || ps.IsEmpty ())
+ return null;
+
+ PermissionSet granted = ad.GrantedPermissionSet;
+ if (granted == null)
+ 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)) {
+ if (!p.IsSubsetOf (null))
+ return p;
+ }
+ } else if (!p.IsSubsetOf (grant)) {
+ return p;
+ }
+ } else {
+ // but non-CAS will throw on failure...
+ try {
+ p.Demand ();
+ }
+ catch (SecurityException) {
+ // ... so we catch
+ return p;
+ }
+ }
+ }
+ return null;
+ }
+
+ [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
public static PolicyLevel LoadPolicyLevelFromFile (string path, PolicyLevelType type)
{
if (path == null)
return pl;
}
- [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+ [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
public static PolicyLevel LoadPolicyLevelFromString (string str, PolicyLevelType type)
{
if (null == str)
return pl;
}
- [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+ [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
public static IEnumerator PolicyHierarchy ()
{
return Hierarchy;
}
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) ||
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);
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;
return al.GetEnumerator ();
}
- [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
+ [SecurityPermission (SecurityAction.Demand, ControlPolicy = true)]
public static void SavePolicy ()
{
IEnumerator e = Hierarchy;
}
}
- [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)
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 ();
}
// 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)
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 ()) {
}
}
+ 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) {
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;
}
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
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;
}
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;
}
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);
}
IPermission failed = null;
if (a != null) {
- an = a.GetName ();
+ an = a.UnprotectedGetName ();
granted = a.GrantedPermissionSet;
refused = a.DeniedPermissionSet;
}
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.");
PermissionSet refused = null;
if (a != null) {
- an = a.GetName ();
+ an = a.UnprotectedGetName ();
granted = a.GrantedPermissionSet;
refused = a.DeniedPermissionSet;
}
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
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;
}