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.
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.
Potential Impact
Integrity, Confidentiality, Availability
Modify Application Data, Execute Unauthorized Code or Commands
Availability
DoS: Resource Consumption (Other)
Confidentiality, Integrity
Modify Memory, Read Memory
Demonstrative Examples
DataPacket *packet;int numHeaders;PacketHeader *headers;
sock=AcceptSocketConnection();ReadPacket(packet, sock);numHeaders =packet->headers;
if (numHeaders > 100) {ExitError("too many headers!");}headers = malloc(numHeaders * sizeof(PacketHeader);ParsePacketHeaders(packet, headers);int GetUntrustedInt () {return(0x0000FFFF);}
void main (int argc, char **argv) {
char path[256];char *input;int i;short s;unsigned int sz;
i = GetUntrustedInt();s = i;/* s is -1 so it passes the safety check - CWE-697 */if (s > 256) {DiePainfully("go away!\n");}
/* s is sign-extended and saved in sz */sz = s;
/* output: i=65535, s=-1, sz=4294967295 - your mileage may vary */printf("i=%d, s=%d, sz=%u\n", i, s, sz);
input = GetUserInput("Enter pathname:");
/* strncpy interprets s as unsigned int, so it's treated as MAX_INT(CWE-195), enabling buffer overflow (CWE-119) */strncpy(path, input, s);path[255] = '\0'; /* don't want CWE-170 */printf("Path is: %s\n", path);
}int getValueFromArray(int *array, int len, int index) {
int value;
// check that the array index is less than the maximum
// length of the array
if (index < len) {
// get the value at the specified index of the array
value = array[index];
}
// if array index is invalid then output error message
// and return value indicating error
else {printf("Value is: %d\n", array[index]);value = -1;}
return value;
}...
// check that the array index is within the correct
// range of values for the array
if (index >= 0 && index < len) {
...public class BankAccount {
public final int MAXIMUM_WITHDRAWAL_LIMIT = 350;
// variable for bank account balance
private double accountBalance;
// constructor for BankAccount
public BankAccount() {accountBalance = 0;}
// method to deposit amount into BankAccount
public void deposit(double depositAmount) {...}
// method to withdraw amount from BankAccount
public void withdraw(double withdrawAmount) {
if (withdrawAmount < MAXIMUM_WITHDRAWAL_LIMIT) {
double newBalance = accountBalance - withdrawAmount;accountBalance = newBalance;
}else {System.err.println("Withdrawal amount exceeds the maximum limit allowed, please try again...");...}
}
// other methods for accessing the BankAccount object
...
}public class BankAccount {
public final int MINIMUM_WITHDRAWAL_LIMIT = 0;public final int MAXIMUM_WITHDRAWAL_LIMIT = 350;
...
// method to withdraw amount from BankAccount
public void withdraw(double withdrawAmount) {
if (withdrawAmount < MAXIMUM_WITHDRAWAL_LIMIT &&withdrawAmount > MINIMUM_WITHDRAWAL_LIMIT) {
...Mitigations & Prevention
If the number to be used is always expected to be positive, change the variable type from signed to unsigned or size_t.
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.
Detection Methods
- Automated Static Analysis — 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 sea
Real-World CVE Examples
| CVE ID | Description |
|---|---|
| CVE-2010-1866 | 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). |
| CVE-2009-1099 | Chain: 16-bit counter can be interpreted as a negative value, compared to a 32-bit maximum value, leading to buffer under-write. |
| CVE-2011-0521 | Chain: kernel's lack of a check for a negative value leads to memory corruption. |
| CVE-2010-3704 | Chain: parser uses atoi() but does not check for a negative value, which can happen on some platforms, leading to buffer under-write. |
| CVE-2010-2530 | Chain: Negative value stored in an int bypasses a size check and causes allocation of large amounts of memory. |
| CVE-2009-3080 | Chain: negative offset value to IOCTL bypasses check for maximum index, then used as an array index for buffer under-read. |
| CVE-2008-6393 | chain: file transfer client performs signed comparison, leading to integer overflow and heap-based buffer overflow. |
| CVE-2008-4558 | chain: negative ID in media player bypasses check for maximum index, then used as an array index for buffer under-read. |
Related Weaknesses
Frequently Asked Questions
What is CWE-839?
CWE-839 (Numeric Range Comparison Without Minimum Check) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Base-level weakness. 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.
How can CWE-839 be exploited?
Attackers can exploit CWE-839 (Numeric Range Comparison Without Minimum Check) to modify application data, execute unauthorized code or commands. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-839?
Key mitigations include: If the number to be used is always expected to be positive, change the variable type from signed to unsigned or size_t.
What is the severity of CWE-839?
CWE-839 is classified as a Base-level weakness (Medium abstraction). It has been observed in 8 real-world CVEs.