# VNX-805: Buffer Access with Incorrect Length Value

A C/C++ call writes into a byte buffer with a length that does not come from that buffer. Reported.

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

## Overview

VNX-805 maps to [CWE-805: Buffer Access with Incorrect Length Value](https://cwe.mitre.org/data/definitions/805.html). It reports C and C++ copies and reads into a fixed byte array whose length argument is larger than the array, or comes unchecked from a packet or a string-to-number conversion.

## What gets flagged

```c
char name[16];
ssize_t n = read(fd, name, 32);              /* FLAGGED: 32 > 16 */

#define NAME_LEN 32
#define MAX_INPUT 256
char user[NAME_LEN];
recv(sock, user, MAX_INPUT, 0);              /* FLAGGED */

char small[8]; char big[128];
memcpy(small, input, sizeof(big));           /* FLAGGED: size of another buffer */

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

Not flagged: `fgets(line, sizeof(line), stdin)`, `read(fd, b, 32)` into `char b[64]`, `if (len > sizeof(payload)) return -1;` before the copy, `memcpy(buf, pkt, MIN(k, sizeof(buf)))`, `memset(vals, 0, 40)` on `int vals[10]`.

```c
/* FLAGGED: unchecked conversion as the length, inline or with arithmetic */
std::memcpy(out, src, std::atoi(argv[2]));
vlen = ntohs(t->len);
memcpy(value, t->data, vlen - 4);
```

Not flagged: `size_t n = MIN(ntohs(h->len), cap);` followed by `memcpy(dst, h->body, n)`, or a pointer parameter that shares its name with an array declared in an earlier function.

## Remediation

1. Size every copy by the destination: `sizeof(dst)` for arrays, the allocated length for pointers.
2. Compare an externally supplied length with the destination's capacity before using it.
3. Prefer bounded APIs that take the destination size (`snprintf`, `strlcpy`).

## References

- [CWE-805: Buffer Access with Incorrect Length Value](https://cwe.mitre.org/data/definitions/805.html)
- [SEI CERT ARR38-C](https://wiki.sei.cmu.edu/confluence/display/c/ARR38-C.+Guarantee+that+library+functions+do+not+form+invalid+pointers)

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

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