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
//! This defines `mkdir`, the primary entrypoint to sandboxed directory creation.

#[cfg(racy_asserts)]
use crate::fs::{
    canonicalize, map_result, mkdir_unchecked, stat_unchecked, FollowSymlinks, Metadata,
};
use crate::fs::{mkdir_impl, DirOptions};
use std::{fs, io, path::Path};

/// Perform a `mkdirat`-like operation, ensuring that the resolution of the path
/// never escapes the directory tree rooted at `start`.
#[cfg_attr(not(racy_asserts), allow(clippy::let_and_return))]
#[inline]
pub fn mkdir(start: &fs::File, path: &Path, options: &DirOptions) -> io::Result<()> {
    #[cfg(racy_asserts)]
    let stat_before = stat_unchecked(start, path, FollowSymlinks::No);

    // Call the underlying implementation.
    let result = mkdir_impl(start, path, options);

    #[cfg(racy_asserts)]
    let stat_after = stat_unchecked(start, path, FollowSymlinks::No);

    #[cfg(racy_asserts)]
    check_mkdir(start, path, options, &stat_before, &result, &stat_after);

    result
}

#[cfg(racy_asserts)]
#[allow(clippy::enum_glob_use)]
fn check_mkdir(
    start: &fs::File,
    path: &Path,
    options: &DirOptions,
    stat_before: &io::Result<Metadata>,
    result: &io::Result<()>,
    stat_after: &io::Result<Metadata>,
) {
    use io::ErrorKind::*;

    match (
        map_result(stat_before),
        map_result(result),
        map_result(stat_after),
    ) {
        (Err((NotFound, _)), Ok(()), Ok(metadata)) => {
            assert!(metadata.is_dir());
            assert_same_file_metadata!(
                &stat_unchecked(
                    start,
                    &canonicalize(start, path).unwrap(),
                    FollowSymlinks::No
                )
                .unwrap(),
                &metadata
            );
        }

        (Ok(metadata_before), Err((AlreadyExists, _)), Ok(metadata_after)) => {
            assert_same_file_metadata!(&metadata_before, &metadata_after);
        }

        (_, Err((kind, message)), _) => match map_result(&canonicalize(start, path)) {
            Ok(canon) => match map_result(&mkdir_unchecked(start, &canon, options)) {
                Err((unchecked_kind, unchecked_message)) => {
                    assert_eq!(
                        kind,
                        unchecked_kind,
                        "unexpected error kind from mkdir start='{:?}', \
                         path='{}':\nstat_before={:#?}\nresult={:#?}\nstat_after={:#?}",
                        start,
                        path.display(),
                        stat_before,
                        result,
                        stat_after
                    );
                    assert_eq!(message, unchecked_message);
                }
                _ => panic!("unsandboxed mkdir success"),
            },
            Err((_canon_kind, _canon_message)) => {
                /* TODO: Check error messages
                assert_eq!(kind, canon_kind);
                assert_eq!(message, canon_message);
                */
            }
        },

        other => panic!(
            "inconsistent mkdir checks: start='{:?}' path='{}':\n{:#?}",
            start,
            path.display(),
            other,
        ),
    }
}