开源软件名称(OpenSource Name):lizhuohua/linux-kernel-module-rust开源软件地址(OpenSource Url):https://github.com/lizhuohua/linux-kernel-module-rust开源编程语言(OpenSource Language):Rust 80.9%开源软件介绍(OpenSource Introduction):Writing Linux Kernel Module in RustDevice drivers on Linux-powered embedded or IoT systems execute in kernel space thus must be fully trusted. Any fault in drivers may significantly impact the whole system. However, third-party embedded hardware manufacturers usually ship their proprietary device drivers with their embedded devices. These out-of-tree device drivers are generally of poor quality because of a lack of code audit. We propose an approach that helps third-party developers to improve the reliability and safety of device drivers without modifying the kernel: Rewriting device drivers in a memory-safe programming language called Rust. Rust's rigorous language model assists the device driver developers to detect many security issues at compile time. We designed a framework to help developers to quickly build device drivers in Rust. We also utilized Rust’s security features to provide several useful infrastructures for developers so that they can easily handle kernel memory allocation and concurrency management, at the same time, some common bugs (e.g. use-after-free) can be alleviated. We demonstrate the generality of our framework by implementing a real-world device driver on Raspberry Pi 3, and our evaluation shows that device drivers generated by our framework have acceptable binary size for canonical embedded systems and the runtime overhead is negligible. More details about the design and implementation can be found in our paper: Securing the Device Drivers of Your Embedded Systems: Framework and Prototype. RequirementsToolchain
Linux Kernel HeadersA pre-built kernel (with configuration and header files) is needed.
Build
$ cargo install cargo-xbuild
$ rustup component add --toolchain=nightly rust-src
$ rustup component add rustfmt-preview
$ cd hello_world
Load and TestExamples are tested on Ubuntu 18.04 (Linux kernel 4.15.0-46-generic), hello_worldThe simplest kernel module. It just prints "hello" and "goodbye". $ sudo insmod helloworld.ko # load the module
$ sudo rmmod helloworld # remove the module
$ dmesg # dump kernel messages yes_chardevA simple character device which is similar to the $ sudo insmod yes_chardev.ko
$ cat /proc/devices # find the major number of the device 'yes', for example, 243
$ sudo mknod /dev/yes c 243 0 # make a filesystem node (replace 243 with your own major number)
$ sudo cat /dev/yes # read from the device
$ sudo rmmod yes_chardev simple_sysctlA simple sysctl device driver. $ sudo insmod simple_sysctl.ko
$ cat /proc/sys/rust/example/test # the default value should be 1
$ sudo sh -c "echo 2 > /proc/sys/rust/example/test" # change the value
$ cat /proc/sys/rust/example/test # now the value is 2
$ sudo rmmod simple_sysctl There is another way to read/write the sysctl value: $ sysctl rust.example.test # read
$ sudo sysctl -w rust.example.test=2 # write sync_exampleA simple example to illustrate the use of let mutex_data = sync::Mutex::new(50);
let mut data = mutex_data.lock();
println!("Data {} is locked by a mutex", *data);
*data = 100;
println!("Now data is {}", *data);
println!("Hello from Rust!"); The above code snippet will output like this: [ 424.328154] Mutex is locked!
[ 424.328156] Data 50 is locked by a mutex
[ 424.328158] Now data is 100
[ 424.328158] Hello from Rust!
[ 424.328160] Mutex is dropped! smsc95xxA highly simplified real-world device driver for LAN9512 USB to Ethernet controller, which is used on Raspberry Pi 3. The implementation resembles the C version. RoadmapThe efforts of writing kernel modules in Rust can be traced back to 2013 (the first commit of rust.ko), long before Rust's first stable version was released. Here we list some of the objectives that people have already achieved and what we plan to achieve in the future.
AcknowledgmentThanks to these previous works on writing Linux kernel driver in Rust. Their attempts inspire us a lot.
LicenseGPL-2.0 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论