CWE-196: Unsigned to Signed Conversion Error

VariantDraftExploit Likelihood: Medium

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.

View on MITRE
Back to CWE Lookup

Extended Description

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

Structure
Simple

Applicable To

Languages
CC++
Platforms

Frequently Asked Questions

What is CWE-196: Unsigned to Signed Conversion Error?+

CWE-196: Unsigned to Signed Conversion Error is a Common Weakness Enumeration (CWE) entry maintained by MITRE. 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. 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.

What are the security consequences of Unsigned to Signed Conversion Error?+

If exploited, CWE-196 (Unsigned to Signed Conversion Error) it can compromise Availability, Integrity, Confidentiality and Access Control, leading to outcomes such as DoS: Crash, Exit, or Restart, Modify Memory, Execute Unauthorized Code or Commands and Bypass Protection Mechanism.

How do you prevent or mitigate Unsigned to Signed Conversion Error?+

Recommended mitigations for CWE-196 include: Choose a language which is not subject to these casting flaws. 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. Error check the return values of all functions. Be aware of implicit casts made, and use unsigned variables for sizes if at all possible.

Which programming languages are affected by Unsigned to Signed Conversion Error?+

CWE-196 commonly affects C and C++. Note that weaknesses are often language-agnostic patterns, so secure coding practices apply broadly.

What is the difference between a CWE and a CVE?+

A CWE (Common Weakness Enumeration) like CWE-196 describes a category of software weakness — the underlying flaw type. A CVE (Common Vulnerabilities and Exposures) identifies a specific, real-world vulnerability in a particular product. In short, a CWE is the kind of mistake, and a CVE is an instance of that mistake being found in software.

Learn More