forked from memflow/memflow-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.rs
More file actions
351 lines (314 loc) · 11.6 KB
/
process.rs
File metadata and controls
351 lines (314 loc) · 11.6 KB
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
use memflow::cglue;
use memflow::os::process::*;
use memflow::prelude::v1::*;
use super::ProcessVirtualMemory;
use libc::pid_t;
use procfs::process::{MMPermissions, MMapExtension, MMapPath};
use itertools::Itertools;
pub struct LinuxProcess {
virt_mem: ProcessVirtualMemory,
pid: pid_t,
info: ProcessInfo,
}
impl Clone for LinuxProcess {
fn clone(&self) -> Self {
Self {
virt_mem: self.virt_mem.clone(),
pid: self.pid,
info: self.info.clone(),
}
}
}
impl LinuxProcess {
fn proc_handle(&self) -> Result<procfs::process::Process> {
procfs::process::Process::new(self.pid)
.map_err(|_| Error(ErrorOrigin::OsLayer, ErrorKind::UnableToReadDir))
}
fn module_maps(&self) -> Result<Vec<procfs::process::MemoryMap>> {
let maps = self
.proc_handle()?
.maps()
.map_err(|_| Error(ErrorOrigin::OsLayer, ErrorKind::UnableToReadDir))?
.memory_maps;
Ok(maps
.into_iter()
.filter(|map| matches!(map.pathname, MMapPath::Path(_)))
.coalesce(|m1, m2| {
if m1.address.1 == m2.address.0
// When the file gets mapped in memory, offsets change.
// && m2.offset - m1.offset == m1.address.1 - m1.address.0
&& m1.dev == m2.dev
&& m1.inode == m2.inode
{
Ok(procfs::process::MemoryMap {
address: (m1.address.0, m2.address.1),
perms: MMPermissions::NONE,
offset: m1.offset,
dev: m1.dev,
inode: m1.inode,
pathname: m1.pathname,
extension: MMapExtension::default(),
})
} else {
Err((m1, m2))
}
})
.collect())
}
pub fn try_new(info: ProcessInfo) -> Result<Self> {
Ok(Self {
virt_mem: ProcessVirtualMemory::new(&info),
pid: info.pid as pid_t,
info,
})
}
pub fn mmap_path_to_name_string(path: &MMapPath) -> ReprCString {
match path {
MMapPath::Path(buf) => buf
.file_name()
.and_then(|o| o.to_str())
.unwrap_or("unknown")
.into(),
MMapPath::Heap => "[heap]".into(),
MMapPath::Stack => "[stack]".into(),
MMapPath::TStack(_) => "[tstack]".into(),
MMapPath::Vdso => "[vdso]".into(),
MMapPath::Vvar => "[vvar]".into(),
MMapPath::Vsyscall => "[vsyscall]".into(),
MMapPath::Rollup => "[rollup]".into(),
MMapPath::Anonymous => "[anonymous]".into(),
MMapPath::Vsys(_) => "[vsys]".into(),
MMapPath::Other(s) => s.as_str().into(),
}
}
pub fn mmap_path_to_path_string(path: &MMapPath) -> ReprCString {
match path {
MMapPath::Path(buf) => buf.to_str().unwrap_or("unknown").into(),
MMapPath::Heap => "[heap]".into(),
MMapPath::Stack => "[stack]".into(),
MMapPath::TStack(_) => "[tstack]".into(),
MMapPath::Vdso => "[vdso]".into(),
MMapPath::Vvar => "[vvar]".into(),
MMapPath::Vsyscall => "[vsyscall]".into(),
MMapPath::Rollup => "[rollup]".into(),
MMapPath::Anonymous => "[anonymous]".into(),
MMapPath::Vsys(_) => "[vsys]".into(),
MMapPath::Other(s) => s.as_str().into(),
}
}
#[cfg(memflow_plugin_api = "2")]
fn collect_envars(&self) -> Result<Vec<EnvVarInfo>> {
let path = format!("/proc/{}/environ", self.pid);
let data = std::fs::read(path)
.map_err(|_| Error(ErrorOrigin::OsLayer, ErrorKind::EnvarNotFound))?;
let mut out = Vec::new();
for entry in data.split(|b| *b == 0).filter(|entry| !entry.is_empty()) {
let entry = String::from_utf8_lossy(entry);
if let Some((name, value)) = entry.split_once('=') {
out.push(EnvVarInfo {
name: ReprCString::from(name),
value: ReprCString::from(value),
address: Address::NULL,
arch: self.info.proc_arch,
});
}
}
Ok(out)
}
}
cglue_impl_group!(LinuxProcess, ProcessInstance, {});
cglue_impl_group!(LinuxProcess, IntoProcessInstance, {});
impl Process for LinuxProcess {
/// Walks the process' module list and calls the provided callback for each module structure
/// address
///
/// # Arguments
/// * `target_arch` - sets which architecture to retrieve the modules for (if emulated). Choose
/// between `Some(ProcessInfo::sys_arch())`, and `Some(ProcessInfo::proc_arch())`. `None` for all.
/// * `callback` - where to pass each matching module to. This is an opaque callback.
fn module_address_list_callback(
&mut self,
target_arch: Option<&ArchitectureIdent>,
mut callback: ModuleAddressCallback,
) -> Result<()> {
let module_maps = self.module_maps()?;
module_maps
.iter()
.enumerate()
.filter(|_| target_arch.is_none() || Some(&self.info().sys_arch) == target_arch)
.take_while(|(i, _)| {
callback.call(ModuleAddressInfo {
address: Address::from(*i as u64),
arch: self.info.proc_arch,
})
})
.for_each(|_| {});
Ok(())
}
/// Retrieves a module by its structure address and architecture
///
/// # Arguments
/// * `address` - address where module's information resides in
/// * `architecture` - architecture of the module. Should be either `ProcessInfo::proc_arch`, or `ProcessInfo::sys_arch`.
fn module_by_address(
&mut self,
address: Address,
architecture: ArchitectureIdent,
) -> Result<ModuleInfo> {
if architecture != self.info.sys_arch {
return Err(Error(ErrorOrigin::OsLayer, ErrorKind::NotFound));
}
let module_maps = self.module_maps()?;
module_maps
.get(address.to_umem() as usize)
.map(|map| ModuleInfo {
address,
parent_process: self.info.address,
base: Address::from(map.address.0),
size: (map.address.1 - map.address.0) as umem,
name: Self::mmap_path_to_name_string(&map.pathname),
path: Self::mmap_path_to_path_string(&map.pathname),
arch: self.info.sys_arch,
})
.ok_or(Error(ErrorOrigin::OsLayer, ErrorKind::NotFound))
}
fn module_import_list_callback(
&mut self,
info: &ModuleInfo,
callback: ImportCallback,
) -> Result<()> {
memflow::os::util::module_import_list_callback(&mut self.virt_mem, info, callback)
}
fn module_export_list_callback(
&mut self,
info: &ModuleInfo,
callback: ExportCallback,
) -> Result<()> {
memflow::os::util::module_export_list_callback(&mut self.virt_mem, info, callback)
}
fn module_section_list_callback(
&mut self,
info: &ModuleInfo,
callback: SectionCallback,
) -> Result<()> {
memflow::os::util::module_section_list_callback(&mut self.virt_mem, info, callback)
}
/// Retrieves address of the primary module structure of the process
///
/// This will generally be for the initial executable that was run
fn primary_module_address(&mut self) -> Result<Address> {
// TODO: Is it always 0th mod?
Ok(Address::from(0))
}
/// Retrieves the process info
fn info(&self) -> &ProcessInfo {
&self.info
}
/// Retrieves the state of the process
fn state(&mut self) -> ProcessState {
ProcessState::Unknown
}
/// Changes the dtb this process uses for memory translations.
/// This function serves no purpose in memflow-native.
fn set_dtb(&mut self, _dtb1: Address, _dtb2: Address) -> Result<()> {
Ok(())
}
fn mapped_mem_range(
&mut self,
gap_size: imem,
start: Address,
end: Address,
out: MemoryRangeCallback,
) {
if let Ok(proc) = self.proc_handle() {
if let Ok(maps) = proc
.maps()
.map_err(|_| Error(ErrorOrigin::OsLayer, ErrorKind::UnableToReadDir))
{
maps.memory_maps
.into_iter()
.filter(|map| {
Address::from(map.address.1) > start && Address::from(map.address.0) < end
})
.filter(|m| m.perms.contains(MMPermissions::READ))
.map(|map| {
(
Address::from(map.address.0),
(map.address.1 - map.address.0) as umem,
PageType::empty()
.noexec(!map.perms.contains(MMPermissions::EXECUTE))
.write(map.perms.contains(MMPermissions::WRITE)),
)
})
.map(|(s, sz, perms)| {
if s < start {
let diff = start - s;
(start, sz - diff as umem, perms)
} else {
(s, sz, perms)
}
})
.map(|(s, sz, perms)| {
if s + sz > end {
let diff = (s + sz) - end;
(s, sz - diff as umem, perms)
} else {
(s, sz, perms)
}
})
.coalesce(|a, b| {
if gap_size >= 0 && a.0 + a.1 + gap_size as umem >= b.0 && a.2 == b.2 {
Ok((a.0, (b.0 - a.0) as umem + b.1, a.2))
} else {
Err((a, b))
}
})
.map(<_>::into)
.feed_into(out);
}
}
}
#[cfg(memflow_plugin_api = "2")]
fn envar_list_callback(
&mut self,
target_arch: Option<&ArchitectureIdent>,
mut callback: EnvVarCallback,
) -> Result<()> {
if let Some(arch) = target_arch {
if *arch != self.info.proc_arch {
return Ok(());
}
}
for envar in self.collect_envars()? {
if !callback.call(envar) {
break;
}
}
Ok(())
}
#[cfg(memflow_plugin_api = "2")]
fn environment_block_address(&mut self, _architecture: ArchitectureIdent) -> Result<Address> {
// Linux does not expose a stable public env-block pointer through procfs.
Ok(Address::NULL)
}
#[cfg(memflow_plugin_api = "2")]
fn envar_list_from_address(
&mut self,
_env_block: Address,
architecture: ArchitectureIdent,
callback: EnvVarCallback,
) -> Result<()> {
self.envar_list_callback(Some(&architecture), callback)
}
}
impl MemoryView for LinuxProcess {
fn read_raw_iter(&mut self, data: ReadRawMemOps) -> Result<()> {
self.virt_mem.read_raw_iter(data)
}
fn write_raw_iter(&mut self, data: WriteRawMemOps) -> Result<()> {
self.virt_mem.write_raw_iter(data)
}
fn metadata(&self) -> MemoryViewMetadata {
self.virt_mem.metadata()
}
}