<?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 : CAS</title><link>http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx</link><description>Tags: CAS</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>CLR v4 Security Policy Roundup</title><link>http://blogs.msdn.com/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>1</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/9737125.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=9737125</wfw:commentRss><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/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/ClickOnce/default.aspx">ClickOnce</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx">Policy</category></item><item><title>Temporarily re-enabling CAS policy during migration</title><link>http://blogs.msdn.com/shawnfa/archive/2009/06/12/temporarily-re-enabling-cas-policy-during-migration.aspx</link><pubDate>Fri, 12 Jun 2009 21:27:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9737038</guid><dc:creator>shawnfa</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/9737038.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=9737038</wfw:commentRss><description>&lt;p&gt;Over the last few weeks we’ve been looking at the &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;changes to security policy in .NET 4,&lt;/a&gt; namely that security policy is now in the hands of the host and the operating system.&lt;/p&gt;  &lt;p&gt;While we’ve looked at how to update code that &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx"&gt;implicitly uses CAS policy&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx"&gt;loads assemblies from remote sources&lt;/a&gt;, and &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/06/09/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx"&gt;explicitly uses CAS policy,&lt;/a&gt; in applications of larger size it may not be practical to update all the code at once.&amp;#160; Similarly, you might be able to update the code in your application, but may rely on a third party assembly that is not yet updated for the changes in CAS policy.&lt;/p&gt;  &lt;p&gt;If you do find yourself needing to re-enable CAS policy temporarily, in order to move a large code base to the new v4 security APIs bit by bit rather than all at once, or to use an assembly that you don’t control, there is a configuration switch that you can set in order to flip your process back into legacy CAS policy mode.&lt;/p&gt;  &lt;p&gt;In order to temporarily enable legacy CAS policy in your process, you’ll need an .exe.config file for your application with the legacy security policy switch set in its runtime section.&amp;#160; So, if your application’s entry point is YourApp.exe, you’ll have next to it a YourApp.exe.config file.&amp;#160; (You can also use the app.config feature in your Visual Studio project).&amp;#160; The file should look like this for any release of the .NET Framework v4 after beta 1:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;   &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;NetFx40_LegacySecurityPolicy&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;enabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;true&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In .NET 4 Beta 1, the switch has a slightly different name:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;legacyCasPolicy&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;enabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;true&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;One thing to note is that this switch must be set on the process-level.&amp;#160; So, if you’re using a third party control that uses CAS policy, you may well need to set the switch for both Visual Studio in devenv.exe.config and for your application itself.&amp;#160; That way the control will work both in the Visual Studio process during your development, as well as in your process at runtime.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9737038" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx">Policy</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx">CLR v4</category></item><item><title>Coding with Security Policy in .NET 4 part 2 – Explicit uses of CAS policy</title><link>http://blogs.msdn.com/shawnfa/archive/2009/06/09/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx</link><pubDate>Tue, 09 Jun 2009 22:14:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9717560</guid><dc:creator>shawnfa</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/9717560.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=9717560</wfw:commentRss><description>&lt;p&gt;Over the last few posts, I’ve been looking at how the update to the CLR v4 &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;security policy&lt;/a&gt; interacts with how you write managed code against the v4 .NET Framework.&amp;#160; So far we’ve looked at the implicit uses of CAS policy, such as &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;loading assemblies and creating AppDomains with Evidence&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx"&gt;loading assemblies from remote sources&lt;/a&gt;.&amp;#160; Now let’s look at how to work with code which was written to work with CAS policy explicitly.&lt;/p&gt;  &lt;p&gt;The good news is that explicit use of CAS policy is frequently very easy to spot, as opposed to implicit uses which can be somewhat more subtle.&amp;#160; APIs that directly manipulate policy (such as SecurityManager.ResolvePolicy) as well as those that require CAS policy to sandbox (such as AppDomain.SetAppDomainPolicy) fall into this category.&amp;#160; Other APIs that explicitly use CAS policy are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;AppDomain.SetAppDomainPolicy &lt;/li&gt;    &lt;li&gt;HostSecurityManager.DomainPolicy &lt;/li&gt;    &lt;li&gt;PolicyLevel.CreateAppDomainLevel &lt;/li&gt;    &lt;li&gt;SecurityManager.LoadPolicyLevelFromString &lt;/li&gt;    &lt;li&gt;SecurityManager.LoadPolicyLevelFromFile &lt;/li&gt;    &lt;li&gt;SecurityManager.ResolvePolicy &lt;/li&gt;    &lt;li&gt;SecurityManager.ResolveSystemPolicy &lt;/li&gt;    &lt;li&gt;SecurityManager.ResolvePolicyGroups &lt;/li&gt;    &lt;li&gt;SecurityManager.PolicyHierarchy &lt;/li&gt;    &lt;li&gt;SecurityManager.SavePolicy &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;As with the implicit CAS policy uses, the explicit APIs also are obsolete in .NET 4, and will throw NotSupportedExceptions by default:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;System.NotSupportedException: This method uses CAS policy, which has been obsoleted by the .NET Framework. In order to enable CAS policy for compatibility reasons, please use the NetFx40_LegacySecurityPolicy configuration switch. Please see &lt;a href="http://go.microsoft.com/fwlink/?LinkId=131738"&gt;http://go.microsoft.com/fwlink/?LinkId=131738&lt;/a&gt; for more information.&lt;/div&gt;  &lt;p&gt;Let’s take a look at how code which used these APIs in the past might get updated with new v4 APIs.&lt;/p&gt;  &lt;p&gt;Generally, there are three reasons that the explicit policy APIs are being used:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The code wants to figure out what the grant set of an assembly or AppDomain is &lt;/li&gt;    &lt;li&gt;The code wants to create a sandbox &lt;/li&gt;    &lt;li&gt;The code wants to figure out what a safe sandbox is to setup &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The correct way way to update the code calling an explicit policy API in v4 depends upon what it was trying to do by calling the API in the first place.&amp;#160; Let’s take a look at each of the reasons for using an explicit policy API in turn and figure out what the replacement code should look like.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Figuring out what the grant set of an assembly or AppDomain is&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Sometimes an application or library wants to figure out what the grant set of a particular assembly or domain was and would do so with code similar to:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;   &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; GetAssemblyGrantSet(&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; assembly)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; assemblyEvidence = assembly.Evidence;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.ResolvePolicy(assemblyEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsFullyTrusted(&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; assembly)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; grant = GetAssemblyGrantSet(assembly);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; grant.IsUnrestricted();&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; GetAppDomainGrantSet(&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; domain)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; appDomain = domain.Evidence;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.ResolvePolicy(appDomain);&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsFullyTrusted(&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; domain)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; grant = GetAppDomainGrantSet(domain);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; grant.IsUnrestricted();&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This code worked by resolving the assembly or AppDomain’s evidence through CAS policy to determine what would be granted to that particular evidence.&amp;#160; There are a few problems here – for instance, the code doesn’t take into account simple sandbox domains, hosted AppDomains, dynamic assemblies, or assemblies loaded from byte arrays.&amp;#160; (Take a look at AssemblyExtensionMethods.GetPermissionSet() on &lt;a href="http://clrsecurity.codeplex.com"&gt;http://clrsecurity.codeplex.com&lt;/a&gt; for code that does take most of the other considerations into account).&amp;#160;&amp;#160; These methods also cause a full CAS policy resolution to occur, which is not a cheap operation.&amp;#160; &lt;/p&gt;

&lt;p&gt;Instead of requiring people to manually jump through hoops in order to recreate the CLR’s security policy system in v4, we’ve directly exposed the grant sets of assemblies and AppDomains as properties of the objects themselves.&amp;#160; The above code can be replaced with:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; GetAssemblyGrantSet(&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; assembly)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; assembly.PermissionSet;&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsFullyTrusted(&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; assembly)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; assembly.IsFullyTrusted;&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; GetAppDomainGrantSet(&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; domain)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; domain.PermissionSet;&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsFullyTrusted(&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; domain)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; domain.IsFullyTrusted;&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Which has the dual benefit of being more accurate (these properties read the real grant set that the CLR is using, no matter how it was determined), and also being faster than a full policy resolution.&lt;/p&gt;

&lt;p&gt;Accessing the PermissionSet property of an AppDomain or an Assembly does require that the accessing code be fully trusted.&amp;#160; The reason is that the permission sets themselves can contain sensitive data.&amp;#160; (For instance, FileIOPermission can contain full path information about the local machine in it).&amp;#160;&amp;#160; Partial trust code, however, can use the IsFullyTrusted property.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a Sandbox&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I suspect many people who have read this blog already know what I’m going to say here :-)&amp;#160; Instead of using SetAppDomainPolicy to create a sandbox, which suffers from many problems, the replacement API is the &lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx"&gt;simple sandboxing API&lt;/a&gt;.&amp;#160; I’ve already covered most of the reasoning for this change when &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx"&gt;I talked about sandboxing in CLR v4&lt;/a&gt;, so let’s look at the final reason that code may have been using CAS policy APIs&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figuring out what a safe grant set is to provide a sandbox&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes a host needs to figure out what is a reasonable set of permissions to assign to a sandbox.&amp;#160; For instance, even though ClickOnce does not use CAS policy, it still needs to figure out if the permission set that the ClickOnce application is requesting is a reasonable set of permissions for it to have.&amp;#160;&amp;#160; (For instance, if it’s requesting only the permission to execute, that’s going to be fine, while if an application from the Internet is requesting permission to read and write all of the files on your disk, that’s not such a good idea).&lt;/p&gt;

&lt;p&gt;In order to solve this problem in v2, code might look like this:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsSafeGrantSet(&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; grantSet, &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; sandboxEvidence)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// Figure out what the CLR's policy system says is safe to give a sandbox&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// with this evidence&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; systemGrantSet = &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.ResolveSystemPolicy(sandboxEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// We'll consider this safe only if we're requesting a subset of the safe&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// sandbox set.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; grantSet.IsSubsetOf(systemGrantSet);&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Since system wide CAS policy (which this code depends upon to determine safety) is deprecated in v4, we need to find a new way to accomplish this goal.&lt;/p&gt;

&lt;p&gt;The answer is with a new API called GetStandardSandbox.&amp;#160;&amp;#160; GetStandardSandbox is used to have the CLR provide what it considers a safe sandbox grant set for an AppDomain that will host code with the specified evidence.&amp;#160; It’s the CLR’s way of providing suggestions to hosts who are making trust decisions.&amp;#160;&amp;#160; One thing that is very important to note is what GetStandardSandbox is not however.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;u&gt;GetStandardSandbox is not a policy API&lt;/u&gt;&lt;/em&gt;.&amp;#160; This isn’t the CLR applying CAS to evidence in order to modify grant set, and the CLR does not take any external factors such as CAS policy into account when returning its grant set.&amp;#160; Instead, GetStandardSandbox is simply a helper API for hosts which are trying to setup sandboxes.&lt;/p&gt;

&lt;p&gt;With that in mind, the way the above code would be written in CLR v4 is:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsSafeGrantSet(&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; grantSet, &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; sandboxEvidence)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// Figure out what the CLR considers a safe grant set&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; clrSandbox = &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.GetStandardSandbox(sandboxEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// We'll consider this safe only if we're requesting a subset of the safe&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// sandbox set.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; grantSet.IsSubsetOf(clrSandbox);&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Similarly, if you are a host trying to setup an AppDomain to sandbox assemblies that are coming from the Internet, you might do so this way:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Find a safe sandbox set to give to assemblies downloaded&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// from the internet&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; internetEvidence = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;internetEvidence.AddHostEvidence(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Zone&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SecurityZone&lt;/span&gt;.Internet));&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; clrSandbox = &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.GetStandardSandbox(internetEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Create a sandboxed AppDomain to hold them&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt; sandboxSetup = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;sandboxSetup.ApplicationBase = DownloadDirectory;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; sandbox = &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CreateDomain(&lt;span style="color: #a31515"&gt;&amp;quot;Internet sandbox&amp;quot;&lt;/span&gt;,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; internetEvidence,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sandboxSetup,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; clrSandbox);&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9717560" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx">Policy</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx">CLR v4</category></item><item><title>More Implicit Uses of CAS Policy: loadFromRemoteSources</title><link>http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx</link><pubDate>Mon, 08 Jun 2009 21:59:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9709114</guid><dc:creator>shawnfa</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/9709114.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=9709114</wfw:commentRss><description>&lt;p&gt;In my last post about changes to the CLR v4 security policy model, &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx"&gt;I looked at APIs which implicitly use CAS policy in their operation&lt;/a&gt; (such as Assembly.Load overloads that take an Evidence parameter), and how to migrate code that was using those APIs.&amp;#160;&amp;#160; There are another set of assembly loads which cause implicit use of CAS policy, which I’ll look at today – these are loads from remote sources.&lt;/p&gt;  &lt;p&gt;For example, in .NET 3.5 the following code:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;   &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; internetAssembly = &lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt;.LoadFrom(&lt;span style="color: #a31515"&gt;@&amp;quot;http://www.microsoft.com/assembly.dll&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; intranetAssembly = &lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt;.LoadFrom(&lt;span style="color: #a31515"&gt;@&amp;quot;\\server\share\assembly.dll&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Will by default load internetAssembly with the Internet permission set and intranetAssembly with the LocalIntranet permission set.&amp;#160;&amp;#160; That was because the CLR would internally gather evidence for both assemblies and run that evidence though CAS policy in order to find the permission set to grant that assembly. &lt;/p&gt;

&lt;p&gt;Now that &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx"&gt;the sandboxing model has changed in the v4 CLR&lt;/a&gt;, there is no more CAS policy to apply the assembly’s evidence to by default, and&amp;#160; therefore default behavior of both of these loads would be to load the assemblies with a grant set of full trust.&lt;/p&gt;

&lt;p&gt;That creates a problem for code which was written before .NET 4 shipped – this code may quite reasonably be expecting that the above assembly loads are safe because the CLR will automatically apply a restricted grant set to the assemblies if they are coming from a remote location.&amp;#160;&amp;#160; Now when the code runs in the v4 CLR, the assemblies are elevated to full trust, which amounts to a silent elevation of privilege bug against the .NET 2.0 code which was expecting that these assemblies be sandboxed.&amp;#160; Obviously that’s not a good thing.&lt;/p&gt;

&lt;p&gt;Instead of silently granting these assemblies full trust, the v4 CLR will actually take the opposite approach.&amp;#160; We’ll detect that these assemblies are being loaded in such a way that&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;They would have been sandboxed by the v2 CLR &lt;em&gt;and&lt;/em&gt; &lt;/li&gt;

  &lt;li&gt;Are going to be given full trust by the v4 CLR &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once we detect an assembly load where both of the above conditions are true, the CLR will refuse to load the assembly with the following message:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;p&gt;System.IO.FileLoadException: Could not load file or assembly '&lt;em&gt;&amp;lt;assemblyPath&amp;gt;&lt;/em&gt;' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515 (COR_E_NOTSUPPORTED)) ---&amp;gt;&lt;/p&gt;

  &lt;p&gt;System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=131738 for more information.&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;This exception is saying “The v4 CLR is not going to sandbox the assembly that you’re trying to load, however the v2 CLR would have.&amp;#160; We don’t know if that’s safe in your application or not, so we’re going to fail the assembly load to ensure that your application is secure by default.&amp;#160; However, if this is a safe assembly load, go ahead and enable loading from remote sources for this process.”&lt;/p&gt;

&lt;p&gt;That leads to the next question -- how do you know if it is safe to enable loadFromRemoteSources in your application?&amp;#160; This decision generally comes down to applying these tests:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Do you trust the string that you’re passing to Assembly.LoadFrom? &lt;/li&gt;

  &lt;li&gt;Do you trust the assembly that you’re loading? &lt;/li&gt;

  &lt;li&gt;Do you trust the server hosting the assembly (and the network path from the server back to your application)? &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you answered yes to all three questions then your application is a good candidate for enabling the loadFromRemoteSources switch.&amp;#160; If you answered no to any of the three questions, then you may need to take further action before enabling the switch and loading the assembly.&amp;#160;&amp;#160; (For instance, you may have some application logic to ensure that the string being passed to LoadFrom is going to a server you trust, or your application might download the assembly first and verify it has an Authenticode signature that it trusts).&lt;/p&gt;

&lt;p&gt;Let’s look at some examples:&lt;/p&gt;

&lt;p&gt;The most straight-forward reason that you would want to enable this is in the case that you know what the assemblies you are loading are, you trust them, and you trust the server that they are hosted on.&amp;#160; For example, if your application is hosted on a share on your company’s intranet, and happens to need to load other assemblies from other shares on the network, you probably want to enable the switch.&amp;#160;&amp;#160; (In many cases, this category of applications used to have to fight with CAS policy to get things loaded the way they wanted, now with loadFromRemoteSources set things should just work.)&lt;/p&gt;

&lt;p&gt;On the other hand, if you are an application that takes as untrusted input a string which then is passed through to Assembly.LoadFrom, you probably don’t want to enable this switch, as you might be opening yourself up to an elevation of privilege attack via that untrusted input.&lt;/p&gt;

&lt;p&gt;Similarly, if your application takes as input an assembly name to LoadFrom, however you trust that input.&amp;#160; (Maybe it comes directly from your application’s user, and there is no trust boundary between the user and your app – for instance, the user is pointing you at a plugin they trust and wish to load in the app), you may also want to enable this switch.&lt;/p&gt;

&lt;p&gt;Another consideration to take into account when considering loadFromRemoteSources is that this is a process-wide configuration switch.&amp;#160; This means that it applies to all places in your code which loads assemblies, not just a single LoadFrom call.&amp;#160; If you only trust the inputs to some of your assembly loads, then you may wish to consider not using the loadFromRemoteSources switch and instead take a different approach.&lt;/p&gt;

&lt;p&gt;Since the first condition for the NotSupportedException that blocks remote assembly loads is that the load would have been sandboxed by the v2 CLR, one alternate way to enable these loads without setting loadFromRemoteSources for the entire process is to load the assemblies into a domain that you create &lt;a href="http://blogs.msdn.com/shawnfa/archive/2005/08/08/449050.aspx"&gt;with the simple sandboxing API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This will work because even in v2.0, simple sandbox domains never apply CAS policy, and therefore any remote loads in simple sandbox domains would not have required CAS policy to sandbox them.&amp;#160; Since the assemblies would not have used CAS policy in v2, the loads are considered safe to use in v4 as well, and will succeed without the NotSupportedException being thrown. &lt;/p&gt;

&lt;p&gt;For example, if you want to enable only a subset of LoadFroms to load assemblies in full trust, if you create a fully trusted simple sandbox, then any assemblies loaded into that sandbox would have the same full trust grant set in v2 as in v4.&amp;#160;&amp;#160; (The full trust grant set of the domain applies to all assemblies loaded into it).&amp;#160;&amp;#160; This will cause the CLR to allow the loads to proceed in full trust in v4 without having to throw the switch.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Since this application only trusts a handful of LoadFrom operations,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// we'll put them all into the same AppDomain which is a simple sandbox&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// with a full trust grant set.&amp;#160; The application itself will not enable&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// loadFromRemoteSources, but instead channel all of the trusted loads&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// into this domain.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; trustedLoadFromRemoteSourceGrantSet&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;PermissionState&lt;/span&gt;.Unrestricted);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt; trustedLoadFromRemoteSourcesSetup = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;trustedLoadFromRemoteSourcesSetup.ApplicationBase =&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CurrentDomain.SetupInformation.ApplicationBase;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; trustedRemoteLoadDomain =&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CreateDomain(&lt;span style="color: #a31515"&gt;&amp;quot;Trusted LoadFromRemoteSources Domain&amp;quot;&lt;/span&gt;,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;null&lt;/span&gt;,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; trustedLoadFromRemoteSourcesSetup,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; trustedLoadFromRemoteSourcesGrantSet);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Now all trusted remote LoadFroms can be done in the trustedRemoteLoadDomain,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// and communicated with via a MarshalByRefObject.&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;As an example in the opposite direction, maybe your application has mostly loads which are safe to have remote targets, however there are a small handful of places that do need to be sandboxed.&amp;#160; By creating a simple sandboxed AppDomain for those loads, you can then safely set the loadFromRemoteSources switch for the rest of your process.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Since this application trusts almost all of its assembly loads, it&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// is going to enable the process-wide loadFromRemoteSources switch.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// However, the loads that it does not trust still need to be sandboxed.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// First figure out a grant set that the CLR considers safe to apply&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// to code from the Internet.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; sandboxEvidence = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;sandboxEvidence.AddHostEvidence(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Zone&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SecurityZone&lt;/span&gt;.Internet));&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; remoteLoadGrantSet = &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.GetStandardSandbox(sandboxEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt; remoteLoadSetup = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;trustedLoadFromRemoteSourcesSetup.ApplicationBase = GetSandboxRoot();&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; remoteLoadSandbox =&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CreateDomain(&lt;span style="color: #a31515"&gt;&amp;quot;Remote Load Sandbox&amp;quot;&lt;/span&gt;,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sandboxEvidence,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; remoteLoadSetup,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; remoteLoadGrantSet);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Now all trusted remote LoadFroms can be done in the default domain&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// with loadFromRemoteSources set, and untrusted loads can be done&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// in the sandbox that we just setup.&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;(Similarly, if the process is in legacy CAS policy mode, the v4 CLR will have the same behavior as the v2 CLR, and there will be no exception).&lt;/p&gt;

&lt;p&gt;Let’s say that you’ve considered the security implications and your application is a good candidate to enable loadFromRemoteSources, how do you go about doing so?&amp;#160;&amp;#160; Basically, you just provide a .exe.config file for your application with a loadFromRemoteSources runtime switch enabled.&amp;#160;&amp;#160; So, if your application’s entry point is YourApp.exe, you’ll want to make a YourApp.exe.config.&amp;#160;&amp;#160; (Or use the app.config file in your Visual Studio project).&amp;#160;&amp;#160; This configuration file will need to contain runtime section such as:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;loadFromRemoteSources&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;enabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;true&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This setting will cause the CLR to notice that even though it is going to load an assembly that would have been sandboxed in the v2 runtime, your application has explicitly stated that this is a safe thing to do.&amp;#160;&amp;#160; Since your application has said that it understands the security impact of loading from remote locations and it is safe in the context of this application, the CLR will then allow these loads to succeed without throwing a NotSupportedException to block them.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9709114" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx">Policy</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx">CLR v4</category></item><item><title>CLR 4 Security on Channel 9</title><link>http://blogs.msdn.com/shawnfa/archive/2009/05/28/clr-4-security-on-channel-9.aspx</link><pubDate>Thu, 28 May 2009 23:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9648833</guid><dc:creator>shawnfa</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/9648833.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=9648833</wfw:commentRss><description>&lt;p&gt;A while back I did an interview with &lt;a href="http://channel9.msdn.com/Niners/Charles/"&gt;Charles Torre&lt;/a&gt;&amp;#160; about the changes to security in CLR v4, and he posted it to the Channel 9 videos site yesterday.&lt;/p&gt;  &lt;p&gt;I start out talking about the security policy changes I've been covering here over the last week, and then transition into an overview of some of the transparency changes that I'll be talking about once I finish with the policy changes.&lt;/p&gt; &lt;object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="320" height="240"&gt; &lt;param name="source" value="http://channel9.msdn.com/App_Themes/default/vp09_04_23.xap" /&gt; &lt;param name="initParams" value="m=mms://mschnlnine.wmod.llnwd.net/a1809/d1/ch9/6/7/9/8/6/4/CLR4SecurityModel_s_ch9.wmv,autostart=false,autohide=true,showembed=true, thumbnail=http://mschnlnine.vo.llnwd.net/d1/ch9/6/7/9/8/6/4/CLR4SecurityModel_large_ch9.png, postid=468976" /&gt; &lt;param name="background" value="#00FFFFFF" /&gt; &lt;a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"&gt; &lt;img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none" /&gt; &lt;/a&gt; &lt;/object&gt;  &lt;p&gt;(The full video is also available here: &lt;a href="http://channel9.msdn.com/posts/Charles/Shawn-Farkas-CLR-4-Inside-the-new-Managed-Security-Model/" mce_href="http://channel9.msdn.com/posts/Charles/Shawn-Farkas-CLR-4-Inside-the-new-Managed-Security-Model/"&gt;http://channel9.msdn.com/posts/Charles/Shawn-Farkas-CLR-4-Inside-the-new-Managed-Security-Model/&lt;/a&gt;)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9648833" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx">Policy</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx">CLR v4</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Transparency/default.aspx">Transparency</category></item><item><title>Visual Studio 10 Security Tab Changes</title><link>http://blogs.msdn.com/shawnfa/archive/2009/05/28/visual-studio-10-security-tab-changes.aspx</link><pubDate>Thu, 28 May 2009 17:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9644742</guid><dc:creator>shawnfa</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/9644742.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=9644742</wfw:commentRss><description>&lt;p&gt;Kris Makey, who works on the Visual Studio team, has written up a good blog post about the &lt;a href="http://blogs.msdn.com/krimakey/archive/2009/05/20/where-did-my-permission-set-controls-go.aspx"&gt;changes you’ll see on the security tab in Visual Studio 10 when it comes to editing permission sets&lt;/a&gt;.&amp;#160; He covers what the changes are, and some of the reasons why we worked with the Visual Studio team to make those changes.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9644742" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx">CLR v4</category></item><item><title>Coding with Security Policy in .NET 4.0 – Implicit uses of CAS policy</title><link>http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx</link><pubDate>Wed, 27 May 2009 20:46:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9644730</guid><dc:creator>shawnfa</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/9644730.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=9644730</wfw:commentRss><description>&lt;p&gt;Last week we looked at &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx"&gt;sandboxing and the v4 CLR&lt;/a&gt; – with the key change being that the CLR now defers exclusively to the host application when setting up sandboxed domains by moving away from the old CAS policy model, and moving instead to simple sandboxed AppDomains.&lt;/p&gt;  &lt;p&gt;This leads to an interesting situation when your program calls APIs that assume the presence of CAS policy, either implicitly [for example, &lt;a href="http://msdn.microsoft.com/en-us/library/0wcskf6d.aspx"&gt;Assembly.Load(string, Evidence)]&lt;/a&gt; or explicitly [for example &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.securitymanager.policyhierarchy.aspx"&gt;SecurityManager.PolicyHierarchy&lt;/a&gt;].&amp;#160;&amp;#160; These APIs require CAS policy in order to return correct results, however by default there is no longer CAS policy to apply behind the scenes anymore.&lt;/p&gt;  &lt;p&gt;Let’s take a look at what happens if these APIs are called, and what should be done to update your code to take into account the new security policy model.&lt;/p&gt;  &lt;p&gt;(In addition to this blog post, the CLR security test team is preparing a set of blog posts about how they moved our test code base forward to deal with these and other v4 security changes – those posts will provide additional advice about how to replace uses of obsolete APIs based upon the real world examples they’ve seen).&lt;/p&gt;  &lt;p&gt;In general, APIs that assume the presence of CAS policy have been marked obsolete, and will give a compiler warning when you build against them:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;Microsoft (R) Visual C# 2010 Compiler version 4.0.20506    &lt;br /&gt;Copyright (C) Microsoft Corporation. All rights reserved.     &lt;br /&gt;    &lt;br /&gt;obsolete.cs(32,1): warning CS0618: '&amp;lt;&lt;i&gt;API Name&amp;gt;&lt;/i&gt;' is     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; obsolete: 'This method is obsolete and will be removed in a future     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; release of the .NET Framework. Please use &lt;i&gt;&amp;lt;suggested alternate API&amp;gt;&lt;/i&gt;. See     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;a href="http://go.microsoft.com/fwlink/?LinkId=131738"&gt;http://go.microsoft.com/fwlink/?LinkId=131738&lt;/a&gt; for more information.'     &lt;br /&gt;&lt;/div&gt;  &lt;p&gt;Additionally, these APIs will throw a NotSupportedException if they are called at runtime:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;Unhandled Exception: System.NotSupportedException: This method uses CAS policy, which has been obsoleted by the .NET Framework. In order to enable CAS policy for compatibility reasons, please use the NetFx40_LegacySecurityPolicy configuration switch. Please see &lt;a href="http://go.microsoft.com/fwlink/?LinkId=131738"&gt;http://go.microsoft.com/fwlink/?LinkId=131738&lt;/a&gt; for more information.&lt;/div&gt;  &lt;p&gt;(In the beta 1 release, this message is slightly different:)&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;Unhandled Exception: System.NotSupportedException: This method uses CAS policy, which has been obsoleted by the .NET Framework. In order to enable CAS policy for compatibility reasons, please use the legacyCasPolicy configuration switch. Please see &lt;a href="http://go.microsoft.com/fwlink/?LinkId=131738"&gt;http://go2.microsoft.com/fwlink/?LinkId=131738&lt;/a&gt; for more information.&lt;/div&gt;  &lt;p&gt;Let’s take a look at the set of APIs which make implicit use of CAS policy first, and then see what they might be replaced with in a v4.0 application.&lt;/p&gt;  &lt;p&gt;The general way to recognize an API which is implicitly using CAS policy is that they tend to take an Evidence parameter which was used to resolve against CAS policy and provide a grant set for an assembly.&amp;#160; For instance:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Activator.CreateInstance and Activator.CreateInstanceFrom overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;AppDomain.CreateInstance, AppDomain.CreateInstanceFrom, AppDomain.CreateInstanceAndUnwrap, and AppDomain.CreateInstanceAndUnwrap overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;AppDomain.DefineDynamicAssembly overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;AppDomain.ExecuteAssembly and AppDomain.ExecuteAssemblyByName overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;AppDomain.Load and AppDomain.LoadFrom overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;Assembly.Load and Assembly.LoadFrom overloads which take an Evidence parameter &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;It’s important to note that although these APIs all take Evidence parameters, the concept of Evidence itself is not deprecated and continues to exist (and even enhanced in v4.0 – but that’s another show).&amp;#160; Evidence itself is still a useful tool for hosts to use when figuring out what grant sets they want to give assemblies.&amp;#160; The common thread with these APIs is that they used the Evidence to resolve against CAS policy – and it’s the CAS policy portion that’s been deprecated in v4.&lt;/p&gt;  &lt;p&gt;Let’s say that your application is using one of the Evidence-taking overloads of these APIs, and thus had an implicit dependency on CAS policy.&amp;#160; Figuring out what to replace the API call with depends upon what your application was trying to accomplish with the API call.&lt;/p&gt;  &lt;p&gt;We’ve found that commonly the goal of calling one of these APIs was not to sandbox the assembly being loaded, but rather to access other parameters on the overload which may not be available without also providing Evidence.&amp;#160; In these cases, you can go ahead and just drop the Evidence parameter from the API.&amp;#160; We’ve ensured that all of the above APIs now have overloads that provide the full set of parameters without requiring an Evidence parameter.&lt;/p&gt;  &lt;p&gt;Additionally, in many cases we’ve found that code passes in Assembly.GetExecutingAssembly().Evidence or simply null to the Evidence parameter.&amp;#160; In both of those cases, it’s safe to simply call an overload of the API which does not require an Evidence parameter as well.&lt;/p&gt;  &lt;p&gt;The other reason to provide Evidence when calling these APIs is to sandbox the assembly in question.&amp;#160; The correct way to do this in v4 (and the best way to do this in v2.0 and higher of the .NET Framework) is to simply load the assembly &lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx"&gt;into a simple sandboxed AppDomain&lt;/a&gt;.&amp;#160; The assembly will then be sandboxed by virtue of the fact that it’s loaded in the sandboxed domain, and you will no longer need to load the assembly with an Evidence parameter to restrict its grant set.&lt;/p&gt;  &lt;p&gt;I’ve listed the benefits of using simple sandboxed domains before, and they continue to apply in this scenario.&amp;#160; For example, using a simple sandbox rather than an Evidence resolve to sandbox assemblies allows your application:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;To be in charge of its own sandbox.&amp;#160; The load-with-Evidence route took a dependency on what the grant set that the CLR would give the assembly was.&amp;#160; That grant set could change from version to version of the CLR (&lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/07/11/661769.aspx"&gt;since each version has independent CAS policies&lt;/a&gt;), and even from user to user.&amp;#160; This makes supporting your application more difficult than it needs to be – with simple sandboxing there are no external dependencies for grant set resolution – your application is in charge of its own sandboxes &lt;/li&gt;    &lt;li&gt;To setup real isolation boundaries – hosting multiple levels of partial trust code within a single AppDomain turns out to be incredibly difficult to do correctly.&amp;#160; Further, hosting partial trust code in a domain wtih full trust code that does not expect to be run along with partial trust code also turns out to be problematic from a security perspective.&amp;#160; By isolating the partial trust code in its own sandboxed domain, a real isolation boundary is setup for the code and your application is kept much more secure by default. &lt;/li&gt;    &lt;li&gt;To have version and bitness independence – I touched on this in the first point, but to reiterate it, your application is no longer dependent upon each version of the CLR’s security policy to be setup in the same way, as well as each bitness of the policy within a single version. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;So, to summarize, if you’re using one of the Evidence taking APIs which would have resolved an assembly’s grant set against CAS policy in the past:&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top"&gt;&lt;b&gt;Use&lt;/b&gt;&lt;/td&gt;        &lt;td valign="top"&gt;&lt;b&gt;Replacement&lt;/b&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Passing null, Assembly.GetExecutingAssembly().Evidence, or AppDomain.CurrentDomain.Evidence&lt;/td&gt;        &lt;td valign="top"&gt;Call an overload which does not require an Evidence parameter.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Using a parameter of the API which was only available on an overload taking an Evidence parameter as well.&lt;/td&gt;        &lt;td valign="top"&gt;Call one of the newly added overloads which provides access to your parameter without requiring Evidence.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Sandboxing the assembly being loaded.&lt;/td&gt;        &lt;td valign="top"&gt;Load the assembly into a sandboxed AppDomain, and let the domain do the sandboxing.&amp;#160; This will remove the need for the Evidence parameter.&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Next time, I’ll look at the explicit uses of CAS policy, and what their replacements should be.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9644730" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx">Policy</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx">CLR v4</category></item><item><title>Sandboxing in .NET 4.0</title><link>http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx</link><pubDate>Fri, 22 May 2009 20:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9635605</guid><dc:creator>shawnfa</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/9635605.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=9635605</wfw:commentRss><description>&lt;P&gt;Yesterday I talked about the &lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;changes in security policy for managed applications&lt;/A&gt;, namely that managed applications will run with full trust - the same as native applications - when you execute them directly.&lt;/P&gt;
&lt;P&gt;That change doesn’t mean that managed code can no longer be sandboxed however - far from it.&amp;nbsp; Hosts such as ASP.NET and ClickOnce continue to use the CLR to sandbox untrusted code.&amp;nbsp; Additionally, any application can continue to create AppDomains to sandbox code in.&lt;/P&gt;
&lt;P&gt;As part of our overhaul of security policy in v4, we made some interesting changes to how that sandboxing should be accomplished as well.&amp;nbsp; In previous releases, the CLR provided a variety of ways to sandbox code – but many of them were problematic to use correctly.&amp;nbsp; In the v4 framework, we made it a goal to simplify and standardize how sandboxing should be done in managed code.&lt;/P&gt;
&lt;P&gt;One of the key observations we made about sandboxing is that there really isn’t a good reason for the CLR to be involved in the decision as to what grant set should be given to partial trust code.&amp;nbsp;&amp;nbsp; If your application says “I want to run this code with ReflectionPermission/RestrictedMemberAccess and SecurityPermission/Execution”, that’s exactly the set of permissions that the code should run with.&amp;nbsp;&amp;nbsp; After all, your application knows much better than the CLR what operations the sandboxed code can be safely allowed to undertake.&lt;/P&gt;
&lt;P&gt;The problem is, sandboxing by providing an AppDomain policy level doesn’t provide total control to the application doing the sandboxing.&amp;nbsp; For instance, imagine you wanted to provide the sandbox grant set of RestrictedMemberAccess + Execution permission.&amp;nbsp; You might setup a policy level that grants AllCode this grant set and assign it to the AppDomain.&amp;nbsp;&amp;nbsp; However, if the code you place in that AppDomain has evidence that says it came from the Internet, the CLR will instead produce a grant set that doesn’t include RestrictedMemberAccess for the sandbox.&amp;nbsp; Rather than allowing safe partial trust reflection as you wanted, your sandbox just became execute-only.&lt;/P&gt;
&lt;P&gt;This really doesn’t make sense – what right does the CLR have to tell your application what should and should not be allowed in its sandboxes?&amp;nbsp; In the v1.x release of the runtime, developers had to go to great lengths in order to ensure they got the grant set they wanted.&amp;nbsp; (&lt;A href="http://blogs.msdn.com/ericlippert/" mce_href="http://blogs.msdn.com/ericlippert/"&gt;Eric Lippert’s&lt;/A&gt; CAS policy acrobatics to get VSTO working correctly is the stuff of legends around the security team – fabulous adventures in coding indeed!).&lt;/P&gt;
&lt;P&gt;As many a frustrated application developer found out, intersecting with the application supplied grant set was only the tip of the iceburg when it came to the difficulties of coding with CAS policy.&amp;nbsp; You would also run into a slew of other problems – such as each version of the CLR having an entirely independent security policy to deal with.&lt;/P&gt;
&lt;P&gt;In v2.0, we introduced the &lt;A href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx"&gt;simple sandboxing API&lt;/A&gt; as a way for applications to say “This is the grant set I want my application to have.&amp;nbsp; Please don’t mess with it.”.&amp;nbsp; This went a long way toward making writing an application which sandboxes code an easier task.&lt;/P&gt;
&lt;P&gt;Beginning with v4.0, the CLR is getting out of the policy business completely.&amp;nbsp; By default, the CLR is not going to supply a CAS policy level that interferes with the wishes of the application that is trying to do sandboxing.&lt;/P&gt;
&lt;P&gt;Effectively, we’ve simplified the multiple ways that you could have sandboxed code in v3.5 into one easy option – all sandboxes in v4 must be setup with the simple sandboxing API.&lt;/P&gt;
&lt;P&gt;This means that the days of wrangling with complicated policy trees with arbitrary decision nodes in them are thankfully a thing of the past.&amp;nbsp; All that’s needed from here on out is a simple statement: “here is my sandboxed grant set, and here are the assemblies I wish to trust.”&lt;/P&gt;
&lt;P&gt;Next time, I’ll look at the implications of this on code that interacts with policy, looking at what you used to write, and what it would be replaced with in v4.0 of the CLR.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9635605" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx">Policy</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx">CLR v4</category></item><item><title>Security Policy in the v4 CLR</title><link>http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx</link><pubDate>Thu, 21 May 2009 22:03:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9634177</guid><dc:creator>shawnfa</dc:creator><slash:comments>13</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/9634177.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=9634177</wfw:commentRss><description>&lt;p&gt;One of the first changes that you might see to security in the v4 CLR is that we’ve overhauled the security policy system.&amp;#160; In previous releases of the .NET Framework, CAS policy applied to all assemblies loaded into an application (except for in &lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx"&gt;simple sandbox domains&lt;/a&gt;). &lt;/p&gt;  &lt;p&gt;That lead to a lot of interesting problems.&amp;#160; For instance, one of the more common issues people ran into was that they would develop an application on their local machine that they wanted to share with other people on the network.&amp;#160;&amp;#160; Once the application was working on their machine, they would share it out, but nobody could run it over the network because CAS policy provided a lower grant set to assemblies loaded from the intranet than it does to assemblies loaded from the local machine.&amp;#160;&amp;#160; The usual result was unexpected and unhandled SecurityExceptions when trying to use the application.&lt;/p&gt;  &lt;p&gt;Generally, the only solution to this problem was to either manually update the CAS policy on each machine that wanted to run the application, deploy the application some other way (for instance via ClickOnce), or use native code.&lt;/p&gt;  &lt;p&gt;One of the worst things about this problem was that the additional pain of not being able to just share a managed app over the network wasn’t actually buying any security.&amp;#160; If an application wanted to attack your machine, it could bypass the sandbox that the CLR was setting up simply by being written in native code.&lt;/p&gt;  &lt;p&gt;Effectively, running an executable &lt;em&gt;is&lt;/em&gt; a trust decision – you’re saying that you trust the application that you’re running enough to execute with the privileges your Windows account has.&lt;/p&gt;  &lt;p&gt;That leads to an interesting observation – the CLR isn’t the correct place to be setting permission restrictions for applications that are being launched directly (either from the command prompt, or from Windows explorer for instance).&amp;#160; Instead, that should be done through Windows itself using mechanisms like &lt;a href="http://technet.microsoft.com/en-us/library/cc779607(WS.10).aspx"&gt;SRP&lt;/a&gt;, which apply equally to both managed and native applications.&lt;/p&gt;  &lt;p&gt;In the v3.5 SP1 release, these observations (writing managed code to use on the network was harder than it needed to be, and it wasn’t even buying any extra security) &lt;a href="http://blogs.msdn.com/shawnfa/archive/2008/05/12/fulltrust-on-the-localintranet.aspx"&gt;led us to relax CAS policy for LocalIntranet applications slightly&lt;/a&gt;.&amp;#160;&amp;#160; We enabled applications that were run directly from an intranet share (and any assemblies loaded from immediately next to that application) to be fully trusted by pretending that it had MyComputer zone evidence instead of LocalIntranet.&lt;/p&gt;  &lt;p&gt;In the v4.0 release of the runtime, the CLR has taken that a step further.&amp;#160; &lt;strong&gt;&lt;em&gt;By default, unhosted applications are not subject to managed security policy when run under v4.0.&lt;/em&gt;&lt;/strong&gt;&amp;#160;&amp;#160; Effectively, this means any managed application that you launch from the command prompt or by double clicking the .exe in Windows Explorer will run fully trusted, as will all of the assemblies that it loads (including assemblies that it loads from a location other than the the directory where the executable lives).&lt;/p&gt;  &lt;p&gt;For applications run from the local machine, there really should be no observable change.&amp;#160; However, for applications that are shared out over a network, this means that everything should just work – just as if you had run the application from your computer while you were developing it.&lt;/p&gt;  &lt;p&gt;One very important point about this change is that it specifically applies only to unhosted code.&amp;#160; In my next post, we’ll look at what v4.0 security policy means for CLR hosts.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9634177" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx">Policy</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx">CLR v4</category></item><item><title>.NET 4.0 Security</title><link>http://blogs.msdn.com/shawnfa/archive/2009/05/20/net-4-0-security.aspx</link><pubDate>Thu, 21 May 2009 01:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9633014</guid><dc:creator>shawnfa</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/9633014.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=9633014</wfw:commentRss><description>&lt;P&gt;The first beta of the &lt;A href="http://blogs.msdn.com/somasegar/archive/2009/05/18/visual-studio-2010-and-net-fx-4-beta-1-ships.aspx" mce_href="http://blogs.msdn.com/somasegar/archive/2009/05/18/visual-studio-2010-and-net-fx-4-beta-1-ships.aspx"&gt;v4.0 .NET Framework is now available&lt;/A&gt;, and with it comes a lot of changes to the CLR's security system.&amp;nbsp; We've updated both the policy and enforcement portions of the runtime in a lot of ways that I'm pretty excited to finally see available.&amp;nbsp; Since there are a lot of security changes, I'll spend the next month or so taking a deeper look at each of them.&amp;nbsp; At a high level, the major areas that are seeing updates with the v4 CLR are:&amp;nbsp; &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/06/12/clr-v4-security-policy-roundup.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/06/12/clr-v4-security-policy-roundup.aspx"&gt;Security policy&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Security transparency&lt;/LI&gt;
&lt;LI&gt;APTCA&lt;/LI&gt;
&lt;LI&gt;Evidence&lt;/LI&gt;
&lt;LI&gt;AppDomain Managers&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Like I did when we shipped the v2.0 CLR, I'll come back and update this post with links to the details about each of the features we added as I write more detailed blog posts about each of them.&lt;/P&gt;
&lt;P&gt;Tomorrow, I'll start by looking at probably the most visible change of the group - the update to the CLR's security policy system.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9633014" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx">CLR v4</category></item><item><title>FullTrust on the LocalIntranet</title><link>http://blogs.msdn.com/shawnfa/archive/2008/05/12/fulltrust-on-the-localintranet.aspx</link><pubDate>Mon, 12 May 2008 20:49:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8495622</guid><dc:creator>shawnfa</dc:creator><slash:comments>13</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/8495622.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=8495622</wfw:commentRss><description>&lt;p&gt;We released the &lt;a href="http://blogs.msdn.com/somasegar/archive/2008/05/12/visual-studio-2008-and-net-fx-3-5-sp1-beta-available-now.aspx"&gt;first beta of .NET 3.5 SP 1&lt;/a&gt; this morning, and it includes a change to the default grant set for applications launched from the LocalIntranet zone.&amp;nbsp; The quick summary is that as of .NET 3.5 SP1, applications run from a network share will receive a grant set of FullTrust by default, making them act the same as if they were launched off of your computer directly.&amp;nbsp; Since this is an issue that &lt;a href="http://blogs.msdn.com/shawnfa/archive/2004/12/30/344554.aspx"&gt;I know a lot of people run into&lt;/a&gt;, I hope that this change makes it easier to use and deploy managed applications.&amp;nbsp; For people who want to keep their machines working the same as they did for previous .NET Framework releases, you can set the DWORD registry value LegacyMyComputerZone to 1 in the HKLM\Software\Microsoft\.NETFramework registry key.&lt;/p&gt; &lt;p&gt;With the high-level summary out of the way, let's take a look under the hood to see what changed to make this possible :-)&lt;/p&gt; &lt;p&gt;The core of this change is a modification in how we assign evidence to network launched applications.&amp;nbsp; When we see an .exe launched directly off a network share, rather than giving that .exe Zone evidence of LocalInranet, we instead give the .exe Zone evidence of MyComputer.&amp;nbsp; This causes the .exe to match the default MyComputer code group rather than the LocalIntranet group, and in default CAS policy that code group grants FullTrust.&amp;nbsp;&amp;nbsp; (This also explains why the opt-out registry value is named LegacyMyComputerZone)&lt;/p&gt; &lt;p&gt;In addition to the entry point .exe of the application, we'll also extend this MyComputer evidence to any assembly loaded from the same directory as the .exe.&amp;nbsp; So, if you place any managed .dll's immediately next to your .exe, those will also all be given FullTrust by default in .NET 3.5 SP1.&lt;/p&gt; &lt;p&gt;Since we're only including assemblies loaded from the same directory as the entry point application, things will just work for most applications, however more complicated programs that need to load assemblies from different subdirectories or other network shares may not see all of their assemblies get fully trusted by default.&amp;nbsp; For these more types of applications, ClickOnce deployment is the recommended way to elevate to FullTrust.&lt;/p&gt; &lt;p&gt;We've specifically disabled this change for any hosted applications.&amp;nbsp; So this means that if your program uses the CorBindToRuntime API to host the CLR, we won't start trusting assemblies it loads from a share.&amp;nbsp; Similarly, hosts like Internet Explorer will not start trusting controls that it loads into the browser.&lt;/p&gt; &lt;p&gt;To summarize the under the hood changes, assemblies which will now receive Zone evidence of MyComputer and therefore be fully trusted by default are:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Any managed .exe which is launched directly from a network share&lt;/li&gt; &lt;li&gt;Any assembly in that .exe's process which is loaded from the same directory as the .exe itself was.&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Assemblies which will not see this change include:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Assemblies loaded from a subdirectory of the share where the .exe was launched from&lt;/li&gt; &lt;li&gt;Assemblies loaded from shares other than the one where the main .exe was launched&lt;/li&gt; &lt;li&gt;Any assembly loaded on a machine with the LegacyMyComputer registry value set to 1&lt;/li&gt; &lt;li&gt;Any assembly loaded into a CLR host, including assemblies loaded into Internet Explorer as controls.&lt;/li&gt; &lt;li&gt;Any assembly loaded from shares by an application that was launched from the "real" MyComputer zone.&lt;/li&gt;&lt;/ol&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8495622" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/ClickOnce/default.aspx">ClickOnce</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx">Policy</category></item><item><title>CAS and Native Code</title><link>http://blogs.msdn.com/shawnfa/archive/2008/03/04/cas-and-native-code.aspx</link><pubDate>Tue, 04 Mar 2008 21:27:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8031807</guid><dc:creator>shawnfa</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/8031807.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=8031807</wfw:commentRss><description>&lt;p&gt;CAS is complicated enough to understand when all of the moving parts are written in managed code (and therefore have all the associated managed meta-information like grant sets, etc).&amp;nbsp; However, once native code comes into play things can get even more confusing.&amp;nbsp; Let's take a look at how CAS works when there's native code on the call stack.&lt;/p&gt; &lt;p&gt;For this discussion, lets assume we have a call stack (growing down) like so:&lt;/p&gt; &lt;p&gt; &lt;div style="border-right: black thin inset; padding-right: 0.5em; border-top: black thin inset; padding-left: 0.5em; padding-bottom: 0.5em; margin: 0.5em; border-left: black thin inset; padding-top: 0.5em; border-bottom: black thin inset"&gt; &lt;div style="border-right: black thin inset; padding-right: 0.5em; border-top: black thin inset; padding-left: 0.5em; padding-bottom: 0.5em; margin: 0.5em; border-left: black thin inset; padding-top: 0.5em; border-bottom: black thin inset"&gt;Managed code: M1&lt;/div&gt; &lt;div style="border-right: black thin inset; padding-right: 0.5em; border-top: black thin inset; padding-left: 0.5em; padding-bottom: 0.5em; margin: 0.5em; border-left: black thin inset; padding-top: 0.5em; border-bottom: black thin inset"&gt;Managed code: M2&lt;/div&gt; &lt;div style="border-right: black thin inset; padding-right: 0.5em; border-top: black thin inset; padding-left: 0.5em; padding-bottom: 0.5em; margin: 0.5em; border-left: black thin inset; padding-top: 0.5em; border-bottom: black thin inset"&gt;Native code: N1&lt;/div&gt; &lt;div style="border-right: black thin inset; padding-right: 0.5em; border-top: black thin inset; padding-left: 0.5em; padding-bottom: 0.5em; margin: 0.5em; border-left: black thin inset; padding-top: 0.5em; border-bottom: black thin inset"&gt;Managed code: M3&lt;/div&gt; &lt;div style="border-right: black thin inset; padding-right: 0.5em; border-top: black thin inset; padding-left: 0.5em; padding-bottom: 0.5em; margin: 0.5em; border-left: black thin inset; padding-top: 0.5em; border-bottom: black thin inset"&gt;Managed code: M4&lt;/div&gt;AppDomain: AD&lt;/div&gt; &lt;p&gt;So in this example, M1 calls M2, which calls N1, which calls back to managed code M3 and M4.&amp;nbsp; All of the managed objects live in AppDomain AD.&lt;/p&gt; &lt;h2&gt;&lt;/h2&gt; &lt;h2&gt;Full Demands&lt;/h2&gt; &lt;p&gt;Now lets consider what happens when M4 triggers a full stack walk for a demand.&amp;nbsp; In this case, the CLR essentially ignores the native code on the stack, and does a stack walk looking at M3, M2, M1, and AD.&amp;nbsp; Intuitively, this is done because the native code doesn't have a grant set associated with it, and therefore including it on a CAS stack walk wouldn't make much sense.&lt;/p&gt; &lt;p&gt;It's important to note however that the stack walk proceeds &lt;em&gt;through&lt;/em&gt; N1, rather than stopping at it.&amp;nbsp; So in this case if M3 and M2 are fully trusted, but M1 is partial trust and M4 triggered a FullTrust demand, that demand would fail.&lt;/p&gt; &lt;p&gt;Stack walk modifiers like Assert continue to work just as you would expect as well, so it's perfectly legal for M2 to Assert FullTrust, which would make M4's demand succeed.&lt;/p&gt; &lt;h2&gt;&lt;/h2&gt; &lt;h2&gt;Link Demands&lt;/h2&gt; &lt;p&gt;Link demands are much more interesting when native code gets involved.&amp;nbsp; In our example, imagine that managed method M3 has a LinkDemand for FullTrust on it.&amp;nbsp; Normally, LinkDemands are evaluated at JIT time against the caller of the method with the demand.&amp;nbsp; But in this case, the caller of M3 is native code which is not JITed (and doesn't have a grant set to evaluate against in any event).&lt;/p&gt; &lt;p&gt;The obvious solution then is to evaluate the LinkDemand against the last managed frame on the call stack before we transitioned to native code.&amp;nbsp; At the time we're JITing this method however, we only know that it's calling a native method -- we don't know what managed code that native code is going to turn around and call later on.&amp;nbsp; In this example, when M2 is being JITed we know that it may call N1, however we don't know that N1 will turn around and call M3 and therefore don't know that M3 has as a LinkDemand to evaluate against M2.&lt;/p&gt; &lt;p&gt;However, it turns out that we don't need to know what M3 is going to LinkDemand of M2.&amp;nbsp; Since M2 is calling N1, it must have UnmanagedCode permission, and since UnmanagedCode permission is never handed out without FullTrust, we can reason that M2 must be fully trusted.&amp;nbsp; If M2 is fully trusted, then it will satisfy any LinkDemand that M3 will require.&lt;/p&gt; &lt;p&gt;With this analysis, we could say that LinkDemands are implicitly satisfied by native code callers.&amp;nbsp; However, that may not be the effect that we want in all cases.&amp;nbsp; For instance, one common scenario might be to expose a managed object via COM Interop to a script.&amp;nbsp; (For instance, make a managed object available via COM to some JavaScript in a web page).&lt;/p&gt; &lt;p&gt;From the CLR's perspective, this script is just native code, so our reasoning would lead to any link demands on methods the script calls being satisfied.&amp;nbsp; However, we may not want the script to satisfy all link demands.&amp;nbsp; In order to solve this problem, the CLR treats calls to managed code from native via COM Interop differently.&lt;/p&gt; &lt;p&gt;If native code uses a COM interface to call a managed method protected with a LinkDemand, then that LinkDemand is evaluated against the AppDomain which the managed object lives in.&lt;/p&gt; &lt;p&gt;Let's go back to our example again, and see how these rules apply.&amp;nbsp; Let's suppose that N1 calls M3 through COM Interop, M3 has a LinkDemand for FullTrust, and the AppDomain AD is also fully trusted.&amp;nbsp; In this case, the call succeeds because the AppDomain's grant set satisfies M3's LinkDemand.&lt;/p&gt; &lt;p&gt;Now let's consider the case where this AppDomain is hosted by Internet Explorer, and therefore has only the Internet grant set.&amp;nbsp; When N1 calls into M3, we'll see that the object M3 is being called on lives in AppDomain AD, and that causes us to look up the grant set of that domain.&amp;nbsp; We then check M3's LinkDemand for FullTrust against the AppDomain's grant set of Internet.&amp;nbsp; Since the AppDomain's grant set does not satisfy the LinkDemand, we throw a SecurityException.&amp;nbsp; Note that this is true even if the last managed frame on the stack (M2) is fully trusted.&lt;/p&gt; &lt;p&gt;If we make one more change to the scenario, and have N1 call M3 via reverse P/Invoke we a different result.&amp;nbsp; Even though the AppDomain is partially trusted, since M3 was not invoked from COM Interop, we allow the call to succeed.&lt;/p&gt; &lt;h2&gt;3 Rules for Native Code CAS Evaluation&lt;/h2&gt; &lt;p&gt;That's a ton of complexity, but thankfully we can boil it down to three rules depending upon your scenario:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;If a full demand is done, native stack frames are ignored and the stack walk proceeds exactly as if there were only managed frames on the stack.&lt;/li&gt; &lt;li&gt;If a link demand is done via COM Interop, the link demand is evaluated against the grant set of the AppDomain that the managed object lives in.&lt;/li&gt; &lt;li&gt;If a link demand is done via reverse P/Invoke, the link demand is satisfied and the call succeeds. &lt;/li&gt;&lt;/ol&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8031807" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Under+the+Hood/default.aspx">Under the Hood</category></item><item><title>Manifested Controls Redux</title><link>http://blogs.msdn.com/shawnfa/archive/2008/01/24/manifested-controls-redux.aspx</link><pubDate>Thu, 24 Jan 2008 21:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7224860</guid><dc:creator>shawnfa</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/7224860.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=7224860</wfw:commentRss><description>&lt;P&gt;Last year, I made a &lt;A href="http://blogs.msdn.com/shawnfa/archive/2007/03/07/specifying-permissions-for-ie-controls-in-orcas.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2007/03/07/specifying-permissions-for-ie-controls-in-orcas.aspx"&gt;series&lt;/A&gt; &lt;A href="http://blogs.msdn.com/shawnfa/archive/2007/03/12/tying-your-ie-hosted-control-to-a-manifest.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2007/03/12/tying-your-ie-hosted-control-to-a-manifest.aspx"&gt;of&lt;/A&gt; &lt;A href="http://blogs.msdn.com/shawnfa/archive/2007/03/09/manifests-for-ie-hosted-controls.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2007/03/09/manifests-for-ie-hosted-controls.aspx"&gt;posts&lt;/A&gt; about a new feature available in the betas of .NET 3.5 which enabled you to specify declaratively the set of permissions that IE hosted managed controls should run with.&amp;nbsp; Since the betas there have been a couple of tweaks to the manifest control model, so those posts need a refresh.&lt;/P&gt;
&lt;P&gt;Most notably, the Low Safety (Unrestricted) setting for the Permissions for Components with Manifests URL action is not a part of the final shipping Orcas bits.&amp;nbsp; Instead, the two options are:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;EM&gt;High Safety&lt;/EM&gt; - manifested controls can run with the permissions it requests, but only if those permissions are a subset of the permissions it would have been granted by CAS policy or if the manifests are signed by a trusted publisher.&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;Disabled&lt;/EM&gt; - manifested controls may not run at all.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;If you're using a machine that had one of the .NET 3.5 betas on it, the Low Safety option will still appear in your Internet Explorer dialog box, however the CLR will treat a value of Low Safety as if it were Disabled.&lt;/P&gt;
&lt;P&gt;A lot of times when people look at this feature, they would like a full end-to-end sample of a control in a web page taking advantage of a manifest to elevate its permissions.&amp;nbsp; I've attached a ZIP file containing a sample control to this post.&lt;/P&gt;
&lt;P&gt;In order to use this sample:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a ManifestControl subdirectory in your wwwroot. 
&lt;LI&gt;Copy ManifestControl.control, ManifestControl.dll, ManifestControl.dll.manifest, and ManifestControl.html to the ManifestControl directory created in step 1. 
&lt;LI&gt;Ensure that your web server is setup to allow downloading of .dll, .control, and .dll.manifest files. 
&lt;LI&gt;Install ManifestControl.cer in your Trusted Publishers certificate store. 
&lt;LI&gt;Install ManifestControl.cer in your Trusted Root Certification Authorities certificate store. (&lt;STRONG&gt;Once you are done with the sample, the test certificate should be removed from both of these certificate stores&lt;/STRONG&gt;) 
&lt;LI&gt;Navigate Internet Explorer to &lt;A href="http://localhost/ManifestControl/ManifestControl.html" mce_href="http://localhost/ManifestControl/ManifestControl.html"&gt;http://localhost/ManifestControl/ManifestControl.html&lt;/A&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7224860" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/shawnfa/attachment/7224860.ashx" length="20562" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx">Policy</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Orcas/default.aspx">Orcas</category></item><item><title>Transparency as Least Privilege</title><link>http://blogs.msdn.com/shawnfa/archive/2007/10/30/transparency-as-least-privilege.aspx</link><pubDate>Tue, 30 Oct 2007 21:35:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5784462</guid><dc:creator>shawnfa</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/5784462.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=5784462</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/shawnfa/archive/2007/10/02/avoiding-assembly-level-declarative-security.aspx"&gt;In my last post I mentioned that there is a better alternative to RequestRefuse for achieving least privilege&lt;/a&gt;. The tool I like to use for least privilege is actually the security transparency model available in v2.0+ of the CLR (and which became the basis of the &lt;a href="http://blogs.msdn.com/shawnfa/archive/2007/05/14/silverlight-security-cheat-sheet.aspx"&gt;Silverlight security model&lt;/a&gt;).&lt;/p&gt; &lt;p&gt;On the desktop CLR, transparent code cannot elevate the privileges of the call stack in any way.&amp;nbsp; Let's take a quick look at how this is enforced:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;strong&gt;Uverifiable code&lt;/strong&gt; - if a transparent method contains unverifiable code, the CLR injects a demand for SecurityPermission/UnmanagedCode.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Satisfy a LinkDemand&lt;/strong&gt; - LinkDemands satisfied by a transparent method are converted into full demands&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Use the SuppressUnmanagedCodeAttribute &lt;/strong&gt;- this is really an extension of the LinkDemand rule.&amp;nbsp; the SuppressUnmanagedCodeAttribute converts the full demand for SecurityPermission/UnmanagedCode into a LinkDemand for the same permission.&amp;nbsp; However, since link demands satisfied by transparent code are converted into full demands, the net effect is that SuppressUnmanagedCodeAttribute becomes a no-op.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Assert permissions&lt;/strong&gt; - Asserts are an explicit attempt to elevate the permissions of the call stack, and are therefore disallowed in transparent code.&amp;nbsp; They are not converted into full demands&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Call critical code without going through a TreatAsSafe layer &lt;/strong&gt;- the treat as safe layer is responsible for validating inputs and outputs of critical code, and transparent code must access critical code via this layer.&amp;nbsp; Attempts to call critical code or access critical data directly are disallowed.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;This effectively means that fully trusted transparent code runs "as caller".&amp;nbsp; It won't cause any security demands to fail, however it won't cause them to succeed either. If the caller of the transparent code is allowed to do the security operation it will succeed, otherwise it fails.&lt;/p&gt; &lt;p&gt;What's interesting is that if we take this principal to the extreme, and have the entire call stack be transparent (such as in Silverlight), transparency transitions from running "as caller" to running "as application".&amp;nbsp; This is because each level in the transparency call chain passes the security demand up to its caller, until we hit the top of the stack -- and find the AppDomain.&amp;nbsp; Since all security demands end up going against the AppDomain's permission set, that permission set alone controls what may happen within the domain.&lt;/p&gt; &lt;p&gt;In this way, my fully trusted code will be fully trusted if it is run within a FullTrust domain.&amp;nbsp; However, that same code run within an AppDomain with the Internet permission set (perhaps if it's being used by a control in the browser), will effectively be running with Internet permissions.&amp;nbsp; Any demand for a permission outside of the Internet permission set will be passed along until it hits the AppDomain and fail.&lt;/p&gt; &lt;p&gt;Let's say that you're writing an application that you know will never need to interact with native code.&amp;nbsp; You'd normally do a RequestRefuse for UnmanagedCode permission on your application's assemblies to ensure that it's never accidentally satisfying an UnmanagedCode demand.&amp;nbsp; Instead of doing that, you can make sure that your application assemblies are entirely transparent and that they are run in a domain without UnmanagedCode permission -- perhaps via ClickOnce deployment.&lt;/p&gt; &lt;p&gt;This way you can use ClickOnce to explicitly control the grant set of the applicaiton, and transparency to ensure that you're not accidentally causing an elevation outside of that call stack, even if some of your application is installed in the GAC and fully trusted.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5784462" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category></item><item><title>Avoiding Assembly Level Declarative Security</title><link>http://blogs.msdn.com/shawnfa/archive/2007/10/02/avoiding-assembly-level-declarative-security.aspx</link><pubDate>Tue, 02 Oct 2007 19:24:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5244758</guid><dc:creator>shawnfa</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/shawnfa/comments/5244758.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shawnfa/commentrss.aspx?PostID=5244758</wfw:commentRss><description>&lt;p&gt;I've written in the past about &lt;a href="http://blogs.msdn.com/shawnfa/archive/2004/08/30/222918.aspx"&gt;the three assembly level declarative security actions&lt;/a&gt;: RequestMinimum, RequestOptional, and RequestRefuse.&amp;nbsp; Although the CLR has supported these since v1.0, I tend to stay away from using them as much as I possibly can, and also recommend that others avoid them as well.&amp;nbsp; Let me go through each one individually:&lt;/p&gt; &lt;h2&gt;RequestMinimum&lt;/h2&gt; &lt;p&gt;RequestMinimum is the most common of the three, and in fact is &lt;a href="http://msdn2.microsoft.com/en-us/library/ms182325(VS.80).aspx"&gt;mentioned by a FxCop rule&lt;/a&gt; and automatically inserted by the C# compiler in some cases.&amp;nbsp; There are several reasons I don't like to use this security action.&lt;/p&gt; &lt;p&gt;The first problem I have with RequestMinimum is a usability problem stemming from the fact that if&amp;nbsp;your assembly is not granted its RequestMinimum permission set, the CLR will refuse to load it by throwing an exception.&amp;nbsp; If the assembly in question happens to be the entry point of my application, that means the user of my app ends up&amp;nbsp;staring down an unhandled exception dialog (since my&amp;nbsp;code never got loaded, and therefore never had a chance to handle this error more&amp;nbsp;gracefully).&amp;nbsp; If my mother runs an app I give her and it throws a FileLoadException due to insufficient permissions, she's not going to have any idea what to do with that.&lt;/p&gt; &lt;p&gt;Even if the assembly is not the entry point assembly, gracefully handling failure to load due to RequestMinimum means that an application needs a try ... catch around every code path which could potentially lead to loading the assembly in question; that's not something that a lot of applications are setup to do.&lt;/p&gt; &lt;p&gt;The other problem I find with RequestMinimum is that in the case of writing a shared library it's very difficult to figure out what the correct grant set to request is.&amp;nbsp; Unless all of the APIs being exposed require the same set of permissions, having only a single RequestMinimum set doesn't help much.&amp;nbsp; For instance, an assembly like System.dll exposes hundreds of classes each with indivdual permission requirements varying from SecurityPermission/Execution to FullTrust.&amp;nbsp; If we decided to use the minimum grant set of any API in the assembly as the ReuqestMinimum, we've somewhat defeated the purpose of the security action; now I cannot be assured that the assembly loading means that it has the required permissions to work in all cases.&amp;nbsp; On the other hand, if I use the maximum grant set required, the assembly won't load even if an application was only intending to use APIs that would work in the grant set that the assembly is loaded into.&lt;/p&gt; &lt;p&gt;Finally, since security demands go against the entire call stack, even if the assembly in question has all the permissions that it needs to run doesn't mean that all assemblies and AppDomains on the call stack will have those permissions.&amp;nbsp; So even if an assembly does have its minimum grant set met, it doesn't have any guarantee that its APIs will all work without throwing a SecurityException.&lt;/p&gt; &lt;h2&gt;RequestOptional&lt;/h2&gt; &lt;p&gt;RequestOptional is probably the least commonly used assembly level security action, and with good reason.&amp;nbsp; This security action is somewhat poorly named, and people who use it for the first time often don't realize that &lt;a href="http://blogs.msdn.com/shawnfa/archive/2005/09/14/466661.aspx"&gt;it will actually remove permissions from your grant set&lt;/a&gt;.&amp;nbsp; In fact, I run across RequestOptional most commonly when helping someone debug a SecurityException that they can't figure out.&amp;nbsp; ("Why does this exception&amp;nbsp;occur? All assemblies should be fully trusted!")&lt;/p&gt; &lt;p&gt;Even once you do understand the full effect of RequestOptional, the fact that it is confusingly named and that most people do not understand it is argument enough for me&amp;nbsp;to stay away.&amp;nbsp; Code readability is extremely important, and if I can expect that the next person to read through my code will be confused by that RequestOptional I stuck on my assembly, that's not a good thing -- especially when it comes to security.&lt;/p&gt; &lt;h2&gt;RequestRefuse&lt;/h2&gt; &lt;p&gt;I expect this is the most controversial security action to take a stance against.&amp;nbsp; A good number of people probably use RequestRefuse as a least-privilege mechanism.&amp;nbsp; My problem with that is that you're stating "I don't want permissions A and B", however when someone comes along and introduces permission C you haven't refused it, and therefore you still get it.&amp;nbsp; Personally, I believe the CLR has other, much better, least-privilege mechanisms available -- and I prefer to use those whenever possible.&amp;nbsp; (Sounds like the topic of a future blog post :-) ).&lt;/p&gt; &lt;p&gt;Finally, and this should come as no surprise to any regular visitors, &lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx"&gt;I'm a huge fan of the model where every sandboxed AppDomain has exactly two permission sets -- FullTrust and one sandbox grant set&lt;/a&gt;.&amp;nbsp; RequestRefuse (and RequestOptional) both detract from this model by creating multiple permission sets within an otherwise homogenous AppDomain.&amp;nbsp; By keeping AppDomains entirely homogenous, it's much easier to reason about security within the domain.&amp;nbsp; And, in my opinion, keeping&amp;nbsp;the security model as simple as possible&amp;nbsp;can only be goodness -- complexity can lead to&amp;nbsp;mistakes in reasoning, which&amp;nbsp;leads to security holes.&amp;nbsp;&lt;/p&gt; &lt;h2&gt;So What Instead?&lt;/h2&gt; &lt;p&gt;Some of the abilities promised by the assembly level declarative security actions seem to be very useful at first glance.&amp;nbsp; Notably the ability to specify a grant set that the code was tested to run in (which is what RequestMinimum seems to promise) and the ability to run with least privilege (via RequestRefuse).&lt;/p&gt; &lt;p&gt;For RequestMinimum, I think that ClickOnce is a far superior alternative for a variety of reasons.&amp;nbsp; Most importantly, ClickOnce gives the user of the application a nicer experience when the application does not by default meet the minimum grant set.&amp;nbsp; Rather than throwing an exception in the face of a non-developer, a friendly user interface is displayed which may even offer the ability to grant the application the ability to run.&lt;/p&gt; &lt;p&gt;Also, the grant set specified for a ClickOnce application applies to all of the assemblies in the application as well as the AppDomain&amp;nbsp; (hey -- it's my favorite homogenous model again!).&amp;nbsp; This means that you don't have to worry about the scenario where your assembly meets its minimum grant set, but some APIs still fail with SecurityExceptions because another assembly further up the call stack did not meet the minimum grant set.&lt;/p&gt; &lt;p&gt;That covers RequestMinimum -- but what about the least privilege promise of RequestRefuse?&amp;nbsp; As AB might say -- that's another show.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5244758" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/ClickOnce/default.aspx">ClickOnce</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx">CAS</category><category domain="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx">Policy</category></item></channel></rss>