![]() |
General Programming »
Cryptography & Security »
Security
Intermediate
Code Access Security from the perspective of the Developer and AdministratorBy benjaminwoottonLooking at Code Access Security From the Perspective of the Developer and Administrator |
Windows, .NET 1.0, .NET 1.1, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Code Access Security represents a fundamentally different way of controlling access rights to protected resources. Traditionally, permissions such as access to the file system, databases or network were allocated based on characteristics of the user. All processes executed by the user would assume an equivalent set of permissions.
However, models of development such as the growth in code mobility and components have deemed this inadequate. It became apparent that the actions of untrusted or remote components should be restricted more than trusted code, thus turning the permission granting process on its head. Instead of looking at user characteristics, Code Access Security looks at characteristics of the code, including its trusted status when deciding on how to grant permissions. The details of how this takes place and which permissions are granted are configurable by the system administrator.
As a result of this flexibility, Code Access Security is a complex topic. Though there are many articles that describe it in a lot of detail, it�s easy to end up confused by the myriad of terms � permissions, permission sets, evidence, code groups, membership conditions and policy levels are just a few of the concepts to understand. I found it was easier to look at the topic from two perspectives � that of the systems administrator and the developer. Though appreciation of the whole is important, most of the complexity is only relevant depending on which group you fall into.
Note that there is another similar article at CodeProject that goes into more detail on this subject. This one takes more of a tutorial approach and is a great follow up to this...!
The administrator is responsible for defining a security policy to represent choices such as these. At a high level, the administrator needs to ask themselves the following two questions:
1. How should code be grouped?Should code running from the local network be granted different permissions to code running from the Internet or local host? If so, we have to conceptually divide these types of code into separate groups.
Likewise, if some code has been digitally signed, the question must be asked if this should be placed into one �signed code� group, or whether this group should be further subdivided into all of the code signed by Microsoft, and all of the Code signed by Acme corp? Again this choice is likely to be made based on whether we need to assign different permissions to code from the two groups.
2. Which permissions should be granted to code groups?
For every code group, which permissions should be granted to code in that group? Does one code group, such as the Microsoft signed group running from the Internet deserve a greater level of permissions than unsigned code running from the local host?
Every code group has an associated permission set. The administrator decides exactly how code is deemed to have fallen into a specified code group, and which permissions it should be granted as a result of this classification.
The .NET framework adds the concept of code groups. Every code group has a set of membership conditions. These are the conditions that must be satisfied if a piece of code is to become a member of the group. For instance, the default �Microsoft Strong Name� group has the membership condition that an assembly must have an attached strong name signed with the public key of Microsoft. Only presented assemblies that meet this condition will become a member of this group. It is through the examination of the aforementioned evidence that the membership conditions are examined. For this reason, evidence is an important concept for both administrators and developers.
There are 12 code groups defined in a default installation of the .NET framework, with these defaults being based on the concept of security zones found in Internet Explorer. Each provides a slightly different permission set; the Internet code group provides a very restrictive permission set and the aforementioned �Microsoft Strong Name� group will provide a much more flexible permission set.
Note that a given assembly can be part of more than one code group. The final grant will be the union of all of the permissions granted from each group membership.
Policy is stored in XML files, one for each policy level. The location of these files is listed here. It is usual to administer security policy using the command line caspol.exe, or the graphical mscorcfg.cfg.
FileIOPermission can be created and have a set of readable and writable paths added to it. It is then possible to specifically request that the permission is allowed by the runtime, not only for the component in question, but also for all direct and indirect callers:
FileIOPermission permission = new FileIOPermission( PermissionState.Unrestricted ); permission.AddPathList(FileIOPermissionAccess.Write, "C:\\temp\\out.txt"); permission.Demand();
It is also possible to turn the permission off:
permission.Deny();
The second scenario is particularly useful for a developer who may be calling untrusted code. It is worth restricting access to certain resources prior to calling a dangerous component:
permission.Deny(); UntrustedComponent.DoSomethingDangerous(); permission.RevertDeny();
Note that these demands and denials are present in the .NET framework base classes. For example, constructing an instance of the
System.Net.Sockets.Socket class, and calling the Connect()
method produces a demand for the SocketPermission.
[assembly:FileIOPermission( SecurityAction.RequestMinimum, Read = "c:\\temp" )] [assembly:FileDialogPermission( SecurityAction.RequestOptional )]
More importantly, the developer can explicitly deny specific permissions. This allows the runtime to reduce the subset of granted permissions to a component, reducing the scope that someone can misuse the component in a way other than that intended by the original developer. In the code below, the developer is stating that even if hard disk write permission is granted, then accessing the Windows folder should be denied. This prevents an untrusted, malicious component from luring the potentially more trusted component into performing access to the Windows folder on it�s behalf. This blanket denial would prevent a multitude of security holes that might have been presented by the component. All library developers have a responsibility to take this action.
[assembly:FileIOPermission( SecurityAction.Deny, Write = "c:\\windows" )]
[class:DnsPermission( SecurityAction.InheritanceDemand, Unrestricted = true )] class MyDnsClass { � }
The same developer might also like to ensure that anyone linking to their web browser class has full permission to access the web.
[class:WebPermission( SecurityAction.LinkDemand, Unrestricted = true )] class MyWebBrowser { � }
SecurityException is thrown. Catching these exceptions means that the developer can handle the problem elegantly, either by displaying an error message or attempting an alternate course of action.
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Feb 2004 Editor: Nishant Sivakumar |
Copyright 2004 by benjaminwootton Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |