Microsoft’s AI Scanner Finds Three Memory Safety Flaws in Rust

| 5 min read

Three defects in Rust’s standard library show how unsafe internals can still expose memory safety risks through safe APIs. Microsoft researchers used their Hyenas scanning system to identify issues involving panic handling, string replacement and operating system interactions. Two findings affect stable Rust; the oldest had gone unnoticed for nearly 11 years.

A thread exit can disrupt stack overflow handling

The oldest flaw involves process::exit. When a child thread called the function, Rust could remove the main thread’s alternate signal stack while that thread was still using it. If the main thread then overflowed its stack, the operating system might fail to deliver the signal properly, causing a segmentation fault instead of Rust’s expected stack overflow handling.

Microsoft’s FORGE Lab said it found no reliable way to exploit the issue under the conditions it tested and judged practical exploitation unlikely. The researchers reported the defect to Rust maintainers after reproducing it and examining its security impact. Microsoft’s account of the research describes Hyenas as the lab’s AI based vulnerability scanner and research version of codename MDASH.

Safe interfaces still depend on sound internals

Rust’s ownership model and type system prevent many memory errors, but parts of its standard library must use unsafe code for operations the compiler can’t verify. Types such as Vec and String rely on that code behind safe interfaces. Those interfaces must remain sound under any valid use from safe Rust, including unusual inputs and failures.

One reported issue affected MapWindows::clone. The implementation allocated a buffer before fully initializing it, then called a user supplied clone operation to fill it. If that operation panicked, Rust’s unwinding process could run the buffer’s destructor, which might read uninitialized memory. The reported fix ensures the partially initialized buffer isn’t destroyed before initialization finishes.

A separate flaw involved str::replace and a custom implementation of the nightly Pattern trait. The function assumed the implementation would provide an ASCII byte in a context where that assumption wasn’t guaranteed. A non ASCII byte could lead the replacement operation to create invalid UTF-8, setting up later string operations to trigger undefined behavior.

Operating system assumptions affect stable Rust

The third category in Microsoft’s findings concerns Rust’s interaction with operating systems and C libraries. Alongside the process::exit behavior, researchers found a problem in CommandExt::exec involving the process wide environment variable array.

The function could modify this shared array while holding a read lock, leaving room for other threads to observe inconsistent data. In some circumstances, concurrent activity could leave an environment pointer referring to freed memory. Microsoft said the issue, like the signal stack flaw, affects stable Rust.

A proposed change was under review when Microsoft published its findings. It avoids modifying the shared environment array by calling execve directly. The researchers also said two issues they reported remained open at publication; their discussion included related reports from other researchers and fixes still under review.

Automated discovery still needs code level analysis

Hyenas helped flag some of the defects, while researchers traced their causes, reproduced them and assessed whether they could matter in practice. Microsoft’s team placed the issues in three broad groups: unsafe code that mishandles panics, code that relies on user supplied implementations behaving as expected, and code that makes incorrect assumptions about the underlying system.

The first two groups have drawn attention before. Microsoft said researchers were studying similar weaknesses with Rudra, a static analyzer, in 2021, and that Rust had addressed panic safety issues as far back as 2015. Their recurrence points to the difficulty of maintaining strict safety guarantees across complex library code.

Microsoft says the findings may also inform Microsoft Perception, its multi agent security system, and the development of MDASH, which brings automated vulnerability discovery into Microsoft Defender. The company’s Web App Pen Test Agent can use MDASH findings to guide testing, evaluate exploitability in live applications and support remediation. The practical takeaway for Rust maintainers is direct: automated discovery can surface subtle defects, but careful review of unsafe boundaries remains essential to keeping safe APIs sound.

Source: James Smith · www.csoonline.com