<?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>Safe Integer Arithmetic in C</title><link>http://blogs.msdn.com/b/michael_howard/archive/2006/02/02/523392.aspx</link><description>There has been plenty of literature written regarding integer arithmetic issues and security bugs. If you need a good refresher, I would urge you to read one or more of the following: 
 
 
 Reviewing Code for Integer Manipulation Vulnerabilities </description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>Librerie sicure per C e C++ &amp;laquo; Satius est supervacua scire</title><link>http://blogs.msdn.com/b/michael_howard/archive/2006/02/02/523392.aspx#8351727</link><pubDate>Wed, 02 Apr 2008 18:57:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8351727</guid><dc:creator>Librerie sicure per C e C++ « Satius est supervacua scire</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://manuel91.wordpress.com/2008/04/02/librerie-sicure-per-c-e-c/"&gt;http://manuel91.wordpress.com/2008/04/02/librerie-sicure-per-c-e-c/&lt;/a&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8351727" width="1" height="1"&gt;</description></item><item><title>eschew  &amp;raquo; Blog Archive   &amp;raquo; links for 2006-11-02</title><link>http://blogs.msdn.com/b/michael_howard/archive/2006/02/02/523392.aspx#1068407</link><pubDate>Mon, 13 Nov 2006 16:52:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1068407</guid><dc:creator>eschew  » Blog Archive   » links for 2006-11-02</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://eschew.org/blog/?p=138"&gt;http://eschew.org/blog/?p=138&lt;/a&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1068407" width="1" height="1"&gt;</description></item><item><title>List of useful security libraries</title><link>http://blogs.msdn.com/b/michael_howard/archive/2006/02/02/523392.aspx#552800</link><pubDate>Thu, 16 Mar 2006 16:33:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:552800</guid><dc:creator>HoraceWang</dc:creator><description>List of useful security libraries&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=552800" width="1" height="1"&gt;</description></item><item><title>List of useful security libraries</title><link>http://blogs.msdn.com/b/michael_howard/archive/2006/02/02/523392.aspx#540124</link><pubDate>Tue, 28 Feb 2006 00:23:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:540124</guid><dc:creator>Michael Howard's Web Log</dc:creator><description>I was asked last week for a list of &amp;amp;quot;drop-in-and-more-secure&amp;amp;quot; replacements, created at Microsoft,&amp;amp;amp;nbsp;for...&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=540124" width="1" height="1"&gt;</description></item><item><title>re: Safe Integer Arithmetic in C</title><link>http://blogs.msdn.com/b/michael_howard/archive/2006/02/02/523392.aspx#526283</link><pubDate>Tue, 07 Feb 2006 06:40:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:526283</guid><dc:creator>David LeBlanc</dc:creator><description>There's a few things to remember about taking this approach - the first is that conversion between integer types on entry into a function will happen without any warning, IIRC, even at warning level 4. So if you do have signed values coming in and forget to use the conversion function, you'll get unexpected results and no warning. Another aspect of this is if you don't know what casting behavior to expect - what if the input is a short? If I pass a short to a function expecting an unsigned int, serious mayhem can ensue since it will sign extend first.&lt;br /&gt;&lt;br /&gt;The second thing is that there are some common cases where you'll get a lot better perf by writing a dedicated function. Specifically, a*b+c is a common case where if you have 32-bit inputs, you can upcast all of them to either _int64 or unsigned _int64 (depending on the sign of the inputs), and if they're all the same sign, the operations cannot overflow internally, and you can then check for overflow on the whole value instead of having many chained comparisons. For this specific case, you'd get better perf than SafeInt as well. We wrote such a function for Office and used it fairly often.&lt;br /&gt;&lt;br /&gt;Lastly, while reviewing Office's MSO.dll for integer problems, we found a number of cases where someone changed from signed to unsigned, and created vulnerable code that wasn't previously a problem. While it is true that unsigned numbers are a LOT easier to check, if you had something along the lines of if(input &amp;lt; 0) return InputError();, then changed input from signed to unsigned and forgot to correctly change all the code (to something like if(input &amp;gt; max_size) ), then you're creating problems.&lt;br /&gt;&lt;br /&gt;The thing to remember is that when correcting integer problems, you really have to be careful. It's a complex problem.&lt;br /&gt;&lt;br /&gt;While I am indeed biased, I have also spent a great deal of time thinking about this problem. If you are using C++, SafeInt is much less likely to allow errors than any C constructs. As an example, I was shown an early version of IntSafe and a code snippet that 'proved' IntSafe was substantially better performing than SafeInt. It turned out that SafeInt was checking 3 operations, and IntSafe was checking one. It wasn't a fair comparison, and more importantly, SafeInt was giving the programmer a LOT more protection. If you're stuck with C, then you obviously cannot use SafeInt, and in that case, IntSafe can be a big help. Nonetheless, be aware of the trade-offs and where you'll need to be doing more work to avoid mistakes.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=526283" width="1" height="1"&gt;</description></item><item><title>re: Urgent Offthread Post</title><link>http://blogs.msdn.com/b/michael_howard/archive/2006/02/02/523392.aspx#525109</link><pubDate>Sun, 05 Feb 2006 13:30:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:525109</guid><dc:creator>Karridine</dc:creator><description>Michael, your name came up when I Googled &amp;quot;QuickTime blogs&amp;quot;, and I went down-page 15 posts but couldn't find a more appropriate thread, so I share with you (and your VAST reading audience) that I've downloaded and installed the eTunes/QuickTime bundle &lt;br /&gt;&lt;br /&gt;BUT!&lt;br /&gt;Whenever I click on a QT-enabled website, it STILL tells me to upgrade to QT 4.5 or 7.5 or whatever it is! And I've RE-installed QT twice last week, for a total of 3 installations on WinXP platform, and STILL the detour when I hit a QT-webFunction...&lt;br /&gt;&lt;br /&gt;And Yahoo's YahooSetupVideoPlayer.exe is infected with the Trojan Spy.Bombka so I cannot install it... I can't defuse it, excise the bad bits, or notify Yahoo to deal with this PROBLEM, durn it! I still can't play &amp;quot;Its In the Koran!&amp;quot;&lt;br /&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=525109" width="1" height="1"&gt;</description></item><item><title>Kernel Mustard  &amp;raquo; Blog Archive   &amp;raquo;  SafeInt</title><link>http://blogs.msdn.com/b/michael_howard/archive/2006/02/02/523392.aspx#524912</link><pubDate>Sat, 04 Feb 2006 23:07:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:524912</guid><dc:creator>Kernel Mustard  » Blog Archive   »  SafeInt</dc:creator><description>PingBack from &lt;a rel="nofollow" target="_new" href="http://kernelmustard.com/2006/02/04/safeint/"&gt;http://kernelmustard.com/2006/02/04/safeint/&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=524912" width="1" height="1"&gt;</description></item><item><title>re: Safe Integer Arithmetic in C</title><link>http://blogs.msdn.com/b/michael_howard/archive/2006/02/02/523392.aspx#523978</link><pubDate>Fri, 03 Feb 2006 14:41:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:523978</guid><dc:creator>Alan</dc:creator><description>Wow, that sucks really amazingly! &lt;br/&gt;&lt;br/&gt;So using the nice SafeInt class I could express the concept of safely evaluating a*b+1 by writing, well, a*b+1, but you think it would be much better to write&lt;br/&gt;&lt;br/&gt;if (SUCCEEDED(IntToDWord(cElem,&amp;amp;cbElem)) &amp;amp;&amp;amp;&lt;br/&gt;    SUCCEEDED(DWordMult(cbBlockSize, cbElem, &amp;amp;cbElem)) &amp;amp;&amp;amp;&lt;br/&gt;    SUCCEEDED(DWordAdd(cbElem,1,&amp;amp;cbElem)))&lt;br/&gt;&lt;br/&gt;I'm truly appalled. Making code that much less readable makes it harder to see what it's doing, which is going to cause bugs (both security related and non-security related).&lt;br/&gt;&lt;br/&gt;Not to mention having to check return values on *every single arithmetic operation* instead of getting a sensible exception and handling it in one place.&lt;br/&gt;&lt;br/&gt;What were you thinking? Are you doing this for some kind of bet or joke?&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=523978" width="1" height="1"&gt;</description></item><item><title>re: Safe Integer Arithmetic in C</title><link>http://blogs.msdn.com/b/michael_howard/archive/2006/02/02/523392.aspx#523836</link><pubDate>Fri, 03 Feb 2006 09:17:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:523836</guid><dc:creator>c</dc:creator><description>Testing to see if these are turned on yet :)&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=523836" width="1" height="1"&gt;</description></item><item><title>re: Safe Integer Arithmetic in C</title><link>http://blogs.msdn.com/b/michael_howard/archive/2006/02/02/523392.aspx#523707</link><pubDate>Fri, 03 Feb 2006 04:29:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:523707</guid><dc:creator>Selva</dc:creator><description>Hi Howard,&lt;br/&gt;  I have a very dumb question on the code snippet which was used to demonstrate integer overflow and  underflow in the MSDN article&lt;br/&gt;&amp;quot;Reviewing Code for Integer Manipulation Vulnerabilities&amp;quot;&lt;br/&gt;Thanks for the article and I am really enjoying reading it.But I have one doubt:&lt;br/&gt;&lt;br/&gt;I think I am not getting the overflow scenario correctly.&lt;br/&gt;&amp;quot;&lt;br/&gt;bool func(char *s1, size_t len1, &lt;br/&gt;          char *s2, size_t len2) {&lt;br/&gt;   if (1 + len1 + len2 &amp;gt; 64) &lt;br/&gt;      return false;&lt;br/&gt;&lt;br/&gt;   // accommodate for the trailing null in the addition&lt;br/&gt;   char *buf = (char*)malloc(len1+len2+1);&lt;br/&gt;   if (buf) {&lt;br/&gt;      StringCchCopy(buf,len1+len2,s1);&lt;br/&gt;      StringCchCat(buf,len1+len2,s2);&lt;br/&gt;   }&lt;br/&gt;&lt;br/&gt;   // do other stuff with buf&lt;br/&gt;&lt;br/&gt;   if (buf) free(buf);&lt;br/&gt;&lt;br/&gt;   return true;&lt;br/&gt;}&lt;br/&gt;&amp;quot;&lt;br/&gt;I am continuosly failing to see a statement which could trigger a crash.&lt;br/&gt;The statements which could cause a crash if the value of len2 is 0xFFFFFFFF and value of len1 is 64,are potentially:&lt;br/&gt;&lt;br/&gt;1. StringCchCat(buf,len1+len2,s2);&lt;br/&gt;2.if (buf) free(buf);&lt;br/&gt;&lt;br/&gt;The reasoning which my silly mind tries to give me is&lt;br/&gt;&lt;br/&gt;a.Statement 1 wont crash since len2+len2 gives 63 which is well within the buffer length we have allocated for buf.&lt;br/&gt;b.Statement 2 will crash only when buffer s1 is uninitialized.&lt;br/&gt;c.It is not possible to allocate 0xFFFFFFFF bytes to a buffer.&lt;br/&gt;&lt;br/&gt;I tried different combinations but I m not able to make the code crash.&lt;br/&gt;&lt;br/&gt;Could you please enlighten me,if possible and if you have a moment?&lt;br/&gt;&lt;br/&gt;Thank you very much for your time.&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=523707" width="1" height="1"&gt;</description></item></channel></rss>