CS.SV.TAINTED.CALL.LOOP_BOUND.RESOURCE

Resource allocation in a loop whose exit condition is controlled by tainted data

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.CALL.LOOP_BOUND.RESOURCE checker flags code where an unvalidated integer data is passed as a call argument to a function where it is used as a loop boundary while any resource, managed or unmanaged, is allocated within that loop.

While some resources might be quickly disposed of with the garbage collector if no references to it remain at the end of the loop body, the timing of such disposal is not guaranteed. Moreover, some allocated resources can have an increased lifespan that can result in excessive resource consumption, potentially leading to the exhaustion of resources and program failure. This behavior can be exploited by the potential attackers when exit conditions from such loops are controlled by the external input.

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

Vulnerable code example 1

1  using System;
2  using System.IO;
3  namespace TaintedResource {
4    class TestTaintedResource {
5      const string fileName = "File.dat";
6  
7      public static void TaintedResourceExample1() {
8              int num = getTaintedData();
9              sinkMethod(num);               // CS.SV.TAINTED.CALL.LOOP_BOUND.RESOURCE
10     }
11  
12     public static void sinkMethod(int value) {
13       for(int i = 0; i < value; i++)
14       {
15          Test obj = new Test();
16       }
17     }
18  
19      public static int getTaintedData()
20      {
21        try
22        {
23          using (BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open)))
24          {
25            return(br.ReadInt32());
26          }
27        }
28        catch (Exception e)
29        {
30          Console.WriteLine(e);
31        }
32      }
33    }
34    class Test
35    {
36      ---
37      ---
38    }
39  }

Klocwork produces an issue report at line 14, indicating that, “One or more objects are allocated inside a loop body whose exit condition is controlled by an unvalidated integer value 'taintedIntValue' through a call to 'sinkMethod' at line 9.” In this case, potentially tainted data is used as a loop boundary, which could be exploited by a malicious user.

Fixed code example 1

1   using System;
2   using System.IO;
3   namespace TaintedResource
4   {
5     class TestTaintedResource
6     {
7       const string fileName = "File.dat";
8   
9       public static void TaintedResourceExample1()
10      {
11        int num = getTaintedData();
12        if (num > MAX_ALLOCATION_QUOTA_IN_ONE_GO)
13        {
14           return;
15        }
16        sinkMethod(num);
17      }
18  
19      public static void sinkMethod(int value) {
20        for(int i = 0; i < value; i++)
21        {
22           Test obj = new Test();
23        }
24      }
25  
26  
27      public static int getTaintedData()
28      {
29        try
30        {
31          using (BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open)))
32          {
33            return(br.ReadInt32());
34          }
35        }
36        catch (Exception e)
37        {
38          Console.WriteLine(e);
39        }
40      }
41    }
42    class Test
43    {
44      ---
45      ---
46    }
47  }

In this example, Klocwork no longer reports a defect because the integer 'num' is checked at line 12 before it's used as a loop condition.