CWE-839: Numeric Range Comparison Without Minimum Check (Base)

Description: The product checks a value to ensure that it is less than or equal to a maximum, but it does not also verify that the value is greater than or equal to the minimum.

Extended Description: Some products use signed integers or floats even when their values are only expected to be positive or 0. An input validation check might assume that the value is positive, and only check for the maximum value. If the value is negative, but the code assumes that the value is positive, this can produce an error. The error may have security consequences if the negative value is used for memory allocation, array access, buffer access, etc. Ultimately, the error could lead to a buffer overflow or other type of memory corruption. The use of a negative number in a positive-only context could have security implications for other types of resources. For example, a shopping cart might check that the user is not requesting more than 10 items, but a request for -3 items could cause the application to calculate a negative price and credit the attacker's account.


Alternate Terms: ::TERM:Signed comparison:DESCRIPTION:The signed comparison term is often used to describe when the product uses a signed variable and checks it to ensure that it is less than a maximum value (typically a maximum buffer size), but does not verify that it is greater than 0.::

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




Common Consequences: ::SCOPE:Integrity:SCOPE:Confidentiality:SCOPE:Availability:IMPACT:Modify Application Data:IMPACT:Execute Unauthorized Code or Commands:NOTE:An attacker could modify the structure of the message or data being sent to the downstream component, possibly injecting commands.::SCOPE:Availability:IMPACT:DoS: Resource Consumption (Other):NOTE:in some contexts, a negative value could lead to resource consumption.::SCOPE:Confidentiality:SCOPE:Integrity:IMPACT:Modify Memory:IMPACT:Read Memory:NOTE:If a negative value is used to access memory, buffers, or other indexable structures, it could access memory outside the bounds of the buffer.::


Potential Mitigations: ::PHASE:Implementation:STRATEGY:Enforcement by Conversion:DESCRIPTION:If the number to be used is always expected to be positive, change the variable type from signed to unsigned or size_t.::PHASE:Implementation:STRATEGY:Input Validation:DESCRIPTION:If the number to be used could have a negative value based on the specification (thus requiring a signed value), but the number should only be positive to preserve code correctness, then include a check to ensure that the value is positive.::

Observed Examples: ::REFERENCE:CVE-2010-1866:DESCRIPTION:Chain: integer overflow (CWE-190) causes a negative signed value, which later bypasses a maximum-only check (CWE-839), leading to heap-based buffer overflow (CWE-122).:LINK:https://www.cve.org/CVERecord?id=CVE-2010-1866::REFERENCE:CVE-2009-1099:DESCRIPTION:Chain: 16-bit counter can be interpreted as a negative value, compared to a 32-bit maximum value, leading to buffer under-write.:LINK:https://www.cve.org/CVERecord?id=CVE-2009-1099::REFERENCE:CVE-2011-0521:DESCRIPTION:Chain: kernel's lack of a check for a negative value leads to memory corruption.:LINK:https://www.cve.org/CVERecord?id=CVE-2011-0521::REFERENCE:CVE-2010-3704:DESCRIPTION:Chain: parser uses atoi() but does not check for a negative value, which can happen on some platforms, leading to buffer under-write.:LINK:https://www.cve.org/CVERecord?id=CVE-2010-3704::REFERENCE:CVE-2010-2530:DESCRIPTION:Chain: Negative value stored in an int bypasses a size check and causes allocation of large amounts of memory.:LINK:https://www.cve.org/CVERecord?id=CVE-2010-2530::REFERENCE:CVE-2009-3080:DESCRIPTION:Chain: negative offset value to IOCTL bypasses check for maximum index, then used as an array index for buffer under-read.:LINK:https://www.cve.org/CVERecord?id=CVE-2009-3080::REFERENCE:CVE-2008-6393:DESCRIPTION:chain: file transfer client performs signed comparison, leading to integer overflow and heap-based buffer overflow.:LINK:https://www.cve.org/CVERecord?id=CVE-2008-6393::REFERENCE:CVE-2008-4558:DESCRIPTION:chain: negative ID in media player bypasses check for maximum index, then used as an array index for buffer under-read.:LINK:https://www.cve.org/CVERecord?id=CVE-2008-4558::