CVE-2021-45695
JSON

9.8critical

Incorrect reliance on Trait memory layout in mopa

Severity
9.8critical CVSS:3.1 by NVD
Exploit likelihood
1.70% (higher than 77% of CVEs; EPSS, by FIRST)
Published
Last changed
Assigned by
mitre
Also known as
GHSA-2gxj-qrp2-53jvGHSA-8mv5-7x95-7wcfRUSTSEC-2021-0095

Description

An issue was discovered in the mopa crate through 2021-06-01 for Rust. It incorrectly relies on Trait memory layout, possibly leading to future occurrences of arbitrary code execution or ASLR bypass.

Affected

Product or packageVersionsReported by
Unspecified productn/aCVE List
mopa (crates.io)up to 0.2.2, from 0.0.0-0GitHub Advisory Database, RustSec Advisory Database
mopa_project mopaup to 0.2.2NVD

All scores

GitHub9.8critical CVSS:3.1CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H
GitHubhigh rating
NVD9.8critical CVSS:3.1CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H
NVD7.5high CVSS:2.0AV:N/AC:L/Au:N/C:P/I:P/A:P

What each source says

CVE List

An issue was discovered in the mopa crate through 2021-06-01 for Rust. It incorrectly relies on Trait memory layout, possibly leading to future occurrences of arbitrary code execution or ASLR bypass.

GitHub Advisory Database

The mopa crate redefines the deprecated TraitObject struct from core::raw. This is done to then transmute a reference to a trait object (&dyn Trait for any trait Trait) into this struct and retrieve the data field for the purpose of downcasting. This is used to implement downcast_ref_unchecked(), in terms of which downcast_ref() is also implemented. Same goes for mutable reference downcasting and Box downcasting.

The Rust compiler explicitly reserves the right to change the memory layout of &dyn Trait for any trait Trait. The worst case scenario is that it swaps data and vtable, making an executable location breach and compromisation of ASLR possible, since reads from data would read vtable instead. Likewise, arbitrary code execution is also theoretically possible if reads of vtable generated by the compiler read data instead.

GitHub Advisory Database

The mopa crate redefines the deprecated TraitObject struct from core::raw like so:

#[repr(C)]
#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct TraitObject {
    pub data: *mut (),
    pub vtable: *mut (),
}

This is done to then transmute a reference to a trait object (&dyn Trait for any trait Trait) into this struct and retrieve the data field for the purpose of downcasting. This is used to implement downcast_ref_unchecked(), in terms of which downcast_ref() is also implemented. Same goes for mutable reference downcasting and Box downcasting.

The Rust compiler explicitly reserves the right to change the memory layout of &dyn Trait for any trait Trait. The worst case scenario is that it swaps data and vtable, making an executable location breach and compromisation of ASLR possible, since reads from data would read vtable instead. Likewise, arbitrary code execution is also theoretically possible if reads of vtable generated by the compiler read data instead.

While, as of Rust 1.52, this unsound assumption still holds true, updating the compiler may silently create UB in a crate which previously compiled and run without issues, compromising the security of builds which are believed to be reproducible.

A potential strategy to resolve this has already been suggested in an issue on the GitHub repository of the crate.

NVD (es)

Se ha detectado un problema en la crate mopa versiones hasta 01-06-2021 para Rust. Se basa incorrectamente en la disposición de la memoria de Trait, conllevando posiblemente a futuras ocurrencias de ejecución de código arbitrario o omisión de ASLR

References

Sources

SourceRecordLicence
CVE ListCVE-2021-45695 changed 2024-08-04CVE Terms of Use
GitHub Advisory DatabaseGHSA-2gxj-qrp2-53jv changed 2023-11-08CC-BY-4.0
GitHub Advisory DatabaseGHSA-8mv5-7x95-7wcf changed 2023-11-08CC-BY-4.0
RustSec Advisory DatabaseRUSTSEC-2021-0095 changed 2023-11-08CC0-1.0
NVDCVE-2021-45695 changed 2026-06-17Public domain