# VNX-843: Access of Resource Using Incompatible Type ('Type Confusion')

A generic socket address is cast to a family-specific structure without checking its family.

| Field | Value |
|---|---|
| Rule ID | `VNX-843` |
| Severity | High |
| SARIF level | `error` |
| Kind | `sast` |
| Languages | C, C++ |
| CWE | [CWE-843](https://cwe.mitre.org/data/definitions/843.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 | `type-confusion` `cwe-843` `downcast` `sockaddr` |
| Source | [`internal/sast/rules/vnx-843.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-843.rego) |

## Overview

VNX-843 maps to [CWE-843: Access of Resource Using Incompatible Type ('Type Confusion')](https://cwe.mitre.org/data/definitions/843.html). It reports two shapes in C and C++:

1. **Socket address read as the wrong family.** A `struct sockaddr *` or `sockaddr_storage` cast (C cast or `reinterpret_cast`) to `sockaddr_in`, `sockaddr_in6`, `sockaddr_un`, `sockaddr_ll` or `sockaddr_nl` when nothing in the 20 lines above within the same function establishes the family (`sa_family`, `ss_family`, `ai_family`, an `AF_*` constant) and the cast is not the start of filling the structure (its `*_family` assigned within the next 4 lines).
2. **Unchecked C++ downcast.** `static_cast<Derived *>`/`<Derived &>` or a C-style `(Derived *)` cast of a variable whose declared type, in the 40 lines above, is a base class of `Derived` according to the project's class declarations (one or two levels), when no condition in the 8 lines above names the variable and no `dynamic_cast`/`typeid` tests it.

Not reported: upcasts, casts inside a tagged dispatch (an if/switch on a type, kind or tag field in the 8 lines above), casts from `void *`, CRTP `static_cast<Impl *>(this)`, numeric casts, `reinterpret_cast` to byte pointers, checked downcasts, unions and tagged-variant misuse, comments and string literals.

## Why this matters

A `static_cast` to the wrong derived class is undefined behaviour and the classic type-confusion exploit primitive: fields of one subclass are read and written through the layout of another. An IPv6 peer read as `sockaddr_in`, or a Unix socket path read as an IP address, reads memory through the wrong structure.

## What gets flagged

```cpp
double circle_area(Shape *s)
{
    Circle *c = static_cast<Circle *>(s);                     // FLAGGED
    return 3.14159 * c->radius * c->radius;
}

auto *in = reinterpret_cast<const sockaddr_in *>(addr);       // FLAGGED
```

```c
void log_peer(const struct sockaddr *sa)
{
    const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;   // FLAGGED
}
```

Not flagged: a cast inside `switch (sa->sa_family) { case AF_INET: ... }`, `if (s->kind() == 1) { ... static_cast<Circle *>(s) ... }`, `dynamic_cast<Circle *>(s)`, `static_cast<Base *>(d)`.

## Remediation

1. Check `sa_family`/`ss_family` before casting a socket address and handle each family separately.
2. Use `dynamic_cast` and test the result, or check the object's kind first, before downcasting.
3. Prefer `std::variant` and `std::visit` to hand-rolled tagged hierarchies.

## References

- [CWE-843: Access of Resource Using Incompatible Type ('Type Confusion')](https://cwe.mitre.org/data/definitions/843.html)
- [SEI CERT EXP39-C: Do not access a variable through a pointer of an incompatible type](https://wiki.sei.cmu.edu/confluence/display/c/EXP39-C.+Do+not+access+a+variable+through+a+pointer+of+an+incompatible+type)

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

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