7 ms·
Windows Kernel-Mode Drivers Written in Rust
- TillE 11y agoJudging from the screenshot it even works on Windows XP. This is really impressive, since Windows kernel drivers typically have to be written in plain old C.
- adamnemecek 11y agoBeing able to integrate with C efficiently is one of the goals of the language.
- steveklabnik 11y agoAs a side note, Windows XP support in Rust is partial. We don't support running the compiler on XP, but do support compiling _to_ it, but some parts of the standard library may not work. It's missing some primitives, basically. Not that that matters in the context of writing a kernel driver, mind you...
- pravic 11y agoYep, most of modern atomic primitives at WinAPI available since Vista (like SRWLock, Condition variables). But core of NT is stable since 3.1 (1993) and even there are structures which are dated 1989 (e.g. [FILE_OBJECT](https://msdn.microsoft.com/en-us/library/windows/hardware/ff545834(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/hardware/ff...).
- poizan42 11y agoThis is not at all true. Microsoft has recommended for a long time that new drivers use WDF which is a C++ library
- tobias3 11y agoDo you have a source for that? KMDF seems to be a C-API.
- drv 11y agoIt has a C API, but everything under the hood is C++. The source is actually public now: https://github.com/Microsoft/Windows-Driver-Frameworks https://github.com/Microsoft/Windows-Driver-Frameworks
- poizan42 11y agoI meant it's written in C++[0]. The interface is exported through standard C functions, but the header files also have the declarations guarded with "extern "C"". Microsoft do have some sample drivers that are written in C++, e.g. https://github.com/Microsoft/Windows-driver-samples/tree/master/video/KMDOD https://github.com/Microsoft/Windows-driver-samples/tree/mas... [0]: https://github.com/Microsoft/Windows-Driver-Frameworks/tree/master/src/framework/kmdf/src/core https://github.com/Microsoft/Windows-Driver-Frameworks/tree/...
- wyldfire 11y agoIt's just the kind of area Rust does really well in. From a certain point of view, Rust can produce executables that are pretty much "written in plain old C" in that they can provide linkage that works like C linkage, don't necessarily need heap/GC, don't necessarily need big std library presuming userspace context. I'd wager you could do something similar with D (soon if not now), since its no-std support is catching up w/Rust's.
- CyberShadow 11y agoI have a "framework" (really just a build tool and some examples) for bare-bones Win32 support for D[1]. I haven't tried writing kernel drivers with it though. I used it to create a few Fallout 4 mods[2]. Avoiding the stdlib was not necessary there, but I didn't want the bloat. One interesting application of D's metaprogramming has been automatically recursively instrumenting the DirectX COM API just from the D interface definitions[3] (not IDLs). [1]: https://github.com/CyberShadow/SlimD [2]: https://github.com/CyberShadow/csfo4 [3]: https://github.com/CyberShadow/csfo4/blob/master/dxlog/d3d11.d
- deleted 11y ago[deleted]
- pjmlp 11y agoWindows has been moving to C++ on kernel space since Vista, even newer C-like APIs are actually C++ code with extern "C" calling convention. They have stated multiple times that C++ is the way forward for systems programming on Windows.
- boduh 11y agoAny reference for this?
- pjmlp 11y agoYes, Herb Sutter's blog regarding C99. https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99/ https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-an... The new C runtime library was re-written in C++ with extern "C" for the entry points. https://blogs.msdn.microsoft.com/vcblog/2014/06/10/the-great-c-runtime-crt-refactoring/ https://blogs.msdn.microsoft.com/vcblog/2014/06/10/the-great... Vista introduced support for C++ in the kernel https://msdn.microsoft.com/en-us/library/jj620896(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/jj620896(v=vs.110)....
- justanotheratom 11y agoIt is very nice to see this effort. I have one suggestion - integrate with WDF APIs instead of the lower level and legacy WDM APIs.
- PeCaN 11y agoExcept WDF is a C++ API.
- pravic 11y agoIt is not effort. It was just a fun :)
- poizan42 11y ago"Legacy" is as much said. It still forms the basis interface that is used by WDF. Also you still need to use the WDM directly if you are writing a filesystem driver. Besides that, the WDF source is available under a MIT license, so it might make more sense to just port it to Rust.
- aidanhs 11y agoThis looks very cool, definitely something I'm tempted to play with (having never done any dev in this area before). However, the feedback loop for debugging drivers on Windows seems quite manual (https://msdn.microsoft.com/en-us/library/windows/hardware/mt269367(v=vs.85).aspx#Section4_Install https://msdn.microsoft.com/en-us/library/windows/hardware/mt...). On Linux I'd have SSH at my disposal - are there any best practices for speeding up this copy/install process? Cygwin sshd could be an option, off the top of my head?
- Sanddancer 11y agoFirst step I'd say is to import your certificate into the the debugged computer, so it doesn't warn you every time you re-upload the driver [1]. Second, use powershell remoting [2]. Pretty much all of the other steps can be established and automated with a powershell remote connection. I'm in fact surprised that microsoft hasn't updated that page to use powershell, because driver debugging is a task where it could be invaluable in helping. [1] https://blog.jourdant.me/ps-installing-drivers-from-untrusted-publishers/ https://blog.jourdant.me/ps-installing-drivers-from-untruste... [2] https://technet.microsoft.com/en-us/library/dd819505.aspx https://technet.microsoft.com/en-us/library/dd819505.aspx
- nickpsecurity 11y agoThis is a neat project but with a caveat: Microsoft has solid, verification tools for C drivers but Rust doesn't have the equivalent. The tools are especially important for finding behavioral problems at the interface between the driver itself and Windows. So, my suggestion for production by Rust developers is to simultaneously develop the driver in Rust and a C subset. Keep the two logically equivalent. Rust's checker will catch problems in C program possibly. More importantly, Microsoft's analysis tools will probably catch problems in both if it's a logical error. Should result in some pretty robust drivers.
- colejohnson66 11y agoAs someone who's never worked with the Windows kernel, I'm curious about the tools you mention. How do I get them?
- robododo 11y agoDriver Verifier is the main one I've used. It's pretty awesome. https://msdn.microsoft.com/en-us/library/windows/hardware/ff545448(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/hardware/ff...
- drv 11y agoMicrosoft has been shipping static analysis tools with the Windows DDK for a long time (originally PREfast[1], now Static Driver Verifier[2]). I believe the static analysis is even integrated with Visual Studio now. [1]: http://research.microsoft.com/en-us/news/features/prefast.aspx http://research.microsoft.com/en-us/news/features/prefast.as... [2]: https://msdn.microsoft.com/en-us/library/windows/hardware/ff552808(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/hardware/ff...
- pjmlp 11y agoThey are part of the DDK https://msdn.microsoft.com/en-us/library/windows/hardware/ff539547%28v=vs.85%29.aspx https://msdn.microsoft.com/en-us/library/windows/hardware/ff... https://msdn.microsoft.com/en-us/library/windows/hardware/hh439725%28v=vs.85%29.aspx https://msdn.microsoft.com/en-us/library/windows/hardware/hh... https://msdn.microsoft.com/en-us/library/windows/hardware/ff543998%28v=vs.85%29.aspx https://msdn.microsoft.com/en-us/library/windows/hardware/ff...