CS.RESOURCE.LOOP

Object Allocation inside a loop

This checker reports a defect whenever any object is created inside the body of a loop that has no explicit exit condition. While some resources might be quickly disposed of with the garbage collector if no references to the object 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 or resources and program failure.

Vulnerability and risk

The software does not properly control the allocation and maintenance of a limited resource, thereby enabling an actor to influence the amount of resources consumed, eventually leading to the potential exhaustion of available resources. Limited resources include memory, file system storage, database connection pool entries, and CPU. If an attacker can trigger the allocation of these limited resources, but the number or size of the resources is not controlled, then the attacker could initiate a Denial of Service attack that consumes all available resources. Denial of Service attacks prevent valid users from accessing the software, and it can potentially have an impact on the surrounding environment.

Vulnerable code example 1

Copy
   namespace ResourceLoop
   {
     class TestResourceLoop
     {
       public static void ResourceLoopExample1()
       {
          for (;;){
            Test obj = new Test();         // CS.RESOURCE.LOOP
            -----
           -----
         }
       } 
    }
    class Test
    {
     ---
     ---
     }
  }

In the above example, the attacker can trigger the ResourceLoopExample1 method where the Class Test can have an uncontrolled resource allocation. The available resources can be consumed by the uncontrolled resource consumption.

Klocwork reports a CS.RESOURCE.LOOP defect at line 8, indicating that “An object of type 'Test' is allocated inside a potentially infinite loop that can cause uncontrolled limited resource consumption”.

Fixed code example 1a

Copy
   namespace ResourceLoop
   {
     class TestResourceLoop
     {
       public static void ResourceLoopExample1()
       {
               resource_count = 0;
               for (;;){
           Test obj = new Test(); 
          ++resource_count;
          if (resource_count > MAX_RESOURCES_TO_BE_CONSUMED) {
            break;
          }
          -----
        }
      } 
    }
    class Test
    {
      ---
      ---
    }
  }

Klocwork no longer reports a defect, since the for loop has a conditional exit by using the break statement.

Fixed code example 1b

Copy
   namespace ResourceLoop
   {
     class TestResourceLoop
     {
       public static void ResourceLoopExample1()
       {
               resource_count = 0;
               for (; resource_count > MAX_RESOURCES_TO_BE_CONSUMED; resource_count++){
           Test obj = new Test
          -----
        }
      } 
    }
    class Test
    {
      ---
      ---
    }
  }

Klocwork no longer reports a defect, since the loop is controlled by the resource_count variable and compared with MAX_RESOURCES_TO_BE_CONSUMED as a condition of the loop.

Vulnerable code example 2

Copy
   namespace ResourceLoop
   {
     class TestResourceLoop
     {
       public static void ResourceLoopExample2()
       {
               while(true){
           int[] obj = new int[SOME_NUMBER];         // CS.RESOURCE.LOOP
           -----
          -----
        }
      } 
    }
  }

In the above example, an attacker can trigger the ResourceLoopExample2 method where the Class Test can have an uncontrolled resource allocation. The available resources can be consumed by the uncontrolled creation of array objects.

Klocwork reports a CS.RESOURCE.LOOP defect at line 8, indicating that “An object of type 'int[ ]' is allocated inside a potentially infinite loop that can cause uncontrolled limited resource consumption”.

Fixed code example 2

Copy
   namespace ResourceLoop
   {
     class TestResourceLoop
     {
       public static void ResourceLoopExample2()
       {
               while(true){
           int[] obj = new int[SOME_NUMBER]; 
           number_of_resources += obj.length;
          if (number_of_resources > MAX_RESOURCES_TO_BE_CONSUMED){
            break;
          }
  
          -----
          -----
        }
      } 
    }
  }

Klocwork no longer reports a defect, since the uncontrolled while loop has been conditionally stopped by the break statement.

Vulnerable code example 3

Copy
   namespace ResourceLoop
   {
     class TestResourceLoop
     {
       public static void ResourceLoopExample3()
       {
               do {
           Test obj = new { X = 1, Y = 1 };         // CS.RESOURCE.LOOP
           -----
          -----
        } while(true);
      } 
    }
    class Test
    {
      ---
      ---
    }
  }

In the above example, an attacker can trigger the ResourceLoopExample1 method where the Class Test can have an uncontrolled resource allocation. The available resources can be consumed by the uncontrolled resource consumption.

Klocwork reports a CS.RESOURCE.LOOP defect at line 8, indicating that “An object of type '<anonymous type>' is allocated inside a potentially infinite loop that can cause uncontrolled limited resource consumption”.

Fixed code example 3

Copy
   namespace ResourceLoop
   {
     class TestResourceLoop
     {
       public static void ResourceLoopExample3()
       {
               do{
           Test obj = new { X = 1, Y = 1 }; 
           number_of_resources += 1;
          if (number_of_resources > MAX_RESOURCES_TO_BE_CONSUMED){
              break;
          }
  
          -----
          -----
        }while(true);
      } 
    }
  }

Klocwork no longer reports a defect, since the uncontrolled for loop has been conditionally stopped by the break statement.