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
use once_cell::unsync::OnceCell;
use vmi_core::{
Pa, Va, VmiError, VmiOs, VmiState, VmiVa,
driver::VmiRead,
os::{ProcessId, ProcessObject, VmiOsImageArchitecture, VmiOsProcess},
};
use super::{LinuxFsStruct, LinuxMmStruct, LinuxPath, LinuxVmAreaStruct, macros::impl_offsets};
use crate::{ArchAdapter, LinuxError, LinuxOs};
/// A Linux task struct.
///
/// The `task_struct` is the process descriptor in the Linux kernel,
/// representing a task (process or thread).
///
/// # Implementation Details
///
/// Corresponds to `task_struct`.
pub struct LinuxTaskStruct<'a, Driver>
where
Driver: VmiRead,
Driver::Architecture: ArchAdapter<Driver>,
{
/// The VMI state.
vmi: VmiState<'a, LinuxOs<Driver>>,
/// The virtual address of the `task_struct` structure.
va: Va,
/// Cached flags.
flags: OnceCell<u32>,
}
impl<Driver> VmiVa for LinuxTaskStruct<'_, Driver>
where
Driver: VmiRead,
Driver::Architecture: ArchAdapter<Driver>,
{
fn va(&self) -> Va {
self.va
}
}
impl<'a, Driver> LinuxTaskStruct<'a, Driver>
where
Driver: VmiRead,
Driver::Architecture: ArchAdapter<Driver>,
{
impl_offsets!();
/// Creates a new Linux task struct.
pub fn new(vmi: VmiState<'a, LinuxOs<Driver>>, process: ProcessObject) -> Self {
Self {
vmi,
va: process.0,
flags: OnceCell::new(),
}
}
/// Returns the process flags.
///
/// Process flags in Linux include information about the process state,
/// such as whether it's exiting, a kernel thread, etc.
///
/// # Implementation Details
///
/// Corresponds to `task_struct.flags`.
pub fn flags(&self) -> Result<u32, VmiError> {
self.flags
.get_or_try_init(|| {
let offsets = self.offsets();
let __task_struct = &offsets.task_struct;
self.vmi.read_u32(self.va + __task_struct.flags.offset())
})
.copied()
}
/// Returns the memory descriptor (`mm_struct`) of the user-mode process.
///
/// The `mm_struct` contains the memory management information for a process.
/// Kernel threads don't have an `mm_struct` and return `None`.
///
/// # Implementation Details
///
/// Corresponds to `task_struct->mm`.
pub fn mm(&self) -> Result<Option<LinuxMmStruct<'a, Driver>>, VmiError> {
let offsets = self.offsets();
let __task_struct = &offsets.task_struct;
let mm = self
.vmi
.read_va_native(self.va + __task_struct.mm.offset())?;
if mm.is_null() {
return Ok(None);
}
Ok(Some(LinuxMmStruct::new(self.vmi, mm)))
}
/// Returns the active memory context (`mm_struct`) of the process.
///
/// Used by kernel threads to reference the last used `mm_struct` before
/// entering kernel mode.
///
/// If a kernel thread ([`mm()`] is `None`) needs memory access,
/// it temporarily borrows `active_mm` from the last scheduled user-space
/// process.
///
/// When the kernel thread exits, the original `mm_struct` is restored.
///
/// # Implementation Details
///
/// Corresponds to `task_struct->active_mm`.
///
/// [`mm()`]: Self::mm
pub fn active_mm(&self) -> Result<LinuxMmStruct<'a, Driver>, VmiError> {
let offsets = self.offsets();
let __task_struct = &offsets.task_struct;
let mm = self
.vmi
.read_va_native(self.va + __task_struct.active_mm.offset())?;
if mm.is_null() {
return Err(LinuxError::CorruptedStruct("task_struct->active_mm").into());
}
Ok(LinuxMmStruct::new(self.vmi, mm))
}
/// Returns the filesystem context (`fs_struct`) of the process.
///
/// `fs_struct` contains:
/// - [`root`]: The process’s root directory (used for chroot).
/// - [`pwd`]: The current working directory.
///
/// All threads in the same process share the same `fs_struct`, unless
/// explicitly changed.
///
/// Kernel threads don't have an `fs_struct` and return `None`.
///
/// # Implementation Details
///
/// Corresponds to `task_struct->fs`.
///
/// [`root`]: LinuxFsStruct::root
/// [`pwd`]: LinuxFsStruct::pwd
pub fn fs(&self) -> Result<Option<LinuxFsStruct<'a, Driver>>, VmiError> {
let offsets = self.offsets();
let __task_struct = &offsets.task_struct;
let __fs_struct = &offsets.fs_struct;
let fs = self
.vmi
.read_va_native(self.va + __task_struct.fs.offset())?;
if fs.is_null() {
return Ok(None);
}
Ok(Some(LinuxFsStruct::new(self.vmi, fs)))
}
/// Constructs the absolute path from a `path` structure.
///
/// Takes into account the process's filesystem root when constructing the
/// absolute path.
///
/// Returns the resolved path as a string if successful, or `None` if the path
/// could not be resolved (e.g., if the root is null).
///
/// # Implementation Details
///
/// Concatenates `task_struct->fs->root` with the `path` structure to construct
/// the absolute path.
pub fn d_path(&self, path: &LinuxPath<Driver>) -> Result<Option<String>, VmiError> {
let root = match self.fs()? {
Some(root) => root.root()?,
None => return Ok(None),
};
Ok(Some(LinuxOs::<Driver>::construct_path(
self.vmi, path, &root,
)?))
}
/// Returns the path of the executable image for a process.
///
/// Returns the executable path as a string, or `None` for special processes
/// like kernel threads or those in the process of exiting.
///
/// # Implementation Details
///
/// Corresponds to `d_path(task->mm->exe_file->f_path)`.
pub fn image_path(&self) -> Result<Option<String>, VmiError> {
let flags = self.flags()?;
const PF_EXITING: u32 = 0x00000004; // getting shut down
const PF_KTHREAD: u32 = 0x00200000; // kernel thread
if flags & PF_KTHREAD != 0 {
return Ok(None);
}
if flags & PF_EXITING != 0 {
return Ok(None);
}
let mm = match self.mm()? {
Some(mm) => mm,
None => return Ok(None),
};
let exe_file = match mm.exe_file()? {
Some(exe_file) => exe_file,
None => return Ok(None),
};
self.d_path(&exe_file.path()?)
}
}
impl<'a, Driver> VmiOsProcess<'a, Driver> for LinuxTaskStruct<'a, Driver>
where
Driver: VmiRead,
Driver::Architecture: ArchAdapter<Driver>,
{
type Os = LinuxOs<Driver>;
fn id(&self) -> Result<ProcessId, VmiError> {
let offsets = self.offsets();
let __task_struct = &offsets.task_struct;
let result = self.vmi.read_u32(self.va + __task_struct.tgid.offset())?;
Ok(ProcessId(result))
}
fn object(&self) -> Result<ProcessObject, VmiError> {
Ok(ProcessObject(self.va))
}
fn name(&self) -> Result<String, VmiError> {
let task_struct_comm_offset = 0xBC0;
self.vmi.read_string(self.va + task_struct_comm_offset)
}
fn parent_id(&self) -> Result<ProcessId, VmiError> {
unimplemented!()
}
fn architecture(&self) -> Result<VmiOsImageArchitecture, VmiError> {
unimplemented!()
}
fn translation_root(&self) -> Result<Pa, VmiError> {
unimplemented!()
}
fn user_translation_root(&self) -> Result<Pa, VmiError> {
unimplemented!()
}
fn image_base(&self) -> Result<Va, VmiError> {
unimplemented!()
}
fn regions(
&self,
) -> Result<
impl Iterator<Item = Result<<Self::Os as VmiOs>::Region<'a>, VmiError>> + use<'a, Driver>,
VmiError,
> {
let mut result = Vec::new();
let mm = match self.mm()? {
Some(mm) => mm,
None => return Ok(result.into_iter()),
};
let vmi = self.vmi;
let mt = mm.mm_mt()?;
mt.enumerate(|node| {
println!("XXXNode: {}", node);
if !node.is_null() {
result.push(Ok(LinuxVmAreaStruct::new(vmi, node)));
}
true
})?;
Ok(result.into_iter())
}
fn lookup_region(
&self,
_address: Va,
) -> Result<Option<<Self::Os as VmiOs>::Region<'a>>, VmiError> {
unimplemented!()
}
fn user_modules(
&self,
) -> Result<
impl Iterator<Item = Result<<Self::Os as VmiOs>::UserModule<'a>, VmiError>> + use<'a, Driver>,
VmiError,
> {
#[allow(unreachable_code)]
{
unimplemented!() as Result<std::iter::Empty<_>, VmiError>
}
}
fn lookup_user_module(
&self,
_address: Va,
) -> Result<Option<<Self::Os as VmiOs>::UserModule<'a>>, VmiError> {
unimplemented!()
}
fn threads(
&self,
) -> Result<
impl Iterator<Item = Result<<Self::Os as VmiOs>::Thread<'a>, VmiError>> + use<'a, Driver>,
VmiError,
> {
#[allow(unreachable_code)]
{
unimplemented!() as Result<std::iter::Empty<_>, VmiError>
}
}
fn is_valid_address(&self, _address: Va) -> Result<Option<bool>, VmiError> {
unimplemented!()
}
}