Status: Incomplete Weakness ID: 197 (Weakness Base)Summary Truncation errors occur when a primitive is cast to a primitive of a smaller size and data is lost in the conversion. This example, while not exploitable, shows the possible mangling of values associated with truncation errors: C Example: #include <stdio.h> int main() { 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); return (0); } The above code, when compiled and run, returns the following output: Int MAXINT: 2147483647 Short MAXINT: -1 A frequent paradigm for such a problem being exploitable is 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. Implementation Ensure that no casts, implicit or explicit, take place that move from a larger size primitive or a smaller size primitive. When a primitive is cast to a smaller primitive, the high order bits of the large value are lost in the conversion, potentially resulting in an unexpected value that is not equal to the original value. This value may be required as an index into a buffer, a loop iterator, or simply necessary state data. In any case, the value cannot be trusted and the system will be in an undefined state. While this method may be employed viably to isolate the low bits of a value, this usage is rare, and truncation usually implies that an implementation error has occurred.
Submissions PLOVER. (Externally Mined) Modifications CWE Content Team. MITRE. 2008-09-08. (Internal) updated Applicable_Platforms, Common_Consequences,
Relationships, Other_Notes, Taxonomy_Mappings CWE Content Team. MITRE. 2008-11-24. (Internal) updated Relationships,
Taxonomy_Mappings CWE Content Team. MITRE. 2009-05-27. (Internal) updated Demonstrative_Examples |
|
Page Last Updated:
May 26, 2009
|
|
CWE is a Software Assurance strategic initiative sponsored by the National Cyber Security Division of the U.S. Department of Homeland Security. This Web site is hosted by The MITRE Corporation. Contact cwe@mitre.org for more information. |
|||
