Permissions in .NET's Code Access Security (CAS) refer to the rights or privileges granted to an assembly or piece of code to access system resources, such as file systems, the network, or the registry. These permissions are based on the identity of the code, which is determined by evidence. The goal of CAS is to protect a system by ensuring that code runs with the minimum necessary privileges, thus reducing the risk of malicious code or bugs that could cause harm.
Permission Sets
Permissions and Code Groups
There are several types of permissions in CAS, which correspond to different resources or actions that code can access. Some common permission types include:
FileIOPermission filePermission = new FileIOPermission(FileIOPermissionAccess.Read, @"C:\Documents");
filePermission.Demand();
SecurityPermission securityPermission = new SecurityPermission(SecurityPermissionFlag.ControlEvidence);
securityPermission.Demand();
ReflectionPermission reflectionPermission = new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess);
reflectionPermission.Demand();
UIPermission uiPermission = new UIPermission(UIPermissionWindow.AllWindows);
uiPermission.Demand();
WebPermission webPermission = new WebPermission(NetworkAccess.Connect, "http://example.com");
webPermission.Demand();
EnvironmentPermission environmentPermission = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "TEMP");
environmentPermission.Demand();
FileIOPermission (for file access) and UIPermission (for user interface access).PermissionSet permissionSet = new PermissionSet(PermissionState.None);
permissionSet.Add(new FileIOPermission(FileIOPermissionAccess.Read, @"C:\Documents"));
permissionSet.Add(new UIPermission(UIPermissionWindow.AllWindows));
PrincipalPermission principalPermission = new PrincipalPermission("Admin", null);
principalPermission.Demand();
SqlClientPermission sqlPermission = new SqlClientPermission(PermissionState.Unrestricted);
sqlPermission.Demand();
Permissions are typically used with the Demand() method in the .NET framework. When code attempts to execute a security-sensitive operation, it demands the permissions it requires. If the calling code does not have the necessary permissions, a SecurityException is thrown.
SecurityException.Permissions in CAS are typically assigned through code groups. A code group categorizes code based on its evidence (e.g., strong name, URL, publisher), and each code group is linked to a permission set that specifies what resources or actions that code can access.
For example:
Consider the following example of a code group configuration in the security policy, where different permissions are applied based on the code's evidence:
<codeGroup class="UnionCodeGroup" version="1" PermissionSetName="FullTrust">
<membershipConditions>
<strongNameMembershipCondition publicKey="publicKeyBytes" />
</membershipConditions>
</codeGroup>
<codeGroup class="UnionCodeGroup" version="1" PermissionSetName="ExecutionOnly">
<membershipConditions>
<urlMembershipCondition url="http://*" />
</membershipConditions>
</codeGroup>
.NET allows fine-grained control over what resources an assembly can access. Permissions can be assigned at various levels of granularity:
FileIOPermission, UIPermission, SecurityPermission, etc., allow specific control over particular resources.The security policy defines which permissions are granted to code based on the code's evidence. The .NET runtime evaluates the security policy and assigns the appropriate permission set to each assembly based on its evidence. Security policy files, such as machine.config or app.config, map evidence to permission sets and code groups.
Permissions in Code Access Security (CAS) are critical for managing and enforcing security within the .NET Framework. Permissions determine what actions an assembly can perform, ensuring that it can only access resources it is allowed to use. By defining permission sets and associating them with code groups based on evidence, developers can control which code is trusted and what resources it can access, thus maintaining a secure environment.
Open this section to load past papers