CWE-126

Buffer Over-read. [Improper-Control-Of-A-Resource-Through-Its-Lifetime]

Required inputs: IR, StaticSemanticAnalysis

The product reads from a buffer using buffer access mechanisms such as indexes or pointers that reference memory locations after the targeted buffer.
Demonstrative Examples
Example 1

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.

Example Language:C
    int processMessageFromSocket(int socket) {
        int success;

        char buffer[BUFFER_SIZE];
        char message[MESSAGE_SIZE];

        // get message from socket and store into buffer

        //Ignoring possibliity that buffer > BUFFER_SIZE
        if (getMessage(socket, buffer, BUFFER_SIZE) > 0) {
            // place contents of the buffer into message structure
            ExMessage *msg = recastBuffer(buffer);

            // copy message body into string for processing
            int index;
            for (index = 0; index < msg->msgLength; index++) {
                message[index] = msg->msgBody[index];
            }
            message[index] = '\0';

            // process message
            success = processMessage(message);
        }
        return success;
    }

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).

Example 2

The following C/C++ example demonstrates a buffer over-read due to a missing NULL terminator. The main method of a pattern matching utility that looks for a specific pattern within a specific file uses the string strncopy() method to copy the command line user input file name and pattern to the Filename and Pattern character arrays respectively.

Example Language:C
    int main(int argc, char **argv)
    {

        char Filename[256];
        char Pattern[32];

        /* Validate number of parameters and ensure valid content */
        ...

        /* copy filename parameter to variable, may cause off-by-one overflow */
        strncpy(Filename, argv[1], sizeof(Filename));

        /* copy pattern parameter to variable, may cause off-by-one overflow */
        strncpy(Pattern, argv[2], sizeof(Pattern));

        printf("Searching file: %s for the pattern: %s\n", Filename, Pattern);
        Scan_File(Filename, Pattern);
    }

However, the code do not take into account that strncpy() will not add a NULL terminator when the source buffer is equal in length of longer than that provide size attribute. Therefore if a user enters a filename or pattern that are the same size as (or larger than) their respective character arrays, a NULL terminator will not be added (CWE-170) which leads to the printf() read beyond the expected end of the Filename and Pattern buffers.

To fix this problem, be sure to subtract 1 from the sizeof() call to allow room for the null byte to be added.

Example Language:C
    /* copy filename parameter to variable, no off-by-one overflow */
    strncpy(Filename, argv[2], sizeof(Filename)-1);
    Filename[255]='\0';

    /* copy pattern parameter to variable, no off-by-one overflow */
    strncpy(Pattern, argv[3], sizeof(Pattern)-1);
    Pattern[31]='\0';
Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See section 9.4. "3rd-Party Licenses" in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

arithmetic_out_of_bounds

Pointer arithmetic on {node0} might create pointer outside array bounds of {name0}

None

False

out_of_bounds

Access into array is out of bounds

None

False

possible_indirect_out_of_bounds

Pointer-indirect access through {node0} might be out of bounds accessing {name0}

None

False

possible_out_of_bounds

Access into array might be out of bounds

None

False

undereferenced_arithmetic_out_of_bounds

Pointer arithmetic on {node0} might create pointer one past the end of {name0} (but not dereferenced)

None

False

undereferenced_out_of_bounds

Access is one past the end of the array (but not dereferenced)

None

False

undereferenced_possible_indirect_out_of_bounds

Pointer-indirect access through {node0} might be one past the end accessing {name0} (but not dereferenced)

None

False

undereferenced_possible_out_of_bounds

Access might be one past the end of the array (but not dereferenced)

None

False

Options

abstract_interpretation_out_of_bounds

abstract_interpretation_out_of_bounds : bool = False

Use additional "symbolic expression analysis" as postprocessing step. This can remove false positives, but might require more time. Option is automatically active if option StaticSemanticAnalysis/performance.general.enhanced_analysis is active.
 

exclude_very_high_indices

exclude_very_high_indices : bool = True

Enables heuristic to detect false positives: When index used for array access is very high in comparison to the array's size, assume false positive.
 

report_unbounded_arrays

report_unbounded_arrays : bool = False

If true, accesses into arrays with unknown bound are reported as being potentially outside the allowed range. This affects arrays like extern char buf[];.
 

report_undereferenced_one_past_the_end

report_undereferenced_one_past_the_end : bool = False

If true, report accesses one past the end of an array even if there is no dereference of the resulting pointer.
 

report_unknown_index

report_unknown_index : bool = False

If false, do not report possible out-of-bound findings for which the analysis was not able to infer any restricting information about the array index (this can lead to excluding both false positives and true findings).