Getting started with Rust in the Linux kernel
Rusty Nail

© Photo by Andrey Tikhonovskiy on Unsplash
A major step for the Linux kernel's long-term security and reliability is the introduction of Rust as a potential development language. We'll show you how to set up your Linux system to support Rust programming.
Rust's entrance into the Linux kernel is a game-changer for security and reliability. Why Rust in the Kernel? Nearly 60 to 70 percent of security vulnerabilities in kernels and low-level C/C++ code are due to memory unsafety [1], a category that includes issues like buffer overflow, use-after-free, and null-pointer dereference. These bugs are especially critical in kernel space, where a simple memory error in a device driver can crash the entire system or lead to privilege escalation. In contrast, Rust's strong memory safety guarantees eliminate these classes of bugs by design. Rust's compiler enforces strict borrowing rules, preventing unauthorized memory access and data races at compile time. These precautions mean that new kernel components written in Rust are far less likely to introduce memory-corruption vulnerabilities.
Beyond safety, Rust brings other advantages. Its rich type system and emphasis on correctness catch many errors early (e.g., improper null handling or integer overflow can be avoided with options for checked arithmetic types). Performance is comparable to C, since Rust has no garbage collector and generates optimized native code. In fact, Google's Android team noted that Rust can reduce bugs in privileged kernel code "while preserving performance characteristics" [2]. Rust also enables modern abstractions, for instance, using Resource Acquisition Is Initialization (RAII) to ensure resources (like locks or memory) are freed reliably via Rust's Drop trait. Overall, integrating Rust into Linux is about making drivers safer and more robust without sacrificing speed or low-level control.
Preparing for Rust Kernel Development
Writing a Rust-based kernel module currently requires a custom kernel build environment. Before diving into code, you'll need to set up the necessary tools and kernel source:
- Kernel source with Rust support: Download a recent Linux kernel source (6.1 or later). For example, you can use
wget
to fetch the latest stable kernel tarball from kernel.org or usegit
to clone Linus's repository. Make sure the version is 6.1 or later so that it includes Rust support in Kconfig. - Compiler toolchain (LLVM/Clang and Rust): The kernel's Rust support relies on LLVM. Building the kernel with Rust requires Clang/LLVM (GCC support for Rust is not mature yet). Install the latest LLVM/Clang and libclang.
On Ubuntu, you can use the official LLVM Apt repository for up-to-date versions:
sudo apt install -y build-essential clang lld libclang-dev
Next, install Rust. It's recommended to use the Rust toolchain installer rustup
to get the correct version (Figure 1):
# Install rustup (if not already installed) curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

You'll need to clone the Linux kernel project (Figure 2). The kernel might depend on unstable Rust features, so a specific nightly or stable version is required. The kernel source provides a script to indicate the minimum supported Rust compiler version. Navigate to the kernel source directory and run the script (Figure 3):

cd linux rustup override set $(scripts/min-tool-version.sh rustc)

Then add the Rust source component (Figure 4 – for building core/alloc crates):
rustup component add rust-src> cargo install --locked --version $(scripts/min-tool-version.sh bindgen) bindgen

The next step is to verify Rust toolchain availability: The kernel provides a Make target to check that everything is in place. Run the following inside the kernel source tree (Figure 5):
$ make LLVM=1 rustavailable

If all requirements are met, you should see a confirmation stating that Rust is available. If something is missing or version-mismatched, the output will explain what's wrong (e.g., an outdated rustc
or missing component).
At this point, I have the kernel source and the necessary compilers. The next step is to configure and build the kernel with Rust support enabled. Open the kernel configuration menu:
$ make LLVM=1 menuconfig
In General setup, find and enable Rust support (CONFIG_RUST). (This option only appears if the rustavailable
check passed and the toolchain is detected.) Enabling this will unveil further Rust-related options. For example, under Kernel hacking | Sample kernel code | Rust samples, you can choose to build sample Rust modules (small examples in the kernel tree – useful for testing). In this case, just ensure CONFIG_RUST=y
.
Compile the kernel with LLVM:
$ make LLVM=1 -j$(nproc)
Using LLVM=1
tells the build system to use Clang/LLVM for everything (including Rust compilation). This build will produce the kernel image and modules. It will also generate Rust-specific metadata in the build directory that is needed for compiling Rust modules (such as bindings and .rs
files under rust/
in the build output). Building a kernel can take some time. If you only plan to build an external module and not boot this kernel, you could skip installing it. However, to test the Rust driver properly, it's ideal to boot into the new kernel, because a stock kernel won't know how to load Rust modules.
Conclusion
Integrating Rust into the Linux kernel opens a new chapter in system programming. You can use Rust to write a simple, memory-safe kernel driver without sacrificing performance.
Infos
- Grauer, Yael. Future of Memory Safety: Challenges and Recommendations. Consumer Reports. January 2023: https://innovation.consumerreports.org/Memory-Safety-Convening-Report-.pdf
- "Rust in the Linux Kernel" by Wedson Almeida Filho, Google Security Blog, April 14, 2021:https://security.googleblog.com/2021/04/rust-in-linux-kernel.html
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

News
-
FreeBSD Promises a Full Desktop Installer
FreeBSD has lacked an option to include a full desktop environment during installation.
-
Linux Hits an Important Milestone
If you pay attention to the news in the Linux-sphere, you've probably heard that the open source operating system recently crashed through a ceiling no one thought possible.
-
Plasma Bigscreen Returns
A developer discovered that the Plasma Bigscreen feature had been sitting untouched, so he decided to do something about it.
-
CachyOS Now Lets Users Choose Their Shell
Imagine getting the opportunity to select which shell you want during the installation of your favorite Linux distribution. That's now a thing.
-
Wayland 1.24 Released with Fixes and New Features
Wayland continues to move forward, while X11 slowly vanishes into the shadows, and the latest release includes plenty of improvements.
-
Bugs Found in sudo
Two critical flaws allow users to gain access to root privileges.
-
Fedora Continues 32-Bit Support
In a move that should come as a relief to some portions of the Linux community, Fedora will continue supporting 32-bit architecture.
-
Linux Kernel 6.17 Drops bcachefs
After a clash over some late fixes and disagreements between bcachefs's lead developer and Linus Torvalds, bachefs is out.
-
ONLYOFFICE v9 Embraces AI
Like nearly all office suites on the market (except LibreOffice), ONLYOFFICE has decided to go the AI route.
-
Two Local Privilege Escalation Flaws Discovered in Linux
Qualys researchers have discovered two local privilege escalation vulnerabilities that allow hackers to gain root privileges on major Linux distributions.