Secure Coding in C and C++
Robert C. Seacord
Format: PDF / Kindle (mobi) / ePub
"The security of information systems has not improved at a rate consistent with the growth and sophistication of the attacks being made against them. To address this problem, we must improve the underlying strategies and techniques used to create our systems. Specifically, we must build security in from the start, rather than append it as an afterthought. That's the point of Secure Coding in C and C++. In careful detail, this book shows software developers how to build high-quality systems that are less vulnerable to costly and even catastrophic attack. It's a book that every developer should read before the start of any serious project."
--Frank Abagnale, author, lecturer, and leading consultant on fraud prevention and secure documents
Learn the Root Causes of Software Vulnerabilities and How to Avoid Them
Commonly exploited software vulnerabilities are usually caused by avoidable software defects. Having analyzed nearly 18,000 vulnerability reports over the past ten years, the CERT/Coordination Center (CERT/CC) has determined that a relatively small number of root causes account for most of them. This book identifies and explains these causes and shows the steps that can be taken to prevent exploitation. Moreover, this book encourages programmers to adopt security best practices and develop a security mindset that can help protect software from tomorrow's attacks, not just today's.
Drawing on the CERT/CC's reports and conclusions, Robert Seacord systematically identifies the program errors most likely to lead to security breaches, shows how they can be exploited, reviews the potential consequences, and presents secure alternatives.
Coverage includes technical detail on how to
- Improve the overall security of any C/C++ application
- Thwart buffer overflows and stack-smashing attacks that exploit insecure string manipulation logic
- Avoid vulnerabilities and security flaws resulting from the incorrect use of dynamic memory management functions
- Eliminate integer-related problems: integer overflows, sign errors, and truncation errors
- Correctly use formatted output functions without introducing format-string vulnerabilities
- Avoid I/O vulnerabilities, including race conditions
Secure Coding in C and C++ presents hundreds of examples of secure code, insecure code, and exploits, implemented for Windows and Linux. If you're responsible for creating secure C or C++ software--or for keeping it safe--no other book offers you this much detailed, expert assistance.
features of C++ are not required to create usable data abstractions. The CERT C Secure Coding Standard [Seacord 2008], “DCL12-C. Implement abstract data types using opaque types,” describes creating abstract data types using private (opaque) data types and information hiding. Arbitrary-Precision Arithmetic Arbitrary-precision arithmetic effectively provides a new integer type whose width is limited only by the available memory of the host system. Many arbitrary-precision arithmetic packages are
minimum time. This approach reduces the opportunities an attacker has to execute arbitrary code with elevated privileges. This principle can be implemented in the following ways: • Grant each system, subsystem, and component the fewest privileges with which it can operate. • Acquire and discard privileges such that, at any given point, the system has only the privileges it needs for the task in which it is engaged. • Discard the privilege to change privileges if no further changes are
similar function to strcpy() but allows a maximum size n to be specified: Click here to view code image 1 char *strncpy( 2 char * restrict s1, const char * restrict s2, size_t n 3 ); The strncpy() function can be used as shown in the following example: Click here to view code image strncpy(dest, source, dest_size - 1); dest[dest_size - 1] = '\0'; Because the strncpy() function is not guaranteed to null-terminate the destination string, the programmer must be careful to ensure that the
sections are mapped into the process address space and are writable by default. Constructors have not been used in exploits because they are called before main(). As a result, the focus is on destructors and the .dtors section. The contents of the .dtors section in the executable image can be examined with the objdump command as shown in Example 3.9. The head and tail tags can be seen, as well as the address of the destroy() function (in little endian format). An attacker can transfer control
them, in a single pointer-size field [Sinha 2005]. While it is technically undefined behavior, a pointer can also be made to point outside the bounds of the object before being restored: Click here to view code image 1 int* p = new int; 2 p+=10; 3 // ... collector may run here ... 4 p-=10; 5 *p = 10; // can we be sure that the int is still there? The object allocated at the beginning of the following function is clearly reachable via p throughout f(): Click here to view code image