ArticleslgStudy

computer science

Code Access Security

Code Access Security is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Code Access Security rather than just read about it. In short: Code Access Security (CAS), 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.

Key takeaways

  • Code Access Security belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Code Access Security to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Code Access Security from memory before moving on to harder problems.

Reference excerpt

Code Access Security (CAS), 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 (one or more permissions). 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 evidence that is used by .NET code access security are:

Application directory: the directory in which an assembly resides. Publisher: the assembly's publisher's digital signature (requires the assembly to be signed via Authenticode). 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. A developer can use custom evidence (so-called assembly evidence) but this requires writing a security assembly and in version 1.1 of .NET this facility does not work. 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:

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. The first three policies are stored in XML files and are administered through the .NET Configuration Tool 1.1 (mscorcfg.msc). The final policy is administered through code for the current application domain. Code access security will present an assembly's evidence to each policy and will then take the intersection (that is the permissions common to all the generated permission sets) as the permissions granted to the assembly. By default, the Enterprise, User, and AppDomain policies give full trust (that is they allow all assemblies to have all permissions) 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.

Code group Code groups associate a piece of evidence with a named permission set. The administrator uses the .NET Configuration Tool to specify a particular type of evidence (for example, Site) and a particular value for that evidence (for example, www.mysite.com) and then identifies the permission set that the code group will be granted.

Demands Code that performs some privileged action will make a demand for one or more permissions. The demand makes the CLR walk the call stack and for each method the CLR will ensure that the demanded permissions are in the method's assembly's granted permissions. If the permission is not granted then a security exception is thrown. This prevents downloaded code from performing privileged actions. For example, if an assembly is downloaded from an untrusted site the assembly will not have any file IO permissions and so if this assembly attempts to access a file, will throw an exception preventing the call.

References

Understanding .NET Security Code Access Security Policy Tool (Caspol.exe)

Worked examples

Example 1 — a first encounter with Code Access Security

Start with the simplest possible case. Write down what Code Access Security claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Code Access Security before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Code Access Security ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Code Access Security

In research
Code Access Security appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Code Access Security in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Code Access Security is common in secondary-school and first-year university syllabi. It links to neighbouring topics Common Language Infrastructure, Cryptographic protocols, Microsoft Windows security technology, so understanding it makes those chapters shorter.
In everyday life
Look for Code Access Security outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Code Access Security” →

How to study Code Access Security in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Code Access Security means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Code Access Security out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Code Access Security in simple terms?

Code Access Security (CAS), 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.

Why does Code Access Security matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Code Access Security?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Code Access Security.

Tags

  • Common Language Infrastructure
  • Cryptographic protocols
  • Microsoft Windows security technology

Keep exploring