<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">.NET Security Blog</title><subtitle type="html" /><id>http://blogs.msdn.com/shawnfa/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/shawnfa/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2009-05-22T10:54:00Z</updated><entry><title>Using SecAnnotate to Analyze Your Assemblies for Transparency Violations – An Example</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/11/18/using-secannotate-to-analyze-your-assemblies-for-transparency-violations-an-example.aspx" /><link rel="enclosure" type="text/xml" length="7018" href="http://blogs.msdn.com/shawnfa/attachment/9924363.ashx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/11/18/using-secannotate-to-analyze-your-assemblies-for-transparency-violations-an-example.aspx</id><published>2009-11-18T16:41:00Z</published><updated>2009-11-18T16:41:00Z</updated><content type="html">&lt;P&gt;SecAnnotate (available in the final .NET 4 SDK, and in beta form &lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/11/18/secannotate-beta.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/11/18/secannotate-beta.aspx"&gt;here&lt;/A&gt;) can be used to analyze your assemblies, especially APTCA assemblies in order to find transparency violations without needing code coverage from a test case.&amp;nbsp; Instead, the static analysis provided by SecAnnotate is valuable in ensuring that your assembly is fully correct from a transparency perspective.&amp;nbsp; Let’s take a look at how it might be used for a simple APTCA library.&lt;/P&gt;
&lt;P&gt;SecAnnotate runs in two different modes.&amp;nbsp; By default, it runs in annotation mode where it loops over your assembly looking for transparency violations and creating suggestions for how they might be fixed.&amp;nbsp; It then applies these suggestions to its model of the transparency of your assembly and loops again finding any violations that this updated set of annotations would have caused.&amp;nbsp; This process is repeated until there are no further transparency violations found.&amp;nbsp; That is, SecAnnotate’s algorithm is basically:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Make a pass over the assembly finding any transparency violations &lt;/LI&gt;
&lt;LI&gt;Generate a set of annotations that would fix the violations found in (1) &lt;/LI&gt;
&lt;LI&gt;Apply the fixes from (2) in memory to the assembly, with the more recent annotations taking precedent over annotations found in previous passes. &lt;/LI&gt;
&lt;LI&gt;If any updates were made in step (3), repeat from step (1) &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;It’s important to note that SecAnnotate is making suggestions for annotations that would fix the transparency violations.&amp;nbsp; A real human does need to scan the suggested annotations to ensure that they make sense for your code.&amp;nbsp; For example, SecAnnotate will tend to be conservative and if a violation could be fixed by making a method either critical or safe critical, SecAnnotate will recommend that the method be critical.&amp;nbsp; However, if you look at the method maybe it really is safe critical (using unverifiable code as an implementation detail, or issuing a demand, etc).&amp;nbsp; In that case you can mark the method as safe critical and avoid having the criticalness of the method fan-out.&lt;/P&gt;
&lt;P&gt;Similarly, in some cases SecAnnotate will indicate that a method must be safe critical (to satisfy inheritance rules for instance).&amp;nbsp; In those cases, it’s important to make sure that the method really is safe critical – that it is validating inputs, outputs, and ensuring that the operation being performed is safe.&lt;/P&gt;
&lt;P&gt;Let’s look at an example of using SecAnnotate on a Buffer class (used here to illustrate use of SecAnnotate, not as an example of a world-class Buffer type :-):&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: black thin inset; BORDER-LEFT: black thin inset; PADDING-BOTTOM: 1em; MARGIN: 1em 1em 1em 2em; PADDING-LEFT: 1em; PADDING-RIGHT: 1em; FONT-FAMILY: monospace; BACKGROUND: lightgrey; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: black thin inset; BORDER-RIGHT: black thin inset; PADDING-TOP: 1em"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; System;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; System.Runtime.InteropServices;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; System.Security;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; System.Security.Permissions;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: blue"&gt;assembly&lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;AllowPartiallyTrustedCallers&lt;/SPAN&gt;]&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: blue"&gt;assembly&lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;SecurityRules&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;SecurityRuleSet&lt;/SPAN&gt;.Level2)]&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;sealed&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Buffer&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;IDisposable&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;{&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IntPtr&lt;/SPAN&gt; m_buffer;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; m_size;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; Buffer(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; size)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (size &amp;lt;= 0)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"size"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_size = size;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_buffer = &lt;SPAN style="COLOR: #2b91af"&gt;Marshal&lt;/SPAN&gt;.AllocCoTaskMem(size);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ~Buffer()&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dispose(&lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Dispose()&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dispose(&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;GC&lt;/SPAN&gt;.SuppressFinalize(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Dispose(&lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; disposing)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (m_buffer != &lt;SPAN style="COLOR: #2b91af"&gt;IntPtr&lt;/SPAN&gt;.Zero)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Marshal&lt;/SPAN&gt;.FreeCoTaskMem(m_buffer);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_buffer = &lt;SPAN style="COLOR: #2b91af"&gt;IntPtr&lt;/SPAN&gt;.Zero;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IntPtr&lt;/SPAN&gt; NativePointer&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [SecurityPermission(&lt;SPAN style="COLOR: #2b91af"&gt;SecurityAction&lt;/SPAN&gt;.LinkDemand, UnmanagedCode = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)]&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (m_buffer == &lt;SPAN style="COLOR: #2b91af"&gt;IntPtr&lt;/SPAN&gt;.Zero)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ObjectDisposedException&lt;/SPAN&gt;(GetType().FullName);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; m_buffer;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; Size&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (m_buffer == &lt;SPAN style="COLOR: #2b91af"&gt;IntPtr&lt;/SPAN&gt;.Zero)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ObjectDisposedException&lt;/SPAN&gt;(GetType().FullName);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; m_size;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We can then build this into Buffer.dll and run SecAnnotate.exe over it as follows:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: black thin inset; BORDER-LEFT: black thin inset; PADDING-BOTTOM: 1em; MARGIN: 1em 1em 1em 2em; PADDING-LEFT: 1em; PADDING-RIGHT: 1em; FONT-FAMILY: monospace; BACKGROUND: lightgrey; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: black thin inset; BORDER-RIGHT: black thin inset; PADDING-TOP: 1em"&gt;
&lt;P style="MARGIN: 0px"&gt;C:\blog&amp;gt;c:\Windows\Microsoft.NET\Framework\v4.0.21006\csc.exe /debug /t:library Buffer.cs&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Microsoft (R) Visual C# 2010 Compiler version 4.0.21006.1&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Copyright (C) Microsoft Corporation. All rights reserved.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;C:\blog&amp;gt;"c:\Program Files\SecAnnotate\SecAnnotate.exe" Buffer.dll&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Microsoft (R) .NET Framework Security Transparency Annotator 4.0.21105.0&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Copyright (c) Microsoft Corporation.&amp;nbsp; All rights reserved.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Annotating 'buffer'.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Beginning pass 1.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Pass complete, 4 new annotation(s) generated.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Beginning pass 2.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Pass complete, 2 new annotation(s) generated.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Beginning pass 3.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Pass complete, 2 new annotation(s) generated.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Beginning pass 4.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Pass complete, 0 new annotation(s) generated.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Annotating complete. 8 errors found.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp; MethodsMustOverrideWithConsistentTransparency : 2 violation(s)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp; SecurityRuleSetLevel2MethodsShouldNotBeProtectedWithLinkDemands : 1&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp; violation(s)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp; TransparentMethodsMustNotReferenceCriticalCode : 4 violation(s)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp; TransparentMethodsShouldNotBeProtectedWithLinkDemands : 1 violation(s)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Writing annotation report 'TransparencyAnnotations.xml'.&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;We can see from the output that Buffer.dll required 3 passes to annotate and on the fourth pass no new violations were found.&lt;/P&gt;
&lt;P&gt;In the first pass, SecAnnotate found four annotations:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Buffer’s constructor calls Marshal.AllocCoTaskMem which is security critical.&amp;nbsp; Therefore, the suggested annotation is that Buffer’s constructor also become critical. &lt;/LI&gt;
&lt;LI&gt;Dispose(bool) calls Marshal.FreeCoTaskMem which is security critical.&amp;nbsp; SecAnnotate suggests that Dispose(bool) become security critical &lt;/LI&gt;
&lt;LI&gt;The NativePointer property getter is protected with a LinkDemand.&amp;nbsp; Level 2 transparency deprecates LinkDemands in favor of security critical APIs, so the LinkDemand should be removed and the getter be made critical. &lt;/LI&gt;
&lt;LI&gt;The NativePointer property additionally is security transparent, but is protected with a LinkDemand.&amp;nbsp; This is a strange pattern since transparent code shouldn’t be doing anything that needs protecting. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Those four annotations lead SecAnnotate in pass 1 to update the constructor, Dispose(bool) and NativePointer getter to be security critical and move on to pass 2.&amp;nbsp; The second pass results in the following violations:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Dispose() calls Dispose(bool) which was made critical in pass 1.&amp;nbsp; Since Dispose() is transparent, this is a violation.&amp;nbsp; SecAnnotate will now make Dispose() critical. &lt;/LI&gt;
&lt;LI&gt;Buffer’s Finalizer also calls Dispose(bool), which means that it also must now become security critical. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;After applying those annotations to its in-memory model of Buffer.dll, SecAnnotate continues onto pass 3:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Dispose() is critical from pass 2.&amp;nbsp; However, it implements security transparent interface member IDisposable.Dispose.&amp;nbsp; This is a violation of the transparency rules – so SecAnnotate suggests Dispose() become safe critical. &lt;/LI&gt;
&lt;LI&gt;Similarly, the finalizer is critical from pass 2, however it overrides transparent virtual method Object.Finalize. This is also a violation of transparency rules – so SecAnnotate suggests that the finalizer become safe critical. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Applying this final set of annotations leads to a pass with no new errors detected, and so SecAnnotate writes out its final report.&amp;nbsp; This report is divided into two sections – the required annotations section listing methods and types that need to be updated to fix transparency violations and the rule information section with details about each transparency rule that was tripped by this assembly.&lt;/P&gt;
&lt;P&gt;I’ve attached the output from SecAnnotate.exe to this blog post so that you can see an example report even if you haven’t run SecAnnotate yourself.&lt;/P&gt;
&lt;P&gt;Let’s look first at the required annotations section.&amp;nbsp; For each type, method, or field that needs to have a transparency annotation added, there will be an XML section with the suggested annotations, the reason for the annotation, and the pass number that the annotation was detected in.&lt;/P&gt;
&lt;P&gt;For example, the XML for Buffer’s constructor looks like this:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: black thin inset; BORDER-LEFT: black thin inset; PADDING-BOTTOM: 1em; MARGIN: 1em 1em 1em 2em; PADDING-LEFT: 1em; PADDING-RIGHT: 1em; FONT-FAMILY: monospace; BACKGROUND: lightgrey; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: black thin inset; BORDER-RIGHT: black thin inset; PADDING-TOP: 1em"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;.ctor(Int32)&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;annotations&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;critical&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;TransparentMethodsMustNotReferenceCriticalCode&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;pass&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;1&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceFile&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;c:\blog\buffer.cs&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceLine&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;20&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Transparent method 'Buffer..ctor(System.Int32)' references security critical method 'System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Int32)'.&amp;nbsp; In order for this reference to be allowed under the security transparency rules, either 'Buffer..ctor(System.Int32)' must become security critical or safe-critical, or 'System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Int32)' become security safe-critical or transparent.&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;critical&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;annotations&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which indicates that on pass 1, SecAnnotate detected that Buffer’s constructor was transparent but called Marshal.AllocCoTaskMem.&amp;nbsp; Since we have symbols available, SecAnnotate also pointed out the source file and line number that made this API call.&amp;nbsp; Because of this call, pass 1 suggests that the constructor (taking an Int32 parameter – in case you have multiple overloads) become security critical.&lt;/P&gt;
&lt;P&gt;The Dispose(bool) section looks very similar:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: black thin inset; BORDER-LEFT: black thin inset; PADDING-BOTTOM: 1em; MARGIN: 1em 1em 1em 2em; PADDING-LEFT: 1em; PADDING-RIGHT: 1em; FONT-FAMILY: monospace; BACKGROUND: lightgrey; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: black thin inset; BORDER-RIGHT: black thin inset; PADDING-TOP: 1em"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Dispose(Boolean)&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;annotations&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;critical&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;TransparentMethodsMustNotReferenceCriticalCode&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;pass&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;1&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceFile&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;c:\blog\buffer.cs&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceLine&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;38&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Transparent method 'Buffer.Dispose(System.Boolean)' references security critical method 'System.Runtime.InteropServices.Marshal.FreeCoTaskMem(System.IntPtr)'.&amp;nbsp; In order for this reference to be allowed under the security transparency rules, either 'Buffer.Dispose(System.Boolean)' must become security critical or safe-critical, or 'System.Runtime.InteropServices.Marshal.FreeCoTaskMem(System.IntPtr)' become security safe-critical or transparent.&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;critical&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;annotations&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As we expect NativePointer getter has two violations in pass 1 – both of which suggest that the method become security critical.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: black thin inset; BORDER-LEFT: black thin inset; PADDING-BOTTOM: 1em; MARGIN: 1em 1em 1em 2em; PADDING-LEFT: 1em; PADDING-RIGHT: 1em; FONT-FAMILY: monospace; BACKGROUND: lightgrey; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: black thin inset; BORDER-RIGHT: black thin inset; PADDING-TOP: 1em"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;get_NativePointer()&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;annotations&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;critical&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;SecurityRuleSetLevel2MethodsShouldNotBeProtectedWithLinkDemands&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;pass&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;1&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceFile&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;c:\blog\buffer.cs&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceLine&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;47&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;'Buffer.get_NativePointer()' is protected with a LinkDemand for 'SecurityPermissionAttribute'.&amp;nbsp; In the level 2 security rule set, it should be protected by being security critical instead.&amp;nbsp; Remove the LinkDemand and mark 'Buffer.get_NativePointer()' security critical.&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;TransparentMethodsShouldNotBeProtectedWithLinkDemands&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;pass&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;1&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceFile&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;c:\blog\buffer.cs&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceLine&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;47&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Transparent method 'Buffer.get_NativePointer()' is protected with a LinkDemand for 'SecurityPermissionAttribute'.&amp;nbsp; Remove this LinkDemand, or make the method security critical or safe-critical.&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;critical&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;annotations&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The rules violated here are different, but both suggest that the method become critical and so are both listed in the &amp;lt;critical&amp;gt; section of the method’s report.&lt;/P&gt;
&lt;P&gt;More interesting is the report output for Dispose() – remember, pass 2 detected that this method should be critical because it is calling the critical Dispose(bool) overload from pass 1.&amp;nbsp; However, pass 3 detected that being critical actually tripped an inheritance rule violation and the method should really be safe critical:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: black thin inset; BORDER-LEFT: black thin inset; PADDING-BOTTOM: 1em; MARGIN: 1em 1em 1em 2em; PADDING-LEFT: 1em; PADDING-RIGHT: 1em; FONT-FAMILY: monospace; BACKGROUND: lightgrey; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: black thin inset; BORDER-RIGHT: black thin inset; PADDING-TOP: 1em"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Dispose()&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;annotations&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;safeCritical&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;MethodsMustOverrideWithConsistentTransparency&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;pass&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;3&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceFile&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;c:\blog\buffer.cs&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceLine&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;29&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Critical method 'Buffer.Dispose()' is overriding transparent or safe critical method 'System.IDisposable.Dispose()' in violation of method override rules.&amp;nbsp; 'Buffer.Dispose()' must become transparent or safe-critical in order to override a transparent or safe-critical virtual method or implement a transparent or safe-critical interface method.&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;safeCritical&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;critical&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;TransparentMethodsMustNotReferenceCriticalCode&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;pass&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;2&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceFile&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;c:\blog\buffer.cs&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceLine&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;30&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Transparent method 'Buffer.Dispose()' references security critical method 'Buffer.Dispose(System.Boolean)'.&amp;nbsp; In order for this reference to be allowed under the security transparency rules, either 'Buffer.Dispose()' must become security critical or safe-critical, or 'Buffer.Dispose(System.Boolean)' become security safe-critical or transparent.&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;critical&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;annotations&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This section contains both a &amp;lt;safeCritical&amp;gt; and a &amp;lt;critical&amp;gt; section – so SecAnnotate is making two different suggestions for this method.&amp;nbsp; The way to read this output is to scan for the pass numbers.&amp;nbsp; Remember that the SecAnnotate algorithm has the later passes override the earlier passes for transparency purposes so the final suggested annotation is the one with the largest pass number.&amp;nbsp; In this case we have a safe critical suggestion in pass 3 – which means that SecAnnotate is suggesting this method be safe critical.&lt;/P&gt;
&lt;P&gt;If SecAnnotate is recommending the annotation from the largest pass number, then why does it output all of the lower passes as well?&amp;nbsp;&amp;nbsp; The reason is to provide the most context to the person analyzing the report.&amp;nbsp; By tracing through the passes from lowest number to highest we can see why SecAnnotate first decided that the method must be critical before later deciding to make it safe critical (and the reason behind that switch).&amp;nbsp;&amp;nbsp; As we’ll see later, that information can be quite useful when deciding upon which annotations to add to your source code.&lt;/P&gt;
&lt;P&gt;In this case, SecAnnotate is saying that the second pass caused Dispose() to be marked critical due to calling Dispose(bool).&amp;nbsp; In the third pass, in order to satisfy inheritance rules, Dispose() needed to be marked safe critical.&lt;/P&gt;
&lt;P&gt;Similar analysis applies to the finalizer section of the report.&lt;/P&gt;
&lt;P&gt;Now that we’ve finished looking through the report, let’s update the Buffer code to use SecAnnotate’s recommendations in order to fix its transparency violations.&lt;/P&gt;
&lt;P&gt;To start with, the final set of annotations that SecAnnotate recommends for us is:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Constructor –&amp;gt; critical (from pass 1) &lt;/LI&gt;
&lt;LI&gt;Dispose() –&amp;gt; safe critical (from pass 3) &lt;/LI&gt;
&lt;LI&gt;Dispose(bool) –&amp;gt; critical (from pass 1) &lt;/LI&gt;
&lt;LI&gt;Finalizer –&amp;gt; safe critical (from pass 3) &lt;/LI&gt;
&lt;LI&gt;NativePointer getter –&amp;gt; critical (from pass 1) &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;As I mentioned earlier, SecAnnotate is making recommendations for a set of annotations that will fix up transparency violations in the assembly being analyzed.&amp;nbsp; However, a real human should always look at the recommendations to ensure that they are optimal and correct for the particular code.&lt;/P&gt;
&lt;P&gt;Let’s look at them one by one.&amp;nbsp; Generally, it’s convenient to work from APIs identified in earlier passes out to APIs that were identified in later passes (as we’ll see here).&amp;nbsp; In fact one technique for using SecAnnotate is to limit it to only one pass, fix up that pass, and then rerun SecAnnotate to completion.&amp;nbsp; I’ll talk more about using that technique in a later post.&lt;/P&gt;
&lt;P&gt;With that in mind, let’s start with the constructor which was flagged in pass 1.&amp;nbsp; The constructor must be critical because it calls a critical API to allocate memory.&amp;nbsp; However, making the constructor itself security critical has an important consequence – transparent code can no longer use the constructor code.&amp;nbsp; If we want to continue to allow transparent access to the Buffer class then we need to find a way to make the constructor safe critical instead.&lt;/P&gt;
&lt;P&gt;The critical operation that was identified is calling a critical API to allocate unmanaged memory.&amp;nbsp; We might want to prevent transparent code from being able to do this if they could access the address of this memory, however there is no direct transparent access to that memory.&amp;nbsp; Further, the allocated buffer will be released when the finalizer or dispose method is called – which means that a leak can only be caused by transparent code if it holds onto a strong reference to the Buffer object.&amp;nbsp; That is not a threat that this API cares about because partial trust code could already cause the same effect by simply holding onto a strong reference to a large managed array.&lt;/P&gt;
&lt;P&gt;You might notice that it doesn’t make much sense to make the constructor safe critical in order to expose it to partial trust code, after all you must be critical to do anything interesting with a Buffer object in the first place.&amp;nbsp;&amp;nbsp; While that’s true, being safe critical also opens another important use case – full trust callers do not need to be critical to allocate a Buffer object either.&amp;nbsp; Their code that accesses the buffer itself may still be required to be critical, however we don’t expand that out to the code that is simply allocating the buffer in the first place.&amp;nbsp; By not requiring the allocation methods to become critical, we’ve reduced the audit burden on them, as we’ve done that work here lower in the stack to prove that this is a safe operation.&lt;/P&gt;
&lt;P&gt;From this analysis, it turns out to be safe to make the constructor safe critical.&lt;/P&gt;
&lt;P&gt;Next let’s look at the Dispose(bool) method.&amp;nbsp; This is also flagged as being security critical because it references a security critical API.&amp;nbsp; That API is exposing the ability to release the memory used by the Buffer.&amp;nbsp; However, since we allow transparent code to allocate the buffer, it stands to reason that we also want it to be able to free the buffer.&amp;nbsp; Our threat model shows that there is a threat here – since the Buffer class is not thread safe, it is possible for critical code to be using the memory address that the buffer allocated at the same time that the buffer is released.&amp;nbsp; This could lead to transparent code causing critical code to trash memory on the heap.&lt;/P&gt;
&lt;P&gt;That threat might be mitigated by thoroughly documenting that this Buffer type is not thread safe, and that critical code should not expose it out to transparent code that might trigger a race condition on the use of the object.&amp;nbsp;&amp;nbsp; Analysis of the type makes us believe that this would be the most common use pattern anyway (there’s not any compelling functionality that is gained by allowing outside code access to your buffer, especially malicious partial trust code).&lt;/P&gt;
&lt;P&gt;With that in mind, it turns out that safe critical might be a better annotation for Dispose(bool) rather than critical.&lt;/P&gt;
&lt;P&gt;The final pass 1 annotation is the NativePointer getter.&amp;nbsp; This is flagged as being critical because it used to be protected with a LinkDemand which is now obsolete in the level 2 security transparency model.&amp;nbsp; Making the getter be critical makes sense because we don’t want to expose the address of the pointer out to unaudited or untrusted code.&lt;/P&gt;
&lt;P&gt;However, that leads us to an interesting thought – if we don’t want to expose the unaudited address to partial trust code, and that address is stored in the m_buffer field, then it might make sense to make that field itself security critical.&amp;nbsp; In general SecAnnotate cannot make a suggestion like this because it doesn’t know what fields store sensitive information.&amp;nbsp; However, we know that this field is sensitive, so we should make it critical.&lt;/P&gt;
&lt;P&gt;This will have the side effect of causing SecAnnotate (and the runtime) to flag any code that gets added to the Buffer class later which accesses m_buffer directly, rather than going through the NativePointer property getter.&amp;nbsp; Since that code is touching a sensitive field, SecAnnotate will flag it for accessing a critical piece of data and ensure that it is either critical or safe critical and audited.&lt;/P&gt;
&lt;P&gt;Now, let’s move to the later passes.&amp;nbsp;&amp;nbsp; Both Dispose() and the finalizer got flagged in pass 2 to be security safe critical because they were using security critical method Dispose(bool).&amp;nbsp; However, we previously decided that Dispose(bool) should be safe critical rather than critical.&amp;nbsp; That means that both Dispose() and the finalizer can stay transparent (since pass 2 would now be satisfied by transparent calling critical code).&amp;nbsp;&amp;nbsp; This is an example of using the full SecAnnotate report in order to come up with a better set of annotations – we know why the method was flagged to be critical in pass 2, so we know we may not need to consider later passes.&lt;/P&gt;
&lt;P&gt;With all that in mind, our final set of proposed annotations based upon the SecAnnotate report is:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;m_buffer –&amp;gt; critical &lt;/LI&gt;
&lt;LI&gt;Constructor –&amp;gt; safe critical &lt;/LI&gt;
&lt;LI&gt;Dispose(bool) –&amp;gt; safe critical &lt;/LI&gt;
&lt;LI&gt;NativePointer getter –&amp;gt; critical &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Putting these updates into place, we can rerun SecAnnotate to check our work.&amp;nbsp; In this case, SecAnnotate will find that there is one remaining violation to correct which stemmed from our decision to push the security critical attribute down from the NativePointer property onto the field that it is exposing (note that had we gone with the original SecAnnotae suggestion of only marking the NativePointer getter as critical, this violation wouldn’t have shown up – instead it was our decision to mark the underlying sensitive data as critical that flagged a new violation):&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: black thin inset; BORDER-LEFT: black thin inset; PADDING-BOTTOM: 1em; MARGIN: 1em 1em 1em 2em; PADDING-LEFT: 1em; PADDING-RIGHT: 1em; FONT-FAMILY: monospace; BACKGROUND: lightgrey; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: black thin inset; BORDER-RIGHT: black thin inset; PADDING-TOP: 1em"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;get_Size()&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;annotations&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;critical&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;TransparentMethodsMustNotReferenceCriticalCode&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;pass&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;1&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceFile&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;c:\blog\buffer.cs&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;sourceLine&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;72&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;Transparent method 'Buffer.get_Size()' references security critical field 'Buffer.m_buffer'.&amp;nbsp; In order for this reference to be allowed under the security transparency rules, either 'Buffer.get_Size()' must become security critical or safe-critical, or 'Buffer.m_buffer' become security safe-critical or transparent.&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;reason&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;rule&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;critical&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;annotations&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;method&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the Size getter is accessing the critical m_buffer field, SecAnnotate flags it to be critical.&amp;nbsp; However, since it is not exposing this field and simply using it to make sure that the buffer hasn’t yet been cleaned up (the end user can never figure out anything other than the buffer has a non-null field value from calling this property), we can safely make this a safe critical field as well.&lt;/P&gt;
&lt;P&gt;With that final update in place:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: black thin inset; BORDER-LEFT: black thin inset; PADDING-BOTTOM: 1em; MARGIN: 1em 1em 1em 2em; PADDING-LEFT: 1em; PADDING-RIGHT: 1em; FONT-FAMILY: monospace; BACKGROUND: lightgrey; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: black thin inset; BORDER-RIGHT: black thin inset; PADDING-TOP: 1em"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; System;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; System.Runtime.InteropServices;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; System.Security;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; System.Security.Permissions;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: blue"&gt;assembly&lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;AllowPartiallyTrustedCallers&lt;/SPAN&gt;]&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: blue"&gt;assembly&lt;/SPAN&gt;: SecurityRules(SecurityRuleSet.Level2)]&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;sealed&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Buffer&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;IDisposable&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;{&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;SPAN style="COLOR: #2b91af"&gt;SecurityCritical&lt;/SPAN&gt;]&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IntPtr&lt;/SPAN&gt; m_buffer;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; m_size;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Safe critical because we're only exposing the ability to allocate native memory, not&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// access that memory directly (access is gated through security critical APIs).&amp;nbsp; Since&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// the threat of using up all of the memory for a process is not something that we're&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// looking to mitigate (after all, holding a large managed array has the same effect)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// we don't need to gate access to this constructor.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;SPAN style="COLOR: #2b91af"&gt;SecuritySafeCritical&lt;/SPAN&gt;]&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; Buffer(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; size)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (size &amp;lt;= 0)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"size"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_size = size;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_buffer = &lt;SPAN style="COLOR: #2b91af"&gt;Marshal&lt;/SPAN&gt;.AllocCoTaskMem(size);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ~Buffer()&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dispose(&lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Dispose()&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dispose(&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;GC&lt;/SPAN&gt;.SuppressFinalize(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Safe critical because we're simply releasing the memory held by the buffer.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// This is not safe to use cross-thread, so it is important to document that&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// trusted code not give access to their Buffer classes to untrusted code&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// which may trigger race conditions between use of the Buffer and release of&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// it.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;SPAN style="COLOR: #2b91af"&gt;SecuritySafeCritical&lt;/SPAN&gt;]&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Dispose(&lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; disposing)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (m_buffer != &lt;SPAN style="COLOR: #2b91af"&gt;IntPtr&lt;/SPAN&gt;.Zero)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Marshal&lt;/SPAN&gt;.FreeCoTaskMem(m_buffer);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_buffer = &lt;SPAN style="COLOR: #2b91af"&gt;IntPtr&lt;/SPAN&gt;.Zero;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IntPtr&lt;/SPAN&gt; NativePointer&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;SPAN style="COLOR: #2b91af"&gt;SecurityCritical&lt;/SPAN&gt;]&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (m_buffer == &lt;SPAN style="COLOR: #2b91af"&gt;IntPtr&lt;/SPAN&gt;.Zero)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ObjectDisposedException&lt;/SPAN&gt;(GetType().FullName);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; m_buffer;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; Size&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Safe critical since we aren't exposing the m_buffer field, and just use it&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// as an internal implementation detail to detect if the buffer is disposed&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// or not.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;SPAN style="COLOR: #2b91af"&gt;SecuritySafeCritical&lt;/SPAN&gt;]&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (m_buffer == &lt;SPAN style="COLOR: #2b91af"&gt;IntPtr&lt;/SPAN&gt;.Zero)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ObjectDisposedException&lt;/SPAN&gt;(GetType().FullName);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; m_size;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We should now be done annotating this class.&amp;nbsp; To ensure that we are, in fact, done we can run SecAnnotate in verification mode.&amp;nbsp; Unlike the default annotation mode, verification mode does not attempt to make multiple passes over the assembly and figure out a suggested set of annotations to fix any errors.&amp;nbsp; Instead, it just runs to ensure that there are no existing transparency violations in the assembly.&lt;/P&gt;
&lt;P&gt;Its return value is equivalent to the number of violations found, so running in verification mode can be used as a post-build step to ensure that assemblies contain no transparency violations:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: black thin inset; BORDER-LEFT: black thin inset; PADDING-BOTTOM: 1em; MARGIN: 1em 1em 1em 2em; PADDING-LEFT: 1em; PADDING-RIGHT: 1em; FONT-FAMILY: monospace; BACKGROUND: lightgrey; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: black thin inset; BORDER-RIGHT: black thin inset; PADDING-TOP: 1em"&gt;
&lt;P style="MARGIN: 0px"&gt;C:\blog&amp;gt;"c:\Program Files\SecAnnotate\SecAnnotate.exe" /v Buffer.dll&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Microsoft (R) .NET Framework Security Transparency Annotator 4.0.21105.0&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Copyright (c) Microsoft Corporation.&amp;nbsp; All rights reserved.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Verifying 'buffer'.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Verification complete. 0 error(s) found.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;Transparency annotations on all assemblies successfully verified.&lt;/P&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And with that we’re done – Buffer.dll is now verified to contain no violations of security transparency rules.&amp;nbsp; When we go to ship this assembly, the SecuritySafeCritical surface area will be subject to a security audit to ensure that they are safe and secure, and that our threat model has sufficient mitigation for any threats exposed.&amp;nbsp; &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9924363" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /><category term="Transparency" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Transparency/default.aspx" /><category term="SecAnnotate" scheme="http://blogs.msdn.com/shawnfa/archive/tags/SecAnnotate/default.aspx" /></entry><entry><title>SecAnnotate Beta</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/11/18/secannotate-beta.aspx" /><link rel="enclosure" type="application/octet-stream" length="768000" href="http://blogs.msdn.com/shawnfa/attachment/9924347.ashx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/11/18/secannotate-beta.aspx</id><published>2009-11-18T16:24:00Z</published><updated>2009-11-18T16:24:00Z</updated><content type="html">&lt;P&gt;One of the design goals of the security transparency system in the CLR is that it should be as static as possible and not rely on dynamic state (such as the call stack) to function.&amp;nbsp; A fallout of this is that we can write tools to analyze assemblies and find transparency violations in the assembly without having to trip over those violations at runtime through a test case to find them.&lt;/P&gt;
&lt;P&gt;The primary tool that does this is the .NET Framework Security Transparency Annotator – or SecAnnotate for short.&amp;nbsp; This tool will ship in the .NET 4 SDK when it releases, however it is not in the .NET 4 Beta 2 SDK.&amp;nbsp; Instead, until the final release of the Framework SDK is available, SecAnnotate is available to&amp;nbsp; download from this blog post.&lt;/P&gt;
&lt;P&gt;The SecAnnotate does require that you’ve installed the &lt;A href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx" mce_href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx"&gt;.NET Framework 4 Beta 2&lt;/A&gt; in order to run.&lt;/P&gt;
&lt;P&gt;I’ll have some follow up blog posts with information on how to make use of SecAnnotate when developing your transparency aware managed code.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9924347" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /><category term="Transparency" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Transparency/default.aspx" /><category term="SecAnnotate" scheme="http://blogs.msdn.com/shawnfa/archive/tags/SecAnnotate/default.aspx" /></entry><entry><title>Differences Between the Security Rule Sets</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/11/12/differences-between-the-security-rule-sets.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/11/12/differences-between-the-security-rule-sets.aspx</id><published>2009-11-12T20:59:00Z</published><updated>2009-11-12T20:59:00Z</updated><content type="html">&lt;p&gt;In my last post I talked about the &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/11/11/transparency-models-a-tale-of-two-levels.aspx"&gt;two different security rule sets supported by the v4 CLR&lt;/a&gt;.&amp;#160; At a high level, level 1 is the v2.0 security transparency model, and level 2 encompasses the updated v4 security transparency model.&amp;#160; Digging down a little deeper, it’s interesting to look at some of the exact details that change between the various transparency models.&amp;#160; For fun, let’s also compare both rule sets to Silverlight.&lt;/p&gt;  &lt;p&gt;A couple of interesting things to notice – the level 2 rules look quite similar to the Silverlight rules.&amp;#160; This means that you can basically learn one set of core security enforcement rules and apply them to both desktop and Silverlight development.&amp;#160; As I mentioned previously, it’s also interesting to note that &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/11/09/transparency-as-enforcement-in-clr-v4.aspx"&gt;the penalty for transparency violations in level 2 transparency are generally hard failures, while the penalty in level 1 was generally triggering a full trust demand&lt;/a&gt;.&amp;#160; That means that many of the level 1 transparency violations are generally more expensive (since they incur a runtime check at each invocation, rather than a single JIT time check).&amp;#160; They also have a tendency to hide on you, since runtime checks can sometimes succeed during testing, and fail once you deploy your app to a new environment.&lt;/p&gt;  &lt;p&gt;In the interest of completeness, I’m going to list several things in this table that I have yet to blog about.&amp;#160; (Although, you’ll recognize many of &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/11/03/transparency-101-basic-transparency-rules.aspx"&gt;the core transparency rules&lt;/a&gt; covered here.) Over the next few weeks, I should cover the various interesting pieces of this table – but rather than waiting to blog about them before adding them here, I think it would be more useful to list the full table now.&lt;/p&gt;  &lt;p&gt;I’ll also note that while level 1 transparency is supported in the v4 runtime, it exists solely for compatibility with existing code.&amp;#160; No new code should be written using the level 1 rule set, and over time existing code should consider moving to the level 2 rule set as well.&amp;#160; (That is, don’t necessarily treat this chart as a way to select which security rule set you want to work with, but rather as a way to compare the evolution of transparency from .NET 2.0, to Silverlight 2, and finally into .NET 4).&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2"&gt;&lt;thead&gt;     &lt;tr&gt;       &lt;td&gt;&amp;#160;&lt;/td&gt;        &lt;td&gt;&lt;strong&gt;Level 1 (v2)&lt;/strong&gt;&lt;/td&gt;        &lt;td&gt;&lt;strong&gt;Level 2 (v4)&lt;/strong&gt;&lt;/td&gt;        &lt;td&gt;&lt;strong&gt;Silverlight 2&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/thead&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Introduced in&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;.NET 2.0&lt;/td&gt;        &lt;td&gt;.NET 4&lt;/td&gt;        &lt;td&gt;Silverlight 2&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Default for&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;Assemblies built against runtimes prior to v4&lt;/td&gt;        &lt;td&gt;Assemblies built against the v4 runtime&lt;/td&gt;        &lt;td&gt;Silverlight assemblies&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Explicit attribute&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;[&lt;span style="color: blue"&gt;assembly&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;SecurityRules&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SecurityRuleSet&lt;/span&gt;.Level1)] &lt;/td&gt;        &lt;td&gt;[&lt;span style="color: blue"&gt;assembly&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;SecurityRules&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SecurityRuleSet&lt;/span&gt;.Level2)]&lt;/td&gt;        &lt;td&gt;N/A – all assemblies use the Silverlight transparency model&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Attribute to create a mixed transparency assembly (containing a combination of critical, safe critical, and transparent code in a single assembly)&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;[&lt;span style="color: blue"&gt;assembly&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;SecurityCritical&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SecurityCriticalScope&lt;/span&gt;.Explicit)]&lt;/td&gt;        &lt;td&gt;[&lt;span style="color: blue"&gt;assembly&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;AllowPartiallyTrustedCallers&lt;/span&gt;]&lt;/td&gt;        &lt;td&gt;N/A – platform assemblies are all mixed transparency, application assemblies are all fully transparent&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Effect of the APTCA attribute&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;Allows partial trusted callers access to the assembly by removing an implicit link demand for full trust on all code in signed assemblies.&lt;/td&gt;        &lt;td&gt;Allows partial trusted callers access to the assembly by making all code in the assembly transparent by default. (Although the assembly can explicitly make some of the code critical or safe critical.)&lt;/td&gt;        &lt;td&gt;N/A – Silverlight does not have the AllowPartiallyTrustedCallers attribute&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Publicly visible security critical members are implicitly safe critical&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;Yes – there is no such thing as a publicly visible safe critical API in level 1 transparency.&lt;/td&gt;        &lt;td&gt;No – only APIs explicitly annotated as SecuritySafeCritical are safe critical.&lt;/td&gt;        &lt;td&gt;No – only APIs explicitly annotated as SecuritySafeCritical are safe critical.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Link demands should be used for JIT time security enforcement&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;Yes – since publicly visible critical APIs are implicitly safe critical, link demands must be used to prevent JIT time access to security sensitive APIs.&lt;/td&gt;        &lt;td&gt;No – since code must be critical to access either a link demand or a public critical API, level 2 transparency deprecates the use of granular link demands in favor of simply protecting security sensitive APIs by keeping them security critical.&lt;/td&gt;        &lt;td&gt;No – Silverlight does not support link demands.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Result if a transparent method attempts to call a method protected by a link demand&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;The link demand is converted into a full demand at runtime, which may fail with a SecurityException.&lt;/td&gt;        &lt;td&gt;A MethodAccessException will be thrown at JIT time.&lt;/td&gt;        &lt;td&gt;N/A – Silverlight does not support link demands.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Result if a transparent method attempts to call unmanaged code&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;A demand for SecurityPermission/UnmanagedCode will be triggered at runtime, which may fail with a SecurityException.&lt;/td&gt;        &lt;td&gt;A MethodAccessException will be thrown at JIT time.&lt;/td&gt;        &lt;td&gt;A MethodAccessException will be thrown at JIT time.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Result if a transparent method has unverifiable IL&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;A demand for SecurityPermission/UnmanagedCode will be triggered at runtime, which may fail with a SecurityException.&lt;/td&gt;        &lt;td&gt;A VerificationException will be thrown at JIT time.&lt;/td&gt;        &lt;td&gt;A VerificationException will be thrown at JIT time.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Result if a transparent method attempts to perform a security assert&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;An InvalidOperationException is thrown at runtime.&lt;/td&gt;        &lt;td&gt;An InvalidOperationException is thrown at runtime.&lt;/td&gt;        &lt;td&gt;N/A – Silverlight does not support security asserts.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Result if at transparent attempts to call a critical method&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;If the method is:          &lt;ul&gt;           &lt;li&gt;&lt;em&gt;Within the same assembly&lt;/em&gt; – a MethodAccessException is thrown at JIT time. &lt;/li&gt;            &lt;li&gt;&lt;em&gt;In another level 1 assembly&lt;/em&gt; – N/A (level 1 publicly visible critical code is implicitly safe critical, so there is no violation). &lt;/li&gt;            &lt;li&gt;&lt;em&gt;In a level 2 assembly&lt;/em&gt; – the critical code is treated as safe critical with a link demand for full trust.&amp;#160; This, in turn, triggers a runtime demand for full trust which may fail with a SecurityException. &lt;/li&gt;         &lt;/ul&gt;       &lt;/td&gt;        &lt;td&gt;A MethodAccessException is thrown at JIT time.&lt;/td&gt;        &lt;td&gt;A MethodAccessException is thrown at JIT time.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Transparency of partial trust assemblies&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;Fully transparent – all transparency annotations are ignored for partial trust assemblies.&lt;/td&gt;        &lt;td&gt;Fully transparent – all transparency annotations are ignored for partial trust assemblies.&lt;/td&gt;        &lt;td&gt;Application assemblies are fully transparent – all transparency annotations are ignored for them.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Default transparency for assemblies which are security agnostic&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;All types are transparent, all methods are safe critical.&lt;/td&gt;        &lt;td&gt;All types and methods are security critical (except where this would violate inheritance rules).&lt;/td&gt;        &lt;td&gt;N/A – all Silverlight assemblies are exposed to the sandbox.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Protection for non-APTCA assemblies being used by partial trust code&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;Provided to all signed assemblies by adding an implicit link demand for full trust to all code within the signed assembly.&lt;/td&gt;        &lt;td&gt;Provided to all fully trusted assemblies because all of the types and methods within a non-APTCA assembly are security critical by default (see above).&lt;/td&gt;        &lt;td&gt;N/A – all Silverlight assemblies are exposed to the sandbox.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Fully transparent assemblies are exposed to partial trust&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;No – signed fully transparent assemblies still have an implicit link demand for full trust unless they are additionally marked APTCA&lt;/td&gt;        &lt;td&gt;Yes – no additional APTCA attribute is required for fully transparent code. &lt;/td&gt;        &lt;td&gt;Yes&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Critical members can be converted to safe critical members protected by a link demand for FullTrust&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;No&lt;/td&gt;        &lt;td&gt;Yes.&amp;#160; If a publicly visible security critical method is called by a security transparent level 1 method, the CLR will treat the critical method as if it was safe critical with a link demand for full trust on it.&amp;#160; (If the caller is level 2, this conversion does not occur).&lt;/td&gt;        &lt;td&gt;No&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Security critical annotations support both applying to a larger scope (type, assembly, etc) and all of its contained members, or only the scope itself&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;Yes.&amp;#160; The SecurityCriticalScope enumeration is used to toggle between having a SecurityCritical attribute apply to only the container (SecurityCriticalScope.Explicit) or the container and all of its members (SecurityCriticalScope.Everything)&lt;/td&gt;        &lt;td&gt;No.&amp;#160; SecurityCriticalScope is not used in level 2 assemblies, and is ignored if it is provided.&amp;#160; All security critical attributes implicitly use SecurityCriticalScope.Everything.&lt;/td&gt;        &lt;td&gt;No.&amp;#160; All SecurityCritical attributes implicitly use SecurityCriticalScope.Everything.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Members introduced within a security critical scope can add additional attributes to become safe critical&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;Yes.&amp;#160; Adding a SecurityTreatAsSafe or SecuritySafeCritical attribute within a critical scope changed the contained member’s transparency.&lt;/td&gt;        &lt;td&gt;No.&amp;#160; The larger scope always wins, attributes applied at smaller scopes are not considered.&lt;/td&gt;        &lt;td&gt;No.&amp;#160; The larger scope always wins, attributes applied at smaller scopes are not considered. &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Critical or safe critical annotations at a larger scope (type, assembly, etc) introduce only to methods introduced directly in a type. Overridden virtuals and interface implementations do not use the larger scoped annotation and are transparent by default.&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;No, the outer scope applies to all contained members.&lt;/td&gt;        &lt;td&gt;Yes.&amp;#160; Since the transparency of the members not introduced by the local type are not under its direct control, overrides and interface implementations must restate their intended transparency.&lt;/td&gt;        &lt;td&gt;Yes.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Can define security critical attributes that are protected from use by partial trust code&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;No.&amp;#160; Link demands must be used for this purpose.&lt;/td&gt;        &lt;td&gt;Yes.&amp;#160; Security critical attributes cannot be applied to transparent targets.&lt;/td&gt;        &lt;td&gt;Yes.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;If reflected upon, security critical targets trigger a security demand&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;No, although link demands do become full demands under reflection.&lt;/td&gt;        &lt;td&gt;Yes.&amp;#160; Reflecting upon a level 2 security critical target will trigger a full demand for full trust.&amp;#160; This happens based upon the target of the reflection, not based upon the code that is performing the reflection operation.&amp;#160; (For example, level 1 code reflecting on a level 2 critical method will still trigger the demand).&lt;/td&gt;        &lt;td&gt;Yes.&amp;#160; Transparent code may not reflect upon critical code.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;th&gt;&lt;strong&gt;Delegate binding rules are enforced&lt;/strong&gt;&lt;/th&gt;        &lt;td&gt;No.&lt;/td&gt;        &lt;td&gt;No.&lt;/td&gt;        &lt;td&gt;Yes – Silverlight contains delegate binding rules that prevent critical delegates from being bound to transparent targets and vice versa.&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9921126" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="Silverlight" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Silverlight/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /><category term="Transparency" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Transparency/default.aspx" /></entry><entry><title>Transparency Models: A Tale of Two Levels</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/11/11/transparency-models-a-tale-of-two-levels.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/11/11/transparency-models-a-tale-of-two-levels.aspx</id><published>2009-11-11T19:24:51Z</published><updated>2009-11-11T19:24:51Z</updated><content type="html">&lt;p&gt;Earlier this week, we looked at how the &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/11/09/transparency-as-enforcement-in-clr-v4.aspx"&gt;v4 CLR continued the evolution of the security transparency model&lt;/a&gt; that started in v2 and started evolving with Silverlight in order to make it the primary security enforcement mechanism of the .NET 4 runtime.&lt;/p&gt; &lt;p&gt;The result is that the v4 transparency model, while having roots in the v2 transparency model, is also somewhat different in both the rules that it enforces and how it enforces them.&amp;nbsp; These differences are enough that code written for the v2 transparency model will not likely run without some modifications in the v4 model.&amp;nbsp; Since the v4 runtime is compatible with v2 assemblies, the CLR security system needs to provide a way for code written for the older v2 transparency model to continue to run until it is updated to work in the more modern v4 transparency model.&lt;/p&gt; &lt;p&gt;This was done by splitting the transparency models up into two rule sets:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Level 1 - the security transparency model that shipped in the v2 CLR  &lt;li&gt;Level 2 - the security transparency model that ships with the v4 CLR&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Assemblies built against the v2 .NET framework are automatically considered to be level 1 assemblies - after all, if they were written before the v4 transparency model even shipped how could they possibly be written to use that model?&amp;nbsp;&amp;nbsp; Similarly, assemblies built against the v4 runtime are by default considered to be using the level 2 model.&amp;nbsp; Since level 1 exists largely for compatibility reasons, new code starts out automatically using the modern transparency enforcement system.&lt;/p&gt; &lt;p&gt;What about existing code bases that are simply being recompiled for v4 however?&amp;nbsp; Those assemblies were also not written with the v4 transparency rules in mind, so it doesn't follow that a simple recompile has fixed up the assembly's code to understand the new security rules.&amp;nbsp; In fact, the first step in moving v2 code to v4 is very likely trying to simply getting it to compile with as few source changes as possible.&lt;/p&gt; &lt;p&gt;For assemblies in this bucket, the CLR offers an attribute to lock an assembly (even though it is built for v4) back to the level 1 security transparency rules.&amp;nbsp; In order to do that, all the assembly needs to do is apply the following assembly level attribute:&lt;/p&gt; &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt; &lt;p style="margin: 0px"&gt;[&lt;span style="color: blue"&gt;assembly&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;SecurityRules&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SecurityRuleSet&lt;/span&gt;.Level1)]&lt;/p&gt;&lt;/div&gt; &lt;p&gt;(Both the SecurityRulesAttribute and the SecurityRuleSet enumeration live in the System.Security namespace)  &lt;p&gt;Adding this attribute unblocks the assembly being recompiled from being forced to update to the new security transparency model immediately, allowing you more time to make that transition.&amp;nbsp; When the assembly is ready to move forward to the v4 transparency model, the level 1 attribute can simply be replaced with the equivalent attribute stating that the assembly is now going to be using the level 2 rules:  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt; &lt;p style="margin: 0px"&gt;[&lt;span style="color: blue"&gt;assembly&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;SecurityRules&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SecurityRuleSet&lt;/span&gt;.Level2)]&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Although this isn't strictly necessary, as level 2 is the default for all assemblies built against the v4 runtime, I consider it a good practice to explicitly attribute assemblies with the security rule set that they are written to use.&amp;nbsp; Being explicit, rather than relying on defaults, future proofs your code by having it be very clear about the security model that it understands.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9920999" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /><category term="Transparency" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Transparency/default.aspx" /></entry><entry><title>Transparency as Enforcement in CLR v4</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/11/09/transparency-as-enforcement-in-clr-v4.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/11/09/transparency-as-enforcement-in-clr-v4.aspx</id><published>2009-11-09T20:23:35Z</published><updated>2009-11-09T20:23:35Z</updated><content type="html">&lt;p&gt;Now that we know &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/11/03/transparency-101-basic-transparency-rules.aspx"&gt;the basics of security transparency&lt;/a&gt;, let's look at how it evolved over time.&amp;nbsp; In .NET v2.0, &lt;a href="http://blogs.msdn.com/shawnfa/archive/2005/08/31/458641.aspx"&gt;many of the transparency rules we previously looked at were in place&lt;/a&gt;, with the exception of some of the &lt;a href="http://blogs.msdn.com/shawnfa/archive/2007/05/11/silverlight-security-iii-inheritance.aspx"&gt;inheritance rules that were introduced for the first time in the Silverlight transparency implementation&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;However, in .NET 2.0 transparency was used as a security audit mechanism rather than a security enforcement mechanism.&amp;nbsp; This means that:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;There is no cross-assembly transparency enforcement.  &lt;li&gt;Transparency violations were generally "soft" violations and enforced via a demand.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Because transparency was not enforced cross-assembly, there was no such thing as a public security critical method.&amp;nbsp; Instead, all publicly visible security critical methods were implicitly security safe critical.&amp;nbsp; If a publicly visible method wanted to have protection against transparent (and therefore partially trusted) callers, it needed to use a LinkDemand instead of relying on being critical.&lt;/p&gt; &lt;p&gt;The other fallout of the v2 transparency model being used only for security audit is that violations of the transparency rules were always allowed in full trust - after all, there's not any security risk to audit against in a fully trusted domain.&amp;nbsp;&amp;nbsp; This means that for most of the transparency rules, a violation resulted in a full demand for a full trust equivalent permission.&amp;nbsp;&amp;nbsp; (The one exception to this was the rule that transparent code may not assert - even in v2 this was a hard rule).&lt;/p&gt; &lt;p&gt;For example, if a transparent method tried to call native code in v2, the CLR would issue a full demand for SecurityPermission/UnmanagedCode.&amp;nbsp; This demand would succeed if the whole stack was fully trusted, indicating that even though the code doing the native invoke may not have been subjected to a security audit, it was OK to proceed since the current context doesn't involve partial trust.&lt;/p&gt; &lt;p&gt;However, if this same code was called from within an ASP.NET medium trust AppDomain, it would fail with a SecurityException.&amp;nbsp; In this scenario, the fact that the transparent code may not have been audited is more significant since partial trust is in play, so the operation was blocked.&lt;br&gt;As we moved toward using security transparency as the enforcement mechanism of Silverlight and the v4 desktop CLR, we needed to harden up these rules a bit.&amp;nbsp; The first step toward doing this was to make it possible to expose security critical methods publicly.&amp;nbsp; Therefore, on the v4 CLR public security critical methods are no longer implicitly security safe critical.&lt;/p&gt; &lt;p&gt;The fact that assemblies can now expose security critical APIs has an interesting side effect.&amp;nbsp; Since code must be security critical if it wants to call a security critical API, and it must also be security critical if it wants to call an API protected by a link demand, link demand functionality is redundant.&lt;br&gt;That is, having a link demand for a granular permission requires that the caller be security critical.&amp;nbsp; Since the v4 CLR requires that all security critical code be fully trusted only full trust code may call the link demand protected method.&amp;nbsp; That is, calling a security critical method also requires that the caller be fully trusted and security critical; therefore there is no difference on security requirements of calling methods between an API protected with a link demand and an API protected by being marked security critical.&lt;/p&gt; &lt;p&gt;This, in turn, lead us to deprecate the use of link demands to protect security sensitive APIs in favor of just leaving the APIs as security critical.&amp;nbsp; (In fact, one of the recommended steps when using the v4 transparency model is to remove link demands and replace them with security critical APIs instead).&lt;/p&gt; &lt;p&gt;The next step toward using security transparency as a full enforcement mechanism is to treat all transparency errors as hard violations, regardless of the trust context they occur in.&amp;nbsp; Rather than transparency violations being converted to full demands in the v4 transparency model, they are treated as strict violations - and will unconditionally result in an exception when they are encountered.&amp;nbsp; This also matches the way that Silverlight enforces its transparency model.&lt;/p&gt; &lt;p&gt;The exact exception that will be triggered, and the time that it is triggered, depends upon the transparency violation being encountered.&amp;nbsp; For example, a transparent method trying to call a critical method will result in a MethodAccessException the first time that the JIT sees the call.&amp;nbsp; However, a transparent method overriding a critical method will instead result in a TypeLoadException when the violating type is first loaded.&lt;/p&gt; &lt;p&gt;We already talked about the final piece of the transparency as an enforcement mechanism puzzle when we first looked at the transparency rules.&amp;nbsp; The v4 CLR treats all partially trusted code as if it were fully transparent - which means that full trust code can rely on the fact that partial trust code can never use any of its security critical components without having to provide any extra protection.&amp;nbsp; Instead, the full trust code can focus on exposing a safe set of security safe critical APIs and auditing those thoroughly.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9919750" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /><category term="Transparency" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Transparency/default.aspx" /></entry><entry><title>Bridging the Gap Between Transparent and Critical Code</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/11/05/bridging-the-gap-between-transparent-and-critical-code.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/11/05/bridging-the-gap-between-transparent-and-critical-code.aspx</id><published>2009-11-05T17:59:29Z</published><updated>2009-11-05T17:59:29Z</updated><content type="html">&lt;p&gt;Last time we looked at &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/11/03/transparency-101-basic-transparency-rules.aspx"&gt;the set of operations that can only be performed by security critical code&lt;/a&gt;. One interesting observation is that just because you are doing one of these operations does not mean that your method in and of itself is security sensitive. For instance, you might implement a method with unverifiable IL as a performance optimization - however that optimization is done in an inherently safe way. &lt;p&gt;Another example of a safe operation that uses security critical constructs is the Isolated Storage example from that post. Although Isolated Storage performs a security assert, which is security sensitive and requires it to be critical, it makes this assert safe by several techniques including issuing a demand for IsolatedStoragePermission. &lt;p&gt;Similarly, the file classes might use P/Invokes in order to implement their functionality. However, they ensure that this is safe by issuing a demand for FileIOPermission in order to ensure that they are only used in sandboxes which explicitly decided to allow access to the file system. &lt;p&gt;In these cases, you might want transparent code to be able to call into your method, since the fact that it is doing something critical is more of an implementation detail than anything else. These methods form the boundary between the security sensitive portions of your code and the security transparent portions, and are marked as Security Safe Critical. &lt;p&gt;A security safe critical method can do everything that a security critical method can do, however it does not require that its caller (or overriding methods) be security critical themselves. Instead, it takes on the responsibility of validating that all of its operations are safe. This includes (but is not limited to): &lt;ol&gt; &lt;li&gt;&lt;em&gt;Verifying that the core operations it is performing are safe&lt;/em&gt;. A method that formats the hard disk would never be security safe critical for instance.&lt;/li&gt; &lt;li&gt;&lt;em&gt;Verifying that the inputs that the method uses make sense&lt;/em&gt;. For example, Isolated Storage only allows access to paths within the Isolated Storage root and rejects attempts to use its APIs to open arbitrary files on the machine.&lt;/li&gt; &lt;li&gt;&lt;em&gt;Verifying that the outputs are also safe&lt;/em&gt;. This includes the obvious: return values, output and reference parameters. However, non-obvious results of operations are also included here. For example, exceptions thrown or even state transitions of objects need to be safe as well. If outside code can observe a change based upon using a safe critical method, then that safe critical method is responsible for ensuring that exposing this change is something safe to do.&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;With this in mind, another interesting observation can be made. Since the security safe critical layer of code is the bridge between transparent code and security critical code, it really forms the attack surface of a library. &lt;p&gt;This means that upon adopting the security transparency model, an APTCA library's audit burden really falls mostly upon the security safe critical surface of the library. If any of the safe critical code in the assembly is not correctly verifying operations, inputs, or outputs, then that safe critical method is a security hole that needs be closed. &lt;p&gt;Conversely, since transparent (and therefore, in .NET 4, partially trusted) code cannot directly call through to security critical code the audit burden on this code is significantly reduced. Similarly, since transparent code cannot be doing any security sensitive operations it also has a significantly reduced security audit burden. &lt;p&gt;By using security transparency, therefore, it becomes very easy to identify the sections of code that need to be focused on in security review in order to make sure that the shipping assembly is secure. &lt;p&gt;(Our internal security reviewers sometimes jokingly refer to the SecuritySafeCriticalAttribute as the BigRedFlagAttribute)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9918103" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /><category term="Transparency" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Transparency/default.aspx" /></entry><entry><title>Transparency 101: Basic Transparency Rules</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/11/03/transparency-101-basic-transparency-rules.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/11/03/transparency-101-basic-transparency-rules.aspx</id><published>2009-11-03T17:38:27Z</published><updated>2009-11-03T17:38:27Z</updated><content type="html">&lt;p&gt;One of the biggest changes in the .NET 4 security model is a move toward security transparency as a primary security enforcement mechanism of the platform. As you'll recall, we &lt;a href="http://blogs.msdn.com/shawnfa/archive/2005/08/31/458641.aspx"&gt;introduced security transparency in the v2 release of .NET&lt;/a&gt; as more of an audit mechanism in order to help make the surface area of APTCA libraries as safe as possible. In Silverlight, we evolved transparency into &lt;a href="http://blogs.msdn.com/shawnfa/archive/2007/05/14/silverlight-security-cheat-sheet.aspx"&gt;the security model&lt;/a&gt; that the entire managed platform was built on top of.&amp;nbsp; With .NET 4 we continue that evolution, making security transparency now the consistent way to enforce security both on Silverlight and on the desktop CLR.  &lt;p&gt;Before we dive deep into what all this means, let's take a quick refresher over the basic concepts of transparency.  &lt;p&gt;The fundamental idea of security transparency is to separate code which may potentially do dangerous or security sensitive things from code which is benign from a security perspective. The security sensitive code is called security critical, and the code which does not perform security sensitive operations is called security transparent.  &lt;p&gt;With that in mind, let's figure out what operations are security sensitive, and therefore require the code performing them to be security critical.  &lt;p&gt;Imagine for a minute that the CLR shipped exactly as-is, but without the ability to do two important operations:  &lt;ul&gt; &lt;li&gt;Call native code, either via COM Interop or P/Invoke.  &lt;li&gt;Execute unverifiable code&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Without either of these operations, all the possible code that could run on the CLR would be entirely safe - there's no possible thing that it could do that could be dangerous. On the flip side, there's also not very much interesting it could do (taking into account that the BCL is managed code, and would have to abide by these rules as well).  &lt;p&gt;For example, you could write a calculator application or an XML parser library with the operations available to you in verifiable IL, however the utility of that code would be severely limited by the fact that you could not receive any input from the user of your application (which would require either your app itself or the BCL interop with native code in order to read from a file or standard input); similarly you couldn't display the results of your calculations without talking to native code either.  &lt;p&gt;Obviously the CLR wouldn't be a very interesting platform for writing code on if these restrictions were in place, so we need to make them available. However, since they both allow taking full control of the process, we need to restrict them to trusted code only. Therefore, calling native code and having unverifiable code are our first set of operations that are security critical.  &lt;p&gt;(Note that &lt;i&gt;containing&lt;/i&gt; unverifiable code and &lt;i&gt;calling&lt;/i&gt; native code are the operations here - there's no inherent problem with calling an unverifiable method and the fact that a method contains unverifiable code does not in and of itself mean that it is dangerous to use).  &lt;p&gt;We've now determined that code needs to be security critical in order to work with native code or unverifiable code - easy enough; this gives us our first set of security critical methods. However, since these methods are performing security sensitive operations using them may also be a security sensitive operation. That leads us to our third transparency rule - you must be critical if you:  &lt;ul&gt; &lt;li value="3"&gt;Call critical code&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Some code, such as the File classes, are security sensitive but mitigate their security risk by demanding permission to use them. In the case of the File classes, if the sandbox they are running in is granted the appropriate FileIOPermission then they are safe to use; otherwise they are not.  &lt;p&gt;If trusted code wants to use the File classes in a sandbox that does not support them, it can assert away the file IO demands. For instance, IsolatedStorage does exactly this to allow access to a safe isolated storage file store in sandboxes that do not allow unrestricted access to the user's hard drive.  &lt;p&gt;By doing this, however, the trusted code has removed the mitigation that the original security critical code put in place - the permission demand - and asserted that the demand is not necessary anymore for some reason. (In the case of isolated storage because the file paths are well controlled, a quota is being enforced, and an IsolatedStoragePermission demand will be issued).  &lt;p&gt;Since permission asserts remove security checks, performing an assert is security sensitive.&amp;nbsp; This means we've now got the fourth operation which requires code to be security critical:  &lt;ul&gt; &lt;li&gt;Perform a security assert &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Some code which performs a security sensitive operation will protect itself with a LinkDemand, which rather than requiring that it only run in a specific sandbox instead says that the operation is viable in any sandbox - as long as the code executing the operation is trusted. For example, the Marshal class falls into this category.  &lt;p&gt;Marshaling data back and forth between native and managed code makes sense in every sandbox - it's a generally useful operation. However, you certainly don't want the sandboxed code using methods like ReadByte and WriteByte to start manipulating memory. Therefore, the Marshal class protects itself with a LinkDemand for a full trust equivalent permission.  &lt;p&gt;Since this LinkDemand is Marshal's way of calling out that any use of these methods are security sensitive, our fifth transparency rule is easily derived. Code must be security critical if it attempts to:  &lt;ul&gt; &lt;li&gt;Satisfy a link demand &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Security transparency and inheritance have an interesting interaction, which is sometimes rather subtle. However, understanding it will lead us to a few more operations that require code be security critical.  &lt;p&gt;Let's start with security critical types - when a type, such as SafeHandle, declares itself to be security critical it's saying that any use of that type is potentially security sensitive. This includes not only direct uses, such as creating instances and calling methods on the type, but also more subtle uses - such as deriving from the type. Therefore, a type must be security critical if it wants to:  &lt;ul&gt; &lt;li&gt;Derive from a non-transparent type or implement a non-transparent interface. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;If a base type has security critical virtual methods, it's interesting to think about what requirements we might want to place on overrides of those virtuals. At first glance there doesn't appear to be any security requirements for overriding these methods - after all, once you've overridden a method none of its code is going to execute, so the fact that it is security critical doesn't matter.  &lt;p&gt;However, from the perspective of the caller of the security critical virtual method, it is actually rather important that any override of a critical virtual remain security critical.  &lt;p&gt;To see why, let's take an example. X509Certificate provides an Import method which is security critical in the v4 release of the CLR. This method takes both the raw bytes of the certificate and the password necessary to gain access to the private key of that certificate.  &lt;p&gt;Since the code on the other end of the virtual function call is going to be receiving sensitive information, such as a password and a certificate that may have a private key, it is by definition security sensitive.&amp;nbsp; The code which calls the Import virtual is passing this sensitive information through the call under the assumption that the method which will ultimately execute is itself trustworthy.&amp;nbsp; Therefore, it methods are security critical if they:  &lt;ul&gt; &lt;li&gt;Override a security critical virtual or implement a security critical interface method &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;This is the final core transparency rule - the core set of things that are security sensitive and therefore require the code doing them to be security critical.  &lt;p&gt;It's interesting to note that this list of critical operations:  &lt;ol&gt; &lt;li&gt;Call native code &lt;/li&gt; &lt;li&gt;Contain unverifiable code &lt;/li&gt; &lt;li&gt;Call critical code &lt;/li&gt; &lt;li&gt;Perform security asserts &lt;/li&gt; &lt;li&gt;Satisfy link demands &lt;/li&gt; &lt;li&gt;Derive from non-transparent types &lt;/li&gt; &lt;li&gt;Override security critical virtuals &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Could also read as a list of operations that partial trust code cannot perform. In fact, in the v4 CLR we now force all partial trust code to be entirely transparent. Or, put another way, only full trust code can be security critical. This is very similar to the way that Silverlight requires that all user assemblies are entirely transparent, and only Silverlight platform assemblies can contain security critical code. This is one of the basic steps that allowed us to use security transparency as a security enforcement mechanism in Silverlight and the v4 desktop framework.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9916865" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /><category term="Transparency" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Transparency/default.aspx" /></entry><entry><title>CLR v4 Security Policy Roundup</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/06/12/clr-v4-security-policy-roundup.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/06/12/clr-v4-security-policy-roundup.aspx</id><published>2009-06-12T21:33:00Z</published><updated>2009-06-12T21:33:00Z</updated><content type="html">&lt;P&gt;Over the last few weeks we’ve been taking a look at the updates to the CLR security policy system in the v4 release of the .NET Framework.&amp;nbsp; Here’s a quick index of those topics:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Overview&lt;/LI&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;Security Policy in the v4 CLR&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx"&gt;Sandboxing in .NET 4.0&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;LI&gt;Updating code to work with the new model&lt;/LI&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx"&gt;Implicit uses of CAS policy part 1 (Assembly.Load with Evidence, AppDomain creation with Evidence)&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx"&gt;Implicit uses of CAS policy part 2 (loading assemblies from remote sources)&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/06/09/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/06/09/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx"&gt;Explicit uses of CAS policy (APIs such as SecurityManager which directly use CAS policy)&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;LI&gt;Temporarily re-enabling CAS policy during migration&lt;/LI&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/06/12/temporarily-re-enabling-cas-policy-during-migration.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/06/12/temporarily-re-enabling-cas-policy-during-migration.aspx"&gt;The NetFx40_LegacySecurityPolicy config file switch&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/UL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9737125" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="ClickOnce" scheme="http://blogs.msdn.com/shawnfa/archive/tags/ClickOnce/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /></entry><entry><title>Temporarily re-enabling CAS policy during migration</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/06/12/temporarily-re-enabling-cas-policy-during-migration.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/06/12/temporarily-re-enabling-cas-policy-during-migration.aspx</id><published>2009-06-12T21:27:13Z</published><updated>2009-06-12T21:27:13Z</updated><content type="html">&lt;p&gt;Over the last few weeks we’ve been looking at the &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;changes to security policy in .NET 4,&lt;/a&gt; namely that security policy is now in the hands of the host and the operating system.&lt;/p&gt;  &lt;p&gt;While we’ve looked at how to update code that &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx"&gt;implicitly uses CAS policy&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx"&gt;loads assemblies from remote sources&lt;/a&gt;, and &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/06/09/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx"&gt;explicitly uses CAS policy,&lt;/a&gt; in applications of larger size it may not be practical to update all the code at once.&amp;#160; Similarly, you might be able to update the code in your application, but may rely on a third party assembly that is not yet updated for the changes in CAS policy.&lt;/p&gt;  &lt;p&gt;If you do find yourself needing to re-enable CAS policy temporarily, in order to move a large code base to the new v4 security APIs bit by bit rather than all at once, or to use an assembly that you don’t control, there is a configuration switch that you can set in order to flip your process back into legacy CAS policy mode.&lt;/p&gt;  &lt;p&gt;In order to temporarily enable legacy CAS policy in your process, you’ll need an .exe.config file for your application with the legacy security policy switch set in its runtime section.&amp;#160; So, if your application’s entry point is YourApp.exe, you’ll have next to it a YourApp.exe.config file.&amp;#160; (You can also use the app.config feature in your Visual Studio project).&amp;#160; The file should look like this for any release of the .NET Framework v4 after beta 1:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;   &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;NetFx40_LegacySecurityPolicy&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;enabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;true&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In .NET 4 Beta 1, the switch has a slightly different name:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;legacyCasPolicy&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;enabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;true&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;One thing to note is that this switch must be set on the process-level.&amp;#160; So, if you’re using a third party control that uses CAS policy, you may well need to set the switch for both Visual Studio in devenv.exe.config and for your application itself.&amp;#160; That way the control will work both in the Visual Studio process during your development, as well as in your process at runtime.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9737038" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>Coding with Security Policy in .NET 4 part 2 – Explicit uses of CAS policy</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/06/09/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/06/09/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx</id><published>2009-06-09T22:14:31Z</published><updated>2009-06-09T22:14:31Z</updated><content type="html">&lt;p&gt;Over the last few posts, I’ve been looking at how the update to the CLR v4 &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;security policy&lt;/a&gt; interacts with how you write managed code against the v4 .NET Framework.&amp;#160; So far we’ve looked at the implicit uses of CAS policy, such as &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;loading assemblies and creating AppDomains with Evidence&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx"&gt;loading assemblies from remote sources&lt;/a&gt;.&amp;#160; Now let’s look at how to work with code which was written to work with CAS policy explicitly.&lt;/p&gt;  &lt;p&gt;The good news is that explicit use of CAS policy is frequently very easy to spot, as opposed to implicit uses which can be somewhat more subtle.&amp;#160; APIs that directly manipulate policy (such as SecurityManager.ResolvePolicy) as well as those that require CAS policy to sandbox (such as AppDomain.SetAppDomainPolicy) fall into this category.&amp;#160; Other APIs that explicitly use CAS policy are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;AppDomain.SetAppDomainPolicy &lt;/li&gt;    &lt;li&gt;HostSecurityManager.DomainPolicy &lt;/li&gt;    &lt;li&gt;PolicyLevel.CreateAppDomainLevel &lt;/li&gt;    &lt;li&gt;SecurityManager.LoadPolicyLevelFromString &lt;/li&gt;    &lt;li&gt;SecurityManager.LoadPolicyLevelFromFile &lt;/li&gt;    &lt;li&gt;SecurityManager.ResolvePolicy &lt;/li&gt;    &lt;li&gt;SecurityManager.ResolveSystemPolicy &lt;/li&gt;    &lt;li&gt;SecurityManager.ResolvePolicyGroups &lt;/li&gt;    &lt;li&gt;SecurityManager.PolicyHierarchy &lt;/li&gt;    &lt;li&gt;SecurityManager.SavePolicy &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;As with the implicit CAS policy uses, the explicit APIs also are obsolete in .NET 4, and will throw NotSupportedExceptions by default:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;System.NotSupportedException: This method uses CAS policy, which has been obsoleted by the .NET Framework. In order to enable CAS policy for compatibility reasons, please use the NetFx40_LegacySecurityPolicy configuration switch. Please see &lt;a href="http://go.microsoft.com/fwlink/?LinkId=131738"&gt;http://go.microsoft.com/fwlink/?LinkId=131738&lt;/a&gt; for more information.&lt;/div&gt;  &lt;p&gt;Let’s take a look at how code which used these APIs in the past might get updated with new v4 APIs.&lt;/p&gt;  &lt;p&gt;Generally, there are three reasons that the explicit policy APIs are being used:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The code wants to figure out what the grant set of an assembly or AppDomain is &lt;/li&gt;    &lt;li&gt;The code wants to create a sandbox &lt;/li&gt;    &lt;li&gt;The code wants to figure out what a safe sandbox is to setup &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The correct way way to update the code calling an explicit policy API in v4 depends upon what it was trying to do by calling the API in the first place.&amp;#160; Let’s take a look at each of the reasons for using an explicit policy API in turn and figure out what the replacement code should look like.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Figuring out what the grant set of an assembly or AppDomain is&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Sometimes an application or library wants to figure out what the grant set of a particular assembly or domain was and would do so with code similar to:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;   &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; GetAssemblyGrantSet(&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; assembly)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; assemblyEvidence = assembly.Evidence;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.ResolvePolicy(assemblyEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsFullyTrusted(&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; assembly)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; grant = GetAssemblyGrantSet(assembly);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; grant.IsUnrestricted();&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; GetAppDomainGrantSet(&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; domain)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; appDomain = domain.Evidence;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.ResolvePolicy(appDomain);&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsFullyTrusted(&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; domain)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; grant = GetAppDomainGrantSet(domain);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; grant.IsUnrestricted();&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This code worked by resolving the assembly or AppDomain’s evidence through CAS policy to determine what would be granted to that particular evidence.&amp;#160; There are a few problems here – for instance, the code doesn’t take into account simple sandbox domains, hosted AppDomains, dynamic assemblies, or assemblies loaded from byte arrays.&amp;#160; (Take a look at AssemblyExtensionMethods.GetPermissionSet() on &lt;a href="http://clrsecurity.codeplex.com"&gt;http://clrsecurity.codeplex.com&lt;/a&gt; for code that does take most of the other considerations into account).&amp;#160;&amp;#160; These methods also cause a full CAS policy resolution to occur, which is not a cheap operation.&amp;#160; &lt;/p&gt;

&lt;p&gt;Instead of requiring people to manually jump through hoops in order to recreate the CLR’s security policy system in v4, we’ve directly exposed the grant sets of assemblies and AppDomains as properties of the objects themselves.&amp;#160; The above code can be replaced with:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; GetAssemblyGrantSet(&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; assembly)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; assembly.PermissionSet;&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsFullyTrusted(&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; assembly)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; assembly.IsFullyTrusted;&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; GetAppDomainGrantSet(&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; domain)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; domain.PermissionSet;&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsFullyTrusted(&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; domain)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; domain.IsFullyTrusted;&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Which has the dual benefit of being more accurate (these properties read the real grant set that the CLR is using, no matter how it was determined), and also being faster than a full policy resolution.&lt;/p&gt;

&lt;p&gt;Accessing the PermissionSet property of an AppDomain or an Assembly does require that the accessing code be fully trusted.&amp;#160; The reason is that the permission sets themselves can contain sensitive data.&amp;#160; (For instance, FileIOPermission can contain full path information about the local machine in it).&amp;#160;&amp;#160; Partial trust code, however, can use the IsFullyTrusted property.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a Sandbox&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I suspect many people who have read this blog already know what I’m going to say here :-)&amp;#160; Instead of using SetAppDomainPolicy to create a sandbox, which suffers from many problems, the replacement API is the &lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx"&gt;simple sandboxing API&lt;/a&gt;.&amp;#160; I’ve already covered most of the reasoning for this change when &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx"&gt;I talked about sandboxing in CLR v4&lt;/a&gt;, so let’s look at the final reason that code may have been using CAS policy APIs&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figuring out what a safe grant set is to provide a sandbox&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes a host needs to figure out what is a reasonable set of permissions to assign to a sandbox.&amp;#160; For instance, even though ClickOnce does not use CAS policy, it still needs to figure out if the permission set that the ClickOnce application is requesting is a reasonable set of permissions for it to have.&amp;#160;&amp;#160; (For instance, if it’s requesting only the permission to execute, that’s going to be fine, while if an application from the Internet is requesting permission to read and write all of the files on your disk, that’s not such a good idea).&lt;/p&gt;

&lt;p&gt;In order to solve this problem in v2, code might look like this:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsSafeGrantSet(&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; grantSet, &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; sandboxEvidence)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// Figure out what the CLR's policy system says is safe to give a sandbox&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// with this evidence&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; systemGrantSet = &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.ResolveSystemPolicy(sandboxEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// We'll consider this safe only if we're requesting a subset of the safe&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// sandbox set.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; grantSet.IsSubsetOf(systemGrantSet);&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Since system wide CAS policy (which this code depends upon to determine safety) is deprecated in v4, we need to find a new way to accomplish this goal.&lt;/p&gt;

&lt;p&gt;The answer is with a new API called GetStandardSandbox.&amp;#160;&amp;#160; GetStandardSandbox is used to have the CLR provide what it considers a safe sandbox grant set for an AppDomain that will host code with the specified evidence.&amp;#160; It’s the CLR’s way of providing suggestions to hosts who are making trust decisions.&amp;#160;&amp;#160; One thing that is very important to note is what GetStandardSandbox is not however.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;u&gt;GetStandardSandbox is not a policy API&lt;/u&gt;&lt;/em&gt;.&amp;#160; This isn’t the CLR applying CAS to evidence in order to modify grant set, and the CLR does not take any external factors such as CAS policy into account when returning its grant set.&amp;#160; Instead, GetStandardSandbox is simply a helper API for hosts which are trying to setup sandboxes.&lt;/p&gt;

&lt;p&gt;With that in mind, the way the above code would be written in CLR v4 is:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsSafeGrantSet(&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; grantSet, &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; sandboxEvidence)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// Figure out what the CLR considers a safe grant set&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; clrSandbox = &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.GetStandardSandbox(sandboxEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// We'll consider this safe only if we're requesting a subset of the safe&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// sandbox set.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; grantSet.IsSubsetOf(clrSandbox);&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Similarly, if you are a host trying to setup an AppDomain to sandbox assemblies that are coming from the Internet, you might do so this way:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Find a safe sandbox set to give to assemblies downloaded&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// from the internet&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; internetEvidence = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;internetEvidence.AddHostEvidence(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Zone&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SecurityZone&lt;/span&gt;.Internet));&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; clrSandbox = &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.GetStandardSandbox(internetEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Create a sandboxed AppDomain to hold them&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt; sandboxSetup = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;sandboxSetup.ApplicationBase = DownloadDirectory;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; sandbox = &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CreateDomain(&lt;span style="color: #a31515"&gt;&amp;quot;Internet sandbox&amp;quot;&lt;/span&gt;,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; internetEvidence,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sandboxSetup,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; clrSandbox);&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9717560" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>More Implicit Uses of CAS Policy: loadFromRemoteSources</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx</id><published>2009-06-08T21:59:07Z</published><updated>2009-06-08T21:59:07Z</updated><content type="html">&lt;p&gt;In my last post about changes to the CLR v4 security policy model, &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx"&gt;I looked at APIs which implicitly use CAS policy in their operation&lt;/a&gt; (such as Assembly.Load overloads that take an Evidence parameter), and how to migrate code that was using those APIs.&amp;#160;&amp;#160; There are another set of assembly loads which cause implicit use of CAS policy, which I’ll look at today – these are loads from remote sources.&lt;/p&gt;  &lt;p&gt;For example, in .NET 3.5 the following code:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;   &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; internetAssembly = &lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt;.LoadFrom(&lt;span style="color: #a31515"&gt;@&amp;quot;http://www.microsoft.com/assembly.dll&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; intranetAssembly = &lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt;.LoadFrom(&lt;span style="color: #a31515"&gt;@&amp;quot;\\server\share\assembly.dll&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Will by default load internetAssembly with the Internet permission set and intranetAssembly with the LocalIntranet permission set.&amp;#160;&amp;#160; That was because the CLR would internally gather evidence for both assemblies and run that evidence though CAS policy in order to find the permission set to grant that assembly. &lt;/p&gt;

&lt;p&gt;Now that &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx"&gt;the sandboxing model has changed in the v4 CLR&lt;/a&gt;, there is no more CAS policy to apply the assembly’s evidence to by default, and&amp;#160; therefore default behavior of both of these loads would be to load the assemblies with a grant set of full trust.&lt;/p&gt;

&lt;p&gt;That creates a problem for code which was written before .NET 4 shipped – this code may quite reasonably be expecting that the above assembly loads are safe because the CLR will automatically apply a restricted grant set to the assemblies if they are coming from a remote location.&amp;#160;&amp;#160; Now when the code runs in the v4 CLR, the assemblies are elevated to full trust, which amounts to a silent elevation of privilege bug against the .NET 2.0 code which was expecting that these assemblies be sandboxed.&amp;#160; Obviously that’s not a good thing.&lt;/p&gt;

&lt;p&gt;Instead of silently granting these assemblies full trust, the v4 CLR will actually take the opposite approach.&amp;#160; We’ll detect that these assemblies are being loaded in such a way that&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;They would have been sandboxed by the v2 CLR &lt;em&gt;and&lt;/em&gt; &lt;/li&gt;

  &lt;li&gt;Are going to be given full trust by the v4 CLR &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once we detect an assembly load where both of the above conditions are true, the CLR will refuse to load the assembly with the following message:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;p&gt;System.IO.FileLoadException: Could not load file or assembly '&lt;em&gt;&amp;lt;assemblyPath&amp;gt;&lt;/em&gt;' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515 (COR_E_NOTSUPPORTED)) ---&amp;gt;&lt;/p&gt;

  &lt;p&gt;System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=131738 for more information.&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;This exception is saying “The v4 CLR is not going to sandbox the assembly that you’re trying to load, however the v2 CLR would have.&amp;#160; We don’t know if that’s safe in your application or not, so we’re going to fail the assembly load to ensure that your application is secure by default.&amp;#160; However, if this is a safe assembly load, go ahead and enable loading from remote sources for this process.”&lt;/p&gt;

&lt;p&gt;That leads to the next question -- how do you know if it is safe to enable loadFromRemoteSources in your application?&amp;#160; This decision generally comes down to applying these tests:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Do you trust the string that you’re passing to Assembly.LoadFrom? &lt;/li&gt;

  &lt;li&gt;Do you trust the assembly that you’re loading? &lt;/li&gt;

  &lt;li&gt;Do you trust the server hosting the assembly (and the network path from the server back to your application)? &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you answered yes to all three questions then your application is a good candidate for enabling the loadFromRemoteSources switch.&amp;#160; If you answered no to any of the three questions, then you may need to take further action before enabling the switch and loading the assembly.&amp;#160;&amp;#160; (For instance, you may have some application logic to ensure that the string being passed to LoadFrom is going to a server you trust, or your application might download the assembly first and verify it has an Authenticode signature that it trusts).&lt;/p&gt;

&lt;p&gt;Let’s look at some examples:&lt;/p&gt;

&lt;p&gt;The most straight-forward reason that you would want to enable this is in the case that you know what the assemblies you are loading are, you trust them, and you trust the server that they are hosted on.&amp;#160; For example, if your application is hosted on a share on your company’s intranet, and happens to need to load other assemblies from other shares on the network, you probably want to enable the switch.&amp;#160;&amp;#160; (In many cases, this category of applications used to have to fight with CAS policy to get things loaded the way they wanted, now with loadFromRemoteSources set things should just work.)&lt;/p&gt;

&lt;p&gt;On the other hand, if you are an application that takes as untrusted input a string which then is passed through to Assembly.LoadFrom, you probably don’t want to enable this switch, as you might be opening yourself up to an elevation of privilege attack via that untrusted input.&lt;/p&gt;

&lt;p&gt;Similarly, if your application takes as input an assembly name to LoadFrom, however you trust that input.&amp;#160; (Maybe it comes directly from your application’s user, and there is no trust boundary between the user and your app – for instance, the user is pointing you at a plugin they trust and wish to load in the app), you may also want to enable this switch.&lt;/p&gt;

&lt;p&gt;Another consideration to take into account when considering loadFromRemoteSources is that this is a process-wide configuration switch.&amp;#160; This means that it applies to all places in your code which loads assemblies, not just a single LoadFrom call.&amp;#160; If you only trust the inputs to some of your assembly loads, then you may wish to consider not using the loadFromRemoteSources switch and instead take a different approach.&lt;/p&gt;

&lt;p&gt;Since the first condition for the NotSupportedException that blocks remote assembly loads is that the load would have been sandboxed by the v2 CLR, one alternate way to enable these loads without setting loadFromRemoteSources for the entire process is to load the assemblies into a domain that you create &lt;a href="http://blogs.msdn.com/shawnfa/archive/2005/08/08/449050.aspx"&gt;with the simple sandboxing API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This will work because even in v2.0, simple sandbox domains never apply CAS policy, and therefore any remote loads in simple sandbox domains would not have required CAS policy to sandbox them.&amp;#160; Since the assemblies would not have used CAS policy in v2, the loads are considered safe to use in v4 as well, and will succeed without the NotSupportedException being thrown. &lt;/p&gt;

&lt;p&gt;For example, if you want to enable only a subset of LoadFroms to load assemblies in full trust, if you create a fully trusted simple sandbox, then any assemblies loaded into that sandbox would have the same full trust grant set in v2 as in v4.&amp;#160;&amp;#160; (The full trust grant set of the domain applies to all assemblies loaded into it).&amp;#160;&amp;#160; This will cause the CLR to allow the loads to proceed in full trust in v4 without having to throw the switch.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Since this application only trusts a handful of LoadFrom operations,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// we'll put them all into the same AppDomain which is a simple sandbox&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// with a full trust grant set.&amp;#160; The application itself will not enable&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// loadFromRemoteSources, but instead channel all of the trusted loads&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// into this domain.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; trustedLoadFromRemoteSourceGrantSet&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;PermissionState&lt;/span&gt;.Unrestricted);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt; trustedLoadFromRemoteSourcesSetup = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;trustedLoadFromRemoteSourcesSetup.ApplicationBase =&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CurrentDomain.SetupInformation.ApplicationBase;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; trustedRemoteLoadDomain =&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CreateDomain(&lt;span style="color: #a31515"&gt;&amp;quot;Trusted LoadFromRemoteSources Domain&amp;quot;&lt;/span&gt;,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;null&lt;/span&gt;,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; trustedLoadFromRemoteSourcesSetup,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; trustedLoadFromRemoteSourcesGrantSet);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Now all trusted remote LoadFroms can be done in the trustedRemoteLoadDomain,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// and communicated with via a MarshalByRefObject.&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;As an example in the opposite direction, maybe your application has mostly loads which are safe to have remote targets, however there are a small handful of places that do need to be sandboxed.&amp;#160; By creating a simple sandboxed AppDomain for those loads, you can then safely set the loadFromRemoteSources switch for the rest of your process.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Since this application trusts almost all of its assembly loads, it&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// is going to enable the process-wide loadFromRemoteSources switch.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// However, the loads that it does not trust still need to be sandboxed.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// First figure out a grant set that the CLR considers safe to apply&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// to code from the Internet.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; sandboxEvidence = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;sandboxEvidence.AddHostEvidence(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Zone&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SecurityZone&lt;/span&gt;.Internet));&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; remoteLoadGrantSet = &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.GetStandardSandbox(sandboxEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt; remoteLoadSetup = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;trustedLoadFromRemoteSourcesSetup.ApplicationBase = GetSandboxRoot();&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; remoteLoadSandbox =&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CreateDomain(&lt;span style="color: #a31515"&gt;&amp;quot;Remote Load Sandbox&amp;quot;&lt;/span&gt;,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sandboxEvidence,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; remoteLoadSetup,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; remoteLoadGrantSet);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Now all trusted remote LoadFroms can be done in the default domain&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// with loadFromRemoteSources set, and untrusted loads can be done&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// in the sandbox that we just setup.&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;(Similarly, if the process is in legacy CAS policy mode, the v4 CLR will have the same behavior as the v2 CLR, and there will be no exception).&lt;/p&gt;

&lt;p&gt;Let’s say that you’ve considered the security implications and your application is a good candidate to enable loadFromRemoteSources, how do you go about doing so?&amp;#160;&amp;#160; Basically, you just provide a .exe.config file for your application with a loadFromRemoteSources runtime switch enabled.&amp;#160;&amp;#160; So, if your application’s entry point is YourApp.exe, you’ll want to make a YourApp.exe.config.&amp;#160;&amp;#160; (Or use the app.config file in your Visual Studio project).&amp;#160;&amp;#160; This configuration file will need to contain runtime section such as:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;loadFromRemoteSources&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;enabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;true&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This setting will cause the CLR to notice that even though it is going to load an assembly that would have been sandboxed in the v2 runtime, your application has explicitly stated that this is a safe thing to do.&amp;#160;&amp;#160; Since your application has said that it understands the security impact of loading from remote locations and it is safe in the context of this application, the CLR will then allow these loads to succeed without throwing a NotSupportedException to block them.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9709114" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>CLR 4 Security on Channel 9</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/05/28/clr-4-security-on-channel-9.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/05/28/clr-4-security-on-channel-9.aspx</id><published>2009-05-28T23:30:00Z</published><updated>2009-05-28T23:30:00Z</updated><content type="html">&lt;p&gt;A while back I did an interview with &lt;a href="http://channel9.msdn.com/Niners/Charles/"&gt;Charles Torre&lt;/a&gt;&amp;#160; about the changes to security in CLR v4, and he posted it to the Channel 9 videos site yesterday.&lt;/p&gt;  &lt;p&gt;I start out talking about the security policy changes I've been covering here over the last week, and then transition into an overview of some of the transparency changes that I'll be talking about once I finish with the policy changes.&lt;/p&gt; &lt;object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="320" height="240"&gt; &lt;param name="source" value="http://channel9.msdn.com/App_Themes/default/vp09_04_23.xap" /&gt; &lt;param name="initParams" value="m=mms://mschnlnine.wmod.llnwd.net/a1809/d1/ch9/6/7/9/8/6/4/CLR4SecurityModel_s_ch9.wmv,autostart=false,autohide=true,showembed=true, thumbnail=http://mschnlnine.vo.llnwd.net/d1/ch9/6/7/9/8/6/4/CLR4SecurityModel_large_ch9.png, postid=468976" /&gt; &lt;param name="background" value="#00FFFFFF" /&gt; &lt;a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"&gt; &lt;img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none" /&gt; &lt;/a&gt; &lt;/object&gt;  &lt;p&gt;(The full video is also available here: &lt;a href="http://channel9.msdn.com/posts/Charles/Shawn-Farkas-CLR-4-Inside-the-new-Managed-Security-Model/" mce_href="http://channel9.msdn.com/posts/Charles/Shawn-Farkas-CLR-4-Inside-the-new-Managed-Security-Model/"&gt;http://channel9.msdn.com/posts/Charles/Shawn-Farkas-CLR-4-Inside-the-new-Managed-Security-Model/&lt;/a&gt;)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9648833" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /><category term="Transparency" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Transparency/default.aspx" /></entry><entry><title>Visual Studio 10 Security Tab Changes</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/05/28/visual-studio-10-security-tab-changes.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/05/28/visual-studio-10-security-tab-changes.aspx</id><published>2009-05-28T17:00:00Z</published><updated>2009-05-28T17:00:00Z</updated><content type="html">&lt;p&gt;Kris Makey, who works on the Visual Studio team, has written up a good blog post about the &lt;a href="http://blogs.msdn.com/krimakey/archive/2009/05/20/where-did-my-permission-set-controls-go.aspx"&gt;changes you’ll see on the security tab in Visual Studio 10 when it comes to editing permission sets&lt;/a&gt;.&amp;#160; He covers what the changes are, and some of the reasons why we worked with the Visual Studio team to make those changes.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9644742" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Visual Studio" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Visual+Studio/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>Coding with Security Policy in .NET 4.0 – Implicit uses of CAS policy</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx</id><published>2009-05-27T20:46:59Z</published><updated>2009-05-27T20:46:59Z</updated><content type="html">&lt;p&gt;Last week we looked at &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx"&gt;sandboxing and the v4 CLR&lt;/a&gt; – with the key change being that the CLR now defers exclusively to the host application when setting up sandboxed domains by moving away from the old CAS policy model, and moving instead to simple sandboxed AppDomains.&lt;/p&gt;  &lt;p&gt;This leads to an interesting situation when your program calls APIs that assume the presence of CAS policy, either implicitly [for example, &lt;a href="http://msdn.microsoft.com/en-us/library/0wcskf6d.aspx"&gt;Assembly.Load(string, Evidence)]&lt;/a&gt; or explicitly [for example &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.securitymanager.policyhierarchy.aspx"&gt;SecurityManager.PolicyHierarchy&lt;/a&gt;].&amp;#160;&amp;#160; These APIs require CAS policy in order to return correct results, however by default there is no longer CAS policy to apply behind the scenes anymore.&lt;/p&gt;  &lt;p&gt;Let’s take a look at what happens if these APIs are called, and what should be done to update your code to take into account the new security policy model.&lt;/p&gt;  &lt;p&gt;(In addition to this blog post, the CLR security test team is preparing a set of blog posts about how they moved our test code base forward to deal with these and other v4 security changes – those posts will provide additional advice about how to replace uses of obsolete APIs based upon the real world examples they’ve seen).&lt;/p&gt;  &lt;p&gt;In general, APIs that assume the presence of CAS policy have been marked obsolete, and will give a compiler warning when you build against them:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;Microsoft (R) Visual C# 2010 Compiler version 4.0.20506    &lt;br /&gt;Copyright (C) Microsoft Corporation. All rights reserved.     &lt;br /&gt;    &lt;br /&gt;obsolete.cs(32,1): warning CS0618: '&amp;lt;&lt;i&gt;API Name&amp;gt;&lt;/i&gt;' is     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; obsolete: 'This method is obsolete and will be removed in a future     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; release of the .NET Framework. Please use &lt;i&gt;&amp;lt;suggested alternate API&amp;gt;&lt;/i&gt;. See     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;a href="http://go.microsoft.com/fwlink/?LinkId=131738"&gt;http://go.microsoft.com/fwlink/?LinkId=131738&lt;/a&gt; for more information.'     &lt;br /&gt;&lt;/div&gt;  &lt;p&gt;Additionally, these APIs will throw a NotSupportedException if they are called at runtime:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;Unhandled Exception: System.NotSupportedException: This method uses CAS policy, which has been obsoleted by the .NET Framework. In order to enable CAS policy for compatibility reasons, please use the NetFx40_LegacySecurityPolicy configuration switch. Please see &lt;a href="http://go.microsoft.com/fwlink/?LinkId=131738"&gt;http://go.microsoft.com/fwlink/?LinkId=131738&lt;/a&gt; for more information.&lt;/div&gt;  &lt;p&gt;(In the beta 1 release, this message is slightly different:)&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;Unhandled Exception: System.NotSupportedException: This method uses CAS policy, which has been obsoleted by the .NET Framework. In order to enable CAS policy for compatibility reasons, please use the legacyCasPolicy configuration switch. Please see &lt;a href="http://go.microsoft.com/fwlink/?LinkId=131738"&gt;http://go2.microsoft.com/fwlink/?LinkId=131738&lt;/a&gt; for more information.&lt;/div&gt;  &lt;p&gt;Let’s take a look at the set of APIs which make implicit use of CAS policy first, and then see what they might be replaced with in a v4.0 application.&lt;/p&gt;  &lt;p&gt;The general way to recognize an API which is implicitly using CAS policy is that they tend to take an Evidence parameter which was used to resolve against CAS policy and provide a grant set for an assembly.&amp;#160; For instance:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Activator.CreateInstance and Activator.CreateInstanceFrom overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;AppDomain.CreateInstance, AppDomain.CreateInstanceFrom, AppDomain.CreateInstanceAndUnwrap, and AppDomain.CreateInstanceAndUnwrap overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;AppDomain.DefineDynamicAssembly overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;AppDomain.ExecuteAssembly and AppDomain.ExecuteAssemblyByName overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;AppDomain.Load and AppDomain.LoadFrom overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;Assembly.Load and Assembly.LoadFrom overloads which take an Evidence parameter &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;It’s important to note that although these APIs all take Evidence parameters, the concept of Evidence itself is not deprecated and continues to exist (and even enhanced in v4.0 – but that’s another show).&amp;#160; Evidence itself is still a useful tool for hosts to use when figuring out what grant sets they want to give assemblies.&amp;#160; The common thread with these APIs is that they used the Evidence to resolve against CAS policy – and it’s the CAS policy portion that’s been deprecated in v4.&lt;/p&gt;  &lt;p&gt;Let’s say that your application is using one of the Evidence-taking overloads of these APIs, and thus had an implicit dependency on CAS policy.&amp;#160; Figuring out what to replace the API call with depends upon what your application was trying to accomplish with the API call.&lt;/p&gt;  &lt;p&gt;We’ve found that commonly the goal of calling one of these APIs was not to sandbox the assembly being loaded, but rather to access other parameters on the overload which may not be available without also providing Evidence.&amp;#160; In these cases, you can go ahead and just drop the Evidence parameter from the API.&amp;#160; We’ve ensured that all of the above APIs now have overloads that provide the full set of parameters without requiring an Evidence parameter.&lt;/p&gt;  &lt;p&gt;Additionally, in many cases we’ve found that code passes in Assembly.GetExecutingAssembly().Evidence or simply null to the Evidence parameter.&amp;#160; In both of those cases, it’s safe to simply call an overload of the API which does not require an Evidence parameter as well.&lt;/p&gt;  &lt;p&gt;The other reason to provide Evidence when calling these APIs is to sandbox the assembly in question.&amp;#160; The correct way to do this in v4 (and the best way to do this in v2.0 and higher of the .NET Framework) is to simply load the assembly &lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx"&gt;into a simple sandboxed AppDomain&lt;/a&gt;.&amp;#160; The assembly will then be sandboxed by virtue of the fact that it’s loaded in the sandboxed domain, and you will no longer need to load the assembly with an Evidence parameter to restrict its grant set.&lt;/p&gt;  &lt;p&gt;I’ve listed the benefits of using simple sandboxed domains before, and they continue to apply in this scenario.&amp;#160; For example, using a simple sandbox rather than an Evidence resolve to sandbox assemblies allows your application:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;To be in charge of its own sandbox.&amp;#160; The load-with-Evidence route took a dependency on what the grant set that the CLR would give the assembly was.&amp;#160; That grant set could change from version to version of the CLR (&lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/07/11/661769.aspx"&gt;since each version has independent CAS policies&lt;/a&gt;), and even from user to user.&amp;#160; This makes supporting your application more difficult than it needs to be – with simple sandboxing there are no external dependencies for grant set resolution – your application is in charge of its own sandboxes &lt;/li&gt;    &lt;li&gt;To setup real isolation boundaries – hosting multiple levels of partial trust code within a single AppDomain turns out to be incredibly difficult to do correctly.&amp;#160; Further, hosting partial trust code in a domain wtih full trust code that does not expect to be run along with partial trust code also turns out to be problematic from a security perspective.&amp;#160; By isolating the partial trust code in its own sandboxed domain, a real isolation boundary is setup for the code and your application is kept much more secure by default. &lt;/li&gt;    &lt;li&gt;To have version and bitness independence – I touched on this in the first point, but to reiterate it, your application is no longer dependent upon each version of the CLR’s security policy to be setup in the same way, as well as each bitness of the policy within a single version. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;So, to summarize, if you’re using one of the Evidence taking APIs which would have resolved an assembly’s grant set against CAS policy in the past:&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top"&gt;&lt;b&gt;Use&lt;/b&gt;&lt;/td&gt;        &lt;td valign="top"&gt;&lt;b&gt;Replacement&lt;/b&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Passing null, Assembly.GetExecutingAssembly().Evidence, or AppDomain.CurrentDomain.Evidence&lt;/td&gt;        &lt;td valign="top"&gt;Call an overload which does not require an Evidence parameter.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Using a parameter of the API which was only available on an overload taking an Evidence parameter as well.&lt;/td&gt;        &lt;td valign="top"&gt;Call one of the newly added overloads which provides access to your parameter without requiring Evidence.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Sandboxing the assembly being loaded.&lt;/td&gt;        &lt;td valign="top"&gt;Load the assembly into a sandboxed AppDomain, and let the domain do the sandboxing.&amp;#160; This will remove the need for the Evidence parameter.&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Next time, I’ll look at the explicit uses of CAS policy, and what their replacements should be.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9644730" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>Sandboxing in .NET 4.0</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx</id><published>2009-05-22T20:54:00Z</published><updated>2009-05-22T20:54:00Z</updated><content type="html">&lt;P&gt;Yesterday I talked about the &lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;changes in security policy for managed applications&lt;/A&gt;, namely that managed applications will run with full trust - the same as native applications - when you execute them directly.&lt;/P&gt;
&lt;P&gt;That change doesn’t mean that managed code can no longer be sandboxed however - far from it.&amp;nbsp; Hosts such as ASP.NET and ClickOnce continue to use the CLR to sandbox untrusted code.&amp;nbsp; Additionally, any application can continue to create AppDomains to sandbox code in.&lt;/P&gt;
&lt;P&gt;As part of our overhaul of security policy in v4, we made some interesting changes to how that sandboxing should be accomplished as well.&amp;nbsp; In previous releases, the CLR provided a variety of ways to sandbox code – but many of them were problematic to use correctly.&amp;nbsp; In the v4 framework, we made it a goal to simplify and standardize how sandboxing should be done in managed code.&lt;/P&gt;
&lt;P&gt;One of the key observations we made about sandboxing is that there really isn’t a good reason for the CLR to be involved in the decision as to what grant set should be given to partial trust code.&amp;nbsp;&amp;nbsp; If your application says “I want to run this code with ReflectionPermission/RestrictedMemberAccess and SecurityPermission/Execution”, that’s exactly the set of permissions that the code should run with.&amp;nbsp;&amp;nbsp; After all, your application knows much better than the CLR what operations the sandboxed code can be safely allowed to undertake.&lt;/P&gt;
&lt;P&gt;The problem is, sandboxing by providing an AppDomain policy level doesn’t provide total control to the application doing the sandboxing.&amp;nbsp; For instance, imagine you wanted to provide the sandbox grant set of RestrictedMemberAccess + Execution permission.&amp;nbsp; You might setup a policy level that grants AllCode this grant set and assign it to the AppDomain.&amp;nbsp;&amp;nbsp; However, if the code you place in that AppDomain has evidence that says it came from the Internet, the CLR will instead produce a grant set that doesn’t include RestrictedMemberAccess for the sandbox.&amp;nbsp; Rather than allowing safe partial trust reflection as you wanted, your sandbox just became execute-only.&lt;/P&gt;
&lt;P&gt;This really doesn’t make sense – what right does the CLR have to tell your application what should and should not be allowed in its sandboxes?&amp;nbsp; In the v1.x release of the runtime, developers had to go to great lengths in order to ensure they got the grant set they wanted.&amp;nbsp; (&lt;A href="http://blogs.msdn.com/ericlippert/" mce_href="http://blogs.msdn.com/ericlippert/"&gt;Eric Lippert’s&lt;/A&gt; CAS policy acrobatics to get VSTO working correctly is the stuff of legends around the security team – fabulous adventures in coding indeed!).&lt;/P&gt;
&lt;P&gt;As many a frustrated application developer found out, intersecting with the application supplied grant set was only the tip of the iceburg when it came to the difficulties of coding with CAS policy.&amp;nbsp; You would also run into a slew of other problems – such as each version of the CLR having an entirely independent security policy to deal with.&lt;/P&gt;
&lt;P&gt;In v2.0, we introduced the &lt;A href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx"&gt;simple sandboxing API&lt;/A&gt; as a way for applications to say “This is the grant set I want my application to have.&amp;nbsp; Please don’t mess with it.”.&amp;nbsp; This went a long way toward making writing an application which sandboxes code an easier task.&lt;/P&gt;
&lt;P&gt;Beginning with v4.0, the CLR is getting out of the policy business completely.&amp;nbsp; By default, the CLR is not going to supply a CAS policy level that interferes with the wishes of the application that is trying to do sandboxing.&lt;/P&gt;
&lt;P&gt;Effectively, we’ve simplified the multiple ways that you could have sandboxed code in v3.5 into one easy option – all sandboxes in v4 must be setup with the simple sandboxing API.&lt;/P&gt;
&lt;P&gt;This means that the days of wrangling with complicated policy trees with arbitrary decision nodes in them are thankfully a thing of the past.&amp;nbsp; All that’s needed from here on out is a simple statement: “here is my sandboxed grant set, and here are the assemblies I wish to trust.”&lt;/P&gt;
&lt;P&gt;Next time, I’ll look at the implications of this on code that interacts with policy, looking at what you used to write, and what it would be replaced with in v4.0 of the CLR.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9635605" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry></feed>