358
underlying representation. [REF-7] Also be careful to account for 32-bit, 64-bit, and other
potential differences that may affect the numeric representation.
Phase: Implementation
Strategy = Input Validation
Perform input validation on any numeric input by ensuring that it is within the expected range.
Enforce that the input meets both the minimum and maximum requirements for the expected
range.
Phase: Architecture and Design
For any security checks that are performed on the client side, ensure that these checks are
duplicated on the server side, in order to avoid CWE-602. Attackers can bypass the client-side
checks by modifying values after the checks have been performed, or by changing the client to
remove the client-side checks entirely. Then, these modified values would be submitted to the
server.
Phase: Implementation
When processing structured incoming data containing a size field followed by raw data,
identify and resolve any inconsistencies between the size field and the actual size of the data
(CWE-130).
Phase: Implementation
When allocating memory that uses sentinels to mark the end of a data structure - such as NUL
bytes in strings - make sure you also include the sentinel in your calculation of the total amount
of memory that must be allocated.
Phase: Implementation
Replace unbounded copy functions with analogous functions that support length arguments,
such as strcpy with strncpy. Create these if they are not available.
Effectiveness = Moderate
This approach is still susceptible to calculation errors, including issues such as off-by-one
errors (CWE-193) and incorrectly calculating buffer lengths (CWE-131). Additionally, this
only addresses potential overflow issues. Resource consumption / exhaustion issues are still
possible.
Phase: Implementation
Use sizeof() on the appropriate data type to avoid CWE-467.
Phase: Implementation
Use the appropriate type for the desired action. For example, in C/C++, only use unsigned
types for values that could never be negative, such as height, width, or other numbers related to
quantity. This will simplify validation and will reduce surprises related to unexpected casting.
Phase: Architecture and Design
Strategy = Libraries or Frameworks
Use a vetted library or framework that does not allow this weakness to occur or provides
constructs that make this weakness easier to avoid. Use libraries or frameworks that make it
easier to handle numbers without unexpected consequences, or buffer allocation routines that
automatically track buffer size. Examples include safe integer handling packages such as SafeInt
(C++) or IntegerLib (C or C++). [REF-106]
Phase: Operation
Phase: Build and Compilation
Strategy = Environment Hardening
CWE Version 4.14
CWE-131: Incorrect Calculation of Buffer Size
CWE-131: Incorrect Calculation of Buffer Size