Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Qwaz opened this issue Aug 20, 2020 · 1 comment
Open

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

Qwaz opened this issue Aug 20, 2020 · 1 comment

Comments

@Qwaz
Copy link

Qwaz commented Aug 20, 2020

// createates a raw c-string
// and deallocates it in the deconstructor
pub struct StrcCtx{
pub ptr: *mut c_char,
}
impl StrcCtx {
pub fn new(s: &str) -> StrcCtx{
StrcCtx{
ptr: strc_noctx!(s),
}
}
}
impl Drop for StrcCtx{
fn drop(&mut self) {
unsafe{
let _ = str_fromraw!(self.ptr);
}
}
}

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants