<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>.NET Security Blog</title><link>http://blogs.msdn.com/b/shawnfa/</link><description /><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>Declarative Security and Reflection</title><link>http://blogs.msdn.com/b/shawnfa/archive/2010/04/21/declarative-security-and-reflection.aspx</link><pubDate>Wed, 21 Apr 2010 21:15:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10000306</guid><dc:creator>shawnfa</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/shawnfa/rsscomments.aspx?WeblogPostID=10000306</wfw:commentRss><comments>http://blogs.msdn.com/b/shawnfa/archive/2010/04/21/declarative-security-and-reflection.aspx#comments</comments><description>&lt;p&gt;If you’re using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.reflection.customattributedata.getcustomattributes.aspx"&gt;CustomAttributeData APIs&lt;/a&gt; to examine declarative security permission, you might notice that the returned information looks a little strange.&amp;#160; The CustomAttributeData object that you’ll see for the declarative security attribute will have a ConstructorInfo that is of the correct type, but may not be the same constructor that was actually used in the source code.&amp;#160; Similarly, the ConstructorArguments and NamedArguments property will say that the attribute has neither, even though if you look at your source code you may see one or both of these types of arguments.&lt;/p&gt;  &lt;p&gt;So, what’s going on?&lt;/p&gt;  &lt;p&gt;This behavior is a side-effect of the fact that security permission attributes aren’t “real” custom attributes in the sense that they’re not stored in the CustomAttribute metadata table, but rather in their own table – the DeclSecurity table.&amp;#160; Reflection tries to abstract this away from the callers of the CustomAttributeData APIs by including both real custom attributes and pseudo-custom attributes like declarative security in its return value.&amp;#160; However, it cannot abstract this detail away perfectly, which is why the constructor information that it normally provides for custom attributes is not provided for declarative security.&lt;/p&gt;  &lt;p&gt;Specifically, the metadata format for declarative security blobs is structured in such a way that it is easy for the security system to quickly and lazily determine if a permission blob is needed during execution, and if so very quickly rehydrate the permission in question.&amp;#160; For instance, the format of the declarative security table allows us to avoid doing any work at all when a method has a declarative assert until the point that a demand actually hits the stack frame with that assert.&amp;#160; This optimization means that in full trust, you will never have to waste time building up permission objects that might never be used.&amp;#160; Similarly, the blog format is optimized to make it quick for us to build up that asserted permission set once a demand does hit the method in question – meaning that we don’t slow down stack walks any more than necessary.&lt;/p&gt;  &lt;p&gt;Since the metadata format is optimized for this scenario, it’s not setup in such a way that you can trivially map it back to the exact constructor that might have been used in code in the first place.&amp;#160; In fact, in the general case, it’s actually impossible to do so.&amp;#160; If you really did want to reverse-engineer what the source declaration might have been for declarative security you could do so by reading out of the declarative security table, and having special knowledge about how each different type of permission can build up its internal state.&lt;/p&gt;  &lt;p&gt;(Interestingly, you can also see this separation of declarative security from real custom attributes in the IL syntax.&amp;#160; Rather than using a .custom declaration for declarative security, as you would for a standard custom attribute, you use either a .permission or a .permissionset declaration.&amp;#160; ILDASM will show this if you look at the IL output of an assembly that uses declarative security).&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10000306" width="1" height="1"&gt;</description></item><item><title>Is CAS dead in .NET 4?</title><link>http://blogs.msdn.com/b/shawnfa/archive/2010/02/24/so-is-cas-dead-in-net-4-or-what.aspx</link><pubDate>Wed, 24 Feb 2010 19:40:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9968845</guid><dc:creator>shawnfa</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/shawnfa/rsscomments.aspx?WeblogPostID=9968845</wfw:commentRss><comments>http://blogs.msdn.com/b/shawnfa/archive/2010/02/24/so-is-cas-dead-in-net-4-or-what.aspx#comments</comments><description>&lt;P&gt;With all the changes in the security system of .NET 4, the question frequently arises “so, is CAS dead now?”.&amp;nbsp;&amp;nbsp; One of the reasons that this question comes up so frequently, is that the term CAS in the .NET 1 security model was overloaded to refer to many different aspects of the security system:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;EM&gt;CAS policy&lt;/EM&gt; – policy levels, code groups, and of course our old friend caspol.exe&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;CAS enforcement&lt;/EM&gt; – primarily the act of demanding and asserting permissions&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;CAS permissions&lt;/EM&gt; – granted by CAS policy or a host to set the level of operations that an application can perform&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;I’ve talked in the past about &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;the many problems with CAS policy over the years&lt;/A&gt;.&amp;nbsp; There are &lt;A href="http://blogs.msdn.com/shawnfa/archive/2006/07/11/661769.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2006/07/11/661769.aspx"&gt;versioning problems&lt;/A&gt;.&amp;nbsp; The host doesn’t have control over the policy applied to the code it is hosting.&amp;nbsp; Enterprise administrators &lt;A href="http://blogs.msdn.com/shawnfa/archive/2004/09/07/226530.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2004/09/07/226530.aspx"&gt;don’t have a good way to deploy CAS policy updates&lt;/A&gt;.&amp;nbsp; CAS policy caused managed applications to run differently from native applications, &lt;A href="http://blogs.msdn.com/shawnfa/archive/2004/12/30/344554.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2004/12/30/344554.aspx"&gt;often in confusing and undesirable ways&lt;/A&gt;.&amp;nbsp; And of course, there’s the general complexity and difficulty of use (caspol is nobody’s favorite tool).&lt;/P&gt;
&lt;P&gt;For these reasons, in v4 of the CLR, CAS policy has been deprecated and &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;policy decisions are instead left entirely up to the host of an application&lt;/A&gt;.&amp;nbsp; However, the other security mechanisms that fell under the name CAS, which allow hosts to configure AppDomains to host sandboxed code and allow library authors to write a safe APTCA library exposing services to partial trust absolutely still exist and are supported.&lt;/P&gt;
&lt;P&gt;For instance, when a host sets up a sandboxed AppDomain to run code in, it does this by figuring out what grant set should be given to an application and supplying that grant as a set of permissions – the exact same permissions that have been used since v1 of the .NET Framework.&amp;nbsp;&amp;nbsp; Custom permissions can still be created by hosts or APTCA library authors to protect their libraries, and assemblies and AppDomains still receive permission objects in their grant sets.&lt;/P&gt;
&lt;P&gt;Similarly, permissions demands are still alive and well, and are one of the most common ways that safe critical APIs in APTCA libraries will check to ensure that the sandbox they are running in supports a given operation.&amp;nbsp;&amp;nbsp; For example, opening a file is a security safe critical operation which demands FileIOPermission to ensure that the host has setup the current sandbox with permission to access the requested file.&lt;/P&gt;
&lt;P&gt;What does all of this mean in practice for things like ClickOnce and ASP.NET Medium Trust sandboxes?&amp;nbsp;&amp;nbsp; Both ASP.NET and ClickOnce are hosts that setup sandboxes for partial trust code – which is a core scenario for the CLR that is still very much alive.&amp;nbsp; ASP.NET simply sets up an AppDomain with the Medium Trust permission set (or whichever other permission set has been configured for the site in question), and all of the application assemblies loaded into that domain will receive the partial trust permission set that ASP.NET configured.&amp;nbsp; If those applications try to open a file or do some other operation that is only allowed in certain sandboxes, a permission demand will be issued, and if that demand succeeds the operation will succeed.&lt;/P&gt;
&lt;P&gt;Similarly, ClickOnce continues to work in the same way as it always had.&amp;nbsp; The ClickOnce runtime sets up an AppDomain with the permissions specified in the application’s manifest and the application will run in a sandbox with that permission set.&amp;nbsp;&amp;nbsp; Safe critical APIs which issue demands outside of the application’s grant set will lead to security exceptions, while safe critical APIs that access resources allowed under the application’s grant set will work just like they used to.&lt;/P&gt;
&lt;P&gt;In fact, the actual ClickOnce code really didn’t change very much at all for v4 security.&amp;nbsp; Since ClickOnce has always setup homogenous AppDomains dating back to its introduction in .NET 2.0, it has never had a dependency on CAS policy at runtime!&lt;/P&gt;
&lt;P&gt;Even though we’ve moved away from CAS policy, the CLR still provides mechanisms for partially trusted code to be setup and run – and that’s something we’ve continued to invest in making a better and safer experience.&amp;nbsp; A lot of our work with security transparency in this release, for instance, was to make it safer for APTCA library authors to expose their code to partial trust.&amp;nbsp; The new &lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/11/18/using-secannotate-to-analyze-your-assemblies-for-transparency-violations-an-example.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/11/18/using-secannotate-to-analyze-your-assemblies-for-transparency-violations-an-example.aspx"&gt;SecAnnotate tool&lt;/A&gt; was designed exactly to help ensure that more libraries could be safely exposed in a partial trust sandbox.&lt;/P&gt;
&lt;P&gt;Recently, I was having a discussion with our security MVPs about how the overload of the term CAS is causing “CAS is dead” confusion, &lt;A href="http://www.pluralsight.com/community/blogs/keith/default.aspx" mce_href="http://www.pluralsight.com/community/blogs/keith/default.aspx"&gt;Keith Brown&lt;/A&gt; remarked to me that he prefers to think of it along these lines:&amp;nbsp; .NET 4: Security just got a whole lot simpler.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9968845" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/CAS/">CAS</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/CLR+v4/">CLR v4</category></item><item><title>Using SecAnnotate to Analyze Your Assemblies for Transparency Violations – An Example</title><link>http://blogs.msdn.com/b/shawnfa/archive/2009/11/18/using-secannotate-to-analyze-your-assemblies-for-transparency-violations-an-example.aspx</link><pubDate>Wed, 18 Nov 2009 16:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9924363</guid><dc:creator>shawnfa</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/shawnfa/rsscomments.aspx?WeblogPostID=9924363</wfw:commentRss><comments>http://blogs.msdn.com/b/shawnfa/archive/2009/11/18/using-secannotate-to-analyze-your-assemblies-for-transparency-violations-an-example.aspx#comments</comments><description>&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;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-09-92-43-63/TransparencyAnnotations.xml" length="7018" type="text/xml" /><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Security/">Security</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/CLR+v4/">CLR v4</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Transparency/">Transparency</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/SecAnnotate/">SecAnnotate</category></item><item><title>SecAnnotate Beta</title><link>http://blogs.msdn.com/b/shawnfa/archive/2009/11/18/secannotate-beta.aspx</link><pubDate>Wed, 18 Nov 2009 16:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9924347</guid><dc:creator>shawnfa</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/shawnfa/rsscomments.aspx?WeblogPostID=9924347</wfw:commentRss><comments>http://blogs.msdn.com/b/shawnfa/archive/2009/11/18/secannotate-beta.aspx#comments</comments><description>&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;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-09-92-43-47/SecAnnotate.msi" length="768000" type="application/octet-stream" /><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Security/">Security</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/CLR+v4/">CLR v4</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Transparency/">Transparency</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/SecAnnotate/">SecAnnotate</category></item><item><title>Differences Between the Security Rule Sets</title><link>http://blogs.msdn.com/b/shawnfa/archive/2009/11/12/differences-between-the-security-rule-sets.aspx</link><pubDate>Thu, 12 Nov 2009 20:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9921126</guid><dc:creator>shawnfa</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/shawnfa/rsscomments.aspx?WeblogPostID=9921126</wfw:commentRss><comments>http://blogs.msdn.com/b/shawnfa/archive/2009/11/12/differences-between-the-security-rule-sets.aspx#comments</comments><description>&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;</description><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Security/">Security</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Silverlight/">Silverlight</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/CLR+v4/">CLR v4</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Transparency/">Transparency</category></item><item><title>Transparency Models: A Tale of Two Levels</title><link>http://blogs.msdn.com/b/shawnfa/archive/2009/11/11/transparency-models-a-tale-of-two-levels.aspx</link><pubDate>Wed, 11 Nov 2009 19:24:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9920999</guid><dc:creator>shawnfa</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/shawnfa/rsscomments.aspx?WeblogPostID=9920999</wfw:commentRss><comments>http://blogs.msdn.com/b/shawnfa/archive/2009/11/11/transparency-models-a-tale-of-two-levels.aspx#comments</comments><description>&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;</description><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Security/">Security</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/CLR+v4/">CLR v4</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Transparency/">Transparency</category></item><item><title>Transparency as Enforcement in CLR v4</title><link>http://blogs.msdn.com/b/shawnfa/archive/2009/11/09/transparency-as-enforcement-in-clr-v4.aspx</link><pubDate>Mon, 09 Nov 2009 20:23:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9919750</guid><dc:creator>shawnfa</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/shawnfa/rsscomments.aspx?WeblogPostID=9919750</wfw:commentRss><comments>http://blogs.msdn.com/b/shawnfa/archive/2009/11/09/transparency-as-enforcement-in-clr-v4.aspx#comments</comments><description>&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;</description><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Security/">Security</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/CLR+v4/">CLR v4</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Transparency/">Transparency</category></item><item><title>Bridging the Gap Between Transparent and Critical Code</title><link>http://blogs.msdn.com/b/shawnfa/archive/2009/11/05/bridging-the-gap-between-transparent-and-critical-code.aspx</link><pubDate>Thu, 05 Nov 2009 17:59:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9918103</guid><dc:creator>shawnfa</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/shawnfa/rsscomments.aspx?WeblogPostID=9918103</wfw:commentRss><comments>http://blogs.msdn.com/b/shawnfa/archive/2009/11/05/bridging-the-gap-between-transparent-and-critical-code.aspx#comments</comments><description>&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;</description><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Security/">Security</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/CLR+v4/">CLR v4</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Transparency/">Transparency</category></item><item><title>Transparency 101: Basic Transparency Rules</title><link>http://blogs.msdn.com/b/shawnfa/archive/2009/11/03/transparency-101-basic-transparency-rules.aspx</link><pubDate>Tue, 03 Nov 2009 17:38:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9916865</guid><dc:creator>shawnfa</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/shawnfa/rsscomments.aspx?WeblogPostID=9916865</wfw:commentRss><comments>http://blogs.msdn.com/b/shawnfa/archive/2009/11/03/transparency-101-basic-transparency-rules.aspx#comments</comments><description>&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;</description><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Security/">Security</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/CLR+v4/">CLR v4</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Transparency/">Transparency</category></item><item><title>CLR v4 Security Policy Roundup</title><link>http://blogs.msdn.com/b/shawnfa/archive/2009/06/12/clr-v4-security-policy-roundup.aspx</link><pubDate>Fri, 12 Jun 2009 21:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9737125</guid><dc:creator>shawnfa</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/shawnfa/rsscomments.aspx?WeblogPostID=9737125</wfw:commentRss><comments>http://blogs.msdn.com/b/shawnfa/archive/2009/06/12/clr-v4-security-policy-roundup.aspx#comments</comments><description>&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;</description><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Security/">Security</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/ClickOnce/">ClickOnce</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/CAS/">CAS</category><category domain="http://blogs.msdn.com/b/shawnfa/archive/tags/Policy/">Policy</category></item></channel></rss>