


#define REALLOC_ARRAY (pointer, number, type) \ ((type *)reallocarray(NULL, number, sizeof(type))) #define MALLOC(type) ((type *)reallocarray(NULL, 1, sizeof(type))) #define MALLOC_ARRAY (number, type) \ For this we introduce the C preprocessor macros MALLOC(type), MALLOC_ARRAY(number, type) and REALLOC_ARRAY(pointer, number, type). With the help of the reallocarray() function we can replace the potential unsafe malloc() and realloc() functions. If an integer overflow is detected, reallocarray() returns NULL and set errno to ENOMEM. This means, that in reallocarray() the result of the multiplication is checked for an integer overflow before calling realloc(). The reallocarray() function is similar to realloc() except it operates on nmemb members of size size and checks for integer overflow in the calculation nmemb x size. This new function has integrated integer overflow detection, and is described in the manpage as follows: The OpenBSD 5.6 release introduces a very helpful new libc function reallocarray(3). #include #include #include int main( int argc, char **argv)įor (i = 0 i 0 check. For that, a buffer mem is allocated with the size of 5 * sizeof(char *). In the preceding program execution the character A is printed 5 times. The following vulnerable program int-example is used to print the character A as many times as the user specifies. In other words, when an integer overflow occurs, the value may wrap to result in a small or negative number. So the size of the result is truncated to a size that fits into the available process register width. Since the computation overflows, the arithmetic operation is handled in the following way: c = ((size_t) 0xffff + 0x1) % 0x10000 More precise, according to the C standard unsigned integer operations do wrap around, the C Standard, 6.2.5, paragraph 9, states:Ī computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type. In C programming language, a computation of unsigned integer values can never overflow, this means that UINT_MAX + 1 yields zero. The value 0x10000 is too large for a 16 bit binary register, so the addition results in an arithmetic overflow. If we add a and b and store the result in c, the addition would lead to an arithmetic overflow: c = a + b For a, the maximum 16 bit representable value 0xffff (hexadecimal value of 65535) is assigned, and for b the value of 0x1 (hexadecimal value of 1).

Let's assume we have three 16 bit unsigned integer values a, b and c.

The following example helps to clarify what exactly leads to an arithmetic overflow. Typical process register widths are shown in the following table. Process registers represent an amount of storage available in digital processors and its width defines the range of values that can be represented. To clarify the problem, I'll introduce the term process register. Integer overflows occur when the result of an arithmetic operation is a value, that is too large to fit in the available storage space.
#Integer overflow software#
In their 2011 report MITRE places integer overflows in the “Top 25 Most Dangerous Software Errors”. These errors are also a source of serious vulnerabilities, such as integer overflow errors in OpenSSH and Firefox, both of which allow attackers to execute arbitrary code. These errors can lead to serious software failures, e.g., a truncation error on a cast of a floating point value to a 16-bit integer played a crucial role in the destruction of Ariane 5 flight 501 in 1996. A short paragraph in Understanding Integer Overflow in C/C++ (Will Dietz, Peng Li, John Regehr, and Vikram Adve) highlights the scope of such errors: Integer overflows are known bugs in C which can lead to exploitable vulnerabilities.
