CWE-192 Variante Incomplet Medium likelihood

Integer Coercion Error

An integer coercion error occurs when a program incorrectly converts, extends, or truncates a number between different data types, leading to unexpected values.

Définition

What is CWE-192?

An integer coercion error occurs when a program incorrectly converts, extends, or truncates a number between different data types, leading to unexpected values.
These errors happen during operations like type casting or when moving values between different-sized integers (e.g., from a short to a long). While they often cause crashes or corrupt data, they can also create subtle logic flaws. For example, a miscalculated size check might later enable a buffer overflow, turning a data integrity issue into a serious security vulnerability. Managing this at scale is difficult; an ASPM like Plexicus can help you track and remediate these flaws across your entire stack. While SAST tools catch the pattern, Plexicus uses AI to suggest the actual code fix, saving hours of manual work by pinpointing the risky type conversions and providing corrected code snippets.
Impact réel

Real-world CVEs caused by CWE-192

  • Chain: integer coercion error (CWE-192) prevents a return value from indicating an error, leading to out-of-bounds write (CWE-787)

Comment les attaquants l'exploitent

Parcours de l'attaquant étape par étape

  1. 1

    The following code is intended to read an incoming packet from a socket and extract one or more headers.

  2. 2

    The code performs a check to make sure that the packet does not contain too many headers. However, numHeaders is defined as a signed int, so it could be negative. If the incoming packet specifies a value such as -3, then the malloc calculation will generate a negative number (say, -300 if each header can be a maximum of 100 bytes). When this result is provided to malloc(), it is first converted to a size_t type. This conversion then produces a large value such as 4294966996, which may cause malloc() to fail or to allocate an extremely large amount of memory (CWE-195). With the appropriate negative numbers, an attacker could trick malloc() into using a very small positive number, which then allocates a buffer that is much smaller than expected, potentially leading to a buffer overflow.

  3. 3

    The following code reads a maximum size and performs validation on that size. It then performs a strncpy, assuming it will not exceed the boundaries of the array. While the use of "short s" is forced in this particular example, short int's are frequently used within real-world code, such as code that processes structured data.

  4. 4

    This code first exhibits an example of CWE-839, allowing "s" to be a negative number. When the negative short "s" is converted to an unsigned integer, it becomes an extremely large positive integer. When this converted integer is used by strncpy() it will lead to a buffer overflow (CWE-119).

Exemple de code vulnérable

Vulnerable C

The following code is intended to read an incoming packet from a socket and extract one or more headers.

Vulnérable C
DataPacket *packet;
  int numHeaders;
  PacketHeader *headers;
  sock=AcceptSocketConnection();
  ReadPacket(packet, sock);
  numHeaders =packet->headers;
  if (numHeaders > 100) {
  	ExitError("too many headers!");
  }
  headers = malloc(numHeaders * sizeof(PacketHeader);
  ParsePacketHeaders(packet, headers);
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-192

  • Requirements A language which throws exceptions on ambiguous data casts might be chosen.
  • Architecture and Design Design objects and program flow such that multiple or complex casts are unnecessary
  • Implementation Ensure that any data type casting that you must used is entirely understood in order to reduce the plausibility of error in use.
Signaux de détection

How to detect CWE-192

Automated Static Analysis High

Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

Correction automatique Plexicus

Plexicus détecte automatiquement CWE-192 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-192 ?

An integer coercion error occurs when a program incorrectly converts, extends, or truncates a number between different data types, leading to unexpected values.

Quelle est la gravité de CWE-192 ?

MITRE évalue la probabilité d'exploitation comme Moyenne — l'exploitation est réaliste mais nécessite généralement des conditions spécifiques.

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

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

Comment puis-je prévenir CWE-192 ?

A language which throws exceptions on ambiguous data casts might be chosen. Design objects and program flow such that multiple or complex casts are unnecessary

Comment Plexicus détecte et corrige CWE-192 ?

Le moteur SAST de Plexicus reconnaît la signature de flux de données de CWE-192 à 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-192 ?

MITRE publie la définition canonique à https://cwe.mitre.org/data/definitions/192.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.