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
6 changes: 6 additions & 0 deletions .github/scripts/provision-kvm-host.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ ipv6_prefix="${ipv6_prefix:-2001:db8::/64}"
# carries IPv6 link-local addresses, so it must be on.
sudo sysctl -qw net.ipv6.conf.all.disable_ipv6=0 net.ipv6.conf.default.disable_ipv6=0
sudo sysctl -qw net.ipv4.ip_forward=1 net.ipv6.conf.all.forwarding=1
# The runner's Docker sets the iptables forward policy to drop, which drops VM
# traffic too (install.md: allow forwarding to and from vm-veth-*).
for ipt in iptables ip6tables; do
sudo "$ipt" -I FORWARD -i 'vm-veth+' -j ACCEPT
sudo "$ipt" -I FORWARD -o 'vm-veth+' -j ACCEPT
done

sudo mkdir -p /etc/hyper
sudo tee /etc/hyper/config.toml >/dev/null <<EOF
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ jobs:

# Root-gated cases are tagged by name: every test that needs root ends in
# `_as_root` and self-skips when run unprivileged. We select them by that
# suffix (via sudo -E so the runner's cargo/rustup home and PATH are kept)
# suffix (via sudo -E so the runner's PATH is kept; RUSTUP_HOME explicitly,
# since root's HOME is /root and its own toolchain there is 0700, which the
# tests that rebuild the helper as the invoking user cannot exec)
# so the root-only paths -- sys-test ok, fake-bin argv capture, mknod/chown
# jail build -- actually run. A new root test is picked up automatically by
# following the naming convention; no test-binary enumeration to maintain.
Expand All @@ -325,7 +327,7 @@ jobs:
# spawned helper's profile reach the collector.
- name: Gated tests as root + coverage
run: |
sudo -E env "PATH=$PATH" \
sudo -E env "PATH=$PATH" "RUSTUP_HOME=$HOME/.rustup" \
cargo llvm-cov nextest \
-p hyper-suidhelper -p hyperrs-fs -p hyperrs-firecracker -p hyperrs-linux -p hyperrs-process \
--features hyper-suidhelper/insecure_mode \
Expand Down
1 change: 1 addition & 0 deletions native/Cargo.lock

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

5 changes: 5 additions & 0 deletions native/guest-agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ tonic-prost-build = "0.14"

[dev-dependencies]
pretty_assertions = "1"
proptest = "1"
rstest = "0.28"
tokio-stream = "0.1"

[[test]]
name = "exec"
path = "tests/exec.rs"

[[test]]
name = "exec_properties"
path = "tests/exec_properties.rs"
97 changes: 97 additions & 0 deletions native/guest-agent/tests/exec_properties.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//! Laws of the guest side of `Exec` (`exec::run`), which the host's
//! `Hyper.exec/3` contract rests on:
//!
//! - argv round-trips: the program sees exactly the argument vector sent,
//! with no shell splitting, globbing or re-quoting;
//! - stdout and stderr are each captured byte-for-byte, arbitrary bytes and
//! outputs larger than a pipe buffer on both streams at once included;
//! - the exit status is the program's own `0..=255` code;
//! - a non-empty env *replaces* the environment: the program sees exactly
//! that map and nothing inherited.

use hyper_guest_agent::exec::run;
use proptest::collection::{btree_map, vec};
use proptest::prelude::*;
use std::collections::BTreeMap;
use std::io::Write;

fn sh(script: &str, args: &[String]) -> Vec<String> {
let mut argv = vec!["/bin/sh".into(), "-c".into(), script.into(), "sh".into()];
argv.extend_from_slice(args);
argv
}

fn arg() -> impl Strategy<Value = String> {
any::<String>().prop_map(|s| s.replace('\0', ""))
}

proptest! {
#![proptest_config(ProptestConfig::with_cases(64))]

#[test]
fn argv_reaches_the_program_unchanged(args in vec(arg(), 0..8)) {
let (code, out, _) = run(&sh(r#"for a; do printf '%s\0' "$a"; done"#, &args), &BTreeMap::new(), None);
prop_assert_eq!(code, 0);
let expected: Vec<u8> = args.iter().flat_map(|a| a.bytes().chain([0])).collect();
prop_assert_eq!(out, expected);
}

#[test]
fn exit_status_is_the_programs_own(n in 0u8..=255) {
let (code, _, _) = run(&sh(&format!("exit {n}"), &[]), &BTreeMap::new(), None);
prop_assert_eq!(code, i32::from(n));
}

#[test]
fn non_empty_env_is_exactly_what_the_program_sees(
env in btree_map("[A-Z_][A-Z0-9_]{0,12}", "[ -~]{0,24}", 1..8)
) {
let (code, out, _) = run(&["/usr/bin/env".into()], &env, None);
prop_assert_eq!(code, 0);
let seen: BTreeMap<String, String> = std::str::from_utf8(&out)
.unwrap()
.lines()
.map(|l| {
let (k, v) = l.split_once('=').unwrap();
(k.to_owned(), v.to_owned())
})
.collect();
prop_assert_eq!(seen, env);
}
}

proptest! {
#![proptest_config(ProptestConfig::with_cases(16))]

#[test]
fn both_streams_are_captured_byte_for_byte(
stdout in vec(any::<u8>(), 0..300_000),
stderr in vec(any::<u8>(), 0..300_000),
) {
let dir = std::env::temp_dir();
let tag = format!("{}-{}", std::process::id(), rand_tag());
let out_path = dir.join(format!("ga-out-{tag}"));
let err_path = dir.join(format!("ga-err-{tag}"));
std::fs::File::create(&out_path).unwrap().write_all(&stdout).unwrap();
std::fs::File::create(&err_path).unwrap().write_all(&stderr).unwrap();

let args = [
out_path.to_string_lossy().into_owned(),
err_path.to_string_lossy().into_owned(),
];
let (code, out, err) =
run(&sh(r#"cat "$1" & cat "$2" >&2; wait"#, &args), &BTreeMap::new(), None);
let _ = std::fs::remove_file(&out_path);
let _ = std::fs::remove_file(&err_path);

prop_assert_eq!(code, 0);
prop_assert!(out == stdout, "stdout differs: {} vs {} bytes", out.len(), stdout.len());
prop_assert!(err == stderr, "stderr differs: {} vs {} bytes", err.len(), stderr.len());
}
}

fn rand_tag() -> u64 {
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT: AtomicU64 = AtomicU64::new(0);
NEXT.fetch_add(1, Ordering::Relaxed)
}
79 changes: 79 additions & 0 deletions native/hyperrs-elfsum/tests/stamped_slot_runtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//! The end-to-end contract the helper's install-time verification rests on:
//! the digest `Elf::stamp` writes into a built executable is exactly what
//! `checksum!()` reports when that executable runs, and a verifier that
//! re-zeroes `.note.sum` and BLAKE3-hashes the file recomputes it. Laws:
//! * runtime report == stamped digest (the stamper patched the slot the
//! loader maps, and the read is not constant-folded to the link-time zero);
//! * re-zero + rehash of the stamped file == stamped digest;
//! * an unstamped executable reports all zeroes.
#![cfg(all(target_os = "linux", feature = "stamp"))]

use hyperrs_elfsum::Elf;
use pretty_assertions::assert_eq;
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::process::Command;

const CHILD: &str = "HYPER_ELFSUM_REPORT_SLOT";
const TEST: &str = "a_stamped_executable_reports_its_own_stamp_at_runtime";

fn hex(d: &[u8]) -> String {
d.iter().map(|b| format!("{b:02x}")).collect()
}

fn report(exe: &Path) -> String {
let out = Command::new(exe)
.args(["--exact", TEST, "--nocapture", "--test-threads=1"])
.env(CHILD, "1")
.output()
.unwrap();
assert!(out.status.success(), "{out:?}");
let stdout = String::from_utf8(out.stdout).unwrap();
stdout
.split_once("slot=")
.and_then(|(_, rest)| rest.get(..64))
.unwrap_or_else(|| panic!("child printed no slot: {stdout}"))
.to_string()
}

#[test]
fn a_stamped_executable_reports_its_own_stamp_at_runtime() {
if std::env::var_os(CHILD).is_some() {
println!("slot={}", hyperrs_elfsum::checksum!().hex());
return;
}
let dir = std::env::temp_dir().join(format!("elfsum-{}", std::process::id()));
fs::create_dir_all(&dir).unwrap();
let original = fs::read("/proc/self/exe").unwrap();

let unstamped = dir.join("unstamped");
fs::write(&unstamped, &original).unwrap();
fs::set_permissions(&unstamped, fs::Permissions::from_mode(0o755)).unwrap();
assert_eq!(report(&unstamped), "0".repeat(64));

let mut bytes = original.clone();
let digest = Elf::read(&mut bytes[..]).unwrap().stamp();
let stamped = dir.join("stamped");
fs::write(&stamped, &bytes).unwrap();
fs::set_permissions(&stamped, fs::Permissions::from_mode(0o755)).unwrap();

assert_eq!(report(&stamped), hex(&digest));

let changed: Vec<usize> = (0..bytes.len())
.filter(|&i| bytes[i] != original[i])
.collect();
let (first, last) = (changed[0], *changed.last().unwrap());
assert!(
last - first < digest.len(),
"stamp touched bytes outside one slot"
);
let at = (0..=bytes.len() - digest.len())
.find(|&i| bytes[i..i + digest.len()] == digest)
.unwrap();
let mut rezeroed = bytes.clone();
rezeroed[at..at + digest.len()].fill(0);
assert_eq!(*blake3::hash(&rezeroed).as_bytes(), digest);

fs::remove_dir_all(&dir).unwrap();
}
158 changes: 158 additions & 0 deletions native/hyperrs-firecracker/tests/jail_root_planted.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//! The jail `root/` is owned by the jail uid, so a Firecracker a hostile guest has taken over can
//! plant any entry there before the helper stages into it. Law: a name the jail uid planted where
//! the helper stages `vmlinux` or `rootfs` is never written through, retyped or re-owned.

use assert_fs::fixture::ChildPath;
use assert_fs::prelude::*;
use assert_fs::TempDir;
use hyperrs_firecracker::jailer::chroot::{JailLayout, JailRoot, KernelImage};
use hyperrs_firecracker::VmId;
use hyperrs_fs::dir::DirError;
use hyperrs_fs::file::{self, Any, IsBlockDevice, OpenFlags, Owner, SafeFile};
use hyperrs_fs::path::{IsAbsolute, SafePathBuf, StrictComponents};
use nix::errno::Errno;
use nix::unistd::{getgid, getuid};
use pretty_assertions::assert_eq;
use rstest::{fixture, rstest};
use std::assert_matches;
use std::fs;
use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
use std::path::{Path, PathBuf};

type Strict = SafePathBuf<IsAbsolute, StrictComponents>;

fn strict(p: impl AsRef<Path>) -> Strict {
p.as_ref().to_path_buf().try_into().unwrap()
}

fn vm() -> VmId {
"hostile".parse().unwrap()
}

struct Jail {
base: TempDir,
root: ChildPath,
}

impl Jail {
fn open_root(&self) -> JailRoot {
JailLayout::new(&strict(self.base.path()), &vm())
.open_root()
.unwrap()
}
}

#[fixture]
fn jail() -> Jail {
let base = TempDir::new().unwrap();
let root = base.child("firecracker/hostile/root");
root.create_dir_all().unwrap();
Jail { base, root }
}

fn any_block_device() -> Option<PathBuf> {
fs::read_dir("/dev")
.ok()?
.flatten()
.map(|e| e.path())
.find(|p| {
fs::symlink_metadata(p)
.map(|m| m.file_type().is_block_device())
.unwrap_or(false)
})
}

fn me() -> Owner {
Owner {
uid: getuid(),
gid: getgid(),
}
}

fn is_root() -> bool {
nix::unistd::geteuid().is_root()
}

#[rstest]
fn add_rootfs_over_a_planted_regular_file_refuses_and_leaves_it(jail: Jail) {
let Some(dev) = any_block_device() else {
eprintln!(
"SKIP add_rootfs_over_a_planted_regular_file_refuses_and_leaves_it: no block device"
);
return;
};
let planted = jail.root.child("rootfs");
planted.write_str("planted").unwrap();
fs::set_permissions(&planted, fs::Permissions::from_mode(0o644)).unwrap();
let device = SafeFile::<IsBlockDevice, Any, Any>::open(&strict(&dev), OpenFlags::PATH).unwrap();

let err = jail.open_root().add_rootfs(&device, me()).unwrap_err();

assert_matches!(err, DirError::Open(file::ValidationError::WrongFileType));
planted.assert("planted");
assert_eq!(fs::metadata(&planted).unwrap().mode() & 0o7777, 0o644);
}

#[rstest]
fn add_rootfs_over_a_planted_symlink_refuses_and_never_touches_its_target(jail: Jail) {
let Some(dev) = any_block_device() else {
eprintln!("SKIP add_rootfs_over_a_planted_symlink: no block device");
return;
};
let victim = jail.base.child("victim");
victim.write_str("victim").unwrap();
fs::set_permissions(&victim, fs::Permissions::from_mode(0o640)).unwrap();
jail.root.child("rootfs").symlink_to_file(&victim).unwrap();
let device = SafeFile::<IsBlockDevice, Any, Any>::open(&strict(&dev), OpenFlags::PATH).unwrap();

let err = jail.open_root().add_rootfs(&device, me()).unwrap_err();

assert_matches!(
err,
DirError::Open(file::ValidationError::Open(Errno::ELOOP))
);
victim.assert("victim");
assert_eq!(fs::metadata(&victim).unwrap().mode() & 0o7777, 0o640);
}

/// Root-only: unprivileged `linkat(AT_EMPTY_PATH)` is `ENOENT` before 6.10, so neither the
/// hard-link nor the `EXDEV` copy path would be reached.
#[rstest]
fn stage_kernel_never_writes_through_a_planted_vmlinux_symlink_as_root(
jail: Jail,
#[values(false, true)] cross_device: bool,
) {
if !is_root() {
eprintln!("SKIP stage_kernel_never_writes_through_a_planted_vmlinux_symlink_as_root");
return;
}
let src_dir = if cross_device {
match TempDir::new_in("/dev/shm") {
Ok(d) => d,
Err(_) => {
eprintln!("SKIP cross-device staging: no /dev/shm");
return;
}
}
} else {
TempDir::new_in(jail.base.path()).unwrap()
};
let src = src_dir.child("vmlinux");
src.write_str("kernel").unwrap();
fs::set_permissions(&src, fs::Permissions::from_mode(0o644)).unwrap();
let victim = jail.base.child("victim");
victim.write_str("victim").unwrap();
jail.root.child("vmlinux").symlink_to_file(&victim).unwrap();

let err = jail
.open_root()
.stage_kernel(&KernelImage::open(&strict(&src)).unwrap())
.unwrap_err();

assert_eq!(err.errno(), Some(Errno::EEXIST), "{err:?}");
victim.assert("victim");
assert!(fs::symlink_metadata(jail.root.join("vmlinux"))
.unwrap()
.file_type()
.is_symlink());
}
Loading
Loading