13 When thinking about security one must never forget that **putting a lock on something is not secure per se** . Even if you have the biggest and strongest front door, when your roommate leaves the key under the doormat a stranger or a friend can just walk right in. If the reason the key is left under the door mat is that it weighs 1kg then maybe you bought the wrong lock.

17 As C is a medium level programming language, it only provides very basic functionality and checks for incorrect usage - barely more than the processor itself. If you program in C, you can shoot yourself in the foot as easily as you can when operating as root on a UNIX™ system.

21 There are a variety of possible mistakes and even more ways to make use of them, which is called ***exploiting***. Successfully exploiting a so-called bug can affect a target system in various ways. An attacker might prevent the target system from providing any service(s) by crashing or hanging it, he could possibly gain access on the target system which he didn't have before (remote exploit), he might elevate his privileges on a target system that he could access before (local exploit), or he could simply get information that was disclosed.

25 But what are the common security flaws, how and why do they happen and how do they get exploited? One example is a bug that allows an attacker to write to memory locations. This could happen if data that is input by an attacker or user is incorrectly or not checked. Once the attacker has the ability to modify memory, he can use clever tricks to gain control over the exploited program(s) which may lead to more system-wide exploitations and possibly resulting in what was mentioned above.

37 A buffer overflow can be compared to what occurs when you fill a five pound sack with 10 pounds of apples. Basically, what happens is that the apples end up spilling over. While some might fall harmless at your feet, others can end up rolling onto the not-so-clean portion of your kitchen. While that doesn't sound too bad, assume we take it another step and say your small child eats these apples. If you're lucky, nothing will happen. However, if your not-so-clean portion of the kitchen is like many others, chances are, that they'll get ill.

41 This analogy is very similar to what happens in a buffer overflow. First, let's take a minute and describe what a buffer is. A buffer is something (an array, a pointer, or whatever) a programmer uses to store data. While this problem isn't dramatic, the issue arises when the programmer places a limit on how much this buffer can hold. Now, let's assume we have the following innocent prompt within a program:

57 That seems simple enough, what could be wrong with it? Well, let's take a minute and assume you enter your first name which is 11 characters. What is going to happen? Well, C being the "I'm only doing what you told me to do." language that it is, will put 11 <<FootNote(Ok, it really is 12 because the scanf function will append the string termination character of NULL (or '\0').)>> characters in this 10 space buffer. Now, where does this extra character go? Let's rewind back to the early 1900s for the answer. John von Neumann was theorizing how data should appear in a computer. He determined it would be easier for the computer if data and functions occupied the same regions of memory. This little decision is the root of all buffer overflows, because these extra characters start writing over the regions of memory the next portion of your program reside.

61 Now, just like we don't know where the apples were going to fall, this code can overwrite something relatively unimportant (the next ***printf(3)***-call) or something much more important (a return-statement). The keyword ***return*** means something special to C and the computer on which it runs. It informs the computer that it should adjust some internal components, that afterwards dictate what portion of memory to move to next. What an exploiter of a weakness of this type will do, is to craft the incoming data (in this case your first name) in such a way, that this internal component is told to move to some other function on the machine, which the exploiter will then use to further subvert the machine in question.

69 An off by one error is what results when the programmer has an incomplete understanding of a counting problem. Generally there are two groups of counting problems. One group deals with values which are counts and sizes, and the other group deals with values which are indicies and offsets. Counts are usually based on a starting sum of one, but indicies are usually based on a starting offset of zero. When the classification of a value from one of these groups is confused with the other, an ***off by one*** error occurs.

73 If a count is confused with an index, the value as used is one too high. Likewise, if an index is confused for a count, the value is one too low. In either case, whether a value is a count or an index depends on the context and what is being counted. Therefore, it is extremely important to define what is included in a count, as we will see with the standard library string functions.

77 Off by one errors are often introduced when an array, such as a string of characters or a byte addressed buffer, is referenced. Furthermore, what is being counted is often misunderstood. Many standard library string functions count the number of characters in a string, and many more count the number of bytes in a string. The major difference is whether the terminating NUL character in the string is counted.

119 The strlen(3) library function returns the byte count ***excluding the NUL terminating character***, so the programmer is not testing the size of the incoming string against the allocated space. The programmer should have used a buffer of size 81 to make room for the NUL.

123 This type of error allows an attacker to modify data that is adjacent to the buffer in memory. For example, suppose the program's idea of what the string was stored in memory following the buffer. Then an attacker could follow a number of steps to first modify this value following the buffer, next directly adjust the buffer to a desired size, and finally store the desired data into the buffer. With such an attack, the attacker would be able to store data in memory almost anywhere after the start of the buffer, possibly modifying other data that is always addressable or even statisically addressable at a point after the start of the buffer.

127 A buffer attack allows an attacker to potentially bypass tests designed to keep the system secure. Depending on the severity of this offset hole, arbitrary exploitation is possible (a simple one-byte write into the base register on x86 may lead to arbitrary control over program flow).

136 * Always review the documentation of functions like strlen(3), and be completely sure of what they return. Is it a count of characters in or operated on in a string, or is it an index into a string, and does the value include or exclude a terminating ***NUL*** character.

191 By not specifying the format of the data passed to printf and simply passing the variable msg, any format characters contained within the msg variable will be expanded internally by printf. By abusing the various format characters available, an information leakage may occur (for example, passing %s will expand the printf to dereference a pointer from the stack [which may or may not be valid]). Clever crafting of format strings may not only allow an attacker arbitrary information leakage but arbitrary program flow (with the abuse of the %n format modifier, allowing writes to user-supplied memory addresses). Always pass a format modifier to the *printf family of functions to avoid the format string bug.

241 Lack of synchronization in certain areas of your application code may lead to the creation of a race condition. When non-atomic code is written with the assumption of non-divisability (being atomic), a new category of programming bugs open, many of which are potentially exploitable. Race conditions can prop up in signal handling code, I/O code, multi-threaded logic code and more.

273 Imagine a suid application allows execution of any file with identical MD5s (as the example above). What if a user was to replace the file after the MD5() function is called and before execlp()? Rather than executing the expected file, any arbitrary file may be called. Similar race conditions may be found in the insecure usage of mktemp() and friends.