Avoid in-band error indicators while designing interfaces. This practice is commonly used by C library functions but is not recommended. One example from the C standard of a troublesome in-band error indicator is EOF (see FIO34-C. Use int to capture the return value of character IO functions and FIO35-C. Use feof() and ferror() to detect end-of-file and file errors when sizeof(int) == sizeof(char)). Another problematic use of in-band error indicators from the C standard involving the size_t
and time_t
types is described by MSC31-C. Ensure that return values are compared against the proper type.
...
Noncompliant Code Example
This specific non-compliant noncompliant code example is from the Linux Kernel Mailing List archive site at http://lkml.org/, although similar examples are common.
...
The sprintf()
function returns the number of characters written in the array, not counting the terminating null character. This number is frequently added to an existing counter to keep track of the location of the index into the array. However, the call to sprintf()
can (and will) return -1 on error conditions such as an encoding error. If this happens on the first call (which is likely), the count
variable, already at zero, is decremented. If this index is subsequently used, it will result in an out-of-bounds read or write.
Compliant Solution (sprintf_m()
)
Wiki Markup |
---|
This compliant solution shows the redesigned API for {{sprintf()}} from the CERT managed string library \[[Burch 06|AA. C References#Burch06]\]. |
Code Block | ||
---|---|---|
| ||
errno_t sprintf_m( string_m buf, const string_m fmt, int *count, /* ... */ ); |
The sprintf_m()
API separates out the return status of the function from information about the number of characters written. In this case, *count
is set to the number of characters written in buf
, while the return value indicates the return status. Returning the status as the return value of the function increases the likelihood that a programmer will check the return status of the function.
...
Code Block | ||
---|---|---|
| ||
int i; rsize_t count = 0; errno_t err; for (i = 0; i < 9; ++i) { err = sprintf_m(buf + count, "%02x ", &count, ((u8 *)&slreg_num)[i]); if (err != 0) { /* handle print error */ } } err = sprintf_m(buf + count, "%02x ", &count, ((u8 *)&slreg_num)[i]); if (err != 0) { /* handle print error */ } |
Exceptions
ERR02-EX1: Null pointers are another example of an in-band error indicator. Use of null pointers is not quite as bad because it is supported by the language. According to C99 Section 6.3.2.3, "Pointers":
...
For example, the functions defined in TR24731-1 provide hooks for internal constraint violations. If a constraint violation handler is guaranteed not to return upon an error occurring, then one may safely ignore errors returned by these functions. One might accomplish this by having the constraint violation handler call abort()
, or longjmp()
, for instance.
See ERR03-AC. Use runtime-constraint handlers when calling functions defined by TR24731-1 for more on the functions defined in TR24731-1.
...
Noncompliant Code Example (TR24731-1)
In this example, the error handler returns normally, while the strcpy_s()
function's return value is not checked.
Code Block | ||
---|---|---|
| ||
constraint_handler_t handle_errors(void) { constraint_handler_t data; /* define what to do when error occurs */ return data; } /*...*/ set_constraint_handler(handle_errors); /*...*/ /* Returns zero on success */ errno_t function(char *dst1){ char src1[100] = "hello"; strcpy_s(dst1, sizeof(dst1), src1); /* At this point strcpy_s may have yielded an error and handle_errors() might have returned */ /* ... */ return 0; } |
Compliant Code Example (TR24731-1)
Here the code is rectified by having the error handler terminate the program, which ensures that strcpy_s()
never returns unless it worked perfectly.
Code Block | ||
---|---|---|
| ||
/* * The abort_handler_s() function writes a message on the * standard error stream and then calls the abort() function. */ set_constraint_handler(abort_handler_s); /*...*/ /* Returns zero on success */ errno_t function(char *dst1){ char src1[100] = "hello"; strcpy_s(dst1, sizeof(dst1), src1); /* Because abort_handler_s() never returns, we only get here if strcpy_s() succeeds. */ /* ... */ return 0; } |
Risk Analysis
The risk of using in-band error indicators is difficult to quantify and is consequently given as low. However, if the use of in-band error indicators results in programmers failing to check status codes or incorrectly checking them, the consequences can be more severe.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ERR02-A C | low | unlikely | high | P1 | L3 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Wiki Markup |
---|
\[[Burch 06|AA. C References#Burch06]\] \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.2.4, "Storage durations of objects," and Section 7.20.3, "Memory management functions" \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "NZN Returning error status" \[[ISO/IEC TR 24731-1:2007|AA. C References#ISO/IEC TR 24731-1-2007]\] |
...
12. Error Handling (ERR) ERR03-AC. Use runtime-constraint handlers when calling functions defined by TR24731-1