# VNX-667: Improper locking: shared String/boxed monitor or Lock released outside finally

Reports Java synchronized blocks on interned Strings or boxed values, and Lock.lock() calls with no finally block that unlocks the same lock.

| Field | Value |
|---|---|
| Rule ID | `VNX-667` |
| Severity | Low |
| SARIF level | `note` |
| Kind | `sast` |
| Languages | Java |
| CWE | [CWE-667](https://cwe.mitre.org/data/definitions/667.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 | `improper-locking` `cwe-667` `java` `concurrency` |
| Source | [`internal/sast/rules/vnx-667.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-667.rego) |

## Overview

VNX-667 maps to [CWE-667: Improper Locking](https://cwe.mitre.org/data/definitions/667.html). It reports two Java locking mistakes that compile, look correct and still fail:

1. **A shared monitor.** `synchronized` on a String literal, an `.intern()`ed string, `Boolean.TRUE`/`FALSE`, a `valueOf(...)` box, or a field, variable or method parameter the file declares as `String`, `Boolean`, `Integer`, `Long`, `Short`, `Byte` or `Character`. Interned strings and cached boxes are the same object everywhere in the JVM, so unrelated code (another library, another tenant) contends on, or deadlocks with, your lock. A boxed field reassigned inside the block (`generation++`) changes the monitor while it is held.
2. **A lock with no finally.** A statement `lock.lock()` or `lockInterruptibly()` (also `rw.readLock().lock()`) with no `finally` in the next 60 lines that calls the same receiver's `unlock()`. An exception in the critical section leaves the lock held forever.

## What gets flagged

```java
private static final String LOCK = "LOCK";
synchronized (LOCK) { n++; }                 // FLAGGED: interned literal
synchronized (tenant.intern()) { ... }        // FLAGGED
synchronized (Boolean.TRUE) { ... }           // FLAGGED
```

```java
lock.lock();                                  // FLAGGED: unlock() is skipped if this throws
if (amount > balance) throw new IllegalStateException();
balance -= amount;
lock.unlock();
```

## What is not flagged

- `synchronized (this)`, `synchronized (Foo.class)`, or a private `Object` field.
- `lock.lock(); try { ... } finally { lock.unlock(); }`, and `try { lock.lock(); ... } finally { lock.unlock(); }`.
- `tryLock()`, `FileChannel.lock()` assignments, methods that are themselves named `lock`/`acquire` (wrappers).
- Test files (for the Lock shape), comments and string literals.

## Remediation

```java
private final Object lock = new Object();
synchronized (lock) { ... }

lock.lock();
try {
    ...
} finally {
    lock.unlock();
}
```

## References

- [CWE-667: Improper Locking](https://cwe.mitre.org/data/definitions/667.html)
- [SEI CERT LCK01-J: Do not synchronize on objects that may be reused](https://wiki.sei.cmu.edu/confluence/display/java/LCK01-J.+Do+not+synchronize+on+objects+that+may+be+reused)
- [SEI CERT LCK08-J: Ensure actively held locks are released on exceptional conditions](https://wiki.sei.cmu.edu/confluence/display/java/LCK08-J.+Ensure+actively+held+locks+are+released+on+exceptional+conditions)

## 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-667
// nosec vnx-667
```

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