Skip to content

Format string vulnerability in scanf functions

Description

The scanf functions are used to parse input from a string, but they can be vulnerable to format string attacks if the format string is not properly sanitized. This can lead to buffer overflows and potentially allow an attacker to execute arbitrary code.

Examples

Insecure Code

c
sscanf(str, "%s", &buf);

Secure Code

c
sscanf(str, "%10s", &buf);

Remediation

Use a format string with a limited field width to prevent buffer overflows, such as "%10s" instead of "%s".

Rule Details

FieldValue
IDCODE-0563
CategoryInjection
SeverityHIGH
CWECWE-120
ConfidenceHIGH
ImpactHIGH
LikelihoodMEDIUM
ExploitabilityEASY
Tagsformat string vulnerability, buffer overflow
OWASPA1:2017-Injection, A03:2021-Injection