How to Maintain a CodeGroup in Security Policy at Runtime?

Whenever protected resources are accessed by an assembly, it's permissions are determined by the code access security system of CLR. Each permission set granted to an assembly is based on the assembly's evidence (such as its URL or publisher certificate, strong name), which in turn is based on configurable security policy.

Code groups are the building blocks of security policies. A Code Group is made of an association between an evidence value and a permission set.

  • A hierarchical structure of Code Groups defines a security policy. The .NET framework comes with three different security policies: Enterprise, Machine, User. Additionally an host can define application domain-level policy by calling the AppDomain.SetAppDomainPolicy method on the System.AppDomain class. The first three policies are typically set by administrator while the latter is eventually defined by developers.

CodeGroup.gif

There are number of built-in permission sets as shown below.

  • FullTrust
  • Everything
  • Internet
  • LocalIntranet
  • Execution
  • SkipVerification
  • Nothing

Let's see how to create/delete a code group at runtime

Register CodeGroup

We need to decide on the following while creating a code-group

  1. At what level we need to set the code group?
  2. What evidence value is to be set?
  3. What permission set we need to provide for this code-group?

To access a security level

We can use SecurityManager.PolicyHierarchy()

IEnumerator secLevels = SecurityManager.PolicyHierarchy();
PolicyLevel policyMachineLevel =
null;
CodeGroup machineCodeGroupRoot =
null
;
while
(secLevels.MoveNext())
{
PolicyLevel level = secLevels.Current
as
PolicyLevel;
if(level != null && level.Label == "Machine")
//used to check whether the level is Machine Level
{
policyMachineLevel = level;
machineCodeGroupRoot = level.RootCodeGroup;
break
;
}
}
return policyMachineLevel;

To provide evidence value

We can use Assembly.GetExecutingAssembly() to get the assembly object and then we can use assembly.Evidence to get the evidence information.

Assembly myAssembly = Assembly.GetExecutingAssembly();
Evidence evidence = myAssembly.Evidence;
IEnumerator enuEvd = evidence.GetEnumerator();
StrongNamePublicKeyBlob pubKey =
null;
while(enuEvd.MoveNext())
// Get public key so as to use it as evidence
{
Object obj = enuEvd.Current;
//It can be either of zone,url,strongname,hash
StrongName sn = obj as
StrongName;
if(sn != null
)
{
pubKey = sn.PublicKey;
break
;
}
}
return pubKey;

Register a code-group with PublicKey as Evidence and FullTrust as PermissionSet

StrongNamePublicKeyBlob cdeGroupKey = {Get the public of executing assembly using the above logic}
UnionCodeGroup myCodeGroup =
new UnionCodeGroup(new StrongNameMembershipCondition(cdeGroupKey,null,null), new PolicyStatement(new NamedPermissionSet("FullTrust")));
//create a codegroup with public key as evidence
myCodeGroup.Description = "Code group grants full trust to all code originating from the Arsenal team";
myCodeGroup.Name = "MyGroup";
machineCodeGroupRoot.AddChild(myCodeGroup);
//add this group to the security level you have chosen
SecurityManager.SavePolicyLevel(policyMachineLevel); //at last save the policy

CodeGroup1.gif

To check whether a CodeGroup is present

We can navigate through the machine level code group object to find whether the codegroup is already registered

foreach(CodeGroup codeGroup in machineCodeGroupRoot.Children)
{
if
(codeGroup.Name == "MyGroup")
{
//already added
return
;
}
}

To delete a codegroup

We just need to call in the above code before returning

machineCodeGroupRoot.RemoveChild(codeGroup);


Similar Articles