CWE-124: Buffer Underwrite ('Buffer Underflow') (Base)

Description: The product writes to a buffer using an index or pointer that references a memory location prior to the beginning of the buffer.

Extended Description: This typically occurs when a pointer or its index is decremented to a position before the buffer, when pointer arithmetic results in a position before the beginning of the valid memory location, or when a negative index is used.


Alternate Terms: ::TERM:buffer underrun:DESCRIPTION:Some prominent vendors and researchers use the term buffer underrun. Buffer underflow is more commonly used, although both terms are also sometimes used to describe a buffer under-read (CWE-127).::

Applicable Platforms: ::LANGUAGE NAME:C:LANGUAGE PREVALENCE:Undetermined::LANGUAGE NAME:C++:LANGUAGE PREVALENCE:Undetermined::

Modes of Introduction: ::PHASE:Implementation::



Common Consequences: ::SCOPE:Integrity:SCOPE:Availability:IMPACT:Modify Memory:IMPACT:DoS: Crash, Exit, or Restart:NOTE:Out of bounds memory access will very likely result in the corruption of relevant memory, and perhaps instructions, possibly leading to a crash.::SCOPE:Integrity:SCOPE:Confidentiality:SCOPE:Availability:SCOPE:Access Control:SCOPE:Other:IMPACT:Execute Unauthorized Code or Commands:IMPACT:Modify Memory:IMPACT:Bypass Protection Mechanism:IMPACT:Other:NOTE:If the corrupted memory can be effectively controlled, it may be possible to execute arbitrary code. If the corrupted memory is data rather than instructions, the system will continue to function with improper changes, possibly in violation of an implicit or explicit policy. The consequences would only be limited by how the affected data is used, such as an adjacent memory location that is used to specify whether the user has special privileges.::SCOPE:Access Control:SCOPE:Other:IMPACT:Bypass Protection Mechanism:IMPACT:Other:NOTE:When the consequence is arbitrary code execution, this can often be used to subvert any other security service.::


Potential Mitigations: ::PHASE:Requirements:DESCRIPTION:Choose a language that is not susceptible to these issues.::PHASE:Implementation:DESCRIPTION:All calculated values that are used as index or for pointer arithmetic should be validated to ensure that they are within an expected range.::

Observed Examples: ::REFERENCE:CVE-2021-24018:DESCRIPTION:buffer underwrite in firmware verification routine allows code execution via a crafted firmware image:LINK:https://www.cve.org/CVERecord?id=CVE-2021-24018::REFERENCE:CVE-2002-2227:DESCRIPTION:Unchecked length of SSLv2 challenge value leads to buffer underflow.:LINK:https://www.cve.org/CVERecord?id=CVE-2002-2227::REFERENCE:CVE-2007-4580:DESCRIPTION:Buffer underflow from a small size value with a large buffer (length parameter inconsistency, CWE-130):LINK:https://www.cve.org/CVERecord?id=CVE-2007-4580::REFERENCE:CVE-2007-1584:DESCRIPTION:Buffer underflow from an all-whitespace string, which causes a counter to be decremented before the buffer while looking for a non-whitespace character.:LINK:https://www.cve.org/CVERecord?id=CVE-2007-1584::REFERENCE:CVE-2007-0886:DESCRIPTION:Buffer underflow resultant from encoded data that triggers an integer overflow.:LINK:https://www.cve.org/CVERecord?id=CVE-2007-0886::REFERENCE:CVE-2006-6171:DESCRIPTION:Product sets an incorrect buffer size limit, leading to off-by-two buffer underflow.:LINK:https://www.cve.org/CVERecord?id=CVE-2006-6171::REFERENCE:CVE-2006-4024:DESCRIPTION:Negative value is used in a memcpy() operation, leading to buffer underflow.:LINK:https://www.cve.org/CVERecord?id=CVE-2006-4024::REFERENCE:CVE-2004-2620:DESCRIPTION:Buffer underflow due to mishandled special characters:LINK:https://www.cve.org/CVERecord?id=CVE-2004-2620::



Notes: ::TYPE:Relationship:NOTE:This could be resultant from several errors, including a bad offset or an array index that decrements before the beginning of the buffer (see CWE-129).::