YOUR FEEDBACK
Werner Keil wrote: Java 6 update 10. If I'd be running Apple, I'd probably really drop dead...


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP LINKS YOU MUST CLICK ON


"Secure Coding in C and C++" A Linux.SYS-CON.com Interview With Robert Seacord
An interview with Robert Seacord, senior vulnerability analyst at CERT

Robert C. Seacord, a senior vulnerability analyst at the CERT/Coordination Center at Carnegie Mellon University, has just published the book Secure Coding in C and C++ (Addison-Wesley, 2005). I sat down with him to discuss software security in the Linux environment and elsewhere.

LWM: There's an ongoing debate over whether Linux is more secure than Windows. Some people argue that since Linux's source code is freely available, it makes it easy for hackers to implement hacks and break into Linux systems, whereas this becomes more difficult with proprietary operating systems. What's your take on this topic?

RCS: I agree there's no real security through obscurity, but obscurity does provide an impediment. Attackers can "reuse" sections of code to develop exploits that have the same properties as the code they're attacking - an unintended but unfortunate consequence of reuse.

A number of security experts have argued that Open Source software is more secure because it's open to review by a broad range of individuals. However, Open Source software provides no guarantee that the software is reviewed or that those reviewing it understand the program's context in a larger system or the program's external interactions. In other words, just being able to read the code doesn't mean you have the wherewithal to do anything with it or about it.

On the other hand, attackers have also become adept at reverse-engineering executables so not releasing the source only slows attackers down. An analysis of CERT/CC vulnerability reports conducted by Omar Alhazmi at Colorado State University shows vulnerabilities accruing in both Windows and Linux operating systems at similar rates.

LWM: In your experience, which programming languages (e.g., C, C++, Java) provide the most secure programming safeguards and the most tools to ensure the code doesn't contain vulnerabilities?

RCS: While languages like Java that have automatic garbage collection, lack pointers, and have strict type checking can limit or prevent vulnerabilities resulting from buffer overflows and common dynamic memory management errors, programming errors that result in security vulnerabilities can happen in any language. There are tradeoffs that have to be considered in language and application development platforms that can't always be objectively reduced to "Which is more secure?"

Java programs can be vulnerable to SQL injection and cross-site scripting (XSS) vulnerabilities. Integer errors can also occur undetected, although the consequence of these errors is typically not as severe as they may be in C and C++ language programs.

Interestingly, Java language security may be due more to the reasons given above and less to Java bytecodes than typically understood. C++ programs compiled to MSIL in the Microsoft .NET environment are as susceptible to buffer overflows and other common vulnerabilities as their compiled counterparts, although additional security can be gained by using the new system-level data structures for arrays, strings, and other data types.

Because of the history of security vulnerabilities in C and C++, a number of tools and products have been developed to make these languages more secure. So, ironically, these languages generally have the best tool support.

LWM: What's the most important coding practice that we can implement to develop more secure software? Can you also provide us your Top 5 best practices for writing secure C/C++ code?

RCS: The most important coding practice is to be extremely paranoid in handling any data that originated with the user, either directly or indirectly.

As for the remaining four:

  1. Many vulnerabilities in C/C++ result from the incautious manipulation of strings. Your entire development team should select a clear and unambiguous approach to dealing with strings in your application and apply it consistently. C++ programmers have the option of using the standard std::string class, which is generally less error prone than standard C-style strings. C language programmers may want to consider using or building a string library so that all string operations (and potential vulnerabilities) can be isolated to a single module. CERT has begun developing a managed string library that could be used as the basis of such an effort and submitted it as a paper to the J11 WG14 C standard working group. Eventually we hope to release the source code for this library on the Secure Coding web site at www.cert.org/.
  2. Your entire development team should select a clear and unambiguous approach to dealing with integers and apply it consistently. Operations on integers can result in overflow, truncation, sign errors, and other related issues that can lead to exploitable vulnerabilities. Having a plan to deal with integers up-front is critical because almost every integer operation can result in an exceptional condition, particularly when the inputs can be controlled by an attacker. Generally speaking, you should limit all integer inputs to acceptable values and use safe integer operations that detect and report error conditions when dealing with "tainted" inputs.
  3. Sit down together as a development team and do code inspections. Maintain a list of common programming defects you find and make sure you check for these until they cease to be found. The inspections serve several purposes. First, they can be effective in identifying and removing defects in the code that can lead to vulnerabilities. Second, and potentially more importantly, it lets more experienced coders mentor less experienced team members about what to look for and how to correct problems. Third, inspections provide a mechanism for fostering a consistent approach to applying security-coding practices throughout the project.
  4. Use the defense-in-depth approach of applying multiple strategies so that a single error isn't necessarily fatal. Start with secure coding practices and then evaluate your code using a variety of manual processes and automated tools. Use a secure runtime environment as a final line of defense.
LWM: What are the three most dangerous C lib functions to use and why?

RCS: I would have to say gets(), strcpy(), and sprintf().

The gets() function is on the list because it can't be used safely. The function reads a line from standard input into the buffer until a terminating new line or EOF is found. In fact, the Linux man page for this function contains the following advice:

Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use.

There are two alternative functions that can be used: fgets() and gets_s(). The following example shows how these calls are used:

1.  #define BUFFSIZE 8
2.  int main(int argc, char *argv[]){
3.  char buff[BUFFSIZE]; // insecure use of gets()
4.  gets(buff);
5.  printf("gets: %s.\n", buff); // more secure use of fgets()
6.  if (fgets(buff, BUFFSIZE, stdin) == NULL) {
7.  printf("read error.\n");
8.  abort();
9.  }
10.  printf("fgets: %s.\n", buff); // more secure use of gets_s()
11.  if (gets_s(buff, BUFFSIZE) == NULL) {
12.  printf("invalid input.\n");
13.  abort();
14.  }
15.  printf("gets_s: %s.\n", buff);
16.  return 0;
17.  }

The fgets() function is defined in C99 and has similar behavior to gets().The fgets() function accepts two additional arguments: the number of characters to read and an input stream. The gets_s() function is defined in ISO/IEC TR 24731 to provide a compatible version of gets() that was less prone to buffer overflow.

The strcpy() function is on the list because, even though it can generally be used safely, it's often used in an insecure fashion, for example by dynamically allocating the required storage as shown below:

1.  int main(int argc, char *argv[]) {
2.  char *buff = (char *)malloc(strlen(argv[1])+1);
3.  if (buff != NULL) {
4.    strcpy(buff, argv[1]);
5.    printf("argv[1] = %s.\n", buff);
6.  }
7.  else {/* Couldn't get the memory - recover */
8.  }
9.  return 0;
10.  }

There are also many, many alternatives to using strcpy() that are generally less error prone, including strcpy_s(), strlcpy() (available for many flavors of Unix but not GCC/Linux), strdup(), and others.

About Ibrahim Haddad
Dr. Ibrahim Haddad is Director of Technology in the Software Operations Group at Motorola Inc. focusing on embedded and open source technologies and roadmaps.

LATEST LINUX STORIES
Hans Reiser’s children Rory, 8, and Niorline, 7, represented pro bono by Morrison and Foerster, the big-time San Francisco law firm, have sued their father for the wrongful death of their mother, seeking unspecified damages for being deprived of her “love, support, companionship, c...
Two years ago this weekend Linux programmer Hans Reiser murdered his estranged wife. Friday he was sentenced to 15 years to life as part of a deal that reduced his first-degree murder conviction and its mandatory 25 years-to-life sentence to second-degree murder in exchange for taking ...
Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe launched AIR 1.0 in February '08 and Microsoft launched Silverlight (September '07). At the 6th International AJAXWorld RIA Conference & Expo in October SYS-CON Events is delighted to be...
Red Hat CTO Brian Stevens, Citrix CTO Simon Crosby, Egenera CTO Pete Manca, Allen Stewart, Group Manager, Windows Virtualization at Microsoft, and Brian Duckering, Sr. Director of Products and Alliances at Symantec were the top industry executives who joined Jeremy Geelan in the 4th Fl...
Microsoft is going to pump up to $100 million more into Novell for additional certificates that it will sell or give to customers to redeem for SUSE Linux support. Novell should get the money on November 1 right before the second anniversary of the widely loathed Microsoft-Novell pact ...
IBM introduced a series of new products, services and initiatives that further expand IBM's commitment to Linux and open source by enabling the next generation of Linux. As the company marks ten years of support for Linux, IBM announced a number of cross-company initiatives to driv...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE