pigeonhands / rust-arch

rust crates for the arch linux package system
MIT License
6 stars 3 forks source link

StrcCtx deallocates a memory region that it doesn't own #2

Open Qwaz opened 4 years ago

Qwaz commented 4 years ago

https://github.com/pigeonhands/rust-arch/blob/8458c22a161cb8996659fd124de49972f8164712/alpm-rs/src/macros.rs#L18-L38

Description

StrcCtx deallocate a memory region that it doesn't own when StrcCtx is created without using StrcCtx::new. This can introduce memory safety issues such as double-free and use-after-free to client programs.

Demonstration

Crate: alpm-rs Version: 0.1.24 OS: Ubuntu 18.04.5 LTS Rust: rustc 1.45.2 (d3fb005a3 2020-07-31)

#![forbid(unsafe_code)]

use alpm_rs::macros::StrcCtx;

fn main() {
    let mut v1: Vec<i8> = vec![1, 2, 3, 0];
    let _ = StrcCtx {
        ptr: v1.as_mut_ptr(),
    };

    // use-after-free in v1
    // v1 and v2 are backed by the same buffer
    let v2: Vec<i8> = vec![4, 5, 6, 0];

    let measure1 = v2[0];
    v1[0] = 123;
    let measure2 = v2[0];

    assert_eq!(measure1, measure2);
}

Output:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `4`,
 right: `123`', src/main.rs:38:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Return Code: 101

Morganamilo commented 3 years ago

I understand this comes from a run of Rudra, but this repo seems rather unfinished and dead. There are official alpm bindings at https://github.com/archlinux/alpm.rs. This repo should probably be archived if the author is still around.