501
Detection Methods
Fuzzing
Fuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs
- either randomly or algorithmically - and dynamically invoking the code with those inputs.
Even with random inputs, it is often capable of generating unexpected results such as crashes,
memory corruption, or resource consumption. Fuzzing effectively produces repeatable test cases
that clearly indicate bugs, which helps developers to diagnose the issues.
Effectiveness = High
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 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
Ensure that no casts, implicit or explicit, take place that move from a larger size primitive or a
smaller size primitive.
Demonstrative Examples
Example 1:
This example, while not exploitable, shows the possible mangling of values associated with
truncation errors:
Example Language: C (Bad)
int intPrimitive;
short shortPrimitive;
intPrimitive = (int)(~((int)0) ^ (1 << (sizeof(int)*8-1)));
shortPrimitive = intPrimitive;
printf("Int MAXINT: %d\nShort MAXINT: %d\n", intPrimitive, shortPrimitive);
The above code, when compiled and run on certain systems, returns the following output:
Example Language: (Result)
Int MAXINT: 2147483647
Short MAXINT: -1
This problem may be exploitable when the truncated value is used as an array index, which can
happen implicitly when 64-bit values are used as indexes, as they are truncated to 32 bits.
Example 2:
In the following Java example, the method updateSalesForProduct is part of a business application
class that updates the sales information for a particular product. The method receives as
arguments the product ID and the integer amount sold. The product ID is used to retrieve the
total product count from an inventory object which returns the count as an integer. Before calling
the method of the sales object to update the sales count the integer values are converted to The
primitive type short since the method requires short type for the method arguments.
CWE Version 4.14
CWE-197: Numeric Truncation Error
CWE-197: Numeric Truncation Error