← Index berkoc · research
High published

oneringbuf (Rust): vmem-feature double-free reachable from safe Rust

The vmem storage backend of the oneringbuf crate bit-copies a heap allocation into an mmap region and then lets the source allocation drop normally, running each element destructor twice. A deterministic double-free reachable from entirely safe Rust, fixed in 0.7.1 (RUSTSEC-2026-0143).

Vendor
skilvingr/rust-oneringbuf · cargo
CWE
CWE-415 CWE-416
Disclosed
Patched
0.7.1

Background

oneringbuf is a Rust crate on crates.io that implements a single-producer single-consumer ring buffer. It offers more than one storage backend for the ring's element array. The default backend keeps the buffer on the heap as an ordinary Box<[...]>. An optional vmem feature switches the backend to a memory-mapped region, which lets the implementation wrap the read and write indices using a doubly-mapped virtual address window instead of explicit modulo arithmetic on every access. That trick is common in high-throughput ring buffers because it removes a branch from the hot path.

The crate stores each slot as an UnsafeSyncCell<T>, a small wrapper that holds a MaybeUninit<T> and implements Drop so that an occupied slot's T is destructed when the buffer itself is destroyed. The wrapper's Drop calls assume_init_drop() on the inner cell, which runs T's destructor in place. This is the correct behaviour for a ring buffer that owns its elements: when the container goes away, the elements it still holds have to be dropped exactly once.

The crate is the renamed successor of an archived predecessor crate, mutringbuf, which carries the same defect across its vmem-enabled releases. The repository for the predecessor was archived on 2025-11-20. Both crates were handled together in the same disclosure round, with the predecessor receiving its own advisory under RUSTSEC-2026-0142.

The bug

The vmem backend constructs its storage by first building an ordinary heap allocation of UnsafeSyncCell<T> slots, then copying those bytes into the freshly mapped region with ptr::copy_nonoverlapping. A byte-for-byte copy is exactly what is wanted for the raw storage layout. The problem is what happens to the source allocation afterwards.

After the copy, the source Box<[UnsafeSyncCell<T>]> falls out of scope and drops the normal way. Because UnsafeSyncCell<T> implements Drop, the per-element destructors run over the source slice as the box is freed. The destination region now holds a bitwise duplicate of those same elements, including any owned resources such as heap pointers, file descriptors, or other handles inside T. When the ring buffer is later destroyed, its own Drop walks the mmap region and runs the per-element destructors a second time over the same logical values. Each owned T is therefore dropped twice: once when the source box is freed at construction time, and once when the buffer is torn down.

ptr::copy_nonoverlapping performs a shallow bitwise copy and does not duplicate the resources that the bits refer to. After the copy there are two UnsafeSyncCell<T> values that both believe they own the same underlying resource. Running Drop on both is a classic double-free: the second destructor frees memory (or closes a handle) that the first destructor already released.

The defect lives in the vmem construction path, recorded in the advisory as the affected function oneringbuf::VmemStorage::new. It is not gated behind any unsafe block on the caller's side. A program that enables the vmem feature and constructs a ring buffer over any element type with a non-trivial Drop reaches the double-free from entirely safe Rust, which is why the advisory classifies the issue as unsound rather than as a mere logic bug. The standard taxonomy for this class is CWE-415 (double free), with CWE-416 (use-after-free) covering the window where the freed resource is touched again.

Reproducer

The trigger does not require an exotic element type. Any T whose Drop does observable work, the simplest being a type that owns a heap allocation such as String, Vec<_>, or Box<_>, reproduces the fault deterministically. The shape is:

  1. Build the crate with the vmem feature enabled.
  2. Construct a ring buffer whose element type has a non-trivial Drop.
  3. Push at least one element so a slot is occupied, or rely on the construction-time copy of initialised slots, depending on the configuration.
  4. Drop the ring buffer.

The destructor for the affected T runs twice over the same value. Under a hardened allocator or with sanitizer instrumentation this surfaces immediately as a double-free abort; without instrumentation it is heap corruption whose downstream effect depends on the allocator and the surrounding allocations. The determinism is the notable property here: there is no race and no timing dependency, the second free happens on every teardown of an affected buffer.

Impact

This is a memory-safety break reachable from safe Rust, which is the strongest form of unsoundness a crate can ship. A double-free corrupts allocator metadata and is a well-established primitive for memory-corruption exploitation when an attacker can influence heap layout and the contents of the freed object. The exact exploitability depends on the host program, the allocator in use, and the element type, so the practical severity ranges from a reliable crash to a potential corruption primitive in the worst case.

Because the fault is deterministic and reachable without any unsafe on the consumer's side, every downstream user of the vmem feature with a non-trivial element type was affected, independent of input. There is no attacker-controlled precondition beyond using the crate in a normal way. The RustSec advisory records the issue under the memory-corruption category with the unsound informational flag.

No CVE was assigned and the advisory carries no CVSS vector, so this writeup does not attach a numeric score. The "High" rating here reflects the nature of the defect, a deterministic double-free reachable from safe Rust, rather than a published CVSS figure.

The fix

The fix shipped in oneringbuf 0.7.1 through upstream pull request skilvingr/rust-oneringbuf#3. After the bytes have been copied into the mmap region, the constructor releases the source Box<[UnsafeSyncCell<T>]> without running the per-element destructors over it. In practice that means deallocating the source backing storage as raw memory rather than letting the typed box drop, so ownership of each element transfers cleanly to the mmap copy and Drop runs exactly once, at buffer teardown, as intended.

All vulnerable versions (< 0.7.1) were yanked from crates.io, so cargo add and cargo update no longer resolve to an affected release. Existing lockfiles pinned to a yanked version still resolve, but cargo audit matches them against RUSTSEC-2026-0143 and flags the memory-corruption issue. The remediation is to upgrade to oneringbuf >= 0.7.1.

Disclosure timeline

  • 2026-05-14: Upstream fix merged as skilvingr/rust-oneringbuf#3, 0.7.1 released, and all affected versions yanked from crates.io.
  • 2026-05-14: RustSec advisory submitted via rustsec/advisory-db PR #2883, which added advisories for both oneringbuf and its archived predecessor mutringbuf in the same change. The PR was merged the same day; auto-ID assignment produced RUSTSEC-2026-0143 for oneringbuf and RUSTSEC-2026-0142 for mutringbuf.
  • 2026-05-14 to 2026-05-15: Reviewer feedback on PR #2883 asked to trim two over-detailed sections (an explicit affected-paths file-line list and a predecessor-crate cross-reference) from the oneringbuf advisory. Both were removed in a follow-up commit, bringing the advisory body into the conventional length range. The technical description, trigger, and fix text were left unchanged.
  • 2026-05-15: Maintainer raised whether mutringbuf should additionally carry a separate informational = "unmaintained" advisory. The decision was to keep one full memory-corruption advisory per crate and skip a parallel unmaintained filing, since the yank already redirects new installs and the existing advisory body names oneringbuf >= 0.7.1 as the migration target.

References

  • RustSec advisory: RUSTSEC-2026-0143 (https://rustsec.org/advisories/RUSTSEC-2026-0143.html)
  • Advisory source in rustsec/advisory-db: crates/oneringbuf/RUSTSEC-2026-0143.md
  • advisory-db PR #2883 (merged): https://github.com/rustsec/advisory-db/pull/2883
  • Upstream fix: https://github.com/skilvingr/rust-oneringbuf/pull/3
  • CWE-415: Double Free
  • CWE-416: Use After Free
  • Predecessor crate advisory: RUSTSEC-2026-0142 (mutringbuf)