Failure to specify function prototypes results in a function being implicitly defined. Without a function prototype, the compiler will assume the the correct number and type of parameters have been supplied to a function. This can result in undefined, and perhaps unintended behavior. Given this, functions should always be declared with the appropriate function prototype.
C99 eliminated implicit function declarations from the C language [[ISO/IEC9899-1999]]. However, many compilers allow compilation of programs containing implicitly defined functions, although they may issue a warning message. These warnings should be resolved [[MSC00-A]], but do not prevent program compilation.
Non-Compliant Code Example 1
In this example, the definition of func()
expects three parameters but is supplied only two. However, because there is no prototype for func()
, the compiler assumes that the correct number of arguments has been supplied, and uses the next value on the program stack as the missing third argument.
function(1, 2); ... int func(int one, int two, int three){ printf("%d %d %d", one, two, three); return 1; }
Compliant Solution 1
To correct this example, the appropriate function prototype for func()
should be specified.
int function(int, int, int); ... function(1,2); ... int func(int one, int two, int three){ printf("%d %d %d", one, two, three); return 1; }
Non-Compliant Code Example 1
The following example is based on rule [[MEM02-A]]. The header file stdlib.h
contains the function prototype for malloc()
. Failing to include stdlib.h
causes malloc()
to be implicitly defined.
char *p = malloc(10);
Compliant Solution 2
#include <stdlib.h> ... char *p = malloc(10);
Risk Assessment
Failure to specify function prototypes can often lead to system crashes and possible denial-of-service attacks. Examples of vulnerabilities resulting from missing function prototypes include:
- CVE-2002-1236, CAN-2003-0422 - CGI crashes when called without any arguments
- CVE-2002-1531, CAN-2002-1077 - crash in HTTP request without a Content-Length field
- CAN-2002-1358 - empty elements/strings in protocol test suite affect many SSH2 servers/clients
- CAN-2003-0477 - FTP server crashes in PORT command without an argument
- CVE-2002-0107 - resultant infoleak in web server via GET requests without HTTP/1.0 version string
- CAN-2002-0596 - GET reqeust with empty parameter leads to error message infoleak (path disclosure)
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
DRAFT |
1 (medium) |
2 (possible) |
2 (medium) |
P4 |
L3 |
References
- ISO/IEC 9899 Common Warnings 2