Methods that can both modify a static field and be invoked from untrusted code to modify a static field must synchronize access to that the static field. That is necessary because there is no guarantee that untrusted clients will externally synchronize when accessing the field. Because a Even when client-side locking is a specified requirement of the method, untrusted clients can fail to synchronize (whether inadvertently or maliciously). Because the static field is shared by all clients, untrusted clients may violate the contract by failing to provide suitable locking.unmigrated-wiki-markup
According to Joshua Block \[[Bloch 08|AA. Java References#Bloch 08]\]Bloch [Bloch 2008]:
If a method modifies a static field, you must synchronize access to this field, even if the method is typically used only by a single thread. It is not possible for clients to perform external synchronization on such a method because there can be no guarantee that unrelated clients will do likewise.
Documented design intent is irrelevant when dealing with untrusted code , because an attacker can always chose choose to ignore the documentation.
Noncompliant Code Example
This noncompliant code example does not fails to synchronize access to the static counter
field. :
Code Block | ||
---|---|---|
| ||
/** This class is not thread-safe! */ public final class CountHits { private static int counter; public void incrementCounter() { counter++; } } |
This class definition does not violate CON02complies with VNA02-J. Ensure that compound operations on shared variables are atomic, which applies only applies to classes that promise thread-safety. However, this class has a mutable static counter
field that is modified by the publicly accessible incrementCounter()
method. Consequently, this class cannot be used securely by trusted client code, if because untrusted code can purposely fail to externally synchronize access to the field.
Compliant Solution
This compliant solution uses a static private final lock to protect the counter
field and , consequently, does not depend on any consequently lacks any dependence on external synchronization. This solution also complies with CON07 LCK00-J. Use private final lock objects to synchronize classes that may interact with untrusted code.
Code Block | ||
---|---|---|
| ||
/** This class is thread-safe */ public final class CountHits { private static int counter; private static final Object lock = new Object(); public void incrementCounter() { synchronized (lock) { counter++; } } } |
Risk Assessment
Failing Failure to internally internally synchronize access to static fields that may can be modified by by untrusted code will result in incorrectly synchronized code, if risks incorrect synchronization because the author of the untrusted code chooses to can inadvertently or maliciously ignore the synchronization policy.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|
LCK05-J |
Low |
Probable |
Medium | P4 | L3 |
Automated Detection
...
TODO
Related Vulnerabilities
Any vulnerabilities resulting from the violation of this rule are listed on the CERT website.
References
Wiki Markup |
---|
\[[API 06|AA. Java References#API 06]\]
\[[Bloch 08|AA. Java References#Bloch 08]\] Item 67: "Avoid excessive synchronization" |
Issue Tracking
...
||Completed||Priority||Locked||CreatedDate||CompletedDate||Assignee||Name||
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
CodeSonar |
| JAVA.CONCURRENCY.UG.METH | Unguarded Method (Java) | ||||||
Parasoft Jtest |
| CERT.LCK05.IASF | Inspect accesses to "static" fields which may require synchronization |
Related Guidelines
Bibliography
[API 2014] | |
Item 67, "Avoid Excessive Synchronization" |
...
CON31-J. Avoid client-side locking when using classes that do not commit to their locking strategy 11. Concurrency (CON) CON33-J. Document thread-safety and use annotations where applicable