CWE-193: Off-by-one Error (Base)

Description: A product calculates or uses an incorrect maximum or minimum value that is 1 more, or 1 less, than the correct value.



Alternate Terms: ::TERM:off-by-five:DESCRIPTION:An off-by-five error was reported for sudo in 2002 (CVE-2002-0184), but that is more like a length calculation error.::

Applicable Platforms: ::LANGUAGE CLASS:Not Language-Specific:LANGUAGE PREVALENCE:Undetermined::

Modes of Introduction: ::PHASE:Implementation::



Common Consequences: ::SCOPE:Availability:IMPACT:DoS: Crash, Exit, or Restart:IMPACT:DoS: Resource Consumption (CPU):IMPACT:DoS: Resource Consumption (Memory):IMPACT:DoS: Instability:NOTE:This weakness will generally lead to undefined behavior and therefore crashes. In the case of overflows involving loop index variables, the likelihood of infinite loops is also high.::SCOPE:Integrity:IMPACT:Modify Memory:NOTE:If the value in question is important to data (as opposed to flow), simple data corruption has occurred. Also, if the wrap around results in other conditions such as buffer overflows, further memory corruption may occur.::SCOPE:Confidentiality:SCOPE:Availability:SCOPE:Access Control:IMPACT:Execute Unauthorized Code or Commands:IMPACT:Bypass Protection Mechanism:NOTE:This weakness can sometimes trigger buffer overflows which can be used to execute arbitrary code. This is usually outside the scope of a program's implicit security policy.::

Detection Methods: ::METHOD:Automated Static Analysis:DESCRIPTION:Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect sources (origins of input) with sinks (destinations where the data interacts with external components, a lower layer such as the OS, etc.):EFFECTIVENESS:High::

Potential Mitigations: ::PHASE:Implementation:DESCRIPTION: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().::

Observed Examples: ::REFERENCE:CVE-2003-0252:DESCRIPTION:Off-by-one error allows remote attackers to cause a denial of service and possibly execute arbitrary code via requests that do not contain newlines.:LINK:https://www.cve.org/CVERecord?id=CVE-2003-0252::REFERENCE:CVE-2001-1391:DESCRIPTION:Off-by-one vulnerability in driver allows users to modify kernel memory.:LINK:https://www.cve.org/CVERecord?id=CVE-2001-1391::REFERENCE:CVE-2002-0083:DESCRIPTION:Off-by-one error allows local users or remote malicious servers to gain privileges.:LINK:https://www.cve.org/CVERecord?id=CVE-2002-0083::REFERENCE:CVE-2002-0653:DESCRIPTION:Off-by-one buffer overflow in function usd by server allows local users to execute arbitrary code as the server user via .htaccess files with long entries.:LINK:https://www.cve.org/CVERecord?id=CVE-2002-0653::REFERENCE:CVE-2002-0844:DESCRIPTION:Off-by-one buffer overflow in version control system allows local users to execute arbitrary code.:LINK:https://www.cve.org/CVERecord?id=CVE-2002-0844::REFERENCE:CVE-1999-1568:DESCRIPTION:Off-by-one error in FTP server allows a remote attacker to cause a denial of service (crash) via a long PORT command.:LINK:https://www.cve.org/CVERecord?id=CVE-1999-1568::REFERENCE:CVE-2004-0346:DESCRIPTION:Off-by-one buffer overflow in FTP server allows local users to gain privileges via a 1024 byte RETR command.:LINK:https://www.cve.org/CVERecord?id=CVE-2004-0346::REFERENCE:CVE-2004-0005:DESCRIPTION:Multiple buffer overflows in chat client allow remote attackers to cause a denial of service and possibly execute arbitrary code.:LINK:https://www.cve.org/CVERecord?id=CVE-2004-0005::REFERENCE:CVE-2003-0356:DESCRIPTION:Multiple off-by-one vulnerabilities in product allow remote attackers to cause a denial of service and possibly execute arbitrary code.:LINK:https://www.cve.org/CVERecord?id=CVE-2003-0356::REFERENCE:CVE-2001-1496:DESCRIPTION:Off-by-one buffer overflow in server allows remote attackers to cause a denial of service and possibly execute arbitrary code.:LINK:https://www.cve.org/CVERecord?id=CVE-2001-1496::REFERENCE:CVE-2004-0342:DESCRIPTION:This is an interesting example that might not be an off-by-one.:LINK:https://www.cve.org/CVERecord?id=CVE-2004-0342::REFERENCE:CVE-2001-0609:DESCRIPTION:An off-by-one enables a terminating null to be overwritten, which causes 2 strings to be merged and enable a format string.:LINK:https://www.cve.org/CVERecord?id=CVE-2001-0609::REFERENCE:CVE-2002-1745:DESCRIPTION:Off-by-one error allows source code disclosure of files with 4 letter extensions that match an accepted 3-letter extension.:LINK:https://www.cve.org/CVERecord?id=CVE-2002-1745::REFERENCE:CVE-2002-1816:DESCRIPTION:Off-by-one buffer overflow.:LINK:https://www.cve.org/CVERecord?id=CVE-2002-1816::REFERENCE:CVE-2002-1721:DESCRIPTION:Off-by-one error causes an snprintf call to overwrite a critical internal variable with a null value.:LINK:https://www.cve.org/CVERecord?id=CVE-2002-1721::REFERENCE:CVE-2003-0466:DESCRIPTION: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.:LINK:https://www.cve.org/CVERecord?id=CVE-2003-0466::REFERENCE:CVE-2003-0625:DESCRIPTION:Off-by-one error allows read of sensitive memory via a malformed request.:LINK:https://www.cve.org/CVERecord?id=CVE-2003-0625::REFERENCE:CVE-2006-4574:DESCRIPTION:Chain: security monitoring product has an off-by-one error that leads to unexpected length values, triggering an assertion.:LINK:https://www.cve.org/CVERecord?id=CVE-2006-4574::



Notes: ::TYPE:Relationship:NOTE: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.::