Why Rust is Safe
Ben Williamson
BSides Canberra VIII (2025) · Day 2 · Main Track
Overview
In a compelling presentation at BSides Canberra, Ben Williamson tackled one of the most persistent and costly challenges in software security: memory safety issues. Titled "Why Rust is Safe," Williamson's talk illuminated how the Rust programming language offers a radical departure from traditional systems languages like C and C++, providing robust memory and thread safety guarantees without sacrificing performance or low-level control. He underscored the critical importance of this innovation by citing reports from Google, Microsoft, and Mozilla, which indicate that over 70% of severe security vulnerabilities in memory-unsafe codebases stem directly from memory unsafety.

Key moments
- 0:00 Introduction and memory safety problem
- 4:00 Rust's core pitch: Fast, control, and safe
- 4:40 Ownership system: Move, Borrow, Share, Regions
- 14:00 Move semantics: Preventing use-after-close bugs
- 32:00 Regions: Safely returning references from functions
- 44:00 Mutable XOR Shared: Rust's core safety principle
- 45:30 Automatic thread safety through ownership
- 52:20 Q&A: Rust vs. Swift comparison
Why Rust is Safe
Speakers: Ben Williamson
Conference: BSides Canberra
YouTube: https://www.youtube.com/watch?v=aLviWMC_U3A
Overview
In a compelling presentation at BSides Canberra, Ben Williamson tackled one of the most persistent and costly challenges in software security: memory safety issues. Titled "Why Rust is Safe," Williamson's talk illuminated how the Rust programming language offers a radical departure from traditional systems languages like C and C++, providing robust memory and thread safety guarantees without sacrificing performance or low-level control. He underscored the critical importance of this innovation by citing reports from Google, Microsoft, and Mozilla, which indicate that over 70% of severe security vulnerabilities in memory-unsafe codebases stem directly from memory unsafety.
Williamson, drawing on two decades of experience in C/C++ for embedded systems and safety-critical applications, shared his journey from assuming memory corruption was an unavoidable "pain" in systems programming to becoming a passionate advocate for Rust. His talk, inspired by the work of Rust standard library contributor Aria Desires and her "Rustonomicon" document, deconstructs Rust's core safety mechanisms through an intuitive thought experiment rather than complex syntax. The presentation serves as a foundational guide for developers seeking to understand how Rust achieves its unique blend of speed, control, and unparalleled safety.
The talk makes a strong case that Rust is not just another programming language but a paradigm shift, offering a practical solution to eradicate an entire class of security bugs at compile time. By systematically addressing the root causes of memory errors, Rust promises a future where software is inherently more secure and robust, reducing the burden on developers to manually manage complex memory contracts and preventing costly runtime failures.
Background
▶ Watch: Introduction and memory safety problem (0:00)
For decades, systems level programming, which includes operating system kernels, device drivers, embedded systems, and high-performance applications, has been dominated by C and C++. These languages offer unparalleled control over hardware and memory, essential for performance and direct interaction with system resources. However, this power comes at a significant cost: manual memory management. Developers are solely responsible for allocating, deallocating, and accessing memory correctly, a task fraught with peril.
The consequences of memory mismanagement are severe, leading to a host of critical security vulnerabilities. Common examples include buffer overflows, where data writes exceed allocated buffer boundaries; use after free errors, where memory is accessed after it has been deallocated; and data races, which occur when multiple threads access the same memory location concurrently without proper synchronization, and at least one of the accesses is a write. These bugs are notoriously difficult to debug, often manifesting as unpredictable crashes or subtle security exploits.
While higher-level languages like JavaScript or Java offer memory safety through garbage collectors and runtimes, these mechanisms introduce overheads that are unacceptable for performance-critical or resource-constrained systems. For a long time, the prevailing assumption was that any language providing safe pointers would inevitably incur such runtime penalties, making them unsuitable for systems programming. Ben Williamson himself, with 20 years of experience in embedded Linux, browser security, and even autonomous and safety-critical systems at Apple (including work on the iCloud keychain sync protocol and a custom IPSec implementation), shared this very assumption. His experience involved countless hours debugging memory corruption issues, which he once considered an inherent part of the job. Rust emerged to challenge this long-held belief, demonstrating that it's possible to achieve both low-level control and compile-time memory safety without a garbage collector or a separate runtime, fundamentally changing the landscape of systems programming.
Key Findings
▶ Watch: Ownership system: Move, Borrow, Share, Regions (4:40)
The central revelation of Ben Williamson's talk is that Rust achieves complete memory and thread safety at compile time, without any runtime overhead or garbage collection, while maintaining the performance and control of C or C++. This seemingly impossible feat is accomplished through Rust's innovative ownership system, a set of compile-time rules that govern how memory is managed and accessed.
The ownership system is built upon four foundational concepts:
- Move semantics: Types representing resources (like file handles or heap allocations) cannot be implicitly copied. Instead, they are moved, ensuring there's always a single, clear owner for a resource, preventing use after invalidation bugs.
- Borrowing: Rust allows temporary, exclusive access to data through borrowed references (safe pointers). These references ensure that while a piece of data is being modified, no other part of the program can access or modify it, preventing concurrent modification issues.
- Sharing: For read-only access, Rust permits multiple shared references to data. The crucial rule here is that if data is shared, it cannot be mutated by any reference, guaranteeing consistency for all readers.
- Regions: This concept extends the lifetime of borrowed and shared references, allowing functions to safely return pointers into objects. The compiler tracks the "region" of code where such a reference is valid, ensuring the underlying data remains valid for the reference's entire lifetime.
These principles combine to enforce what Williamson calls the "mutable XOR shared" rule: at any given time, data can either be exclusively mutable (one writer) or immutably shared (many readers), but never both. This compile-time enforcement of the reader writer pattern not only guarantees memory safety but also inherently provides thread safety, eliminating entire classes of concurrency bugs like data races that plague multi-threaded C/C++ applications. Rust's approach means that if a program compiles, it is guaranteed to be free of these memory and concurrency errors in its safe Rust portions.
Technical Deep Dive
▶ Watch: Regions: Safely returning references from functions (32:00)
Rust's core innovation lies in its ability to enforce memory safety at compile time, without a garbage collector or runtime. Williamson meticulously explains this through a thought experiment, starting from a simplified C and progressively introducing Rust's concepts.
He begins by asking, "What is the root of all evil in C?" The answer: pointers. Pointers offer immense power but demand extreme responsibility, leading to bugs like use after free and memory corruption. In C, function declarations for pointers offer no semantic information about how the pointer will be used (read-only, mutable, owned, freed, stored, accessed by other threads). The burden falls entirely on the developer to read comments and adhere to API contracts.
Williamson's thought experiment starts by removing all pointers and global variables from C, leaving only local variables, scalars, structs, and fixed-length arrays with bounds checks. This results in a memory-safe but practically useless language, incapable of dynamic data structures. Even without pointers, resource types like file descriptors (open file handles) can lead to use after invalidation bugs. If a File struct containing a file descriptor is passed by value to a close function, an implicit copy occurs. The original File variable remains valid in the calling scope, allowing subsequent writes to a closed file.
To solve this, Williamson introduces the conceptual move keyword. When applied to a type (e.g., File), instances of that type cannot be copied; they can only be moved. When a File is passed to close, it's moved, consumed by the function, and the original variable is invalidated. Any attempt to use it afterward results in a compile-time error. This ensures a single owner for the resource.
However, this move behavior is problematic for operations like write, which should access the file temporarily without consuming it. This leads to the introduction of the borrow keyword, representing a temporary, exclusive access to an object. A function can borrow a File, mutate it, and then "give it back" upon return. Conceptually, the object is moved into the function and then moved back, but practically, this is implemented as pass by reference (a pointer).
The rules for borrow are critical:
- Borrows are exclusive: Only one borrow can exist on an object at a time. Attempting to borrow the same object twice, or an already borrowed object, results in a compile-time error.
- Borrows must be given back: A borrowed object cannot be owned or moved away by the borrower. It must be returned to the caller.
- Borrows are for function inputs only (initial rule): The borrow begins on function call and ends on function return. This prevents dangling references or dangling pointers to data that might go out of scope.
This initial borrow concept is limited. For example, a dynamic array (Array) needs to return pointers to its internal elements. If a get function returns a borrowed reference to an item, the reference would become invalid as soon as get returns. To address this, Williamson introduces regions. A region extends the lifetime of the borrow beyond the function's return, as long as the returned reference is in use. The compiler tracks this region, marking the original Array as borrowed for the duration. If, within this region, an attempt is made to destroy the Array, the compiler issues an error because the Array is still borrowed. This mechanism prevents use after free bugs with heap allocations.
A crucial example illustrates this: if an Array is at capacity and a push operation triggers a reallocation, the internal data might move to a new heap location. If a previously obtained borrowed reference (x) points to the old location, dereferencing x after reallocation would be a use after free bug. Rust's ownership system prevents this: while x is active (its region persists), the Array is considered borrowed. Therefore, calling push (which requires exclusive access to potentially mutate the Array and its internal pointer) while x is active will result in a compile-time error: "Array is already borrowed." This mechanism prevents iterator invalidation bugs common in C++.
However, exclusive borrowing prevents accessing multiple elements concurrently, even for read-only purposes. This leads to the concept of sharing. A share keyword is introduced, allowing multiple read-only references to an object. When an object is shared, it is "locked in a readonly state," and no one can mutate it. This is the "mutable XOR shared" principle in action: you can have either one mutable (exclusive) reference OR many immutable (shared) references, but never both simultaneously. The compiler enforces this. If an Array is shared (e.g., to get two read-only references x and y), any attempt to borrow it for a push operation (which requires mutable, exclusive access) will result in a compile-time error: "Array is already shared."
This entire system, built on move semantics, borrowing, sharing, and regions, operates at compile time. It generates the same efficient machine code as raw pointers but with robust safety guarantees.
Finally, Williamson connects these single-threaded safety guarantees to thread safety. The "mutable XOR shared" principle directly maps to the definition of a data race: two threads accessing the same location, at least one mutating, with insufficient synchronization. Since Rust makes mutation and sharing impossible simultaneously without explicit mechanisms, it inherently prevents data races. For intentional shared mutation, Rust provides library types like mutexes. A mutex wraps data, allowing it to be shared between threads via shared references. The lock method on a mutex returns a guard object, which, when held, provides a borrowed (exclusive) reference to the inner data. This ensures that only one thread can access and mutate the data at a time, enforced at runtime but integrated safely into Rust's ownership system. When the guard object goes out of scope (its region ends), the lock is released.
The speaker emphasizes that Safe Rust is the default, and to perform operations like direct memory manipulation or FFI calls to C, developers must explicitly enter Unsafe Rust using the unsafe keyword. Best practice dictates wrapping unsafe code within a module that provides a safe API, effectively extending the capabilities of the safe language without exposing raw pointers directly to client code. This clear boundary simplifies auditing of security-critical code.
Demo / Proof of Concept
▶ Watch: Mutable XOR Shared: Rust's core safety principle (44:00)
This talk does not feature a live coding demonstration or a traditional software proof of concept. Instead, Ben Williamson's presentation itself serves as an elaborate conceptual demonstration. He employs a thought experiment to incrementally build up Rust's core safety mechanisms, starting from a simplified C-like language and progressively adding features like move semantics, borrowing, sharing, and regions.
Through this pedagogical approach, Williamson "demonstrates" how each concept addresses specific memory safety challenges, such as use after invalidation with file descriptors, use after free with heap allocations, and data races in concurrent programming. The "proof of concept" is the logical consistency and power of these rules when combined, showing how the compiler can enforce them at compile time to eliminate entire classes of bugs without runtime overhead. The visual progression of code snippets on slides, highlighting compile-time errors for unsafe operations, effectively illustrates the principles without requiring actual Rust syntax.
Defensive Implications
▶ Watch: Q&A: Rust vs. Swift comparison (52:20)
Rust's ownership system offers profound defensive implications for software security. By shifting the burden of memory safety from runtime vigilance to compile-time enforcement, it fundamentally alters the landscape of vulnerability management.
- Elimination of Entire Bug Classes: The most significant impact is the eradication of common memory safety issues like buffer overflows, use after free, and data races in Safe Rust. This means that security teams no longer need to spend extensive resources on auditing code for these specific vulnerabilities, as the compiler inherently prevents them. This allows defenders to focus on higher-level logic bugs and architectural flaws.
- Reduced Attack Surface: Since memory corruption is largely prevented, attackers lose a primary vector for exploiting software. Techniques like heap spraying, stack smashing, and various forms of use-after-free exploits become significantly harder, if not impossible, to achieve against Rust programs.
- Compile-Time Guarantees: Instead of discovering critical bugs during testing, deployment, or worse, via an exploit, Rust catches them at the earliest possible stage: compilation. This drastically reduces the cost of fixing vulnerabilities and improves overall software quality and reliability from the outset.
- Clear Unsafe Boundaries: Rust's explicit
unsafekeyword creates a well-defined boundary around code that interacts with raw pointers or foreign functions. This makes security audits more targeted and effective, as auditors can focus their attention on these specific, carefully isolated blocks of code, ensuring that the Safe API they expose maintains Rust's guarantees. - Inherent Thread Safety: The "mutable XOR shared" principle directly translates into thread safety, preventing data races at compile time. This is a massive defensive advantage in multi-threaded applications, where concurrency bugs are notoriously difficult to reproduce, diagnose, and fix. Rust's standard library concurrency primitives (e.g., mutexes, channels) are built upon these ownership rules, providing safe abstractions for concurrent programming.
- Improved Code Robustness: The rigorous checks imposed by the borrow checker lead to more robust and predictable software. Developers are forced to design their memory access patterns carefully, leading to clearer code and fewer unexpected behaviors, which themselves can mask security vulnerabilities.
In essence, Rust enables a proactive security posture, embedding safety into the language's core rather than relying on reactive measures or developer discipline.
Key Takeaways
- Rust eliminates over 70% of severe security bugs: By preventing memory unsafety issues like buffer overflows, use after free, and data races at compile time, Rust drastically reduces a major source of critical vulnerabilities.
- Ownership system is key: Rust achieves safety through its unique ownership system, comprising move semantics, borrowing, sharing, and regions, which govern how data is accessed and managed without runtime overhead.
- "Mutable XOR Shared" principle: This fundamental rule dictates that data can either have one exclusive mutable reference OR many shared immutable references, but never both simultaneously, ensuring both memory and thread safety.
- Compile-time enforcement, zero runtime cost: All safety guarantees are enforced by the compiler (the borrow checker), meaning Rust programs run as fast as C/C++ with no performance penalty from garbage collection or a runtime.
- Unsafe code is isolated: The
unsafekeyword clearly delineates code that bypasses Rust's safety checks, allowing for necessary low-level operations or FFI while providing a clear boundary for security auditing and ensuring that safe APIs are exposed to client code. - A new paradigm for systems programming: Rust offers a practical solution to develop high-performance, low-level systems software with unprecedented levels of safety and robustness, shifting the debugging of memory errors from runtime to compile time.
About the Speaker(s)
Ben Williamson is an experienced software engineer with a distinguished career spanning over two decades, primarily working with C and C++. He specializes in embedded systems, having worked on handheld devices, embedded Linux, and browser security. His extensive background includes a significant nine-year tenure at Apple, where he contributed to autonomous and safety-critical systems, and played a key role in developing the crypto protocols for the iCloud keychain sync protocol and a custom IPSec implementation for iCloud hosts.
Williamson's journey led him from the traditional challenges of memory management in C/C++ to becoming a staunch advocate for Rust. He has practical experience using Rust in production at scale for performance-critical and security-critical applications. Beyond his development work, Ben is passionate about educating others on Rust, having taught hundreds of engineers and assisted teams in adopting the language. He is also the creator of the "Rust Curious" YouTube channel, where he aims to provide high-quality, accessible video learning materials for Rust, and offers in-person training workshops.