diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c33343fe..1f0f51cf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -139,7 +139,7 @@ jobs: FCTL_TEST_FIRECRACKER_AGENT: ${{ github.workspace }}/target/x86_64-unknown-linux-musl/release/fctl-agent run: | cargo test --locked -p fctl-host --lib driver::firecracker:: -- --ignored --nocapture --test-threads 1 - - name: Boot a sandbox on a host that runs as root + - name: Boot sandboxes on a host that runs as root env: FCTL_TEST_FIRECRACKER_AGENT: ${{ github.workspace }}/target/x86_64-unknown-linux-musl/release/fctl-agent run: | @@ -147,9 +147,12 @@ jobs: test -x "$bin" sudo env "FCTL_TEST_FIRECRACKER_AGENT=$FCTL_TEST_FIRECRACKER_AGENT" "FCTL_TEST_FIRECRACKER_DATA=$RUNNER_TEMP/root" \ "$bin" driver::firecracker::tests::boots_a_sandbox driver::firecracker::tests::sandboxes_of_a_project_share_its_package_caches \ + driver::firecracker::tests::stopped_sandboxes_resume_where_they_were \ --ignored --nocapture --test-threads 1 - # Jails lend disks to their users, which only root can. - sudo "$bin" driver::firecracker::vm::tests::stale_jails_go --exact --nocapture + # Jails lend disks and saved states to their users, which only root can. + sudo "$bin" driver::firecracker::vm::tests::stale_jails_go \ + driver::firecracker::vm::tests::a_jailed_vms_state_is_saved_and_resumed_through_its_jail \ + --exact --nocapture --test-threads 1 # Whiteouts kept as overlayfs reads them, for images that lie over # others, need root, which the VMs' init is. - name: Unpack layers as root diff --git a/crates/fctl-host/src/driver/firecracker/mod.rs b/crates/fctl-host/src/driver/firecracker/mod.rs index 2fdf8464..d1aba6f5 100644 --- a/crates/fctl-host/src/driver/firecracker/mod.rs +++ b/crates/fctl-host/src/driver/firecracker/mod.rs @@ -79,6 +79,11 @@ struct Saved { /// The Firecracker release that saved it: only the same one loads it. firecracker: String, at_ms: i64, + /// Whether the VM ran in Firecracker's jailer, whose chroot has its + /// disks and sockets at other paths than the host: it resumes in one + /// then, and only then. + #[serde(default)] + jailed: bool, } /// What a VM's saved state may leave free, at least: below it, the VM stops @@ -245,9 +250,9 @@ async fn binary_version(firecracker: &Path) -> Result { .with_context(|| format!("{} didn't say its version", firecracker.display())) } -/// Gives back the disk that the pages of zeros in the file at `path` take, -/// which most of a VM's saved memory is. -fn punch_zeros(path: &Path) -> Result<()> { +/// Gives back the disk that the pages of zeros in the first `len` bytes of +/// the file at `path` take, which most of a VM's saved memory is. +fn punch_zeros(path: &Path, len: u64) -> Result<()> { use nix::fcntl::{FallocateFlags, fallocate}; use std::os::unix::fs::FileExt; const PAGE: usize = 4096; @@ -270,8 +275,9 @@ fn punch_zeros(path: &Path) -> Result<()> { let mut offset = 0u64; // Where the run of zero pages this is in started. let mut zeros: Option = None; - loop { - let n = file.read_at(&mut buf, offset)?; + while offset < len { + let want = usize::try_from(len - offset).map_or(buf.len(), |left| left.min(buf.len())); + let n = file.read_at(&mut buf[..want], offset)?; if n == 0 { break; } @@ -1047,11 +1053,9 @@ impl FirecrackerDriver { vm.kill().await; } // A host that keeps no states (`snapshots = false`) resumes none it - // kept before, nor does a jailed VM ([`stop`](Self::stop)) one saved - // before the host ran as root: they go. + // kept before: they go. if let Some(saved) = self.record(&spec.name).and_then(|r| r.saved) && self.cfg().snapshots - && vm.jails.is_none() && !self.resized(&vm, spec, log) { match self.resume_saved(&vm, &spec.name, &saved, log).await { @@ -1462,15 +1466,13 @@ impl FirecrackerDriver { /// Stops the sandbox's VM, saving its state first where the host /// keeps them (`snapshots`) and the disk has room for its memory, so /// that it resumes where it was. A VM paused while the disk was nearly - /// full stops without, as does one in the jailer's chroot, which - /// Firecracker would write its state into and which goes as it stops, - /// and an image builder, which boots afresh for each build. + /// full stops without, as does an image builder, which boots afresh + /// for each build. pub async fn stop(&self, sb: &Sandbox) -> Result<()> { let lock = self.lock(&format!("sandbox {}", sb.name)); let _held = lock.lock().await; let vm = self.vm(&sb.name)?; if self.cfg().snapshots - && vm.jails.is_none() && !crate::driver::is_builder(&sb.name) && vm.running() && !vm.paused().await @@ -1500,7 +1502,14 @@ impl FirecrackerDriver { // would each find room for their memory, and all write it at once. let lock = self.lock("saving"); let _held = lock.lock().await; - let memory_mb = vm.memory_mb().unwrap_or(u64::MAX / 2); + // A VM that an earlier fctl started in a jail has no configuration + // in its directory, which a resume needs. + let Some(memory_mb) = vm.memory_mb() else { + tracing::info!( + "{name} stops without saving its state: the host has no record of its VM's configuration" + ); + return Ok(false); + }; let floor = self .host .min_free_disk_mb() @@ -1529,11 +1538,15 @@ impl FirecrackerDriver { let (dir, new) = (vm.dir.join("saved"), vm.dir.join("saved.new")); let _ = std::fs::remove_dir_all(&new); let _ = std::fs::remove_dir_all(&dir); + let jailed = vm.jailed(); let saved = async { vm.snapshot(&new).await?; // The memory the VM never touched, or gave back, takes no disk. + // No more than the VM's is read: a jailed VM's Firecracker made + // the file, and could make it longer. let memory = new.join("memory"); - if let Err(e) = tokio::task::spawn_blocking(move || punch_zeros(&memory)).await? { + let len = memory_mb.saturating_mul(1 << 20); + if let Err(e) = tokio::task::spawn_blocking(move || punch_zeros(&memory, len)).await? { tracing::warn!("giving back the disk {name}'s memory doesn't use: {e:#}"); } std::fs::rename(&new, &dir)?; @@ -1543,6 +1556,7 @@ impl FirecrackerDriver { record.saved = Some(Saved { firecracker, at_ms: vsock::now_ms(), + jailed, }); self.save_record(name, &record) } @@ -1559,13 +1573,23 @@ impl FirecrackerDriver { /// stopped, with its programs running on, then links its network and /// lets the state go: the VM's disk is past it once the VM runs. /// Fails, letting it go too, when the state can't come back: another - /// Firecracker saved it, say. + /// Firecracker saved it, say, or it was saved in the jailer and the + /// host runs VMs without it now, or the other way round. async fn resume_saved(&self, vm: &Vm, name: &str, saved: &Saved, log: Log<'_>) -> Result<()> { let dir = vm.dir.join("saved"); let restored = async { if !dir.join("vmstate").is_file() || !dir.join("memory").is_file() { bail!("its saved state is incomplete"); } + match (saved.jailed, vm.jails.is_some()) { + (true, false) => bail!( + "it was saved in Firecracker's jailer, which this host doesn't run VMs in now" + ), + (false, true) => bail!( + "it was saved outside Firecracker's jailer, which this host runs VMs in now" + ), + _ => {} + } let firecracker = self.firecracker(log).await?; let version = binary_version(&firecracker).await?; if version != saved.firecracker { @@ -1574,8 +1598,9 @@ impl FirecrackerDriver { saved.firecracker ); } + let jail = self.jail_for(vm, &firecracker)?; log(format!("Resuming the sandbox {name} where it stopped")); - vm.restore(&dir, &firecracker, name).await + vm.restore(&dir, &firecracker, jail.as_ref(), name).await } .await; // Firecracker maps the memory it loaded, which stays while it runs. @@ -1995,7 +2020,13 @@ mod tests { file.write_all_at(b"its stack", (6 << 20) + 17)?; file.sync_all()?; let before = std::fs::metadata(&memory)?.blocks() * 512; - punch_zeros(&memory)?; + // Only as much as the VM's memory: the half past it stays. + punch_zeros(&memory, 4 << 20)?; + let half = std::fs::metadata(&memory)?.blocks() * 512; + if before >= 8 << 20 { + assert!((4 << 20..before).contains(&half), "{half} bytes"); + } + punch_zeros(&memory, 8 << 20)?; let after = std::fs::metadata(&memory)?; assert_eq!(after.len(), 8 << 20); let mut page = [0u8; 16]; @@ -2031,6 +2062,7 @@ mod tests { saved: Some(Saved { firecracker: firecracker.into(), at_ms: 2, + jailed: vm.jails.is_some(), }), }, )?; @@ -2084,6 +2116,48 @@ mod tests { Ok(()) } + /// A state saved in Firecracker's jailer resumes only in it, and one + /// saved without it only without: a jail's chroot has the VM's disks + /// and sockets at other paths than the host. It goes, and the sandbox + /// boots from its disk. + #[tokio::test] + async fn states_resume_only_where_vms_run_as_they_were_saved() -> Result<()> { + let dir = tempfile::tempdir()?; + let d = driver(dir.path()); + let log = |_: String| {}; + let cases = [ + ( + true, + false, + "it was saved in Firecracker's jailer, which this host doesn't run VMs in now", + ), + ( + false, + true, + "it was saved outside Firecracker's jailer, which this host runs VMs in now", + ), + ]; + for (saved_jailed, jailed_now, said) in cases { + let name = format!("fctl-ses-01jailed{saved_jailed}"); + with_saved(&d, &name, "1.17.0")?; + let mut record = d.record(&name).context("record")?; + let saved = record.saved.as_mut().context("saved")?; + saved.jailed = saved_jailed; + let saved = saved.clone(); + d.save_record(&name, &record)?; + let vm = Vm::new(d.sandboxes().join(&name))?; + let vm = match jailed_now { + true => vm.with_jails(dir.path().join("firecracker/jail")), + false => vm, + }; + let e = d.resume_saved(&vm, &name, &saved, &log).await; + assert_eq!(e.map_err(|e| e.to_string()), Err(said.to_owned())); + assert!(!vm.dir.join("saved").exists()); + assert_eq!(d.record(&name).and_then(|r| r.saved), None); + } + Ok(()) + } + /// A VM asked for another size boots from its disk rather than resume /// as it was; what a save that stopped halfway left goes with the rest. #[tokio::test] @@ -2205,6 +2279,23 @@ mod tests { } } + /// The user the Firecracker of the sandbox `name` runs as, on a host + /// whose data directory is `data`. + fn firecracker_uid(data: &Path, name: &str) -> Result { + let pid = std::fs::read_to_string( + data.join("firecracker/sandboxes") + .join(name) + .join("firecracker.pid"), + )?; + let status = std::fs::read_to_string(format!("/proc/{}/status", pid.trim()))?; + status + .lines() + .find_map(|l| l.strip_prefix("Uid:")) + .and_then(|l| l.split_whitespace().next()) + .and_then(|u| u.parse().ok()) + .context("Firecracker's uid") + } + /// What passt, the VM's console and the VM's network say, for a test /// whose sandbox can't reach the network. async fn network_report(d: &super::super::Driver, sb: &Sandbox, data: &Path) -> String { @@ -2294,19 +2385,7 @@ mod tests { // As root, the VM runs in Firecracker's jailer, as a user of // its own. if nix::unistd::geteuid().is_root() { - let pid = std::fs::read_to_string( - cfg.data_dir() - .join("firecracker/sandboxes") - .join(&name) - .join("firecracker.pid"), - )?; - let status = std::fs::read_to_string(format!("/proc/{}/status", pid.trim()))?; - let uid: u32 = status - .lines() - .find_map(|l| l.strip_prefix("Uid:")) - .and_then(|l| l.split_whitespace().next()) - .and_then(|u| u.parse().ok()) - .context("Firecracker's uid")?; + let uid = firecracker_uid(&cfg.data_dir(), &name)?; assert!(uid >= vm::JAIL_ID_BASE, "Firecracker runs as {uid}"); eprintln!("jailed as {uid}"); } @@ -2354,7 +2433,8 @@ mod tests { d.exec(&sb, "echo kept > \"$HOME/note\"").await?; d.stop(&sb).await?; // Its disk, which its jail had, is the host's again, as are the - // images' filesystems, which their unpacking VMs' jails had. + // images' filesystems, which their unpacking VMs' jails had, + // and the state it saved in its jail. if nix::unistd::geteuid().is_root() { use std::os::unix::fs::MetadataExt; let firecracker = cfg.data_dir().join("firecracker"); @@ -2362,16 +2442,16 @@ mod tests { .flatten() .map(|e| e.path().join("rootfs.ext4")) .filter(|p| p.is_file()); - let disks = std::fs::read_dir(firecracker.join("sandboxes").join(&name))? - .flatten() + let sandbox = firecracker.join("sandboxes").join(&name); + let disks = std::fs::read_dir(&sandbox)?.flatten().map(|e| e.path()); + let saved = std::fs::read_dir(sandbox.join("saved")) + .into_iter() + .flat_map(|files| files.flatten()) .map(|e| e.path()); - for path in images.chain(disks) { + for path in images.chain(disks).chain(saved) { let owner = std::fs::symlink_metadata(&path)?.uid(); assert!(owner < vm::JAIL_ID_BASE, "{path:?} is {owner}'s"); } - // A jailed VM saves no state yet. - let saved = firecracker.join("sandboxes").join(&name).join("saved"); - assert!(!saved.exists(), "the jailed VM saved its state"); } let listed = d.list().await?; assert!(listed.iter().any(|s| s.name == name && !s.running), "{listed:?}"); @@ -2600,29 +2680,19 @@ mod tests { /// A sandbox stopped with its state saved resumes with its programs /// running and its network linked again; without it, it boots from its - /// disk. As [`boots_a_sandbox`] says. + /// disk. As root, its VM runs in Firecracker's jailer, and its state + /// goes through the jail's chroot: taken out as root's, and lent to a + /// new jail to load. As [`boots_a_sandbox`] says. #[tokio::test] #[ignore = "needs KVM, passt and a static fctl-agent"] async fn stopped_sandboxes_resume_where_they_were() -> Result<()> { use super::super::Driver; + use std::os::unix::fs::MetadataExt; let temp = tempfile::tempdir()?; let data = std::env::var("FCTL_TEST_FIRECRACKER_DATA") .map(PathBuf::from) .unwrap_or_else(|_| temp.path().to_owned()); - let mut cfg = HostConfig { - data_dir: Some(data.clone()), - agent_bin: Some( - std::env::var("FCTL_TEST_FIRECRACKER_AGENT") - .context("FCTL_TEST_FIRECRACKER_AGENT isn't set")? - .into(), - ), - ..HostConfig::default() - }; - cfg.firecracker.kernel = std::env::var("FCTL_TEST_FIRECRACKER_KERNEL").ok(); - cfg.firecracker.disk_gb = 4; - // Jailed VMs save no state yet: as root, this tests saving without - // the jailer. - cfg.firecracker.jailer = false; + let cfg = vm_host(&data)?; let d = Driver::select("firecracker", &cfg).await?; let log = |line: String| eprintln!("{line}"); let name = format!( @@ -2640,6 +2710,17 @@ mod tests { }; let sb = d.ensure(&spec, &log).await?; let dir = data.join("firecracker/sandboxes").join(&name); + let root = nix::unistd::geteuid().is_root(); + // The chroots of the VM's jails. + let key = Vm::new(dir.clone())?.key; + let chroots = || -> Vec { + std::fs::read_dir(data.join("firecracker/jail")) + .into_iter() + .flat_map(|execs| execs.flatten()) + .map(|e| e.path().join(&key).join("root")) + .filter(|p| p.is_dir()) + .collect() + }; let result = async { // A program of its own, and a file in memory (/run is a tmpfs). d.exec( @@ -2657,10 +2738,30 @@ mod tests { ); assert!(dir.join("saved/memory").is_file()); assert!(!d.list().await?.iter().any(|s| s.name == name && s.running)); + // Out of the jail, which went: root's, and private. + if root { + assert_eq!(chroots(), Vec::::new()); + for file in ["vmstate", "memory"] { + let meta = std::fs::symlink_metadata(dir.join("saved").join(file))?; + assert_eq!((meta.uid(), meta.mode() & 0o777), (0, 0o600), "{file}"); + } + } let started = std::time::Instant::now(); d.ensure(&spec, &log).await?; eprintln!("resumed in {:?}", started.elapsed()); assert!(!dir.join("saved").exists()); + // In a jail of its own again, which no longer holds the state. + if root { + let uid = firecracker_uid(&data, &name)?; + assert!(uid >= vm::JAIL_ID_BASE, "Firecracker runs as {uid}"); + eprintln!("resumed jailed as {uid}"); + let jailed = chroots(); + assert_eq!(jailed.len(), 1, "{jailed:?}"); + for file in ["vmstate", "memory"] { + let left = std::fs::symlink_metadata(jailed[0].join(file)); + assert!(left.is_err(), "{file} is still in the jail"); + } + } assert_eq!(d.exec(&sb, "cat /run/held").await?, held); let alive = d .exec(&sb, "kill -0 \"$(cat /run/held)\" && echo alive") diff --git a/crates/fctl-host/src/driver/firecracker/vm.rs b/crates/fctl-host/src/driver/firecracker/vm.rs index 1ceaf749..7590792e 100644 --- a/crates/fctl-host/src/driver/firecracker/vm.rs +++ b/crates/fctl-host/src/driver/firecracker/vm.rs @@ -28,6 +28,14 @@ const API_TIMEOUT: Duration = Duration::from_secs(10); /// memory goes to or comes from a file, gigabytes of it. const SNAPSHOT_TIMEOUT: Duration = Duration::from_secs(600); +/// How long a jailed Firecracker may take to make a socket in its chroot, +/// or a Firecracker that loads a state to answer on its API socket. +const SOCKET_TIMEOUT: Duration = Duration::from_secs(10); + +/// The files of a VM's saved state, as Firecracker writes them: its own +/// state, and the VM's memory. +const STATE_FILES: [&str; 2] = ["vmstate", "memory"]; + /// The kernel's command line, before what fctl-agent reads: the console, a /// reset that ends Firecracker, and no modules to look for. On x86_64 also /// what Firecracker suggests to boot faster: no PS/2 probing, and no PCI @@ -103,6 +111,41 @@ struct Lent { uid: u32, /// The VM's writable disks, which the jail's user owns while it runs. writable: Vec, + /// The saved state a VM resumes from (in the sandbox's `saved/`), which + /// the jail's user owns until Firecracker has loaded it. + #[serde(default)] + saved: Vec, +} + +/// A jail the host made for a VM ([`Vm::make_jail`]): its directory +/// (`//`), the chroot in it (`root`), +/// which the jail's user owns, and what the jail was lent, noted beside the +/// chroot. +struct Made { + dir: PathBuf, + root: PathBuf, + lent: Lent, +} + +impl Made { + /// Notes what the jail is lent, before it is: for a host that stops + /// halfway. + fn note(&self) -> Result<()> { + std::fs::write(self.dir.join("jail.json"), serde_json::to_vec(&self.lent)?)?; + Ok(()) + } + + /// Lends the file of a saved state `from` to the jail's user, who must + /// read it to load it, as a hard link `name` at the chroot's root. + fn lend_saved(&mut self, from: &Path, name: &str) -> Result<()> { + link_into(&self.root, from, name)?; + let host = from.canonicalize()?; + self.lent.saved.push(host.clone()); + self.note()?; + let uid = self.lent.uid; + std::os::unix::fs::lchown(&host, Some(uid), Some(uid)) + .with_context(|| format!("lending {} to the VM's jail", host.display())) + } } /// The first uid and gid of the VMs' jails: far from those people, systems @@ -214,6 +257,12 @@ impl Vm { }) } + /// Whether the VM runs in a jail ([`Jail`]), which a state it saves + /// needs to resume in. + pub fn jailed(&self) -> bool { + self.jail().is_some() + } + pub fn console(&self) -> PathBuf { self.dir.join("console.log") } @@ -283,30 +332,51 @@ impl Vm { /// so the host never goes through it to reach the sockets: it links /// them into a directory of its own beside it, checked to be the /// sockets Firecracker made ([`link_sockets`](Self::link_sockets)), and - /// `run` leads there. + /// `run` leads there. The host keeps the configuration with its own + /// paths in the VM's directory, where the jail's user can't change it: + /// the VM's size, and the disks a state it saves resumes with. async fn start_jailed(&self, spec: &Spec, firecracker: &Path, jail: &Jail) -> Result<()> { - let jails = self - .jails - .as_ref() - .context("no directory for the VM's jail")?; - let exec = firecracker - .file_name() - .context("Firecracker's path has no file name")?; - std::fs::create_dir_all(jails)?; - if let Some(why) = jails_problem(jails) { - bail!("{why}"); - } - let (uid, inside, root) = self.make_jail(jails, exec, spec)?; + let (made, drives) = self.make_jail(firecracker, &spec.drives)?; + let inside = Spec { + kernel: put_in_jail(&made.root, &spec.kernel, "vmlinux")?, + initrd: put_in_jail(&made.root, &spec.initrd, "initrd.cpio")?, + drives, + ..spec.clone() + }; let mut config = self.config(&inside); config["vsock"]["uds_path"] = json!("/v.sock"); std::fs::write( - root.join("firecracker.json"), + made.root.join("firecracker.json"), serde_json::to_vec_pretty(&config)?, )?; - // The host's links to the sockets, beside the chroot (on its - // filesystem, as links must be), where only root may change them; - // `run`, short as sockets' paths must be, leads there. - let sockets = root.with_file_name("run"); + std::fs::create_dir_all(&self.dir)?; + std::fs::write( + self.dir.join("firecracker.json"), + serde_json::to_vec_pretty(&self.config(spec))?, + )?; + let sockets = self.spawn_jailed(jail, firecracker, &made, Some("/firecracker.json"))?; + self.link_sockets(&made.root, &sockets, &["api.sock", "v.sock"]) + .await + } + + /// Starts Firecracker `firecracker` through its jailer, in the jail + /// `made`, with its configuration at `config` in the chroot, or none + /// for a state to load; where the host's links to its sockets go, + /// beside the chroot (on its filesystem, as links must be), where only + /// root may change them, and where `run`, short as sockets' paths must + /// be, leads. + fn spawn_jailed( + &self, + jail: &Jail, + firecracker: &Path, + made: &Made, + config: Option<&str>, + ) -> Result { + let jails = self + .jails + .as_ref() + .context("no directory for the VM's jail")?; + let sockets = made.root.with_file_name("run"); std::fs::create_dir(&sockets)?; let _ = std::fs::remove_file(&self.run); let _ = std::fs::remove_dir_all(&self.run); @@ -315,29 +385,41 @@ impl Vm { std::fs::create_dir_all(&self.dir)?; let console = std::fs::File::create(self.console())?; let mut cmd = std::process::Command::new(&jail.jailer); - cmd.args(jailer_args(&self.key, firecracker, uid, jails)) - .stdin(Stdio::null()) - .stdout(console.try_clone()?) - .stderr(console); + cmd.args(jailer_args( + &self.key, + firecracker, + made.lent.uid, + jails, + config, + )) + .stdin(Stdio::null()) + .stdout(console.try_clone()?) + .stderr(console); spawn_detached(&mut cmd, &self.pid_file("firecracker")) .with_context(|| format!("starting {}", jail.jailer.display()))?; - self.link_sockets(&root, &sockets, Duration::from_secs(10)) - .await + Ok(sockets) } - /// Makes this VM's jail under `jails` for Firecracker `exec`: a chroot, - /// owned by a uid and gid that no other jail here has (picked under the - /// jails' lock, which other processes' VMs take too), holding the VM's - /// kernel and initramfs and its disks, the writable ones lent to the + /// Makes this VM's jail for Firecracker `firecracker`, under the VMs' + /// jails: a chroot, owned by a uid and gid that no other jail here has + /// (picked under the jails' lock, which other processes' VMs take too), + /// holding the VM's disks `drives`, the writable ones lent to the /// jail's user ([`Lent`]), what an earlier jail of it lent given back - /// first. Its uid, `spec` as the chroot has it, and the chroot. - fn make_jail( - &self, - jails: &Path, - exec: &std::ffi::OsStr, - spec: &Spec, - ) -> Result<(u32, Spec, PathBuf)> { + /// first. The jail, and the disks as its chroot has them, in their + /// order: `/drive0.ext4`, `/drive1.ext4`, … + fn make_jail(&self, firecracker: &Path, drives: &[Drive]) -> Result<(Made, Vec)> { use std::os::unix::fs::PermissionsExt; + let jails = self + .jails + .as_ref() + .context("no directory for the VM's jail")?; + let exec = firecracker + .file_name() + .context("Firecracker's path has no file name")?; + std::fs::create_dir_all(jails)?; + if let Some(why) = jails_problem(jails) { + bail!("{why}"); + } let dir = jails.join(exec).join(&self.key); let root = dir.join("root"); let uid = { @@ -350,23 +432,27 @@ impl Vm { std::os::unix::fs::chown(&root, Some(uid), Some(uid))?; uid }; - let mut lent = Lent { - uid, - writable: Vec::new(), + let mut made = Made { + dir, + root, + lent: Lent { + uid, + ..Lent::default() + }, }; - let mut inside = spec.clone(); - inside.kernel = put_in_jail(&root, &spec.kernel, "vmlinux")?; - inside.initrd = put_in_jail(&root, &spec.initrd, "initrd.cpio")?; - for (i, drive) in inside.drives.iter_mut().enumerate() { + let mut inside = Vec::new(); + for (i, drive) in drives.iter().enumerate() { let host = drive .path .canonicalize() .with_context(|| format!("finding {}", drive.path.display()))?; - drive.path = link_into(&root, &host, &format!("drive{i}.ext4"))?; + inside.push(Drive { + path: link_into(&made.root, &host, &format!("drive{i}.ext4"))?, + writable: drive.writable, + }); if drive.writable { - // Noted before it is lent, for a host that stops halfway. - lent.writable.push(host.clone()); - std::fs::write(dir.join("jail.json"), serde_json::to_vec(&lent)?)?; + made.lent.writable.push(host.clone()); + made.note()?; std::os::unix::fs::chown(&host, Some(uid), Some(uid))?; } else { let mode = std::fs::metadata(&host)?.permissions().mode(); @@ -375,20 +461,21 @@ impl Vm { } } } - std::fs::write(dir.join("jail.json"), serde_json::to_vec(&lent)?)?; - Ok((uid, inside, root)) + made.note()?; + Ok((made, inside)) } - /// Links the sockets Firecracker makes in the chroot `root`, its API's - /// and its vsock's, into `sockets`, within `within`: each opened without - /// following a link and checked to be a socket, then linked by what - /// was opened, so that the jail's user, who may change the chroot, - /// can't send the host to another VM's sockets. - async fn link_sockets(&self, root: &Path, sockets: &Path, within: Duration) -> Result<()> { + /// Links the sockets `names` that Firecracker makes in the chroot + /// `root` (its API's, `api.sock`, and its vsock's, `v.sock`) into + /// `sockets`, once each is there: each opened without following a link + /// and checked to be a socket, then linked by what was opened, so that + /// the jail's user, who may change the chroot, can't send the host to + /// another VM's sockets. + async fn link_sockets(&self, root: &Path, sockets: &Path, names: &[&str]) -> Result<()> { use std::os::fd::AsRawFd; use std::os::unix::fs::{FileTypeExt, OpenOptionsExt}; - let until = Instant::now() + within; - for name in ["api.sock", "v.sock"] { + let until = Instant::now() + SOCKET_TIMEOUT; + for name in names { loop { let opened = std::fs::OpenOptions::new() .read(true) @@ -426,8 +513,8 @@ impl Vm { } /// Gives back what this VM's jails lent of the host's ([`Lent`]): its - /// writable disks go back to the host's user, so that a later jail - /// with the same uid owns none of them. + /// writable disks and the state it resumed from go back to the host's + /// user, so that a later jail with the same uid owns none of them. fn give_back(&self) { for jail in self.jail_dirs() { give_back(&jail); @@ -673,12 +760,31 @@ impl Vm { /// Pauses the VM and saves its state in `dir`: Firecracker's /// (`vmstate`) and the VM's memory (`memory`), with its programs as - /// they are. The VM stays paused. + /// they are. The VM stays paused. A jailed Firecracker writes them at + /// the root of its chroot, as the jail's user, and the host takes them + /// out ([`take_out`]); what is there by those names goes first, so that + /// they are files Firecracker makes. pub async fn snapshot(&self, dir: &Path) -> Result<()> { std::fs::create_dir_all(dir)?; if !self.paused().await { self.pause().await?; } + let Some((root, uid)) = self.jail() else { + return self.create_snapshot(dir).await; + }; + for name in STATE_FILES { + let _ = std::fs::remove_file(root.join(name)); + } + self.create_snapshot(Path::new("/")).await?; + for name in STATE_FILES { + take_out(&root, uid, name, &dir.join(name))?; + } + Ok(()) + } + + /// Has Firecracker save the paused VM's state in `dir`, as Firecracker + /// sees it. + async fn create_snapshot(&self, dir: &Path) -> Result<()> { let body = json!({ "snapshot_type": "Full", "snapshot_path": dir.join("vmstate"), @@ -697,51 +803,112 @@ impl Vm { /// Starts Firecracker on the state saved in `dir` ([`snapshot`](Self::snapshot)), /// the VM running on where it was, after stopping what an earlier boot - /// left. The VM's disks must be where they were. What it started goes - /// again when it fails. - pub async fn restore(&self, dir: &Path, firecracker: &Path, name: &str) -> Result<()> { + /// left: through its jailer when `jail` says so, as a state saved in a + /// jail must. The VM's disks must be where they were. What it started + /// goes again when it fails. + pub async fn restore( + &self, + dir: &Path, + firecracker: &Path, + jail: Option<&Jail>, + name: &str, + ) -> Result<()> { self.kill().await; - let restored = async { - std::fs::create_dir_all(&self.run)?; - let console = std::fs::File::create(self.console())?; - let mut cmd = std::process::Command::new(firecracker); - cmd.arg("--api-sock") - .arg(self.api()) - .args(["--id", name, "--level", "Warning"]) - .stdin(Stdio::null()) - .stdout(console.try_clone()?) - .stderr(console); - spawn_detached(&mut cmd, &self.pid_file("firecracker")) - .with_context(|| format!("starting {}", firecracker.display()))?; - let until = Instant::now() + Duration::from_secs(10); - while self.state().await.is_err() { - if Instant::now() > until || !self.running() { - bail!("Firecracker did not start: {}", self.console_tail()); - } - tokio::time::sleep(Duration::from_millis(20)).await; - } - let body = json!({ - "snapshot_path": dir.join("vmstate"), - "mem_backend": { "backend_type": "File", "backend_path": dir.join("memory") }, - "resume_vm": true, - }); - api_within( - &self.api(), - "PUT", - "/snapshot/load", - Some(&body), - SNAPSHOT_TIMEOUT, - ) - .await?; - Ok::<_, anyhow::Error>(()) - } - .await; + let restored = match jail { + Some(jail) => self.restore_jailed(dir, firecracker, jail).await, + None => self.restore_processes(dir, firecracker, name).await, + }; if restored.is_err() { self.kill().await; } restored } + async fn restore_processes(&self, dir: &Path, firecracker: &Path, name: &str) -> Result<()> { + std::fs::create_dir_all(&self.run)?; + let console = std::fs::File::create(self.console())?; + let mut cmd = std::process::Command::new(firecracker); + cmd.arg("--api-sock") + .arg(self.api()) + .args(["--id", name, "--level", "Warning"]) + .stdin(Stdio::null()) + .stdout(console.try_clone()?) + .stderr(console); + spawn_detached(&mut cmd, &self.pid_file("firecracker")) + .with_context(|| format!("starting {}", firecracker.display()))?; + self.load(dir).await + } + + /// Firecracker through its jailer, on the state saved in `dir`, in a + /// jail made as a start makes one ([`start_jailed`](Self::start_jailed)) + /// but without a configuration: its chroot holds the disks the VM + /// booted with, as they were there, and the saved state, lent to the + /// jail's user, who reads it ([`Made::lend_saved`]). Firecracker makes + /// the vsock's socket as it loads the state, so the host links that + /// out only then. Loaded, the state's names in the chroot go: the + /// memory stays mapped, and a later save makes new files. + async fn restore_jailed(&self, dir: &Path, firecracker: &Path, jail: &Jail) -> Result<()> { + let drives = self + .booted_drives() + .context("the host has no record of the disks the VM booted with")?; + let (mut made, _) = self.make_jail(firecracker, &drives)?; + for name in STATE_FILES { + made.lend_saved(&dir.join(name), name)?; + } + let sockets = self.spawn_jailed(jail, firecracker, &made, None)?; + self.link_sockets(&made.root, &sockets, &["api.sock"]) + .await?; + self.load(Path::new("/")).await?; + self.link_sockets(&made.root, &sockets, &["v.sock"]).await?; + for name in STATE_FILES { + let _ = std::fs::remove_file(made.root.join(name)); + } + Ok(()) + } + + /// Has the Firecracker just started load the state saved in `dir`, as + /// Firecracker sees it, once it answers, and resume the VM. + async fn load(&self, dir: &Path) -> Result<()> { + let until = Instant::now() + SOCKET_TIMEOUT; + while self.state().await.is_err() { + if Instant::now() > until || !self.running() { + bail!("Firecracker did not start: {}", self.console_tail()); + } + tokio::time::sleep(Duration::from_millis(20)).await; + } + let body = json!({ + "snapshot_path": dir.join("vmstate"), + "mem_backend": { "backend_type": "File", "backend_path": dir.join("memory") }, + "resume_vm": true, + }); + api_within( + &self.api(), + "PUT", + "/snapshot/load", + Some(&body), + SNAPSHOT_TIMEOUT, + ) + .await + .map(drop) + } + + /// The disks the VM booted with, from the configuration with the + /// host's paths that the host keeps in the VM's directory. + fn booted_drives(&self) -> Option> { + let config = std::fs::read(self.dir.join("firecracker.json")).ok()?; + let config: Value = serde_json::from_slice(&config).ok()?; + config["drives"] + .as_array()? + .iter() + .map(|d| { + Some(Drive { + path: PathBuf::from(d["path_on_host"].as_str()?), + writable: !d["is_read_only"].as_bool()?, + }) + }) + .collect() + } + /// Whether the VM is paused. One that doesn't run, or whose Firecracker /// doesn't answer, isn't. pub async fn paused(&self) -> bool { @@ -1018,9 +1185,16 @@ fn pick_jail_id(key: &str, taken: &std::collections::HashSet) -> u32 { /// The jailer's arguments for the VM `key`: Firecracker `firecracker` as /// `uid` (and the gid of the same number), chrooted under `jails`, in a /// cgroup of its own under `fctl` (Firecracker runs a thread for each vCPU -/// and a few of its own), with its configuration and API socket at the -/// chroot's root. -fn jailer_args(key: &str, firecracker: &Path, uid: u32, jails: &Path) -> Vec { +/// and a few of its own), with its API socket at the chroot's root, and +/// its configuration at `config` in the chroot, or none for a state to +/// load. +fn jailer_args( + key: &str, + firecracker: &Path, + uid: u32, + jails: &Path, + config: Option<&str>, +) -> Vec { let mut args: Vec = vec!["--id".into(), key.into()]; args.extend(["--exec-file".into(), firecracker.as_os_str().to_owned()]); args.extend([ @@ -1041,13 +1215,13 @@ fn jailer_args(key: &str, firecracker: &Path, uid: u32, jails: &Path) -> Vec Result { Ok(Path::new("/").join(name)) } +/// Takes the file `name` that Firecracker made at the root of its chroot +/// `root`, as the jail's user `uid`, out to the host's `to`: opened +/// without following a link and checked to be a regular file of that +/// user's with no other name, then linked by what was opened, so that the +/// jail's user can't hand the host another file in its place (one of the +/// host's through a link, or the disk it was lent). `to` is then the host's +/// user's, and private, and the chroot's name for it goes. +fn take_out(root: &Path, uid: u32, name: &str, to: &Path) -> Result<()> { + use std::os::fd::AsRawFd; + use std::os::unix::fs::{MetadataExt, OpenOptionsExt, PermissionsExt}; + let file = std::fs::OpenOptions::new() + .read(true) + .custom_flags(nix::libc::O_PATH | nix::libc::O_NOFOLLOW) + .open(root.join(name)) + .with_context(|| format!("opening the {name} Firecracker saved in the VM's jail"))?; + let meta = file.metadata()?; + if !meta.is_file() { + bail!("{name} in the VM's jail isn't a file"); + } + if meta.uid() != uid { + bail!("{name} in the VM's jail isn't its user's"); + } + if meta.nlink() != 1 { + bail!("{name} in the VM's jail has another name"); + } + let _ = std::fs::remove_file(to); + nix::unistd::linkat( + nix::fcntl::AT_FDCWD, + format!("/proc/self/fd/{}", file.as_raw_fd()).as_str(), + nix::fcntl::AT_FDCWD, + to, + nix::fcntl::AtFlags::AT_SYMLINK_FOLLOW, + ) + .with_context(|| format!("taking the VM's {name} out of its jail"))?; + let me = nix::unistd::geteuid().as_raw(); + let group = nix::unistd::getegid().as_raw(); + std::os::unix::fs::lchown(to, Some(me), Some(group))?; + std::fs::set_permissions(to, std::fs::Permissions::from_mode(0o600))?; + let _ = std::fs::remove_file(root.join(name)); + Ok(()) +} + /// Puts a file the VM boots from (its kernel or initramfs) in a jail's /// chroot `root` as `name`: a hard link where everyone may read it and it /// is on the chroot's filesystem, else a copy that the jail's user may read @@ -1444,21 +1661,33 @@ mod tests { assert!((JAIL_ID_BASE..JAIL_ID_BASE + JAIL_IDS).contains(&pick_jail_id("zz", &none))); } + /// The jailer runs Firecracker as the jail's user, with its + /// configuration to boot, and without one to load a saved state. #[test] fn the_jailer_runs_firecracker_as_the_jails_user() { - let args = jailer_args( - "0123456789abcdef", - Path::new("/var/lib/fctl/firecracker/bin/firecracker-v1.17.0"), - 1_900_000_007, - Path::new("/var/lib/fctl/firecracker/jail"), - ); - let line: Vec = args + let args = |config| { + jailer_args( + "0123456789abcdef", + Path::new("/var/lib/fctl/firecracker/bin/firecracker-v1.17.0"), + 1_900_000_007, + Path::new("/var/lib/fctl/firecracker/jail"), + config, + ) .iter() .map(|a| a.to_string_lossy().into_owned()) - .collect(); + .collect::>() + .join(" ") + }; + let jailer = "--id 0123456789abcdef --exec-file /var/lib/fctl/firecracker/bin/firecracker-v1.17.0 --uid 1900000007 --gid 1900000007 --chroot-base-dir /var/lib/fctl/firecracker/jail --cgroup-version 2 --parent-cgroup fctl --cgroup pids.max=256"; assert_eq!( - line.join(" "), - "--id 0123456789abcdef --exec-file /var/lib/fctl/firecracker/bin/firecracker-v1.17.0 --uid 1900000007 --gid 1900000007 --chroot-base-dir /var/lib/fctl/firecracker/jail --cgroup-version 2 --parent-cgroup fctl --cgroup pids.max=256 -- --api-sock /api.sock --config-file /firecracker.json --level Warning" + args(Some("/firecracker.json")), + format!( + "{jailer} -- --api-sock /api.sock --config-file /firecracker.json --level Warning" + ) + ); + assert_eq!( + args(None), + format!("{jailer} -- --api-sock /api.sock --level Warning") ); } @@ -1529,7 +1758,7 @@ mod tests { let other = std::os::unix::net::UnixListener::bind(dir.path().join("other.sock"))?; std::os::unix::fs::symlink(dir.path().join("other.sock"), root.join("v.sock"))?; let e = vm - .link_sockets(&root, &sockets, Duration::from_millis(100)) + .link_sockets(&root, &sockets, &["api.sock", "v.sock"]) .await; assert_eq!( e.map_err(|e| e.to_string()), @@ -1540,8 +1769,7 @@ mod tests { // Its own: the host's links reach it, whatever the jail does after. std::fs::remove_file(root.join("v.sock"))?; let own = std::os::unix::net::UnixListener::bind(root.join("v.sock"))?; - vm.link_sockets(&root, &sockets, Duration::from_millis(100)) - .await?; + vm.link_sockets(&root, &sockets, &["v.sock"]).await?; std::fs::remove_file(root.join("v.sock"))?; std::os::unix::fs::symlink(dir.path().join("other.sock"), root.join("v.sock"))?; let _conn = std::os::unix::net::UnixStream::connect(vm.vsock())?; @@ -1553,17 +1781,25 @@ mod tests { } /// Jails that no running VM has go once they are older than a VM takes - /// to start, with what they lent given back. + /// to start, with what they lent given back: the VM's disk, and the + /// state it was resuming from when its host stopped. #[test] fn stale_jails_go() -> Result<()> { let dir = tempfile::tempdir()?; let jails = dir.path().join("jail"); let disk = dir.path().join("disk.ext4"); std::fs::write(&disk, b"ext4")?; + let memory = dir.path().join("memory"); + std::fs::write(&memory, b"memory")?; let lent = Lent { uid: JAIL_ID_BASE, writable: vec![disk.clone()], + saved: vec![memory.clone()], }; + // As an earlier fctl noted it, before jails were lent states. + let older: Lent = + serde_json::from_str(r#"{"uid":1900000000,"writable":["/data/disk.ext4"]}"#)?; + assert!(older.saved.is_empty()); let jail = |key: &str, age: Duration| -> Result { let d = jails.join("firecracker-v1.17.0").join(key); std::fs::create_dir_all(d.join("root"))?; @@ -1578,17 +1814,21 @@ mod tests { // Lent to the jail's user, which only root can (CI runs this as root). let root = nix::unistd::geteuid().is_root(); if root { - std::os::unix::fs::chown(&disk, Some(JAIL_ID_BASE), Some(JAIL_ID_BASE))?; + for file in [&disk, &memory] { + std::os::unix::fs::chown(file, Some(JAIL_ID_BASE), Some(JAIL_ID_BASE))?; + } } let keys = std::collections::HashSet::from(["bbbbbbbbbbbbbbbb".to_owned()]); remove_stale_jails(&jails, &keys, Duration::from_secs(600)); assert!(!stale.exists() && running.exists() && starting.exists()); - // The disk it lent is the host's user's again, and still there. + // What it lent is the host's user's again, and still there. use std::os::unix::fs::MetadataExt; - assert_eq!( - std::fs::metadata(&disk)?.uid(), - nix::unistd::geteuid().as_raw() - ); + for file in [&disk, &memory] { + assert_eq!( + std::fs::metadata(file)?.uid(), + nix::unistd::geteuid().as_raw() + ); + } if !root { eprintln!("only root lends disks: this checked that the jail went"); } @@ -1624,7 +1864,7 @@ mod tests { let uid = nix::unistd::geteuid().as_raw(); let lent = Lent { uid, - writable: Vec::new(), + ..Lent::default() }; std::fs::write(jail.join("jail.json"), serde_json::to_vec(&lent)?)?; // What the jail put where the link goes. @@ -1737,20 +1977,75 @@ mod tests { /// A stand-in for Firecracker's API on `socket`: it says what its VM /// does, pauses and resumes it as Firecracker does, and refuses to - /// resume one that runs. It answers each request on a connection of its - /// own, and keeps them in `requests`. - fn firecracker_api(socket: &Path) -> Result<(tokio::task::JoinHandle<()>, Requests)> { + /// resume one that runs. It saves a paused VM's state where it's told, + /// and loads one, making the vsock's socket as it does; in `chroot` + /// when it stands in for a jailed Firecracker, as the jail's user (the + /// chroot's owner), who loads only what it may read. It answers each + /// request on a connection of its own, and keeps them in `requests`. + fn firecracker_api( + socket: &Path, + chroot: Option<&Path>, + ) -> Result<(tokio::task::JoinHandle<()>, Requests)> { + use std::os::unix::fs::MetadataExt; use tokio::io::{AsyncReadExt, AsyncWriteExt}; std::fs::create_dir_all(socket.parent().context("no parent")?)?; let listener = tokio::net::UnixListener::bind(socket)?; let requests = std::sync::Arc::new(std::sync::Mutex::new(Vec::new())); let seen = requests.clone(); + let user = match chroot { + Some(root) => Some(std::fs::metadata(root)?.uid()), + None => None, + }; + let chroot = chroot.map(Path::to_owned); + // Where a path in a request is, as the host sees it. + let at = move |path: &Value| -> Option { + let path = Path::new(path.as_str()?); + match &chroot { + Some(root) => Some(root.join(path.strip_prefix("/").ok()?)), + None => Some(path.to_owned()), + } + }; + let save = { + let at = at.clone(); + move |asked: &Value| -> Option<()> { + let memory = [b"the agent's heap".as_slice(), &[0u8; 8192]].concat(); + for (path, bytes) in [ + (at(&asked["snapshot_path"])?, b"state".as_slice()), + (at(&asked["mem_file_path"])?, &memory), + ] { + std::fs::write(&path, bytes).ok()?; + if let Some(uid) = user { + std::os::unix::fs::chown(&path, Some(uid), Some(uid)).ok()?; + } + } + Some(()) + } + }; + let load = move |asked: &Value| -> Option> { + for path in [ + at(&asked["snapshot_path"])?, + at(&asked["mem_backend"]["backend_path"])?, + ] { + let file = std::fs::metadata(path).ok()?; + if user.is_some_and(|uid| file.uid() != uid) { + return None; + } + } + let vsock = match user { + Some(_) => { + Some(std::os::unix::net::UnixListener::bind(at(&json!("/v.sock"))?).ok()?) + } + None => None, + }; + Some(vsock) + }; let task = tokio::spawn(async move { let mut state = "Running"; + let mut _vsock = None; while let Ok((mut stream, _)) = listener.accept().await { let mut buf = Vec::new(); let mut chunk = [0u8; 1024]; - let request = loop { + let (line, body) = loop { let Ok(n) = stream.read(&mut chunk).await else { return; }; @@ -1769,12 +2064,15 @@ mod tests { .unwrap_or(0); if body.len() >= len { let line = head.lines().next().unwrap_or_default(); - break format!("{line} {body}"); + break (line.to_owned(), body.to_owned()); } }; + let request = format!("{line} {body}"); if let Ok(mut r) = seen.lock() { r.push(request.clone()); } + let asked: Value = serde_json::from_str(&body).unwrap_or_default(); + let done = "HTTP/1.1 204 No Content\r\n\r\n".to_owned(); let answer = if request.starts_with("GET / ") { let body = format!( r#"{{"id":"fctl-ses-x","state":"{state}","vmm_version":"1.17.0"}}"# @@ -1785,12 +2083,23 @@ mod tests { ) } else if request.contains(r#""state":"Paused""#) { state = "Paused"; - "HTTP/1.1 204 No Content\r\n\r\n".to_owned() + done } else if request.contains(r#""state":"Resumed""#) && state == "Paused" { state = "Running"; - "HTTP/1.1 204 No Content\r\n\r\n".to_owned() - } else if request.starts_with("PUT /snapshot/create ") && state == "Paused" { - "HTTP/1.1 204 No Content\r\n\r\n".to_owned() + done + } else if request.starts_with("PUT /snapshot/create ") + && state == "Paused" + && save(&asked).is_some() + { + done + } else if let Some(vsock) = request + .starts_with("PUT /snapshot/load ") + .then(|| load(&asked)) + .flatten() + { + _vsock = vsock; + state = "Running"; + done } else { let body = r#"{"fault_message":"The requested operation is not supported in the current VM state."}"#; format!( @@ -1804,6 +2113,34 @@ mod tests { Ok((task, requests)) } + /// Firecracker's API in the jail whose chroot is `root`, served once + /// `ready` is there, which the host puts in before it starts the + /// jailer, with the vsock's socket when `booting`: Firecracker makes it + /// at once from its configuration, and as it loads a state otherwise. + async fn firecracker_in_jail( + root: PathBuf, + ready: &'static str, + booting: bool, + ) -> Result<( + tokio::task::JoinHandle<()>, + Requests, + Option, + )> { + let until = Instant::now() + Duration::from_secs(10); + while std::fs::symlink_metadata(root.join(ready)).is_err() { + if Instant::now() > until { + bail!("the jail never had {ready}"); + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + let (api, requests) = firecracker_api(&root.join("api.sock"), Some(&root))?; + let vsock = match booting { + true => Some(std::os::unix::net::UnixListener::bind(root.join("v.sock"))?), + false => None, + }; + Ok((api, requests, vsock)) + } + #[tokio::test] async fn pauses_and_resumes_the_vm_through_firecrackers_api() -> Result<()> { let dir = tempfile::tempdir()?; @@ -1811,7 +2148,7 @@ mod tests { // No Firecracker: nothing is paused. assert!(!vm.paused().await); assert!(vm.pause().await.is_err()); - let (api, requests) = firecracker_api(&vm.api())?; + let (api, requests) = firecracker_api(&vm.api(), None)?; assert_eq!(vm.state().await?, "Running"); vm.pause().await?; assert!(vm.paused().await); @@ -1841,11 +2178,12 @@ mod tests { async fn saves_the_vms_state_through_firecrackers_api() -> Result<()> { let dir = tempfile::tempdir()?; let vm = Vm::new(dir.path().join("fctl-ses-x"))?; - let (api, requests) = firecracker_api(&vm.api())?; + let (api, requests) = firecracker_api(&vm.api(), None)?; assert_eq!(vm.firecracker_version().await?, "1.17.0"); let saved = dir.path().join("saved"); vm.snapshot(&saved).await?; assert!(vm.paused().await); + assert!(saved.join("vmstate").is_file() && saved.join("memory").is_file()); let requests = requests.lock().map(|r| r.clone()).unwrap_or_default(); let created = format!( r#"PUT /snapshot/create HTTP/1.1 {{"mem_file_path":"{0}/memory","snapshot_path":"{0}/vmstate","snapshot_type":"Full"}}"#, @@ -1861,8 +2199,9 @@ mod tests { "GET / HTTP/1.1 ", ] ); - // The memory the VM booted with, from its configuration. + // The memory and disks the VM booted with, from its configuration. assert_eq!(vm.memory_mb(), None); + assert!(vm.booted_drives().is_none()); std::fs::create_dir_all(&vm.dir)?; let spec = unpack_spec("/k".into(), "/i".into(), "fctl-ses-x", Path::new("/r")); std::fs::write( @@ -1870,11 +2209,219 @@ mod tests { serde_json::to_vec(&vm.config(&spec))?, )?; assert_eq!(vm.memory_mb(), Some(1024)); + let drives = vm.booted_drives().context("no drives")?; + assert_eq!(drives.len(), 1); + assert!(drives[0].path == Path::new("/r") && drives[0].writable); api.abort(); let _ = std::fs::remove_dir_all(&vm.run); Ok(()) } + /// The host takes out of a jail only what Firecracker saved there, as + /// the jail's user: not a link to a file of the host's, not a file with + /// another name (the disk the jail was lent, say), not another user's, + /// nor anything but a file, which it opens without waiting on it. What + /// it takes is the host's user's, and private. + #[test] + fn takes_out_of_a_jail_only_what_firecracker_saved_there() -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + let dir = tempfile::tempdir()?; + let root = dir.path().join("root"); + std::fs::create_dir(&root)?; + let inside = root.join("memory"); + let to = dir.path().join("memory"); + let me = nix::unistd::geteuid().as_raw(); + let take = |uid| take_out(&root, uid, "memory", &to).map_err(|e| e.to_string()); + let hosts = dir.path().join("hosts"); + std::fs::write(&hosts, b"the host's")?; + std::os::unix::fs::symlink(&hosts, &inside)?; + assert_eq!(take(me), Err("memory in the VM's jail isn't a file".into())); + std::fs::remove_file(&inside)?; + let disk = dir.path().join("disk.ext4"); + std::fs::write(&disk, b"disk")?; + std::fs::hard_link(&disk, &inside)?; + assert_eq!( + take(me), + Err("memory in the VM's jail has another name".into()) + ); + std::fs::remove_file(&inside)?; + nix::unistd::mkfifo(&inside, nix::sys::stat::Mode::S_IRWXU)?; + assert_eq!(take(me), Err("memory in the VM's jail isn't a file".into())); + std::fs::remove_file(&inside)?; + std::fs::write(&inside, b"the agent's heap")?; + std::fs::set_permissions(&inside, std::fs::Permissions::from_mode(0o644))?; + assert_eq!( + take(me.wrapping_add(1)), + Err("memory in the VM's jail isn't its user's".into()) + ); + assert!(std::fs::symlink_metadata(&to).is_err()); + assert_eq!(std::fs::read(&hosts)?, b"the host's"); + assert_eq!(std::fs::metadata(&disk)?.nlink(), 1); + // What Firecracker made: out of the jail, private. + take_out(&root, me, "memory", &to)?; + assert!(std::fs::symlink_metadata(&inside).is_err()); + let taken = std::fs::symlink_metadata(&to)?; + assert!(taken.is_file() && taken.nlink() == 1 && taken.uid() == me); + assert_eq!(taken.mode() & 0o777, 0o600); + assert_eq!(std::fs::read(&to)?, b"the agent's heap"); + // Nothing saved: the error says what is missing. + let missing = take_out(&root, me, "vmstate", &dir.path().join("vmstate")); + assert!(missing.is_err_and(|e| e.to_string().contains("vmstate"))); + Ok(()) + } + + /// A jailed VM's state goes through its jail: Firecracker saves it at + /// the root of its chroot, as the jail's user, and the host takes it out + /// to the sandbox's `saved/`, root's; a resume makes a jail around it, + /// with no configuration, the disks the VM booted with where they were + /// in the chroot and the state lent to the jail's user to load, and + /// links the vsock's socket out once Firecracker has made it, as it + /// loads the state. Stand-ins: a jailer that waits, and Firecracker's + /// API, served in the chroot. Jails are root's to make: CI runs this as + /// root. + #[tokio::test] + async fn a_jailed_vms_state_is_saved_and_resumed_through_its_jail() -> Result<()> { + use crate::driver::fake::write_script; + use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt}; + if !nix::unistd::geteuid().is_root() { + eprintln!("only root makes jails: skipped"); + return Ok(()); + } + let dir = tempfile::tempdir()?; + let jails = dir.path().join("jail"); + if let Some(why) = jails_problem(&jails) { + eprintln!("{why}: skipped"); + return Ok(()); + } + let asked = dir.path().join("jailer.args"); + let jail = Jail { + jailer: dir.path().join("jailer"), + }; + write_script( + &jail.jailer, + &format!( + "#!/bin/sh\necho \"$@\" >> '{}'\nsleep 60\n", + asked.display() + ), + )?; + let firecracker = dir.path().join("firecracker-v1.17.0"); + std::fs::write(&firecracker, b"")?; + let file = |name: &str| -> Result { + let path = dir.path().join(name); + std::fs::write(&path, name)?; + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644))?; + Ok(path) + }; + let (kernel, initrd, image) = (file("vmlinux")?, file("initrd")?, file("rootfs.ext4")?); + let vm = Vm::new(dir.path().join("sandboxes/fctl-ses-j"))?.with_jails(jails.clone()); + std::fs::create_dir_all(&vm.dir)?; + let disk = vm.dir.join("disk.ext4"); + std::fs::write(&disk, b"disk")?; + let mut spec = unpack_spec(kernel, initrd, "fctl-ses-j", &image); + spec.drives.insert( + 0, + Drive { + path: image.clone(), + writable: false, + }, + ); + spec.drives[1].path = disk.clone(); + let root = jails.join("firecracker-v1.17.0").join(&vm.key).join("root"); + let owner = |path: &Path| std::fs::symlink_metadata(path).map(|m| m.uid()); + let result = async { + // Booted, its writable disk lent to the jail's user, and its + // configuration kept with the host's paths. + let booting = tokio::spawn(firecracker_in_jail(root.clone(), "firecracker.json", true)); + vm.start(&spec, &firecracker, Some(&jail)).await?; + let (api, requests, _vsock) = booting.await??; + let uid = owner(&root)?; + assert!(uid >= JAIL_ID_BASE, "{uid}"); + assert!(vm.running() && vm.jailed()); + assert_eq!(owner(&disk)?, uid); + assert_eq!(vm.memory_mb(), Some(1024)); + let booted = vm.booted_drives().context("no drives")?; + let booted: Vec<_> = booted.into_iter().map(|d| (d.path, d.writable)).collect(); + assert_eq!(booted, [(image.clone(), false), (disk.clone(), true)]); + // Saved in the chroot, and taken out: what was there by the + // state's names goes first. + let hosts = dir.path().join("hosts"); + std::fs::write(&hosts, b"the host's")?; + std::os::unix::fs::symlink(&hosts, root.join("memory"))?; + let saved = vm.dir.join("saved"); + vm.snapshot(&saved).await?; + assert_eq!(std::fs::read(&hosts)?, b"the host's"); + for name in STATE_FILES { + let file = std::fs::symlink_metadata(saved.join(name))?; + assert!(file.is_file() && file.uid() == 0, "{name}: {file:?}"); + assert_eq!(file.mode() & 0o777, 0o600, "{name}"); + assert!(std::fs::symlink_metadata(root.join(name)).is_err(), "{name}"); + } + assert!(std::fs::read(saved.join("memory"))?.starts_with(b"the agent's heap")); + let asked_for = |requests: &Requests, what: &str| { + requests + .lock() + .map(|r| r.iter().find(|r| r.starts_with(what)).cloned()) + .unwrap_or_default() + }; + assert_eq!( + asked_for(&requests, "PUT /snapshot/create").as_deref(), + Some( + r#"PUT /snapshot/create HTTP/1.1 {"mem_file_path":"/memory","snapshot_path":"/vmstate","snapshot_type":"Full"}"# + ) + ); + // Stopped, as a stop that saved stops it: the jail goes, and + // gives the disk back. + vm.kill().await; + api.abort(); + assert!(!vm.running() && std::fs::symlink_metadata(&root).is_err()); + assert_eq!(owner(&disk)?, 0); + // Resumed in a jail made around the state, which its user owns + // as Firecracker loads it (the stand-in refuses it otherwise). + let resuming = tokio::spawn(firecracker_in_jail(root.clone(), "memory", false)); + vm.restore(&saved, &firecracker, Some(&jail), "fctl-ses-j") + .await?; + let (api, requests, _) = resuming.await??; + let uid = owner(&root)?; + assert!(vm.running() && vm.jailed()); + assert_eq!( + asked_for(&requests, "PUT /snapshot/load").as_deref(), + Some( + r#"PUT /snapshot/load HTTP/1.1 {"mem_backend":{"backend_path":"/memory","backend_type":"File"},"resume_vm":true,"snapshot_path":"/vmstate"}"# + ) + ); + let args = std::fs::read_to_string(&asked)?; + let args: Vec<_> = args.lines().collect(); + assert_eq!(args.len(), 2, "{args:?}"); + assert!(args[0].ends_with("--api-sock /api.sock --config-file /firecracker.json --level Warning")); + assert!(args[1].ends_with("-- --api-sock /api.sock --level Warning")); + // Its sockets, the vsock's made as the state loaded, linked out. + assert!(std::fs::metadata(vm.vsock())?.file_type().is_socket()); + assert!(std::fs::metadata(vm.api())?.file_type().is_socket()); + // The chroot has the disks it booted with, where they were, and + // no longer the state, which Firecracker has loaded; nor the + // kernel or a configuration, which a resume doesn't need. + let ino = |path: &Path| std::fs::metadata(path).map(|m| m.ino()); + assert_eq!(ino(&root.join("drive0.ext4"))?, ino(&image)?); + assert_eq!(ino(&root.join("drive1.ext4"))?, ino(&disk)?); + for gone in ["vmstate", "memory", "vmlinux", "initrd.cpio", "firecracker.json"] { + assert!(std::fs::symlink_metadata(root.join(gone)).is_err(), "{gone}"); + } + // Lent until the VM stops: the disk, and the state, which the + // host lets go once it resumed. + assert_eq!(owner(&disk)?, uid); + assert_eq!(owner(&saved.join("memory"))?, uid); + vm.kill().await; + api.abort(); + for file in [&disk, &saved.join("vmstate"), &saved.join("memory")] { + assert_eq!(owner(file)?, 0, "{file:?}"); + } + Ok::<_, anyhow::Error>(()) + } + .await; + vm.kill().await; + result + } + #[test] fn reads_an_answer_once_all_of_it_is_there() -> Result<()> { let whole = diff --git a/docs/architecture.md b/docs/architecture.md index a79e2b2d..5eff0911 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -3496,9 +3496,12 @@ in `firecracker/` in the host's data directory. goes, and lets the states of stopped sandboxes go first while the disk is low. One another Firecracker release saved, or one that doesn't load, goes too, and the sandbox boots from its disk. A VM that runs through - the jailer saves none yet: Firecracker would write it in the jail's - chroot, which goes as the VM stops, so it boots from its disk. Image - builders save none either: they boot afresh for each build. + the jailer saves its state at the root of its chroot, as the jail's + user, and the host takes it out to `saved/` before the jail goes; it + resumes in a new jail (below). A state saved in the jailer resumes + only in one, and one saved without it only without, so turning + `jailer` on or off lets the states kept before go. Image builders save + none: they boot afresh for each build. - **The VM's init** is fctl-agent, from an initramfs the host makes of its own static fctl-agent (`crates/fctl-agent/src/vm`). It mounts the root, copies itself to `/opt/fctl/bin/fctl-agent`, sets up what images don't @@ -3556,9 +3559,23 @@ checked to be a socket. Its writable disk is lent to the jail's user while it runs, noted in `jail.json` beside the chroot, and given back to root as the VM stops, so that a later jail with the same uid owns nothing; jails of VMs that don't run, which a host that stopped -or crashed left, go before the next VM starts. The jailer needs cgroup v2 -and a filesystem that allows device files (it makes `/dev/kvm` in the -chroot): without either, the driver isn't ready and says what to change. +or crashed left, go before the next VM starts. The host keeps the VM's +configuration with its own paths in the sandbox's directory, for its +size and its disks. A jailed VM's saved state goes through its chroot: +Firecracker writes `vmstate` and `memory` at its root, as the jail's +user, and the host takes each out to `saved/` by what it opened without +following a link, checked to be a regular file of that user's with no +other name (so that the jail can't hand it a host's file, or the disk it +was lent), then gives it to root, readable by root alone. A resume makes +a new jail as a start does, but with no configuration: hard links of the +disks the VM booted with, at the same paths in the chroot, and of the +saved state, lent to the jail's user, who must read it. The host starts +Firecracker in it, has it load the state, and only then links the +vsock's socket out, since Firecracker makes it as the state loads; the +state's links in the chroot go once it is loaded. The jailer needs +cgroup v2 and a filesystem that allows device files (it makes `/dev/kvm` +in the chroot): without either, the driver isn't ready and says what to +change. `passt` runs outside the jail and drops to `nobody` itself; its pid's file goes in `/tmp` under a name with a random part. A jailed VM's cache server listens in the VM's socket directory, whose path stays short, and diff --git a/docs/cli.md b/docs/cli.md index e790de7c..c2227562 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -947,14 +947,15 @@ the session with Claude running where it was rather than starting it again; the host lets those states go first when the disk runs low, and `snapshots = false` keeps none: turned off, it lets those kept before go at its next sweep or the sandbox's next start, rather than resume them. A -host that runs its VMs through the jailer keeps none yet. With Apple's -`container`, a sandbox's disk image keeps the space of the files its -session deleted (a build of fctl leaves about 5.5 GB) until the host -stops the sandbox: then the host gives that space back to the Mac with -`container clean`. That needs Apple's `container` 1.4 or later. With an -older one, a stopped sandbox keeps the most its session ever stored, and -`fctl host logs` says so each time a sandbox stops. `df` in the sandbox -shows its 512 GiB disk image rather than what the Mac has free. +sandbox whose state was kept before `jailer` was turned on or off starts +again from its disk. With Apple's `container`, a sandbox's disk image +keeps the space of the files its session deleted (a build of fctl leaves +about 5.5 GB) until the host stops the sandbox: then the host gives that +space back to the Mac with `container clean`. That needs Apple's +`container` 1.4 or later. With an older one, a stopped sandbox keeps the +most its session ever stored, and `fctl host logs` says so each time a +sandbox stops. `df` in the sandbox shows its 512 GiB disk image rather +than what the Mac has free. The host keeps `min_free_disk_gb` free (20 GiB unless `host.toml` says otherwise) on the disk where its sandboxes are: Apple's `container` app diff --git a/docs/security.md b/docs/security.md index f40ec1f2..c13b9475 100644 --- a/docs/security.md +++ b/docs/security.md @@ -237,8 +237,12 @@ its VM, and nothing outside it except through these channels: in a cgroup of its own, so that an escape reaches neither the host's files nor another VM's. The host reaches the VM's sockets through links of its own, so a jail can't send it to another VM's, and takes the VM's - disk back from the jail's user when it stops. Their images unpack in a - VM of their own, so a crafted layer can't reach the host's filesystem. + disk back from the jail's user when it stops. The state a VM saves as + it stops is written in its chroot, and the host takes it out only as + the files Firecracker made there, never through a link the jail put in + their place, so a jail can't have the host read or change another + file. Their images unpack in a VM of their own, so a crafted layer + can't reach the host's filesystem. - **fctl-agent** runs as root in the sandbox, and Claude and everything it runs as the image's user, so that the agent's commands can't stop