# CVE-2022-31146: Use After Free in Wasmtime

> A use-after-free vulnerability in Wasmtime's Cranelift code generator allows attackers to execute arbitrary code or cause denial of service via crafted WebAssembly modules.

- **Severity:** Medium 6.4 (`CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:U/C:H/I:L/A:L`)
- **Exploited in the wild:** Not known
- **Login needed:** Any signed-in user
- **Attackable:** Over the internet
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 20 Jul 2022
- **Updated:** 23 Apr 2025
- **Also known as:** GHSA-5fhj-g3p3-pq9g, RUSTSEC-2022-0100, UBUNTU-CVE-2022-31146

## What is CVE-2022-31146?

CVE-2022-31146 is a medium-severity use after free vulnerability in bytecodealliance wasmtime, published on 20 Jul 2022. Any signed-in user can exploit it over the network, with no action from the victim.

### Official description

Wasmtime is a standalone runtime for WebAssembly. There is a bug in the Wasmtime's code generator, Cranelift, where functions using reference types may be incorrectly missing metadata required for runtime garbage collection. This means that if a GC happens at runtime then the GC pass will mistakenly think these functions do not have live references to GC'd values, reclaiming them and deallocating them. The function will then subsequently continue to use the values assuming they had not been GC'd, leading later to a use-after-free. This bug was introduced in the migration to the `regalloc2` register allocator that occurred in the Wasmtime 0.37.0 release on 2022-05-20. This bug has been patched and users should upgrade to Wasmtime version 0.38.2. Mitigations for this issue can be achieved by disabling the reference types proposal by passing `false` to `wasmtime::Config::wasm_reference_types` or downgrading to Wasmtime 0.36.0 or prior.

Weakness: CWE-416 (Use After Free)

## Which versions are affected by CVE-2022-31146?

5 open source packages ship an affected version, including wasmtime and cranelift-codegen. It affects 2 products.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `wasmtime` | crates.io | From 0.0.0-0 | 0.38.2 |
| `wasmtime` | crates.io | From 0.37.0 | 0.38.2 |
| `wasmtime` | crates.io | From 0.37.0 | 0.38.2 |
| `cranelift-codegen` | crates.io | From 0.84.0 | 0.85.2 |
| `cranelift-codegen` | crates.io | From 0.84.0 | 0.85.2 |

Products: Bytecodealliance Cranelift-Codegen, Bytecodealliance Wasmtime.

## How do I fix CVE-2022-31146?

Upgrade wasmtime to 0.38.2 or later.

## Is CVE-2022-31146 being exploited?

There is no known exploitation of CVE-2022-31146 in the wild and no public exploit code.

## Other vulnerabilities in bytecodealliance wasmtime

- [CVE-2023-26489](https://insights.sbomify.com/CVE-2023-26489/): Guest-controlled out-of-bounds read/write on x86_64 in wasmtime (critical 10.0)
- [CVE-2021-32629](https://insights.sbomify.com/CVE-2021-32629/): Memory access due to code generation flaw in Cranelift module (high 7.2)
- [CVE-2022-31169](https://insights.sbomify.com/CVE-2022-31169/): Cranelift vulnerable to miscompilation of constant values in division on AArch64 (medium 5.9)
- [CVE-2022-31104](https://insights.sbomify.com/CVE-2022-31104/): Miscompilation of `i8x16.swizzle` and `select` with v128 inputs in Wasmtime (medium 4.8)
- [CVE-2023-27477](https://insights.sbomify.com/CVE-2023-27477/): wasmtime vulnerable to miscompilation of `i8x16.select` with the same inputs on x86_64 (low 3.1)

## Fix prompt for AI coding agents

```text
Check this codebase for CVE-2022-31146 and fix it if it is affected.

What it is: CVE-2022-31146 is a medium-severity use after free vulnerability in bytecodealliance wasmtime, published on 20 Jul 2022. Any signed-in user can exploit it over the network, with no action from the victim.
Exploitation: There is no known exploitation of CVE-2022-31146 in the wild and no public exploit code.

Affected packages and fixed versions:
- wasmtime (crates.io): affected from 0.0.0-0; fixed in 0.38.2
- wasmtime (crates.io): affected from 0.37.0; fixed in 0.38.2
- wasmtime (crates.io): affected from 0.37.0; fixed in 0.38.2
- cranelift-codegen (crates.io): affected from 0.84.0; fixed in 0.85.2
- cranelift-codegen (crates.io): affected from 0.84.0; fixed in 0.85.2

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/CVE-2022-31146/ (Markdown: https://insights.sbomify.com/CVE-2022-31146/index.md)
```

## Sources

- CVE record: https://www.cve.org/CVERecord?id=CVE-2022-31146
- NVD: https://nvd.nist.gov/vuln/detail/CVE-2022-31146
- OSV: https://osv.dev/vulnerability/CVE-2022-31146

---
From sbomify Insights: https://insights.sbomify.com/CVE-2022-31146/
Summaries are generated and may contain mistakes; the official description above is the record.
