Skip to main content

vmi_os_linux/comps/
path.rs

1use vmi_core::{Va, VmiError, VmiState, VmiVa, driver::VmiRead};
2
3use super::{LinuxDEntry, LinuxVFSMount, macros::impl_offsets};
4use crate::{ArchAdapter, LinuxError, LinuxOs};
5
6/// A Linux path struct.
7///
8/// The struct `path` is a fundamental structure in the Linux kernel used to
9/// represent a location in the filesystem.
10///
11/// # Implementation Details
12///
13/// Corresponds to `path`.
14pub struct LinuxPath<'a, Driver>
15where
16    Driver: VmiRead,
17    Driver::Architecture: ArchAdapter<Driver>,
18{
19    /// The VMI state.
20    vmi: VmiState<'a, LinuxOs<Driver>>,
21
22    /// The virtual address of the `path` structure.
23    va: Va,
24}
25
26impl<Driver> VmiVa for LinuxPath<'_, Driver>
27where
28    Driver: VmiRead,
29    Driver::Architecture: ArchAdapter<Driver>,
30{
31    fn va(&self) -> Va {
32        self.va
33    }
34}
35
36impl<'a, Driver> LinuxPath<'a, Driver>
37where
38    Driver: VmiRead,
39    Driver::Architecture: ArchAdapter<Driver>,
40{
41    impl_offsets!();
42
43    /// Creates a new `path`.
44    pub fn new(vmi: VmiState<'a, LinuxOs<Driver>>, va: Va) -> Self {
45        Self { vmi, va }
46    }
47
48    /// Returns the directory entry (dentry) in the VFS (Virtual Filesystem).
49    ///
50    /// # Implementation Details
51    ///
52    /// Corresponds to `path->dentry`.
53    pub fn dentry(&self) -> Result<LinuxDEntry<'a, Driver>, VmiError> {
54        let offsets = self.offsets();
55        let __path = &offsets.path;
56
57        let dentry = self.vmi.read_va_native(self.va + __path.dentry.offset())?;
58
59        if dentry.is_null() {
60            return Err(LinuxError::CorruptedStruct("path->dentry").into());
61        }
62
63        Ok(LinuxDEntry::new(self.vmi, dentry))
64    }
65
66    /// Returns the the mount point associated with the path.
67    ///
68    /// # Implementation Details
69    ///
70    /// Corresponds to `path->mnt`.
71    pub fn mnt(&self) -> Result<LinuxVFSMount<'a, Driver>, VmiError> {
72        let offsets = self.offsets();
73        let __path = &offsets.path;
74
75        let mnt = self.vmi.read_va_native(self.va + __path.mnt.offset())?;
76
77        if mnt.is_null() {
78            return Err(LinuxError::CorruptedStruct("path->mnt").into());
79        }
80
81        Ok(LinuxVFSMount::new(self.vmi, mnt))
82    }
83}