<?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>Safely Impersonating Another User</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx</link><description>Yesterday I posted a bit of code that shows how to impersonate another user in managed code. However, that code had a subtle security hole waiting to bite you if you used it directly. Both Dean and Eric found the problem. In fact Eric reminded me of a</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Safely Impersonating Another User</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#400754</link><pubDate>Wed, 23 Mar 2005 03:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400754</guid><dc:creator>Maheshwar Jayaraman</dc:creator><description>I may be missing something here...&lt;br&gt;Wont the finally be called even when there was an exception and so the &lt;br&gt;impersonationContext.Undo(); will be called twice in case of an exception!&lt;br&gt;&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Safely Impersonating Another User</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#400892</link><pubDate>Wed, 23 Mar 2005 11:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400892</guid><dc:creator>Nicole Calinoiu</dc:creator><description>Isn't it a wee bit late in the game to be considering this sort of thing to be a _subtle_ security hole?  Allowing any caller code to run in the impersonation context strikes me as a severe security hole, with potential subtlety being a matter of discoverability only.  For example, if DoSomeWorkWhileImpersonating() were to use a previously assigned delegate, that might be considered subtle, but only in the sense of &amp;quot;not immediately obvious to a reviewer of the code&amp;quot;.  Failure to implement a reversion pattern that accounts for potential exceptions is not particularly difficult to discover by either well-intentioned or malign examiners of one's code, and it's often relatively easy to exploit as well...</description></item><item><title>re: Safely Impersonating Another User</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#400988</link><pubDate>Wed, 23 Mar 2005 15:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400988</guid><dc:creator>Shawn</dc:creator><description>Good catch Maheshwar -- that could would indeed double undo, and even more importantly double CloseHandle.  I've updated it to solve the problem.&lt;br&gt;&lt;br&gt;-Shawn</description></item><item><title>re: Safely Impersonating Another User</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#400989</link><pubDate>Wed, 23 Mar 2005 15:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400989</guid><dc:creator>Shawn</dc:creator><description>Nicole,&lt;br&gt;&lt;br&gt;This is a very severe hole.  You're right, the subtlety I was referring to was the ability for a code reviewer to find the hole, or the initial programmer to know it was there in the first place.  Obviously its a problem if you're using managed code or not, but I think it becomes a bit more of an issue in managed langauges since we get so used to security state being tied to the stack frame, and thus being safe from situations like this.&lt;br&gt;&lt;br&gt;The double-subtlety here is that you can get bitten by first-chance exception handling, which is something a lot of people who are C# only developers would tend to not think about either.&lt;br&gt;&lt;br&gt;-Shawn</description></item><item><title>Safe Impersonation With Whidbey</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#401907</link><pubDate>Fri, 25 Mar 2005 00:14:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:401907</guid><dc:creator>.Net Security Blog</dc:creator><description /></item><item><title>Safe Impersonation With Whidbey</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#401915</link><pubDate>Fri, 25 Mar 2005 00:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:401915</guid><dc:creator>.Net Security Blog</dc:creator><description /></item><item><title>More on First Pass Exception Issues</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#404321</link><pubDate>Thu, 31 Mar 2005 23:43:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:404321</guid><dc:creator>.Net Security Blog</dc:creator><description /></item><item><title>Windows Impersonation in ASP.NET</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#406885</link><pubDate>Sun, 10 Apr 2005 11:58:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:406885</guid><dc:creator>Pierre Greborio.NET</dc:creator><description /></item><item><title>Re: Windows Impersonation in ASP.NET</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#430698</link><pubDate>Mon, 20 Jun 2005 09:52:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:430698</guid><dc:creator>Daniel Fisher(lennybacon) on C# and .Net from Wuppertal</dc:creator><description /></item><item><title>Re: Windows Impersonation in ASP.NET</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#430699</link><pubDate>Mon, 20 Jun 2005 09:52:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:430699</guid><dc:creator>Daniel Fisher(lennybacon) on C# and .Net from Wuppertal</dc:creator><description /></item><item><title>Re: Windows Impersonation in ASP.NET</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#430700</link><pubDate>Mon, 20 Jun 2005 09:53:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:430700</guid><dc:creator>Daniel Fisher(lennybacon) on C# and .Net from Wuppertal</dc:creator><description /></item><item><title>Re: Windows Impersonation in ASP.NET</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#430701</link><pubDate>Mon, 20 Jun 2005 09:53:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:430701</guid><dc:creator>Daniel Fisher(lennybacon) on C# and .Net from Wuppertal</dc:creator><description /></item><item><title>So, What Was Wrong with That Code Anyway?</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#1228824</link><pubDate>Thu, 07 Dec 2006 06:07:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1228824</guid><dc:creator>K. Scott Allen</dc:creator><description>&lt;p&gt;WWWTC #9 ranks 10 out of 10 on the &amp;quot;difficult and subtle&amp;quot; scale. Let's say we write the following...&lt;/p&gt;
</description></item><item><title>So, What Was Wrong with That Code Anyway?</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#1229375</link><pubDate>Thu, 07 Dec 2006 07:10:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1229375</guid><dc:creator>Mirror blog entries from the industry</dc:creator><description>&lt;p&gt;WWWTC #9 ranks 10 out of 10 on the &amp;quot;difficult and subtle&amp;quot; scale. Let's say we write the following code&lt;/p&gt;
</description></item><item><title>re: Safely Impersonating Another User</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#1628624</link><pubDate>Thu, 08 Feb 2007 20:28:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1628624</guid><dc:creator>Hafthor</dc:creator><description>&lt;p&gt;Regarding IDisposable wrapping WindowsImpersonationContext -- Dispose method on WindowsImpersonationContext already calls Undo().&lt;/p&gt;
&lt;p&gt;// Declaring Type: System.Security.Principal.WindowsImpersonationContext &lt;/p&gt;
&lt;p&gt;// Assembly: mscorlib, Version=2.0.0.0 &lt;/p&gt;
&lt;p&gt;[ComVisible(false)]&lt;/p&gt;
&lt;p&gt;public void Dispose()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;this.Dispose(true);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;[ComVisible(false)]&lt;/p&gt;
&lt;p&gt;protected virtual void Dispose(bool disposing)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;if ((disposing &amp;amp;&amp;amp; (this.m_safeTokenHandle != null)) &amp;amp;&amp;amp; !this.m_safeTokenHandle.IsClosed)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;this.Undo();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;this.m_safeTokenHandle.Dispose();&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;so couldn't you do&lt;/p&gt;
&lt;p&gt;try&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;// Begin impersonating the user, relying on Dispose to call Undo&lt;/p&gt;
&lt;p&gt; &amp;nbsp;using(WindowsImpersonationContext impersonationContext = WindowsIdentity.Impersonate(userHandle.Token))&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;DoSomeWorkWhileImpersonating();&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;catch // end search for qualifying exception handler to prevent exception filter exploit&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;throw;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;</description></item><item><title>re: Safely Impersonating Another User</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#1728724</link><pubDate>Tue, 20 Feb 2007 22:22:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1728724</guid><dc:creator>shawnfa</dc:creator><description>&lt;p&gt;You're right, it does, but the second call isn't hurting anything either and it makes the intention of the code more clear to the reader.&lt;/p&gt;
&lt;p&gt;-Shawn&lt;/p&gt;
</description></item><item><title>
			Brian Low			 &amp;raquo; Impersonate User		</title><link>http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx#2213110</link><pubDate>Sat, 21 Apr 2007 02:30:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2213110</guid><dc:creator>
			Brian Low			 » Impersonate User		</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.brianlow.com/index.php/2007/04/20/impersonate-user/"&gt;http://www.brianlow.com/index.php/2007/04/20/impersonate-user/&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>