
ISO/IEC TR 24731 defines alternative versions of C standard functions that are designed to be safer replacements for existing functions. For example, ISO/IEC TR 24731 defines the strcpy_s()
, strcat_s()
, strncpy_s()
, and strncat_s()
functions as replacements for strcpy()
, strcat()
, strncpy()
, and strncat()
, respectively.
The ISO/IEC TR 24731 functions were created by Microsoft to help retrofit its existing, legacy code base in response to numerous, well-publicized security incidents over the past decade. These functions were then proposed to the ISO/IEC JTC1/SC22/ WG14 international standardization working group for the programming language C for standardization.
The strcpy_s()
function, for example, has this signature:
errno_t strcpy_s( char * restrict s1, rsize_t s1max, const char * restrict s2 );
The signature is similar to strcpy()
but takes an extra argument of type rsize_t
that specifies the maximum length of the destination buffer. (Functions that accept parameters of type rsize_t
diagnose a constraint violation if the values of those parameters are greater than RSIZE_MAX
. Extremely large object sizes are frequently a sign that an object's size was calculated incorrectly. For example, negative numbers appear as very large positive numbers when converted to an unsigned type like size_t
. For those reasons, it is sometimes beneficial to restrict the range of object sizes to detect errors. For machines with large address spaces, ISO/IEC TR 24731 recommends that RSIZE_MAX
be defined as the smaller of the size of the largest object supported or (SIZE_MAX >> 1)
, even if this limit is smaller than the size of some legitimate, but very large, objects.) The semantics are also similar. When there are no input validation errors, the strcpy_s()
function copies characters from a source string to a destination character array up to and including the terminating null character. The function returns zero on success.
The strcpy_s()
function only succeeds when the source string can be fully copied to the destination without overflowing the destination buffer. The following conditions are treated as a constraint violation:
- The source and destination pointers are checked to see if they are null.
- The maximum length of the destination buffer is checked to see if it is equal to zero, greater than
RSIZE_MAX
, or less than or equal to the length of the source string.
When a constraint violation is detected, the destination string is set to the null string and the function returns a nonzero value. In the following example, the strcpy_s()
function is used to copy src1
to dst1
.
char src1[100] = "hello"; char src2[7] = {'g','o','o','d','b','y','e'}; char dst1[6], dst2[5]; int r1, r2; r1 = strcpy_s(dst1, 6, src1); r2 = strcpy_s(dst2, 5, src2);
However, the call to copy src2
to dst2
fails because there is insufficient space available to copy the entire string, which consists of seven characters, to the destination buffer. As a result, r2
is assigned a nonzero value and dst2[0]
is set to "\0."
Users of the ISO/IEC TR 24731 functions are less likely to introduce a security flaw because the size of the destination buffer and the maximum number of characters to append must be specified. ISO/IEC TR 24731 functions also ensure null termination of the destination string.
ISO/IEC TR 24731 functions are still capable of overflowing a buffer if the maximum length of the destination buffer and number of characters to copy are incorrectly specified. As a result, these functions are not especially secure but may be useful in preventive maintenance to reduce the likelihood of vulnerabilities in an existing legacy code base.
Risk Assessment
String handling functions defined in C99 Section 7.21 and elsewhere are susceptible to common programming errors that can lead to serious, exploitable vulnerabilities. Proper use of TR 24731 functions can eliminate the majority of these issues.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
STR00-A |
3 (medium) |
2 (probable) |
2 (medium) |
P12 |
L1 |
Examples of vulnerabilities resulting from the violation of this recommendation can be found on the CERTwebsite.
Examples of vulnerabilities resulting from the violation of this rule can be found on the CERTwebsite.
References
[[ISO/IEC TR 24731-2006]]
[[ISO/IEC 9899-1999]] Section 7.21, "String handling <string.h>"
[[Seacord 05a]] Chapter 2, "Strings"
[[Seacord 05b]]