SV.FMTSTR.GENERICFormat string vulnerabilityWhen format strings aren't explicit, they can be injected from outside the code, which means that an attacker may be able to provide a specially crafted format string to execute arbitrary code. This type of weakness can typically be introduced in
The SV.FMTSTR.GENERIC checker finds instances of format strings that can be affected by the user. Vulnerability and riskExternally controlled format strings in printf functions can lead to buffer overflows and data representation problems. This type of vulnerability may allow local or remote attackers to cause a denial-of-service (DoS), and possibly execute arbitrary code through format specifiers that are injected into messages. Mitigation and preventionTo avoid format string issues
Vulnerable code example 11 int main() 2 { 3 printf(some_unknown_function("This is suspicious")); /* SV.FMTSTR.GENERIC reported here */ 4 5 return 0; 6 } Fixed code example 1Printf(gettext(str)) calls are permissible, as shown in this example. 1 int main() 2 { 3 printf(gettext("This should be OK")); /* No defect reported here */ 4 return 0; 5 } Vulnerable code example 21 int main() 2 { 3 printf(some_unknown_function("This is suspicious")); /* SV.FMTSTR.GENERIC reported here */ 4 return 0; 5 } Fixed code example 2If possible, replace printf(str) calls with printf("%s",str) to specify a character string. 1 int main() 2 { 3 printf("%s",some_unknown_function("This is better")); /* SV.FMTSTR.GENERIC not reported */ 4 return 0; 5 } Related checkersExternal guidance
|