Improper Validation of Specified Index, Position, or Offset in Input

Incomplete Base
Structure: Simple
Description

The product receives input that is expected to specify an index, position, or offset into an indexable resource such as a buffer or file, but it does not validate or incorrectly validates that the specified index/position/offset has the required properties.

Extended Description

Often, indexable resources such as memory buffers or files can be accessed using a specific position, index, or offset, such as an index for an array or a position for a file. When untrusted input is not properly validated before it is used as an index, attackers could access (or attempt to access) unauthorized portions of these resources. This could be used to cause buffer overflows, excessive resource allocation, or trigger unexpected failures.

Common Consequences 1
Scope: Other

Impact: Varies by Context

Potential Mitigations 1
Phase: Implementation

Strategy: Input Validation

Assume all input is malicious. Use an "accept known good" input validation strategy, i.e., use a list of acceptable inputs that strictly conform to specifications. Reject any input that does not strictly conform to specifications, or transform it into something that does. When performing input validation, consider all potentially relevant properties, including length, type of input, the full range of acceptable values, missing or extra inputs, syntax, consistency across related fields, and conformance to business rules. As an example of business rule logic, "boat" may be syntactically valid because it only contains alphanumeric characters, but it is not valid if the input is only expected to contain colors such as "red" or "blue." Do not rely exclusively on looking for malicious or malformed inputs. This is likely to miss at least one undesirable input, especially if the code's environment changes. This can give attackers enough room to bypass the intended validation. However, denylists can be useful for detecting potential attacks or determining which inputs are so malformed that they should be rejected outright.

Effectiveness: High

Demonstrative Examples 3

ID : DX-134

The following example retrieves the sizes of messages for a pop3 mail server. The message sizes are retrieved from a socket that returns in a buffer the message number and the message size, the message number (num) and size (size) are extracted from the buffer and the message size is placed into an array using the message number for the array index.

Code Example:

Bad
C

/* capture the sizes of all messages / int getsizes(int sock, int count, int *sizes) { ``` ... char buf[BUFFER_SIZE]; int ok; int num, size;

c

// continue read from socket until buf only contains '.'* if (DOTLINE(buf)) ``` break; else if (sscanf(buf, "%d %d", &num, &size) == 2) sizes[num - 1] = size; } ... }

In this example the message number retrieved from the buffer could be a value that is outside the allowable range of indices for the array and could possibly be a negative number. Without proper validation of the value to be used for the array index an array overflow could occur and could potentially lead to unauthorized access to memory addresses and system crashes. The value of the array index should be validated to ensure that it is within the allowable range of indices for the array as in the following code.

Code Example:

Good
C

/* capture the sizes of all messages / int getsizes(int sock, int count, int *sizes) { ``` ... char buf[BUFFER_SIZE]; int ok; int num, size;

c

// continue read from socket until buf only contains '.'* if (DOTLINE(buf)) ``` break; else if (sscanf(buf, "%d %d", &num, &size) == 2) { if (num > 0 && num <= (unsigned)count) sizes[num - 1] = size; else

c

ID : DX-133

In the following example the method displayProductSummary is called from a Web service servlet to retrieve product summary information for display to the user. The servlet obtains the integer value of the product number from the user and passes it to the displayProductSummary method. The displayProductSummary method passes the integer value of the product number to the getProductSummary method which obtains the product summary from the array object containing the project summaries using the integer value of the product number as the array index.

Code Example:

Bad
Java

// Method called from servlet to obtain product information* public String displayProductSummary(int index) {

java
In this example the integer value used as the array index that is provided by the user may be outside the allowable range of indices for the array which may provide unexpected results or cause the application to fail. The integer value used for the array index should be validated to ensure that it is within the allowable range of indices for the array as in the following code.

Code Example:

Good
Java

// Method called from servlet to obtain product information* public String displayProductSummary(int index) {

java
An alternative in Java would be to use one of the collection objects such as ArrayList that will automatically generate an exception if an attempt is made to access an array index that is out of bounds.

Code Example:

Good
Java
java

ID : DX-90

The following example asks a user for an offset into an array to select an item.

Code Example:

Bad
C
c
The programmer allows the user to specify which element in the list to select, however an attacker can provide an out-of-bounds offset, resulting in a buffer over-read (Buffer Over-read).
Observed Examples 2
CVE-2005-0369large ID in packet used as array index
CVE-2001-1009negative array index as argument to POP LIST command
Applicable Platforms
Languages:
Not Language-Specific : Often
Modes of Introduction
Implementation
Related Weaknesses
Notes
MaintenanceThis entry is still under development and will continue to see updates and content improvements.