...
Unintentional truncation results in a loss of data and, in some cases, leads to software vulnerabilities.
...
Noncompliant Code Example
The standard functions strncpy()
and strncat()
copy a specified number of characters n
from a source string to a destination array. If there is no null character in the first n
characters of the source array, the result will not be null-terminated and any remaining characters are truncated.
Code Block | ||
---|---|---|
| ||
char *string_data; char a[16]; /* ... */ strncpy(a, string_data, sizeof(a)); |
Compliant Solution (adequate space)
Either the strcpy()
or strncpy()
function can be used to copy a string and a null character to a destination buffer, provided there is enough space. Care must be taken to ensure that the destination buffer is large enough to hold the string to be copied and the null byte to prevent errors such as data truncation and buffer overflow.
...
It is assumed that string_data
is null-terminated, that is, a null byte can be found within the bounds of the referenced character array. Otherwise, strlen()
will stray into other objects before finding a null byte.
Compliant Solution (TR 24731-1)
Wiki Markup |
---|
The {{strcpy_s()}} function defined in \[[ISO/IEC TR 24731-1:2007|AA. C References#ISO/IEC TR 24731-1-2007]\] provides additional safeguards, including accepting the size of the destination buffer as an additional argument (see [STR07-AC. Use TR 24731 for remediation of existing string manipulation code]). Also, {{strnlen_s()}} accepts a maximum-length argument for strings that may not be null-terminated. |
...
If a runtime constraint error is detected by either the call to strnlen_s()
or strcpy_s()
, the currently registered runtime-constraint handler is invoked. See ERR03-AC. Use runtime-constraint handlers when calling functions defined by TR24731-1 for more information on using runtime-constraint handlers with TR 24731-1 functions.
Exceptions
STR03-EX1: The intent of the programmer is to intentionally truncate the null-terminated byte string.
Risk Assessment
Truncating strings can lead to a loss of data.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
STR03-A C | medium | probable | medium | P8 | L2 |
Automated Detection
The LDRA tool suite V 7.6.0 is able to can detect violations of this recommendation.
...
The tool Compass / ROSE could detect violations in the following manner: All calls to strncpy()
and the other functions should be follwed by an assignment of a terminating character to null-terminate the string.
Related Vulnerabilities
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.21, "String handling <string.h>" \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "CJM String Termination" \[[ISO/IEC TR 24731-1:2007|AA. C References#ISO/IEC TR 24731-1-2007]\] \[[Seacord 05a|AA. C References#Seacord 05a]\] Chapter 2, "Strings" |
...
07. Characters and Strings (STR) STR04-A. Use plain char for characters in the basic character set