vmi-os-windows 0.7.0

Windows OS specific code for VMI
Documentation
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use std::iter::FusedIterator;

use vmi_core::{Pa, Registers as _, Va, VmiError, VmiState, driver::VmiRead};

use crate::{
    WindowsOs, WindowsWow64Kind,
    arch::{ArchAdapter, StructLayout, StructLayout32, StructLayout64},
};

/// Field offsets for a `LIST_ENTRY` structure.
pub trait ListEntry<Layout>
where
    Layout: StructLayout,
{
    /// Offset of the `LIST_ENTRY::Flink` field.
    const OFFSET_FLINK: u64;

    /// Offset of the `LIST_ENTRY::Blink` field.
    const OFFSET_BLINK: u64;
}

/// `LIST_ENTRY` structure layout.
pub struct ListEntryLayout;

impl ListEntry<StructLayout32> for ListEntryLayout {
    const OFFSET_FLINK: u64 = 0x00;
    const OFFSET_BLINK: u64 = 0x04;
}

impl ListEntry<StructLayout64> for ListEntryLayout {
    const OFFSET_FLINK: u64 = 0x00;
    const OFFSET_BLINK: u64 = 0x08;
}

/// Iterator over a `LIST_ENTRY` chain with a compile-time pointer width.
///
/// Generic over [`StructLayout`], so it is monomorphized for either
/// 32-bit or 64-bit structures. Prefer [`ListEntryIterator`] when the
/// pointer width is not known at compile time.
pub struct ListEntryIteratorBase<'a, Driver, Layout>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
    Layout: StructLayout,
    ListEntryLayout: ListEntry<Layout>,
{
    /// VMI state.
    vmi: VmiState<'a, WindowsOs<Driver>>,

    /// Address of the list head.
    list_head: Va,

    /// Offset to the containing structure.
    ///
    /// The offset is subtracted from the entry address to get the containing
    /// structure, similar to the `CONTAINING_RECORD` macro in the Windows
    /// kernel.
    offset: u64,

    /// The translation root.
    root: Pa,

    /// Current entry.
    current: Option<Va>,

    /// Whether the iterator has been initialized.
    initialized: bool,

    _marker: std::marker::PhantomData<Layout>,
}

impl<'a, Driver, Layout> ListEntryIteratorBase<'a, Driver, Layout>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
    Layout: StructLayout,
    ListEntryLayout: ListEntry<Layout>,
{
    /// Creates a new list entry iterator.
    pub fn new(vmi: VmiState<'a, WindowsOs<Driver>>, list_head: Va, offset: u64, root: Pa) -> Self {
        Self {
            vmi,
            list_head,
            offset,
            root,
            current: None,
            initialized: false,
            _marker: std::marker::PhantomData,
        }
    }

    /// Returns the next entry in the list.
    ///
    /// Corresponds to the `LIST_ENTRY.Flink` pointer.
    fn next_entry(&self, entry: Va) -> Result<Va, VmiError> {
        Layout::read_va(
            self.vmi,
            (
                entry + <ListEntryLayout as ListEntry<Layout>>::OFFSET_FLINK,
                self.root,
            ),
        )
    }

    /// Returns the previous entry in the list.
    ///
    /// Corresponds to the `LIST_ENTRY.Blink` pointer.
    fn previous_entry(&self, entry: Va) -> Result<Va, VmiError> {
        Layout::read_va(
            self.vmi,
            (
                entry + <ListEntryLayout as ListEntry<Layout>>::OFFSET_BLINK,
                self.root,
            ),
        )
    }

    /// Returns the first entry in the list.
    ///
    /// Returns `None` if the `list_head` is `NULL`.
    fn first_entry(&self) -> Result<Option<Va>, VmiError> {
        if self.list_head.is_null() {
            return Ok(None);
        }

        Ok(Some(self.next_entry(self.list_head)?))
    }

    /// Returns the last entry in the list.
    ///
    /// Returns `None` if the `list_head` is `NULL`.
    fn last_entry(&self) -> Result<Option<Va>, VmiError> {
        if self.list_head.is_null() {
            return Ok(None);
        }

        Ok(Some(self.previous_entry(self.list_head)?))
    }

    /// Walks to the next entry in the list.
    fn walk_next(&mut self) -> Result<Option<Va>, VmiError> {
        let entry = match self.current {
            Some(entry) => entry,
            None => {
                // If `self.current` is `None`, we need to initialize the iterator.
                //
                // However, if the iterator has already been initialized, we should
                // return `None` to prevent infinite iteration.
                if self.initialized {
                    return Ok(None);
                }

                self.initialized = true;

                match self.first_entry() {
                    Ok(Some(first)) => first,
                    Ok(None) => return Ok(None),
                    Err(err) => return Err(err),
                }
            }
        };

        if entry == self.list_head {
            return Ok(None);
        }

        match self.next_entry(entry) {
            Ok(next) => self.current = Some(next),
            Err(err) => {
                // Terminate iteration so that callers who `continue` on
                // errors do not spin forever on the same failing entry.
                self.current = None;
                return Err(err);
            }
        }

        Ok(Some(entry - self.offset))
    }

    /// Walks to the previous entry in the list.
    fn walk_next_back(&mut self) -> Result<Option<Va>, VmiError> {
        let entry = match self.current {
            Some(entry) => entry,
            None => {
                // If `self.current` is `None`, we need to initialize the iterator.
                //
                // However, if the iterator has already been initialized, we should
                // return `None` to prevent infinite iteration.
                if self.initialized {
                    return Ok(None);
                }

                self.initialized = true;

                match self.last_entry() {
                    Ok(Some(last)) => last,
                    Ok(None) => return Ok(None),
                    Err(err) => return Err(err),
                }
            }
        };

        if entry == self.list_head {
            return Ok(None);
        }

        match self.previous_entry(entry) {
            Ok(prev) => self.current = Some(prev),
            Err(err) => {
                self.current = None;
                return Err(err);
            }
        }

        Ok(Some(entry - self.offset))
    }
}

impl<Driver, Layout> Iterator for ListEntryIteratorBase<'_, Driver, Layout>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
    Layout: StructLayout,
    ListEntryLayout: ListEntry<Layout>,
{
    type Item = Result<Va, VmiError>;

    fn next(&mut self) -> Option<Self::Item> {
        self.walk_next().transpose()
    }
}

impl<Driver, Layout> DoubleEndedIterator for ListEntryIteratorBase<'_, Driver, Layout>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
    Layout: StructLayout,
    ListEntryLayout: ListEntry<Layout>,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        self.walk_next_back().transpose()
    }
}

impl<Driver, Layout> FusedIterator for ListEntryIteratorBase<'_, Driver, Layout>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
    Layout: StructLayout,
    ListEntryLayout: ListEntry<Layout>,
{
}

enum ListEntryWrapper<'a, Driver>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
{
    W32(ListEntryIteratorBase<'a, Driver, StructLayout32>),
    W64(ListEntryIteratorBase<'a, Driver, StructLayout64>),
}

impl<'a, Driver> ListEntryWrapper<'a, Driver>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
{
    fn w32(vmi: VmiState<'a, WindowsOs<Driver>>, list_head: Va, offset: u64, root: Pa) -> Self {
        Self::W32(ListEntryIteratorBase::new(vmi, list_head, offset, root))
    }

    fn w64(vmi: VmiState<'a, WindowsOs<Driver>>, list_head: Va, offset: u64, root: Pa) -> Self {
        Self::W64(ListEntryIteratorBase::new(vmi, list_head, offset, root))
    }

    fn native(vmi: VmiState<'a, WindowsOs<Driver>>, list_head: Va, offset: u64, root: Pa) -> Self {
        match vmi.registers().address_width() {
            4 => Self::w32(vmi, list_head, offset, root),
            8 => Self::w64(vmi, list_head, offset, root),
            _ => panic!("Unsupported address width"),
        }
    }

    fn walk_next(&mut self) -> Result<Option<Va>, VmiError> {
        match self {
            Self::W32(inner) => inner.walk_next(),
            Self::W64(inner) => inner.walk_next(),
        }
    }

    fn walk_next_back(&mut self) -> Result<Option<Va>, VmiError> {
        match self {
            Self::W32(inner) => inner.walk_next_back(),
            Self::W64(inner) => inner.walk_next_back(),
        }
    }
}

/// Iterator over a `LIST_ENTRY` chain with a runtime pointer width.
///
/// Wraps [`ListEntryIteratorBase`] and erases the [`StructLayout`] type
/// parameter, dispatching between 32-bit and 64-bit layouts dynamically.
pub struct ListEntryIterator<'a, Driver>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
{
    inner: ListEntryWrapper<'a, Driver>,
}

impl<'a, Driver> From<ListEntryIteratorBase<'a, Driver, StructLayout32>>
    for ListEntryIterator<'a, Driver>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
{
    fn from(value: ListEntryIteratorBase<'a, Driver, StructLayout32>) -> Self {
        Self {
            inner: ListEntryWrapper::W32(value),
        }
    }
}

impl<'a, Driver> From<ListEntryIteratorBase<'a, Driver, StructLayout64>>
    for ListEntryIterator<'a, Driver>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
{
    fn from(value: ListEntryIteratorBase<'a, Driver, StructLayout64>) -> Self {
        Self {
            inner: ListEntryWrapper::W64(value),
        }
    }
}

impl<'a, Driver> ListEntryIterator<'a, Driver>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
{
    /// Creates a new list entry iterator.
    pub fn new(vmi: VmiState<'a, WindowsOs<Driver>>, list_head: Va, offset: u64) -> Self {
        Self::with_kind(
            vmi,
            list_head,
            offset,
            vmi.translation_root(list_head),
            WindowsWow64Kind::Native,
        )
    }

    /// Creates a new list entry iterator with an explicit address space
    /// root and pointer width.
    pub fn with_kind(
        vmi: VmiState<'a, WindowsOs<Driver>>,
        list_head: Va,
        offset: u64,
        root: Pa,
        kind: WindowsWow64Kind,
    ) -> Self {
        let inner = match kind {
            WindowsWow64Kind::Native => ListEntryWrapper::native(vmi, list_head, offset, root),
            WindowsWow64Kind::X86 => ListEntryWrapper::w32(vmi, list_head, offset, root),
        };

        Self { inner }
    }

    /// Walks to the next entry in the list.
    fn walk_next(&mut self) -> Result<Option<Va>, VmiError> {
        self.inner.walk_next()
    }

    /// Walks to the previous entry in the list.
    fn walk_next_back(&mut self) -> Result<Option<Va>, VmiError> {
        self.inner.walk_next_back()
    }
}

impl<Driver> Iterator for ListEntryIterator<'_, Driver>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
{
    type Item = Result<Va, VmiError>;

    fn next(&mut self) -> Option<Self::Item> {
        self.walk_next().transpose()
    }
}

impl<Driver> DoubleEndedIterator for ListEntryIterator<'_, Driver>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        self.walk_next_back().transpose()
    }
}

impl<Driver> FusedIterator for ListEntryIterator<'_, Driver>
where
    Driver: VmiRead,
    Driver::Architecture: ArchAdapter<Driver>,
{
}