Code Access Security
Code Access Security, in the Microsoft.NET framework, is Microsoft's solution to prevent untrusted code from performing privileged actions. When the CLR loads an assembly it will obtain evidence for the assembly and use this to identify the code group that the assembly belongs to. A [|code group] contains a permission set. Code that performs a privileged action will perform a code access demand which will cause the CLR to walk up the call stack and examine the permission set granted to the assembly of each method in the call stack.
The code groups and permission sets are determined by the administrator of the machine who defines the security policy.
Microsoft considers CAS as obsolete and discourages its use. It is also not available in.NET Core and.NET.
Evidence
Evidence can be any information associated with an assembly. The default evidences that are used by.NET code access security are:- Application directory: the directory in which an assembly resides.
- Publisher: the assembly's publisher's digital signature.
- URL: the complete URL where the assembly was launched from
- Site: the hostname of the URL/Remote Domain/VPN.
- Zone: the security zone where the assembly resides
- Hash: a cryptographic hash of the assembly, which identifies a specific version.
- Strong Name: a combination of the assembly name, version and public key of the signing key used to sign the assembly. The signing key is not an X.509 certificate, but a custom key pair generated by the strong naming tool, SN.EXE or by Visual Studio.
Evidence based on a hash of the assembly is easily obtained in code. For example, in C#, evidence may be obtained by the following code clause:
this.GetType.Assembly.Evidence
Policy
A policy is a set of expressions that uses evidence to determine a code group membership. A code group gives a permission set for the assemblies within that group. There are four policies in.NET:- Enterprise: policy for a family of machines that are part of an Active Directory installation.
- Machine: policy for the current machine.
- User: policy for the logged on user.
- AppDomain: policy for the executing application domain.
Code access security will present an assembly's evidence to each policy and will then take the intersection as the permissions granted to the assembly.
By default, the Enterprise, User, and AppDomain policies give full trust and the Machine policy is more restrictive. Since the intersection is taken, this means that the final permission set is determined by the Machine policy.
Note that the policy system has been eliminated in.NET Framework 4.0.