Getting started with Rust in the Linux kernel

Rusty Nail

© Photo by Andrey Tikhonovskiy on Unsplash

© Photo by Andrey Tikhonovskiy on Unsplash

Article from Issue 295/2025
Author(s):

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 use git 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
Figure 1: Installing rustup.

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):

Figure 2: Cloning the kernel project.
cd linux
rustup override set $(scripts/min-tool-version.sh rustc)
Figure 3: Ensuring the build will use the required Rust version.

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
Figure 4: Adding the Rust source component.

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
Figure 5: Verifying Rust toolchain availability.

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

  1. Grauer, Yael. Future of Memory Safety: Challenges and Recommendations. Consumer Reports. January 2023: https://innovation.consumerreports.org/Memory-Safety-Convening-Report-.pdf
  2. "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

The Author

Marcin Gastol is a Microsoft MVP, Microsoft Certified Trainer, and conference speaker. He works as a senior DevOps engineer and has extensive experience in Azure technologies. Visit his blog at https://marcingastol.com/.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Kernel News

    This month in Kernel News: Shared Processes with Hyper-Threading; Cleaning Up printk(); and Rust in the Kernel.

  • Kernel News

    In kernel news: Rust in Linux; and Compiler and Kernel Frenemies.

  • Embedded Rust

    Rust, a potential successor to C/C++, claims to solve some memory safety issues while maintaining high performance. We look at Rust on embedded systems, where memory safety, concurrency, and security are equally important.

  • Rust Language

    We look at a few features of Rust, Mozilla's systems programming language, and its similarity to other languages.

  • Rust

    Largely unnoticed by the public, the Mozilla Foundation is tinkering with its own programming language, Rust, which is intended to make writing reliable, fast, and concurrently running applications easier. For this purpose, the developers are borrowing generously from other languages.

comments powered by Disqus
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.

Learn More

News