SV.TAINTED.ALLOC_SIZEUnvalidated input used in memory allocationWhenever input is accepted from the user or the outside environment, it should be validated for type, length, format, and range before it is used. Until properly validated, the data is said to be tainted. The SV.TAINTED family of checkers looks for the use of tainted data in code. The SV.TAINTED.ALLOC_SIZE checker flags code that uses tainted data in determining the size of a memory allocation. Vulnerability and riskWhen input to code isn't validated properly, an attacker can craft the input in a form that isn't expected by the application. The receipt of unintended input can result in altered control flow, arbitrary resource control, and arbitrary code execution. With this sort of opportunity, an attacker could
Allocating memory using integers supplied by the user can lead to excessive resource consumption. If the user passes an extremely large integer, the application will allocate an extremely large quantity of memory. This will result in the application consuming the system's memory and possibly bringing the system to a halt and triggering a denial-of-service (DoS) attack. Mitigation and preventionTo avoid tainted input errors:
Vulnerable code example1 char *buffer = NULL; 2 3 void allocateBuffer() 4 { 5 unsigned size; 6 scanf("%u", &size); 7 buffer = malloc(size); 8 } Klocwork produces an issue report at line 7 indicating that an unvalidated integer value 'size' is received through a call to 'scanf' at line 6 and can be used to alter memory allocation size through a call to 'malloc' at line 7. In this case, the SV.TAINTED.ALLOC_SIZE checker finds code that uses potentially tainted data in determining the size of a memory allocation. Fixed code example1 #define MAX_BUFFER_SIZE 512 2 char *buffer = NULL; 3 4 void allocateBuffer() 5 { 6 unsigned size; 7 scanf("%u", &size); 8 // validate input before using it in a memory allocation 9 if(size <= MAX_BUFFER_SIZE) 10 { 11 buffer = malloc(size); 12 } 13 } The Klocwork checker no longer produces an issue report because the integer value 'size' is validated before being used in a memory allocation at line 11. Related checkersExternal guidance
|