<?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>Evil Compiler Tricks, and Checking for Pointer Math</title><link>http://blogs.msdn.com/b/david_leblanc/archive/2008/04/04/evil-compiler-tricks-and-checking-for-pointer-math.aspx</link><description>My favorite programming geek hobby being integer overflows, this caught my eye – 
 "gcc silently discards some wraparound checks" http://www.kb.cert.org/vuls/id/162289 
 Basically, what it says is that code which looks like this: 
 ============ snip</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Evil Compiler Tricks, and Checking for Pointer Math</title><link>http://blogs.msdn.com/b/david_leblanc/archive/2008/04/04/evil-compiler-tricks-and-checking-for-pointer-math.aspx#8362174</link><pubDate>Sun, 06 Apr 2008 07:13:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8362174</guid><dc:creator>lally</dc:creator><description>&lt;P&gt;While already a nice entry point for exploits, how about 16-bit targets? &amp;nbsp;Hitting 2^16 is pretty easy.&lt;/P&gt;
&lt;P&gt;[dcl] 16-bit code is always a great place to look when auditing. tends to be easy pickings. You also often run into programmer error because they didn't take into account int promotion.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8362174" width="1" height="1"&gt;</description></item><item><title>re: Evil Compiler Tricks, and Checking for Pointer Math</title><link>http://blogs.msdn.com/b/david_leblanc/archive/2008/04/04/evil-compiler-tricks-and-checking-for-pointer-math.aspx#8361834</link><pubDate>Sun, 06 Apr 2008 03:06:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8361834</guid><dc:creator>moocat</dc:creator><description>&lt;P&gt;One question I have is whether that check is ever sufficient for real code.&lt;/P&gt;
&lt;P&gt;What I am thinking is that you really need to know that 'len' is not greater then the size of the buffer so you don't read/write some random memory that is not part of the buffer. &amp;nbsp;Furthermore, I neither know of any valid way to get a buffer where the buffer wraps around the end of memory nor do I think there is any reason that could possibly be needed.&lt;/P&gt;
&lt;P&gt;So, based on that, if you check that 'len' is less then the size of buffer, the check for wraparound is redundant and unnecessary.&lt;/P&gt;
&lt;P&gt;[dcl] that's another way of doing it. you still have to make sure that the multiplication doesn't wrap in either case. This is why I'm ambivalent about fully supporting pointer math within SafeInt - it is more properly done by using another class we have internal to Office, which is BoundedPointer - what it does should be obvious from the name. STL iterators now also solve the problem nicely. There could be cases where it's more efficient to do this first, then other checks later.&lt;/P&gt;
&lt;P&gt;[dcl] You could also hit a case where I check to make sure the result is in the buffer, but it actually wrapped, and I'm not at the spot in the buffer I ought to be. So you always have to check both that you got there the right way AND that you're still in the buffer. If either condition fails, you have a problem. It is possible to write checks for some code that will do both at once, and that's obviously desirable.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8361834" width="1" height="1"&gt;</description></item><item><title>re: Evil Compiler Tricks, and Checking for Pointer Math</title><link>http://blogs.msdn.com/b/david_leblanc/archive/2008/04/04/evil-compiler-tricks-and-checking-for-pointer-math.aspx#8358650</link><pubDate>Sat, 05 Apr 2008 01:41:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8358650</guid><dc:creator>Raymond Chen - MSFT</dc:creator><description>&lt;P&gt;The gcc optimization is legal because 5.7.5 says "If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, [then...]; otherwise, the behavior is undefined." Since the first part is not satisfied (you can't have an array that straddles 0), the second half is operative: The behavior is undefined. According to 1.2.3.12, the standard imposes no requirements on undefined behavior. They even call out ignoring the situation entirely as an acceptable response.&lt;/P&gt;
&lt;P&gt;In other words, the language requires you to validate the parameters to the + operator BEFORE you perform the operation, not after.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8358650" width="1" height="1"&gt;</description></item><item><title>When adding security bugs to your code is not your fault!</title><link>http://blogs.msdn.com/b/david_leblanc/archive/2008/04/04/evil-compiler-tricks-and-checking-for-pointer-math.aspx#8358562</link><pubDate>Sat, 05 Apr 2008 01:08:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8358562</guid><dc:creator>Michael Howard's Web Log</dc:creator><description>&lt;p&gt;David LeBlanc and I (and a bunch of others) just had a little email exchange about some fascinating integer&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8358562" width="1" height="1"&gt;</description></item></channel></rss>