ABV.TAINTEDBuffer overflow-array index from tainted input out of boundsABV.TAINTED checks for buffer overflows caused by unvalidated, or tainted, input data originating from the user or external devices. This checker flags execution paths through the code in which input data involved in a buffer overflow was not validated. Vulnerability and riskBuffer overflows are frequently the source of application attacks and exploits. Mitigation and preventionTo avoid the possibility of these attacks from tainted input
Vulnerable code example1 #include <stdio.h> 2 void wrapped_read(char* buf, int count) { 3 fgets(buf, count, stdin); 4 } 5 6 void TaintedAccess() 7 { 8 char buf1[12]; 9 char buf2[12]; 10 11 char dst[16]; 12 13 wrapped_read(buf1, sizeof(buf1)); 14 wrapped_read(buf2, sizeof(buf2)); 15 sprintf(dst, "%s-%s\n", buf1, buf2); 16 } Klocwork produces a buffer overflow report for line 15 indicating that unvalidated input is used as an array index to 'dst'. Array 'dst' is defined with a size of 16, but lines 13 and 14 may produce an input of 22 characters, plus terminating nulls. In this case, the input data hasn't been checked in relation to the buffer size, so it's considered to be tainted. Fixed code example1 #include <stdio.h> 2 void wrapped_read(char* buf, int count) { 3 fgets(buf, count, stdin); 4 } 5 6 void TaintedAccess() 7 { 8 char buf1[12]; 9 char buf2[12]; 10 11 char dst[25]; 12 13 wrapped_read(buf1, sizeof(buf1)); 14 wrapped_read(buf2, sizeof(buf2)); 15 sprintf(dst, "%s-%s\n", buf1, buf2); 16 } In the fixed code example, the size for 'dst' has been correctly defined as 25. Related checkersExternal guidance
ExtensionThis checker can be extended through the Klocwork knowledge base. See Tuning C/C++ analysis for more information. |