# GHSA-wrw7-89jp-8q8g: Unsoundness in `Iterator` and `DoubleEndedIterator` impls for `glib::VariantStrIter`

> An unsound pointer mutation in the Rust glib library causes null pointer dereferences and crashes when iterating over VariantStrIter.

- **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:** 30 Mar 2024
- **Also known as:** RUSTSEC-2024-0429

## What is GHSA-wrw7-89jp-8q8g?

GHSA-wrw7-89jp-8q8g is a vulnerability in glib, published on 30 Mar 2024. 

### Official description

The `VariantStrIter::impl_get` function (called internally by implementations of the `Iterator` and `DoubleEndedIterator` traits for this type) was unsound, resulting in undefined behaviour.

An immutable reference `&p` to a `*mut libc::c_char` pointer initialized to `NULL` was passed as an argument to a C function that that mutates the pointer behind `&p` in-place (i.e. as an out-argument), which was unsound. After changes in recent versions of the Rust compiler, these unsound writes through `&p` now seem to be completely disregarded when building the `glib` crate with optimizations.

This subsequently caused all calls of `VariantStrIter::impl_get` to violate the safety requirements of the `std::ffi::CStr::from_ptr` function - which requires its argument to be a valid pointer to a C-style string - resulting in crashes due to `NULL` pointer dereferences.

This was fixed by passing the out-argument pointer explitly as `&mut p` instead of `&p`.

This issue has been present since this code was initially added in `glib` v0.15.0. The mismatch in mutability was likely missed (and not raised as an error by the compiler) because the C function wrapped by `VariantStrIter::impl_get` is variadic (`glib_sys::g_variant_get_child`), and the pointer in question is one of the variadic arguments.

## Which versions are affected by GHSA-wrw7-89jp-8q8g?

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


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `glib` | crates.io | From 0.15.0 | 0.20.0 |
| `glib` | crates.io | From 0.15.0 | 0.20.0 |
| `glib` | crates.io | From 0.15.0 | 0.20.0 |

## How do I fix GHSA-wrw7-89jp-8q8g?

Upgrade glib to 0.20.0 or later.

## Is GHSA-wrw7-89jp-8q8g being exploited?

There is no known exploitation of GHSA-wrw7-89jp-8q8g in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-wrw7-89jp-8q8g and fix it if it is affected.

What it is: GHSA-wrw7-89jp-8q8g is a vulnerability in glib, published on 30 Mar 2024. 
Exploitation: There is no known exploitation of GHSA-wrw7-89jp-8q8g in the wild and no public exploit code.

Affected packages and fixed versions:
- glib (crates.io): affected from 0.15.0; fixed in 0.20.0
- glib (crates.io): affected from 0.15.0; fixed in 0.20.0
- glib (crates.io): affected from 0.15.0; fixed in 0.20.0

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-wrw7-89jp-8q8g/ (Markdown: https://insights.sbomify.com/GHSA-wrw7-89jp-8q8g/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-wrw7-89jp-8q8g

---
From sbomify Insights: https://insights.sbomify.com/GHSA-wrw7-89jp-8q8g/
Summaries are generated and may contain mistakes; the official description above is the record.
