# VNX-821: Incorrect Synchronization

Java code synchronizes, but on a monitor that does not give the intended mutual exclusion. Reported.

| Field | Value |
|---|---|
| Rule ID | `VNX-821` |
| Severity | Low |
| SARIF level | `note` |
| Kind | `sast` |
| Languages | Java |
| CWE | [CWE-821](https://cwe.mitre.org/data/definitions/821.html) |
| CAPEC | [CAPEC-66](https://capec.mitre.org/data/definitions/66.html) |
| ATT&CK | [T1190](https://attack.mitre.org/techniques/T1190/) |
| CWSS vector | `CWSS:1.0/TI:L/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:L/DI:H/EX:L/EC:N/P:L` |
| Tags | `lock-sync` `cwe-821` `concurrency` |
| Source | [`internal/sast/rules/vnx-821.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-821.rego) |

## Overview

VNX-821 maps to [CWE-821: Incorrect Synchronization](https://cwe.mitre.org/data/definitions/821.html). The code takes a lock, but the lock object makes the synchronization useless or shares it with unrelated code.

## What gets flagged

```java
synchronized (new Object()) { n--; }          // FLAGGED: a new monitor every call

Object lock = new Object();
synchronized (lock) { ... }                   // FLAGGED: local lock

synchronized ("cache") { size++; }            // FLAGGED: interned literal shared JVM-wide
synchronized (Boolean.TRUE) { ... }           // FLAGGED
synchronized (getClass()) { ... }             // FLAGGED: subclasses lock a different object

private final ReentrantLock lock = new ReentrantLock();
synchronized (lock) { ... }                   // FLAGGED: use lock.lock()/unlock()

private final String LOCK = "LOCK";
synchronized (LOCK) { ... }                   // FLAGGED: interned String

private static int total;
public synchronized void add(int n) {
    total += n;                               // FLAGGED: instance lock, static data
}
```

Not flagged: `synchronized (this)`, `synchronized (lock)` on a `private final Object lock = new Object()`, `synchronized (Foo.class)`, `static synchronized` methods.

```java
// FLAGGED: every thread locks itself
synchronized (Thread.currentThread()) { entries.add(amount); }
```

Not flagged: a non-final lock field assigned only in the constructor, and `total++` inside `synchronized (Registry.class)` within an instance-synchronized method.

## Remediation

1. Lock on a dedicated `private final Object` field (static when the data is static).
2. Use `lock()`/`unlock()` on `java.util.concurrent` locks, never their monitor.
3. Lock static state on the class (`Foo.class`) or a static final lock.

## References

- [CWE-821: Incorrect Synchronization](https://cwe.mitre.org/data/definitions/821.html)
- [SEI CERT LCK01-J](https://wiki.sei.cmu.edu/confluence/display/java/LCK01-J.+Do+not+synchronize+on+objects+that+may+be+reused)

## Suppressing this rule

Put a `nosec` comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file's language (`#`, `//`, `--` or `;`):

```text
# nosec vnx-821
// nosec vnx-821
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-821 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
