Concurrency related vulnerabilities can manifest themselves when assumptions are made about the multithreaded behavior of derived classes. An overridden synchronized method's contract may be violated if a subclass defines an implementation that is not safe for concurrent access. Furthermore, if an overridden method makes concurrency guarantees, even if it is not itself synchronized, the subclass must adhere to those guarantees.
The rule CON04-J. Synchronize using an internal private lock object places stringent documentation requirements on any class that can be subclassed and makes concurrency guarantees.
Noncompliant Code Example (synchronized method)
This noncompliant code example defines a synchronized doSomething() method in a class called SynchronizedBase.
class ThreadSafeBase {
public synchronized void doSomething() {
// ...
}
}
class ThreadUnsafeSubclass extends ThreadSafeBase {
public void doSomething() {
// ...
}
}
The base method can be safely used by many threads. However, if a subclass ThreadUnsafeSubclass overrides the method but leaves it unsynchronized, its instance cannot be safely passed to multiple threads.
This problem is hard to notice because threads that accept instances of ThreadSafeBase also accept instances of its subclasses. Consequently, the threads may incorrectly assume that the subclasses are thread-safe.
Compliant Solution (synchronized method)
This compliant solution synchronizes the doSomething() method of the subclass.
class ThreadSafeBase {
public synchronized void doSomething() {
// ...
}
}
class ThreadSafeSubclass extends ThreadSafeBase {
public synchronized void doSomething() {
// ...
}
}
This compliant solution does not violate CON04-J. Synchronize using an internal private lock object because the accessibility of the class is package-private which is allowable when untrusted code cannot infiltrate the package.
Noncompliant Code Example (private lock)
This noncompliant code example defines a doSomething() method in a class called ThreadSafeBase. This method utilizes an internal private lock, in accordance with CON04-J. Synchronize using an internal private lock object.
class ThreadSafeBase {
private final Object lock = new Object();
public void doSomething() {
synchronized (lock) {
// ...
}
}
}
class ThreadUnsafeSubclass extends ThreadSafeBase {
public void doSomething() {
// ...
}
}
The base method can be safely used by many threads. However, if a subclass ThreadUnsafeSubclass overrides the method but provides no concurrency guarantees, its instance cannot be safely passed to multiple threads.
Compliant Solution (private lock)
This compliant solution synchronizes the doSomething() method of the subclass.
class ThreadSafeBase {
private final Object lock = new Object();
public void doSomething() {
synchronized (lock) {
// ...
}
}
}
class ThreadSafeSubclass extends ThreadSafeBase {
private final Object lock = new Object();
public void doSomething() {
synchronized (lock) {
// ...
}
}
}
Note that the ThreadSafeBase.lock and ThreadSafeSubclass.lock objects are distinct and mutually invisible. Consequently ThreadSafeSubclass can provide some thread-safety guarantees similar to ThreadSafeBase.
Risk Assessment
Failure to provide synchronization in overriding methods can result in unexpected behavior.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
|---|---|---|---|---|---|
CON10- J |
low |
probable |
medium |
P4 |
L3 |
Automated Detection
TODO
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
[[API 06]]
[[SDN 08]] Sun bug database, Bug ID 4294756![]()
VOID CON09-J. Do not invoke alien methods that rely on invariants protected by the same lock object 11. Concurrency (CON) CON11-J. Do not assume that declaring an object volatile guarantees visibility of its members