# VNX-806: Buffer Access Using Size of Source Buffer

A C/C++ copy into a destination buffer is bounded by the size of the SOURCE instead of the destination, so a long source overruns the destination.

| Field | Value |
|---|---|
| Rule ID | `VNX-806` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | C, C++ |
| CWE | [CWE-806](https://cwe.mitre.org/data/definitions/806.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-source-size` `cwe-806` `memory-safety` |
| Source | [`internal/sast/rules/vnx-806.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-806.rego) |

## Overview

VNX-806 maps to [CWE-806: Buffer Access Using Size of Source Buffer](https://cwe.mitre.org/data/definitions/806.html). It reports bounded copies whose bound is the length of what is being copied rather than the room in the destination, which makes the bound meaningless.

## What gets flagged

```c
char dst[32];
memcpy(dst, src, strlen(src));               /* FLAGGED */
strncpy(buf, input, strlen(input));          /* FLAGGED */

char out[16]; char big[256];
memcpy(out, big, sizeof(big));               /* FLAGGED: size of the source array */

size_t n = strlen(arg);
memcpy(local, arg, n);                       /* FLAGGED: two-line form */
```

Not flagged: `memcpy(dst, src, sizeof(dst))`, a copy into `malloc(strlen(s) + 1)`, a copy preceded by `if (strlen(src) >= sizeof(dst)) return -1;`, `memcpy(&x, raw, sizeof(x))`, copies between arrays of the same declared size.

```cpp
// FLAGGED: C++ string copied by its own size into a fixed field
std::memcpy(r.user, user.c_str(), user.size() + 1);
```

Not flagged: `std::vector<char> out(s.size()); std::memcpy(out.data(), s.data(), s.size());`, where the destination is sized from the source.

## Remediation

1. Bound the copy by the destination's capacity, not the source's length.
2. Check the source length against the destination size first, and fail or truncate explicitly.
3. When the destination must hold the whole source, allocate it from the source length.

## References

- [CWE-806: Buffer Access Using Size of Source Buffer](https://cwe.mitre.org/data/definitions/806.html)

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

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