CS.SV.TAINTED.INDEX_ACCESS

Unvalidated input in array indexing

Whenever 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 CS.SV.TAINTED family of checkers looks for the use of tainted data in code.

The CS.SV.TAINTED.INDEX_ACCESS checker flags code that uses tainted data to access an array.

Vulnerability and risk

When 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

  • provide unexpected values and cause a program crash
  • cause excessive resource consumption
  • read confidential data
  • use malicious input to modify data or alter control flow
  • execute arbitrary commands

Using values supplied by the user as an array index can lead to index out-of-bounds vulnerabilities. If the vulnerable function allows for the reading from or writing to arbitrary memory, it could lead to application instability or, with a carefully constructed attack, data disclosure vulnerabilities or code injection.

Mitigation and prevention

To avoid tainted input errors:

  • understand all the potential areas in which untrusted inputs could enter your software: parameters or arguments, cookies, input read from the network, environment variables, reverse DNS lookups, query results, filenames, databases, and any external systems
  • use a whitelist or 'known good' policy for inputs, rather than relying only on a blacklist or 'known bad' strategy
  • make sure all relevant properties of the input are validated, including length, type of input, ranges, missing or extra inputs, syntax, and consistency
  • if there are security checks on the client side of an applications, make sure they're duplicated on the server side
  • if the application combines inputs from multiple sources, perform the validation after the sources have been combined

Vulnerable code example

1   namespace myNamespace
2   {    
3       class IndexValueUse
4       {
5           public static void TryUseIndexValue(int value)
6           {  
7               int[] integerArray = new int[MAX_BUFFER_SIZE];
8               int input_value = Console.Read(); 
9   	     integerArray[input_value] = value;
10   }
11      }
12  }

Klocwork produces an issue report at line 9, indicating that unvalidated integer 'input_value' received through a call to 'Console.Read()' at line 8 can be used to access an array at line 9. In this case, the CS.SV.TAINTED.INDEX_ACCESS checker flags potentially tainted data used to access an array, which could be exploited by a malicious user.

Fixed code example

1   namespace myNamespace
2   {    
3       class IndexValueUse
4       {
5           public static void TryUseIndexValue(int value)
6           {  
7               int[] integerArray = new int[MAX_BUFFER_SIZE];
8               int input_value = Console.Read();
9   
10  	     // Validate input before using it in a memory allocation 
11  	     if (input_value > 0 && input_value <= MAX_BUFFER_SIZE)
12  	     {
13  		     integerArray[input_value] = value;
14  	     }
15          }
16      }
17  }

In this example, the externally controlled data is used only after verification at line 10, and thus the code is no longer vulnerable to the buffer overflow attacks.

Security training

Application security training materials provided by Secure Code Warrior.