CWE-130¶
Improper Handling of Length Parameter Inconsistency. [Data-Processing-Errors, Improper-Check-Or-Handling-Of-Exceptional-Conditions, Improper-Neutralization]
Required inputs: IR, StaticSemanticAnalysis
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¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
abstract_interpretation_out_of_bounds¶
abstract_interpretation_out_of_bounds : bool = False
element_size_argument¶
element_size_argument
Names were the argument denotes the element size to be multiplied with another size argument.Type: dict[bauhaus.analysis.config.QualifiedName, int]
Default:
{ 'bsearch': 3, 'bsearch_s': 3, 'fread': 1, 'fwrite': 1, 'qsort': 2, 'qsort_s': 2 }
enable_buffer_analysis¶
enable_buffer_analysis : bool = True
exclude_sizeof_syntactically¶
exclude_sizeof_syntactically : bool = True
sizeof
is a keyword and no standard call.
exclude_very_high_indices¶
exclude_very_high_indices : bool = True
exclude_warnings_for_unknown_arguments¶
exclude_warnings_for_unknown_arguments : bool = False
functions¶
functions
Names of functions being relevant as call targets for this check in addition to those of function_lookup in header files.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'}
ignore_arguments¶
ignore_arguments
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.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} }
ignore_calls_in_functions¶
ignore_calls_in_functions : set[bauhaus.analysis.config.QualifiedName] = set()
length_names¶
length_names : set[bauhaus.analysis.config.QualifiedName] = {'strlen', 'strnlen', 'wcslen', 'wcsnlen'}
pointer_argument¶
pointer_argument
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).Type: dict[bauhaus.analysis.config.QualifiedName, int]
Default:
{ 'mbsrtowcs': 0, 'strxfrm': 0, 'wcsrtombs': 0, 'wcsrtombs_s': 1, 'wcsxfrm': 0 }
report_unbounded_arrays¶
report_unbounded_arrays : bool = False
extern char buf[];.
report_undereferenced_one_past_the_end¶
report_undereferenced_one_past_the_end : bool = False
report_unknown_index¶
report_unknown_index : bool = False
second_size_argument¶
second_size_argument
Names were a second size argument also applies to the first pointer.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 }
size_argument¶
size_argument
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).Type: dict[bauhaus.analysis.config.QualifiedName, int]
Default:
{ 'memchr': 2, 'memset': 2, 'setvbuf': 3, 'wcrtomb_s': 2, 'wmemchr': 2, 'wmemset': 2 }
use_type_based_maximum¶
use_type_based_maximum : bool = True
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