|
1 | | -//! Snapshot metadata and the opaque input of a future VM restore. |
| 1 | +//! Strict snapshot directories, compatibility keys, manifests and restore-time eligibility. |
| 2 | +//! |
| 3 | +//! [`Snapshot::open`] checks the one snapshot format without reading its informational |
| 4 | +//! description. The caller keeps the directory and its entries private and immutable while |
| 5 | +//! using the opened snapshot. |
2 | 6 | //! |
3 | 7 | //! [`SnapshotKey`] derives a compatibility key from a capture's class and manifest. |
4 | 8 | //! [`SnapshotManifest::read`] reads the manifest that OpenVMM wrote, and |
@@ -31,15 +35,192 @@ pub(super) mod manifest; |
31 | 35 | pub(crate) mod tests; |
32 | 36 | mod wire; |
33 | 37 |
|
| 38 | +use std::ffi::OsString; |
| 39 | +use std::fs; |
| 40 | +use std::io; |
| 41 | +use std::path::{Path, PathBuf}; |
| 42 | + |
| 43 | +use crate::host::paths::{absolute, normalized}; |
| 44 | +use crate::host::store::regular; |
| 45 | +use crate::host::{filesystem, platform}; |
| 46 | +use crate::{Error, Result}; |
| 47 | + |
34 | 48 | pub use self::eligibility::{Downtime, DowntimeSource, Eligibility, HostTime, MAX_DOWNTIME_NS}; |
35 | 49 | pub use self::key::{CaptureClass, ClassIdentity, Invariants, KeyIdentity, SnapshotKey}; |
36 | 50 | pub use self::manifest::{ |
37 | 51 | BlockGeometry, HostClock, MANIFEST_NAME, MAX_MANIFEST_LEN, SnapshotManifest, TimeContract, |
38 | 52 | }; |
39 | 53 |
|
40 | | -/// An opaque snapshot input. Creating a VM from a snapshot is not supported yet. |
| 54 | +/// The canonical snapshot key. |
| 55 | +pub const KEY_FILE: &str = "key.bin"; |
| 56 | +/// The informational description, whose contents [`Snapshot::open`] never reads. |
| 57 | +pub const DESCRIPTION_FILE: &str = "snapshot.json"; |
| 58 | +/// The directory of OpenVMM's manifest, RAM and saved state. |
| 59 | +pub const OPENVMM_DIR: &str = "openvmm"; |
| 60 | + |
| 61 | +/// A checked snapshot directory and its verified metadata, for a future VM restore. |
| 62 | +/// |
| 63 | +/// The caller must keep the directory and its entries private and immutable while using this |
| 64 | +/// value. Opening checks them once; it neither watches them nor holds their files open. |
41 | 65 | #[derive(Debug)] |
42 | 66 | pub struct Snapshot { |
43 | | - /// Reserved for snapshot metadata. |
44 | | - pub(crate) _private: (), |
| 67 | + /// The absolute directory path, with its components preserved rather than its links resolved. |
| 68 | + #[cfg_attr( |
| 69 | + not(test), |
| 70 | + expect(dead_code, reason = "SL2.15 uses the snapshot directory (#1103)") |
| 71 | + )] |
| 72 | + pub(crate) dir: PathBuf, |
| 73 | + /// The bounded canonical key checked against the manifest. |
| 74 | + key: SnapshotKey, |
| 75 | + /// The strictly parsed metadata of OpenVMM's snapshot. |
| 76 | + manifest: SnapshotManifest, |
| 77 | +} |
| 78 | + |
| 79 | +impl Snapshot { |
| 80 | + /// Opens the one strict snapshot format without changing any entry. |
| 81 | + /// |
| 82 | + /// `dir` must be a private directory containing exactly [`KEY_FILE`], [`DESCRIPTION_FILE`] |
| 83 | + /// and [`OPENVMM_DIR`]. The latter must be a private directory containing exactly |
| 84 | + /// [`MANIFEST_NAME`], `memory.bin` and `state.bin`. Every file must be regular, not a link or |
| 85 | + /// reparse point, and private to its owner. The manifest is read by |
| 86 | + /// [`SnapshotManifest::read`], its artifact lengths must match, and the bounded canonical |
| 87 | + /// key must be consistent with it. The description is required but its contents are never |
| 88 | + /// read, parsed or trusted. |
| 89 | + /// |
| 90 | + /// The caller must keep the directory and its entries private and immutable while using |
| 91 | + /// the returned snapshot. This check retains no open file handles or lock. |
| 92 | + /// |
| 93 | + /// Fails with [`ErrorKind::Io`](crate::ErrorKind::Io) for an unreadable or invalid directory, |
| 94 | + /// entry, length or key relationship, and otherwise as [`SnapshotManifest::read`] and |
| 95 | + /// [`SnapshotKey::from_bytes`] do. |
| 96 | + pub fn open(dir: &Path) -> Result<Self> { |
| 97 | + let dir = absolute(&normalized(dir)).map_err(|error| entry_error(dir, error))?; |
| 98 | + check_directory(&dir, &[KEY_FILE, DESCRIPTION_FILE, OPENVMM_DIR])?; |
| 99 | + let openvmm = dir.join(OPENVMM_DIR); |
| 100 | + check_directory(&openvmm, &[MANIFEST_NAME, "memory.bin", "state.bin"])?; |
| 101 | + for path in [ |
| 102 | + dir.join(KEY_FILE), |
| 103 | + dir.join(DESCRIPTION_FILE), |
| 104 | + openvmm.join(MANIFEST_NAME), |
| 105 | + ] { |
| 106 | + check_file(&path)?; |
| 107 | + } |
| 108 | + let manifest = SnapshotManifest::read(&openvmm)?; |
| 109 | + for (name, expected) in [ |
| 110 | + ("memory.bin", manifest.memory_len()), |
| 111 | + ("state.bin", manifest.state_len()), |
| 112 | + ] { |
| 113 | + let path = openvmm.join(name); |
| 114 | + let found = check_file(&path)?.len(); |
| 115 | + if found != expected { |
| 116 | + return Err(Error::io(format!( |
| 117 | + "the snapshot artifact {} is {found} bytes long, not the {expected} bytes \ |
| 118 | + that its manifest records", |
| 119 | + path.display() |
| 120 | + ))); |
| 121 | + } |
| 122 | + } |
| 123 | + let path = dir.join(KEY_FILE); |
| 124 | + let bytes = regular::open_regular(&path) |
| 125 | + .and_then(|file| { |
| 126 | + regular::read_at_most(file, SnapshotKey::MAX_LEN as u64, "a snapshot key") |
| 127 | + }) |
| 128 | + .map_err(|error| entry_error(&path, error))?; |
| 129 | + let key = SnapshotKey::from_bytes(&bytes)?; |
| 130 | + key.check_manifest(&manifest)?; |
| 131 | + Ok(Self { dir, key, manifest }) |
| 132 | + } |
| 133 | + |
| 134 | + /// Returns the verified snapshot key. |
| 135 | + pub fn key(&self) -> &SnapshotKey { |
| 136 | + &self.key |
| 137 | + } |
| 138 | + |
| 139 | + /// Returns the strictly parsed OpenVMM manifest. |
| 140 | + pub fn manifest(&self) -> &SnapshotManifest { |
| 141 | + &self.manifest |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/// Names the failing snapshot entry while retaining its I/O error. |
| 146 | +fn entry_error(path: &Path, error: io::Error) -> Error { |
| 147 | + Error::io(format!("cannot examine snapshot entry {}", path.display())).with_source(error) |
| 148 | +} |
| 149 | + |
| 150 | +/// Checks one private directory and its exact fixed entry-name inventory. |
| 151 | +fn check_directory(path: &Path, expected: &[&str]) -> Result<()> { |
| 152 | + if let Some(problem) = |
| 153 | + platform::check_snapshot_dir(path).map_err(|error| entry_error(path, error))? |
| 154 | + { |
| 155 | + return Err(Error::io(format!( |
| 156 | + "the snapshot directory {} {problem}", |
| 157 | + path.display() |
| 158 | + ))); |
| 159 | + } |
| 160 | + let mut found = fs::read_dir(path) |
| 161 | + .and_then(|entries| { |
| 162 | + entries |
| 163 | + .map(|entry| Ok(entry?.file_name())) |
| 164 | + .collect::<io::Result<Vec<_>>>() |
| 165 | + }) |
| 166 | + .map_err(|error| entry_error(path, error))?; |
| 167 | + let mut expected: Vec<OsString> = expected.iter().map(|name| OsString::from(*name)).collect(); |
| 168 | + found.sort(); |
| 169 | + expected.sort(); |
| 170 | + if found != expected { |
| 171 | + return Err(Error::io(format!( |
| 172 | + "the snapshot directory {} holds {found:?}, not exactly {expected:?}", |
| 173 | + path.display() |
| 174 | + ))); |
| 175 | + } |
| 176 | + Ok(()) |
| 177 | +} |
| 178 | + |
| 179 | +/// Checks a regular private file from metadata alone, without reading its contents. |
| 180 | +fn check_file(path: &Path) -> Result<fs::Metadata> { |
| 181 | + #[cfg(windows)] |
| 182 | + let file = { |
| 183 | + use std::os::windows::fs::OpenOptionsExt; |
| 184 | + use windows_sys::Win32::Storage::FileSystem::{ |
| 185 | + FILE_FLAG_OPEN_REPARSE_POINT, FILE_READ_ATTRIBUTES, READ_CONTROL, |
| 186 | + }; |
| 187 | + |
| 188 | + fs::OpenOptions::new() |
| 189 | + .access_mode(READ_CONTROL | FILE_READ_ATTRIBUTES) |
| 190 | + .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT) |
| 191 | + .open(path) |
| 192 | + .map_err(|error| entry_error(path, error))? |
| 193 | + }; |
| 194 | + #[cfg(windows)] |
| 195 | + let metadata = file.metadata().map_err(|error| entry_error(path, error))?; |
| 196 | + #[cfg(not(windows))] |
| 197 | + let metadata = fs::symlink_metadata(path).map_err(|error| entry_error(path, error))?; |
| 198 | + if !metadata.is_file() || filesystem::is_reparse_point(&metadata) { |
| 199 | + return Err(Error::io(format!( |
| 200 | + "the snapshot entry {} is not a regular file, or is a link or reparse point", |
| 201 | + path.display() |
| 202 | + ))); |
| 203 | + } |
| 204 | + #[cfg(unix)] |
| 205 | + { |
| 206 | + use std::os::unix::fs::MetadataExt; |
| 207 | + |
| 208 | + if metadata.uid() != nix::unistd::Uid::effective().as_raw() || metadata.mode() & 0o077 != 0 |
| 209 | + { |
| 210 | + return Err(Error::io(format!( |
| 211 | + "the snapshot file {} is not private to the current user", |
| 212 | + path.display() |
| 213 | + ))); |
| 214 | + } |
| 215 | + } |
| 216 | + #[cfg(windows)] |
| 217 | + if let Some(problem) = |
| 218 | + platform::private_file_problem(&file).map_err(|error| entry_error(path, error))? |
| 219 | + { |
| 220 | + return Err(Error::io(format!( |
| 221 | + "the snapshot file {} {problem}", |
| 222 | + path.display() |
| 223 | + ))); |
| 224 | + } |
| 225 | + Ok(metadata) |
45 | 226 | } |
0 commit comments