<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://keepitlocked.net/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>KeepItLocked.net : C#, Software Security</title><link>http://keepitlocked.net/archive/tags/C_2300_/Software+Security/default.aspx</link><description>Tags: C#, Software Security</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP2 (Build: 20611.960)</generator><item><title>C# and VB.NET Security Throwdown!</title><link>http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx</link><pubDate>Tue, 24 Feb 2009 17:23:00 GMT</pubDate><guid isPermaLink="false">a3f75fb5-0505-4d35-9795-aaa2ed659a71:2737</guid><dc:creator>Alex</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://keepitlocked.net/rsscomments.aspx?PostID=2737</wfw:commentRss><comments>http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx#comments</comments><description>&lt;p&gt;.NET doesn't care what language you write in - as long as your code compiles to &lt;a href="http://en.wikipedia.org/wiki/Common_Intermediate_Language" mce_href="http://en.wikipedia.org/wiki/Common_Intermediate_Language"&gt;CIL&lt;/a&gt;, it's a .NET application. There are plenty of &lt;a href="http://en.wikipedia.org/wiki/Microsoft_.NET_Languages" mce_href="http://en.wikipedia.org/wiki/Microsoft_.NET_Languages"&gt;.NET languages&lt;/a&gt; out there, but the most commonly used ones are C# and Visual Basic .NET (VB.NET). Since both languages end up as CIL before being executed, there should be zero security delta, right?&lt;/p&gt;  &lt;p&gt;It turns out that there are a few security-relevant things to consider when dealing with VB.NET versus C#. That's right, it's time for a security throwdown!&lt;/p&gt;  &lt;p&gt;1) &lt;b&gt;Exception Filters:&lt;/b&gt;&amp;nbsp;&lt;a href="http://msdn.microsoft.com/en-us/library/19d6ckx2%28VS.71%29.aspx" mce_href="http://msdn.microsoft.com/en-us/library/19d6ckx2(VS.71).aspx"&gt;Exception filters&lt;/a&gt; are a language feature supported in CIL, but can only be used from VB.NET. They allow you to perform logical checks when deciding whether to catch an exception. Several people have demonstrated that the exception filter mechanism introduces a potentially vulnerability where &lt;a href="http://www.pluralsight.com/community/blogs/keith/archive/2005/03/31/7149.aspx" mce_href="http://www.pluralsight.com/community/blogs/keith/archive/2005/03/31/7149.aspx"&gt;you can execute code while a class is in an inconsistent state&lt;/a&gt;. This happens if you have a try/finally block (no catch) that relies on code in the finally block to execute. Because the exception filter of the caller executes before the finally block, it can operate on the class when it is in an inconsistent state.&lt;/p&gt;  &lt;p&gt;While a particularly dangerous version of this vulnerability involving Windows impersonation has been &lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/03/03/542430.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2006/03/03/542430.aspx"&gt;fixed in the framework&lt;/a&gt;, you can still make mistakes here. You could either a) perform impersonation and undo in different methods, which is not addressed by the framework, b) allow untrusted code to call your trusted code in some other (non-Windows impersonation) inconsistent state, or c) make a mistake with your own exception filters and try/finally block that creates an error condition.&lt;/p&gt;  &lt;p&gt;For a), Shawn recommends that you look for places where you impersonate and undo in different methods and change them to work in the same method. Of course, you can always simply catch your own exceptions and avoid the try/finally construct when impersonating. Similarly for b), you should avoid using the try/finally block when you are doing something security relevant, and catch your own exception, even if you just rethrow them. For c), it's easy - avoid using exception filters. If you do use them, look for try/finally blocks that might be called while the exception filters are in place.&lt;/p&gt;  &lt;p&gt;While exception filters aren't just a VB.NET problem (your C# code could be vulnerable, since it can be called by code compiled from VB.NET), they may not be something the average C# developer uses in their day-to-day programming.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Winner:&lt;/b&gt; C#. While both languages are vulnerable to malicious code with exception filters, VB.NET introduced the concept and you can shoot yourself in the foot if you write bad exception handlers.&lt;/p&gt;  &lt;p&gt;2) &lt;b&gt;Option Strict: &lt;/b&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/zcd4xwzs%28VS.80%29.aspx" mce_href="http://msdn.microsoft.com/en-us/library/zcd4xwzs(VS.80).aspx"&gt;Option Strict&lt;/a&gt; is a VB.NET compiler directive that restricts data type conversions to widening conversions. It's Off by default, but usually it should be turned On to prevent subtle errors which may cause security issues. Without option strict on, you can do crazy stuff like:&lt;/p&gt;  &lt;pre&gt;Dim someVar&lt;br&gt;somVar = new Collection&lt;br&gt;o.IDoWhatIWant&lt;/pre&gt;

&lt;p&gt;Obviously, there is no IDoWhatIWant method in Collection, but the lack of compile-time verification means that this bug will remain hidden until it's hit at runtime. This, like several of these issues, is a backwards-compatible holdover from early VB days.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Winner: &lt;/b&gt;C#. Option strict shouldn't be an option.&lt;/p&gt;

&lt;p&gt;3) &lt;b&gt;Option Explicit:&lt;/b&gt; &lt;a href="http://msdn.microsoft.com/en-us/library/y9341s4f%28VS.80%29.aspx" mce_href="http://msdn.microsoft.com/en-us/library/y9341s4f(VS.80).aspx"&gt;Option Explicit&lt;/a&gt; is a compiler directive that specifies whether variables need to be declared. This is enabled by default (good), but you can disable it manually (bad). This allows you to make mistakes like:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;valueReceived = 3
    &lt;br&gt;&lt;font color="#008000"&gt;//Notice misspelling&lt;/font&gt;

    &lt;br&gt;valueReceived = valueRecieved + 4

    &lt;br&gt;&lt;font color="#008000"&gt;// Will print 4, rather than 7&lt;/font&gt;

    &lt;br&gt;Debug.WriteLine(valueReceived)&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;This is an easy way to create errors that hide until runtime. While it's not a security bug &lt;i&gt;per se&lt;/i&gt;, it's something to keep an eye out for in a code review. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;Winner:&lt;/b&gt; C#. Ditto previous issue.&lt;/p&gt;

&lt;p&gt;4) &lt;b&gt;Arithmetic overflow:&lt;/b&gt; Arithmetic overflow (also referred to as Integer Overflow) is the problem where arithmetical operations cause a numeric type to grow beyond its representational bounds. The overflow errors can either be &lt;i&gt;checked&lt;/i&gt;, meaning they will thrown an exception, or &lt;i&gt;unchecked&lt;/i&gt;, meaning that the value will simply be wrong&lt;i&gt;.&lt;/i&gt; Obviously, unchecked arithmetic exceptions can lead to some error-prone behavior that impacts the security of the application. You can use a checked or unchecked block to specify the behavior you want:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;unchecked
    &lt;br&gt;{

    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int j = Int32.MaxValue;

    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#008000"&gt;//No Exception thrown&lt;/font&gt;

    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; j++;

    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Prints out -2147483648

    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Console.WriteLine(j);

    &lt;br&gt;}

    &lt;br&gt;checked

    &lt;br&gt;{

    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int i = Int32.MaxValue;

    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#008000"&gt;//Throws System.OverflowException&lt;/font&gt;

    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; i++;

    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Console.WriteLine(i);

    &lt;br&gt;}&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;So what's the default .NET behavior? Consulting the &lt;a href="http://msdn.microsoft.com/en-us/library/khy08726%28VS.80%29.aspx" mce_href="http://msdn.microsoft.com/en-us/library/khy08726(VS.80).aspx"&gt;.NET documentation&lt;/a&gt;, we see that the default behavior "depends on external factors such as compiler options". For C#, it's &lt;a href="http://msdn.microsoft.com/en-us/library/h25wtyxf.aspx" mce_href="http://msdn.microsoft.com/en-us/library/h25wtyxf.aspx"&gt;unchecked&lt;/a&gt; and for VB.NET it's checked (had to do a little poking around to find &lt;a href="http://codebetter.com/blogs/grant.killian/archive/2004/03/11/8875.aspx" mce_href="http://codebetter.com/blogs/grant.killian/archive/2004/03/11/8875.aspx"&gt;this&lt;/a&gt;). Why? Performance. Apparently, when C# was going up against C++ and Java, it was deemed a better idea to save some cycles than prevent overflow weirdness. I guess Visual Basic developers were already on-board with MS, so there was no need to wow them with impressive benchmarking.&lt;/p&gt;

&lt;p&gt;You can force checked or unchecked behavior for either language using the &lt;a href="http://msdn.microsoft.com/en-us/library/h25wtyxf%28VS.71%29.aspx" mce_href="http://msdn.microsoft.com/en-us/library/h25wtyxf(VS.71).aspx"&gt;/checked&lt;/a&gt; compiler switch. From a security perspective, you should always enable this.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Winner:&lt;/b&gt; VB.NET. To think that the majority of .NET applications out there don't check for arithmetic overflow is &lt;i&gt;scary&lt;/i&gt;. I can't imagine all those C# ASP.NET application really care about the few cycles spent checking for overflow conditions.&lt;/p&gt;

&lt;p&gt;5) &lt;b&gt;Unsafe code:&lt;/b&gt; The &lt;a href="http://msdn.microsoft.com/en-us/library/chfa2zb8%28VS.71%29.aspx" mce_href="http://msdn.microsoft.com/en-us/library/chfa2zb8(VS.71).aspx"&gt;unsafe&lt;/a&gt; keyword (which is only available in C#) allows you to use pointers in your .NET application. Is this a good security idea? No. This opens up all of the C/C++ security issues like buffer overflows and memory corruption. This was also a feature introduced to lure C/C++ developers to C# and .NET, and it should almost always be removed and replaced with managed code or the &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163910.aspx" mce_href="http://msdn.microsoft.com/en-us/magazine/cc163910.aspx"&gt;P/Invoke functionality&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Winner:&lt;/b&gt; VB.NET. Mixing different platforms with such different security guarantees in the same assembly is playing with fire.&lt;/p&gt;

&lt;p&gt;6) &lt;b&gt;Unstructured error handling:&lt;/b&gt; VB.NET allows unstructured error handling. Instead of just permitting try/catch/finally blocks for exception handling, you can also use OnError, GoTo, and Resume. This looks something like:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt; On Error GoTo ErrorHandler
    &lt;br&gt;&amp;nbsp;&amp;nbsp; ' Insert code that might generate an error here

    &lt;br&gt;&amp;nbsp;&amp;nbsp; Exit Sub

    &lt;br&gt;ErrorHandler:

    &lt;br&gt;&amp;nbsp;&amp;nbsp; ' Insert code to handle the error here

    &lt;br&gt;Resume Next&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;There is a whole host of problems with unstructured error handling, including &lt;a href="http://msdn.microsoft.com/en-us/library/sf1hwa21%28VS.71%29.aspx" mce_href="http://msdn.microsoft.com/en-us/library/sf1hwa21(VS.71).aspx"&gt;performance, debugging, and maintenance issues&lt;/a&gt;. Error handling is a critical aspect of security, so using unstructured error handling could lead to security issues in a VB.NET application.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Winner:&lt;/b&gt; C#. As much fun as GoTos used to be, let's leave these constructs in the past.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Throwdown Winner: &lt;/b&gt;C#. While it has some problems related to it's initial competition with other languages (C/C++ and Java), VB.NET has so much functionality for backwards-compatibility that it's much easier to make mistakes that lead to security issues.&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx&amp;amp;;subject=C%23+and+VB.NET+Security+Throwdown!" target="_blank" title = "Post http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx&amp;amp;;title=C%23+and+VB.NET+Security+Throwdown!" target="_blank" title = "Post http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx&amp;amp;title=C%23+and+VB.NET+Security+Throwdown!" target="_blank" title = "Post http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx&amp;amp;;title=C%23+and+VB.NET+Security+Throwdown!" target="_blank" title = "Post http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx&amp;amp;;title=C%23+and+VB.NET+Security+Throwdown!&amp;amp;;top=1" target="_blank" title = "Post http://keepitlocked.net/archive/2009/02/24/c-and-vb-net-security-throwdown.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://keepitlocked.net/aggbug.aspx?PostID=2737" width="1" height="1"&gt;</description><category domain="http://keepitlocked.net/archive/tags/Software+Security/default.aspx">Software Security</category><category domain="http://keepitlocked.net/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://keepitlocked.net/archive/tags/C_2300_/default.aspx">C#</category></item></channel></rss>