|
1 | 1 | # Linux kernel modules in safe Rust
|
2 | 2 |
|
3 |
| -1. Make sure you have Rust installed, as well as [LLVM/Clang 3.9 or higher](https://github.com/rust-lang/rust-bindgen/issues/1316) and kernel headers. Install [cargo-xbuild](https://github.com/rust-osdev/cargo-xbuild) and the `rust-src` and `rustfmt` components for `rustup` component: |
| 3 | +This is a framework for writing loadable Linux kernel modules in Rust, |
| 4 | +using safe abstractions around kernel interfaces and primitives. |
| 5 | + |
| 6 | +For more information on the motivation and goals for this project, check |
| 7 | +out [our presentation at Linux Security Summit North America |
| 8 | +2019](https://lssna19.sched.com/event/RHaT). We're immediately focusing |
| 9 | +on making this project viable for out-of-tree modules, but we also see |
| 10 | +this project as a testing ground for whether in-tree components could be |
| 11 | +written in Rust. |
| 12 | + |
| 13 | +There is a simple demo module in the hello-world directory, as well as |
| 14 | +various other examples in the tests/ directory. |
| 15 | + |
| 16 | +## Design |
| 17 | + |
| 18 | +We run [bindgen](https://github.com/rust-lang/rust-bindgen) on the |
| 19 | +kernel headers to generate automatic Rust FFI bindings. bindgen is |
| 20 | +powered by [Clang](https://clang.llvm.org), so we use use the kernel's |
| 21 | +own build system to determine the appropriate CFLAGS (see |
| 22 | +`kernel-cflags-finder`) and pass them to bindgen (see `build.rs`). Then we |
| 23 | +write safe bindings to these types (see the various files inside `src/`). |
| 24 | + |
| 25 | +Each kernel module in Rust lives in a `staticlib` crate, which generates |
| 26 | +a `.a` file. We pass this object to the Linux kernel's own module build |
| 27 | +system for linking into a `.ko`. |
| 28 | + |
| 29 | +The kernel is inherently multi-threaded: kernel resources can be |
| 30 | +accessed from multiple userspace processes at once, which causes |
| 31 | +multiple threads of execution inside the kernel to handle system calls |
| 32 | +(or interrupts). Therefore, the `KernelModule` type is |
| 33 | +[`Sync`](https://doc.rust-lang.org/book/ch16-04-extensible-concurrency-sync-and-send.html), |
| 34 | +so all data shared by a kernel module must be safe to access |
| 35 | +concurrently (such as by implementing locking). |
| 36 | + |
| 37 | +## System requirements |
| 38 | + |
| 39 | +We're currently only running CI on Linux 4.15 (Ubuntu Xenial) on amd64, |
| 40 | +although we try to keep support for newer (and perhaps older) kernels |
| 41 | +working. Other architectures should work but are untested - see |
| 42 | +[#112](https://github.com/fishinabarrel/linux-kernel-module-rust/issues/112) |
| 43 | +for expected status. |
| 44 | + |
| 45 | +You'll need to have [Rust](https://www.rust-lang.org) - in particular |
| 46 | +Rust nightly, as we use [some unstable |
| 47 | +features](https://github.com/fishinabarrel/linux-kernel-module-rust/issues/41) - |
| 48 | +and [Clang](https://clang.llvm.org) installed. You need LLVM/Clang 3.9 |
| 49 | +or higher [to bind constants |
| 50 | +properly](https://github.com/rust-lang/rust-bindgen/issues/1316). If |
| 51 | +you're running kernel 5.0 or newer, [you'll need Clang |
| 52 | +9](https://github.com/fishinabarrel/linux-kernel-module-rust/issues/123) |
| 53 | +(currently the development release). You may need to set the `CLANG` |
| 54 | +environment variable appropriately, e.g., `CLANG=clang-9`. |
| 55 | + |
| 56 | +## Building hello-world |
| 57 | + |
| 58 | +1. Install clang, kernel headers, |
| 59 | +[cargo-xbuild](https://github.com/rust-osdev/cargo-xbuild), and the |
| 60 | +`rust-src` and `rustfmt` components from `rustup`: |
4 | 61 |
|
5 | 62 | ```
|
6 | 63 | apt-get install llvm clang linux-headers-"$(uname -r)" # or the equivalent for your OS
|
|
0 commit comments