CWE-1335 Base Brouillon

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 equal to or greater than the integer's bit width (e.g., shifting…

Définition

What is CWE-1335?

This vulnerability occurs when a program attempts to shift an integer's bits by an invalid amount—either a negative number or a value equal to or greater than the integer's bit width (e.g., shifting a 32-bit integer by 32 or more places). This leads to unpredictable and platform-dependent results.
Shifting bits by a negative count is considered undefined behavior in languages like C and C++. Compilers and interpreters typically don't validate this at runtime, leaving the actual operation to be handled by the underlying hardware. Different CPU architectures may produce varying results—such as shifting in the opposite direction, yielding zero, or even causing a crash—which breaks code portability and introduces subtle bugs. Similarly, an overshift (shifting beyond the bit width) also produces undefined or implementation-defined results. Some languages or compilers might mask the shift count, wrap the value, or return zero, but you cannot rely on consistent behavior. This ambiguity makes the code's outcome architecture- and compiler-dependent, creating security risks when the shifted value is used for calculations, memory offsets, or access controls.
Impact réel

Real-world CVEs caused by CWE-1335

  • An unexpected large value in the ext4 filesystem causes an overshift condition resulting in a divide by zero.

  • An unexpected large value in the ext4 filesystem causes an overshift condition resulting in a divide by zero - fix of CVE-2009-4307.

  • An overshift in a kernel allowed out of bounds reads and writes resulting in a root takeover.

  • Program is not properly handling signed bitwise left-shifts causing an overlapping memcpy memory range error.

  • Compression function improperly executes a signed left shift of a negative integer.

  • Some kernels improperly handle right shifts of 32 bit numbers in a 64 bit register.

  • Putty has an incorrectly sized shift value resulting in an overshift.

  • LED driver overshifts under certain conditions resulting in a DoS.

Comment les attaquants l'exploitent

Parcours de l'attaquant étape par étape

  1. 1

    A negative shift amount for an x86 or x86_64 shift instruction will produce the number of bits to be shifted by taking a 2's-complement of the shift amount and effectively masking that amount to the lowest 6 bits for a 64 bit shift instruction.

  2. 2

    The example above ends up with a shift amount of -5. The hexadecimal value is FFFFFFFFFFFFFFFD which, when bits above the 6th bit are masked off, the shift amount becomes a binary shift value of 111101 which is 61 decimal. A shift of 61 produces a very different result than -5. The previous example is a very simple version of the following code which is probably more realistic of what happens in a real system.

  3. 3

    Note that the good example not only checks for negative shifts and disallows them, but it also checks for over-shifts. No bit operation is done if the shift is out of bounds. Depending on the program, perhaps an error message should be logged.

Exemple de code vulnérable

Vulnerable C

A negative shift amount for an x86 or x86_64 shift instruction will produce the number of bits to be shifted by taking a 2's-complement of the shift amount and effectively masking that amount to the lowest 6 bits for a 64 bit shift instruction.

Vulnérable C
unsigned int r = 1 << -5;
Exemple de code sécurisé

Secure C

Sécurisé C
int choose_bit(int reg_bit, int bit_number_from_elsewhere) 
 {

```
   if (NEED_TO_SHIFT)
   {
  	 reg_bit -= bit_number_from_elsewhere;
   }
   return reg_bit;
 }
 unsigned int handle_io_register(unsigned int *r)
 {
   int the_bit_number = choose_bit(5, 10);
   if ((the_bit_number > 0) && (the_bit_number < 63))
   {
  	 unsigned int the_bit = 1 << the_bit_number;
  	 *r |= the_bit;
   }
   return the_bit;
 }
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-1335

  • Implementation Implicitly or explicitly add checks and mitigation for negative or over-shift values.
Signaux de détection

How to detect CWE-1335

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

This vulnerability occurs when a program attempts to shift an integer's bits by an invalid amount—either a negative number or a value equal to or greater than the integer's bit width (e.g., shifting a 32-bit integer by 32 or more places). This leads to unpredictable and platform-dependent results.

Quelle est la gravité de CWE-1335 ?

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

MITRE lists the following affected platforms: C, C++, C#, Java, JavaScript, Not OS-Specific, Not Technology-Specific.

Comment puis-je prévenir CWE-1335 ?

Implicitly or explicitly add checks and mitigation for negative or over-shift values.

Comment Plexicus détecte et corrige CWE-1335 ?

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

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

Faiblesses associées

Weaknesses related to CWE-1335

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-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-135 Frère

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…

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.