Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix sandboxing in `read_link` on Windows (#14512)

Alex Crichton47e6ba6abc76
diff --git a/crates/wasi/Cargo.toml b/crates/wasi/Cargo.toml
index 5dc3167d24..051e72f211 100644
--- a/crates/wasi/Cargo.toml
+++ b/crates/wasi/Cargo.toml
@@ -57,6 +57,7 @@ features = [
"Win32_System_IO",
"Win32_System_Ioctl",
"Win32_System_Performance",
+ "Win32_System_SystemServices",
]
[features]
diff --git a/crates/wasi/src/filesystem/primitives/tests/fs.rs b/crates/wasi/src/filesystem/primitives/tests/fs.rs
index b2be92453c..8a5313de4a 100644
--- a/crates/wasi/src/filesystem/primitives/tests/fs.rs
+++ b/crates/wasi/src/filesystem/primitives/tests/fs.rs
@@ -864,6 +864,37 @@ fn read_link_absolute() {
);
}
+/// Paths which resolve outside of `start` are refused, regardless of whether
+/// what they name exists or is a symlink.
+#[test]
+fn read_link_escape() {
+ let tmpdir = tmpdir();
+ let root = h::dir_of(&tmpdir);
+ check!(h::create_dir(&root, "sandbox"));
+ check!(h::create_dir(&root, "sandbox/sub"));
+ check!(h::create(&root, "secret"));
+ let start = check!(p::open(&root, Path::new("sandbox"), &p::dir_options()));
+
+ let mut paths = vec![
+ PathBuf::from("../secret"),
+ PathBuf::from("../nope"),
+ PathBuf::from("sub/../../secret"),
+ tmpdir.path().join("secret"),
+ tmpdir.path().join("nope"),
+ ];
+ if got_symlink_permission(&root) {
+ check!(h::symlink_file(&root, &"secret", &"link"));
+ paths.push(PathBuf::from("../link"));
+ paths.push(tmpdir.path().join("link"));
+ }
+ for path in paths {
+ error_contains!(
+ p::read_link(&start, &path),
+ "a path led outside of the filesystem"
+ );
+ }
+}
+
#[test]
fn links_work() {
let tmpdir = tmpdir();
diff --git a/crates/wasi/src/filesystem/primitives/windows/read_link.rs b/crates/wasi/src/filesystem/primitives/windows/read_link.rs
index 834a9145e3..5bdc882dbe 100644
--- a/crates/wasi/src/filesystem/primitives/windows/read_link.rs
+++ b/crates/wasi/src/filesystem/primitives/windows/read_link.rs
@@ -1,10 +1,126 @@
-use super::get_path::concatenate;
+use crate::filesystem::primitives::{FollowSymlinks, OpenOptions, OpenOptionsExt, open};
+use std::ffi::OsString;
+use std::mem::{MaybeUninit, offset_of};
+use std::os::windows::ffi::OsStringExt;
+use std::os::windows::io::AsRawHandle;
use std::path::{Path, PathBuf};
-use std::{fs, io};
+use std::{fs, io, ptr, slice};
+use windows_sys::Wdk::Storage::FileSystem::{
+ REPARSE_DATA_BUFFER, REPARSE_DATA_BUFFER_0_0, REPARSE_DATA_BUFFER_0_1, SYMLINK_FLAG_RELATIVE,
+};
+use windows_sys::Win32::Storage::FileSystem::{
+ FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT, MAXIMUM_REPARSE_DATA_BUFFER_SIZE,
+};
+use windows_sys::Win32::System::IO::DeviceIoControl;
+use windows_sys::Win32::System::Ioctl::FSCTL_GET_REPARSE_POINT;
+use windows_sys::Win32::System::SystemServices::{
+ IO_REPARSE_TAG_MOUNT_POINT, IO_REPARSE_TAG_SYMLINK,
+};
-/// *Unsandboxed* function similar to `read_link`, but which does not perform
-/// sandboxing.
+/// Perform a `readlinkat`-like operation, ensuring that the resolution of the
+/// path never escapes the directory tree rooted at `start`.
+///
+/// The link itself is opened with the sandboxed `open`, without following it,
+/// and its target is read from the reparse point data of the opened handle.
+/// Whether the target is absolute is not checked here; that's left to the
+/// caller.
pub(crate) fn read_link(start: &fs::File, path: &Path) -> io::Result<PathBuf> {
- let out_path = concatenate(start, path)?;
- fs::read_link(out_path)
+ // Open the link with no access mode, instead of generic read.
+ // By default FILE_LIST_DIRECTORY is denied for the junction "C:\Documents and
+ // Settings", so this is needed for a common case.
+ let mut opts = OpenOptions::new();
+ opts.access_mode(0);
+ opts.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS);
+ opts.follow(FollowSymlinks::No);
+ let file = open(start, path, &opts)?;
+ read_reparse_point(&file)
+}
+
+/// Reads the target of the symlink or junction that `file` is a handle to.
+fn read_reparse_point(file: &fs::File) -> io::Result<PathBuf> {
+ #[repr(C, align(8))]
+ struct Buf([MaybeUninit<u8>; MAXIMUM_REPARSE_DATA_BUFFER_SIZE as usize]);
+
+ let mut space = Buf([MaybeUninit::uninit(); MAXIMUM_REPARSE_DATA_BUFFER_SIZE as usize]);
+ let mut bytes = 0;
+ let ok = unsafe {
+ DeviceIoControl(
+ file.as_raw_handle(),
+ FSCTL_GET_REPARSE_POINT,
+ ptr::null(),
+ 0,
+ space.0.as_mut_ptr().cast(),
+ space.0.len() as u32,
+ &mut bytes,
+ ptr::null_mut(),
+ )
+ };
+ if ok == 0 {
+ return Err(io::Error::last_os_error());
+ }
+ let bytes = bytes as usize;
+ let buf = space.0.as_ptr().cast::<REPARSE_DATA_BUFFER>();
+ let data = offset_of!(REPARSE_DATA_BUFFER, Anonymous);
+
+ // SAFETY: each field read below is first checked to be within the `bytes`
+ // that the kernel initialized, and `Buf` is suitably aligned for
+ // `REPARSE_DATA_BUFFER`.
+ unsafe {
+ if bytes < data {
+ return Err(invalid_reparse_data());
+ }
+ let (subst_off, subst_len, path_buffer, relative) = match (*buf).ReparseTag {
+ IO_REPARSE_TAG_SYMLINK => {
+ let path_buffer = data + offset_of!(REPARSE_DATA_BUFFER_0_0, PathBuffer);
+ if bytes < path_buffer {
+ return Err(invalid_reparse_data());
+ }
+ let info = &(*buf).Anonymous.SymbolicLinkReparseBuffer;
+ (
+ info.SubstituteNameOffset,
+ info.SubstituteNameLength,
+ path_buffer,
+ info.Flags & SYMLINK_FLAG_RELATIVE != 0,
+ )
+ }
+ IO_REPARSE_TAG_MOUNT_POINT => {
+ let path_buffer = data + offset_of!(REPARSE_DATA_BUFFER_0_1, PathBuffer);
+ if bytes < path_buffer {
+ return Err(invalid_reparse_data());
+ }
+ let info = &(*buf).Anonymous.MountPointReparseBuffer;
+ (
+ info.SubstituteNameOffset,
+ info.SubstituteNameLength,
+ path_buffer,
+ false,
+ )
+ }
+ _ => {
+ return Err(io::Error::new(
+ io::ErrorKind::Other,
+ "Unsupported reparse point type",
+ ));
+ }
+ };
+
+ let start = path_buffer + usize::from(subst_off);
+ let end = start + usize::from(subst_len);
+ if end > bytes || start % 2 != 0 || end % 2 != 0 {
+ return Err(invalid_reparse_data());
+ }
+ let base = space.0.as_ptr().cast::<u8>();
+ let mut subst = slice::from_raw_parts(base.add(start).cast::<u16>(), (end - start) / 2);
+
+ // Absolute paths start with an NT internal namespace prefix `\??\`.
+ // We should not let it leak through.
+ if !relative && subst.starts_with(&['\\' as u16, '?' as u16, '?' as u16, '\\' as u16]) {
+ subst = &subst[4..];
+ }
+ Ok(PathBuf::from(OsString::from_wide(subst)))
+ }
+}
+
+fn invalid_reparse_data() -> io::Error {
+ io::Error::new(io::ErrorKind::InvalidData, "malformed reparse point data")
}