# VNX-130: Improper Handling of Length Parameter Inconsistency

Reports C/C++ code that uses a length field decoded from a message (ntohs, ntohl, be*toh, n2s.

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

## Overview

VNX-130 maps to [CWE-130: Improper Handling of Length Parameter Inconsistency](https://cwe.mitre.org/data/definitions/130.html). It reports a value decoded from wire byte order (`ntohs`, `ntohl`, `be16toh`/`be32toh`/`le16toh`/`le32toh`, `get_unaligned_be16/be32/le16/le32`, OpenSSL's `n2s`/`n2l`, or bytes combined as `(p[0] << 8) | p[1]`) that is used as the length argument of `memcpy`, `memmove`, `strncpy` or `strncat`, or as the size of `read`/`recv`/`recvfrom`/`fread`, with no comparison against the destination or the received size in between. The conversion written directly in the length argument is reported too.

## What gets flagged

```c
uint16_t len = ntohs(h->len);
memcpy(payload, pkt + sizeof(*h), len);          // FLAGGED

n2s(p, payload);
pl = p;
memcpy(bp, pl, payload);                         // FLAGGED: Heartbleed

std::memcpy(body, frame + 4, ntohs(fh->length)); // FLAGGED
```

## What is not flagged

- A length compared in an `if`/`while`/`assert` or a ternary, or clamped with `MIN()`/`min()`, before the copy.
- Constant and `sizeof` lengths, and decoded values that are never used as a copy length.
- Host-to-network conversions (`htons`, `htonl`).
- Lengths that reach the copy through another function (out of scope).

## Remediation

Before copying, check the decoded length against both the destination size and the number of bytes actually received, and discard the message if either check fails.

## References

- [CWE-130: Improper Handling of Length Parameter Inconsistency](https://cwe.mitre.org/data/definitions/130.html)
- [CVE-2014-0160 (Heartbleed)](https://nvd.nist.gov/vuln/detail/CVE-2014-0160)

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

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