sys-lib 0.5.1

System facts for Rust services: OS, CPU, memory, disks, network, host, this process, virtualization, power, and device identity, container-aware by default, plus build info, instance info, and a process guard. No subprocesses, zero third-party dependencies.
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
//! Virtualization and containers: what this program runs inside.
//!
//! The answer is layered checks (CPUID, firmware strings, hypervisor files,
//! container markers, the Kubernetes service account, WSL's kernel release),
//! read once and cached. Every check that fired is named in
//! [`Virtualization::evidence`], as a constant string, never a raw value.
//!
//! **Detection is advisory.** Do not make security decisions on it: any of
//! these markers can be planted, and a guest can hide from all of them
//! (threat T14).
//!
//! ```
//! use sys_lib as sys;
//!
//! let v = sys::virt::detect();
//! if sys::virt::is_container() {
//!     println!("in {:?}; kubernetes: {}", v.container, v.kubernetes);
//! }
//! for e in &v.evidence {
//!     println!("  {} ({:?})", e.check, e.strength);
//! }
//! ```

use std::sync::OnceLock;

/// What this program runs inside. A container inside a VM sets both
/// `container` and `hypervisor`; inside WSL 2 both `hypervisor` and `wsl`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Virtualization {
    /// The innermost hypervisor visible to this machine.
    pub hypervisor: Option<Hypervisor>,
    /// The container runtime.
    pub container: Option<Container>,
    /// The WSL version, 1 or 2.
    pub wsl: Option<u8>,
    /// Running under Kubernetes. With [`Strength::Weak`] evidence only, this
    /// rests on an environment variable (E-87).
    pub kubernetes: bool,
    /// Every check that fired.
    pub evidence: Vec<Evidence>,
}

impl Virtualization {
    /// Records a check that fired.
    pub(crate) fn fired(&mut self, check: &'static str, strength: Strength) {
        self.evidence.push(Evidence { check, strength });
    }
}

/// A hypervisor.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Hypervisor {
    /// Linux KVM.
    Kvm,
    /// QEMU without KVM (TCG).
    Qemu,
    /// VMware.
    Vmware,
    /// Microsoft Hyper-V.
    HyperV,
    /// Xen.
    Xen,
    /// Oracle VirtualBox.
    VirtualBox,
    /// Parallels.
    Parallels,
    /// FreeBSD bhyve.
    Bhyve,
    /// ACRN.
    Acrn,
    /// QNX Hypervisor.
    Qnx,
    /// Apple's Virtualization framework.
    AppleVirtualization,
    /// AWS Nitro.
    AmazonNitro,
    /// Google Compute Engine.
    GoogleCompute,
    /// Bochs.
    Bochs,
    /// A hypervisor sys-lib cannot name.
    Other,
}

/// A container runtime.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Container {
    /// Docker.
    Docker,
    /// Podman.
    Podman,
    /// containerd or CRI-O, as Kubernetes runs them.
    Containerd,
    /// LXC or LXD.
    Lxc,
    /// `systemd-nspawn`.
    SystemdNspawn,
    /// OpenVZ or Virtuozzo.
    OpenVz,
    /// A container runtime sys-lib cannot name.
    Other,
}

/// One check that fired.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub struct Evidence {
    /// What was checked: a constant, never a value read from the machine.
    pub check: &'static str,
    /// How much it proves.
    pub strength: Strength,
}

/// How much a piece of evidence proves.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Strength {
    /// Set by the kernel, the firmware, or the runtime.
    Strong,
    /// An environment variable or the like: any parent process can set it.
    Weak,
}

/// A cloud vendor, from firmware strings only: sys-lib never contacts a
/// metadata service.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Cloud {
    /// Amazon Web Services.
    Aws,
    /// Google Cloud.
    Gcp,
    /// Microsoft Azure.
    Azure,
    /// Oracle Cloud.
    Oracle,
    /// Alibaba Cloud.
    Alibaba,
    /// Hetzner.
    Hetzner,
    /// DigitalOcean.
    DigitalOcean,
    /// Linode (Akamai).
    Linode,
    /// Vultr.
    Vultr,
    /// Scaleway.
    Scaleway,
    /// OVHcloud.
    Ovh,
    /// A cloud sys-lib cannot name.
    Other,
}

/// What this program runs inside. Cached: about 1 ns after the first call.
#[must_use]
pub fn detect() -> &'static Virtualization {
    static FACTS: OnceLock<Virtualization> = OnceLock::new();
    FACTS.get_or_init(crate::sys::virtualization)
}

/// Running on a hypervisor. Inside WSL 2 this and [`is_wsl`] are both true.
/// A Windows host with Hyper-V, WSL 2, or virtualization-based security
/// enabled is not a VM (E-84).
#[must_use]
pub fn is_vm() -> bool {
    detect().hypervisor.is_some()
}

/// Running in a container.
#[must_use]
pub fn is_container() -> bool {
    detect().container.is_some()
}

/// Running under WSL.
#[must_use]
pub fn is_wsl() -> bool {
    detect().wsl.is_some()
}

/// Running under Kubernetes (see [`Virtualization::kubernetes`]).
#[must_use]
pub fn is_kubernetes() -> bool {
    detect().kubernetes
}

/// The hypervisor, when there is one.
#[must_use]
pub fn hypervisor() -> Option<Hypervisor> {
    detect().hypervisor
}

/// The container runtime, when there is one.
#[must_use]
pub fn container() -> Option<Container> {
    detect().container
}

/// The cloud vendor, from firmware strings and the hypervisor leaves. Instance
/// types, regions, and zones need a network call and belong in a cloud crate.
#[must_use]
pub fn cloud() -> Option<Cloud> {
    static FACTS: OnceLock<Option<Cloud>> = OnceLock::new();
    *FACTS.get_or_init(crate::sys::cloud)
}

/// The firmware strings the detection reads, from sysfs or SMBIOS.
#[cfg_attr(target_vendor = "apple", allow(dead_code))]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Dmi<'a> {
    pub(crate) sys_vendor: &'a str,
    pub(crate) product: &'a str,
    pub(crate) product_version: &'a str,
    pub(crate) board_vendor: &'a str,
    pub(crate) bios_vendor: &'a str,
    pub(crate) asset_tag: &'a str,
}

impl Dmi<'_> {
    #[cfg_attr(target_vendor = "apple", allow(dead_code))]
    fn any(&self, needle: &str) -> bool {
        [
            self.sys_vendor,
            self.product,
            self.product_version,
            self.board_vendor,
            self.bios_vendor,
        ]
        .iter()
        .any(|s| contains_ignore_case(s, needle))
    }
}

#[cfg_attr(target_vendor = "apple", allow(dead_code))]
fn contains_ignore_case(haystack: &str, needle: &str) -> bool {
    haystack
        .as_bytes()
        .windows(needle.len())
        .any(|w| w.eq_ignore_ascii_case(needle.as_bytes()))
}

/// The hypervisor a CPUID vendor signature (leaf 0x40000000) names.
pub(crate) fn hypervisor_from_signature(signature: &[u8; 12]) -> Hypervisor {
    match signature {
        b"KVMKVMKVM\0\0\0" | b"Linux KVM Hv" => Hypervisor::Kvm,
        b"TCGTCGTCGTCG" => Hypervisor::Qemu,
        b"VMwareVMware" => Hypervisor::Vmware,
        b"Microsoft Hv" => Hypervisor::HyperV,
        b"XenVMMXenVMM" => Hypervisor::Xen,
        b"bhyve bhyve " => Hypervisor::Bhyve,
        b"QNXQVMBSQG\0\0" => Hypervisor::Qnx,
        b"ACRNACRNACRN" => Hypervisor::Acrn,
        b"Apple VZ\0\0\0\0" | b"VirtualApple" => Hypervisor::AppleVirtualization,
        b"VBoxVBoxVBox" => Hypervisor::VirtualBox,
        b" prl hyperv " | b" lrpepyh vr " => Hypervisor::Parallels,
        _ => Hypervisor::Other,
    }
}

/// The hypervisor firmware strings name. The clouds' own hypervisors are named
/// first: a Nitro or GCE guest also says KVM.
#[cfg_attr(target_vendor = "apple", allow(dead_code))]
pub(crate) fn hypervisor_from_dmi(dmi: &Dmi<'_>) -> Option<Hypervisor> {
    let table: &[(&str, Hypervisor)] = &[
        ("Amazon EC2", Hypervisor::AmazonNitro),
        ("Google Compute Engine", Hypervisor::GoogleCompute),
        ("Alibaba Cloud ECS", Hypervisor::Kvm),
        ("KubeVirt", Hypervisor::Kvm),
        ("OpenStack", Hypervisor::Kvm),
        ("KVM", Hypervisor::Kvm),
        ("QEMU", Hypervisor::Qemu),
        ("VMware", Hypervisor::Vmware),
        ("innotek GmbH", Hypervisor::VirtualBox),
        ("VirtualBox", Hypervisor::VirtualBox),
        ("Xen", Hypervisor::Xen),
        ("Bochs", Hypervisor::Bochs),
        ("Parallels", Hypervisor::Parallels),
        ("BHYVE", Hypervisor::Bhyve),
        ("Apple Virtualization", Hypervisor::AppleVirtualization),
    ];
    if let Some((_, h)) = table.iter().find(|(needle, _)| dmi.any(needle)) {
        return Some(*h);
    }
    // Hyper-V says only "Microsoft Corporation" and "Virtual Machine": physical
    // Surface machines say the first too.
    let microsoft = contains_ignore_case(dmi.sys_vendor, "Microsoft Corporation");
    (microsoft && contains_ignore_case(dmi.product, "Virtual Machine"))
        .then_some(Hypervisor::HyperV)
}

/// The cloud firmware strings or the hypervisor name.
#[cfg_attr(target_vendor = "apple", allow(dead_code))]
pub(crate) fn cloud_from_dmi(dmi: &Dmi<'_>, hypervisor: Option<Hypervisor>) -> Option<Cloud> {
    let table: &[(&str, Cloud)] = &[
        ("Amazon EC2", Cloud::Aws),
        ("Amazon", Cloud::Aws),
        ("Google Compute Engine", Cloud::Gcp),
        ("Google", Cloud::Gcp),
        ("Alibaba Cloud", Cloud::Alibaba),
        ("Hetzner", Cloud::Hetzner),
        ("DigitalOcean", Cloud::DigitalOcean),
        ("Linode", Cloud::Linode),
        ("Akamai", Cloud::Linode),
        ("Vultr", Cloud::Vultr),
        ("Scaleway", Cloud::Scaleway),
        ("OVHcloud", Cloud::Ovh),
    ];
    if let Some((_, c)) = table.iter().find(|(needle, _)| dmi.any(needle)) {
        return Some(*c);
    }
    // Azure's asset tag is a fixed string; Oracle's is too.
    if dmi.asset_tag == "7783-7084-3265-9085-8269-3286-77" {
        return Some(Cloud::Azure);
    }
    if dmi.asset_tag == "OracleCloud.com" {
        return Some(Cloud::Oracle);
    }
    match hypervisor {
        Some(Hypervisor::AmazonNitro) => Some(Cloud::Aws),
        Some(Hypervisor::GoogleCompute) => Some(Cloud::Gcp),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_virt_signatures() {
        assert_eq!(
            hypervisor_from_signature(b"KVMKVMKVM\0\0\0"),
            Hypervisor::Kvm
        );
        assert_eq!(hypervisor_from_signature(b"Linux KVM Hv"), Hypervisor::Kvm);
        assert_eq!(
            hypervisor_from_signature(b"Microsoft Hv"),
            Hypervisor::HyperV
        );
        assert_eq!(
            hypervisor_from_signature(b"VMwareVMware"),
            Hypervisor::Vmware
        );
        assert_eq!(
            hypervisor_from_signature(b"something new"[..12].try_into().unwrap()),
            Hypervisor::Other
        );
    }

    #[test]
    fn test_virt_dmi_strings() {
        let dmi = |product, sys_vendor| Dmi {
            product,
            sys_vendor,
            ..Dmi::default()
        };
        assert_eq!(
            hypervisor_from_dmi(&dmi("Standard PC (Q35 + ICH9, 2009)", "QEMU")),
            Some(Hypervisor::Qemu)
        );
        assert_eq!(
            hypervisor_from_dmi(&dmi("VirtualBox", "innotek GmbH")),
            Some(Hypervisor::VirtualBox)
        );
        assert_eq!(
            hypervisor_from_dmi(&dmi("m5.large", "Amazon EC2")),
            Some(Hypervisor::AmazonNitro)
        );
        assert_eq!(
            hypervisor_from_dmi(&dmi("Virtual Machine", "Microsoft Corporation")),
            Some(Hypervisor::HyperV)
        );
        // A real Surface is not a VM.
        assert_eq!(
            hypervisor_from_dmi(&dmi("Surface Laptop 5", "Microsoft Corporation")),
            None
        );
        assert_eq!(hypervisor_from_dmi(&dmi("ThinkPad X1", "LENOVO")), None);
    }

    #[test]
    fn test_virt_clouds() {
        let aws = Dmi {
            sys_vendor: "Amazon EC2",
            ..Dmi::default()
        };
        assert_eq!(cloud_from_dmi(&aws, None), Some(Cloud::Aws));
        let azure = Dmi {
            sys_vendor: "Microsoft Corporation",
            product: "Virtual Machine",
            asset_tag: "7783-7084-3265-9085-8269-3286-77",
            ..Dmi::default()
        };
        assert_eq!(
            cloud_from_dmi(&azure, Some(Hypervisor::HyperV)),
            Some(Cloud::Azure)
        );
        let hyperv = Dmi {
            asset_tag: "",
            ..azure
        };
        assert_eq!(cloud_from_dmi(&hyperv, Some(Hypervisor::HyperV)), None);
        assert_eq!(cloud_from_dmi(&Dmi::default(), None), None);
    }

    #[test]
    fn test_virt_live_is_consistent() {
        let v = detect();
        assert!(std::ptr::eq(v, detect()));
        assert_eq!(is_vm(), v.hypervisor.is_some());
        assert_eq!(is_container(), v.container.is_some());
        if let Some(w) = v.wsl {
            assert!(matches!(w, 1 | 2));
        }
        // Evidence names constants, one per check.
        for e in &v.evidence {
            assert!(!e.check.is_empty());
        }
    }
}