vminer 0.1.0

Virtual Machine Introspection library
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#![allow(clippy::too_many_arguments)]

use std::{
    fs,
    io::{self, BufRead, Read},
    os::unix::{net::UnixListener, prelude::*},
    thread,
};
use vmc::{ResultExt, VmError, VmResult};

mod ptrace;

// Architecture-dependant code goes in these module

#[cfg_attr(target_arch = "x86_64", path = "x86_64.rs")]
#[cfg_attr(target_arch = "aarch64", path = "aarch64.rs")]
mod arch;

const LIB_PATH: &[u8] = b"/usr/lib/libvminer_kvm_patch.so\0";
const FUN_NAME: &[u8] = b"vminer_payload\0";
const TOTAL_LEN: usize = LIB_PATH.len() + FUN_NAME.len();

/// Finds the loading address of a library's text in a process' address space
fn find_lib(pid: libc::pid_t, name: &str) -> VmResult<u64> {
    let path = format!("/proc/{pid}/maps");
    let file = fs::File::open(&path)?;
    let file = io::BufReader::new(file);

    for line in file.lines() {
        let line = line?;

        if line.contains(name) && line.contains("r-xp ") {
            let end = line.find('-').unwrap();
            return u64::from_str_radix(&line[..end], 16)
                .with_context(|| format!("failed to parse {path}"));
        }
    }

    Err(VmError::new(format!(
        "failed to find address of \"{name}\""
    )))
}

/// Calls `dlerror` in a traced process
///
/// This is quite useful for debugging, but hopefully should never get called
#[cold]
fn get_dlerror(tracee: &ptrace::Tracee, dlerror: u64) -> VmError {
    let error: VmResult<_> = (|| {
        // char *error = dlerror();
        let mut addr = tracee.funcall0(dlerror)?;

        if addr == 0 {
            return Ok(String::from("Success"));
        }

        let mut error = Vec::with_capacity(128);
        let mut buf = [0];

        loop {
            tracee.peek_data(addr, &mut buf)?;

            match buf {
                [0] => break,
                [n] => error.push(n),
            }
            addr = addr.wrapping_add(1);
        }

        Ok(String::from_utf8_lossy(&error).into_owned())
    })();
    match error {
        Ok(error) => VmError::new(error),
        Err(err) => VmError::with_context("failed to get dlerror", err),
    }
}

/// Gets `errno` from a traced process
///
/// This is quite useful for debugging, but hopefully should never get called
#[cold]
fn get_errno(tracee: &ptrace::Tracee, errno: u64) -> VmError {
    let errno: VmResult<_> = (|| {
        // int *errno = __errno_location();
        let addr = tracee.funcall0(errno)?;

        let mut errno: libc::c_int = 0;
        tracee.peek_data(addr, bytemuck::bytes_of_mut(&mut errno))?;
        Ok(errno)
    })();
    match errno {
        Ok(errno) => io::Error::from_raw_os_error(errno).into(),
        Err(err) => VmError::with_context("failed to get errno", err),
    }
}

struct OnDrop<F: FnMut()>(F);

impl<F: FnMut()> Drop for OnDrop<F> {
    fn drop(&mut self) {
        (self.0)();
    }
}

/// Attach to a process, and make it execute our payload
#[allow(clippy::fn_to_numeric_cast)]
fn attach(pid: libc::pid_t, fds: &[i32]) -> VmResult<()> {
    // Find remote function addresses so we can call them.
    // Use our own functions to get the offset within the lib, and read /proc
    // to bypass the ASLR.
    let our_libc = find_lib(std::process::id() as _, "libc.so")?;
    let their_libc = find_lib(pid, "libc.so")?;

    let their_dlopen = their_libc + (libc::dlopen as u64 - our_libc);
    let their_dlclose = their_libc + (libc::dlclose as u64 - our_libc);
    let their_dlsym = their_libc + (libc::dlsym as u64 - our_libc);
    let their_dlerror = their_libc + (libc::dlerror as u64 - our_libc);

    let their_mmap = their_libc + (libc::mmap as u64 - our_libc);
    let their_errno = their_libc + (libc::__errno_location as u64 - our_libc);

    let tracee = ptrace::Tracee::attach(pid).context("failed to attach to KVM")?;
    log::trace!("Attached to KVM");

    // mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
    let mmap_addr = tracee.funcall6(
        their_mmap,
        0,
        0x1000,
        (libc::PROT_READ) as _,
        (libc::MAP_PRIVATE | libc::MAP_ANONYMOUS) as _,
        -1i64 as _,
        0,
    )?;
    if mmap_addr as i32 == -1 {
        let err = get_errno(&tracee, their_errno);
        return Err(VmError::with_context("remote mmap failed", err));
    }
    log::trace!("mmap at 0x{mmap_addr:x}");

    // Copy arguments of dlopen and dlsym
    let mut buffer = [0u8; TOTAL_LEN];
    buffer[..LIB_PATH.len()].copy_from_slice(LIB_PATH);
    buffer[LIB_PATH.len()..].copy_from_slice(FUN_NAME);

    tracee.poke_data(mmap_addr, &buffer)?;

    // void *handle = dlopen(LIB_PATH, RTLD_NOW);
    let handle = tracee.funcall2(their_dlopen, mmap_addr, libc::RTLD_NOW as _)?;
    if handle == 0 {
        let err = get_dlerror(&tracee, their_dlerror);
        return Err(VmError::with_context("remote dlopen failed", err));
    }
    log::trace!("dlopen handle at 0x{handle:x}");

    let _do_dlclose = OnDrop(|| {
        // dlclose(handle);
        match tracee.funcall1(their_dlclose, handle) {
            Ok(0) => log::trace!("dlclose"),
            Ok(_) => {
                let err = get_dlerror(&tracee, their_dlerror);
                log::error!("Remote dlclose failed: {err}");
            }
            Err(err) => log::error!("Failed to call dlclose: {err}"),
        }
    });

    // payload = dlsym(handle, FUN_NAME);
    let payload = tracee.funcall2(their_dlsym, handle, mmap_addr + LIB_PATH.len() as u64)?;
    if payload == 0 {
        let err = get_dlerror(&tracee, their_dlerror);
        return Err(VmError::with_context("remote dlsym failed", err));
    }
    log::trace!("payload at 0x{payload:x}");

    // payload(fds)
    tracee.poke_data(mmap_addr, bytemuck::cast_slice(fds))?;

    let error = tracee.funcall2(payload, mmap_addr, fds.len() as u64)? as i32;
    if error != 0 {
        let err = io::Error::from_raw_os_error(error);
        return Err(VmError::with_context("payload failed", err));
    }

    Ok(())
}

/// Guess KVM vCPU file descriptors from their names in `/proc`
fn get_vcpus_fds(pid: libc::pid_t) -> VmResult<Vec<i32>> {
    fn read_one(entry: io::Result<fs::DirEntry>) -> Option<i32> {
        let (path, fd_name) = entry
            .and_then(|entry| {
                let path = entry.path();
                let fd_name = path.read_link()?;
                Ok((path, fd_name))
            })
            .map_err(|e| log::warn!("Failed to read fd: {}", e))
            .ok()?;

        let fd_name = fd_name.to_str()?;

        // KVM vCPUs fds look like "anon_inode:kvm-vcpu:1"
        if fd_name.starts_with("anon_inode:kvm-vcpu:") {
            let num = path.file_name()?.to_str()?;
            return num.parse().ok();
        }

        None
    }

    let fds = fs::read_dir(format!("/proc/{pid}/fd"))?
        .filter_map(read_one)
        .collect();
    log::debug!("Found KVM vCPU files: {fds:?}");
    Ok(fds)
}

fn start_listener(
    socket_path: &str,
    fds: &[i32],
) -> VmResult<thread::JoinHandle<VmResult<Vec<arch::Vcpu>>>> {
    let fds_len = fds.len();

    let _ = fs::remove_file(socket_path);
    let listener = UnixListener::bind(socket_path).context("failed to bind listener socket")?;
    // FIXME: is 777 mode really required ?
    fs::set_permissions(socket_path, fs::Permissions::from_mode(0o777))?;

    Ok(thread::spawn(move || {
        let mut registers = bytemuck::Zeroable::zeroed();
        let mut special_registers = bytemuck::Zeroable::zeroed();
        let mut other_registers = bytemuck::Zeroable::zeroed();

        let (mut socket, _) = listener.accept()?;

        (0..fds_len)
            .map(|_| {
                socket.read_exact(bytemuck::bytes_of_mut(&mut registers))?;
                socket.read_exact(bytemuck::bytes_of_mut(&mut special_registers))?;
                socket.read_exact(bytemuck::bytes_of_mut(&mut other_registers))?;
                Ok(arch::Vcpu {
                    registers,
                    special_registers,
                    other_registers,
                })
            })
            .collect()
    }))
}

fn get_regs(pid: libc::pid_t) -> VmResult<Vec<arch::Vcpu>> {
    let fds = get_vcpus_fds(pid)?;
    let socket_path = "/tmp/get_fds";
    let handle = start_listener(socket_path, &fds)?;

    attach(pid, &fds)?;
    log::info!("Payload succeded");

    let regs = handle.join().unwrap()?;
    Ok(regs)
}

pub struct Kvm {
    mem: vmc::mem::MemRemap<vmc::mem::File>,
    vcpus: Vec<arch::Vcpu>,
}

impl Kvm {
    /// Parse /proc/pid/maps file to find the adress of the VM memory
    ///
    /// This is pretty sure to be the largest mapping
    fn find_memory(pid: libc::pid_t) -> VmResult<vmc::mem::File> {
        let mut maps = io::BufReader::new(fs::File::open(format!("/proc/{pid}/maps"))?);
        let mut line = String::with_capacity(200);

        let mut map_guess = 0;
        let mut map_size = 0;

        while maps.read_line(&mut line)? != 0 {
            (|| {
                let i = line.find('-')?;
                let (start_addr, line) = line.split_at(i);
                let start_addr = u64::from_str_radix(start_addr, 16).ok()?;
                let i = line.find(' ')?;

                let (end_addr, line) = line.split_at(i);
                let end_addr = u64::from_str_radix(&end_addr[1..], 16).ok()?;

                // Avoid loaded libs
                if line.contains("/usr/") {
                    return None;
                }

                let cur_size = end_addr - start_addr;
                if cur_size > map_size {
                    map_size = cur_size;
                    map_guess = start_addr;
                }

                Some(())
            })();

            line.clear();
        }

        if map_guess == 0 {
            return Err(VmError::new("failed to find VM memory"));
        }

        log::debug!("Found KVM memory of size 0x{map_size:x} at address 0x{map_guess:x}",);

        Ok(vmc::mem::File::open(
            format!("/proc/{pid}/mem"),
            map_guess,
            map_guess + map_size,
        )?)
    }

    pub fn connect(pid: libc::pid_t) -> VmResult<Kvm> {
        Self::create(pid, |size| {
            (
                vec![vmc::mem::MemoryMap {
                    start: vmc::PhysicalAddress(0),
                    end: vmc::PhysicalAddress(size),
                }],
                vec![vmc::PhysicalAddress(0)],
            )
        })
    }

    pub fn with_default_qemu_mappings(pid: libc::pid_t) -> VmResult<Kvm> {
        let default_qemu_mappings = |size| {
            if cfg!(target_arch = "x86_64") {
                if size <= 2 << 30 {
                    (
                        vec![vmc::mem::MemoryMap {
                            start: vmc::PhysicalAddress(0),
                            end: vmc::PhysicalAddress(size),
                        }],
                        vec![vmc::PhysicalAddress(0)],
                    )
                } else {
                    (
                        vec![
                            vmc::mem::MemoryMap {
                                start: vmc::PhysicalAddress(0),
                                end: vmc::PhysicalAddress(2 << 30),
                            },
                            vmc::mem::MemoryMap {
                                start: vmc::PhysicalAddress(4 << 30),
                                end: vmc::PhysicalAddress(size + (2 << 30)),
                            },
                        ],
                        vec![vmc::PhysicalAddress(0), vmc::PhysicalAddress(2 << 30)],
                    )
                }
            } else if cfg!(target_arch = "aarch64") {
                (
                    vec![vmc::mem::MemoryMap {
                        start: vmc::PhysicalAddress(1 << 30),
                        end: vmc::PhysicalAddress(size + (1 << 30)),
                    }],
                    vec![vmc::PhysicalAddress(0)],
                )
            } else {
                unreachable!()
            }
        };

        Self::create(pid, default_qemu_mappings)
    }

    pub fn with_memory_mappings(
        pid: libc::pid_t,
        mappings: Vec<vmc::mem::MemoryMap>,
        remap_at: Vec<vmc::PhysicalAddress>,
    ) -> VmResult<Kvm> {
        Self::create(pid, |_| (mappings, remap_at))
    }

    fn create<F>(pid: libc::pid_t, make_mappings: F) -> VmResult<Kvm>
    where
        F: FnOnce(u64) -> (Vec<vmc::mem::MemoryMap>, Vec<vmc::PhysicalAddress>),
    {
        let mem = Self::find_memory(pid)?;
        let vcpus = get_regs(pid)?;
        let (mappings, remap_at) = make_mappings(mem.size());
        let mem = vmc::mem::MemRemap::new(mem, mappings, remap_at);

        Ok(Kvm { mem, vcpus })
    }
}

impl vmc::Memory for Kvm {
    #[inline]
    fn memory_mappings(&self) -> &[vmc::mem::MemoryMap] {
        self.mem.memory_mappings()
    }

    #[inline]
    fn read_physical(
        &self,
        addr: vmc::PhysicalAddress,
        buf: &mut [u8],
    ) -> vmc::MemoryAccessResult<()> {
        self.mem.read_physical(addr, buf)
    }
}

impl vmc::HasVcpus for Kvm {
    type Arch = arch::Arch;

    fn arch(&self) -> Self::Arch {
        arch::Arch
    }

    fn vcpus_count(&self) -> usize {
        self.vcpus.len()
    }

    fn registers(
        &self,
        vcpu: vmc::VcpuId,
    ) -> vmc::VcpuResult<<Self::Arch as vmc::Architecture>::Registers> {
        Ok(self
            .vcpus
            .get(vcpu.0)
            .ok_or(vmc::VcpuError::InvalidId)?
            .registers)
    }

    fn special_registers(
        &self,
        vcpu: vmc::VcpuId,
    ) -> vmc::VcpuResult<<Self::Arch as vmc::Architecture>::SpecialRegisters> {
        Ok(self
            .vcpus
            .get(vcpu.0)
            .ok_or(vmc::VcpuError::InvalidId)?
            .special_registers)
    }

    fn other_registers(
        &self,
        vcpu: vmc::VcpuId,
    ) -> vmc::VcpuResult<<Self::Arch as vmc::Architecture>::OtherRegisters> {
        Ok(self
            .vcpus
            .get(vcpu.0)
            .ok_or(vmc::VcpuError::InvalidId)?
            .other_registers)
    }
}

impl vmc::Backend for Kvm {}