SV.TAINTED.BINOP

Use of Unvalidated Integers in Binary Operations

Whenever input data is accepted from the user or the outside environment, it should be validated for type, length, format, and range before it is used. Until this data is properly validated, it is said to be tainted. The SV.TAINTED family of checkers looks for the use of tainted data in code.

The SV.TAINTED.BINOP checker flags code that uses tainted data in arithmetic binary operations, such as addition, subtraction, or multiplication.

Vulnerability and risk

When integer data input to the code is not validated properly and is used as an operand to a binary operation, the result can be an integer overflow or wraparound. This potential situation could allow an attacker to alter the normal control flow, causing an unexpected program behavior. In the worst-case scenarios, 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

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 are 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

Copy
 int get_altitude();
 void set_altitude(int);

 int get_untrusted() {
     int i;
     scanf("%d", &i);
     return i;
 }

 extern const int max_altitude;

 void increase_altitude() {
     int current_altitude = get_altitude();
     int shift = get_untrusted();
     if (shift > 0) {
         int new_altitude = current_altitude + shift;
         if (new_altitude < max_altitude) {
             set_altitude(new_altitude);
         }
     }
 }

In the above example, an attacker can provide an arbitrarily large value for variable 'new_altitude'. If the binary operation at line 16 overflows, this can result in a negative value of variable 'new_altitude', and cause unexpected program behavior.

Klocwork reports an SV.TAINTED.BINOP defect at line 11, indicating: "Unvalidated integer value 'shift' that is received from 'get_untrusted' at line 14 is used as an operand to a binary operator at line 16".

Fixed code example

Copy
 int get_altitude();
 void set_altitude(int);

 int get_untrusted() {
     int i;
     scanf("%d", &i);
     return i;
 }

 extern const int max_altitude;
 extern const int max_trusted_shift;

 void increase_altitude() {
     int current_altitude = get_altitude();
     int shift = get_untrusted();
     if (shift > 0 && shift < max_trusted_shift) {
         int new_altitude = current_altitude + shift;
         if (new_altitude < max_altitude) {
             set_altitude(new_altitude);
         }
     }
 } 

The Klocwork checker no longer produces the defect report here, since the integer value 'shift' is validated at line 16 before being used in the binary operation.

Security training

Application security training materials provided by Secure Code Warrior.