microsandbox-cli 0.6.9

CLI binary for managing microsandbox environments.
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! Reparse-safe host filesystem operations for Windows sandbox copy-out.

use std::ffi::OsStr;
use std::fs::{File, OpenOptions};
use std::io;
use std::os::windows::ffi::OsStrExt;
use std::os::windows::fs::OpenOptionsExt;
use std::os::windows::io::{AsRawHandle, FromRawHandle, RawHandle};
use std::path::{Component, Path};

use anyhow::Context;
use tokio::io::AsyncWriteExt;
use windows_sys::Wdk::Foundation::OBJECT_ATTRIBUTES;
use windows_sys::Wdk::Storage::FileSystem::{
    FILE_CREATE, FILE_DIRECTORY_FILE, FILE_NON_DIRECTORY_FILE, FILE_OPEN, FILE_OPEN_REPARSE_POINT,
    FILE_SYNCHRONOUS_IO_NONALERT, NtCreateFile,
};
use windows_sys::Win32::Foundation::{
    HANDLE, INVALID_HANDLE_VALUE, OBJ_CASE_INSENSITIVE, RtlNtStatusToDosError, UNICODE_STRING,
};
use windows_sys::Win32::Storage::FileSystem::{
    DELETE, FILE_ADD_FILE, FILE_ADD_SUBDIRECTORY, FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_NORMAL,
    FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_REPARSE_POINT, FILE_ATTRIBUTE_TAG_INFO,
    FILE_BASIC_INFO, FILE_DISPOSITION_INFO, FILE_FLAG_BACKUP_SEMANTICS, FILE_LIST_DIRECTORY,
    FILE_READ_ATTRIBUTES, FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_TRAVERSE,
    FILE_WRITE_ATTRIBUTES, FILE_WRITE_DATA, FileAttributeTagInfo, FileBasicInfo,
    FileDispositionInfo, GetFileInformationByHandleEx, SYNCHRONIZE, SetFileInformationByHandle,
};
use windows_sys::Win32::System::IO::IO_STATUS_BLOCK;

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

/// Maximum retries when a destination entry changes during safe replacement.
const MAX_COMPONENT_ATTEMPTS: usize = 128;

/// Share mode used so handles do not unnecessarily block normal filesystem activity.
const SHARE_ALL: u32 = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;

/// Access needed on a pinned directory that will receive copied children.
const DIRECTORY_ACCESS: u32 = FILE_LIST_DIRECTORY
    | FILE_ADD_FILE
    | FILE_ADD_SUBDIRECTORY
    | FILE_TRAVERSE
    | FILE_READ_ATTRIBUTES
    | FILE_WRITE_ATTRIBUTES
    | SYNCHRONIZE;

/// Access needed on a copied regular file.
const FILE_ACCESS: u32 =
    FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE;

/// Access used to inspect an entry without following its reparse point.
const INSPECT_ACCESS: u32 = FILE_READ_ATTRIBUTES | SYNCHRONIZE;

/// Access used to remove a reparse point after verifying the opened handle.
const DELETE_ACCESS: u32 = DELETE | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE;

/// Options common to synchronous, reparse-safe relative opens.
const SAFE_OPEN_OPTIONS: u32 = FILE_OPEN_REPARSE_POINT | FILE_SYNCHRONOUS_IO_NONALERT;

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

/// A destination directory pinned before any guest-derived path is materialized.
pub(super) struct CopyRoot {
    directory: File,
}

/// A regular file opened without traversing any reparse point.
pub(super) struct PendingFile {
    file: tokio::fs::File,
}

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

impl CopyRoot {
    /// Open and pin the operator-selected destination directory.
    ///
    /// Normal Win32 path resolution is intentional here so trusted paths may
    /// contain junctions or symlinks. Every sandbox-derived component below
    /// this handle is opened relative to it with reparse processing disabled.
    pub(super) fn open(path: &Path) -> anyhow::Result<Self> {
        let directory = OpenOptions::new()
            .access_mode(FILE_TRAVERSE | FILE_READ_ATTRIBUTES | SYNCHRONIZE)
            .share_mode(SHARE_ALL)
            .custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
            .open(path)
            .with_context(|| format!("open copy destination root {}", path.display()))?;
        Ok(Self { directory })
    }

    /// Construct a copy root from an already pinned directory.
    pub(super) fn from_directory(directory: File) -> Self {
        Self { directory }
    }

    /// Create or open a directory without processing any reparse component.
    pub(super) fn ensure_directory(&self, path: &Path) -> anyhow::Result<File> {
        let mut current = self
            .directory
            .try_clone()
            .context("duplicate copy root handle")?;

        for component in normal_components(path)? {
            current = open_or_create_directory(&current, component)
                .with_context(|| format!("open destination directory {}", path.display()))?;
        }

        Ok(current)
    }

    /// Open an existing directory without processing any reparse component.
    fn open_directory(&self, path: &Path) -> anyhow::Result<File> {
        let mut current = self
            .directory
            .try_clone()
            .context("duplicate copy root handle")?;

        for component in normal_components(path)? {
            current = open_existing_directory(&current, component)
                .with_context(|| format!("open destination directory {}", path.display()))?;
        }

        Ok(current)
    }

    /// Open or create a regular file without following a reparse point.
    pub(super) fn create_file(&self, path: &Path) -> anyhow::Result<PendingFile> {
        let parent_path = path.parent().unwrap_or_else(|| Path::new(""));
        let parent = self.open_directory(parent_path)?;
        let name = final_component(path)?;
        let file = open_or_create_file(&parent, name)
            .with_context(|| format!("create copied file {}", path.display()))?;

        Ok(PendingFile {
            file: tokio::fs::File::from_std(file),
        })
    }
}

impl PendingFile {
    /// Return the file receiving guest content.
    pub(super) fn file_mut(&mut self) -> &mut tokio::fs::File {
        &mut self.file
    }

    /// Flush copied content and apply the Windows readonly equivalent of the guest mode.
    pub(super) async fn commit(mut self, mode: u32) -> anyhow::Result<()> {
        self.file.flush().await.context("flush copied file")?;
        let file = self.file.into_std().await;
        set_readonly_mode(&file, mode).context("set copied file mode")
    }
}

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

/// Apply the Windows readonly equivalent of guest permission bits to a pinned directory.
pub(super) fn set_directory_mode(directory: &File, mode: u32) -> anyhow::Result<()> {
    set_readonly_mode(directory, mode).context("set copied directory mode")
}

/// Return the normal components of a path relative to the pinned copy root.
fn normal_components(path: &Path) -> anyhow::Result<Vec<&OsStr>> {
    path.components()
        .map(|component| match component {
            Component::Normal(name) => Ok(name),
            _ => anyhow::bail!(
                "destination path {} is not relative to the copy root",
                path.display()
            ),
        })
        .collect()
}

/// Return the final normal component of a root-relative path.
fn final_component(path: &Path) -> anyhow::Result<&OsStr> {
    path.file_name()
        .ok_or_else(|| anyhow::anyhow!("destination path {} has no file name", path.display()))
}

/// Open or create one directory component, replacing only verified reparse points.
fn open_or_create_directory(parent: &File, name: &OsStr) -> io::Result<File> {
    for _ in 0..MAX_COMPONENT_ATTEMPTS {
        match open_relative(
            parent,
            name,
            DIRECTORY_ACCESS,
            FILE_OPEN,
            FILE_ATTRIBUTE_DIRECTORY,
            SAFE_OPEN_OPTIONS | FILE_DIRECTORY_FILE,
        ) {
            Ok(directory) if is_reparse_point(&directory)? => {
                drop(directory);
                remove_reparse_point(parent, name)?;
            }
            Ok(directory) => return Ok(directory),
            Err(open_error) if open_error.kind() == io::ErrorKind::NotFound => {
                match open_relative(
                    parent,
                    name,
                    DIRECTORY_ACCESS,
                    FILE_CREATE,
                    FILE_ATTRIBUTE_DIRECTORY,
                    SAFE_OPEN_OPTIONS | FILE_DIRECTORY_FILE,
                ) {
                    Ok(directory) => return Ok(directory),
                    Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
                    Err(error) => return Err(error),
                }
            }
            Err(open_error) => {
                if remove_if_reparse_point(parent, name)? {
                    continue;
                }
                return Err(open_error);
            }
        }
    }

    Err(io::Error::other(
        "destination directory changed too often during safe creation",
    ))
}

/// Open one existing directory component and reject every reparse point.
fn open_existing_directory(parent: &File, name: &OsStr) -> io::Result<File> {
    let directory = open_relative(
        parent,
        name,
        DIRECTORY_ACCESS,
        FILE_OPEN,
        FILE_ATTRIBUTE_DIRECTORY,
        SAFE_OPEN_OPTIONS | FILE_DIRECTORY_FILE,
    )?;
    if is_reparse_point(&directory)? {
        return Err(io::Error::other(
            "refusing to traverse a destination reparse point",
        ));
    }
    Ok(directory)
}

/// Open or create one regular file, replacing only verified reparse points.
fn open_or_create_file(parent: &File, name: &OsStr) -> io::Result<File> {
    for _ in 0..MAX_COMPONENT_ATTEMPTS {
        match open_relative(
            parent,
            name,
            FILE_ACCESS,
            FILE_OPEN,
            FILE_ATTRIBUTE_NORMAL,
            SAFE_OPEN_OPTIONS | FILE_NON_DIRECTORY_FILE,
        ) {
            Ok(file) if is_reparse_point(&file)? => {
                drop(file);
                remove_reparse_point(parent, name)?;
            }
            Ok(file) => {
                file.set_len(0)?;
                return Ok(file);
            }
            Err(open_error) if open_error.kind() == io::ErrorKind::NotFound => {
                match open_relative(
                    parent,
                    name,
                    FILE_ACCESS,
                    FILE_CREATE,
                    FILE_ATTRIBUTE_NORMAL,
                    SAFE_OPEN_OPTIONS | FILE_NON_DIRECTORY_FILE,
                ) {
                    Ok(file) => return Ok(file),
                    Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
                    Err(error) => return Err(error),
                }
            }
            Err(open_error) => {
                if remove_if_reparse_point(parent, name)? {
                    continue;
                }
                return Err(open_error);
            }
        }
    }

    Err(io::Error::other(
        "destination file changed too often during safe creation",
    ))
}

/// Probe an entry without following it and remove it only if it is still a reparse point.
fn remove_if_reparse_point(parent: &File, name: &OsStr) -> io::Result<bool> {
    let entry = match open_relative(
        parent,
        name,
        INSPECT_ACCESS,
        FILE_OPEN,
        FILE_ATTRIBUTE_NORMAL,
        SAFE_OPEN_OPTIONS,
    ) {
        Ok(entry) => entry,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(true),
        Err(error) => return Err(error),
    };

    if !is_reparse_point(&entry)? {
        return Ok(false);
    }
    drop(entry);
    remove_reparse_point(parent, name)?;
    Ok(true)
}

/// Delete a named entry after reopening and verifying the exact reparse point handle.
fn remove_reparse_point(parent: &File, name: &OsStr) -> io::Result<()> {
    let entry = open_relative(
        parent,
        name,
        DELETE_ACCESS,
        FILE_OPEN,
        FILE_ATTRIBUTE_NORMAL,
        SAFE_OPEN_OPTIONS,
    )?;
    if !is_reparse_point(&entry)? {
        return Err(io::Error::other(
            "destination entry changed while replacing a reparse point",
        ));
    }

    clear_readonly_attribute(&entry)?;
    let disposition = FILE_DISPOSITION_INFO { DeleteFile: true };
    let result = unsafe {
        SetFileInformationByHandle(
            entry.as_raw_handle() as HANDLE,
            FileDispositionInfo,
            std::ptr::from_ref(&disposition).cast(),
            std::mem::size_of::<FILE_DISPOSITION_INFO>() as u32,
        )
    };
    if result == 0 {
        return Err(io::Error::last_os_error());
    }
    drop(entry);
    Ok(())
}

/// Return whether an opened entry is any kind of Windows reparse point.
fn is_reparse_point(file: &File) -> io::Result<bool> {
    let mut information = FILE_ATTRIBUTE_TAG_INFO::default();
    let result = unsafe {
        GetFileInformationByHandleEx(
            file.as_raw_handle() as HANDLE,
            FileAttributeTagInfo,
            std::ptr::from_mut(&mut information).cast(),
            std::mem::size_of::<FILE_ATTRIBUTE_TAG_INFO>() as u32,
        )
    };
    if result == 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(information.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT != 0)
}

/// Clear a readonly attribute before deleting a verified reparse point.
fn clear_readonly_attribute(file: &File) -> io::Result<()> {
    let mut information = basic_information(file)?;
    if information.FileAttributes & FILE_ATTRIBUTE_READONLY != 0 {
        information.FileAttributes &= !FILE_ATTRIBUTE_READONLY;
        if information.FileAttributes == 0 {
            information.FileAttributes = FILE_ATTRIBUTE_NORMAL;
        }
        set_basic_information(file, &information)?;
    }
    Ok(())
}

/// Apply guest write permission as the closest Windows readonly equivalent.
fn set_readonly_mode(file: &File, mode: u32) -> io::Result<()> {
    let mut information = basic_information(file)?;
    if mode & 0o222 == 0 {
        information.FileAttributes |= FILE_ATTRIBUTE_READONLY;
    } else {
        information.FileAttributes &= !FILE_ATTRIBUTE_READONLY;
        if information.FileAttributes == 0 {
            information.FileAttributes = FILE_ATTRIBUTE_NORMAL;
        }
    }
    set_basic_information(file, &information)
}

/// Read basic attributes from an already verified file handle.
fn basic_information(file: &File) -> io::Result<FILE_BASIC_INFO> {
    let mut information = FILE_BASIC_INFO::default();
    let result = unsafe {
        GetFileInformationByHandleEx(
            file.as_raw_handle() as HANDLE,
            FileBasicInfo,
            std::ptr::from_mut(&mut information).cast(),
            std::mem::size_of::<FILE_BASIC_INFO>() as u32,
        )
    };
    if result == 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(information)
}

/// Write basic attributes through an already verified file handle.
fn set_basic_information(file: &File, information: &FILE_BASIC_INFO) -> io::Result<()> {
    let result = unsafe {
        SetFileInformationByHandle(
            file.as_raw_handle() as HANDLE,
            FileBasicInfo,
            std::ptr::from_ref(information).cast(),
            std::mem::size_of::<FILE_BASIC_INFO>() as u32,
        )
    };
    if result == 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(())
}

/// Open or create one name relative to a pinned directory handle.
fn open_relative(
    parent: &File,
    name: &OsStr,
    desired_access: u32,
    disposition: u32,
    attributes: u32,
    options: u32,
) -> io::Result<File> {
    let mut wide_name: Vec<u16> = name.encode_wide().collect();
    if wide_name.is_empty() || wide_name.contains(&0) {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "destination component is empty or contains NUL",
        ));
    }
    let byte_length = wide_name
        .len()
        .checked_mul(std::mem::size_of::<u16>())
        .and_then(|length| u16::try_from(length).ok())
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "component is too long"))?;
    let unicode_name = UNICODE_STRING {
        Length: byte_length,
        MaximumLength: byte_length,
        Buffer: wide_name.as_mut_ptr(),
    };
    let object_attributes = OBJECT_ATTRIBUTES {
        Length: std::mem::size_of::<OBJECT_ATTRIBUTES>() as u32,
        RootDirectory: parent.as_raw_handle() as HANDLE,
        ObjectName: std::ptr::from_ref(&unicode_name),
        Attributes: OBJ_CASE_INSENSITIVE,
        SecurityDescriptor: std::ptr::null(),
        SecurityQualityOfService: std::ptr::null(),
    };
    let mut io_status = IO_STATUS_BLOCK::default();
    let mut handle = INVALID_HANDLE_VALUE;
    let status = unsafe {
        NtCreateFile(
            &mut handle,
            desired_access,
            &object_attributes,
            &mut io_status,
            std::ptr::null(),
            attributes,
            SHARE_ALL,
            disposition,
            options,
            std::ptr::null(),
            0,
        )
    };
    if status < 0 {
        let win32_error = unsafe { RtlNtStatusToDosError(status) };
        return Err(io::Error::from_raw_os_error(win32_error as i32));
    }
    if handle == INVALID_HANDLE_VALUE || handle.is_null() {
        return Err(io::Error::other(
            "NtCreateFile succeeded without returning a handle",
        ));
    }

    Ok(unsafe { File::from_raw_handle(handle as RawHandle) })
}

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

#[cfg(test)]
mod tests {
    use std::os::windows::fs::{symlink_dir, symlink_file};

    use tokio::io::AsyncWriteExt;

    use super::*;

    #[tokio::test]
    async fn copied_file_replaces_symlink_without_following_it() {
        let temp = tempfile::tempdir().unwrap();
        let root_path = temp.path().join("root");
        let victim_path = temp.path().join("victim");
        std::fs::create_dir(&root_path).unwrap();
        std::fs::write(&victim_path, b"original").unwrap();
        if symlink_file(&victim_path, root_path.join("payload")).is_err() {
            // Symlink creation requires Developer Mode or elevated privileges
            // on older Windows installations.
            return;
        }

        let root = CopyRoot::open(&root_path).unwrap();
        let mut pending = root.create_file(Path::new("payload")).unwrap();
        pending.file_mut().write_all(b"guest data").await.unwrap();
        pending.commit(0o644).await.unwrap();

        assert_eq!(std::fs::read(&victim_path).unwrap(), b"original");
        assert_eq!(
            std::fs::read(root_path.join("payload")).unwrap(),
            b"guest data"
        );
        assert!(
            !std::fs::symlink_metadata(root_path.join("payload"))
                .unwrap()
                .file_type()
                .is_symlink()
        );
    }

    #[test]
    fn intermediate_directory_replaces_symlink_without_traversing_it() {
        let temp = tempfile::tempdir().unwrap();
        let root_path = temp.path().join("root");
        let outside_path = temp.path().join("outside");
        std::fs::create_dir(&root_path).unwrap();
        std::fs::create_dir(&outside_path).unwrap();
        if symlink_dir(&outside_path, root_path.join("directory")).is_err() {
            return;
        }

        let root = CopyRoot::open(&root_path).unwrap();
        root.ensure_directory(Path::new("directory/child")).unwrap();

        assert!(root_path.join("directory/child").is_dir());
        assert!(!outside_path.join("child").exists());
    }
}