Introduction
Many developers understand the concept of CAS (Code access security) but very few know how to implement the same. This article will discuss and demonstrate practically all those aspects of CAS which you have ready only in theory till today.
or else you can catch me on my daily free training on http://www.questpond.com/
What is CAS?
In order to know the same, we need to probe the assembly/exe/dll and get evidence like who is the publisher of the code, from which site has this code from, from which zone has it come from ( internet, intranet, etc), etc.
What is a permission and permission set?
- Evidences are gathered about the assembly. In other words from where did this assembly come?
- Depending on evidence the assembly is assigned to a code group. In other words what rights does the assembly depending on the evidence gathered?
- Depending on the code group the assembly is allocated security rights.
- Using the security rights the assembly is run within those rights.
Can we see a quick demo of CAS?
If you want to allocate rights to an assembly we need to install the .NET configuration tool and click on the trust assembly menu as shown in the below figure.
What is CASPOL.exe?
It's the core exe that is responsible to assign permission to the assembly. The .NET configuration tool is just a cover that sits on the top of caspol.exe to ease our work. CASPOL.exe commands are cryptic so the .NET configuration tool is more user-friendly. In case you are interested in using caspol.exe you can go to the visual studio command prompt and type caspol.exe with the necessary parameters.
When I open a .NET 4.0 DLL/Assembly using CASPOL it throws an error?
If you try to open a .NET 4.0 DLL in configuration wizard you will get an error as shown below.
- Granting of permission is no more dependent on the .NET CAS, it's now the job of the host. So the host who runs the .NET DLL will define what kind of rights will the assembly have. NET Framework 4.0 comes with CAS a disabled which means all applications started via Windows Explorer or the command prompt run with full trust. Hosted .NET applications, those running inside Internet Explorer or ASP.NET, will run at the trust level granted by their host, being partially trusted.
- Security transparent model is introduced which divides a .NET code into 3 categories Security critical, Security transparent, and security safe critical.
Can you throw some more light on the security transparent model?
A demo of the security transparent model can really make things clear?
Below is a simple video where we have created a simple code marked as security-critical and then we tried calling the same from security transparent code and we get an error.
What is the concept of sandboxing?
The next question answers the same.
- PermissionSet permset = new PermissionSet(PermissionState.None);
- permset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
- permset.AddPermission(new UIPermission(UIPermissionWindow.AllWindows));
- AppDomainSetup objSetup = new AppDomainSetup();
- objSetup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
- AppDomain domain = AppDomain.CreateDomain("New domain name", AppDomain.CurrentDomain.Evidence, objSetup, permset);
- Interface1 i1 = (ClassLibrary1.Class1)domain.CreateInstanceAndUnwrap("ClassLibrary1", "ClassLibrary1.Class1");
- i1.ShowDialog();
But why this change, what was the problem with CAS?
- First thing CAS was not easy, all those cryptic steps of creating code groups, and permission sets, etc eat your energy completely.
- If you have to move the assembly to a different computer you need to do the whole rework again.
- The worst part CAS does not work on unmanaged code. I am dead sure it's always possible you will download exe which is not written in .NET.
So how can we give code access after .NET 4.0 and later?
The best way to restrict code access is by putting restrictions at the operating system level. Windows SRP (Software restriction policy) helps to achieve the same.
- Log on with an Administrator account.
- Type gpedit.msc into the Run or Search box on your Start menu, click OK, and Group Policy will open.
- Go down to Computer Configuration > Windows Settings > Security Settings.
- Right-click on "Software Restriction Policies" and create new policies.
What if I still want to use CAS in .NET 4.0?
- <configuration>
- <runtime>
- <NetFx40_LegacySecurityPolicy enabled="true"/>
- </runtime>
- </configuration>
http://blogs.rev-net.com/ddewinter/2010/03/02/tip-20-opting-out-of-security-changes-in-net-4-in-asp-net-and-custom-appdomains/
References
- MSDN link to CAS http://msdn.microsoft.com/en-us/library/930b76w0%28VS.90%29.aspx
- What's new in .NET 4.0 security http://www.simple-talk.com/dotnet/.net-framework/whats-new-in-code-access-security-in-.net-framework-4.0---part-i/
- CAS in .NET nice simple article http://www.devx.com/vb2themax/article/19886/1954
- Nice Q and A article explaining CAS http://justindeveloper.wordpress.com/2010/02/09/application-security-for-developers/
- 5 reasons why you would use sandboxing http://blogs.msdn.com/b/shawnfa/archive/2006/04/19/579066.aspx
- Legacy CAS policy in .NET 4.0 backward compatibility http://blogs.rev-net.com/ddewinter/2010/03/02/tip-20-opting-out-of-security-changes-in-net-4-in-asp-net-and-custom-appdomains/

Amit PatelPosted Oct 4, 2012, 11:22 AM
Excellent