# VNX-269: Improper Privilege Management

Privileges are dropped in the wrong order: the user ID is changed to a non-root user before the group ID or the supplementary groups.

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

## Overview

VNX-269 maps to [CWE-269: Improper Privilege Management](https://cwe.mitre.org/data/definitions/269.html). It reports a group change (`setgid`, `setegid`, `setregid`, `setresgid`, `setgroups`, `initgroups`) that comes after the user ID was already dropped to a non-root user in the same function.

## Why this matters

Changing the group ID and the supplementary group list requires root. A daemon that calls `setuid(pw->pw_uid)` first has already given root up, so its following `setgid()` or `setgroups()` fails with `EPERM`. If the result is not checked, or is checked by a branch that carries on, the process runs as the intended user but still in group 0 and in every supplementary group root had, which often means read access to `/etc/shadow`-adjacent files, log directories and sockets meant only for administrators.

## What gets flagged

```c
if (setuid(pw->pw_uid) != 0) exit(1);
if (setgid(pw->pw_gid) != 0) exit(1);      /* flagged: too late */

setresuid(uid, uid, uid);
setgroups(0, NULL);                         /* flagged */
```

```cpp
if (::seteuid(uid) == -1) throw std::runtime_error("seteuid");
if (::setegid(gid) == -1) throw std::runtime_error("setegid");   // flagged
```

```python
os.setuid(pw.pw_uid)
os.setgid(pw.pw_gid)    # flagged
os.setgroups([])        # flagged
```

Not flagged: the correct order (`setgroups`/`initgroups`, then `setgid`, then `setuid`); a regain of root (`seteuid(0)`) between the two calls; `setgid(0)` after the drop, which is the check that root cannot be regained; calls in different functions; and the names in comments or string literals.

## Remediation

1. Drop in this order: clear or set the supplementary groups (`setgroups(0, NULL)` or `initgroups`), then `setgid(gid)`, then `setuid(uid)`.
2. Check every return value and stop on failure.
3. After the drop, verify it: `setuid(0)` and `setgid(0)` must now fail.

## References

- [CWE-269: Improper Privilege Management](https://cwe.mitre.org/data/definitions/269.html)
- [SEI CERT POS36-C: Observe correct revocation order while relinquishing privileges](https://wiki.sei.cmu.edu/confluence/display/c/POS36-C.+Observe+correct+revocation+order+while+relinquishing+privileges)

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

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