# VNX-413: Improper Resource Locking

Java code that may run without the lock it needs or cannot release it: a bare tryLock() whose result is discarded, synchronized (new ...).

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

## Overview

VNX-413 maps to [CWE-413: Improper Resource Locking](https://cwe.mitre.org/data/definitions/413.html). The rule reports Java code that believes it holds exclusive access but may not, or that cannot reliably give it up.

## Why this matters

A discarded `tryLock()` result means the critical section runs whether or not the lock was obtained. `synchronized (new Object())` excludes nobody. A lock released only on the success path stays held after an exception, and every later caller blocks.

## What gets flagged

```java
exportLock.tryLock();                 // FLAGGED: result discarded
synchronized (new Object()) { ... }   // FLAGGED: no shared monitor

lock.lock();                          // FLAGGED: unlock below is not in finally
value = Math.addExact(value, by);
lock.unlock();

rw.readLock().lock();                 // FLAGGED: never unlocked
return items.get(key);
```

Not flagged: `lock.lock(); try { ... } finally { lock.unlock(); }`, `if (lock.tryLock(...))` or an assigned tryLock result, a lock taken in one method and released in another (acquire/close pairs), `lock()` on types that are not java.util.concurrent locks, test files, comments and strings.

## Remediation

1. Always pair `lock()` with `try { ... } finally { lock.unlock(); }` placed immediately after it.
2. Branch on the result of `tryLock()`.
3. Synchronize on a private final field shared by every caller.
4. For confirmed false positives, add a `// vulnetix-ignore: VNX-413` comment on the line.

## References

- [CWE-413: Improper Resource Locking](https://cwe.mitre.org/data/definitions/413.html)
- [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-413
// nosec vnx-413
```

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