CWE-543 Variante Incomplet

Use of Singleton Pattern Without Synchronization in a Multithreaded Context

This vulnerability occurs when a singleton pattern is implemented in a multithreaded application without proper synchronization, potentially leading to multiple instances or corrupted state.

Définition

What is CWE-543?

This vulnerability occurs when a singleton pattern is implemented in a multithreaded application without proper synchronization, potentially leading to multiple instances or corrupted state.
The singleton pattern is designed to ensure only one instance of a class exists. However, in a multithreaded environment, if the creation of that instance is not properly synchronized, multiple threads can simultaneously pass the instance check and create their own copies. This breaks the fundamental guarantee of the pattern and leads to unpredictable application behavior. To prevent this, developers must implement thread-safe initialization. Common solutions include using synchronized blocks during creation, employing eager initialization at class-load time, or leveraging language-specific constructs like atomic references or initialization-on-demand holder idioms. The correct approach depends on your performance requirements and programming language, but ignoring synchronization is not an option in concurrent code.
Impact réel

Real-world CVEs caused by CWE-543

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

    This method is part of a singleton pattern, yet the following singleton() pattern is not thread-safe. It is possible that the method will create two objects instead of only one.

  2. 2

    Consider the following course of events:

  3. 3

    - Thread A enters the method, finds singleton to be null, begins the NumberConverter constructor, and then is swapped out of execution. - Thread B enters the method and finds that singleton remains null. This will happen if A was swapped out during the middle of the constructor, because the object reference is not set to point at the new object on the heap until the object is fully initialized. - Thread B continues and constructs another NumberConverter object and returns it while exiting the method. - Thread A continues, finishes constructing its NumberConverter object, and returns its version.

  4. 4

    At this point, the threads have created and returned two different objects.

Exemple de code vulnérable

Vulnerable Java

This method is part of a singleton pattern, yet the following singleton() pattern is not thread-safe. It is possible that the method will create two objects instead of only one.

Vulnérable Java
private static NumberConverter singleton;
  public static NumberConverter get_singleton() {
  	if (singleton == null) {
  		singleton = new NumberConverter();
  	}
  	return singleton;
  }
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-543

  • Architecture and Design Use the Thread-Specific Storage Pattern. See References.
  • Implementation Do not use member fields to store information in the Servlet. In multithreading environments, storing user data in Servlet member fields introduces a data access race condition.
  • Implementation Avoid using the double-checked locking pattern in language versions that cannot guarantee thread safety. This pattern may be used to avoid the overhead of a synchronized call, but in certain versions of Java (for example), this has been shown to be unsafe because it still introduces a race condition (CWE-209).
Signaux de détection

How to detect CWE-543

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

This vulnerability occurs when a singleton pattern is implemented in a multithreaded application without proper synchronization, potentially leading to multiple instances or corrupted state.

Quelle est la gravité de CWE-543 ?

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

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

Comment puis-je prévenir CWE-543 ?

Use the Thread-Specific Storage Pattern. See References. Do not use member fields to store information in the Servlet. In multithreading environments, storing user data in Servlet member fields introduces a data access race condition.

Comment Plexicus détecte et corrige CWE-543 ?

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

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