GHSA-p75v-367r-2v23 `cell-project` used incorrect variance when projecting through `&Cell<T>`

Not scored Published

A Rust library vulnerability allows memory corruption via incorrect variance handling, potentially leading to code execution.

Exploited in the wild
Not known
Login needed
Not needed
Attackable
Not stated
Victim action needed
Not needed
Fix
Available

What is GHSA-p75v-367r-2v23?

GHSA-p75v-367r-2v23 is a memory corruption vulnerability in cell-project, published on 27 Aug 2020.

Official description

Overview

The issue lies in the implementation of the cell_project macro which used field as *const _ instead of field as *mut _. The problem being that *const T is covariant in T while *mut T is invariant in T. Keep in mind that &amp;Cell&lt;T&gt; is invariant in T, so casting to *const T relaxed the variance, and lead to unsoundness, as shown in the example below.

use std::cell::Cell;
use cell_project::cell_project as cp;

struct Foo&lt;&#39;a&gt; {
    x: Option&lt;&amp;&#39;a Cell&lt;Foo&lt;&#39;a&gt;&gt;&gt;,
}

impl&lt;&#39;a&gt; Drop for Foo&lt;&#39;a&gt; {
    fn drop(&amp;mut self) {
        // `ourselves` is an &amp;Cell&lt;Self&gt;.
        // NB: `Drop` is unsound.
        if let Some(ourselves) = self.x.as_ref() {
            // replace `self` (but this doesn&#39;t actually replace `self`)
            let is_x_none = ourselves.replace(Foo {
                x: None,
            }).x.as_ref().is_none();
            // if we just moved out of `self`, and we had a `Some` originally,
            // how come this is a `None`?
            if is_x_none {
                println!(&#34;how did we get a None?&#34;);
            }
        }
    }
}

fn main() {
    let foo = Cell::new(Foo {
        x: None,
    });
    let x = cp!(Foo&lt;&#39;_&gt;, foo.x);
    x.set(Some(&amp;foo));
}

MIRI error

$ cargo +nightly miri run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `&lt;snip&gt;`
error: Undefined Behavior: not granting access to tag &lt;untagged&gt; because incompatible item is protected: [Unique for &lt;2472&gt; (call 796)]
   --&gt; $RUST_STD_PATH/src/rust/library/core/src/cell.rs:404:31
    |
404 |         mem::replace(unsafe { &amp;mut *self.value.get() }, val)
    |                               ^^^^^^^^^^^^^^^^^^^^^^ not granting access to tag &lt;untagged&gt; because incompatible item is protected: [Unique for &lt;2472&gt; (call 796)]
    |
    = help: this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental
    = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

    = note: inside `std::cell::Cell::&lt;Foo&gt;::replace` at $RUST_STD_PATH/src/rust/library/core/src/cell.rs:404:31
note: inside `&lt;Foo as std::ops::Drop&gt;::drop` at src/main.rs:14:29
   --&gt; src/main.rs:14:29
    |
14  |               let is_x_none = ourselves.replace(Foo {
    |  _____________________________^
15  | |                 x: None,
16  | |             }).x.as_ref().is_none();
    | |______________^
    = note: inside `std::ptr::drop_in_place::&lt;Foo&gt; - shim(Some(Foo))` at $RUST_STD_PATH/src/rust/library/core/src/ptr/mod.rs:486:1
    = note: inside `std::ptr::drop_in_place::&lt;std::cell::UnsafeCell&lt;Foo&gt;&gt; - shim(Some(std::cell::UnsafeCell&lt;Foo&gt;))` at $RUST_STD_PATH/src/rust/library/core/src/ptr/mod.rs:486:1
    = note: inside `std::ptr::drop_in_place::&lt;std::cell::Cell&lt;Foo&gt;&gt; - shim(Some(std::cell::Cell&lt;Foo&gt;))` at $RUST_STD_PATH/src/rust/library/core/src/ptr/mod.rs:486:1
note: inside `main` at src/main.rs:32:1
   --&gt; src/main.rs:32:1
    |
32  | }
    | ^

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to previous error

Affected Versions

All versions of the cell-project crate before 0.1.4 are affected.

Mitigation

This was fixed in Issues/4, and released as version 0.1.4. So just updating to the latest version will include the fix (which may result in a compile error on unsound usage).

Acknowledgements

This was discovered and fixed by @SoniEx2 in cell-project: Issues/3 and Issues/4

Which versions are affected by GHSA-p75v-367r-2v23?

3 open source packages ship an affected version, including cell-project.

PackageAffectedFixed in
cell-project crates.ioAll versions0.1.4
cell-project crates.ioAll versions0.1.4
cell-project crates.ioFrom 0.0.0-00.1.4

How do I fix GHSA-p75v-367r-2v23?

Upgrade cell-project to 0.1.4 or later.

Upgrade to the fixed version. Memory-safe languages, compiler hardening and fuzzing prevent most new cases. More about Memory corruption.

Is GHSA-p75v-367r-2v23 being exploited?

There is no known exploitation of GHSA-p75v-367r-2v23 in the wild and no public exploit code.

Sources

Also known as RUSTSEC-2020-0164.

Is GHSA-p75v-367r-2v23 in your software?

Upload an SBOM and sbomify checks it against every vulnerability here, every day.

Check your SBOMs for free