Home > Buffer Overruns: Spotting the sin
Book Excerpt:
EMAIL THIS

Buffer Overruns: Spotting the sin

19 Sep 2005 | McGraw-Hill

Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   

The 19 Deadly Sins of Software Security The following excerpt is from Chapter 1 of "The 19 Deadly Sins of Software Security" written by Michael Howard, David LeBlanc and John Viega. Click for the complete book excerpt series or visit McGraw-Hill to purchase the book.

Spotting the sin pattern

Here are the components to look for:

  • Input, whether read from the network, a file, or from the command line
  • Transfer of data from said input to internal structures
  • Use of unsafe string handling calls
  • Use of arithmetic to calculate an allocation size or remaining buffer size
Spotting the sin during code review

Spotting this sin during code review ranges from being very easy to extremely difficult. The easy things to look for are usage of unsafe string handling functions. One issue to be aware of is that you can find many instances of safe usage, but it's been our experience that there are problems hiding among the correct calls. Converting code to use only safe calls has a very low regression rate (anywhere from 1/10th to 1/100th of the normal bug-fix regression rate), and it will remove exploits from your code.

One good way to do this is to let the compiler find dangerous function calls for you. If you undefined strcpy, strcat, sprintf, and similar functions, the compiler will find all of them for you. A problem to be aware of is that some apps have re-implemented all or a portion of the C run-time library internally.

A more difficult task is looking for heap overruns. In order to do this well, you need to be aware of integer overflows, which we cover in Sin 3. Basically, you want to first look for allocations, and then examine the arithmetic used to calculate the buffer size.

The overall best approach is to trace user input from the entry points of your application through all the function calls. Being aware of what the attacker controls makes a big difference.

Testing techniques to find the sin

Fuzz testing, which subjects your application to semi-random inputs, is one of the better testing techniques to use. Try increasing the length of input strings while observing the behavior of the app. Something to look out for is that sometimes mismatches between input checking will result in relatively small windows of vulnerable code. For example, someone might put a check in one place that the input must be less than 260 characters, and then allocate a 256 byte buffer. If you test a very long input, it will simply be rejected, but if you hit the overflow exactly, you may find an exploit. Lengths that are multiples of two and multiples of two plus or minus one will often find problems.

Other tricks to try are looking for any place in the input where the length of something is user specified. Change the length so that it does not match the length of the string, and especially look for integer overflow possibilities -- conditions where length +1=0 are often dangerous.

Something that you should do when fuzz testing is to create a specialized test build. Debug builds often have asserts that change program flow and will keep you from hitting exploitable conditions. On the other hand, debug builds on modern compilers typically contain more advanced stack corruption detection. Depending on your heap and operating system, you can also enable more stringent heap corruption checking.

One change you may want to make in your code is that if an assert is checking user input, change the following from

assert(len < MAX_PATH);

to

if(len >= MAX_PATH)
{
     assert(false);
     return false;
}

You should always test your code under some form of memory error detection tool, such as AppVerifier on Windows (see link in the "Other Resources" section) to catch small or subtle buffer overruns early.

Click for the next excerpt in this series: Example sins


Click for the book excerpt series or visit McGraw-Hill to purchase the book.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Microsoft Office Suite
Should you switch to the Office 2007 file format?
Microsoft Office 2010: At a glance
Microsoft Office 2010 ready for testing
New Enterprise Desktop e-zine
Microsoft wraps Office SP2 with better doc support
Combining folder redirection with roaming profiles
Microsoft releases infrastructure updates for server products
Microsoft Office Project Server 2007: New features and some that have been retired
What's hot in Windows security? New Microsoft Office Security Guide
Free HTML editor makes intranet updates a breeze

Microsoft Internet Explorer (IE)
Admins can wear many hats using Netcat
Patching third-party browsers adds more work in Windows shops
Four Internet Explorer 8 Group Policy security settings
Safe enterprise Web browsing: Five tips in five minutes
Top client security tips of 2006
General security configuration: Step 1
Protection against international domain names, URL handling: Step 3
ActiveX opt-ins, information bar and cross-domain protection: Step 4
Windows Vista and IE7: Step 5
Phishing filter: Step 2

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
system tray  (SearchEnterpriseDesktop.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary




Windows Admin Solutions - User Management, Application Management, Windows Deployments
HomeTopicsITKnowledge ExchangeTipsMultimediaWhite PapersBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2008 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts