CWE-366 Base Brouillon Medium likelihood

Race Condition within a Thread

This vulnerability occurs when two or more threads within the same application access and manipulate a shared resource (like a variable, data structure, or file) without proper synchronization.…

Définition

What is CWE-366?

This vulnerability occurs when two or more threads within the same application access and manipulate a shared resource (like a variable, data structure, or file) without proper synchronization. Because the threads can execute in an unpredictable order, they can corrupt the resource's state, leading to crashes, incorrect calculations, or data loss.
Unlike race conditions between separate processes, this issue happens entirely within a single program's threads. It's a flaw in the program's internal logic where the developer assumed certain operations would complete in a specific sequence, but the operating system's thread scheduler can interleave them arbitrarily. Common triggers include checking a flag or counter in one thread while another is modifying it, or performing non-atomic 'read-modify-write' operations on shared data. To prevent this, developers must use proper synchronization primitives like mutexes, semaphores, or atomic operations. These tools create critical sections that ensure only one thread can access the shared resource at a time, guaranteeing predictable and valid states. Failing to implement synchronization correctly—or incorrectly assuming certain operations are thread-safe—leaves the application's behavior undefined and unreliable.
Impact réel

Real-world CVEs caused by CWE-366

  • Chain: two threads in a web browser use the same resource (CWE-366), but one of those threads can destroy the resource before the other has completed (CWE-416).

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 demonstrates the weakness.

Vulnérable C
int foo = 0;
  int storenum(int num) {
  	static int counter = 0;
  	counter++;
  	if (num > foo) foo = num;
  	return foo;
  }
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-366

  • Architecture and Design Use locking functionality. This is the recommended solution. Implement some form of locking mechanism around code which alters or reads persistent data in a multithreaded environment.
  • Architecture and Design Create resource-locking validation checks. If no inherent locking mechanisms exist, use flags and signals to enforce your own blocking scheme when resources are being used by other threads of execution.
Signaux de détection

How to detect CWE-366

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

This vulnerability occurs when two or more threads within the same application access and manipulate a shared resource (like a variable, data structure, or file) without proper synchronization. Because the threads can execute in an unpredictable order, they can corrupt the resource's state, leading to crashes, incorrect calculations, or data loss.

Quelle est la gravité de CWE-366 ?

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

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

Comment puis-je prévenir CWE-366 ?

Use locking functionality. This is the recommended solution. Implement some form of locking mechanism around code which alters or reads persistent data in a multithreaded environment. Create resource-locking validation checks. If no inherent locking mechanisms exist, use flags and signals to enforce your own blocking scheme when resources are being used by other threads of execution.

Comment Plexicus détecte et corrige CWE-366 ?

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

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

Faiblesses associées

Weaknesses related to CWE-366

CWE-362 Parent

Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')

A race condition occurs when multiple processes or threads access a shared resource simultaneously without proper coordination, creating a…

CWE-1223 Frère

Race Condition for Write-Once Attributes

This vulnerability occurs when an untrusted software component wins a race condition and writes to a hardware register before the trusted…

CWE-1298 Frère

Hardware Logic Contains Race Conditions

A hardware race condition occurs when security-critical logic circuits receive signals at slightly different times, creating temporary…

CWE-364 Frère

Signal Handler Race Condition

A signal handler race condition occurs when a program's signal handling routine is vulnerable to timing issues, allowing its state to be…

CWE-367 Frère

Time-of-check Time-of-use (TOCTOU) Race Condition

This vulnerability occurs when a program verifies a resource's state (like a file's permissions or existence) but then uses it after that…

CWE-368 Frère

Context Switching Race Condition

This vulnerability occurs when an application switches between different security contexts (like privilege levels or domains) using a…

CWE-421 Frère

Race Condition During Access to Alternate Channel

A race condition occurs when an application opens a secondary communication channel intended for an authorized user, but fails to secure…

CWE-689 Frère

Permission Race Condition During Resource Copy

This vulnerability occurs when a system copies a file or resource but delays setting its final permissions until the entire copy operation…

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.