Skip to content

Soundness Bug in stable::Deflate::new #491

Description

@lewismosciski

Hi there!

We scanned the most popular libraries on crates.io and found some memory safety bugs in this library.

PoC

use zlib_rs::Deflate;

fn main() {
    let mut d1 = Deflate::new(6, true, 15);
    drop(d1);
    let mut d2 = Deflate::new(6, false, 15);
    drop(d2);
}

Miri Output

error: Undefined Behavior: deallocation through <619> (root of the allocation) at alloc460[0x0] is forbidden
   --> /home/ccuu/Desktop/llm-detector/experiments/cache/crates_src/zlib-rs/0.6.3/zlib-rs-0.6.3/src/allocate.rs:185:14
    |
185 |     unsafe { std::alloc::dealloc(ptr.cast(), layout) };
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
    |
    = help: this indicates a potential bug in the program: it performed an invalid operation, but the Tree Borrows rules it violated are still experimental
    = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/tree-borrows.md for further information
    = help: the accessed tag <619> (root of the allocation) is foreign to the protected tag <3223807> (i.e., it is not a child)
    = help: this deallocation (acting as a foreign write access) would cause the protected tag <3223807> (currently Reserved) to become Disabled
    = help: protected tags must never be Disabled
help: the accessed tag <619> was created here
   --> src/main.rs:11:18
    |
 11 |     let mut d1 = Deflate::new(6, true, 15);
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^
help: the protected tag <3223807> was created here, in the initial state Reserved
   --> src/main.rs:12:5
    |
 12 |     drop(d1);
    |     ^^^^^^^^
    = note: BACKTRACE (of the first span):
    = note: inside `zlib_rs::allocate::zfree_rust` at /home/ccuu/Desktop/llm-detector/experiments/cache/crates_src/zlib-rs/0.6.3/zlib-rs-0.6.3/src/allocate.rs:185:14: 185:53
    = note: inside `zlib_rs::allocate::Allocator::<'_>::deallocate::<u8>` at /home/ccuu/Desktop/llm-detector/experiments/cache/crates_src/zlib-rs/0.6.3/zlib-rs-0.6.3/src/allocate.rs:390:33: 390:97
    = note: inside `zlib_rs::deflate::end` at /home/ccuu/Desktop/llm-detector/experiments/cache/crates_src/zlib-rs/0.6.3/zlib-rs-0.6.3/src/deflate.rs:731:14: 731:80
    = note: inside `<zlib_rs::Deflate as std::ops::Drop>::drop` at /home/ccuu/Desktop/llm-detector/experiments/cache/crates_src/zlib-rs/0.6.3/zlib-rs-0.6.3/src/stable.rs:347:17: 347:53
    = note: inside `std::ptr::drop_in_place::<zlib_rs::Deflate> - shim(Some(zlib_rs::Deflate))` at /home/ccuu/.rustup/toolchains/nightly-2025-10-09-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:804:1: 804:62
    = note: inside `std::mem::drop::<zlib_rs::Deflate>` at /home/ccuu/.rustup/toolchains/nightly-2025-10-09-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:962:24: 962:25
note: inside `main`
   --> src/main.rs:12:5
    |
 12 |     drop(d1);
    |     ^^^^^^^^

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

error: aborting due to 1 previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions