Use of Singleton Pattern Without Synchronization in a Multithreaded Context

Incomplete Variant
Structure: Simple
Description

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

Extended Description

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.

Common Consequences 1
Scope: OtherIntegrity

Impact: OtherModify Application Data

Potential Mitigations 3
Phase: Architecture and Design
Use the Thread-Specific Storage Pattern. See References.
Phase: 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.
Phase: 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 (Generation of Error Message Containing Sensitive Information).

Effectiveness: Limited

Demonstrative Examples 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.

Code Example:

Bad
Java
java
Consider the following course of events:
- 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.
At this point, the threads have created and returned two different objects.
References 1
Thread-Specifc Storage for C/C++
Douglas C. Schmidt, Timothy H. Harrison, and Nat Pryce
ID: REF-474
Applicable Platforms
Languages:
Java : UndeterminedC++ : Undetermined
Modes of Introduction
Implementation
Taxonomy Mapping
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • Software Fault Patterns