Null-terminated byte strings (NTBS) must contain a nullNULL-termination character at or before the address of the last element of the array before they can be safely passed as arguments to standard string handling functions such as strcpy()
or strlen()
. This is because these functions, as well as other string handling functions defined by C99, depend on the existence of a nullNULL-termination character to determine the length of a string. Similarly, NTBS must be nullNULL-terminated before iterating on a character array where the termination condition of the loop depends on the existence of a nullNULL-termination character within the memory allocated for the string, as in the following example:
...
Failure to properly terminate nullNULL-terminated byte strings can result in buffer overflows and other undefined behavior.
...
One method to decrease memory usage in critical situations when all available memory has been exhausted is to use the realloc()
function to half the size of message strings. The standard realloc()
function has no concept of nullNULL-terminated byte strings. As a result, if realloc()
is called to decrease the memory allocated for a nullNULL-terminated byte string, the null NULL terminator may get truncated.
The following non-compliant code example fails to ensure that cur_msg
is properly nullNULL-terminated:
Code Block | ||
---|---|---|
| ||
char *cur_msg = NULL; size_t cur_msg_size = 1024; /* ... */ void lessen_memory_usage(void) { char *temp; size_t temp_size; /* ... */ if (cur_msg != NULL) { temp_size = cur_msg_size/2 + 1; temp = realloc(cur_msg, temp_size); if (temp == NULL) { /* Handle error condition */ } cur_msg = temp; cur_msg_size = temp_size; } } /* ... */ |
Because realloc()
does not guarantee that the string is properly nullNULL-terminated, any subsequent operation on cur_msg
that assumes a nullNULL-termination character may result in undefined behavior.
...
In this compliant solution, the lessen_memory_usage()
function ensures that the resulting string is always properly nullNULL-terminated.
Code Block | ||
---|---|---|
| ||
char *cur_msg = NULL; size_t cur_msg_size = 1024; /* ... */ void lessen_memory_usage(void) { char *temp; size_t temp_size; /* ... */ if (cur_msg != NULL) { temp_size = cur_msg_size/2 + 1; temp = realloc(cur_msg, temp_size); if (temp == NULL) { /* Handle error condition */ } cur_msg = temp; cur_msg_size = temp_size; cur_msg[cur_msg_size - 1] = '\0'; /* ensure string is nullNULL-terminated */ } } /* ... */ |
...
- Presume that all char* parameters are NT(nullNULL-terminated). We must check that they are still NT at the end of the function. Additionally, the return value must be NT. We will also check that they are NT before being passed to another function.
- Any exceptions to the NT rule (functions that accept/return open strings) are specified separately. Given that this is C, the best option might be two hardcoded handling routines in the analysis. If the function either accepts an open string (not null NULL terminated) or can return an open string, we can write some code to specify this. The analysis calls these handling routines to retrieve these specifications. Another option would be to utilize the preprocessor to write in-code specifications. However, this is not in the style of C programmers. Additionally, we can't add these specs to libraries that way. Given the environment, a separate specification, in C, is probably the best option.
- The integer range analysis tracks the lengths of char*s.
- We use a tuple lattice for the analysis. The lattice has 4 elements, bottom, NT(null NULL terminating), O(open) and top(unknown).
- Use the specifications (or the default of NT) to set the initial lattice element for each char*.
- If we index into the string and set a character to '\0', move the string to NT. This only occurs if the index is less than the minimum size of the string. (The integer analysis must be aware of strlen and that it works properly only on NT strings.)
- Check that the parameters to all functions match the specifications. If not, cause an error.
- At the end of the function, Check that the return value and the parameters match the specification for the function. If not, cause an error.
Wiki Markup |
---|
There is a question of what to do about character arrays. One option is to assume that char\[\] is open, and using it as a char\* means that we first must make it nullNULL terminating. This could get annoying for developers very quickly. I think it's better to treat char\[\] as char*, that is, we assume NT and check for it. If the exception case does occur, it will have to be specified. |
This analysis also impacts STR03-A. Do not inadvertently truncate a NULL terminated byte string, STR07-A. Take care when calling realloc() on a NULL terminated byte string, and STR31-C. Guarantee that storage for strings has sufficient space for character data and the NULL terminator.
Alternative Strategies
Testing
...
Risk Assessment
Failure to properly null NULL terminate nullNULL-terminated byte strings can result in buffer overflows and the execution of arbitrary code with the permissions of the vulnerable process or unintended information disclosure.
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Wiki Markup |
---|
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 7.1.1, "Definitions of terms," Section 7.20.3.4 "The realloc function," and Section 7.21, "String handling <string.h>" \[[ISO/IEC TR 24731-2006|AA. C References#ISO/IEC TR 24731-2006]\] Section 6.7.1.4, "The strncpy_s function" \[[Schwarz 05|AA. C References#Schwarz 05]\] \[[Seacord 05|AA. C References#Seacord 05]\] Chapter 2, "Strings" \[[Viega 05|AA. C References#Viega 05]\] Section 5.2.14, "Miscalculated nullNULL termination" |