Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ crossterm = "0.29.0"
dashmap = "6.1.0"
subtle = "2.6.1"
regex-lite = "0.1.8"
sysinfo = { version = "0.39.0", default-features = false, features = ["system", "disk", "network"] }
sysinfo = { version = "0.40.1", default-features = false, features = ["system", "disk", "network"] }
x509-cert = { version = "0.3.0", features = ["builder"] }
signature = "3.0.0"
sha2 = "0.11.0"
Expand Down
15 changes: 9 additions & 6 deletions crates/rite-stdlib/src/verification/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub enum HostInfoScope {
pub fn gather_host_info(scope: HostInfoScope) -> HostInfo {
// These are associated functions in `sysinfo`; no `System` instance or
// refresh is needed, so the `Basic` scope stays cheap.
let os = System::name();
let os_version = System::long_os_version();
let os = System::name().ok();
let os_version = System::long_os_version().ok();
let hostname = get_hostname();

let mut info = HostInfo {
Expand All @@ -57,7 +57,7 @@ pub fn gather_host_info(scope: HostInfoScope) -> HostInfo {
};

if scope == HostInfoScope::Full {
info.kernel_version = System::kernel_version();
info.kernel_version = System::kernel_version().ok();
info.cpu_model = cpu_model();
info.machine_id = get_machine_id();
info.hardening = Some(collect_security_features());
Expand All @@ -69,10 +69,11 @@ pub fn gather_host_info(scope: HostInfoScope) -> HostInfo {
/// Gather the live device environment. MVP: block devices.
#[must_use]
pub fn gather_environment() -> Environment {
let disks = sysinfo::Disks::new_with_refreshed_list();
// Failure handled as in `network_snapshot`.
let disks = sysinfo::Disks::new_with_refreshed_list().ok();
let disks = disks
.list()
.iter()
.flatten()
.map(|d| {
let file_system = osstr_opt(d.file_system());
let kind = match d.kind() {
Expand Down Expand Up @@ -104,6 +105,7 @@ fn osstr_opt(value: &std::ffi::OsStr) -> Option<String> {

fn get_hostname() -> String {
System::host_name()
.ok()
.or_else(|| std::env::var("HOSTNAME").ok())
.unwrap_or_else(|| "unknown".to_string())
}
Expand All @@ -112,7 +114,8 @@ fn cpu_model() -> Option<String> {
// Refresh CPUs only: the brand is all we need, so skip the
// process/memory scan a full `System::new_all()` would run.
let sys =
System::new_with_specifics(RefreshKind::nothing().with_cpu(CpuRefreshKind::everything()));
System::new_with_specifics(RefreshKind::nothing().with_cpu(CpuRefreshKind::everything()))
.ok()?;
sys.cpus()
.first()
// AMD brand strings end in padding spaces.
Expand Down
47 changes: 31 additions & 16 deletions crates/rite-stdlib/src/verification/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! and connects it, which only queries the routing table. The sysfs probes
//! read under a root given as a parameter (see `probe_fixtures`).

use std::ffi::OsStr;
use std::io::ErrorKind;
#[cfg(any(target_os = "linux", test))]
use std::path::Path;
Expand Down Expand Up @@ -37,16 +38,24 @@ const ARPHRD_RAWIP: u16 = 519;
/// Take a snapshot of the network state.
#[must_use]
pub fn network_snapshot() -> NetworkSnapshot {
let networks = sysinfo::Networks::new_with_refreshed_list();
let mut interfaces: Vec<NetworkInterface> = networks
.list()
// `sysinfo` fails only on systems it does not support, none of them a
// Rite target.
let networks = sysinfo::Networks::new_with_refreshed_list().ok();
let mut listed: Vec<_> = networks
.iter()
.flatten()
.filter(|(name, data)| is_interface(name) && !is_loopback(name, data.ip_networks()))
.collect();
// Sorted on the OS name, so the order holds even if two names that are
// not UTF-8 read the same once converted.
listed.sort_by_key(|(name, _)| *name);
let interfaces: Vec<NetworkInterface> = listed
.into_iter()
.map(|(name, data)| {
let (kind, carrier) = link(name);
let mac = data.mac_address();
NetworkInterface {
name: name.clone(),
name: name.to_string_lossy().into_owned(),
kind,
state: data.operational_state().to_string(),
carrier,
Expand All @@ -61,7 +70,6 @@ pub fn network_snapshot() -> NetworkSnapshot {
}
})
.collect();
interfaces.sort_by(|a, b| a.name.cmp(&b.name));
#[cfg(target_os = "linux")]
let radios = radios(
Path::new("/sys/class/rfkill"),
Expand All @@ -79,8 +87,9 @@ pub fn network_snapshot() -> NetworkSnapshot {

/// Whether an interface is loopback: named so, or with only loopback
/// addresses.
fn is_loopback(name: &str, addresses: &[sysinfo::IpNetwork]) -> bool {
matches!(name, "lo" | "lo0")
fn is_loopback(name: &OsStr, addresses: &[sysinfo::IpNetwork]) -> bool {
name == "lo"
|| name == "lo0"
|| (!addresses.is_empty() && addresses.iter().all(|net| net.addr.is_loopback()))
}

Expand Down Expand Up @@ -111,22 +120,22 @@ fn has_route(bind: &str, target: &str) -> Option<bool> {
/// `/sys/class/net`, and the bonding driver keeps a file there
/// (`bonding_masters`); interfaces are directories.
#[cfg(target_os = "linux")]
fn is_interface(name: &str) -> bool {
fn is_interface(name: &OsStr) -> bool {
Path::new("/sys/class/net").join(name).is_dir()
}

#[cfg(not(target_os = "linux"))]
fn is_interface(_name: &str) -> bool {
fn is_interface(_name: &OsStr) -> bool {
true
}

#[cfg(target_os = "linux")]
fn link(name: &str) -> (InterfaceKind, Option<bool>) {
fn link(name: &OsStr) -> (InterfaceKind, Option<bool>) {
sysfs_link(&Path::new("/sys/class/net").join(name))
}

#[cfg(not(target_os = "linux"))]
fn link(_name: &str) -> (InterfaceKind, Option<bool>) {
fn link(_name: &OsStr) -> (InterfaceKind, Option<bool>) {
(InterfaceKind::Unknown, None)
}

Expand Down Expand Up @@ -311,11 +320,17 @@ mod tests {
addr: addr.parse().expect("an address"),
prefix: 8,
};
assert!(is_loopback("lo", &[]));
assert!(is_loopback("lo0", &[net("127.0.0.1")]));
assert!(is_loopback("other", &[net("127.0.0.1"), net("::1")]));
assert!(!is_loopback("en0", &[]));
assert!(!is_loopback("en0", &[net("127.0.0.1"), net("192.168.1.2")]));
assert!(is_loopback(OsStr::new("lo"), &[]));
assert!(is_loopback(OsStr::new("lo0"), &[net("127.0.0.1")]));
assert!(is_loopback(
OsStr::new("other"),
&[net("127.0.0.1"), net("::1")]
));
assert!(!is_loopback(OsStr::new("en0"), &[]));
assert!(!is_loopback(
OsStr::new("en0"),
&[net("127.0.0.1"), net("192.168.1.2")]
));
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions crates/rite-stdlib/src/verification/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ pub fn detect_platform() -> Platform {
Platform {
os: OsIdentity {
id: Some(sysinfo::System::distribution_id()),
version: sysinfo::System::os_version(),
name: sysinfo::System::long_os_version(),
version: sysinfo::System::os_version().ok(),
name: sysinfo::System::long_os_version().ok(),
},
virtualization: guest_virtualization(),
..Platform::default()
Expand Down