CWE-135 Base Brouillon

Incorrect Calculation of Multi-Byte String Length

This vulnerability occurs when software incorrectly measures the length of strings containing multi-byte or wide characters, leading to buffer overflows, data corruption, or crashes.

Définition

What is CWE-135?

This vulnerability occurs when software incorrectly measures the length of strings containing multi-byte or wide characters, leading to buffer overflows, data corruption, or crashes.
Many programming languages and systems use multi-byte character encodings (like UTF-8) or wide characters (like UTF-16) where a single logical character can be made of multiple bytes. Standard string length functions (like strlen in C) often count bytes, not characters, which creates a mismatch. When security checks or memory allocations rely on this incorrect count, buffers can be overrun or under-read, creating a critical entry point for attacks. Developers encounter this when handling international text, file paths, or user input without using encoding-aware functions. To prevent it, always use dedicated library functions designed for your specific character encoding (like mbstowcs or wcslen for wide strings) and validate that memory operations account for the maximum possible bytes per character, not just the count of logical units.
Impact réel

Real-world CVEs caused by CWE-135

Aucune référence CVE publique n'est liée à ce CWE dans le catalogue MITRE pour le moment.

Comment les attaquants l'exploitent

Parcours de l'attaquant étape par étape

  1. 1

    Identifier un chemin de code qui traite des entrées non fiables sans validation.

  2. 2

    Élaborer une charge utile qui exploite le comportement non sécurisé — injection, traversal, débordement ou abus de logique.

  3. 3

    Délivrer la charge utile via une requête normale et observer la réaction de l'application.

  4. 4

    Itérer jusqu'à ce que la réponse divulgue des données, exécute le code de l'attaquant ou élève les privilèges.

Exemple de code vulnérable

Vulnerable C

The following example would be exploitable if any of the commented incorrect malloc calls were used.

Vulnérable C
#include <stdio.h>
  #include <strings.h>
  #include <wchar.h>
  int main() {
  		wchar_t wideString[] = L"The spazzy orange tiger jumped " \
  		"over the tawny jaguar.";
  		wchar_t *newString;
  		printf("Strlen() output: %d\nWcslen() output: %d\n",
  		strlen(wideString), wcslen(wideString));
  		/* Wrong because the number of chars in a string isn't related to its length in bytes //
  		newString = (wchar_t *) malloc(strlen(wideString));
  		*/
  		/* Wrong because wide characters aren't 1 byte long! //
  		newString = (wchar_t *) malloc(wcslen(wideString));
  		*/
  		/* Wrong because wcslen does not include the terminating null */
  		newString = (wchar_t *) malloc(wcslen(wideString) * sizeof(wchar_t));
  		/* correct! */
  		newString = (wchar_t *) malloc((wcslen(wideString) + 1) * sizeof(wchar_t));
  		/* ... */
  }
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-135

  • Implementation Always verify the length of the string unit character.
  • Implementation Use length computing functions (e.g. strlen, wcslen, etc.) appropriately with their equivalent type (e.g.: byte, wchar_t, etc.)
Signaux de détection

How to detect CWE-135

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-135 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-135 ?

This vulnerability occurs when software incorrectly measures the length of strings containing multi-byte or wide characters, leading to buffer overflows, data corruption, or crashes.

Quelle est la gravité de CWE-135 ?

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-135 ?

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

Comment puis-je prévenir CWE-135 ?

Always verify the length of the string unit character. Use length computing functions (e.g. strlen, wcslen, etc.) appropriately with their equivalent type (e.g.: byte, wchar_t, etc.)

Comment Plexicus détecte et corrige CWE-135 ?

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

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

Faiblesses associées

Weaknesses related to CWE-135

CWE-682 Parent

Incorrect Calculation

This vulnerability occurs when software performs a calculation that produces wrong or unexpected results, which are then used to make…

CWE-128 Frère

Wrap-around Error

A wrap-around error happens when a variable exceeds the maximum value its data type can hold, causing it to unexpectedly reset to a very…

CWE-131 Frère

Incorrect Calculation of Buffer Size

This vulnerability occurs when a program miscalculates the amount of memory needed for a buffer, potentially leading to a buffer overflow…

CWE-1335 Frère

Incorrect Bitwise Shift of Integer

This vulnerability occurs when a program attempts to shift an integer's bits by an invalid amount—either a negative number or a value…

CWE-1339 Frère

Insufficient Precision or Accuracy of a Real Number

This vulnerability occurs when a program uses a data type or algorithm that cannot accurately represent or calculate the fractional part…

CWE-190 Frère

Integer Overflow or Wraparound

Integer overflow or wraparound occurs when a calculation produces a numeric result that exceeds the maximum value a variable can hold.…

CWE-191 Frère

Integer Underflow (Wrap or Wraparound)

Integer underflow occurs when a subtraction operation results in a value smaller than the data type's minimum limit, causing the value to…

CWE-193 Frère

Off-by-one Error

An off-by-one error occurs when a program incorrectly calculates a boundary, such as a loop counter or array index, by being one unit too…

CWE-369 Frère

Divide By Zero

A divide-by-zero error occurs when software attempts to perform a division operation where the denominator is zero.

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.