...
Recall that there are two ways to synchronize access to shared mutable variables, method synchronization and block synchronization. Excessive synchronization can induce a denial of service (DoS) vulnerability because another class whose member locks on the class object, can fail to release the lock promptly. However, this requires the victim class to be accessible from the hostile class.
Wiki Markup |
---|
The _private lock object_ idiom can be used to prevent the DoS vulnerability. The idiom consists of a {{private}} object declared as an instance field. The {{private}} object must be explicitly used for locking purposes in {{synchronized}} blocks, within the class's methods. This lock object belongs to an instance of the object and is not associated with the class object itself. Consequently, there is no lock contention between a class method and a method of a hostile class when both try to lock on the class object. \[[Bloch 01|AA. Java References#Bloch 01]\] |
This idiom can also be suitably used by classes designed for inheritance. If a superclass thread requests a lock on the class object's monitor, a subclass thread can interfere with its operation. Refer to the guideline CON02-J. Always synchronize on the appropriate object for more details.
...
If these restrictions are not met, the object's intrinsic lock is not trustworthy. If all conditions are satisfied, then the object gains no significant security from using a private internal lock object, and may synchronize using its own intrinsic lock.
Likewise, if a static method has the synchronized
keyword, the intrinsic lock of the Class object is obtained, and released when the method completes. The same restrictions listed above apply to static methods, since any untrusted code that can access an object of the class, or a subclass, can use the getClass()
method to obtain access to the Class object. Furthermore, hostile code must not be able to access the Class object at all. This could be accomplished, for instance, by making the class package-private.
Noncompliant Code Example
This noncompliant code example exposes the class object someObject
to untrusted code. The untrusted code attempts to acquire a lock on the class object's monitor and upon succeeding, introduces an indefinite delay which holds up the synchronized
changeValue()
method from acquiring the same lock. Note that the untrusted code also violates CON06-J. Do not defer a thread that is holding a lock.
Code Block | ||
---|---|---|
| ||
public class SomeObject {
public synchronized void changeValue() { // Locks on the class object's monitor
// ...
}
}
// Untrusted code
synchronized (someObject) {
while (true) {
Thread.sleep(Integer.MAX_VALUE); // Indefinitely delay someObject
}
}
|
...
Thread-safe classes that use intrinsic synchronization of the class object may be protected by using the private lock object idiom and adapting them to use block synchronization. In this compliant solution, if the method changeValue()
is called, the lock is obtained on a private
Object
that is inaccessible from the caller.
...
Using a private lock may only be achieved with block synchronization, as method synchronization always uses the object's intrinsic lock. However, block synchronization is also preferred over method synchronization, because it is easy to move operations out of the synchronized block when they might take a long time and they are not truly a critical section.
Noncompliant Code Example
This noncompliant code example exposes the class object of someObject
to untrusted code. The untrusted code attempts to acquire a lock on the class object's monitor and upon succeeding, introduces an indefinite delay which holds up the synchronized
changeValue()
method from acquiring the same lock. Note that the untrusted code also violates CON06-J. Do not defer a thread that is holding a lock.
Code Block | ||
---|---|---|
| ||
public class SomeObject {
public static synchronized void ChangeValue() { // Locks on the class object's monitor
// ...
}
}
// Untrusted code
synchronized (someObject.getClass()) {
while (true) {
Thread.sleep(Integer.MAX_VALUE); // Indefinitely delay someObject
}
}
|
Compliant Solution
Thread-safe classes that use intrinsic synchronization of the class object may be protected by using a static private lock object idiom and adapting them to use block synchronization. In this compliant solution, if the method ChangeValue()
is called, the lock is obtained on a static
private
Object
that is inaccessible from the caller.
Code Block | ||
---|---|---|
| ||
public class SomeObject {
private static final Object lock = new Object(); // private lock object
public static void ChangeValue() {
synchronized (lock) { // Locks on the private Object
// ...
}
}
}
|
Using a private lock may only be achieved with block synchronization, as static method synchronization always uses the intrinsic lock of the object's class. However, block synchronization is also preferred over method synchronization, because it is easy to move operations out of the synchronized block when they might take a long time and they are not truly a critical section.
...