Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: wordsmithing

Wiki Markup
The {{java.util.Collections}} interface's documentation \[[API 06|AA. Java References#API 06]\] warns about the consequences of synchronizing on any view over a collection object, rather than synchronizing on the collection object itself.

...

It

...

is

...

imperative

...

that

...

the

...

user

...

manually

...

synchronize

...

on

...

the

...

returned

...

map

...

when

...

iterating

...

over

...

any

...

of

...

its

...

collection

...

views...

...

Failure

...

to

...

follow

...

this

...

advice

...

may

...

result

...

in

...

non-deterministic

...

behavior.

...

Synchronize on the original collection object when using synchronization wrappers to enforce atomicity (CON07-J.

...

Do

...

not

...

assume

...

that

...

a

...

group

...

of

...

calls

...

to

...

independently

...

atomic

...

methods

...

is

...

atomic

...

).

Noncompliant Code Example (collection view)

Wiki Markup
This noncompliant code example creates two views, a synchronized view of an empty map, which is held by the {{map}} field, and a set view of the map's keys, which is held by the {{set}} field. Furthermore, this code synchronizes on the {{set}} view instead of the 

...

{{map}}

...

 

...

view \[[Tutorials 08|AA. Java References#Tutorials 08]\].

{:=
Code Block
bgColor
#FFcccc
}
// map has package-private accessibility
final Map<Integer, String> map = Collections.synchronizedMap(new HashMap<Integer, String>());
private final Set<Integer> set = map.keySet();

public void doSomething() {
  synchronized(set) {  // Incorrectly synchronizes on set
    for (Integer k : set) { 
      // ...
    }
  }
}
{code}
 

h2. Compliant Solution (collection lock object)

This compliant solution synchronizes on the original {{Collection}} object {{map}

If the set is synchronized instead of the map, another thread may modify the contents of the map, and invalidate the k iterator.

Compliant Solution (collection lock object)

This compliant solution synchronizes on the map view instead of the set view. This is safe because the origianl map (created by new HashMap()) is not accessible and hence unmodifiable except via the map field.

Code Block
bgColor#ccccff
} instead of the {{Collection}} view {{set}}. 

{code:bgColor=#ccccff}
// map has package-private accessibility
final Map<Integer, String> map = Collections.synchronizedMap(new HashMap<Integer, String>());
private final Set<Integer> set = map.keySet();

public void doSomething() {
  synchronized(map) {  // Synchronize on map, not set
    for (Integer k : set) { 
      // ...
    }
  }
}
{code}

h2. Risk Assessment

Synchronizing on a collection view instead of the collection object may cause non-deterministic behavior. 

|| Rule || Severity || Likelihood || Remediation Cost || Priority || Level ||
| CON40-J | medium | probable | medium | {color:#cc9900}{*}P8{*}{color} | {color:#cc9900}{*}L2{*}{color} |


h3. Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the [CERT website|https://www.kb.cert.org/vulnotes/bymetric?searchview&query=FIELD+KEYWORDS+contains+CON36-J].

h2. References

Risk Assessment

Synchronizing on a collection view instead of the collection object may cause non-deterministic behavior.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

CON40-J

medium

probable

medium

P8

L2

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

Wiki Markup
\[[API 06|AA. Java References#API 06]\] Class Collections
\[[Tutorials 08|AA. Java References#Tutorials 08]\] [Wrapper Implementations|http://java.sun.com/docs/books/tutorial/collections/implementations/wrapper.html]

...

Issue

...

Tracking

Tasklist
Review List
Review List


{tasklist:Review List}
||Completed||Priority||Locked||CreatedDate||CompletedDate||Assignee||Name||
|F|M|F|1269453074651|          |dmohindr|"warns about the consequences of synchronizing on any view over a collection object, rather than synchronizing on the collection object" ... it doesn't warn against it...it just says you should synchronize the collection...suggest reverting the change|
|T|M|F|1269452979738|1269635081004|rcs|The title could do away with "still"|
{tasklist}



----
[!The CERT Sun Microsystems Secure Coding Standard for Java^button_arrow_left.png!|VOID 

...

VOID CON00-J.

...

Synchronize

...

access

...

to

...

shared

...

mutable

...

variables      11. Concurrency (CON)      CON03-J.

...

Do

...

not

...

use

...

background

...

threads

...

during

...

class

...

initialization

...