Misuse of synchronization primitives is a common source of concurrency issues. Synchronizing on objects that may be reused can result in deadlock and nondeterministic behavior. Consequently, programs must never synchronize on objects that may be reused.
Noncompliant Code Example (Boolean
Lock Object)
This noncompliant code example synchronizes on a Boolean
lock object.
private final Boolean initialized = Boolean.FALSE; public void doSomething() { synchronized (initialized) { // ... } }
The Boolean
type is unsuitable for locking purposes because it allows only two values: true and false. Boolean literals containing the same value share unique instances of the Boolean
class in the JVM. In this example, initialized
refers to the instance corresponding to the value false. If any other code were to inadvertently synchronize on a Boolean
literal with the value false, the lock instance would be reused and the system could become unresponsive or could deadlock.
Noncompliant Code Example (Boxed Primitive)
This noncompliant code example locks on a boxed Integer
object.
int lock = 0; private final Integer Lock = lock; // Boxed primitive Lock is shared public void doSomething() { synchronized (Lock) { // ... } }
Boxed types may use the same instance for a range of integer values; consequently, they suffer from the same reuse problem as Boolean
constants. The wrapper object are reused when the value can be represented as a byte; JVM implementations are also permitted to reuse wrapper objects for larger ranges of values. Note that use of the intrinsic lock associated with the boxed Integer
wrapper object is insecure; instances of the Integer
object constructed using the new
operator (new Integer(value)
) are unique and not reused. In general, locks on any data type that contains a boxed value are insecure.
Compliant Solution (Integer)
This compliant solution recommends locking on a non-boxed Integer
, using a variant of the private lock object idiom. The doSomething()
method synchronizes using the intrinsic lock of the Integer
instance, Lock
.
int lock = 0; private final Integer Lock = new Integer(lock); public void doSomething() { synchronized (Lock) { // ... } }
When explicitly constructed, an Integer
object has a unique reference and its own intrinsic lock that is distinct not only from other Integer
objects, but also from boxed integers that have the same value. While this is an acceptable solution, it can cause maintenance problems because developers can incorrectly assume that boxed integers are also appropriate lock objects. A more appropriate solution is to synchronize on a private final lock object as described in the compliant solution below.
Noncompliant Code Example (Interned String
Object)
This noncompliant code example locks on an interned String
object.
private final String lock = new String("LOCK").intern(); public void doSomething() { synchronized (lock) { // ... } }
According to the Java API class [[API 2006]] java.lang.String
documentation
When the
intern()
method is invoked, if the pool already contains a string equal to thisString
object as determined by theequals(Object)
method, then the string from the pool is returned. Otherwise, thisString
object is added to the pool and a reference to thisString
object is returned.
Consequently, an interned String
object behaves like a global variable in the JVM. As demonstrated in this noncompliant code example, even when every instance of an object maintains its own lock
field, the fields all refer to a common String
constant. Locking on String
constants has the same reuse problem as locking on Boolean
constants.
Additionally, hostile code from any other package can exploit this vulnerability, if the class is accessible. (For more information, see rule LCK00-J. Use private final lock objects to synchronize classes that may interact with untrusted code.)
Noncompliant Code Example (String
Literal)
This noncompliant code example locks on a final String
literal.
// This bug was found in jetty-6.1.3 BoundedThreadPool private final String lock = "LOCK"; // ... synchronized (lock) { // ... } // ...
String
literals are constant and are automatically interned. Consequently, this example suffers from the same pitfalls as the preceding noncompliant code example.
Compliant Solution (String
Instance)
This compliant solution locks on a non-interned String
instance.
private final String lock = new String("LOCK"); public void doSomething() { synchronized (lock) { // ... } }
A String
instance differs from a String
literal. The instance has a unique reference and its own intrinsic lock that is distinct from other String
object instances or literals. Nevertheless, a better approach is to synchronize on a private final lock object as shown in the following compliant solution.
Compliant Solution (Private Final Lock Object
)
This compliant solution synchronizes on a private final lock object. This is one of the few cases where a java.lang.Object
instance is useful.
private final Object lock = new Object(); public void doSomething() { synchronized (lock) { // ... } }
For more information on using an Object
as a lock, see rule LCK00-J. Use private final lock objects to synchronize classes that may interact with untrusted code.
Risk Assessment
A significant number of concurrency vulnerabilities arise from locking on the wrong kind of object. It is important to consider the properties of the lock object rather than indiscreetly scavenging for objects on which to synchronize.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
LCK01-J |
medium |
probable |
medium |
P8 |
L2 |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="af36f3c3-daf2-4f52-8772-c1587ac8f082"><ac:plain-text-body><![CDATA[ |
[[API 2006 |
AA. Bibliography#API 06]] |
Class String, Collections |
]]></ac:plain-text-body></ac:structured-macro> |
|
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="45c77aa1-a149-4d7f-ae8d-2abe785ef1d5"><ac:plain-text-body><![CDATA[ |
[[Findbugs 2008 |
AA. Bibliography#Findbugs 08]] |
|
]]></ac:plain-text-body></ac:structured-macro> |
|
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1a6d4577-f9f6-4560-9bf1-9e7230c5c660"><ac:plain-text-body><![CDATA[ |
[[Miller 2009 |
AA. Bibliography#Miller 09]] |
Locking |
]]></ac:plain-text-body></ac:structured-macro> |
|
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="119cf922-e343-4ea4-ade1-22110f0727fa"><ac:plain-text-body><![CDATA[ |
[[Pugh 2008 |
AA. Bibliography#Pugh 08]] |
"Synchronization" |
]]></ac:plain-text-body></ac:structured-macro> |
|
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0140d2fd-ee16-4603-87af-4274326c67f3"><ac:plain-text-body><![CDATA[ |
[[Tutorials 2008 |
AA. Bibliography#Tutorials 08]] |
[Wrapper Implementations |
http://java.sun.com/docs/books/tutorial/collections/implementations/wrapper.html] |
]]></ac:plain-text-body></ac:structured-macro> |
Automated Detection
The following table summarizes the examples flagged as violations by FindBugs:
Noncompliant Code Example |
Flagged |
Checker |
Message |
---|---|---|---|
|
Yes |
DL_SYNCHRONIZATION_ON_BOOLEAN |
Synchronization on Boolean could deadlock |
Boxed primitive |
Yes |
DL_SYNCHRONIZATION_ON_BOXED_PRIMITIVE |
Synchronization on Integer could deadlock |
interned |
No |
n/a |
n/a |
|
Yes |
DL_SYNCHRONIZATION_ON_SHARED_CONSTANT |
Synchronization on interned String could deadlock |
Related Vulnerabilities
Any vulnerabilities resulting from the violation of this rule are listed on the CERT website.