...
These runtime constraint handlers mitigate some of the potential insecurity caused by in-band error indicators (see ERR02-C. Avoid in-band error indicators).
...
Noncompliant Code Example (TR24731-1)
In this non-compliant noncompliant example, the strcpy_s()
function is called, but no runtime constraint handler has been explicitly registered. As a result, the implementation-defined default handler will be called on a runtime error.
...
As a result, it is generally prudent to explicitly install a runtime constraint handler to ensure consistent behavior across implementations.
Compliant Code Example (TR24731-1)
This compliant solution explicitly installs a runtime constraint handler by invoking the set_constraint_handler_s()
function. This would typically be performed during system initialization and before any functions that used the mechanism were invoked.
Code Block | ||
---|---|---|
| ||
constraint_handler_t handle_errors(void) { /* handle runtime constraint error */ } /*...*/ set_constraint_handler_s(handle_errors); /*...*/ /* Returns zero on success */ errno_t function(char *dst1, size_t size){ char src1[100] = "hello"; if (strcpy_s(dst1, size, src1) != 0) { return -1; } /* ... */ return 0; } |
Compliant Code Example (Visual Studio 2008/.NET Framework 3.5)
Unfortunately, although the ISO/IEC TR 24731-1 functions were created by Microsoft, currently available versions of Microsoft Visual Studio do not support the same interface defined by the TR for installing runtime constraint handlers. Visual Studio calls these functions "invalid parameter handlers," and they are installed by calling the _set_invalid_parameter_handler()
function. The signature of the handler is also significantly different.
Code Block | ||
---|---|---|
| ||
_invalid_parameter_handler handle_errors( const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved ) { /* handle invalid parameter */ } /*...*/ _set_invalid_parameter_handler(handle_errors) /*...*/ errno_t function(char *dst1, size_t size) { char src1[100] = "hello"; if (strcpy_s(dst1, size, src1) != 0) { return -1; } /* ... */ return 0; } |
Risk Analysis
The TR24731-1 standard indicates that if no constraint handler is set, a default one executes when errors arise. The default handler is implementation-defined and "may cause the program to exit or abort." It is important to understand the behavior of the default handler for all implementations being used and replace it if the behavior is inappropriate for the application.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ERR03-A C | low | unlikely | medium | P2 | L3 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Wiki Markup |
---|
\[[ISO/IEC TR 24731-1:2007|AA. C References#ISO/IEC TR 24731-1-2007]\] Section 6.1.4, "Runtime-constraint violations", and Section 6.6.1, "Runtime-constraint handling" \[[MSDN|AA. C References#MSDN]\] "[Parameter Validation|http://msdn.microsoft.com/en-us/library/ksazx244.aspx]" |
...
12. Error Handling (ERR) ERR04-A. Choose an appropriate termination strategy