Home > Articles > Security > Software Security

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

2.3 String Vulnerabilities

Figure 2–9 shows a simple example of a program that checks a user password and grants or denies access. This program shows how strings are used/misused and is not an exemplar for password checking.

The get password program starts in the main() routine (line 8). The first line executed is the puts() call that prints out a string literal (line 10). The puts() function is defined in C99 as a character input/output function, is declared in stdio.h, and writes a string to the input stream pointed to by

 1. bool IsPasswordOkay(void) {
 2. char Password[12];
 3. gets(Password); 
4. if (!strcmp(Password, "goodpass")) 
5. return(true); 
6. else return(false);
 7. }
 8. void main(void) {
 9. bool PwStatus; 
10. puts("Enter password:"); 
11. PwStatus = IsPasswordOkay(); 
12. if (PwStatus == false) { 
13. puts("Access denied"); 
14. exit(-1); 
15. } 
16. else puts("Access granted"); 
17. }

Figure 2–9. Get password program

stdin followed by a newline character ('\n'). The IsPasswordOkay() function is called to retrieve a password from the user (line 11). The function returns a boolean value: true if the password is valid, false if it is not. The value of PwStatus is tested and access is allowed or denied (lines 12–16).

The IsPasswordOkay() function (line 1) uses the gets() function to read characters from the input stream (pointed to by stdin) into the array pointed to by Password until an EOF is encountered or a newline character is read. Any newline character is discarded, and a null character is written immediately after the last character read into the array. The strcmp() function defined in string.h compares the string pointed to by Password to the string literal "goodpass" and returns an integer value of zero if the strings are equal and a nonzero integer value if they are not (lines 4–6). The IsPasswordOkay() function returns true if the password is "goodpass" and the main() function consequently grants access.

Figure 2–10 shows two separate runs of the program. In the first run, the user enters the correct password and is granted access. In the second run, an incorrect password is provided and access is denied. Unfortunately, this program contains a security flaw that allows an attacker to bypass the password protection logic and gain access to the program. Can you identify this flaw?

Security Flaw

The security flaw in the get password program that allows an attacker to gain unauthorized access is caused by the call to gets() (line 3). The gets() function, as already noted, copies characters from standard input into Password until an EOF is encountered or a newline character is read. The Password

Figure 10

Figure 2–10. Program execution

2.3 String Vulnerabilities

array, however, contains only enough space for an 11-character password and a trailing null byte. This condition results in writing beyond the bounds of the Password array if the input is greater than 11 characters in length, as shown by Figure 2–11. The condition that allows an out-of-bounds write to occur is referred to in security circles as a buffer overflow. A buffer overflow is actually a runtime event. The condition that allows a buffer overflow to occur (in this case) is an unbounded string read. Before looking at how this buffer overflow poses a security risk, we first need to understand buffer overflows and process memory organization in general.

Buffer Overflows

Buffer overflows occur when data is written outside of the boundaries of the memory allocated to a particular data structure. C and C++ are susceptible to buffer overflows because these languages (a) define strings as null-terminated arrays of characters; (b) do not perform implicit bounds checking; and (c) provide standard library calls for strings that do not enforce bounds checking.

Depending on the location of the memory and the size of the overflow, a buffer overflow may go undetected but can corrupt data, cause erratic behavior, or terminate the program abnormally.

Buffer overflows are troublesome in that they are not always discovered during the development and testing of software applications. C and C++ compilers do not always identify software flaws that can lead to buffer overflows during compilation or report out-of-bound writes at runtime. Dynamic analysis tools can be used to discover buffer overflows as long as the test data precipitates a detectable overflow.

Figure 11

Figure 2–11. Writing beyond array bounds

Not all buffer overflows lead to software vulnerabilities. However, a buffer overflow can lead to a vulnerability if an attacker can manipulate user-con-trolled inputs to exploit the security flaw. There are, for example, well-known techniques for overwriting frames in the stack to execute arbitrary code. Buffer overflows can also be exploited in heap or static memory areas by overwriting data structures in adjacent memory.

Before examining how these exploits behave, it is useful to understand how process memory is organized and managed. If you are already familiar with process memory organization, execution stack, and heap management, skip to Section 2.5.

  • + Share This
  • 🔖 Save To Your Account