CWE 125 - Out-of-Bounds Read
Disclaimer
The code provided is for
**educational purposes**only and should not be used in a**production environment**without proper review and testing.The provided code should be regarded as
**best practice**. There are also several ways to remediate the Vulnerabilities.The code provided
**may contain vulnerabilities**that could be exploited by attackers, and is intended to be used as a learning tool to help improve security awareness and best practices.The mitigations provided are intended to address
**specific vulnerabilities**in the code, but may not be effective against all potential attack vectors or scenarios.The code provided is not
**guaranteed to be secure or free**from all vulnerabilities, and should be reviewed and tested thoroughly before being used in a production environment.The code provided is
**not a substitute for professional security**advice or guidance, and users should consult with a qualified security professional before implementing any security measures.The authors and contributors of the code provided
**cannot be held responsible**for any damages or losses resulting from the use of this code.The code provided is provided "as is" without any warranties, express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose.
We do not have any sponsors or financial interests in any specific products or services, and the information provided is based solely on our own knowledge and experience.
The vulnerable and mitigated code samples provided on our platform are generated with the help of AI and sourced from the internet. We have made every effort to ensure that the code is accurate and up-to-date, but we cannot guarantee its correctness or completeness. If you notice that any of the code samples belong to you and you wish for it to be removed, please contact us and we will take appropriate action. We also apologize for any inconvenience caused and are committed to giving appropriate credit to the respective authors.
About CWE ID 125
Out-of-Bounds Read
This Vulnerability occurs when a program reads data past the end or before the beginning of a buffer, which can result in reading unintended or sensitive information, or even cause the program to crash.
Impact
Privilege Escalation
Data Corruption or Manipulation
Denial of Service (DoS)
Example with Code Explanation
C
CLet us consider an example case and understand the CWE 125 with context of Vulnerable code and Mitigated code.
The
vulnerable_functionfunction takes a string input and copies it to a buffer with a fixed size of 10 characters using thestrcpyfunction. If the input string is longer than 10 characters,strcpywill write past the end of thebuffer, leading to an out-of-bounds read vulnerability. Themainfunction passes a string input of 20 characters to thevulnerable_function, which may cause it to read beyond the end of the buffer, potentially leading to unexpected behavior or security issues.Some of the ways the Vulnerable code can be mitigated is:
Ensure that the
input lengthischeckedbefore copying the input to the buffer to avoid writing beyond the end of the buffer.Use safer string functions such as
strncpyorsnprintfthat limit the number of characters copied and prevent buffer overflows.Use compiler flags such as
-fstack-protectorto enable stack canaries, which can help detect buffer overflows at runtime.Use a buffer size that is large enough to accommodate the maximum expected input size, or dynamically allocate memory to store the input.
Mitigated Code
The Mitigated code does the following:
The buffer size is dynamically allocated to a maximum length of
MAX_INPUT_LENGTHcharacters, which can accommodate all expected inputs without overflowing the buffer.The
strncpyfunction is used to limit the number of characters copied to the buffer, preventing overflows and ensuring that the null terminator is included.A
null terminatoris added to the end of the string to ensure that it is properly terminated, preventing out-of-bounds reads of uninitialized memory.After the use of the buffer, the dynamically allocated memory is freed,
preventing memory leaksand ensuring that the buffer is not used after it has been freed.
C++
C++Vulnerable Code
In this vulnerable code, the
vulnerable_functionfunction takes a string input and copies it to a buffer with a fixed size of 10 characters using thestrcpyfunction. If the input string is longer than 10 characters,strcpywill write past the end of thebuffer, leading to an out-of-bounds read vulnerability.The
mainfunction passes a string input of 20 characters to thevulnerable_function, which may cause it to read beyond the end of the buffer, potentially leading to unexpected behavior or security issues.Some of the ways the Vulnerable code can be mitigated is:
Ensure that the
input length is checkedbefore copying the input to the buffer to avoid writing beyond the end of the buffer.Use a buffer size that is large enough to accommodate the maximum expected input size, or dynamically allocate memory to store the input.
Use safer string functions such as
strncpyorsnprintfthat limit the number of characters copied and prevent buffer overflows.Use C++ standard library classes like
std::stringinstead of C-style strings and character arrays, which have built-in boundary checking and can help prevent buffer overflows.
Mitigated Code
The Mitigated code does the following:
The
safe_function()function usessnprintf()to copy the input string into the buffer, which ensures that the copied string is null-terminated and will not write beyond the buffer's size.The function checks if the return value of
snprintf()is greater thanMAX_INPUT_LENGTH. If it is, then it prints an error message and returns immediately, preventing any further code execution that might use the buffer.
Java
JavaVulnerable Code
We have an array of integers called
numbersthat has five elements. We then attempt to access an element of the array using an index of5. Since the array only has five elements, the index of5is out of bounds, and attempting to access this element will result in an out-of-bounds read.Some of the ways the vulnerability can be remediated is:
Use defensive programming techniques, such as
input validation and error checking, to catch out-of-bounds read errors before they occur.Always ensure that
array indicesused to access array elements are within the bounds of the array.Use Java's built-in bounds checking mechanism to help prevent out-of-bounds read vulnerabilities.
Mitigated Code
The Mitigated code does the following:
We've added a method called
isValidIndex()that checks if the given index is within the bounds of the given array.In the main method, we use this
isValidIndex()method to check if the index is within bounds before attempting to access the corresponding array element.If the index is
out of bounds, we print an error message instead of attempting to access the array element.
References
Last updated