结构: Simple
Abstraction: Base
状态: Incomplete
被利用可能性: unkown
The program 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 programs 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.
cwe_Nature: ChildOf cwe_CWE_ID: 1023 cwe_View_ID: 1000 cwe_Ordinal: Primary
cwe_Nature: CanPrecede cwe_CWE_ID: 195 cwe_View_ID: 1000
cwe_Nature: CanPrecede cwe_CWE_ID: 682 cwe_View_ID: 1000
cwe_Nature: CanPrecede cwe_CWE_ID: 119 cwe_View_ID: 1000
cwe_Nature: CanPrecede cwe_CWE_ID: 124 cwe_View_ID: 1000
Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Often'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Often'}]
范围 | 影响 | 注释 |
---|---|---|
['Integrity', 'Confidentiality', 'Availability'] | ['Modify Application Data', 'Execute Unauthorized Code or Commands'] | An attacker could modify the structure of the message or data being sent to the downstream component, possibly injecting commands. |
Availability | DoS: Resource Consumption (Other) | in some contexts, a negative value could lead to resource consumption. |
['Confidentiality', 'Integrity'] | ['Modify Memory', 'Read Memory'] | If a negative value is used to access memory, buffers, or other indexable structures, it could access memory outside the bounds of the buffer. |
策略: Enforcement by Conversion
If the number to be used is always expected to be positive, change the variable type from signed to unsigned or size_t.
策略: Input Validation
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.
The following code is intended to read an incoming packet from a socket and extract one or more headers.
bad C
The code performs a check to make sure that the packet does not contain too many headers. However, numHeaders is defined as a signed int, so it could be negative. If the incoming packet specifies a value such as -3, then the malloc calculation will generate a negative number (say, -300 if each header can be a maximum of 100 bytes). When this result is provided to malloc(), it is first converted to a size_t type. This conversion then produces a large value such as 4294966996, which may cause malloc() to fail or to allocate an extremely large amount of memory (CWE-195). With the appropriate negative numbers, an attacker could trick malloc() into using a very small positive number, which then allocates a buffer that is much smaller than expected, potentially leading to a buffer overflow.
The following code reads a maximum size and performs a sanity check on that size. It then performs a strncpy, assuming it will not exceed the boundaries of the array. While the use of "short s" is forced in this particular example, short int's are frequently used within real-world code, such as code that processes structured data.
bad C
This code first exhibits an example of CWE-839, allowing "s" to be a negative number. When the negative short "s" is converted to an unsigned integer, it becomes an extremely large positive integer. When this converted integer is used by strncpy() it will lead to a buffer overflow (CWE-119).
In the following code, the method retrieves a value from an array at a specific array index location that is given as an input parameter to the method
bad C
However, this method only verifies that the given array index is less than the maximum length of the array but does not check for the minimum value (CWE-839). This will allow a negative value to be accepted as the input array index, which will result in a out of bounds read (CWE-125) and may allow access to sensitive memory. The input array index should be checked to verify that is within the maximum and minimum range required for the array (CWE-129). In this example the if statement should be modified to include a minimum range check, as shown below.
good C
The following code shows a simple BankAccount class with deposit and withdraw methods.
bad Java
The withdraw method includes a check to ensure that the withdrawal amount does not exceed the maximum limit allowed, however the method does not check to ensure that the withdrawal amount is greater than a minimum value (CWE-129). Performing a range check on a value that does not include a minimum check can have significant security implications, in this case not including a minimum range check can allow a negative value to be used which would cause the financial application using this class to deposit money into the user account rather than withdrawing. In this example the if statement should the modified to include a minimum range check, as shown below.
good Java
Note that this example does not protect against concurrent access to the BankAccount balance variable, see CWE-413 and CWE-362.
While it is out of scope for this example, note that the use of doubles or floats in financial calculations may be subject to certain kinds of attacks where attackers use rounding errors to steal money.
标识 | 说明 | 链接 |
---|---|---|
CVE-2010-1866 | Chain: integer overflow causes a negative signed value, which later bypasses a maximum-only check, leading to heap-based buffer overflow. | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-1866 |
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. | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-1099 |
CVE-2011-0521 | Chain: kernel's lack of a check for a negative value leads to memory corruption. | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2011-0521 |
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. | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-3704 |
CVE-2010-2530 | Chain: Negative value stored in an int bypasses a size check and causes allocation of large amounts of memory. | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-2530 |
CVE-2009-3080 | Chain: negative offset value to IOCTL bypasses check for maximum index, then used as an array index for buffer under-read. | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-3080 |
CVE-2008-6393 | chain: file transfer client performs signed comparison, leading to integer overflow and heap-based buffer overflow. | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-6393 |
CVE-2008-4558 | chain: negative ID in media player bypasses check for maximum index, then used as an array index for buffer under-read. | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4558 |