CWE-196
Unsigned to Signed Conversion Error
AI Translation Available
The product uses an unsigned primitive and performs a cast to a signed primitive, which can produce an unexpected value if the value of the unsigned primitive can not be represented using a signed primitive.
Status
draft
Abstraction
variant
Likelihood
medium
Affected Platforms
C
C++
Extended Description
AI Translation
Although less frequent an issue than signed-to-unsigned conversion, unsigned-to-signed conversion can be the perfect precursor to dangerous buffer underwrite conditions that allow attackers to move down the stack where they otherwise might not have access in a normal buffer overflow condition. Buffer underwrites occur frequently when large unsigned values are cast to signed values, and then used as indexes into a buffer or for pointer arithmetic.
Technical Details
AI Translation
Common Consequences
availability
integrity
confidentiality
access control
Impacts
dos: crash, exit, or restart
modify memory
execute unauthorized code or commands
bypass protection mechanism
Detection Methods
automated static analysis
Potential Mitigations
Phases:
requirements
architecture and design
implementation
Descriptions:
•
Choose a language which is not subject to these casting flaws.
•
Error check the return values of all functions. Be aware of implicit casts made, and use unsigned variables for sizes if at all possible.
•
Design object accessor functions to implicitly check values for valid sizes. Ensure that all functions which will be used as a size are checked previous to use as a size. If the language permits, throw exceptions rather than using in-band errors.