# GHSA-qvc4-78gw-pv8p: Adverserial use of `make_bitflags!` macro can cause undefined behavior

> Adversarial use of the make_bitflags! macro can cause undefined behavior, potentially leading to crashes or code execution in applications using the enumflags2 library.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Not stated
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 17 Apr 2023
- **Also known as:** RUSTSEC-2023-0035

## What is GHSA-qvc4-78gw-pv8p?

GHSA-qvc4-78gw-pv8p is a memory corruption vulnerability in enumflags2, published on 17 Apr 2023. 

### Official description

The macro relied on an expression of the form `Enum::Variant` always being a
variant of the enum. However, it may also be an associated integer constant, in
which case there's no guarantee that the value of said constant consists only of
bits valid for this bitflag type.

Thus, code like this could create an invalid `BitFlags<Test>`, which would cause
iterating over it to trigger undefined behavior. As the debug formatter
internally iterates over the value, it is also affected.

```rust
use enumflags2::{bitflags, make_bitflags};

#[bitflags]
#[repr(u8)]
#[derive(Copy, Clone, Debug)]
enum Test {
    A = 1,
    B = 2,
}

impl Test {
    const C: u8 = 69;
}

fn main() {
    let x = make_bitflags!(Test::{C});
    // printing or iterating over x is UB
}
```

## Which versions are affected by GHSA-qvc4-78gw-pv8p?

3 open source packages ship an affected version, including enumflags2.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `enumflags2` | crates.io | From 0.7.0 | 0.7.7 |
| `enumflags2` | crates.io | From 0.7.0 | 0.7.7 |
| `enumflags2` | crates.io | From 0.7.0 | 0.7.7 |

## How do I fix GHSA-qvc4-78gw-pv8p?

Upgrade enumflags2 to 0.7.7 or later.

## Is GHSA-qvc4-78gw-pv8p being exploited?

There is no known exploitation of GHSA-qvc4-78gw-pv8p in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-qvc4-78gw-pv8p and fix it if it is affected.

What it is: GHSA-qvc4-78gw-pv8p is a memory corruption vulnerability in enumflags2, published on 17 Apr 2023. 
Exploitation: There is no known exploitation of GHSA-qvc4-78gw-pv8p in the wild and no public exploit code.

Affected packages and fixed versions:
- enumflags2 (crates.io): affected from 0.7.0; fixed in 0.7.7
- enumflags2 (crates.io): affected from 0.7.0; fixed in 0.7.7
- enumflags2 (crates.io): affected from 0.7.0; fixed in 0.7.7

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-qvc4-78gw-pv8p/ (Markdown: https://insights.sbomify.com/GHSA-qvc4-78gw-pv8p/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-qvc4-78gw-pv8p

---
From sbomify Insights: https://insights.sbomify.com/GHSA-qvc4-78gw-pv8p/
Summaries are generated and may contain mistakes; the official description above is the record.
