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

ABV.TAINTED

Buffer overflow-array index from tainted input out of bounds

ABV.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 risk

Buffer overflows are frequently the source of application attacks and exploits.

Mitigation and prevention

To avoid the possibility of these attacks from tainted input

  • make sure you insert a validation condition before the line in which the overflow can occur
  • consider all the potentially relevant input properties of the input, including length, type of input, full range of acceptable values, missing or extra inputs, syntax, consistency in related fields, and conformance to business rules
  • make sure the minimum as well as maximum of a range is verified, so that negative values cannot be mistakenly accepted

Vulnerable code example

1  #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 example

1  #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.

Extension

This checker can be extended through the Klocwork knowledge base. See Tuning C/C++ analysis for more information.