CWE-843 Base Incomplet

Access of Resource Using Incompatible Type ('Type Confusion')

Type confusion occurs when a program creates a resource—like a pointer, object, or variable—with one data type, but later incorrectly accesses it as a different, incompatible type.

Définition

What is CWE-843?

Type confusion occurs when a program creates a resource—like a pointer, object, or variable—with one data type, but later incorrectly accesses it as a different, incompatible type.
This mismatch can cause the program to misinterpret the data in memory, leading to logical errors, crashes, or unexpected behavior because the resource lacks the properties the code expects. In memory-unsafe languages like C and C++, this often results in dangerous out-of-bounds memory access, corrupting data or creating security vulnerabilities. While commonly seen with C/C++ unions when parsing complex data structures, type confusion can appear in many languages. For instance, PHP might fail if an array is passed where a single value is required, and languages like Perl that perform automatic type conversion can also introduce subtle bugs when variables are accessed as unintended types.
Impact réel

Real-world CVEs caused by CWE-843

  • Type confusion in CSS sequence leads to out-of-bounds read.

  • Size inconsistency allows code execution, first discovered when it was actively exploited in-the-wild.

  • Improperly-parsed file containing records of different types leads to code execution when a memory location is interpreted as a different object than intended.

Comment les attaquants l'exploitent

Parcours de l'attaquant étape par étape

  1. 1

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

  2. 2

    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.

  3. 3

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

  4. 4

    Execution of the program might generate output such as:

  5. 5

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

Exemple de code vulnérable

Vulnerable C

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

Vulnérable C
#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);
  		}
  }
Exemple de code sécurisé

Secure pseudo

Sécurisé pseudo
// Validate, sanitize, or use a safe API before reaching the sink.
function handleRequest(input) {
  const safe = validateAndEscape(input);
  return executeWithGuards(safe);
}
What changed: the unsafe sink is replaced (or the input is validated/escaped) so the same payload no longer triggers the weakness.
Liste de contrôle de prévention

How to prevent CWE-843

  • Architecture Use safe-by-default frameworks and APIs that prevent the unsafe pattern from being expressible.
  • Implementation Validate input at trust boundaries; use allowlists, not denylists.
  • Implementation Apply the principle of least privilege to credentials, file paths, and runtime permissions.
  • Testing Cover this weakness in CI: SAST rules + targeted unit tests for the data flow.
  • Operation Monitor logs for the runtime signals listed in the next section.
Signaux de détection

How to detect CWE-843

SAST High

Exécuter une analyse statique (SAST) sur le code source à la recherche du motif non sécurisé dans le flux de données.

DAST Moderate

Exécuter des tests de sécurité applicative dynamique (DAST) contre le point de terminaison en ligne.

Runtime Moderate

Surveiller les journaux runtime pour détecter des traces d'exception inhabituelles, des entrées malformées ou des tentatives de contournement d'autorisation.

Code review Moderate

Revue de code : signaler tout nouveau code qui traite les entrées de cette surface sans utiliser les helpers du framework validés.

Correction automatique Plexicus

Plexicus détecte automatiquement CWE-843 et ouvre une PR de correction en moins de 60 secondes.

Codex Remedium analyse chaque commit, identifie cette faiblesse précise et livre une pull request prête à être relue avec le correctif. Pas de tickets. Pas de transferts.

Questions fréquentes

Frequently asked questions

Qu'est-ce que CWE-843 ?

Type confusion occurs when a program creates a resource—like a pointer, object, or variable—with one data type, but later incorrectly accesses it as a different, incompatible type.

Quelle est la gravité de CWE-843 ?

MITRE n'a pas publié de note de probabilité d'exploitation pour cette faiblesse. Traitez-la comme un impact moyen jusqu'à ce que votre modèle de menace prouve le contraire.

Quels langages ou plateformes sont affectés par CWE-843 ?

MITRE lists the following affected platforms: C, C++.

Comment puis-je prévenir CWE-843 ?

Use safe-by-default frameworks, validate untrusted input at trust boundaries, and apply the principle of least privilege. Cover the data-flow signature in CI with SAST.

Comment Plexicus détecte et corrige CWE-843 ?

Le moteur SAST de Plexicus reconnaît la signature de flux de données de CWE-843 à chaque commit. Lorsqu'une correspondance est trouvée, notre agent Codex Remedium ouvre une PR de correction avec le code corrigé, les tests et un résumé d'une ligne pour le relecteur.

Où puis-je en savoir plus sur CWE-843 ?

MITRE publie la définition canonique à https://cwe.mitre.org/data/definitions/843.html. Vous pouvez également consulter la documentation OWASP et NIST pour des conseils adjacents.

Prêt quand vous l'êtes

Arrêtez de payer par développeur.
Commencez à fermer la boucle.

Plexicus est l'ASPM natif IA qui scanne, filtre, corrige, penteste et explique — de façon autonome. Développeurs illimités, dépôts illimités, actions IA à usage équitable. Vrai niveau gratuit, €269/mo annuel quand vous êtes prêt.