microsandbox 0.7.0

`microsandbox` is the core library for the microsandbox project.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Internal launch selection for released v0.6.x executables.

#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
#[cfg(windows)]
use std::os::windows::io::AsRawHandle;
use std::{
    collections::HashMap,
    fs::File,
    io::Read as _,
    path::{Path, PathBuf},
    process::Stdio,
    sync::LazyLock,
    time::{Duration, SystemTime},
};

use semver::Version;
use tokio::{io::AsyncReadExt, process::Command, sync::Mutex};
#[cfg(windows)]
use windows_sys::Win32::Storage::FileSystem::{
    BY_HANDLE_FILE_INFORMATION, GetFileInformationByHandle,
};

use crate::{MicrosandboxError, MicrosandboxResult};

//--------------------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------------------

static CONTRACTS: LazyLock<Mutex<HashMap<PathBuf, CachedContract>>> =
    LazyLock::new(|| Mutex::new(HashMap::new()));
const PROBE_TIMEOUT: Duration = Duration::from_secs(2);
const MAX_VERSION_OUTPUT: u64 = 4096;

//--------------------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------------------

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct LaunchContract {
    pub patch: u64,
    pub machine: bool,
}

#[derive(Debug, PartialEq, Eq)]
struct FileIdentity {
    object: (u64, u64, u64),
    size: u64,
    modified: SystemTime,
    #[cfg(unix)]
    changed: (i64, i64),
}

struct CachedContract {
    identity: FileIdentity,
    contract: LaunchContract,
    // Retain the object so deleting/replacing a path cannot recycle its file ID.
    _file: File,
}

//--------------------------------------------------------------------------------------------------
// Methods
//--------------------------------------------------------------------------------------------------

impl LaunchContract {
    /// Live resource resizing and its launch capacity flags arrived in v0.6.4.
    pub fn validate_capacity(
        self,
        cpus: u8,
        max_cpus: u8,
        memory: u32,
        max_memory: u32,
    ) -> MicrosandboxResult<()> {
        if self.patch < 4 && (max_cpus > cpus || max_memory > memory) {
            return Err(MicrosandboxError::unsupported(
                crate::error::Operation::SandboxStart,
                crate::error::UnsupportedReason::NotAvailable(
                    "resource hotplug capacity requires runtime v0.6.4 or newer; use fixed CPU and memory sizes with this runtime".into(),
                ),
            ));
        }
        Ok(())
    }

    pub fn legacy_env(self) -> bool {
        self.patch < 10
    }
    #[cfg(any(unix, test))]
    pub fn lifecycle_lock_argument(self) -> bool {
        self.patch >= 9
    }
    pub fn resolved_network(self) -> bool {
        self.patch >= 17
    }

    fn from_version(version: &Version) -> MicrosandboxResult<Self> {
        if version.major == 0 && version.minor == 6 && version.patch <= 18 && version.pre.is_empty()
        {
            Ok(Self {
                patch: version.patch,
                machine: false,
            })
        } else {
            Err(MicrosandboxError::Runtime(format!(
                "no tested sandbox launch contract for runtime {version}"
            )))
        }
    }
}

impl FileIdentity {
    fn capture(file: &File) -> std::io::Result<Self> {
        let metadata = file.metadata()?;
        #[cfg(unix)]
        let object = (metadata.dev(), metadata.ino(), 0);
        #[cfg(windows)]
        let object = {
            let mut info = std::mem::MaybeUninit::<BY_HANDLE_FILE_INFORMATION>::uninit();
            // SAFETY: the file owns this handle; the API initializes `info` on success.
            if unsafe { GetFileInformationByHandle(file.as_raw_handle(), info.as_mut_ptr()) } == 0 {
                return Err(std::io::Error::last_os_error());
            }
            let info = unsafe { info.assume_init() };
            (
                u64::from(info.dwVolumeSerialNumber),
                u64::from(info.nFileIndexHigh),
                u64::from(info.nFileIndexLow),
            )
        };
        Ok(Self {
            object,
            size: metadata.len(),
            modified: metadata.modified()?,
            #[cfg(unix)]
            changed: (metadata.ctime(), metadata.ctime_nsec()),
        })
    }
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

pub(super) async fn resolve(path: &Path) -> MicrosandboxResult<LaunchContract> {
    let path = std::fs::canonicalize(path)?;
    // Serialize cold discovery so concurrent starts share one probe. No failed
    // or canceled discovery is cached, and the lock is released on cancellation.
    let mut contracts = CONTRACTS.lock().await;
    let file = File::open(&path)?;
    let identity = FileIdentity::capture(&file)?;
    let mut magic = [0; 2];
    let wrapper = (&file).read(&mut magic)? == 2 && magic == *b"#!";

    if let Some(cached) = contracts
        .get(&path)
        .filter(|cached| !wrapper && cached.identity == identity)
    {
        return Ok(cached.contract);
    }
    let inspect_path = path.clone();
    let embedded = if wrapper {
        None
    } else {
        tokio::task::spawn_blocking(move || crate::setup::resolve_runtime_version(inspect_path))
            .await
            .map_err(|_| {
                MicrosandboxError::Runtime("runtime version inspection task failed".into())
            })??
    };
    let modern = embedded.is_some();
    let version = match embedded {
        Some(version) => version,
        None => probe(&path).await?,
    };
    let contract = if modern
        && version == Version::parse(env!("CARGO_PKG_VERSION")).expect("Cargo version is semver")
    {
        LaunchContract {
            patch: 18,
            machine: true,
        }
    } else {
        let mut contract = LaunchContract::from_version(&version)?;
        contract.machine = modern;
        contract
    };
    if FileIdentity::capture(&File::open(&path)?)? != identity
        || FileIdentity::capture(&file)? != identity
    {
        return Err(MicrosandboxError::Runtime(
            "runtime executable changed during launch discovery".into(),
        ));
    }
    // A wrapper can select another binary without changing itself. Keep support
    // for it, but do not cache an identity that says nothing about that target.
    if wrapper {
        return Ok(contract);
    }
    // Bound retained handles in long-running hosts that resolve many executables.
    if contracts.len() >= 64 {
        contracts.clear();
    }
    contracts.insert(
        path,
        CachedContract {
            identity,
            contract,
            _file: file,
        },
    );
    Ok(contract)
}

async fn probe(path: &Path) -> MicrosandboxResult<Version> {
    let mut child = Command::new(path)
        .arg("--version")
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .kill_on_drop(true)
        .spawn()?;
    let output = child.stdout.take().ok_or_else(|| {
        MicrosandboxError::Runtime("runtime version probe has no output pipe".into())
    })?;
    let mut output = output.take(MAX_VERSION_OUTPUT + 1);
    let mut bytes = Vec::new();
    let outcome = tokio::time::timeout(PROBE_TIMEOUT, async {
        output.read_to_end(&mut bytes).await?;
        if bytes.len() as u64 > MAX_VERSION_OUTPUT {
            return Err(std::io::Error::other(
                "runtime version output exceeds limit",
            ));
        }
        child.wait().await
    })
    .await;
    let status = match outcome {
        Ok(Ok(status)) => status,
        other => {
            let _ = child.kill().await;
            let _ = child.wait().await;
            return Err(MicrosandboxError::Runtime(
                if other.is_err() {
                    "runtime version probe timed out"
                } else {
                    "runtime version probe failed"
                }
                .into(),
            ));
        }
    };
    if !status.success() {
        return Err(MicrosandboxError::Runtime(
            "runtime version probe exited unsuccessfully".into(),
        ));
    }
    parse_version(&bytes)
}

fn parse_version(bytes: &[u8]) -> MicrosandboxResult<Version> {
    let parsed = std::str::from_utf8(bytes)
        .ok()
        .and_then(|text| text.trim().strip_prefix("msb "))
        .and_then(|value| Version::parse(value).ok());
    parsed.ok_or_else(|| MicrosandboxError::Runtime("unrecognized runtime version response".into()))
}

//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------

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

    #[cfg(unix)]
    fn script(dir: &Path, name: &str, body: &str) -> PathBuf {
        use std::os::unix::fs::PermissionsExt;
        let path = dir.join(name);
        std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).unwrap();
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o700)).unwrap();
        path
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn version_probe_is_bounded_and_never_exposes_output() {
        let dir = tempfile::tempdir().unwrap();
        let path = script(dir.path(), "valid", "printf 'msb 0.6.4\\n'");
        assert_eq!(probe(&path).await.unwrap(), Version::new(0, 6, 4));
        let path = script(dir.path(), "invalid", "printf secret-marker");
        assert!(
            !probe(&path)
                .await
                .unwrap_err()
                .to_string()
                .contains("secret-marker")
        );
        let path = script(dir.path(), "large", "exec /usr/bin/head -c 8192 /dev/zero");
        assert!(probe(&path).await.is_err());
        let path = script(dir.path(), "slow", "exec /bin/sleep 10");
        let start = std::time::Instant::now();
        assert!(
            probe(&path)
                .await
                .unwrap_err()
                .to_string()
                .contains("timed out")
        );
        assert!(start.elapsed() < Duration::from_secs(5));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn a_wrapper_is_reprobed_when_its_target_changes() {
        let dir = tempfile::tempdir().unwrap();
        let version = dir.path().join("version");
        std::fs::write(&version, "msb 0.6.4").unwrap();
        let path = script(
            dir.path(),
            "wrapper",
            &format!("exec /bin/cat '{}'", version.display()),
        );
        assert_eq!(resolve(&path).await.unwrap().patch, 4);
        std::fs::write(&version, "msb 0.6.9").unwrap();
        assert_eq!(resolve(&path).await.unwrap().patch, 9);
    }

    #[tokio::test]
    #[ignore = "requires MSB_LAUNCH_CONTRACT_BINARY naming a real released runtime"]
    async fn released_runtime_contract_and_warm_resolution() {
        let path = PathBuf::from(std::env::var_os("MSB_LAUNCH_CONTRACT_BINARY").unwrap());
        let start = std::time::Instant::now();
        let contract = resolve(&path).await.unwrap();
        let cold = start.elapsed();
        let start = std::time::Instant::now();
        for _ in 0..100 {
            assert_eq!(resolve(&path).await.unwrap(), contract);
        }
        eprintln!(
            "launch contract patch={} cold_us={} warm_mean_us={}",
            contract.patch,
            cold.as_micros(),
            start.elapsed().as_micros() / 100
        );
        assert!(
            CONTRACTS
                .lock()
                .await
                .contains_key(&std::fs::canonicalize(path).unwrap())
        );
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn canceled_discovery_releases_the_cache_and_terminates_its_probe() {
        let dir = tempfile::tempdir().unwrap();
        let pid_path = dir.path().join("probe.pid");
        let path = script(
            dir.path(),
            "slow",
            &format!("echo $$ > '{}'; exec /bin/sleep 30", pid_path.display()),
        );
        let task = tokio::spawn(async move { resolve(&path).await });
        let pid: i32 = tokio::time::timeout(Duration::from_secs(10), async {
            loop {
                // Creation precedes the shell's write, so wait for a complete PID.
                if let Ok(contents) = std::fs::read_to_string(&pid_path)
                    && let Ok(pid) = contents.trim().parse()
                {
                    break pid;
                }
                tokio::time::sleep(Duration::from_millis(5)).await;
            }
        })
        .await
        .unwrap();
        task.abort();
        assert!(task.await.unwrap_err().is_cancelled());
        let valid = script(dir.path(), "valid", "printf 'msb 0.6.4\\n'");
        assert_eq!(
            tokio::time::timeout(Duration::from_secs(5), resolve(&valid))
                .await
                .unwrap()
                .unwrap()
                .patch,
            4
        );
        tokio::time::timeout(Duration::from_secs(2), async {
            // Signal zero checks existence without sending a signal; only this
            // test's recorded probe PID is inspected.
            while unsafe { libc::kill(pid, 0) } == 0 {
                tokio::time::sleep(Duration::from_millis(5)).await;
            }
        })
        .await
        .unwrap();
    }

    #[tokio::test]
    #[ignore = "requires MSB_LAUNCH_CONTRACT_OLD/NEW actual v0.6.4 and v0.6.9 runtime fixtures"]
    async fn released_binary_replacement_invalidates_concurrent_cached_resolution() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir
            .path()
            .join(if cfg!(windows) { "msb.exe" } else { "msb" });
        let replacement = dir.path().join("replacement");
        std::fs::copy(std::env::var_os("MSB_LAUNCH_CONTRACT_OLD").unwrap(), &path).unwrap();
        let (a, b) = tokio::join!(resolve(&path), resolve(&path));
        assert_eq!(a.unwrap().patch, 4);
        assert_eq!(b.unwrap().patch, 4);
        std::fs::copy(
            std::env::var_os("MSB_LAUNCH_CONTRACT_NEW").unwrap(),
            &replacement,
        )
        .unwrap();
        std::fs::rename(replacement, &path).unwrap();
        let (a, b) = tokio::join!(resolve(&path), resolve(&path));
        assert_eq!(a.unwrap().patch, 9);
        assert_eq!(b.unwrap().patch, 9);
        let canonical = std::fs::canonicalize(path).unwrap();
        CONTRACTS.lock().await.remove(&canonical);
    }

    #[test]
    fn released_contract_boundaries_are_explicit() {
        for patch in 0..=18 {
            let contract = LaunchContract::from_version(&Version::new(0, 6, patch)).unwrap();
            assert!(contract.validate_capacity(1, 1, 256, 256).is_ok());
            assert_eq!(
                contract.validate_capacity(1, 2, 256, 256).is_ok(),
                patch >= 4
            );
            assert_eq!(
                contract.validate_capacity(1, 1, 256, 512).is_ok(),
                patch >= 4
            );
            assert_eq!(contract.legacy_env(), patch < 10);
            assert_eq!(contract.lifecycle_lock_argument(), patch >= 9);
            assert_eq!(contract.resolved_network(), patch >= 17);
        }
        for version in ["0.5.0", "0.7.0", "0.6.19", "0.6.9-preview"] {
            assert!(LaunchContract::from_version(&Version::parse(version).unwrap()).is_err());
        }
        assert_eq!(
            parse_version(b"msb 0.6.4\r\n").unwrap(),
            Version::new(0, 6, 4)
        );
        assert!(parse_version(b"secret invalid").is_err());
    }
}