# VNX-822: Untrusted Pointer Dereference

A C/C++ pointer is built from data an attacker controls, so dereferencing it reads or writes an attacker-chosen address. Reported.

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

## Overview

VNX-822 maps to [CWE-822: Untrusted Pointer Dereference](https://cwe.mitre.org/data/definitions/822.html). It reports pointers made from numbers the caller supplies: parsed text, a packet, or a user-space ioctl argument.

## What gets flagged

```c
char *p = (char *)strtoul(argv[1], NULL, 16);               /* FLAGGED */
void (*fn)(void) = (void (*)(void))strtoull(argv[2], 0, 0); /* FLAGGED */

unsigned long addr = strtoul(optarg, NULL, 0);
int *target = (int *)addr;                                  /* FLAGGED: two-line form */

if (sscanf(line, "%p", &ptr) == 1) { ... }                 /* FLAGGED */

recv(sock, buf, sizeof(buf), 0);
struct node *next = *(struct node **)(buf + 8);            /* FLAGGED */

static long demo_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
	struct demo_req *r = (struct demo_req *)arg;          /* FLAGGED: no __user / copy_from_user */
```

```cpp
auto *obj = reinterpret_cast<Widget *>(std::stoull(req.param("handle")));  // FLAGGED
```

Not flagged: `unsigned long n = strtoul(s, NULL, 10);` used as a number, `copy_from_user(&req, (void __user *)arg, sizeof(req))`, a thread argument cast back to its context struct.

```c
/* FLAGGED: address parsed with sscanf, then dereferenced */
unsigned long addr;
sscanf(cmdline, "peek %lx", &addr);
uint32_t value = *(volatile uint32_t *)addr;
```

Not flagged: `pthread_create(&t, NULL, worker, (void *)(intptr_t)id)`, the integer-in-a-pointer idiom for a thread argument.

## Remediation

1. Never turn external input into an address. Pass handles as indexes into a table the program owns, and validate the index.
2. In kernel code, copy user data with `copy_from_user`/`get_user` through a `__user` pointer.
3. Parse network data field by field into local values instead of reading pointers out of the buffer.

## References

- [CWE-822: Untrusted Pointer Dereference](https://cwe.mitre.org/data/definitions/822.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-822
// nosec vnx-822
```

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