Skip to content
This repository was archived by the owner on Aug 10, 2024. It is now read-only.

Commit 54a14ac

Browse files
authored
Merge pull request #3 from sn99/master
NotifyCallback
2 parents 3c8e694 + dbe3c6a commit 54a14ac

File tree

4 files changed

+57
-43
lines changed

4 files changed

+57
-43
lines changed

src/lib.rs

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
#![allow(non_snake_case)]
33

44
extern crate alloc;
5+
56
use windows_kernel_alloc;
67
use windows_kernel_alloc::kernel_alloc::POOL_TAG;
78

8-
pub mod shared_def;
9-
109
use core::panic::PanicInfo;
10+
use core::ptr;
1111
use core::ptr::null_mut;
1212
use windows_kernel_macros::{InitializeObjectAttributes, NT_SUCCESS, PAGED_CODE};
1313
use windows_kernel_string::UNICODE_STRING;
@@ -21,6 +21,7 @@ use windows_kernel_sys::base::{
2121
PCHAR, PFLT_CALLBACK_DATA, PFLT_FILTER, PFLT_PORT, PSECURITY_DESCRIPTOR, PULONG, PVOID,
2222
STATUS_SUCCESS, ULONG, USHORT,
2323
};
24+
use windows_kernel_sys::c_int;
2425
use windows_kernel_sys::fltmgr::{
2526
strcpy, DbgPrint, FltBuildDefaultSecurityDescriptor, FltCloseClientPort,
2627
FltCloseCommunicationPort, FltCreateCommunicationPort, FltFreeSecurityDescriptor,
@@ -42,10 +43,10 @@ const G_FILTER_REGISTRATION: FLT_REGISTRATION = FLT_REGISTRATION {
4243
ContextRegistration: null_mut(),
4344
OperationRegistration: G_CALLBACKS.as_ptr(),
4445
FilterUnloadCallback: Some(InstanceFilterUnloadCallback),
45-
InstanceSetupCallback: Some(InstanceSetupCallback),
46-
InstanceQueryTeardownCallback: Some(InstanceQueryTeardownCallback),
47-
InstanceTeardownStartCallback: Some(InstanceTeardownStartCallback),
48-
InstanceTeardownCompleteCallback: Some(InstanceTeardownCompleteCallback),
46+
InstanceSetupCallback: None, //Some(InstanceSetupCallback),
47+
InstanceQueryTeardownCallback: None, // Some(InstanceQueryTeardownCallback),
48+
InstanceTeardownStartCallback: None, //Some(InstanceTeardownStartCallback),
49+
InstanceTeardownCompleteCallback: None, //Some(InstanceTeardownCompleteCallback),
4950
GenerateFileNameCallback: None,
5051
NormalizeNameComponentCallback: None,
5152
NormalizeContextCleanupCallback: None,
@@ -74,9 +75,9 @@ unsafe extern "C" fn InstanceTeardownCompleteCallback(
7475
///
7576
const G_CALLBACKS: &[FLT_OPERATION_REGISTRATION] = {
7677
&[
77-
FLT_OPERATION_REGISTRATION::new()
78-
.set_major_function(FLT_OPERATION_REGISTRATION::IRP_MJ_CREATE)
79-
.set_preop(Some(PreOperationCreate)),
78+
// FLT_OPERATION_REGISTRATION::new()
79+
// .set_major_function(FLT_OPERATION_REGISTRATION::IRP_MJ_CREATE)
80+
// .set_preop(Some(PreOperationCreate)),
8081
FLT_OPERATION_REGISTRATION::new()
8182
.set_major_function(FLT_OPERATION_REGISTRATION::IRP_MJ_OPERATION_END),
8283
]
@@ -93,21 +94,19 @@ unsafe extern "C" fn PreOperationCreate(
9394
let k = &(*(*(*Data).Iopb).TargetFileObject).FileName;
9495

9596
unsafe {
96-
DbgPrint("%wZ\n".as_ptr() as _, k);
97+
DbgPrint("%wZ\n\0".as_ptr() as _, k);
9798
}
9899

99100
FLT_PREOP_SUCCESS_NO_CALLBACK
100101
}
101102

102-
///
103103
/// This is called before a filter is unloaded.
104104
/// If NULL is specified for this routine, then the filter can never be unloaded.
105-
///
106105
extern "C" fn InstanceFilterUnloadCallback(_Flags: FLT_FILTER_UNLOAD_FLAGS) -> NTSTATUS {
107106
PAGED_CODE!();
108107

109108
unsafe {
110-
DbgPrint("Unloading rust minifilter\0\n".as_ptr() as _);
109+
DbgPrint("Unloading rust minifilter\n\0".as_ptr() as _);
111110
FltCloseCommunicationPort(PORT);
112111

113112
FltUnregisterFilter(G_MINIFILTER_HANDLE);
@@ -156,34 +155,49 @@ pub extern "system" fn DriverEntry(
156155
) -> NTSTATUS {
157156
let mut sd: PSECURITY_DESCRIPTOR = null_mut();
158157
let mut oa: OBJECT_ATTRIBUTES = unsafe { core::mem::zeroed() };
159-
let mut name: UNICODE_STRING = UNICODE_STRING::create("\\mf");
158+
let mut name = "\\mf";
160159

161160
unsafe {
162-
DbgPrint("Hello from Rust!\0".as_ptr() as _);
161+
DbgPrint("Hello from Rust!\n\0".as_ptr() as _);
163162
}
164163

164+
// driver.DriverUnload = Some(driver_exit);
165+
165166
//
166167
// register minifilter driver
167168
//
168169
let mut status: NTSTATUS =
169170
unsafe { FltRegisterFilter(driver, &G_FILTER_REGISTRATION, &mut G_MINIFILTER_HANDLE) };
170171

172+
unsafe {
173+
DbgPrint("1 Here\n\0".as_ptr() as _);
174+
}
175+
171176
if !NT_SUCCESS!(status) {
172177
return status;
173178
}
174179

180+
unsafe {
181+
DbgPrint("2 Here\n\0".as_ptr() as _);
182+
}
183+
175184
status = unsafe { FltBuildDefaultSecurityDescriptor(&mut sd, FLT_PORT_ALL_ACCESS) };
176185

186+
let name = UNICODE_STRING::create(name);
187+
177188
if NT_SUCCESS!(status) {
178189
unsafe {
179190
InitializeObjectAttributes(
180191
&mut oa,
181-
&mut name,
182-
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
192+
&mut name.as_base_unicode(),
193+
OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE,
183194
null_mut(),
184195
sd,
185196
);
186197
}
198+
unsafe {
199+
DbgPrint("3 Here\n\0".as_ptr() as _);
200+
}
187201

188202
status = unsafe {
189203
FltCreateCommunicationPort(
@@ -202,17 +216,31 @@ pub extern "system" fn DriverEntry(
202216
FltFreeSecurityDescriptor(sd);
203217
}
204218

219+
unsafe {
220+
DbgPrint("4 Here\n\0".as_ptr() as _);
221+
}
222+
205223
if NT_SUCCESS!(status) {
224+
unsafe {
225+
DbgPrint("5 Here\n\0".as_ptr() as _);
226+
}
206227
// driver.DriverUnload = Some(driver_exit);
207228

208229
// start minifilter driver
209230
status = unsafe { FltStartFiltering(G_MINIFILTER_HANDLE) };
210231

211232
if !NT_SUCCESS!(status) {
233+
unsafe {
234+
DbgPrint("6 Here\0\n".as_ptr() as _);
235+
}
212236
unsafe {
213237
FltUnregisterFilter(G_MINIFILTER_HANDLE);
214238
}
215239
}
240+
} else {
241+
unsafe {
242+
FltCloseCommunicationPort(PORT);
243+
}
216244
}
217245
}
218246

@@ -227,7 +255,7 @@ unsafe extern "C" fn MiniConnect(
227255
ConnectionPortCookie: *mut PVOID,
228256
) -> NTSTATUS {
229257
CLIENT_PORT = ClientPort;
230-
DbgPrint("Rust connect fromm application\n\0".as_ptr() as _);
258+
DbgPrint("Rust connect from application\n\0".as_ptr() as _);
231259

232260
STATUS_SUCCESS
233261
}
@@ -245,20 +273,13 @@ unsafe extern "C" fn MiniSendRec(
245273
OutputBufferLength: ULONG,
246274
ReturnOutputBufferLength: PULONG,
247275
) -> NTSTATUS {
248-
// let mut msg: PCHAR = "Rust from kernel".as_mut_ptr() as *mut i8;
249-
unsafe {
250-
DbgPrint(
251-
"Rust message from application: %s\n\0".as_ptr() as _,
252-
InputBuffer as PCHAR,
253-
);
254-
}
276+
let mut msg: PCHAR = "Rust from kernel\n\0".as_bytes().as_ptr() as *mut i8;
277+
DbgPrint(
278+
"Rust message from application: %s".as_ptr() as _,
279+
InputBuffer as *mut i8,
280+
);
255281

256-
unsafe {
257-
strcpy(
258-
OutputBuffer as PCHAR,
259-
"Rust from kernel".as_ptr() as *mut i8,
260-
);
261-
}
282+
strcpy(OutputBuffer as PCHAR, msg);
262283

263284
STATUS_SUCCESS
264285
}

src/shared_def.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

windows-kernel-macros/src/lib.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,16 @@ pub const NULL: PVOID = 0 as PVOID;
2828
#[inline]
2929
pub unsafe fn InitializeObjectAttributes(
3030
p: windows_kernel_sys::base::POBJECT_ATTRIBUTES,
31-
n: windows_kernel_string::PUNICODE_STRING,
31+
n: windows_kernel_sys::base::PUNICODE_STRING,
3232
a: windows_kernel_sys::base::ULONG,
3333
r: windows_kernel_sys::base::HANDLE,
3434
s: windows_kernel_sys::base::PVOID,
3535
) {
36-
let mut n = windows_kernel_sys::base::_UNICODE_STRING{
37-
Length: (*n).Length,
38-
MaximumLength: (*n).MaximumLength,
39-
Buffer: (*n).ptr as *mut u16,
40-
};
41-
4236
use core::mem::size_of;
43-
(*p).Length = size_of::<windows_kernel_sys::base::OBJECT_ATTRIBUTES>() as windows_kernel_sys::base::ULONG;
37+
(*p).Length = size_of::<windows_kernel_sys::base::OBJECT_ATTRIBUTES>() as windows_kernel_sys::base::ULONG;
4438
(*p).RootDirectory = r;
4539
(*p).Attributes = a;
46-
(*p).ObjectName = &mut n;
40+
(*p).ObjectName = n;
4741
(*p).SecurityDescriptor = s;
4842
(*p).SecurityQualityOfService = NULL;
4943
}

windows-kernel-string/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl<'a> From<&'a [u8]> for ANSI_STRING {
3131
let mut str = ANSI_STRING::default();
3232

3333
let mut buffer = buffer.to_vec();
34-
if *buffer.last().unwrap() != 0 {
34+
if *buffer.last().expect("bad unwrap on From<&'a [u8]> for ANSI_STRING") != 0 {
3535
//let mut buffer = buffer.to_vec();
3636
buffer.push(0);
3737
}
@@ -127,7 +127,7 @@ impl<'a> From<&'a [u16]> for UNICODE_STRING {
127127
let mut str = UNICODE_STRING::default();
128128

129129
let mut buffer = buffer.to_vec();
130-
if *buffer.last().unwrap() == 0 {
130+
if *buffer.last().expect("bad unwrap on From<&'a [u16]> for UNICODE_STRING") == 0 {
131131
buffer.push(0);
132132
}
133133

0 commit comments

Comments
 (0)