diff options
| author | Fuwn <[email protected]> | 2022-01-03 03:20:12 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-01-03 03:20:12 -0800 |
| commit | 85db2b507f3f69b32811c54a89d9ac7bbbc46121 (patch) | |
| tree | 2efd66da452f8a6a2cc6c91584c925f237506ddf /crates/windows-kernel-rs/src/io.rs | |
| download | driver-85db2b507f3f69b32811c54a89d9ac7bbbc46121.tar.xz driver-85db2b507f3f69b32811c54a89d9ac7bbbc46121.zip | |
feat(driver): commit primer
Diffstat (limited to 'crates/windows-kernel-rs/src/io.rs')
| -rw-r--r-- | crates/windows-kernel-rs/src/io.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/crates/windows-kernel-rs/src/io.rs b/crates/windows-kernel-rs/src/io.rs new file mode 100644 index 0000000..7d783ce --- /dev/null +++ b/crates/windows-kernel-rs/src/io.rs @@ -0,0 +1,31 @@ +use windows_kernel_sys::{base::ANSI_STRING, ntoskrnl::DbgPrint}; + +#[macro_export] +macro_rules! print { + ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*))); +} + +#[macro_export] +macro_rules! println { + () => ($crate::print!("\n")); + ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*))); +} + +#[doc(hidden)] +pub fn _print(args: core::fmt::Arguments) { + // Format the string using the `alloc::format!` as this is guaranteed to return + // a `String` instead of a `Result` that we would have to `unwrap`. This + // ensures that this code stays panic-free. + let s = alloc::format!("{}", args); + + // Print the string. We must make sure to not pass this user-supplied string as + // the format string, as `DbgPrint` may then format any format specifiers it + // contains. This could potentially be an attack vector. + let s = ANSI_STRING { + Length: s.len() as u16, + MaximumLength: s.len() as u16, + Buffer: s.as_ptr() as _, + }; + + unsafe { DbgPrint("%Z\0".as_ptr() as _, &s) }; +} |