5.3 KiB
layout | title | description | category | tags |
---|---|---|---|---|
post | Insane Allocators: segfaults in safe Rust | ...and what it means to be "safe." | rust, memory |
Having recently spent a lot of time down rabbit holes looking at how Rust uses memory, I like to think I finally understand the rules well enough to break them. See, Rust will go so far as to claim:
If all you do is write Safe Rust, you will never have to worry about type-safety or memory-safety. You will never endure a dangling pointer, a use-after-free, or any other kind of Undefined Behavior.
-- The Nomicon
...and subject to (relatively infrequent) borrow checker bugs, it's correct. There's ongoing work to formalize the rules and prove that Rust is safe, but for our purposes it's a reasonable assumption.
Until it isn't. Under specific circumstances, it's totally possible for "safe" Rust programs to encounter memory corruption and trigger a "segfault".
To prove it, this demonstration was run using an unmodified compiler:
Wait, wat?
There are two tricks needed to pull this off. First, I'm making
use of a special environment variable in Linux called
LD_PRELOAD
.
Matt Godbolt goes into way more detail
than I can cover, but the important bit is this: I can insert my own code in place of
functions typically implemented by the C standard library.
Second, there's a very special implementation of malloc
that is being picked up by LD_PRELOAD
:
use std::ffi::c_void;
use std::ptr::null_mut;
// Start off with an empty pointer
static mut ALLOC: *mut c_void = null_mut();
#[no_mangle]
pub extern "C" fn malloc(size: usize) -> *mut c_void {
unsafe {
// If we've never allocated anything, ask the operating system
// for some memory...
if ALLOC == null_mut() {
// Use a `libc` binding to avoid recursive malloc calls
ALLOC = libc::malloc(size)
}
// ...and then give that same section of memory to everyone
// for all subsequent allocations, corrupting the location.
return ALLOC;
}
// Note that we don't ever handle `free`; if the first object
// we allocate gets freed, the memory address being given
// to everyone becomes a "use-after-free" bug.
}
Because this implementation of malloc
is intentionally broken,
every program run using this library will crash. And I mean every
program; if you use dynamic memory, you're going down.
So how is it possible to even run the compiler in this environment?
Shouldn't LD_PRELOAD
cause rustc
to encounter memory corruption
and crash too? The answer is that sudo
deletes environment variables
like LD_PRELOAD
and LD_LIBRARY_PATH
when running commands.
It's technically possible to crash sudo
in the same way using
our evil malloc
implementation, but the default security policy
deletes the variables we need.
Finally, why does the program run when compiled with Rust 1.31, and not 1.32?
The answer is in the release notes:
jemalloc
is removed by default.
In all versions of Rust through 1.31, executables are statically compiled against
jemalloc by default; our dynamically loaded
evil malloc
implementation never gets an opportunity to run. It's still
possible to trigger segfaults in Rust binaries from 1.28 to 1.31 by using the
System
global allocator, but programs prior to 1.28 aren't affected by this
LD_PRELOAD
trick.
So what?
I do want to clarify: the code demonstrated here isn't a security issue, and doesn't call into question Rust's definition of "safe." The code demonstrated here crashes because the memory allocator is lying to it. And even in mission critical systems, safety concerns go way beyond allocators; the F-35 Joint Strike Fighter coding standards give memory allocation about 10 sentences total.
But this example does highlight an assumption of Rust's memory model that I haven't seen discussed much: safe Rust is safe if, and only if, the allocator it relies on is "correct". And because writing an allocator is fundamentally unsafe, Rust's promises will always rely on some amount of "unsafe" code.
That all said, know that "safe" Rust can claim to be so only because it stands on the shoulders of incredible libraries like jemalloc, kmalloc, and others. Without being able to trust the allocators, we'd have no reason to trust the safety guarantees made by Rust. So to all the people who make safe Rust possible - thanks.