summaryrefslogtreecommitdiff
path: root/crates/windows-kernel-rs/src/request.rs
blob: 7717071aaf7d92de12f1878feb8ac0286afda25f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use core::ops::Deref;

use bitflags::bitflags;
use windows_kernel_sys::{
  base::{
    IO_NO_INCREMENT,
    IO_STACK_LOCATION,
    IRP,
    STATUS_SUCCESS,
    _MM_PAGE_PRIORITY as MM_PAGE_PRIORITY,
  },
  ntoskrnl::{
    IoCompleteRequest,
    IoGetCurrentIrpStackLocation,
    MmGetMdlByteCount,
    MmGetMdlByteOffset,
    MmGetSystemAddressForMdlSafe,
  },
};

use crate::{
  error::Error,
  ioctl::{ControlCode, RequiredAccess, TransferMethod},
  user_ptr::UserPtr,
};

bitflags! {
    pub struct IrpFlags: u32 {
        const NOCACHE = windows_kernel_sys::base::IRP_NOCACHE;
        const PAGING_IO = windows_kernel_sys::base::IRP_PAGING_IO;
        const MOUNT_COMPLETION =  windows_kernel_sys::base::IRP_MOUNT_COMPLETION;
        const SYNCHRONOUS_API = windows_kernel_sys::base::IRP_SYNCHRONOUS_API;
        const ASSOCIATED_IRP = windows_kernel_sys::base::IRP_ASSOCIATED_IRP;
        const BUFFERED_IO = windows_kernel_sys::base::IRP_BUFFERED_IO;
        const DEALLOCATE_BUFFER = windows_kernel_sys::base::IRP_DEALLOCATE_BUFFER;
        const INPUT_OPERATION = windows_kernel_sys::base::IRP_INPUT_OPERATION;
        const SYNCHRONOUS_PAGING_IO = windows_kernel_sys::base::IRP_SYNCHRONOUS_PAGING_IO;
        const CREATE_OPERATION = windows_kernel_sys::base::IRP_CREATE_OPERATION;
        const READ_OPERATION = windows_kernel_sys::base::IRP_READ_OPERATION;
        const WRITE_OPERATION = windows_kernel_sys::base::IRP_WRITE_OPERATION;
        const CLOSE_OPERATION = windows_kernel_sys::base::IRP_CLOSE_OPERATION;
        const DEFER_IO_COMPLETION = windows_kernel_sys::base::IRP_DEFER_IO_COMPLETION;
        const OB_QUERY_NAME = windows_kernel_sys::base::IRP_OB_QUERY_NAME;
        const HOLD_DEVICE_QUEUE = windows_kernel_sys::base::IRP_HOLD_DEVICE_QUEUE;
        const UM_DRIVER_INITIATED_IO = windows_kernel_sys::base::IRP_UM_DRIVER_INITIATED_IO;
    }
}

pub struct IoRequest {
  irp: *mut IRP,
}

impl IoRequest {
  /// # Safety
  /// `unsafe`
  pub unsafe fn from_raw(irp: *mut IRP) -> Self {
    Self {
      irp,
    }
  }

  pub fn irp(&self) -> &IRP { unsafe { &*self.irp } }

  pub fn irp_mut(&self) -> &mut IRP { unsafe { &mut *self.irp } }

  pub fn flags(&self) -> IrpFlags {
    IrpFlags::from_bits(self.irp().Flags).unwrap_or(IrpFlags::empty())
  }

  pub fn stack_location(&self) -> &IO_STACK_LOCATION {
    unsafe { &*IoGetCurrentIrpStackLocation(self.irp_mut()) }
  }

  pub fn major(&self) -> u8 { self.stack_location().MajorFunction }

  pub(crate) fn complete(&self, value: Result<u32, Error>) {
    let irp = self.irp_mut();

    match value {
      Ok(value) => {
        irp.IoStatus.Information = value as _;
        irp.IoStatus.__bindgen_anon_1.Status = STATUS_SUCCESS;
      }
      Err(error) => {
        irp.IoStatus.Information = 0;
        irp.IoStatus.__bindgen_anon_1.Status = error.to_ntstatus();
      }
    }

    unsafe {
      IoCompleteRequest(irp, IO_NO_INCREMENT as _);
    }
  }
}

pub struct ReadRequest {
  pub(crate) inner: IoRequest,
}

impl Deref for ReadRequest {
  type Target = IoRequest;

  fn deref(&self) -> &Self::Target { &self.inner }
}

impl ReadRequest {
  pub fn user_ptr(&self) -> UserPtr {
    let stack_location = self.stack_location();
    let irp = self.irp();

    let (ptr, size) = if !irp.MdlAddress.is_null() {
      let ptr = unsafe {
        MmGetSystemAddressForMdlSafe(irp.MdlAddress, MM_PAGE_PRIORITY::HighPagePriority as _)
      };

      let size = unsafe { MmGetMdlByteCount(irp.MdlAddress) } as usize;

      (ptr, size)
    } else if !unsafe { irp.AssociatedIrp.SystemBuffer }.is_null() {
      let ptr = unsafe { irp.AssociatedIrp.SystemBuffer };
      let size = unsafe { stack_location.Parameters.Read }.Length as usize;

      (ptr, size)
    } else {
      (core::ptr::null_mut(), 0)
    };

    unsafe { UserPtr::new_buffered(ptr, 0, size) }
  }

  pub fn offset(&self) -> i64 {
    let stack_location = self.stack_location();
    let irp = self.irp();

    if !irp.MdlAddress.is_null() {
      (unsafe { MmGetMdlByteOffset(irp.MdlAddress) }) as i64
    } else if !unsafe { irp.AssociatedIrp.SystemBuffer }.is_null() {
      unsafe { stack_location.Parameters.Read.ByteOffset.QuadPart }
    } else {
      0
    }
  }
}

impl Into<IoRequest> for ReadRequest {
  fn into(self) -> IoRequest { self.inner }
}

pub struct WriteRequest {
  pub(crate) inner: IoRequest,
}

impl Deref for WriteRequest {
  type Target = IoRequest;

  fn deref(&self) -> &Self::Target { &self.inner }
}

impl WriteRequest {
  pub fn user_ptr(&self) -> UserPtr {
    let stack_location = self.stack_location();
    let irp = self.irp();

    let (ptr, size) = if !irp.MdlAddress.is_null() {
      let ptr = unsafe {
        MmGetSystemAddressForMdlSafe(irp.MdlAddress, MM_PAGE_PRIORITY::HighPagePriority as _)
      };

      let size = unsafe { MmGetMdlByteCount(irp.MdlAddress) } as usize;

      (ptr, size)
    } else if !unsafe { irp.AssociatedIrp.SystemBuffer }.is_null() {
      let ptr = unsafe { irp.AssociatedIrp.SystemBuffer };
      let size = unsafe { stack_location.Parameters.Write }.Length as usize;

      (ptr, size)
    } else {
      (core::ptr::null_mut(), 0)
    };

    unsafe { UserPtr::new_buffered(ptr, size, 0) }
  }

  pub fn offset(&self) -> i64 {
    let stack_location = self.stack_location();
    let irp = self.irp();

    if !irp.MdlAddress.is_null() {
      (unsafe { MmGetMdlByteOffset(irp.MdlAddress) }) as i64
    } else if !unsafe { irp.AssociatedIrp.SystemBuffer }.is_null() {
      unsafe { stack_location.Parameters.Write.ByteOffset.QuadPart }
    } else {
      0
    }
  }
}

impl Into<IoRequest> for WriteRequest {
  fn into(self) -> IoRequest { self.inner }
}

pub struct IoControlRequest {
  pub(crate) inner: IoRequest,
}

impl Deref for IoControlRequest {
  type Target = IoRequest;

  fn deref(&self) -> &Self::Target { &self.inner }
}

impl IoControlRequest {
  pub fn control_code(&self) -> ControlCode {
    let stack_location = self.stack_location();

    unsafe {
      stack_location
        .Parameters
        .DeviceIoControl
        .IoControlCode
        .into()
    }
  }

  pub fn function(&self) -> (RequiredAccess, u32) {
    let code = self.control_code();

    (code.required_access(), code.number())
  }

  pub fn user_ptr(&self) -> UserPtr {
    let stack_location = self.stack_location();
    let irp = self.irp();

    let system_buffer = unsafe { irp.AssociatedIrp.SystemBuffer };

    let mdl_address = if !irp.MdlAddress.is_null() {
      unsafe {
        MmGetSystemAddressForMdlSafe(irp.MdlAddress, MM_PAGE_PRIORITY::HighPagePriority as _)
      }
    } else {
      core::ptr::null_mut()
    };

    let input_size =
      unsafe { stack_location.Parameters.DeviceIoControl.InputBufferLength } as usize;
    let output_size =
      unsafe { stack_location.Parameters.DeviceIoControl.OutputBufferLength } as usize;

    match self.control_code().transfer_method() {
      TransferMethod::Buffered => unsafe {
        UserPtr::new_buffered(system_buffer, input_size, output_size)
      },
      TransferMethod::InputDirect => unsafe {
        UserPtr::new_direct(mdl_address, system_buffer, output_size, input_size)
      },
      TransferMethod::OutputDirect => unsafe {
        UserPtr::new_direct(system_buffer, mdl_address, input_size, output_size)
      },
      TransferMethod::Neither => unsafe { UserPtr::new_neither() },
    }
  }
}

impl Into<IoRequest> for IoControlRequest {
  fn into(self) -> IoRequest { self.inner }
}