Rule Definition
The 'sprintf()' and 'vsprintf()' functions can lead to buffer overflow if used improperly. More secure functions like 'snprintf()' or 'vsnprintf()' are available and should be used.
Remediation
Verify if strings are null-terminated and insert buffer overflow detection in the code. You can also convert existing 'sprintf()' calls to 'snprintf()' calls.
Violation Code Sample
void main(int argc, char **argv)
{
char cmds[128];
sprintf(cmds, "COMMAND: %s\n", argv[0]);
}
Fixed Code Sample
/* Convert to snprintf */
void main(int argc, char **argv)
{
char cmds[128];
char format_string = "COMMAND: %s\n";
snprintf(cmds, format_string, argv[0], 128-strlen(format_string) + 1);
}
Reference
Build Security In (https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding)
Related Technologies
Technical Criterion
Secure Coding - API Abuse
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.