在 Rust 中编写一个简单的驱动程序

  • Rust Language Ecosystem Growth: The Rust language ecosystem is constantly expanding with increasing popularity. It's the only mainstream language offering memory and concurrency safety at compile time, along with a powerful build system (cargo) and a growing number of packages (crates).
  • Daily Driver and Transition to Rust: The author's daily driver is still C++, but they are exploring Rust for system programming. They decided to write a simple WDM driver as a Rust version of the "Booster" driver in their book.
  • Getting Started with Rust Driver Development:

    • Refer to Windows Drivers-rs. Install WDK (normal or EWDK) and LLVM to access the Clang compiler.
    • Create a new Rust library project: cargo new --lib booster.
    • Add a build.rs file to tell cargo to link statically to the CRT with specific code.
    • Edit cargo.toml to add various dependencies like wdk-build, wdk, wdk-macros, etc.
  • The Code Details:

    • Remove the standard library with #[no_std].
    • Add use statements for various Rust and WDK-related modules.
    • Set the global allocator with #![global_allocator].
    • Write the DriverEntry function with proper decorations and use println! macro.
    • Create a device object and a symbolic link.
    • Handle different request types like IRP_MJ_CREATE, IRP_MJ_CLOSE, and IRP_MJ_WRITE. In IRP_MJ_WRITE, change thread priority using relevant functions.
    • Sign the driver manually using signtool sign if no INF/INX file is present.
  • Installing and Testing the Driver: Install the driver using sc.exe on a machine with test signing on. Test it with a C++ application that passes the correct structure to the driver.
  • Conclusion: Writing kernel drivers in Rust is possible, and the WDK crates are evolving. Safe wrappers should be created to improve code readability and enjoy Rust's benefits. Sample KMDF Rust drivers can be found at [https://github.com/microsoft/...], and the code for this post is at [https://github.com/zodiacon/B...]. Learn more about Rust at [https://trainsec.net].
阅读 7
0 条评论