Skip to content

Commit

Permalink
feat(interface): adding a kernel driver interface implementing the pr…
Browse files Browse the repository at this point in the history
…otocol
  • Loading branch information
WolverinDEV committed Nov 9, 2024
1 parent ad09635 commit a933e56
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 5 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,40 @@ jobs:
GITHUB_REF_NAME: ${{ github.ref_name || 'unknown' }}
if: ${{ env.UEFI_MAPPER_GITHUB_TOKEN != '' }}
run: ./.github/workflows/invoke-mapper.ps1

build-driver-interface:
runs-on: windows-latest
needs: [rust-check]

steps:
- name: Install toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly
override: true

- uses: actions/checkout@v4
with:
submodules: recursive

- name: Get package version
shell: bash
run: |
VERSION=$(cargo pkgid --manifest-path driver-interface/Cargo.toml | cut -d# -f2 | cut -d: -f2 | cut -d"@" -f2)
echo "Package version: $VERSION"
echo "ARTIFACT_VERSION=$VERSION" >> "$GITHUB_ENV"
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
cache-all-crates: true

- name: Build Kernel Driver interface
run: cargo b -r -v --lib --manifest-path driver/driver-kernel/Cargo.toml

- name: Upload artifact to portal
shell: bash
run: .github/workflows/artifact_upload.sh driver-interface-kernel target/release/valthrun_driver_kernel.dll target/release/valthrun_driver_kernel.pdb
if: ${{ github.event_name != 'pull_request' }}
env:
ARTIFACT_API_KEY: ${{ secrets.ARTIFACT_API_KEY }}
27 changes: 23 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ members = [

"driver",
"driver-uefi",
"driver-standalone",
"driver-standalone", "driver-interface",
]

[profile.dev]
Expand Down
19 changes: 19 additions & 0 deletions driver-interface/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "valthrun-driver-kernel"
version = "0.1.0"
edition = "2021"
description = "Valthrun Kernel Driver memory interface implementation"

[lib]
crate-type = ["cdylib"]

[dependencies]
windows = { version = "0.48.0", features = [
"Win32_Foundation",
"Win32_Security",
"Win32_System_IO",
"Win32_Storage_FileSystem",
] }
valthrun-driver-protocol = { path = "../valthrun-driver-protocol" }
anyhow = "1.0.93"
obfstr = "0.4.4"
126 changes: 126 additions & 0 deletions driver-interface/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
use std::{
ffi::c_void,
slice,
sync::RwLock,
};

use obfstr::{
obfcstr,
obfstr,
};
use valthrun_driver_protocol::{
command::{
DriverCommand,
DriverCommandInitialize,
InitializeResult,
},
utils::str_to_fixed_buffer,
CommandResult,
PROTOCOL_VERSION,
};
use windows::{
core::PCSTR,
Win32::{
Foundation::{
self,
HANDLE,
},
Storage::FileSystem::{
self,
CreateFileA,
FILE_FLAGS_AND_ATTRIBUTES,
},
System::IO::DeviceIoControl,
},
};

static DRIVER_INTERFACE: RwLock<HANDLE> = RwLock::new(HANDLE(-1));

#[no_mangle]
extern "C" fn execute_command(
command_id: u32,

payload: *mut u8,
payload_length: usize,

error_message: *mut u8,
error_message_length: usize,
) -> u64 {
let control_code = {
(0x00000022 << 16) | // FILE_DEVICE_UNKNOWN
(0x00000000 << 14) | // FILE_SPECIAL_ACCESS
(0x00000001 << 13) | // Custom access code
((command_id & 0x3FF) << 02) |
(0x00000003 << 00)
};

let payload = unsafe { slice::from_raw_parts_mut(payload, payload_length) };
let error_message = unsafe { slice::from_raw_parts_mut(error_message, error_message_length) };

if command_id == DriverCommandInitialize::COMMAND_ID {
let command = unsafe { &mut *(payload.as_mut_ptr() as *mut DriverCommandInitialize) };

/* initialize device handle */
let handle = unsafe {
CreateFileA(
PCSTR::from_raw(
obfcstr!(cr"\\.\GLOBALROOT\Device\valthrun")
.to_bytes()
.as_ptr(),
),
Foundation::GENERIC_READ.0 | Foundation::GENERIC_WRITE.0,
FileSystem::FILE_SHARE_READ | FileSystem::FILE_SHARE_WRITE,
None,
FileSystem::OPEN_EXISTING,
FILE_FLAGS_AND_ATTRIBUTES(0),
None,
)
};

match handle {
Ok(handle) => {
let mut driver_handle = DRIVER_INTERFACE.write().unwrap();
*driver_handle = handle;
}
Err(err) => {
command.driver_protocol_version = PROTOCOL_VERSION;
if err.code().0 as u32 == 0x80070002 {
command.result = InitializeResult::Unavailable;
return CommandResult::Success.bits();
} else {
str_to_fixed_buffer(
error_message,
&format!("{}: {:#}", obfstr!("open kernel driver"), err),
);
return CommandResult::Error.bits();
}
}
}
}

let handle = DRIVER_INTERFACE.read().unwrap();
if handle.is_invalid() {
str_to_fixed_buffer(error_message, "driver not initialized");
return CommandResult::Error.bits();
}

let success = unsafe {
DeviceIoControl(
*handle,
control_code,
Some(payload.as_ptr() as *const c_void),
payload.len() as u32,
Some(payload.as_mut_ptr() as *mut c_void),
payload.len() as u32,
None,
None,
)
};

if success.as_bool() {
CommandResult::Success.bits()
} else {
str_to_fixed_buffer(error_message, obfstr!("ioctrl error"));
CommandResult::Error.bits()
}
}

0 comments on commit a933e56

Please sign in to comment.