kernel-roulette: play Russian Roulette with the Linux kernel
kernel-roulette is a simple Linux kernel module written in Rust and C that implements a character device driver for a virtual device.
When this virtual device is read, there is a chance that the system will crash with a kernel panic *.
More importantly, it demonstrates how to build a kernel module with Rust. The rust code uses
#![no_std] to disable the standard library, but we can still use the following:
alloccrate, which includes data structures such as
Vecthat use dynamic memory allocation.
println!()to print to the
*: It actually uses the
BUG() macro to show a stack trace in
dmesg, which doesn't crash the system but leaves it in a somewhat inconsistent state.
If you really want it to panic replace
Linux kernel headers and
Nightly Rust (install from https://rustup.rs)
Download the Rust library source with
rustup component add rust-src
cargo install xargo
makefrom the project directory
sudo insmod build/roulette.ko
dmesg | tail -n 10and verify that the kernel module was loaded. You should see something like:
[ 35.735871] IPv6: ADDRCONF(NETDEV_UP): enp4s0: link is not ready [ 39.123353] r8169 0000:04:00.0 enp4s0: link up [ 39.123364] IPv6: ADDRCONF(NETDEV_CHANGE): enp4s0: link becomes ready [ 792.965067] roulette: loading out-of-tree module taints kernel. [ 792.965070] roulette: module license 'unspecified' taints kernel. [ 792.965070] Disabling lock debugging due to kernel taint [ 792.965236] roulette: module verification failed: signature and/or required key missing - tainting kernel [ 792.966321] Registered kernel-roulette with major device number 243 [ 792.966322] Run /bin/mknod /dev/kernel-roulette c 243 0 [ 793.477624] Panic probability: 10/100
dmesg: in this case you would run
sudo /bin/mknod /dev/kernel-roulette c 243 0
sudo rmmod build/roulette.ko