Null-terminated byte strings (NTBS) must contain a NULL-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 NULL-termination character to determine the length of a string. Similarly, NTBS must be NULL-terminated before iterating on a character array where the termination condition of the loop depends on the existence of a NULL-termination character within the memory allocated for the string, as in the following example:
size_t i; char ntbs[16]; /* ... */ for (i = 0; i < sizeof(ntbs); ++i) { if (ntbs[i] == '\0') break; /* ... */ }
Failure to properly terminate NULL-terminated byte strings can result in buffer overflows and other undefined behavior.
Non-Compliant Code Example (realloc()
)
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 NULL-terminated byte strings. As a result, if realloc()
is called to decrease the memory allocated for a NULL-terminated byte string, the NULL terminator may get truncated.
The following non-compliant code example fails to ensure that cur_msg
is properly NULL-terminated:
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 NULL-terminated, any subsequent operation on cur_msg
that assumes a NULL-termination character may result in undefined behavior.
Compliant Solution (realloc()
)
In this compliant solution, the lessen_memory_usage()
function ensures that the resulting string is always properly NULL-terminated.
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 NULL-terminated */ } } /* ... */
Mitigation Strategies
Static Analysis
Violations of this rule can be detected using local flow analysis assuming an integer range analysis to track the length of the strings. (Note: I am not entirely familiar with the literature on buffer-overflow analysis, but we should check that none of them already handle this scenario.)
- Presume that all char* parameters are NT(NULL-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 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 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.
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 NULL 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
It would probably be prohibitively expensive to come up with the test cases by hand. Another option is to use a static analysis to generate the test inputs for char*. However, it would still have to generate the inputs for the other values. We would still have to specify whether the function allows open strings or can return open strings, so that the dynamic analysis knows whether to report a defect. Since we still have to write the specifications, this technique will not save developer time there.
Dynamic Analysis
It seems the analysis won't be very different from the static analysis, in which case, we should just do this statically.
Inspection
An inspection would essentially grep for known problem functions and inspect the usage. Obviously, this is extremely costly, as there would be a lot of false positives, and this does not scale well. There may also be many false negatives. Say Dev A inspects a function that returns an open string. Dev A considers it ok and documents it as such, perhaps this is one of the exception cases. Dev B might be inspecting another part of the code and might not realize that Dev A allowed an open string. It might be documented, but this is not very reliable. This might lead to a false sense of confidence that since the developers hand inspected every case that the code is fine, when in fact, a miscommunication can cause a defect.
Risk Assessment
Failure to properly NULL terminate NULL-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.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
STR32-C |
3 (high) |
2 (probable) |
2 (medium) |
P12 |
L1 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
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]] Section 6.7.1.4, "The strncpy_s function"
[[Schwarz 05]]
[[Seacord 05]] Chapter 2, "Strings"
[[Viega 05]] Section 5.2.14, "Miscalculated NULL termination"