Variadic functions accept a variable number of arguments but are problematic. Variadic functions define an implicit contract between the function writer and the function user that allows the function to determine the number of arguments passed in any particular invocation. Failure to enforce this contract may result in undefined behavior.
Argument Processing
Wiki Markup |
---|
In the following code example, the variadic function {{average()}} calculates the average value of the positive integer arguments passed to the function \[[Seacord 05c|AA. C References#Seacord 05c]\]. The function processes arguments until it encounters an argument with the value of {{va_eol}} ({{\-1}}). |
...
Note that va_start()
must be called to initialize the argument list and that va_end()
must be called when finished with a variable argument list.
...
Noncompliant Code Example
In this non-compliant noncompliant code example, the average()
function is called as follows:
...
The omission of the va_eol
terminating value means that the function will continue to process values from the stack until it encounters a va_eol
by coincidence or an error occurs.
Compliant Solution
This compliant solution enforces the contract by adding va_eol
as the final argument.
Code Block | ||
---|---|---|
| ||
int avg = average(1, 4, 6, 4, 1, va_eol); |
...
Noncompliant Code Example
Another common mistake is to use more conversion specifiers than supplied arguments, as shown in this non-compliant noncompliant coding example.
Code Block | ||
---|---|---|
| ||
const char const *error_msg = "Resource not available to user."; /* ... */ printf("Error (%s): %s", error_msg); |
This results in non-existent arguments being processed by the function, potentially leaking information about the process.
Compliant Solution
This compliant solution matches the number of format specifiers with the number of variable arguments.
Code Block | ||
---|---|---|
| ||
const char const *error_msg = "Resource not available to user."; /* ... */ printf("Error: %s", error_msg); |
Argument List Caveats
C99 functions that accept the variadic primitive va_list
as an argument pose an additional risk. Calls to vfprintf()
, vfscanf()
, vprintf()
, vscanf()
, vsnprintf()
, vsprintf()
, and vsscanf()
use the va_arg()
macro, invalidating the parameterized va_list
. Consequently, once a va_list
is passed as an argument to any of these functions, it cannot be used again without a call to va_end()
followed by a call to va_start()
.
Risk Assessment
Incorrectly using a variadic function can result in abnormal program termination or unintended information disclosure.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL10-A C | high | probable | high | P6 | L2 |
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.15, "Variable arguments," and Section 7.19.6.8, "The {{vfprintf}} function" \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "OTR Subprogram Signature Mismatch" \[[MISRA 04|AA. C References#MISRA 04]\] Rule 16.1 \[[Seacord 05c|AA. C References#Seacord 05c]\] |
...
DCL09-C. Declare functions that return an errno error code with a return type of errno_t 02. Declarations and Initialization (DCL) DCL11-AC. Ensure type consistency when using variadic functions