dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Security ClrPermissio


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Security ClrPermissions.cs,NONE,1.1 ClrSecurity.cs,1.1,1.2 CodeAccessPermission.cs,1.3,1.4 PermissionSet.cs,1.3,1.4
Date: Mon, 31 Mar 2003 00:47:38 -0500

Update of /cvsroot/dotgnu-pnet/pnetlib/runtime/System/Security
In directory subversions:/tmp/cvs-serv30378/runtime/System/Security

Modified Files:
        ClrSecurity.cs CodeAccessPermission.cs PermissionSet.cs 
Added Files:
        ClrPermissions.cs 
Log Message:


Redesign the internalcall interface for code access security (CAS)
so that most of the work happens in the C# code, not the runtime engine.


--- NEW FILE ---
/*
 * ClrPermissions.cs - Implementation of the
 *              "System.Security.ClrPermissions" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Security
{

using System;
using System.Runtime.CompilerServices;
using System.Security.Permissions;

// The "ClrPermission" class represents a group of permission set
// values for a particular stack frame with the thread's call stack.

internal sealed class ClrPermissions
{

        // Accessible internal state.
        public PermissionSet granted;
        public PermissionSet denied;
        public PermissionSet permitOnly;

        // Constructor.
        public ClrPermissions(PermissionSet granted, PermissionSet denied,
                                                  PermissionSet permitOnly)
                        {
                                this.granted = granted;
                                this.denied = denied;
                                this.permitOnly = permitOnly;
                        }

        // Set the granted permissions and return a new object.
        public ClrPermissions SetGranted(PermissionSet set)
                        {
                                return new ClrPermissions(set, denied, 
permitOnly);
                        }

        // Set the denied permissions and return a new object.
        public ClrPermissions SetDenied(PermissionSet set)
                        {
                                return new ClrPermissions(granted, set, 
permitOnly);
                        }

        // Set the permit-only permissions and return a new object.
        public ClrPermissions SetPermitOnly(PermissionSet set)
                        {
                                return new ClrPermissions(granted, denied, set);
                        }

}; // class ClrPermissions

}; // namespace System.Security

Index: ClrSecurity.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/runtime/System/Security/ClrSecurity.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** ClrSecurity.cs      18 Dec 2001 10:27:04 -0000      1.1
--- ClrSecurity.cs      31 Mar 2003 05:47:36 -0000      1.2
***************
*** 30,59 ****
  {
  
!       // In all of the methods below, a "skipFrames" value of zero
!       // indicates the method that called these interfaces.
! 
!       // Assert a code access permission for a particular stack frame.
!       // Returns false if the permission was not granted.
!       [MethodImpl(MethodImplOptions.InternalCall)]
!       extern public static bool Assert(CodeAccessPermission perm, int 
skipFrames);
! 
!       // Demand a code access permission for a particular stack frame.
!       // Returns false if the permission was not granted.
!       [MethodImpl(MethodImplOptions.InternalCall)]
!       extern public static bool Demand(CodeAccessPermission perm, int 
skipFrames);
! 
!       // Deny a code access permission for a particular stack frame.
        [MethodImpl(MethodImplOptions.InternalCall)]
!       extern public static void Deny(CodeAccessPermission perm, int 
skipFrames);
  
!       // Set a "PermitOnly" block at a particular stack frame so that
!       // nothing in higher frames can be accessed.
        [MethodImpl(MethodImplOptions.InternalCall)]
!       extern public static void SetPermitOnlyBlock(int skipFrames);
  
!       // Permit a particular stack frame to access the specified permission.
        [MethodImpl(MethodImplOptions.InternalCall)]
!       extern public static void PermitOnly
!               (CodeAccessPermission perm, int skipFrames);
  
  }; // class ClrSecurity
--- 30,49 ----
  {
  
!       // Get the permission sets in the current call context, starting
!       // at a particular stack frame.  This will scan up the stack until
!       // it finds a permissions object.  If none are found, this will
!       // return "null".
        [MethodImpl(MethodImplOptions.InternalCall)]
!       extern public static ClrPermissions GetPermissionsFrom(int skipFrames);
  
!       // Get the permission set for a particular stack frame.
!       // Returns null if no permission set for that stack frame.
        [MethodImpl(MethodImplOptions.InternalCall)]
!       extern public static ClrPermissions GetPermissions(int skipFrames);
  
!       // Set the permission sets for a particular stack frame.
        [MethodImpl(MethodImplOptions.InternalCall)]
!       extern public static void SetPermissions
!                       (ClrPermissions perm, int skipFrames);
  
  }; // class ClrSecurity

Index: CodeAccessPermission.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Security/CodeAccessPermission.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** CodeAccessPermission.cs     30 Mar 2003 11:58:44 -0000      1.3
--- CodeAccessPermission.cs     31 Mar 2003 05:47:36 -0000      1.4
***************
*** 24,27 ****
--- 24,28 ----
  
  using System;
+ using System.Security.Permissions;
  using System.Runtime.CompilerServices;
  
***************
*** 33,49 ****
  
        // Assert permissions for the caller.
!       public void Assert()
                        {
!                               if(!ClrSecurity.Assert(this, 1))
                                {
!                                       throw new SecurityException
!                                               
(_("Exception_SecurityNotGranted"));
                                }
                        }
  
        // Deny permissions to the caller.
        public void Deny()
                        {
!                               ClrSecurity.Deny(this, 1);
                        }
  
--- 34,88 ----
  
        // Assert permissions for the caller.
!       internal void Assert(int skipFrames)
                        {
!                               // Add the permission to the granted 
permissions set.  If there
!                               // are no permissions at all, then assume we 
are unrestricted.
!                               ClrPermissions current;
!                               current = 
ClrSecurity.GetPermissionsFrom(skipFrames);
!                               if(current != null)
                                {
!                                       PermissionSet set = new 
PermissionSet(PermissionState.None);
!                                       set.AddPermission(this.Copy());
!                                       set = set.Union(current.granted);
!                                       ClrSecurity.SetPermissions
!                                               (current.SetGranted(set), 
skipFrames);
                                }
                        }
+       public void Assert()
+                       {
+                               // We must have the "Assertion" security flag 
for this to work.
+                               SecurityPermission perm;
+                               perm = new 
SecurityPermission(SecurityPermissionFlag.Assertion);
+                               perm.Demand();
+ 
+                               // Assert this permission.
+                               Assert(2);
+                       }
  
        // Deny permissions to the caller.
+       internal void Deny(int skipFrames)
+                       {
+                               // Add the permission to the denied permissions 
set.
+                               ClrPermissions current;
+                               current = 
ClrSecurity.GetPermissionsFrom(skipFrames);
+                               PermissionSet set = new 
PermissionSet(PermissionState.None);
+                               set.AddPermission(this.Copy());
+                               if(current == null)
+                               {
+                                       // Initialize the permissions context 
to "allow
+                                       // everything except this permission 
object".
+                                       current = new ClrPermissions
+                                               (new 
PermissionSet(PermissionState.Unrestricted),
+                                                set, null);
+                               }
+                               else
+                               {
+                                       current = 
current.SetDenied(set.Union(current.denied));
+                               }
+                               ClrSecurity.SetPermissions(current, skipFrames);
+                       }
        public void Deny()
                        {
!                               Deny(2);
                        }
  
***************
*** 64,68 ****
        public void Demand()
                        {
!                               if(!ClrSecurity.Demand(this, 1))
                                {
                                        throw new SecurityException
--- 103,132 ----
        public void Demand()
                        {
!                               // Get the current permission state.
!                               ClrPermissions current = 
ClrSecurity.GetPermissionsFrom(1);
!                               if(current == null)
!                               {
!                                       // Null is equivalent to "unrestricted".
!                                       return;
!                               }
! 
!                               // Build a permission set with just this 
permission.
!                               PermissionSet set = new 
PermissionSet(PermissionState.None);
!                               set.AddPermission(this);
! 
!                               // If "PermitOnly" is set, then only check that 
set.
!                               if(current.permitOnly != null)
!                               {
!                                       if(!set.IsSubsetOf(current.permitOnly))
!                                       {
!                                               throw new SecurityException
!                                                       
(_("Exception_SecurityNotGranted"));
!                                       }
!                                       return;
!                               }
! 
!                               // The permission must be granted, but not 
denied.
!                               if(!set.IsSubsetOf(current.granted) ||
!                                  set.IsSubsetOf(current.denied))
                                {
                                        throw new SecurityException
***************
*** 88,109 ****
  
        // Revert all permissions for the caller.
!       [MethodImpl(MethodImplOptions.InternalCall)]
!       extern public static void RevertAll();
  
        // Revert all assertions for the caller.
!       [MethodImpl(MethodImplOptions.InternalCall)]
!       extern public static void RevertAssert();
  
        // Revert all denials for the caller.
!       [MethodImpl(MethodImplOptions.InternalCall)]
!       extern public static void RevertDeny();
  
        // Revert all "PermitOnly" permissions for the caller.
!       [MethodImpl(MethodImplOptions.InternalCall)]
!       extern public static void RevertPermitOnly();
  
  #endif // !ECMA_COMPAT
  
        // Set the caller's permissions to only this object.
  #if ECMA_COMPAT
        void IStackWalk.PermitOnly()
--- 152,257 ----
  
        // Revert all permissions for the caller.
!       public static void RevertAll()
!                       {
!                               ClrPermissions current = 
ClrSecurity.GetPermissions(1);
!                               ClrPermissions parent = 
ClrSecurity.GetPermissionsFrom(2);
!                               if(current != null)
!                               {
!                                       ClrSecurity.SetPermissions(parent, 1);
!                               }
!                       }
  
        // Revert all assertions for the caller.
!       public static void RevertAssert()
!                       {
!                               ClrPermissions current = 
ClrSecurity.GetPermissions(1);
!                               ClrPermissions parent = 
ClrSecurity.GetPermissionsFrom(2);
!                               if(current != null)
!                               {
!                                       if(parent != null)
!                                       {
!                                               ClrSecurity.SetPermissions
!                                                       
(current.SetGranted(parent.granted), 1);
!                                       }
!                                       else
!                                       {
!                                               ClrSecurity.SetPermissions
!                                                       (current.SetGranted
!                                                               (new 
PermissionSet
!                                                                       
(PermissionState.Unrestricted)), 1);
!                                       }
!                               }
!                       }
  
        // Revert all denials for the caller.
!       public static void RevertDeny()
!                       {
!                               ClrPermissions current = 
ClrSecurity.GetPermissions(1);
!                               ClrPermissions parent = 
ClrSecurity.GetPermissionsFrom(2);
!                               if(current != null)
!                               {
!                                       if(parent != null)
!                                       {
!                                               ClrSecurity.SetPermissions
!                                                       
(current.SetDenied(parent.denied), 1);
!                                       }
!                                       else
!                                       {
!                                               ClrSecurity.SetPermissions
!                                                       (current.SetDenied
!                                                               (new 
PermissionSet(PermissionState.None)), 1);
!                                       }
!                               }
!                       }
  
        // Revert all "PermitOnly" permissions for the caller.
!       public static void RevertPermitOnly()
!                       {
!                               ClrPermissions current = 
ClrSecurity.GetPermissions(1);
!                               ClrPermissions parent = 
ClrSecurity.GetPermissionsFrom(2);
!                               if(current != null)
!                               {
!                                       if(parent != null)
!                                       {
!                                               ClrSecurity.SetPermissions
!                                                       
(current.SetPermitOnly(parent.permitOnly), 1);
!                                       }
!                                       else
!                                       {
!                                               ClrSecurity.SetPermissions
!                                                       
(current.SetPermitOnly(null), 1);
!                                       }
!                               }
!                       }
  
  #endif // !ECMA_COMPAT
  
        // Set the caller's permissions to only this object.
+       internal static void PermitOnly(PermissionSet set, int skipFrames)
+                       {
+                               // Make sure that we don't already have a 
"PermitOnly" value.
+                               ClrPermissions current;
+                               current = 
ClrSecurity.GetPermissionsFrom(skipFrames);
+                               if(current != null && current.permitOnly != 
null)
+                               {
+                                       throw new 
SecurityException(_("Exception_PermitOnly"));
+                               }
+ 
+                               // Add the "PermitOnly" set to the call stack.
+                               if(current == null)
+                               {
+                                       // Initialize the permissions context 
to "allow
+                                       // only this permission object".
+                                       current = new ClrPermissions
+                                               (new 
PermissionSet(PermissionState.Unrestricted),
+                                                new 
PermissionSet(PermissionState.None),
+                                                set);
+                               }
+                               else
+                               {
+                                       current = current.SetPermitOnly(set);
+                               }
+                               ClrSecurity.SetPermissions(current, skipFrames);
+                       }
  #if ECMA_COMPAT
        void IStackWalk.PermitOnly()
***************
*** 112,117 ****
  #endif
                        {
!                               ClrSecurity.SetPermitOnlyBlock(1);
!                               ClrSecurity.PermitOnly(this, 1);
                        }
  
--- 260,271 ----
  #endif
                        {
!                               // Demand the permission first, because we 
cannot permit it
!                               // for exclusive access if we are not allowed 
have it at all.
!                               Demand();
! 
!                               // Create a permission set for this object and 
then add it.
!                               PermissionSet set = new 
PermissionSet(PermissionState.None);
!                               set.AddPermission(this.Copy());
!                               PermitOnly(set, 2);
                        }
  

Index: PermissionSet.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Security/PermissionSet.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** PermissionSet.cs    30 Mar 2003 11:58:44 -0000      1.3
--- PermissionSet.cs    31 Mar 2003 05:47:36 -0000      1.4
***************
*** 102,117 ****
        public virtual void Assert()
                        {
                                int posn;
!                               CodeAccessPermission perm;
                                for(posn = 0; posn < permissions.Count; ++posn)
                                {
!                                       perm = (permissions[posn] as 
CodeAccessPermission);
!                                       if(perm != null)
                                        {
!                                               if(!ClrSecurity.Assert(perm, 1))
!                                               {
!                                                       throw new 
SecurityException
!                                                               
(_("Exception_SecurityNotGranted"));
!                                               }
                                        }
                                }
--- 102,119 ----
        public virtual void Assert()
                        {
+                               // We must have the "Assertion" security flag 
for this to work.
+                               SecurityPermission perm;
+                               perm = new 
SecurityPermission(SecurityPermissionFlag.Assertion);
+                               perm.Demand();
+ 
+                               // Assert all of the CodeAccessPermission 
objects in the set.
                                int posn;
!                               CodeAccessPermission caperm;
                                for(posn = 0; posn < permissions.Count; ++posn)
                                {
!                                       caperm = (permissions[posn] as 
CodeAccessPermission);
!                                       if(caperm != null)
                                        {
!                                               caperm.Assert(2);
                                        }
                                }
***************
*** 133,146 ****
                                {
                                        perm = 
((IPermission)(permissions[posn]));
!                                       caperm = (perm as CodeAccessPermission);
!                                       if(caperm != null)
!                                       {
!                                               if(!ClrSecurity.Demand(caperm, 
1))
!                                               {
!                                                       throw new 
SecurityException
!                                                               
(_("Exception_SecurityNotGranted"));
!                                               }
!                                       }
!                                       else
                                        {
                                                perm.Demand();
--- 135,139 ----
                                {
                                        perm = 
((IPermission)(permissions[posn]));
!                                       if(perm != null)
                                        {
                                                perm.Demand();
***************
*** 159,163 ****
                                        if(perm != null)
                                        {
!                                               ClrSecurity.Deny(perm, 1);
                                        }
                                }
--- 152,156 ----
                                        if(perm != null)
                                        {
!                                               perm.Deny(2);
                                        }
                                }
***************
*** 272,278 ****
        public virtual void PermitOnly()
                        {
                                int posn;
                                CodeAccessPermission perm;
-                               ClrSecurity.SetPermitOnlyBlock(1);
                                for(posn = 0; posn < permissions.Count; ++posn)
                                {
--- 265,276 ----
        public virtual void PermitOnly()
                        {
+                               // Demand the permission first, because we 
cannot permit it
+                               // for exclusive access if we are not allowed 
have it at all.
+                               Demand();
+ 
+                               // Create a permission set and copy all CA 
objects into it.
+                               PermissionSet set = new 
PermissionSet(PermissionState.None);
                                int posn;
                                CodeAccessPermission perm;
                                for(posn = 0; posn < permissions.Count; ++posn)
                                {
***************
*** 280,286 ****
                                        if(perm != null)
                                        {
!                                               ClrSecurity.PermitOnly(perm, 1);
                                        }
                                }
                        }
  
--- 278,287 ----
                                        if(perm != null)
                                        {
!                                               set.AddPermission(perm.Copy());
                                        }
                                }
+ 
+                               // Set the current "PermitOnly" context on the 
call stack.
+                               CodeAccessPermission.PermitOnly(set, 2);
                        }
  





reply via email to

[Prev in Thread] Current Thread [Next in Thread]