<?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>SafeHandle: A Reliability Case Study [Brian Grunkemeyer]</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx</link><description>SafeHandle is the best way to represent handles in managed code today. For a high-level overview of what SafeHandle does, Ravi posted a writeup on the BCL blog titled SafeHandles: the best v2.0 feature of the .NET Framework . However, if anyone wants</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Brian Talks About SafeHandles</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#397024</link><pubDate>Wed, 16 Mar 2005 22:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:397024</guid><dc:creator>Chris Lyon's WebLog</dc:creator><description /></item><item><title>Tips </title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#398163</link><pubDate>Thu, 17 Mar 2005 22:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:398163</guid><dc:creator>David Boschmans' Weblog</dc:creator><description>Tips </description></item><item><title>Brian Talks About SafeHandles</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#398336</link><pubDate>Fri, 18 Mar 2005 02:50:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:398336</guid><dc:creator>Chris Lyon's WebLog</dc:creator><description /></item><item><title>Tips </title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#399826</link><pubDate>Mon, 21 Mar 2005 19:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:399826</guid><dc:creator>Ido Samuelson</dc:creator><description>Tips </description></item><item><title>re: SafeHandle: A Reliability Case Study [Brian Grunkemeyer]</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#400152</link><pubDate>Tue, 22 Mar 2005 04:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400152</guid><dc:creator>John Lyon-Smith</dc:creator><description>Great blog Brian.  The best I've read on SafeHandles so far.  At this point I need to put my hand up and ask an unanswered question.  I presume that the safeness of SafeHandles derives from the fact that the handle in question is completely used via the SafeHandle and managed code?  For example, if an unmanaged thread happens to &amp;quot;sneak into your process&amp;quot; and it gets hold of an O/S handle (which is wrapped in a SafeHandle), can it can simply call CloseHandle and corrupt data and/or launch a handle recycling attack?  Unless you are hooking the O/S calls at the DLL entry points there wouldn't appear to be any way to stop that. </description></item><item><title>re: SafeHandle: A Reliability Case Study [Brian Grunkemeyer]</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#400163</link><pubDate>Tue, 22 Mar 2005 05:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400163</guid><dc:creator>Brian Grunkemeyer</dc:creator><description>Thanks. Yes, your concern is correct. The safety in SafeHandle is only guaranteed if the SafeHandle controls the lifetime of the handle exclusively. We do all our work with a ref count on the handle, incrementing it when we start marshaling the handle to a P/Invoke call, and decrementing it when the P/Invoke method returns, or you call Dispose on the SafeHandle. (We also designed critical finalization for SafeHandles to guarantee they get cleaned up during an appdomain unload, even if we give up running normal finalizers because one blocked for a long time.) &lt;br&gt;&lt;br&gt;There are at least two ways SafeHandle can be compromised. The first is another thread (managed or unmanaged) that starts randomly guessing handle values. If another thread either has a dangling handle to a previously closed kernel object, or decides that 0xc40 is a good random number and passes that to CloseHandle(), and you have a SafeHandle that happens to have that number in it as a handle, then the handle is closed and you're open to handle recycling attacks. So as an application writer, you do have to worry about the safety &amp;amp; correctness of other code in your process. &lt;br&gt;&lt;br&gt;Checking the return value of CloseHandle() is a good way to help track this down, and SafeHandle's ReleaseHandle() method returns a bool for this purpose. Under a native debugger, Win32's CloseHandle will hit a breakpoint if you call CloseHandle with an invalid handle. With SafeHandle, we have an MDA that, when enabled, will fire whenever SafeHandle's ReleaseHandle method returns false. This allows us to track down failures in code that doesn't use CloseHandle, etc.&lt;br&gt;&lt;br&gt;The second way to circumvent SafeHandle is by using its DangerousGetHandle method without calling DangerousAddRef &amp;amp; DangerousRelease. For interoperability reasons, a few Framework classes exposed the handle to the user as an acknowledgement that perhaps this type didn't represent the full set of operations you want to do on this resource. So to ensure compatibility, we had to add in a mechanism to get a handle out of the SafeHandle. In those scenarios, frankly you're on your own, and we've made those methods require UnmanagedCode permission. But it isn't ideal, of course. &lt;br&gt;&lt;br&gt;If you must pull a value out of a SafeHandle for some reason (note there are a few corner cases involving marshaling of SafeHandles in structs that aren't fully supported), you should use a constrained execution region to prevent the runtime from introducing failure points in your backout code. (Search around for CER's on the web - I haven't written something on them yet.) Here's an example of what you should write: &lt;br&gt;&lt;br&gt;bool mustRelease = false; &lt;br&gt;RuntimeHelpers.PrepareConstrainedRegions(); &lt;br&gt;try { &lt;br&gt;sh.DangerousAddRef(ref mustRelease); &lt;br&gt;IntPtr handle = sh.DangerousGetHandle(); &lt;br&gt;// Do something with the handle here &lt;br&gt;} &lt;br&gt;finally { &lt;br&gt;// You can only write &amp;quot;reliable&amp;quot; code here.&lt;br&gt;if (mustRelease) &lt;br&gt;sh.DangerousRelease(); &lt;br&gt;} &lt;br&gt;</description></item><item><title>The Worst of the .NET 1.x Years</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#400761</link><pubDate>Wed, 23 Mar 2005 05:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400761</guid><dc:creator>K. Scott Allen</dc:creator><description /></item><item><title>Safe Impersonation With Whidbey</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#401918</link><pubDate>Fri, 25 Mar 2005 00:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:401918</guid><dc:creator>.Net Security Blog</dc:creator><description /></item><item><title>Blog link of the week 24</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#430630</link><pubDate>Mon, 20 Jun 2005 00:56:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:430630</guid><dc:creator>Daniel Moth</dc:creator><description>Blog link of the week 24</description></item><item><title>Constrained Execution Regions and other errata [Brian Grunkemeyer]</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#433087</link><pubDate>Mon, 27 Jun 2005 23:42:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:433087</guid><dc:creator>BCLTeam's WebLog</dc:creator><description>A customer recently asked a good question about some of our new reliability features in Whidbey:&lt;br&gt;&lt;br&gt;There...</description></item><item><title>Unmanaged Interop chapter</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#437394</link><pubDate>Mon, 11 Jul 2005 08:05:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:437394</guid><dc:creator>`(joe (@ (version "2.0")) ,(mk-blog))</dc:creator><description /></item><item><title>Unmanaged Interop chapter</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#437397</link><pubDate>Mon, 11 Jul 2005 08:07:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:437397</guid><dc:creator>`(joe (@ (version "2.0")) ,(mk-blog))</dc:creator><description /></item><item><title>Unmanaged Interop chapter</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#437400</link><pubDate>Mon, 11 Jul 2005 08:09:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:437400</guid><dc:creator>`(joe (@ (version "2.0")) ,(mk-blog))</dc:creator><description /></item><item><title>Unmanaged Interop chapter</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#437423</link><pubDate>Mon, 11 Jul 2005 10:13:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:437423</guid><dc:creator>`(joe (@ (version "2.0")) ,(mk-blog))</dc:creator><description /></item><item><title>SafeHandle</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#452119</link><pubDate>Tue, 16 Aug 2005 15:49:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:452119</guid><dc:creator>notgartner.com: Mitch Denny's Blog</dc:creator><description /></item><item><title>Rotor (SSCLI) 2.0 is available!</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#560991</link><pubDate>Sun, 26 Mar 2006 00:01:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:560991</guid><dc:creator>Generalities &amp; Details: Adventures in the High-tech Underbelly</dc:creator><description /></item><item><title>Converting System.Threading.WaitHandle.Handle to System.Threading.WaitHandle.SafeWaitHandle</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#644302</link><pubDate>Fri, 23 Jun 2006 17:49:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:644302</guid><dc:creator>I want some Moore!</dc:creator><description /></item><item><title>How to use SafeHandle in a Resilient Library [Brian Grunkemeyer]</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#644344</link><pubDate>Fri, 23 Jun 2006 18:20:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:644344</guid><dc:creator>BCLTeam's WebLog</dc:creator><description>SafeHandle is the preferred mechanism for controlling the lifetime of an OS resource (such as a handle...</description></item><item><title>CLR Behavior on OutOfMemoryExceptions [Brian Grunkemeyer]</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#835710</link><pubDate>Tue, 17 Oct 2006 21:27:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:835710</guid><dc:creator>BCLTeam's WebLog</dc:creator><description>&lt;p&gt;For out of memory exceptions, keep in mind that we can run out of memory in the native heaps in the process,&lt;/p&gt;
</description></item><item><title>Resources for geekSpeak - Garbage Collection with Jeffrey Richter</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#945888</link><pubDate>Fri, 03 Nov 2006 23:17:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:945888</guid><dc:creator>geekSpeak</dc:creator><description>&lt;p&gt;Thanks again to Jeffrey Richter who gave us great insight into the garbage collector. Here are some useful&lt;/p&gt;
</description></item><item><title>Type-safe Managed wrappers for kernel32!GetProcAddress</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#1420730</link><pubDate>Sat, 06 Jan 2007 08:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1420730</guid><dc:creator>Mike Stall's .NET Debugging Blog</dc:creator><description>&lt;p&gt;Pinvoke is cool in managed code, but sometimes you need to get straight at kernel32!GetProcAddress. For&lt;/p&gt;
</description></item><item><title>Advanced .NET Debugging  &amp;raquo; Blog Archive   &amp;raquo; P/Invoke Unamanged Windows API Wrapper by Mike Stall</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#1450995</link><pubDate>Thu, 11 Jan 2007 19:19:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1450995</guid><dc:creator>Advanced .NET Debugging  » Blog Archive   » P/Invoke Unamanged Windows API Wrapper by Mike Stall</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://dotnetdebug.net/2007/01/07/pinvoke-unamanged-windows-api-wrapper-by-mike-stall/"&gt;http://dotnetdebug.net/2007/01/07/pinvoke-unamanged-windows-api-wrapper-by-mike-stall/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>   P/Invoking and Finalization &amp;raquo; Advanced .NET Debugging</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#1454489</link><pubDate>Fri, 12 Jan 2007 12:05:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1454489</guid><dc:creator>   P/Invoking and Finalization » Advanced .NET Debugging</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://dotnetdebug.net/2005/08/07/pinvoking-and-finalization/"&gt;http://dotnetdebug.net/2005/08/07/pinvoking-and-finalization/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Long Paths in .NET, Part 2 of 3: Long Path Workarounds [Kim Hamilton]</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#1956933</link><pubDate>Tue, 27 Mar 2007 04:05:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1956933</guid><dc:creator>BCL Team Blog</dc:creator><description>&lt;p&gt;For now, our suggested workaround for users that encounter the MAX_PATH issue is to rearrange directories&lt;/p&gt;
</description></item><item><title>Long Paths in .NET, Part 2 of 3 [Kim Hamilton][译] </title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#2284770</link><pubDate>Thu, 26 Apr 2007 13:12:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2284770</guid><dc:creator>Tom's Notepad</dc:creator><description>&lt;p&gt;Long Paths in .NET, Part 2 of 3&lt;/p&gt;
</description></item><item><title>El laberinto de piedra  &amp;raquo; Blog Archive   &amp;raquo; SafeHandles</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#2459260</link><pubDate>Mon, 07 May 2007 10:26:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2459260</guid><dc:creator>El laberinto de piedra  » Blog Archive   » SafeHandles</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://arnulfoperez.com/blog/2007/05/07/safehandles/"&gt;http://arnulfoperez.com/blog/2007/05/07/safehandles/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Hopeless performance. The ADO.NET 2.0 is slower than ADO 1.1</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#2921314</link><pubDate>Sun, 27 May 2007 18:23:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2921314</guid><dc:creator>《Windows用户态程序高效排错》文章的发布，反馈站点</dc:creator><description>&lt;p&gt;1.0 Warm up. Firstly my mentor shared the following question with me: Why the image gets reversed in&lt;/p&gt;
</description></item><item><title>Soci blog  &amp;raquo; Blog Archive   &amp;raquo; SQL Server 2008 ??jdons??gok 5. - streaming adatok kezel??se kliens oldalr??l</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#6759439</link><pubDate>Thu, 13 Dec 2007 14:33:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6759439</guid><dc:creator>Soci blog  » Blog Archive   » SQL Server 2008 ??jdons??gok 5. - streaming adatok kezel??se kliens oldalr??l</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://soci.hu/blog/index.php/2007/12/13/sql-server-2008-ujdonsagok-5-streaming-adatok-kezelese-kliens-oldalrol/"&gt;http://soci.hu/blog/index.php/2007/12/13/sql-server-2008-ujdonsagok-5-streaming-adatok-kezelese-kliens-oldalrol/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title> BCL Team Blog SafeHandle A Reliability Case Study Brian Grunkemeyer | Cellulite Creams</title><link>http://blogs.msdn.com/bclteam/archive/2005/03/16/396900.aspx#9712398</link><pubDate>Tue, 09 Jun 2009 06:11:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9712398</guid><dc:creator> BCL Team Blog SafeHandle A Reliability Case Study Brian Grunkemeyer | Cellulite Creams</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://cellulitecreamsite.info/story.php?id=4242"&gt;http://cellulitecreamsite.info/story.php?id=4242&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>