← Sun, Nov 15 digest

Memory Safety Bugs in Rust Standard Library

read article ↗ hn thread ↗

Microsoft researchers discovered soundness bugs in Rust's standard library where safe code triggers undefined behavior via panic safety, user trait flaws, and OS state assumptions.

read it The deep dive into standard library soundness bugs, concrete exploit patterns, and system-boundary flaws makes the full piece highly rewarding for systems programmers.

summary

Microsoft's FORGE Lab used an AI-powered vulnerability scanner to uncover memory safety soundness bugs in Rust's core and standard libraries. The findings highlight three systemic pitfalls: unsafe code that breaks invariants before panicking, assuming user-provided trait implementations are correct, and relying on process-wide operating system state like signal stacks and environment variables. While many issues were caught in nightly builds or patched, system-boundary bugs have lurked in stable releases for years. These discoveries demonstrate that writing sound unsafe code remains difficult even for foundational libraries.

key points

  1. Panic safety bugs occur when an unsafe block breaks an invariant, arms a destructor, and panics during an early exit like T::clone, causing uninitialized memory access.
  2. Trusting user-provided trait implementations, such as a custom Pattern returning out-of-bounds or non-ASCII data, can allow safe code to corrupt UTF-8 guarantees and trigger undefined behavior.
  3. System-boundary bugs arise when Rust relies on shared process state, such as process::exit tearing down the main thread's alternate signal stack or CommandExt::exec mutating global environ under a read lock.

who should read it

Rust developers, systems engineers, and security researchers interested in language safety guarantees and vulnerability analysis.

discussion mixed

consensus

Commenters generally agree that while these soundness bugs are expected in complex software, they challenge the absolute branding of Rust's safety, sparking heavy debate over whether unwinding and panic safety are too complex to be practical.

pushback

Some argue that malicious trait implementations or user error shouldn't count against Rust's memory safety guarantees, while others point out that panic safety forces impossible cognitive overhead on developers.

best comment by codeflo ↗

The community-driven quest for "panic safety" in Rust is the same curse that befell C++ in terms of "exception safety", maybe worse because at least more C++ shops are sensible enough to disable exceptions. It's an exercise put forth by language lawyers that increases program complexity beyond comprehension, often without achieving the stated goal. It's simply not practical to think through 20 convoluted edge cases for every innocent little function, which means that no realistic program will safely recover from a panic. It's best to admit this and engineer your system accordingly.

It draws a parallel between Rust's panic safety and C++'s notoriously difficult exception safety, arguing that recovering from panics introduces unsustainable complexity.

Generated by an LLM from the article and thread. Check the sources before relying on it.