Strategies in C to Avoid Common Buffer Overflow Errors
Strategies in C to Avoid Common Buffer Overflow Errors
How can you avoid common buffer overflow errors in C? Check out this strategy to prevent future vulnerabilities and ensure better security.
Join the DZone community and get the full member experience.Join For Free
Many are the variables to be considered in order to describe the relevance of a topic as cybercrimes. All of them represent a justification for employing efforts toward the development of a programming code each more secure. In this context, a very representative example is the costs related to crimes exploiting failures in insecure codes. Anderson et al. (2013) presented a synthetic vision of the financial damage caused by those criminal actions. According to the authors, the costs generated by cyber tax frauds summed a total of 125,000 millions of dollars only in the year 2011.
Although a huge number of vulnerabilities have already been identified, many other ones are found every year. Some of the most common ones are related to the manipulation of variables storing data of type String and Integer. In general, hackers and other types of attackers take advantage of unappropriated memory allocations or of errors during the operations involving those data types. A very typical exploited error is known as buffer overflow. Mitigation strategies to avoid this kind of threat requires a sharp domain of the programming language used for developing the code. Besides, a strong understanding of the computer hierarchy of memory and the way data are represented on them may help programmers to develop software more secure and less error-prone. Hereafter, the aforementioned vulnerabilities will be discussed more deeply and the accordingly mitigation strategies will also be described in more detail.
Common Buffer Overflow Vulnerabilities
According to Seacord (2013, p. 283) "a vulnerability is a set of conditions that allows violation of an explicit or implicit security policy". Several situations can lead to failures in the operations with integers. A common case involves truncation errors generated by trying to store a value in a data type that is smaller than necessary to represent it. The following code shows an example of such an error. Although the size of the two arguments of the program is calculated and summed considering one more position for the null-terminator, the data type used to store the result of the arithmetic operation can produce a truncation error (line 3). A malicious user could enter arguments whose total sum cannot be stored in an unsigned short type. In a situation like that, the dynamical allocation (line 4) executed for holding the concatenation of the two informed strings (lines 5 and 6) would not reserve the correct number of memory positions because of a truncation error.
Another integer-related vulnerability is known as wraparound. CWE (2006) states that this failure takes place "when the logic assumes that the resulting value will always be larger than the original value". The second code below presents a real sample of that vulnerability which was identified by Peslyak (2000) in JPEG files (Alexander Peslyak is better known as Solar Designer). Two bytes are dedicated to comments in a JPEG file. Because of this, the len parameter is decremented by 2 (line 3) and memory is allocated for storing the comment (line 4). The function getComment assumes the len value will always be larger than 2. Nevertheless, if an attacker passes 1 as len value, a negative value is informed to the malloc function. In this case, the behavior is dependent on the function implementation, which can lead to unsecured execution flows.
As well as it can be observed in Integer errors, the number of String-related vulnerabilities is also widely known. A simple error that represents a threat to the security of any system involves the null-termination of strings. The third code below describes an example of this error which was presented by Seacord (2013, p. 48). The loop depends on a condition to be satisfied in order to interrupt its iterations (line 5). Nevertheless, the value stored in the ntbs string is from a copy done via the strncpy function which copies only the number of characters passed to it - the copy does not include a null-terminator. Consequently, the generated ntbs string is not null-terminated.
Avoiding Buffer Overflow Errors
All the presented vulnerabilities can be mitigated by simple interventions on the code. The described string null-terminator error can be worked around by adding a succinct excerpt of code that is responsible for setting a null-terminator at the last position of the string. The code below shows a version of the vulnerable code with the null-terminator error fixed.
The wraparound error detailed in the second presented code may be avoided through the inclusion of a straightforward checking of the parameter len so that to ensure it is in the range of allowed values. Another very simple modification can also fix the truncation vulnerability presented in the first code. Since the problem involves storing eventual values that cannot be represented by an unsigned short int type, changing the type of the total variable to size_t it will guarantee a sufficient precision to represent the value.
The three types of considered vulnerabilities showed that an accurate manner to develop software code is enough to avoid many of the most commons security errors. This is the case of the buffer overflow String-related failures analyzed. By simply ensuring that a null-terminator is set at the end of each string, a well-known error can be worked around. In the same way, many Integer-related errors can be prevented by doing simple numbers bounds checking before carrying out an operation. Additional help to detect vulnerabilities of this type can be the use of some compilers configurations like the -ftrapv flag from GCC which according to Seacord (2013, p. 75) "generates traps for signed overflow" on some arithmetic operations.
Published at DZone with permission of Thiago Nascimento . See the original article here.
Opinions expressed by DZone contributors are their own.