CWE-606: Unchecked Input for Loop Condition

Description

The product does not properly check inputs that are used for loop conditions, potentially leading to a denial of service or other consequences because of excessive looping.

Submission Date :

May 7, 2007, midnight

Modification Date :

2023-06-29 00:00:00+00:00

Organization :

MITRE
Example Vulnerable Codes

Example - 1

The following example demonstrates the weakness.


foo();int i;for (i = 0; i < n; i++){}

unsigned int num;scanf("%u",&num);iterate(num);void iterate(int n){}void iterateFoo(){}

Example - 2

In the following C/C++ example the method processMessageFromSocket() will get a message from a socket, placed into a buffer, and will parse the contents of the buffer into a structure that contains the message length and the message body. A for loop is used to copy the message body into a local character string which will be passed to another method for processing.


// // get message from socket and store into buffer// 
// //Ignoring possibliity that buffer > BUFFER_SIZE// 

// // place contents of the buffer into message structure// 
// // copy message body into string for processing// 
message[index] = msg->msgBody[index];
// // process message// 
ExMessage *msg = recastBuffer(buffer);int index;for (index = 0; index < msg->msgLength; index++) {}message[index] = '\0';success = processMessage(message);
int success;char buffer[BUFFER_SIZE];char message[MESSAGE_SIZE];if (getMessage(socket, buffer, BUFFER_SIZE) > 0) {}return success;int processMessageFromSocket(int socket) {}

However, the message length variable from the structure is used as the condition for ending the for loop without validating that the message length variable accurately reflects the length of the message body (CWE-606). This can result in a buffer over-read (CWE-125) by reading from memory beyond the bounds of the buffer if the message length variable indicates a length that is longer than the size of a message body (CWE-130).

Related Weaknesses

This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.

Visit http://cwe.mitre.org/ for more details.