Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

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

1 int get_altitude();
2 void set_altitude(int);
3
4 int get_untrusted() {
5     int i;
6     scanf("%d", &i);
7     return i;
8 }
9
10 extern const int max_altitude;
11
12 void increase_altitude() {
13     int current_altitude = get_altitude();
14     int shift = get_untrusted();
15     if (shift > 0) {
16         int new_altitude = current_altitude + shift;
17         if (new_altitude < max_altitude) {
18             set_altitude(new_altitude);
19         }
20     }
21 }

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

1 int get_altitude();
2 void set_altitude(int);
3
4 int get_untrusted() {
5     int i;
6     scanf("%d", &i);
7     return i;
8 }
9
10 extern const int max_altitude;
11 extern const int max_trusted_shift;
12
13 void increase_altitude() {
14     int current_altitude = get_altitude();
15     int shift = get_untrusted();
16     if (shift > 0 && shift < max_trusted_shift) {
17         int new_altitude = current_altitude + shift;
18         if (new_altitude < max_altitude) {
19             set_altitude(new_altitude);
20         }
21     }
22 }   

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.