# VNX-415: Double free

Reports a C or C++ pointer released twice on one path: free/g_free/kfree/OPENSSL_free/delete of the same expression again within 20 lines with no reassignment.

| Field | Value |
|---|---|
| Rule ID | `VNX-415` |
| Severity | High |
| SARIF level | `error` |
| Kind | `sast` |
| Languages | C, C++ |
| CWE | [CWE-415](https://cwe.mitre.org/data/definitions/415.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:H/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:H/DI:H/EX:H/EC:N/P:H` |
| Tags | `double-free` `memory-safety` `cwe-415` |
| Source | [`internal/sast/rules/vnx-415.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-415.rego) |

## Overview

VNX-415 maps to [CWE-415: Double Free](https://cwe.mitre.org/data/definitions/415.html). Releasing the same heap block twice corrupts the allocator's free lists; an attacker who controls the allocations in between can usually turn that into a write of their choosing.

The rule reports two shapes:

1. **A second release on the same path.** `free`, `g_free`, `kfree`, `vfree`, `kvfree`, `OPENSSL_free`, `CRYPTO_free`, `xfree`, `std::free`, `delete` or `delete[]` of a variable or member expression, followed within 20 lines by another release of the same expression, with nothing in between that reassigns it (`p = NULL`, `p = malloc(n)`, `get(&p)`, reassigning the struct that holds the member), leaves the path (`return`, `break`, `continue`, `goto`, `exit`, `abort`, `throw` at the release's nesting level) or starts another branch (`else`, `case`, `default`, `#else`, the end of the function).
2. **Release, then goto cleanup.** A release immediately followed by `goto LABEL`, where the cleanup code under `LABEL` releases the same expression again without it having been nulled.

## What gets flagged

```c
free(buf);
if (rc != 0) {
    log_error("invalid header");
}
free(buf);              // FLAGGED: second release on the same path
```

```c
if (token == NULL) {
    free(name);         // FLAGGED: the cleanup label frees name again
    goto out;
}
...
out:
    free(token);
    free(name);
```

## What is not flagged

- A pointer set to `NULL` or reassigned between the two releases.
- Releases on the two arms of an `if`/`else` or in different `case`s, and a release followed by `return`.
- Releases in different functions, `#define` macros, string literals and comments.
- Project-specific wrappers (other than `xfree`) and aliases (`q = p; free(p); free(q)`), which are not tracked.

## Remediation

Release each allocation in exactly one place, and set the pointer to `NULL` right after releasing it when other code may release it later:

```c
free(name);
name = NULL;
goto out;
```

## References

- [CWE-415: Double Free](https://cwe.mitre.org/data/definitions/415.html)
- [SEI CERT MEM30-C / MEM31-C](https://wiki.sei.cmu.edu/confluence/display/c/MEM30-C.+Do+not+access+freed+memory)

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

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