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
/*
MIT License

Copyright (c) 2021 Philipp Schuster

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

//! Small `no_std`-lib that checks if the binary is running inside a QEMU virtual machine.
//! Only works on x86/x86_64 platform. There are no heap allocation required.
//!
//! Under the hood, this is a wrapper around the awesome crate https://crates.io/crates/raw-cpuid.

#![no_std]
#![deny(clippy::all)]
#![deny(rustdoc::all)]

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
compile_error!("This crate only works on the x86/x86_64-platform.");

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use raw_cpuid::{CpuId, Hypervisor};

/// Returns if the code is running inside a QEMU virtual machine.
/// Only works on x86/x86_64 platform.
///
/// Doesn't panic and in case something strange happens, it returns
/// `false` in favor of a `Result`, because these errors are absolutely
/// unlikely.
///
/// ## Example Usage
///
/// ```rust
/// # use runs_inside_qemu::runs_inside_qemu;
///
/// fn main() {
///     // If we are in QEMU, we use the nice "debugcon"-feature which maps
///     // the x86 I/O-port `0xe9` to stdout or a file.
///     if runs_inside_qemu() {
///         unsafe {
///             x86::io::outb(0xe9, b'H');
///             x86::io::outb(0xe9, b'e');
///             x86::io::outb(0xe9, b'l');
///             x86::io::outb(0xe9, b'l');
///             x86::io::outb(0xe9, b'o');
///             x86::io::outb(0xe9, b'\n');
///         }
///     }
/// }
/// ```
pub fn runs_inside_qemu() -> bool {
    let id = CpuId::new();

    // ########## CHECK 1 ##########
    // The `x86` library first checks if the Hypervisor flag is present in the `cpuid` features.
    // If yes, it reads the Hypervisor info leaf from `cpuid`.
    // Also see https://lwn.net/Articles/301888/)
    let hypervisor_info = id.get_hypervisor_info();
    if hypervisor_info.is_none() {
        // QEMU is a Hypervisor and no real machine => exit if this is None
        log::debug!("Hypervisor flag is not set, no Hypervisor info available!");
        return false;
    }
    let hypervisor_info = hypervisor_info.unwrap();

    // if this returns false, because the hypervisor ID can be "KVM",
    // we still could be executed by QEMU -> further checks needed
    if matches!(hypervisor_info.identify(), Hypervisor::QEMU) {
        log::debug!("QEMU is the direct hypervisor");
        return true;
    }

    // ########## CHECK 2 ##########
    // now check the extended CPU brand string (which is specific for QEMU)
    let brand_string = id.get_processor_brand_string();
    if brand_string.is_none() {
        log::debug!("CPU brand string not available, can't verify if code runs inside QEMU");
        return false;
    }
    let brand_string = brand_string.unwrap();
    let brand_string = brand_string.as_str();

    let is_qemu = brand_string.contains("QEMU");
    if is_qemu {
        // "QEMU Virtual CPU version 2.5+"
        log::debug!(
            "Runs inside QEMU with {:?} as accelerator",
            hypervisor_info.identify()
        );
    } else {
        log::debug!("CPU brand string is not the one from QEMU");
    }
    is_qemu
}