YOUR FEEDBACK
SOA Feature Story: Real-Time SOA Starts with the Messaging Bus!
Gerardo Pardo-Castellote wrote: Regarding the previous comment about "TCP ...


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
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

Digg This!

Page 2 of 2   « previous page

Finally sprintf() is a triple threat. Incautious use of this function can result in a buffer overflow vulnerability if, for example, an attacker provides a string argument for the user variable below that exceeds 495 bytes (512 bytes - 16 character bytes - 1 null byte):

1.  char buffer[512];
2.  sprintf(buffer, "Wrong command: %s\n", user);

Secondly, because the sprintf() function accepts a formatted output function that accepts a format string and variable number of arguments, it's subject to format string exploits.

Third, if you Google for sprintf() on the Internet you can usually find some code that looks like this code (that I found in the first link I selected) from the Linux kernel mailing list at http://lkml.org/:

int i;
ssize_t count = 0;

for (i = 0; i < 9; ++i)
   count += sprintf(buf + count, "%02x ", ((u8 *)&sl-reg_num)[i]);

count += sprintf(buf + count, "\n");

So what's wrong with this code? Well, sprintf() can (and will) return -1 on error conditions such as an encoding error. In this case, the count variable, already at zero, can be decremented further - almost always with unexpected results. While this particular error isn't commonly associated with software vulnerabilities, it can easily lead to abnormal program termination.

LWM: Does gcc or Visual Studio produce more secure executables? How do you assess this?

RCS: In general both compilers are constrained by conformance to C language standards such as ISO/IEC 9899. In some places, Microsoft intentionally disregards strict conformance to improve security, for example, by disallowing the %n conversion specifiers for formatted input/output functions in the 2005 version of Visual C++.

I think the most interesting area for differentiation from a security perspective is in each implementation's handling of integers. In a perfect world, C and C++ compilers would identify the potential for exceptional conditions to occur at runtime and provide a mechanism (such as an exception, trap, or signal handler) for applications to handle these events. Unfortunately, the world we live in is far from perfect.

The Visual C++ .NET 2003 compiler generates a compiler warning (C4244) when an integer value is assigned to a smaller integer type. At warning level 1, a warning is issued if a value of type __int64 is assigned to a variable of type unsigned int. At warning level 3 and 4, a "possible loss of data" warning is issued if an integer type is converted to a smaller integer type. For example, the assignment in the following example is flagged at warning level 4:

// C4244.cpp
// compile with: /W4
int main() {
    int b = 0, c = 0;
    short a = b + c; // C4244
}

Visual C++ .NET 2003 also provides runtime error checks that are enabled by the /RTC flag. The /RTCc compiler flag, in particular, provides a similar function to compiler warning C4244 by reporting when a value assigned to a smaller data type results in a loss of data. Visual C++ also includes a runtime_checks pragma that disables or restores the /RTC settings, but it doesn't include flags for catching other runtime errors such as overflows. Visual C++ 2005 adds the ability to catch overflows in operator::new (and is on by default).

Runtime error checks aren't valid in a release (optimized) build for performance reasons.

The gcc and g++ compilers include an -ftrapv compiler option that provides limited support for detecting signed integer exceptions at runtime. According to the gcc man page, this option "generates traps for signed overflow on addition, subtraction, and multiplication operations." In practice, this means that the gcc compiler generates calls to existing library functions rather than generating assembler instructions to perform these arithmetic operations on signed integers. These are enforced at runtime even when optimization is enabled.

If you use this feature, make sure you use gcc version 3.4 or later because the checks implemented by the runtime system before this version don't adequately detect all overflows and shouldn't be relied on to do so.

Neither compiler passes an argument or byte count on calls to variadic functions implemented using the ANSI stdargs, although it's permitted by the C99 specification and would make variadic functions such as the formatted input/output functions more secure.

LWM: Are there any security issues that are unique to Linux/gcc? How can they be overcome? Are there any solutions in sight?

RCS: Data pointers are used in C and C++ to refer to dynamically allocated structures, call-by-reference function arguments, arrays, and other data structures. An attacker can modify these data pointers (when exploiting a buffer overflow vulnerability, for example). If a pointer is subsequently used as a target for an assignment, an attacker can control the address to modify other memory locations with a technique known as an arbitrary memory write.

Most Linux implementations contain a large number of suitable targets for arbitrary memory writes - addresses that can be overwritten and then used to transfer control to attacker-injected code (or existing code selected by the attacker). Linux uses the executable and linking format (ELF) that uses a global offset table (GOT). The GOT contains the absolute addresses of functions in the executable. An attacker can overwrite a GOT entry for a function with the shellcode address using an arbitrary memory write.

A similar problem exists when an attacker overwrites function pointers directly. The GCC compiler generates a .dtors section in an easily identifiable location that contains destructor functions that are invoked following execution of the main C program. These functions can be overwritten and used to transfer control to arbitrary code even when the destructor functions aren't used in the program!

Arbitrary memory writes can easily defeat canary-based protection schemes. Write-protecting targets is difficult because of the number of targets and because there's a requirement to modify many of these targets (for example, function pointers) at runtime. Buffer overflows occurring in any memory segment can be exploited to execute arbitrary code, so moving variables from the stack to the data segment or heap isn't a solution. The best approach to preventing pointer subterfuge resulting from buffer overflows is to eliminate possible buffer overflow conditions.

One way to limit the exposure from some of these targets is to reduce the privileges of potentially vulnerable processes. OpenBSD, for example, enforces a policy called "W xor X" or "W^X" that requires that no part of the process memory address space is both writable and executable. If implemented on Linux systems, this policy could eliminate some (but not all) targets of arbitrary memory write.

LWM: Are there any other sources of information on secure coding in C/C++ on Linux?

RCS: In addition to my book Secure Coding in C and C++, you should also check out Secure Programming for Linux and Unix HOWTO-Creating Secure Software from David Wheeler online at www.dwheeler.com/secure-programs.

SIDEBAR

About the Book
Book: Secure Coding in C and C++
Author: Robert C. Seacord
Publisher: Addison Wesley Professional
List Price: $39.99
ISBN: 0321335724
Published: Sep 9, 2005
Pages: 368
Web Site: http://www.awprofessional.com/title/0321335724#


Page 2 of 2   « previous page

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.

SYS-CON Australia News Desk wrote: 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.
read & respond »
SYS-CON Canada News Desk wrote: 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.
read & respond »
LinuxWorld News Desk wrote: "Secure Coding in C and C++" A LinuxWorld Interview With Robert Seacord. 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.
read & respond »
LATEST LINUX STORIES
SCO - Linux' Worst Nightmare Is Back
The court also said Novell couldn't run interference for Linux and stop SCO from seeking royalty payments for alleged UnixWare and OpenServer infringement by Linux users under its infamous SCOsource licensing program. , it's merely a matter of time before SCO starts seeking those pa
Guilty of Arrogance Too
You have perhaps heard that while we were on vacation Linux file system ace and convicted wife killer Hans Reiser took the cops to where he had buried her body. Two days later when Reiser was supposed to be sentenced to 25 years to life for first decree murder the judge disclosed that
Adobe's Kevin Lynch and Microsoft's Scott Guthrie to Keynote AJAX World RIA Conference & Expo
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
Virtualization Conference Keynote Webcast Live on SYS-CON.TV
Brian Stevens, the Chief Technology Officer and Vice President of Engineering of Red Hat, delivered his Virtualization Keynote 'The Future of the Virtual Enterprise' at SYS-CON's Virtualization Conference & Expo 2007 West in San Francisco. 'Virtualization is the hottest subject today,
Red Hat Delivers on Linux Automation
Red Hat announced advancements that extend the Company's Linux Automation strategy by providing expanded capabilities and incorporating broadened community involvement for secure management of both users and systems across virtual and physical enterprise infrastructures.
Linspire Collapses into Xandros
Xandros acquired Linspire's Linux assets after Linspire changed its name to Digital Cornerstone. With the acquisition Xandros CEO Andy Typaldos has been telling the press, 'Xandros is already the third-largest Linux company in the world, and ... we may already be the largest private Li
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