Functions that takes no parameter should explicitly declare a void parameter as in their parameter list. This holds true during both the declaration and definition section (and they should match), especially given that many Many compilers today still allow implicitly declared functions, even though C99 has eliminated itthem.
Wiki Markup |
---|
Defining a function with a void orargument withoutlist voiddiffers isfrom notdeclaring the samefunction with no arguments, because in the latter case the compiler will not check whether the function is called with parameters at all \[[C void usage|http://tigcc.ticalc.org/doc/keywords.html#void]\]. Thus, function calling with arbitrary parameters will be accepted without a warning at compile time. |
Failure to declare a void parameter will results result in ambiguous functional interface between caller and callee, and even sensitive information outflow.
There is a similar rule that deals with parameter type in a more general sense: \[[DCL07-C. Include the appropriate type information in function declarators|DCL07-C. Include the appropriate type information in function declarators]\]. Wiki Markup
In C++ the usage of foo()
and foo(void)
has exactly the same meaning and effect, so this rule doesn't apply to C++.
But it is still recommended to explicitly declare foo(void)
instead of foo()
to distinguish from foo(...)
, which will then takes take arbitrary parameters.
Noncompliant Code Example (ambiguous interface)
In this particular example, the caller gives a call to foo
with a parameter specified as 3. Because function foo()
is declared without void
, the compiler will not perform any caller check. Due to the accidental internal implementation, the function foo()
outputs i a value: 3 which matches with caller's temptation. In an inherited code base where foo and caller are developed at different timetimes, this leads can lead to the caller's belief that foo()
actually accept accepts one integer as parameter and foo()
will output the corresponding message when the parameter is changed.
In fact, no parameter specified for a function has the same meaning as arbitrary paramemters, which means that the caller can feed arbitrary number of parameters to the function. Arbitrary parameter list is useful for system logging purpose, e.g., but not the callee's intention in other situation.
Code Block | ||
---|---|---|
| ||
void foo () { int i = 3; printf("i value: %d\n", i); } ... /* caller */ foo(3); |
Compliant Solution (ambiguous interface)
In this example, void
is explicitly specified as a parameter.
Code Block | ||
---|---|---|
| ||
/* compile using gcc4.3.3 */ void foo (void) { int i = 3; printf("i value: %d\n", i); } |
...
In gcc-4.3.3, when the above compliant solution is used and foo(3)
is called, gcc will complain issue the following diagnostic, which alerts the programmer of the misuse of the function interface.
Code Block | |
---|---|
#FFCCCC | error: too many arguments to function âfooâ |
...
Another possible vulnerability is the leak of privileged information. In the above following example, suppose a user with high privilege feed privileges feeds some secret input into the caller, then caller will pass the info to foo()
. Because of the way function foo()
is defined, it is easy to assume that there 's is no way that foo()
can retrieve info from caller. However, because the value of i
is really passed into stack (before the return address of caller), a malicious programmer can change the internal implmentation and copy the value manually and contain it in a less privileged file.
Code Block | ||
---|---|---|
| ||
/* compile using gcc4.3.3 */ void foo () { /* use asm code to retrieve i * implicitly from caller * and transfer it to a less privilege file */ } ... /* caller */ foo(i); /* i is fed from user input */ |
...
Code Block | ||
---|---|---|
| ||
void foo (void) { int i = 3; printf("i value: %d\n", i); } |
Again, the simple simplest solution is to explicitly specify void
as the only parameter explicitly.
Risk Assessment
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL20-C | medium | probable | low | P12 | L1 |
...