Logo
-

Byte Open Security

(ByteOS Network)

Log In

Sign Up

ByteOS

Security
Vulnerability Details
Registries
Custom Views
Weaknesses
Attack Patterns
Filters & Tools
CWE-704:Incorrect Type Conversion or Cast
Weakness ID:704
Version:v4.17
Weakness Name:Incorrect Type Conversion or Cast
Vulnerability Mapping:Allowed-with-Review
Abstraction:Class
Structure:Simple
Status:Incomplete
Likelihood of Exploit:
DetailsContent HistoryObserved CVE ExamplesReports
▼Description

The product does not correctly convert an object, resource, or structure from one type to a different type.

▼Extended Description

▼Alternate Terms
▼Relationships
Relevant to the view"Research Concepts - (1000)"
NatureMappingTypeIDName
ChildOfDiscouragedP664Improper Control of a Resource Through its Lifetime
ParentOfAllowedB1389Incorrect Parsing of Numbers with Different Radices
ParentOfAllowedV588Attempt to Access Child of a Non-structure Pointer
ParentOfAllowedB681Incorrect Conversion between Numeric Types
ParentOfAllowedB843Access of Resource Using Incompatible Type ('Type Confusion')
Nature: ChildOf
Mapping: Discouraged
Type: Pillar
ID: 664
Name: Improper Control of a Resource Through its Lifetime
Nature: ParentOf
Mapping: Allowed
Type: Base
ID: 1389
Name: Incorrect Parsing of Numbers with Different Radices
Nature: ParentOf
Mapping: Allowed
Type: Variant
ID: 588
Name: Attempt to Access Child of a Non-structure Pointer
Nature: ParentOf
Mapping: Allowed
Type: Base
ID: 681
Name: Incorrect Conversion between Numeric Types
Nature: ParentOf
Mapping: Allowed
Type: Base
ID: 843
Name: Access of Resource Using Incompatible Type ('Type Confusion')
▼Memberships
NatureMappingTypeIDName
MemberOfProhibitedC737CERT C Secure Coding Standard (2008) Chapter 4 - Expressions (EXP)
MemberOfProhibitedC741CERT C Secure Coding Standard (2008) Chapter 8 - Characters and Strings (STR)
MemberOfProhibitedC747CERT C Secure Coding Standard (2008) Chapter 14 - Miscellaneous (MSC)
MemberOfProhibitedC875CERT C++ Secure Coding Section 07 - Characters and Strings (STR)
MemberOfProhibitedC883CERT C++ Secure Coding Section 49 - Miscellaneous (MSC)
MemberOfProhibitedC998SFP Secondary Cluster: Glitch in Computation
MemberOfProhibitedV1003Weaknesses for Simplified Mapping of Published Vulnerabilities
MemberOfProhibitedC1129CISQ Quality Measures (2016) - Reliability
MemberOfProhibitedC1157SEI CERT C Coding Standard - Guidelines 03. Expressions (EXP)
MemberOfProhibitedC1158SEI CERT C Coding Standard - Guidelines 04. Integers (INT)
MemberOfProhibitedC1161SEI CERT C Coding Standard - Guidelines 07. Characters and Strings (STR)
MemberOfProhibitedC1306CISQ Quality Measures - Reliability
MemberOfProhibitedV1340CISQ Data Protection Measures
MemberOfProhibitedC1416Comprehensive Categorization: Resource Lifecycle Management
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 737
Name: CERT C Secure Coding Standard (2008) Chapter 4 - Expressions (EXP)
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 741
Name: CERT C Secure Coding Standard (2008) Chapter 8 - Characters and Strings (STR)
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 747
Name: CERT C Secure Coding Standard (2008) Chapter 14 - Miscellaneous (MSC)
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 875
Name: CERT C++ Secure Coding Section 07 - Characters and Strings (STR)
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 883
Name: CERT C++ Secure Coding Section 49 - Miscellaneous (MSC)
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 998
Name: SFP Secondary Cluster: Glitch in Computation
Nature: MemberOf
Mapping: Prohibited
Type:View
ID: 1003
Name: Weaknesses for Simplified Mapping of Published Vulnerabilities
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 1129
Name: CISQ Quality Measures (2016) - Reliability
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 1157
Name: SEI CERT C Coding Standard - Guidelines 03. Expressions (EXP)
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 1158
Name: SEI CERT C Coding Standard - Guidelines 04. Integers (INT)
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 1161
Name: SEI CERT C Coding Standard - Guidelines 07. Characters and Strings (STR)
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 1306
Name: CISQ Quality Measures - Reliability
Nature: MemberOf
Mapping: Prohibited
Type:View
ID: 1340
Name: CISQ Data Protection Measures
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 1416
Name: Comprehensive Categorization: Resource Lifecycle Management
▼Tags
NatureMappingTypeIDName
MemberOfProhibitedBSBOSS-294Not Language-Specific Weaknesses
MemberOfProhibitedBSBOSS-312Other (impact)
Nature: MemberOf
Mapping: Prohibited
Type:BOSSView
ID: BOSS-294
Name: Not Language-Specific Weaknesses
Nature: MemberOf
Mapping: Prohibited
Type:BOSSView
ID: BOSS-312
Name: Other (impact)
▼Relevant To View
Relevant to the view"Weaknesses Addressed by the SEI CERT C Coding Standard - (1154)"
NatureMappingTypeIDName
MemberOfProhibitedC1157SEI CERT C Coding Standard - Guidelines 03. Expressions (EXP)
Nature: MemberOf
Mapping: Prohibited
Type: Category
ID: 1157
Name: SEI CERT C Coding Standard - Guidelines 03. Expressions (EXP)
Relevant to the view"Weaknesses Addressed by the SEI CERT C Coding Standard - (1154)"
NatureMappingTypeIDName
MemberOfProhibitedC1158SEI CERT C Coding Standard - Guidelines 04. Integers (INT)
Nature: MemberOf
Mapping: Prohibited
Type: Category
ID: 1158
Name: SEI CERT C Coding Standard - Guidelines 04. Integers (INT)
Relevant to the view"Weaknesses Addressed by the SEI CERT C Coding Standard - (1154)"
NatureMappingTypeIDName
MemberOfProhibitedC1161SEI CERT C Coding Standard - Guidelines 07. Characters and Strings (STR)
Nature: MemberOf
Mapping: Prohibited
Type: Category
ID: 1161
Name: SEI CERT C Coding Standard - Guidelines 07. Characters and Strings (STR)
Relevant to the view"CISQ Quality Measures (2020) - (1305)"
NatureMappingTypeIDName
MemberOfProhibitedC1306CISQ Quality Measures - Reliability
Nature: MemberOf
Mapping: Prohibited
Type: Category
ID: 1306
Name: CISQ Quality Measures - Reliability
Relevant to the view"Software Fault Pattern (SFP) Clusters - (888)"
NatureMappingTypeIDName
MemberOfProhibitedC998SFP Secondary Cluster: Glitch in Computation
Nature: MemberOf
Mapping: Prohibited
Type: Category
ID: 998
Name: SFP Secondary Cluster: Glitch in Computation
▼Background Detail

▼Common Consequences
ScopeLikelihoodImpactNote
OtherN/AOther
N/A
Scope: Other
Likelihood: N/A
Impact: Other
Note:
N/A
▼Potential Mitigations
▼Modes Of Introduction
Phase: Implementation
Note:

N/A

▼Applicable Platforms
Languages
Class: C(Often Prevalence)
Class: C++(Often Prevalence)
Class: Not Language-Specific(Undetermined Prevalence)
▼Demonstrative Examples
Example 1

In this example, depending on the return value of accecssmainframe(), the variable amount can hold a negative value when it is returned. Because the function is declared to return an unsigned value, amount will be implicitly cast to an unsigned number.

Language: ( code)
N/A

Language: C(Bad code)
unsigned int readdata () { int amount = 0; ... amount = accessmainframe(); ... return amount; }

Language: ( code)
N/A

If the return value of accessmainframe() is -1, then the return value of readdata() will be 4,294,967,295 on a system that uses 32-bit integers.

Example 2

The following code uses a union to support the representation of different types of messages. It formats messages differently, depending on their type.

Language: ( code)
N/A

Language: C(Bad code)
#define NAME_TYPE 1 #define ID_TYPE 2 struct MessageBuffer { int msgType; union { char *name; int nameID; }; }; int main (int argc, char **argv) { struct MessageBuffer buf; char *defaultMessage = "Hello World"; buf.msgType = NAME_TYPE; buf.name = defaultMessage; printf("Pointer of buf.name is %p\n", buf.name); /* This particular value for nameID is used to make the code architecture-independent. If coming from untrusted input, it could be any value. */* buf.nameID = (int)(defaultMessage + 1); printf("Pointer of buf.name is now %p\n", buf.name); if (buf.msgType == NAME_TYPE) { printf("Message: %s\n", buf.name); } else { printf("Message: Use ID %d\n", buf.nameID); } }

Language: ( code)
N/A

The code intends to process the message as a NAME_TYPE, and sets the default message to "Hello World." However, since both buf.name and buf.nameID are part of the same union, they can act as aliases for the same memory location, depending on memory layout after compilation.

Language: ( code)
N/A

As a result, modification of buf.nameID - an int - can effectively modify the pointer that is stored in buf.name - a string.

Language: ( code)
N/A

Execution of the program might generate output such as:

Language: ( code)
N/A

``` Pointer of name is 10830 Pointer of name is now 10831 Message: ello World ```

Language: ( code)
N/A

Notice how the pointer for buf.name was changed, even though buf.name was not explicitly modified.

Language: ( code)
N/A

In this case, the first "H" character of the message is omitted. However, if an attacker is able to fully control the value of buf.nameID, then buf.name could contain an arbitrary pointer, leading to out-of-bounds reads or writes.

▼Observed Examples
ReferenceDescription
CVE-2021-43537
Chain: in a web browser, an unsigned 64-bit integer is forcibly cast to a 32-bit integer (CWE-681) and potentially leading to an integer overflow (CWE-190). If an integer overflow occurs, this can cause heap memory corruption (CWE-122)
CVE-2022-3979
Chain: data visualization program written in PHP uses the "!=" operator instead of the type-strict "!==" operator (CWE-480) when validating hash values, potentially leading to an incorrect type conversion (CWE-704)
Reference: CVE-2021-43537
Description:
Chain: in a web browser, an unsigned 64-bit integer is forcibly cast to a 32-bit integer (CWE-681) and potentially leading to an integer overflow (CWE-190). If an integer overflow occurs, this can cause heap memory corruption (CWE-122)
Reference: CVE-2022-3979
Description:
Chain: data visualization program written in PHP uses the "!=" operator instead of the type-strict "!==" operator (CWE-480) when validating hash values, potentially leading to an incorrect type conversion (CWE-704)
▼Affected Resources
    ▼Functional Areas
      ▼Weakness Ordinalities
      OrdinalityDescription
      ▼Detection Methods
      Fuzzing
      Detection Method ID:DM-13
      Description:

      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
      Note:

      N/A

      ▼Vulnerability Mapping Notes
      Usage:Allowed-with-Review
      Reason:Abstraction
      Rationale:

      This CWE entry is a Class and might have Base-level children that would be more appropriate

      Comments:

      Examine children of this entry to see if there is a better fit

      Suggestions:
      ▼Notes
      ▼Taxonomy Mappings
      Taxonomy NameEntry IDFitEntry Name
      CERT C Secure CodingEXP05-CN/ADo not cast away a const qualification
      CERT C Secure CodingEXP39-CCWE More AbstractDo not access a variable through a pointer of an incompatible type
      CERT C Secure CodingINT31-CCWE More AbstractEnsure that integer conversions do not result in lost or misinterpreted data
      CERT C Secure CodingINT36-CCWE More AbstractConverting a pointer to integer or integer to pointer
      CERT C Secure CodingSTR34-CCWE More AbstractCast characters to unsigned types before converting to larger integer sizes
      CERT C Secure CodingSTR37-CCWE More AbstractArguments to character handling functions must be representable as an unsigned char
      Software Fault PatternsSFP1N/AGlitch in computation
      OMG ASCRMASCRM-CWE-704N/AN/A
      Taxonomy Name: CERT C Secure Coding
      Entry ID: EXP05-C
      Fit: N/A
      Entry Name: Do not cast away a const qualification
      Taxonomy Name: CERT C Secure Coding
      Entry ID: EXP39-C
      Fit: CWE More Abstract
      Entry Name: Do not access a variable through a pointer of an incompatible type
      Taxonomy Name: CERT C Secure Coding
      Entry ID: INT31-C
      Fit: CWE More Abstract
      Entry Name: Ensure that integer conversions do not result in lost or misinterpreted data
      Taxonomy Name: CERT C Secure Coding
      Entry ID: INT36-C
      Fit: CWE More Abstract
      Entry Name: Converting a pointer to integer or integer to pointer
      Taxonomy Name: CERT C Secure Coding
      Entry ID: STR34-C
      Fit: CWE More Abstract
      Entry Name: Cast characters to unsigned types before converting to larger integer sizes
      Taxonomy Name: CERT C Secure Coding
      Entry ID: STR37-C
      Fit: CWE More Abstract
      Entry Name: Arguments to character handling functions must be representable as an unsigned char
      Taxonomy Name: Software Fault Patterns
      Entry ID: SFP1
      Fit: N/A
      Entry Name: Glitch in computation
      Taxonomy Name: OMG ASCRM
      Entry ID: ASCRM-CWE-704
      Fit: N/A
      Entry Name: N/A
      ▼Related Attack Patterns
      IDName
      ▼References
      Reference ID: REF-961
      Title: Automated Source Code Reliability Measure (ASCRM)
      Author: Object Management Group (OMG)
      Section: ASCRM-CWE-704
      Publication:
      Publisher:
      Edition:
      URL:http://www.omg.org/spec/ASCRM/1.0/
      URL Date:
      Day:N/A
      Month:01
      Year:2016
      Details not found