Off-by-one Error |
Weakness ID: 193 (Weakness Base) | Status: Draft |
Description Summary
off-by-five: | An "off-by-five" error was reported for sudo in 2002 (CVE-2002-0184), but that is more like a "length calculation" error. |
---|
Example 1
The following code allocates memory for a maximum number of widgets. It then gets a user-specified number of widgets, making sure that the user does not request too many. It then initializes the elements of the array using InitializeWidget(). Because the number of widgets can vary for each request, the code inserts a NULL pointer to signify the location of the last widget.
However, this code contains an off-by-one calculation error. It allocates exactly enough space to contain the specified number of widgets, but it does not include the space for the NULL pointer. As a result, the allocated buffer is smaller than it is supposed to be (CWE-131). So if the user ever requests MAX_NUM_WIDGETS, there is an off-by-one buffer overflow when the NULL is assigned. Depending on the environment and compilation settings, this could cause memory corruption.
Example 2
The following C/C++ example demonstrates the Off-by-one error in the main method of a pattern matching utility that looks for a specific pattern within a specific file. The main method uses the string copy method, strncpy, to copy the command line user input file name and pattern to the Filename and Pattern character arrays respectively.
However, the calls to strncpy use the sizeof method call for the size parameter that does not take into account that the strncpy will add a null terminator to each character array. 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 be added beyond the end of the buffer for the character arrays creating an off-by-one buffer overflow. In addition to creating a buffer overflow that may cause a memory address to be overwritten, if the character arrays are output to the user through the printf method the memory addresses at the overflow location may be output to the user.
To fix this problem, be sure to subtract 1 from the sizeof() call to allow room for the null byte to be added.
Example 3
Similarly, this example uses the strncat and snprintf functions incorrectly, failing to account for the null character function will add.
By leaving a free byte at the end of the buffers for a null character to be added, the off-by-one weakness is avoided.
Example 4
The Off-by-one error can also be manifested when reading characters of a character array using a for loop that has the incorrect size as a continuation condition and attempts to read beyond the end of the buffer for the character array as shown in the following example.
Example 5
As another example the Off-by-one error can occur when using the sprintf library function to copy a string variable to a formatted string variable and the original string variable comes from an untrusted source. As in the following example where a local function, setFilename is used to store the value of a filename to a database but first uses sprintf to format the filename. The setFilename function includes an input parameter with the name of the file that is used as the copy source in the sprintf function. The sprintf function will copy the file name to a char array of size 20 and specifies the format of the new variable as 16 characters followed by the file extension .dat.
However this will cause an Off-by-one error if the original filename is exactly 16 characters or larger because the format of 16 characters with the file extension is exactly 20 characters and does not take into account the required null terminator that will be placed at the end of the string.
Reference | Description |
---|---|
CVE-2003-0252 | |
CVE-2001-1391 | |
CVE-2002-0083 | |
CVE-2002-0653 | |
CVE-2002-0844 | |
CVE-1999-1568 | |
CVE-2004-0346 | |
CVE-2004-0005 | |
CVE-2003-0356 | |
CVE-2001-1496 | |
CVE-2004-0342 | This is an interesting example that might not be an off-by-one. |
CVE-2001-0609 | An off-by-one enables a terminating null to be overwritten, which causes 2 strings to be merged and enable a format string. |
CVE-2002-1745 | Off-by-one error allows source code disclosure of files with 4 letter extensions that match an accepted 3-letter extension. |
CVE-2002-1816 | Off-by-one buffer overflow. |
CVE-2002-1721 | Off-by-one error causes an snprintf call to overwrite a critical internal variable with a null value. |
CVE-2003-0466 | Off-by-one error in function used in many products leads to a buffer overflow during pathname management, as demonstrated using multiple commands in an FTP server. |
CVE-2003-0625 | Off-by-one error allows read of sensitive memory via a malformed request. |
CVE-2006-4574 | Chain: security monitoring product has an off-by-one error that leads to unexpected length values, triggering an assertion. |
Phase: Implementation When copying character arrays or using character manipulation methods, the correct size parameter must be used to account for the null terminator that needs to be added at the end of the array. Some examples of functions susceptible to this weakness in C include strcpy(), strncpy(), strcat(), strncat(), printf(), sprintf(), scanf() and sscanf(). |
Nature | Type | ID | Name | View(s) this relationship pertains to |
---|---|---|---|---|
ChildOf | Weakness Class | 682 | Incorrect Calculation | Development Concepts (primary)699 Research Concepts (primary)1000 |
ChildOf | Category | 741 | CERT C Secure Coding Section 07 - Characters and Strings (STR) | Weaknesses Addressed by the CERT C Secure Coding Standard (primary)734 |
CanPrecede | Weakness Class | 119 | Failure to Constrain Operations within the Bounds of a Memory Buffer | Research Concepts1000 |
CanPrecede | Weakness Base | 170 | Improper Null Termination | Research Concepts1000 |
CanPrecede | Weakness Variant | 617 | Reachable Assertion | Research Concepts1000 |
This is not always a buffer overflow. For example, an off-by-one error could be a factor in a partial comparison, a read from the wrong memory location, an incorrect conditional, etc. |
Under-studied. It requires careful code analysis or black box testing, where inputs of excessive length might not cause an error. Off-by-ones are likely triggered by extensive fuzzing, with the attendant diagnostic problems. |
Mapped Taxonomy Name | Node ID | Fit | Mapped Node Name |
---|---|---|---|
PLOVER | Off-by-one Error | ||
CERT C Secure Coding | STR31-C | Guarantee that storage for strings has sufficient space for character data and the null terminator |
Halvar Flake. "Third Generation Exploits". presentation at Black Hat Europe 2001. <http://www.blackhat.com/presentations/bh-europe-01/halvar-flake/bh-europe-01-halvarflake.ppt>. |
Steve Christey. "Off-by-one errors: a brief explanation". Secprog and SC-L mailing list posts. 2004-05-05. <http://marc.theaimsgroup.com/?l=secprog&m=108379742110553&w=2>. |
klog. "The Frame Pointer Overwrite". Phrack Issue 55, Chapter 8. 1999-09-09. <http://kaizo.org/mirrors/phrack/phrack55/P55-08>. |
G. Hoglund and G. McGraw. "Exploiting Software: How to Break Code (The buffer overflow chapter)". Addison-Wesley. February 2004. |
Submissions | ||||
---|---|---|---|---|
Submission Date | Submitter | Organization | Source | |
PLOVER | Externally Mined | |||
Modifications | ||||
Modification Date | Modifier | Organization | Source | |
2008-09-08 | CWE Content Team | MITRE | Internal | |
updated Alternate Terms, Common Consequences, Relationships, Observed Example, Relationship Notes, Taxonomy Mappings | ||||
2008-11-24 | CWE Content Team | MITRE | Internal | |
updated Relationships, Taxonomy Mappings | ||||
2009-12-28 | CWE Content Team | MITRE | Internal | |
updated Demonstrative Examples, Potential Mitigations |