Skip to main content

Declarative and Imperative Security to Protect Methods



Namespace: System.Security.Permissions

In Declarative security compiler perform the security check prior to running, while in imperative security code itself performs the security checks and performs actions accordingly.

Types of Method Permission Requests

There are six CAS method declarations:
Member name
Description
Assert
The calling code can access the resource identified by the current permission object, even if callers higher in the stack have not been granted permission to access the resource (see Using the Assert Method). *
Demand
All callers higher in the call stack are required to have been granted the permission specified by the current permission object (see Security Demands). *
Deny
The ability to access the resource specified by the current permission object is denied to callers, even if they have been granted permission to access it (see Using the Deny Method). *
InheritanceDemand
The derived class inheriting the class or overriding a method is required to have been granted the specified permission. [ Checks at Load Time]
LinkDemand
The immediate caller is required to have been granted the specified permission.  [ Checks while JIT Compiles ]
PermitOnly
Only the resources specified by this permission object can be accessed, even if the code has been granted permission to access other resources (see Using the PermitOnly Method). *
RequestMinimum
The request for the minimum permissions required for code to run. This action can only be used within the scope of the assembly. ***
RequestOptional
The request for additional permissions that are optional (not required to run). This action can only be used within the scope of the assembly. ***
RequestRefuse
The request that permissions that might be misused will not be granted to the calling code. This action can only be used within the scope of the assembly. ***
* Checks at runtime
*** Checks at grant time

Guidelines for using Method Permissions:
1.     Use SecurityAction.PermitOnly to reduce the permission…
2.     Use SecurityAction.Deny to further refine granted permissions…
3.     User CodeAccessPermission.PermitOnly to imperatively reduce permission.
It’s important when calling third party Controls. Use CodeAccessPermission.RevertPermitOnly to restore the permission.
4.     Use the CodeAccessPermission.Assert to allow partially trusted code to call a method that requires the permission that caller might lack. Do use CodeAccessPermission.RevertAssert to restore the original permissions.
5.     Use CodeAccessPermissions.Demand when your assembly implements custom functionality in unmanaged code.

Techniques for demanding Permissions:
1.       Demand Checks all the callers in the stack to have the permission
2.       LinkDemand checks the intermediate only for the permission

How to Imperatively Demand CAS Permissions:
Use the same classes like for FileIOPermissionAttribute use FileIOPermission class. Against each Security action these classes implement a method to gain/restrict the permission e.g. FileIOPermission.Demand() is alternate to SecturityAction.Demand.

Using Imperative security checks one can improve the application grace by providing nice messages and feedback by catching security exceptions.

Analyzing granted permissions:

To check whether or not the permission is assigned to the assembly use Security.SecurityManager.IsGranted instead of Demand.

If(SecurityManager.IsGranted(new FileIOPermission(FileIOPermissionAccess.Read,
@”C:\windows”))
                //Can Read
else
                //Can’t Read


How to declaratively limit method Permission:

[FileIOPermission(SecurityAction.Deny, ViewAndMoify = @“C:\windows\”)]

How to Imperatively Limit Method Permissions:

FileIOPermission vFilePermission = new FileIOPermission( FileIOPermissionAccess.AllAccess, @“C:\Windows\”);

vFilePermission.Deny();

While catching and logging exceptions, do gain the EventLogPermission and the after logging the event revert back to previous state.

How to improve performance while relaxing the permissions:

Try to use LinkDemand instead of Demand
Try to use carefully the Assert to bypass security checks….
To successfully use Assert, assembly must have SecurityPermissionFalg.Assertion privilege must be granted.

Assert vouch the security and acquire the required permission if it can be granted to the assembly and method.

You can only use Assert ones in a method, to assert multiple permissions use PermissionSet.
Assert doesn’t override OS Role Base Security.

How to call trusted code from partially trusted code:

By default partially trusted code cannot call Strong name assemblies.

To allow partially trusted code to call your assembly method use the following attribute:

[assembly: AllowPartiallyTrustedCallers]

How to Use Permission Set:
 


Comments

Popular posts from this blog

Culture Information and Localization in .NET

Namespace: System.Globalization CultureInfo Class:                 It provides information like the Format of numbers and dates, Culture’s Calendar, Culture’s language and sublanguage (if applicable), Country and region of the culture. The Basic use of CultureInfo class is shown here: • How string Comparisons are performed • How Number Comparison & Formats are performed • Date Comparison and Formats. • How resources are retrieved and used. Cultures are grouped into three categories: Invariant Culture : It’s Culture Insensitive. It can be used to build some trial application. It can be also used to build an application with hard-coded expiry date that ignores cultures. But using it for every comparison will be incorrect and inappropriate. Neutral Culture : English(en), Frensh(fr), and Spanish(sp). A neutral culture is related to language but it’s not related to specific regi...

Using ADO.NET Transaction Object, Saving CLR Objects and SqlNotificationRequest

Serializable allows one transaction to complete before the other start. SqlConnectoin support named savpoint to roll back to; that’s an equaliant to save transaction command in MS SQL Server. Using TransactionScope Object : A transaction can’t span multiple connections.   Local and distributed transaction ares supported in 1.x you have to Enterpeise seveices library to regiser multiple connections and then call EnlistDistributedTransaction. Distributed Transaction Coordinator is required for Distributed transaction that’s available on windows 2000 & +. In 2.X and later use TransactionScope object. Serializable allows one transaction to complete before the other start. SqlConnectoin support named savpoint to roll back to; that’s an equaliant to save transaction command in MS SQL Server. Using TransactionScope Object : Dispoase of the transaction must be called to complete the transaction. Distributed Transaction : ...

Code Access Security in .Net

Namespace: System.Security What is Code Access Security? Code Access Security is a mechanism through which Developers and administrators can restrict code from accessing different resources , without caring about the users’ access level. You can also control resource that can’t be controlled through traditional RBS (Role Based Security), e.g. Web Requests and DNS requests etc. It can be only applied to Managed Applications. These restriction are applied not to the user instead to the Application, thus it does not require username or password. Evidence: It is the information that runtime gather about the assembly to determine which Code Groups the assembly belongs to. The following table shows the common types of evidence that a host can present to the runtime. Evidence Description Application directory The application's installation directory. Hash Cryptographic hash such as SHA1. Publisher Soft...