Skip to content

Soundness Bug in this crate because of UTF-8 validity #66

Description

@lewismosciski

Hi there!

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

PoC

use value_trait::generator::{BaseGenerator, DumpGenerator};

fn main() {
    let mut gen = DumpGenerator::new();

    // Populate internal Vec<u8> with invalid UTF-8 using only safe public API.
    gen.write(&[0xFF, 0xFF]).unwrap();

    // constructs a String via from_utf8_unchecked on attacker bytes.
    let s = gen.consume();

    // Trigger downstream safe stdlib usage that assumes String is valid UTF-8.
    let _first_char = s.chars().next();
}

Miri Output

error: Undefined Behavior: entering unreachable code
  --> /home/ccuu/.rustup/toolchains/nightly-2025-10-09-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/validations.rs:55:27
   |
55 |         let z = unsafe { *bytes.next().unwrap_unchecked() };
   |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
   |
   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
   = note: BACKTRACE:
   = note: inside `core::str::validations::next_code_point::<'_, std::slice::Iter<'_, u8>>` at /home/ccuu/.rustup/toolchains/nightly-2025-10-09-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/validations.rs:55:27: 55:58
   = note: inside `<std::str::Chars<'_> as std::iter::Iterator>::next` at /home/ccuu/.rustup/toolchains/nightly-2025-10-09-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/iter.rs:42:18: 42:49
note: inside `main`
  --> src/main.rs:18:23
   |
18 |     let _first_char = s.chars().next();
   |                       ^^^^^^^^^^^^^^^^

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

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions