CWE-130

Improper Handling of Length Parameter Inconsistency. [Data-Processing-Errors, Improper-Check-Or-Handling-Of-Exceptional-Conditions, Improper-Neutralization]

Required inputs: IR, StaticSemanticAnalysis

The product parses a formatted message or structure, but it does not handle or incorrectly handles a length field that is inconsistent with the actual length of the associated data. If an attacker can manipulate the length parameter associated with an input such that it is inconsistent with the actual length of the input, this can be leveraged to cause the target application to behave in unexpected, and possibly, malicious ways. One of the possible motives for doing so is to pass in arbitrarily large input to the application. Another possible motivation is the modification of application state by including invalid data for subsequent properties of the application. Such weaknesses commonly lead to attacks such as buffer overflows and execution of arbitrary code.
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).

Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See 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

size_too_large

Guarantee that library function “{name}” does not form an invalid pointer of {target_size} and {descr1}{size} {via}{pos}. argument.

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

unknown_element_size

Guarantee that library function “{name}” does not form an invalid pointer due to an unknown element size.

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.
 

element_size_argument

element_size_argument

Type: dict[bauhaus.analysis.config.QualifiedName, int]

Default:

{
   'bsearch': 3,
   'bsearch_s': 3,
   'fread': 1,
   'fwrite': 1,
   'qsort': 2,
   'qsort_s': 2
}
Names were the argument denotes the element size to be multiplied with another size argument.
 

enable_buffer_analysis

enable_buffer_analysis : bool = True

If true, try to compute sizes and offsets using the possibly expensive buffer analysis. Also unreachable code is then detected and not reported. If false, offsets will not be considered and might cause false negatives.
 

exclude_sizeof_syntactically

exclude_sizeof_syntactically : bool = True

Check if the size argument is given by the sizeof operator and the pointer argument to be checked. This option is similar to length_names only that sizeof is a keyword and no standard call.
 

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.
 

exclude_warnings_for_unknown_arguments

exclude_warnings_for_unknown_arguments : bool = False

Exclude warnings for cases where nothing at all is known about the pointer arguments of an operation, caused e.g. by using return values of external routines.
 

functions

functions

Type: set[bauhaus.analysis.config.QualifiedName]

Default: {'asctime_s', 'bsearch', 'bsearch_s', 'ctime_s', 'fgets', 'fgetws', 'fread', 'fwrite', 'getenv_s', 'gets_s', 'mblen', 'mbrlen', 'mbrtoc16', 'mbrtoc32', 'mbrtowc', 'mbsrtowcs', 'mbsrtowcs_s', 'mbstowcs', 'mbstowcs_s', 'mbtowc', 'memchr', 'memcmp', 'memcpy', 'memcpy_s', 'memmove', 'memmove_s', 'memset', 'memset_s', 'qsort', 'qsort_s', 'setvbuf', 'snprintf', 'snprintf_s', 'snwprintf_s', 'sprintf_s', 'strcat_s', 'strcpy_s', 'strerror_s', 'strftime', 'strncat', 'strncat_s', 'strncmp', 'strncpy', 'strncpy_s', 'strnlen_s', 'strxfrm', 'swprintf', 'swprintf_s', 'tmpnam_s', 'vsnprintf', 'vsnprintf_s', 'vsnwprintf_s', 'vsprintf_s', 'vswprintf', 'vswprintf_s', 'wcrtomb_s', 'wcscat_s', 'wcscpy_s', 'wcsftime', 'wcsncat', 'wcsncat_s', 'wcsncmp', 'wcsncpy', 'wcsncpy_s', 'wcsnlen_s', 'wcsrtombs', 'wcsrtombs_s', 'wcstombs', 'wcstombs_s', 'wcsxfrm', 'wctomb_s', 'wmemchr', 'wmemcmp', 'wmemcpy', 'wmemcpy_s', 'wmemmove', 'wmemmove_s', 'wmemset'}

Names of functions being relevant as call targets for this check in addition to those of function_lookup in header files.
 

ignore_arguments

ignore_arguments

Type: dict[bauhaus.analysis.config.QualifiedName, set[int]]

Default:

{
   'bsearch': {0},
   'bsearch_s': {0},
   'getenv_s': {0},
   'mbrtoc16': {0},
   'mbrtoc32': {0},
   'mbrtowc': {0},
   'mbsrtowcs_s': {0, 3},
   'mbstowcs_s': {0},
   'mbtowc': {0},
   'setvbuf': {0},
   'wcrtomb_s': {0},
   'wcsrtombs_s': {0, 3},
   'wcstombs_s': {0},
   'wctomb_s': {0}
}
The analysis typically infers the relevant arguments by itself, but in some cases an argument should be disregarded for the inference of pointer and size arguments.
 

ignore_calls_in_functions

ignore_calls_in_functions : set[bauhaus.analysis.config.QualifiedName] = set()

Qualified names of function definitions in which calls to relevant functions are ignored for this check.
 

length_names

length_names : set[bauhaus.analysis.config.QualifiedName] = {'strlen', 'strnlen', 'wcslen', 'wcsnlen'}

Names of length functions. Pointer arguments that are also the first argument of a length function as a size argument are syntactically not checked further.
 

pointer_argument

pointer_argument

Type: dict[bauhaus.analysis.config.QualifiedName, int]

Default:

{
   'mbsrtowcs': 0,
   'strxfrm': 0,
   'wcsrtombs': 0,
   'wcsrtombs_s': 1,
   'wcsxfrm': 0
}
The analysis typically infers the relevant arguments by itself, but in some cases (e.g. functions having more than one pointer parameter) it may be necessary to provide the index of the single pointer argument (starting with index 0).
 

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

second_size_argument

second_size_argument

Type: dict[bauhaus.analysis.config.QualifiedName, int]

Default:

{
   'mbsrtowcs_s': 4,
   'mbstowcs_s': 4,
   'memcpy_s': 3,
   'memmove_s': 3,
   'memset_s': 3,
   'strncat_s': 3,
   'strncpy_s': 3,
   'wcsncat_s': 3,
   'wcsncpy_s': 3,
   'wcsrtombs_s': 4,
   'wcstombs_s': 4,
   'wmemcpy_s': 3,
   'wmemmove_s': 3
}
Names were a second size argument also applies to the first pointer.
 

size_argument

size_argument

Type: dict[bauhaus.analysis.config.QualifiedName, int]

Default:

{
   'memchr': 2,
   'memset': 2,
   'setvbuf': 3,
   'wcrtomb_s': 2,
   'wmemchr': 2,
   'wmemset': 2
}
The analysis typically infers the relevant arguments by itself, but in some cases (e.g. functions having more than one integer parameter) it may be necessary to provide the index of the single size argument (starting with index 0).
 

use_type_based_maximum

use_type_based_maximum : bool = True

If true, assume a maximal value if only the type of a size value is known. This will result in a size_too_large violation, if the analysis does not know better and the code is reachable according to the buffer analysis.
 

use_type_based_minimum

use_type_based_minimum : bool = False

If true, assume zero if only the type of a size value is known.