Logo
-

Byte Open Security

(ByteOS Network)

Log In

Sign Up

ByteOS

Security
Vulnerability Details
Registries
Custom Views
Weaknesses
Attack Patterns
Filters & Tools
CWE-843:Access of Resource Using Incompatible Type ('Type Confusion')
Weakness ID:843
Version:v4.17
Weakness Name:Access of Resource Using Incompatible Type ('Type Confusion')
Vulnerability Mapping:Allowed
Abstraction:Base
Structure:Simple
Status:Incomplete
Likelihood of Exploit:
DetailsContent HistoryObserved CVE ExamplesReports
▼Description

The product allocates or initializes a resource such as a pointer, object, or variable using one type, but it later accesses that resource using a type that is incompatible with the original type.

▼Extended Description

When the product accesses the resource using an incompatible type, this could trigger logical errors because the resource does not have expected properties. In languages without memory safety, such as C and C++, type confusion can lead to out-of-bounds memory access.

While this weakness is frequently associated with unions when parsing data with many different embedded object types in C, it can be present in any application that can interpret the same variable or memory location in multiple ways.

This weakness is not unique to C and C++. For example, errors in PHP applications can be triggered by providing array parameters when scalars are expected, or vice versa. Languages such as Perl, which perform automatic conversion of a variable of one type when it is accessed as if it were another type, can also contain these issues.

▼Alternate Terms
Object Type Confusion

▼Relationships
Relevant to the view"Research Concepts - (1000)"
NatureMappingTypeIDName
CanPrecedeDiscouragedC119Improper Restriction of Operations within the Bounds of a Memory Buffer
ChildOfAllowed-with-ReviewC704Incorrect Type Conversion or Cast
ParentOfAllowedB1287Improper Validation of Specified Type of Input
Nature: CanPrecede
Mapping: Discouraged
Type: Class
ID: 119
Name: Improper Restriction of Operations within the Bounds of a Memory Buffer
Nature: ChildOf
Mapping: Allowed-with-Review
Type: Class
ID: 704
Name: Incorrect Type Conversion or Cast
Nature: ParentOf
Mapping: Allowed
Type: Base
ID: 1287
Name: Improper Validation of Specified Type of Input
▼Memberships
NatureMappingTypeIDName
MemberOfProhibitedC136Type Errors
MemberOfProhibitedC1157SEI CERT C Coding Standard - Guidelines 03. Expressions (EXP)
MemberOfProhibitedC1416Comprehensive Categorization: Resource Lifecycle Management
Nature: MemberOf
Mapping: Prohibited
Type:Category
ID: 136
Name: Type Errors
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: 1416
Name: Comprehensive Categorization: Resource Lifecycle Management
▼Tags
NatureMappingTypeIDName
MemberOfProhibitedBSBOSS-311Execute Unauthorized Code or Commands (impact)
MemberOfProhibitedBSBOSS-323Read Memory (impact)
MemberOfProhibitedBSBOSS-324DoS: Crash, Exit, or Restart (impact)
MemberOfProhibitedBSBOSS-331Modify Memory (impact)
Nature: MemberOf
Mapping: Prohibited
Type:BOSSView
ID: BOSS-311
Name: Execute Unauthorized Code or Commands (impact)
Nature: MemberOf
Mapping: Prohibited
Type:BOSSView
ID: BOSS-323
Name: Read Memory (impact)
Nature: MemberOf
Mapping: Prohibited
Type:BOSSView
ID: BOSS-324
Name: DoS: Crash, Exit, or Restart (impact)
Nature: MemberOf
Mapping: Prohibited
Type:BOSSView
ID: BOSS-331
Name: Modify Memory (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"Software Development - (699)"
NatureMappingTypeIDName
MemberOfProhibitedC136Type Errors
Nature: MemberOf
Mapping: Prohibited
Type: Category
ID: 136
Name: Type Errors
▼Background Detail

▼Common Consequences
ScopeLikelihoodImpactNote
AvailabilityIntegrityConfidentialityN/ARead MemoryModify MemoryExecute Unauthorized Code or CommandsDoS: Crash, Exit, or Restart

When a memory buffer is accessed using the wrong type, it could read or write memory out of the bounds of the buffer, if the allocated buffer is smaller than the type that the code is attempting to access, leading to a crash and possibly code execution.

Scope: Availability, Integrity, Confidentiality
Likelihood: N/A
Impact: Read Memory, Modify Memory, Execute Unauthorized Code or Commands, DoS: Crash, Exit, or Restart
Note:

When a memory buffer is accessed using the wrong type, it could read or write memory out of the bounds of the buffer, if the allocated buffer is smaller than the type that the code is attempting to access, leading to a crash and possibly code execution.

▼Potential Mitigations
▼Modes Of Introduction
Phase: Implementation
Note:

N/A

▼Applicable Platforms
Languages
Class: C(Undetermined Prevalence)
Class: C++(Undetermined Prevalence)
▼Demonstrative Examples
Example 1

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.

Example 2

The following PHP code accepts a value, adds 5, and prints the sum.

Language: ( code)
N/A

Language: PHP(Bad code)
$value = $_GET['value']; $sum = $value + 5; echo "value parameter is '$value'<p>"; echo "SUM is $sum";

Language: ( code)
N/A

When called with the following query string:

Language: ( code)
N/A

``` value=123 ```

Language: ( code)
N/A

the program calculates the sum and prints out:

Language: ( code)
N/A

``` SUM is 128 ```

Language: ( code)
N/A

However, the attacker could supply a query string such as:

Language: ( code)
N/A

``` value[]=123 ```

Language: ( code)
N/A

The "[]" array syntax causes $value to be treated as an array type, which then generates a fatal error when calculating $sum:

Language: ( code)
N/A

``` Fatal error: Unsupported operand types in program.php on line 2 ```

Example 3

The following Perl code is intended to look up the privileges for user ID's between 0 and 3, by performing an access of the $UserPrivilegeArray reference. It is expected that only userID 3 is an admin (since this is listed in the third element of the array).

Language: ( code)
N/A

Language: Perl(Bad code)
my $UserPrivilegeArray = ["user", "user", "admin", "user"]; my $userID = get_current_user_ID(); if ($UserPrivilegeArray eq "user") { print "Regular user!\n"; } else { print "Admin!\n"; } print "\$UserPrivilegeArray = $UserPrivilegeArray\n";

Language: ( code)
N/A

In this case, the programmer intended to use "$UserPrivilegeArray->{$userID}" to access the proper position in the array. But because the subscript was omitted, the "user" string was compared to the scalar representation of the $UserPrivilegeArray reference, which might be of the form "ARRAY(0x229e8)" or similar.

Language: ( code)
N/A

Since the logic also "fails open" (CWE-636), the result of this bug is that all users are assigned administrator privileges.

Language: ( code)
N/A

While this is a forced example, it demonstrates how type confusion can have security consequences, even in memory-safe languages.

▼Observed Examples
ReferenceDescription
CVE-2010-4577
Type confusion in CSS sequence leads to out-of-bounds read.
CVE-2011-0611
Size inconsistency allows code execution, first discovered when it was actively exploited in-the-wild.
CVE-2010-0258
Improperly-parsed file containing records of different types leads to code execution when a memory location is interpreted as a different object than intended.
Reference: CVE-2010-4577
Description:
Type confusion in CSS sequence leads to out-of-bounds read.
Reference: CVE-2011-0611
Description:
Size inconsistency allows code execution, first discovered when it was actively exploited in-the-wild.
Reference: CVE-2010-0258
Description:
Improperly-parsed file containing records of different types leads to code execution when a memory location is interpreted as a different object than intended.
▼Affected Resources
    ▼Functional Areas
      ▼Weakness Ordinalities
      OrdinalityDescription
      ▼Detection Methods
      ▼Vulnerability Mapping Notes
      Usage:Allowed
      Reason:Acceptable-Use
      Rationale:

      This CWE entry is at the Base level of abstraction, which is a preferred level of abstraction for mapping to the root causes of vulnerabilities.

      Comments:

      Carefully read both the name and description to ensure that this mapping is an appropriate fit. Do not try to 'force' a mapping to a lower-level Base/Variant simply to comply with this preferred level of abstraction.

      Suggestions:
      ▼Notes
      Applicable Platform

      This weakness is possible in any type-unsafe programming language.

      N/A

      Research Gap

      Type confusion weaknesses have received some attention by applied researchers and major software vendors for C and C++ code. Some publicly-reported vulnerabilities probably have type confusion as a root-cause weakness, but these may be described as "memory corruption" instead.

      For other languages, there are very few public reports of type confusion weaknesses. These are probably under-studied. Since many programs rely directly or indirectly on loose typing, a potential "type confusion" behavior might be intentional, possibly requiring more manual analysis.

      N/A

      ▼Taxonomy Mappings
      Taxonomy NameEntry IDFitEntry Name
      CERT C Secure CodingEXP39-CExactDo not access a variable through a pointer of an incompatible type
      Taxonomy Name: CERT C Secure Coding
      Entry ID: EXP39-C
      Fit: Exact
      Entry Name: Do not access a variable through a pointer of an incompatible type
      ▼Related Attack Patterns
      IDName
      ▼References
      Reference ID: REF-811
      Title: Attacking Interoperability
      Author: Mark Dowd, Ryan Smith, David Dewey
      Section: "Type Confusion Vulnerabilities," page 59
      Publication:
      Publisher:
      Edition:
      URL:http://hustlelabs.com/stuff/bh2009_dowd_smith_dewey.pdf
      URL Date:2023-04-07
      Day:N/A
      Month:N/A
      Year:2009
      Reference ID: REF-62
      Title: The Art of Software Security Assessment
      Author: Mark Dowd, John McDonald, Justin Schuh
      Section: Chapter 7, "Type Confusion", Page 319
      Publication:
      Publisher:Addison Wesley
      Edition:1st Edition
      URL:
      URL Date:
      Day:N/A
      Month:N/A
      Year:2006
      Details not found