Skip to main content

vmi_os_linux/comps/
vm_area_struct.rs

1use once_cell::unsync::OnceCell;
2use vmi_core::{
3    MemoryAccess, Va, VmiError, VmiState, VmiVa,
4    driver::VmiRead,
5    os::{VmiOsRegion, VmiOsRegionKind},
6};
7
8use super::macros::impl_offsets;
9use crate::{ArchAdapter, LinuxOs};
10
11/// A Linux VM area struct.
12///
13/// A `vm_area_struct` is a structure that represents a memory region (Virtual
14/// Memory Area, or VMA) in a process's address space.
15///
16/// # Implementation Details
17///
18/// Corresponds to `vm_area_struct`.
19pub struct LinuxVmAreaStruct<'a, Driver>
20where
21    Driver: VmiRead,
22    Driver::Architecture: ArchAdapter<Driver>,
23{
24    /// The VMI state.
25    vmi: VmiState<'a, LinuxOs<Driver>>,
26
27    /// The virtual address of the `vm_area_struct` structure.
28    va: Va,
29
30    /// Cached flags.
31    flags: OnceCell<u64>,
32}
33
34impl<Driver> VmiVa for LinuxVmAreaStruct<'_, Driver>
35where
36    Driver: VmiRead,
37    Driver::Architecture: ArchAdapter<Driver>,
38{
39    fn va(&self) -> Va {
40        self.va
41    }
42}
43
44impl<Driver> std::fmt::Debug for LinuxVmAreaStruct<'_, Driver>
45where
46    Driver: VmiRead,
47    Driver::Architecture: ArchAdapter<Driver>,
48{
49    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
50        let start = self.start();
51        let end = self.end();
52        let protection = self.protection();
53        //let kind = self.kind();
54
55        f.debug_struct("LinuxVmAreaStruct")
56            .field("start", &start)
57            .field("end", &end)
58            .field("protection", &protection)
59            //.field("kind", &kind)
60            .finish()
61    }
62}
63
64impl<'a, Driver> LinuxVmAreaStruct<'a, Driver>
65where
66    Driver: VmiRead,
67    Driver::Architecture: ArchAdapter<Driver>,
68{
69    impl_offsets!();
70
71    /// Creates a new VM area struct.
72    pub fn new(vmi: VmiState<'a, LinuxOs<Driver>>, vad: Va) -> Self {
73        Self {
74            vmi,
75            va: vad,
76            flags: OnceCell::new(),
77        }
78    }
79
80    /// Returns the flags of the memory region.
81    ///
82    /// The flags are a bitmask that represent the memory region's permissions
83    /// and other attributes.
84    ///
85    /// # Notes
86    ///
87    /// This value is cached after the first read.
88    ///
89    /// # Implementation Details
90    ///
91    /// Corresponds to `vm_area_struct.vm_flags`.
92    pub fn flags(&self) -> Result<u64, VmiError> {
93        self.flags
94            .get_or_try_init(|| {
95                let offsets = self.offsets();
96                let __vm_area_struct = &offsets.vm_area_struct;
97
98                self.vmi.read_field(self.va, &__vm_area_struct.vm_flags)
99            })
100            .copied()
101    }
102}
103
104impl<'a, Driver> VmiOsRegion<'a, Driver> for LinuxVmAreaStruct<'a, Driver>
105where
106    Driver: VmiRead,
107    Driver::Architecture: ArchAdapter<Driver>,
108{
109    type Os = LinuxOs<Driver>;
110
111    /// Returns the starting virtual address of the memory region.
112    ///
113    /// # Implementation Details
114    ///
115    /// Corresponds to `vm_area_struct.vm_start`.
116    fn start(&self) -> Result<Va, VmiError> {
117        let offsets = self.offsets();
118        let __vm_area_struct = &offsets.vm_area_struct;
119
120        self.vmi
121            .read_va_native(self.va + __vm_area_struct.vm_start.offset())
122    }
123
124    /// Returns the ending virtual address of the memory region.
125    ///
126    /// # Implementation Details
127    ///
128    /// Corresponds to `vm_area_struct.vm_end`.
129    fn end(&self) -> Result<Va, VmiError> {
130        let offsets = self.offsets();
131        let __vm_area_struct = &offsets.vm_area_struct;
132
133        self.vmi
134            .read_va_native(self.va + __vm_area_struct.vm_end.offset())
135    }
136
137    /// Returns the memory protection of the memory region.
138    ///
139    /// # Implementation Details
140    ///
141    /// Calculated from `vm_area_struct.vm_flags` field.
142    fn protection(&self) -> Result<MemoryAccess, VmiError> {
143        const VM_READ: u64 = 0x00000001;
144        const VM_WRITE: u64 = 0x00000002;
145        const VM_EXEC: u64 = 0x00000004;
146        //const VM_SHARED: u64 = 0x00000008;
147
148        let flags = self.flags()?;
149        let mut protection = MemoryAccess::default();
150        if flags & VM_READ != 0 {
151            protection |= MemoryAccess::R;
152        }
153        if flags & VM_WRITE != 0 {
154            protection |= MemoryAccess::W;
155        }
156        if flags & VM_EXEC != 0 {
157            protection |= MemoryAccess::X;
158        }
159
160        Ok(protection)
161    }
162
163    /// Returns the memory region's kind.
164    fn kind(&self) -> Result<VmiOsRegionKind<'a, Self::Os>, VmiError> {
165        let offsets = self.offsets();
166        let __vm_area_struct = &offsets.vm_area_struct;
167
168        let file = self
169            .vmi
170            .read_va_native(self.va + __vm_area_struct.vm_file.offset())?;
171
172        if file.is_null() {
173            return Ok(VmiOsRegionKind::Private);
174        }
175
176        unimplemented!()
177    }
178}