A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Start to trim down `filesystem::primitives` (#14246)
Alex Crichtonbf330493f435
diff --git a/crates/wasi/src/filesystem/primitives/create_dir.rs b/crates/wasi/src/filesystem/primitives/create_dir.rsdeleted file mode 100644index 0d33961817..0000000000--- a/crates/wasi/src/filesystem/primitives/create_dir.rs+++ /dev/null@@ -1,14 +0,0 @@-//! This defines `create_dir`, the primary entrypoint to sandboxed directory-//! creation.--use crate::filesystem::primitives::{DirOptions, create_dir_impl};-use std::path::Path;-use std::{fs, io};--/// Perform a `mkdirat`-like operation, ensuring that the resolution of the-/// path never escapes the directory tree rooted at `start`.-#[inline]-pub fn create_dir(start: &fs::File, path: &Path, options: &DirOptions) -> io::Result<()> {- // Call the underlying implementation.- create_dir_impl(start, path, options)-}diff --git a/crates/wasi/src/filesystem/primitives/follow_symlinks.rs b/crates/wasi/src/filesystem/primitives/follow_symlinks.rsdeleted file mode 100644index 55774d4ae7..0000000000--- a/crates/wasi/src/filesystem/primitives/follow_symlinks.rs+++ /dev/null@@ -1,16 +0,0 @@-/// Should symlinks be followed in the last component of a path?-///-/// This doesn't affect path components other than the last. So for example in-/// "foo/bar/baz", if "foo" or "bar" are symlinks, they will always be-/// followed. This enum value only determines whether "baz" is followed.-///-/// Instead of passing bare `bool`s as parameters, pass a distinct enum so that-/// the intent is clear.-#[derive(Copy, Clone, Debug, Eq, PartialEq)]-pub enum FollowSymlinks {- /// Yes, do follow symlinks in the last component of a path.- Yes,-- /// No, do not follow symlinks in the last component of a path.- No,-}diff --git a/crates/wasi/src/filesystem/primitives/hard_link.rs b/crates/wasi/src/filesystem/primitives/hard_link.rsdeleted file mode 100644index e46bd3c7b1..0000000000--- a/crates/wasi/src/filesystem/primitives/hard_link.rs+++ /dev/null@@ -1,19 +0,0 @@-//! This defines `hard_link`, the primary entrypoint to sandboxed hard-link-//! creation.--use crate::filesystem::primitives::hard_link_impl;-use std::path::Path;-use std::{fs, io};--/// Perform a `linkat`-like operation, ensuring that the resolution of the path-/// never escapes the directory tree rooted at `start`.-#[inline]-pub fn hard_link(- old_start: &fs::File,- old_path: &Path,- new_start: &fs::File,- new_path: &Path,-) -> io::Result<()> {- // Call the underlying implementation.- hard_link_impl(old_start, old_path, new_start, new_path)-}diff --git a/crates/wasi/src/filesystem/primitives/mod.rs b/crates/wasi/src/filesystem/primitives/mod.rsindex 6ce84fb66e..3756ca09c7 100644--- a/crates/wasi/src/filesystem/primitives/mod.rs+++ b/crates/wasi/src/filesystem/primitives/mod.rs@@ -17,70 +17,172 @@reason = "preexisting from when cap-primitives was imported")]-mod create_dir;+use std::path::{Path, PathBuf};+use std::{fs, io};+mod dir_entry;mod dir_options;mod file_type;-mod follow_symlinks;-mod hard_link;mod maybe_owned_file;mod metadata;-mod open;-mod open_dir;mod open_options;mod open_unchecked_error;mod read_dir;-mod read_link;-mod remove_dir;-mod remove_file;-mod rename;-mod set_times;-mod stat;-mod symlink;--pub(crate) mod errors;-pub(crate) mod manually;-pub(crate) mod via_parent;-use maybe_owned_file::MaybeOwnedFile;+mod errors;+mod manually;+mod via_parent;-pub(crate) use open_unchecked_error::*;+#[cfg(test)]+mod tests;#[cfg(not(windows))]mod rustix;#[cfg(not(windows))]-pub(crate) use self::rustix::fs::*;+use self::rustix::fs as sys;#[cfg(windows)]mod windows;#[cfg(windows)]-pub(crate) use self::windows::fs::*;+use self::windows::fs as sys;-pub use create_dir::create_dir;-pub use dir_entry::DirEntry;-pub use dir_options::DirOptions;#[cfg(windows)]-pub use file_type::_WindowsFileTypeExt;-pub use file_type::FileType;+use file_type::_WindowsFileTypeExt;+use maybe_owned_file::MaybeOwnedFile;+use open_unchecked_error::*;+use sys::read_link_impl as read_link_contents;+use sys::*;++pub(crate) use dir_entry::DirEntry;+pub(crate) use dir_options::DirOptions;+pub(crate) use file_type::FileType;#[cfg(any(unix, target_os = "vxworks"))]-pub use file_type::FileTypeExt;-pub use follow_symlinks::FollowSymlinks;-pub use hard_link::hard_link;+pub(crate) use file_type::FileTypeExt;#[cfg(windows)]-pub use metadata::_WindowsByHandle;-pub use metadata::{Metadata, MetadataExt};-pub use open::open;-pub use open_dir::*;-pub use open_options::*;-pub use read_dir::read_base_dir;-pub use read_link::read_link;-pub use remove_dir::remove_dir;-pub use remove_file::remove_file;-pub use rename::rename;-pub use set_times::{set_times, set_times_nofollow};-pub use stat::stat;+pub(crate) use metadata::_WindowsByHandle;+pub(crate) use metadata::{Metadata, MetadataExt};+pub(crate) use open_options::*;+pub(crate) use read_dir::read_base_dir;+pub(crate) use sys::create_dir_impl as create_dir;+pub(crate) use sys::hard_link_impl as hard_link;+pub(crate) use sys::open_ambient_dir_impl as open_ambient_dir;+pub(crate) use sys::open_impl as open;+pub(crate) use sys::remove_dir_impl as remove_dir;+pub(crate) use sys::remove_file_impl as remove_file;+pub(crate) use sys::rename_impl as rename;+pub(crate) use sys::set_times_impl as set_times;+pub(crate) use sys::set_times_nofollow_impl as set_times_nofollow;+pub(crate) use sys::stat_impl as stat;++/// Should symlinks be followed in the last component of a path?+///+/// This doesn't affect path components other than the last. So for example in+/// "foo/bar/baz", if "foo" or "bar" are symlinks, they will always be+/// followed. This enum value only determines whether "baz" is followed.+///+/// Instead of passing bare `bool`s as parameters, pass a distinct enum so that+/// the intent is clear.+#[derive(Copy, Clone, Debug, Eq, PartialEq)]+pub(crate) enum FollowSymlinks {+ /// Yes, do follow symlinks in the last component of a path.+ Yes,++ /// No, do not follow symlinks in the last component of a path.+ No,+}++/// Like [`read_link_contents`], but additionally verifies that the link target+/// is not absolute.+#[inline]+pub(crate) fn read_link(start: &fs::File, path: &Path) -> io::Result<PathBuf> {+ let contents = read_link_contents(start, path)?;++ // Don't allow reading symlinks to absolute paths. This isn't strictly+ // necessary to preserve the sandbox, since `open` will refuse to follow+ // absolute paths in any case. However, it is useful to enforce this+ // restriction to avoid leaking information about the host filesystem+ // outside the sandbox.+ if contents.has_root() {+ return Err(errors::escape_attempt());+ }++ Ok(contents)+}++/// Perform a `symlinkat`-like operation, ensuring that the resolution of the+/// path never escapes the directory tree rooted at `start`. An error is+/// returned if the target path is absolute.#[cfg(not(windows))]-pub use symlink::symlink;+#[inline]+pub(crate) fn symlink(old_path: &Path, new_start: &fs::File, new_path: &Path) -> io::Result<()> {+ // Don't allow creating symlinks to absolute paths. This isn't strictly+ // necessary to preserve the sandbox, since `open` will refuse to follow+ // absolute symlinks in any case. However, it is useful to enforce this+ // restriction so that a WASI program can't trick some other non-WASI+ // program into following an absolute path.+ if old_path.has_root() {+ return Err(errors::escape_attempt());+ }++ sys::symlink_impl(old_path, new_start, new_path)+}++/// Perform a `symlink_file`-like operation, ensuring that the resolution of+/// the path never escapes the directory tree rooted at `start`.#[cfg(windows)]-pub use symlink::{symlink_dir, symlink_file};-#[cfg(test)]-mod tests;+#[inline]+pub(crate) fn symlink_file(+ old_path: &Path,+ new_start: &fs::File,+ new_path: &Path,+) -> io::Result<()> {+ // As above, don't allow creating symlinks to absolute paths.+ if old_path.has_root() {+ return Err(errors::escape_attempt());+ }++ sys::symlink_file_impl(old_path, new_start, new_path)+}++/// Perform a `symlink_dir`-like operation, ensuring that the resolution of the+/// path never escapes the directory tree rooted at `start`.+#[cfg(windows)]+#[inline]+pub(crate) fn symlink_dir(+ old_path: &Path,+ new_start: &fs::File,+ new_path: &Path,+) -> io::Result<()> {+ // As above, don't allow creating symlinks to absolute paths.+ if old_path.has_root() {+ return Err(errors::escape_attempt());+ }++ sys::symlink_dir_impl(old_path, new_start, new_path)+}++/// Open a directory by performing an `openat`-like operation, ensuring that+/// the resolution of the path never escapes the directory tree rooted at+/// `start`.+#[inline]+fn open_dir(start: &fs::File, path: &Path) -> io::Result<fs::File> {+ open(start, path, &dir_options())+}++/// Open a directory by performing an unsandboxed `openat`-like operation.+#[inline]+#[allow(dead_code)]+fn open_dir_unchecked(start: &fs::File, path: &Path) -> io::Result<fs::File> {+ open_unchecked(start, path, &dir_options()).map_err(Into::into)+}++/// Like `open_dir_unchecked`, but additionally request the ability to read the+/// directory entries.+#[inline]+#[allow(dead_code)]+fn open_dir_for_reading_unchecked(+ start: &fs::File,+ path: &Path,+ follow: FollowSymlinks,+) -> io::Result<fs::File> {+ open_unchecked(start, path, readdir_options().follow(follow)).map_err(Into::into)+}diff --git a/crates/wasi/src/filesystem/primitives/open.rs b/crates/wasi/src/filesystem/primitives/open.rsdeleted file mode 100644index 5fc94be9eb..0000000000--- a/crates/wasi/src/filesystem/primitives/open.rs+++ /dev/null@@ -1,14 +0,0 @@-//! This defines `open`, the primary entrypoint to sandboxed file and directory-//! opening.--use crate::filesystem::primitives::{OpenOptions, open_impl};-use std::path::Path;-use std::{fs, io};--/// Perform an `openat`-like operation, ensuring that the resolution of the-/// path never escapes the directory tree rooted at `start`.-#[inline]-pub fn open(start: &fs::File, path: &Path, options: &OpenOptions) -> io::Result<fs::File> {- // Call the underlying implementation.- open_impl(start, path, options)-}diff --git a/crates/wasi/src/filesystem/primitives/open_dir.rs b/crates/wasi/src/filesystem/primitives/open_dir.rsdeleted file mode 100644index 351b57af1a..0000000000--- a/crates/wasi/src/filesystem/primitives/open_dir.rs+++ /dev/null@@ -1,49 +0,0 @@-//! This defines `open_dir`, a wrapper around `open` which can be used to open-//! path as a directory.--#[allow(unused_imports)]-use crate::filesystem::primitives::open_unchecked;-use crate::filesystem::primitives::{- FollowSymlinks, dir_options, open, open_ambient_dir_impl, readdir_options,-};-use std::path::Path;-use std::{fs, io};--/// Open a directory by performing an `openat`-like operation,-/// ensuring that the resolution of the path never escapes-/// the directory tree rooted at `start`.-#[inline]-pub fn open_dir(start: &fs::File, path: &Path) -> io::Result<fs::File> {- open(start, path, &dir_options())-}--/// Open a directory by performing an unsandboxed `openat`-like operation.-#[inline]-#[allow(dead_code)]-pub(crate) fn open_dir_unchecked(start: &fs::File, path: &Path) -> io::Result<fs::File> {- open_unchecked(start, path, &dir_options()).map_err(Into::into)-}--/// Like `open_dir_unchecked`, but additionally request the ability to read the-/// directory entries.-#[inline]-#[allow(dead_code)]-pub(crate) fn open_dir_for_reading_unchecked(- start: &fs::File,- path: &Path,- follow: FollowSymlinks,-) -> io::Result<fs::File> {- open_unchecked(start, path, readdir_options().follow(follow)).map_err(Into::into)-}--/// Open a directory named by a bare path, using the host process' ambient-/// authority.-///-/// # Ambient Authority-///-/// This function is not sandboxed and may trivially access any path that the-/// host process has access to.-#[inline]-pub fn open_ambient_dir(path: &Path) -> io::Result<fs::File> {- open_ambient_dir_impl(path)-}diff --git a/crates/wasi/src/filesystem/primitives/read_link.rs b/crates/wasi/src/filesystem/primitives/read_link.rsdeleted file mode 100644index 9678ea08e1..0000000000--- a/crates/wasi/src/filesystem/primitives/read_link.rs+++ /dev/null@@ -1,36 +0,0 @@-//! This defines `read_link`, the primary entrypoint to sandboxed symbolic link-//! dereferencing.--use crate::filesystem::primitives::{errors, read_link_impl};-use std::path::{Path, PathBuf};-use std::{fs, io};--/// Perform a `readlinkat`-like operation, ensuring that the resolution of the-/// link path never escapes the directory tree rooted at `start`.-#[inline]-pub fn read_link_contents(start: &fs::File, path: &Path) -> io::Result<PathBuf> {- // Call the underlying implementation.- read_link_impl(start, path)-}--/// Perform a `readlinkat`-like operation, ensuring that the resolution of the-/// path never escapes the directory tree rooted at `start`, and also verifies-/// that the link target is not absolute.-#[inline]-pub fn read_link(start: &fs::File, path: &Path) -> io::Result<PathBuf> {- // Call the underlying implementation.- let result = read_link_contents(start, path);-- // Don't allow reading symlinks to absolute paths. This isn't strictly- // necessary to preserve the sandbox, since `open` will refuse to follow- // absolute paths in any case. However, it is useful to enforce this- // restriction to avoid leaking information about the host filesystem- // outside the sandbox.- if let Ok(path) = &result {- if path.has_root() {- return Err(errors::escape_attempt());- }- }-- result-}diff --git a/crates/wasi/src/filesystem/primitives/remove_dir.rs b/crates/wasi/src/filesystem/primitives/remove_dir.rsdeleted file mode 100644index adb98caea2..0000000000--- a/crates/wasi/src/filesystem/primitives/remove_dir.rs+++ /dev/null@@ -1,14 +0,0 @@-//! This defines `remove_dir`, the primary entrypoint to sandboxed file-//! removal.--use crate::filesystem::primitives::remove_dir_impl;-use std::path::Path;-use std::{fs, io};--/// Perform a `rmdirat`-like operation, ensuring that the resolution of the-/// path never escapes the directory tree rooted at `start`.-#[inline]-pub fn remove_dir(start: &fs::File, path: &Path) -> io::Result<()> {- // Call the underlying implementation.- remove_dir_impl(start, path)-}diff --git a/crates/wasi/src/filesystem/primitives/remove_file.rs b/crates/wasi/src/filesystem/primitives/remove_file.rsdeleted file mode 100644index a678ea039b..0000000000--- a/crates/wasi/src/filesystem/primitives/remove_file.rs+++ /dev/null@@ -1,14 +0,0 @@-//! This defines `remove_file`, the primary entrypoint to sandboxed file-//! removal.--use crate::filesystem::primitives::remove_file_impl;-use std::path::Path;-use std::{fs, io};--/// Perform a `remove_fileat`-like operation, ensuring that the resolution of-/// the path never escapes the directory tree rooted at `start`.-#[inline]-pub fn remove_file(start: &fs::File, path: &Path) -> io::Result<()> {- // Call the underlying implementation.- remove_file_impl(start, path)-}diff --git a/crates/wasi/src/filesystem/primitives/rename.rs b/crates/wasi/src/filesystem/primitives/rename.rsdeleted file mode 100644index 18ce6930c5..0000000000--- a/crates/wasi/src/filesystem/primitives/rename.rs+++ /dev/null@@ -1,19 +0,0 @@-//! This defines `rename`, the primary entrypoint to sandboxed renaming.--use crate::filesystem::primitives::rename_impl;-use std::path::Path;-use std::{fs, io};--/// Perform a `renameat`-like operation, ensuring that the resolution of both-/// the old and new paths never escape the directory tree rooted at their-/// respective starts.-#[inline]-pub fn rename(- old_start: &fs::File,- old_path: &Path,- new_start: &fs::File,- new_path: &Path,-) -> io::Result<()> {- // Call the underlying implementation.- rename_impl(old_start, old_path, new_start, new_path)-}diff --git a/crates/wasi/src/filesystem/primitives/set_times.rs b/crates/wasi/src/filesystem/primitives/set_times.rsdeleted file mode 100644index f1aff11552..0000000000--- a/crates/wasi/src/filesystem/primitives/set_times.rs+++ /dev/null@@ -1,33 +0,0 @@-//! This defines `set_times`, the primary entrypoint to sandboxed-//! filesystem times modification.-//!-//! TODO: `check_set_times` etc.--use crate::filesystem::primitives::{set_times_impl, set_times_nofollow_impl};-use std::path::Path;-use std::time::SystemTime;-use std::{fs, io};--/// Perform a `utimensat`-like operation, ensuring that the resolution of the-/// path never escapes the directory tree rooted at `start`. This function-/// follows symlinks.-#[inline]-pub fn set_times(- start: &fs::File,- path: &Path,- atime: Option<SystemTime>,- mtime: Option<SystemTime>,-) -> io::Result<()> {- set_times_impl(start, path, atime, mtime)-}--/// Like `set_times`, but never follows symlinks.-#[inline]-pub fn set_times_nofollow(- start: &fs::File,- path: &Path,- atime: Option<SystemTime>,- mtime: Option<SystemTime>,-) -> io::Result<()> {- set_times_nofollow_impl(start, path, atime, mtime)-}diff --git a/crates/wasi/src/filesystem/primitives/stat.rs b/crates/wasi/src/filesystem/primitives/stat.rsdeleted file mode 100644index f5ef803795..0000000000--- a/crates/wasi/src/filesystem/primitives/stat.rs+++ /dev/null@@ -1,13 +0,0 @@-//! This defines `stat`, the primary entrypoint to sandboxed metadata querying.--use crate::filesystem::primitives::{FollowSymlinks, Metadata, stat_impl};-use std::path::Path;-use std::{fs, io};--/// Perform an `fstatat`-like operation, ensuring that the resolution of the-/// path never escapes the directory tree rooted at `start`.-#[inline]-pub fn stat(start: &fs::File, path: &Path, follow: FollowSymlinks) -> io::Result<Metadata> {- // Call the underlying implementation.- stat_impl(start, path, follow)-}diff --git a/crates/wasi/src/filesystem/primitives/symlink.rs b/crates/wasi/src/filesystem/primitives/symlink.rsdeleted file mode 100644index e297dcb399..0000000000--- a/crates/wasi/src/filesystem/primitives/symlink.rs+++ /dev/null@@ -1,64 +0,0 @@-//! This defines `symlink`, the primary entrypoint to sandboxed symlink-//! creation.--use crate::filesystem::primitives::errors;-use std::path::Path;-use std::{fs, io};--/// Perform a `symlinkat`-like operation, ensuring that the resolution of the-/// path never escapes the directory tree rooted at `start`. An error is-/// returned if the target path is absolute.-#[cfg(not(windows))]-#[inline]-pub fn symlink(old_path: &Path, new_start: &fs::File, new_path: &Path) -> io::Result<()> {- // Don't allow creating symlinks to absolute paths. This isn't strictly- // necessary to preserve the sandbox, since `open` will refuse to follow- // absolute symlinks in any case. However, it is useful to enforce this- // restriction so that a WASI program can't trick some other non-WASI- // program into following an absolute path.- if old_path.has_root() {- return Err(errors::escape_attempt());- }-- write_symlink_impl(old_path, new_start, new_path)-}--#[cfg(not(windows))]-fn write_symlink_impl(old_path: &Path, new_start: &fs::File, new_path: &Path) -> io::Result<()> {- use crate::filesystem::primitives::symlink_impl;-- // Call the underlying implementation.- symlink_impl(old_path, new_start, new_path)-}--/// Perform a `symlink_file`-like operation, ensuring that the resolution of-/// the path never escapes the directory tree rooted at `start`.-#[cfg(windows)]-#[inline]-pub fn symlink_file(old_path: &Path, new_start: &fs::File, new_path: &Path) -> io::Result<()> {- use crate::filesystem::primitives::symlink_file_impl;-- // As above, don't allow creating symlinks to absolute paths.- if old_path.has_root() {- return Err(errors::escape_attempt());- }-- // Call the underlying implementation.- symlink_file_impl(old_path, new_start, new_path)-}--/// Perform a `symlink_dir`-like operation, ensuring that the resolution of the-/// path never escapes the directory tree rooted at `start`.-#[cfg(windows)]-#[inline]-pub fn symlink_dir(old_path: &Path, new_start: &fs::File, new_path: &Path) -> io::Result<()> {- use crate::filesystem::primitives::symlink_dir_impl;-- // As above, don't allow creating symlinks to absolute paths.- if old_path.has_root() {- return Err(errors::escape_attempt());- }-- // Call the underlying implementation.- symlink_dir_impl(old_path, new_start, new_path)-}diff --git a/crates/wasi/src/filesystem/primitives/tests/fs.rs b/crates/wasi/src/filesystem/primitives/tests/fs.rsindex 10f16ed9e9..205878a47f 100644--- a/crates/wasi/src/filesystem/primitives/tests/fs.rs+++ b/crates/wasi/src/filesystem/primitives/tests/fs.rs@@ -837,12 +837,9 @@ fn read_link_contents() {};check!(h::symlink_file(&start, &"foo", &link));assert_eq!(- check!(super::super::read_link::read_link_contents(- &start,- Path::new(link)- ))- .to_str()- .unwrap(),+ check!(super::super::read_link_contents(&start, Path::new(link)))+ .to_str()+ .unwrap(),"foo");}@@ -858,12 +855,9 @@ fn read_link_contents_absolute() {};check!(std::os::unix::fs::symlink("/foo", tmpdir.path().join(link)));assert_eq!(- check!(super::super::read_link::read_link_contents(- &start,- Path::new(link)- ))- .to_str()- .unwrap(),+ check!(super::super::read_link_contents(&start, Path::new(link)))+ .to_str()+ .unwrap(),"/foo");}diff --git a/crates/wasi/src/filesystem/unix.rs b/crates/wasi/src/filesystem/unix.rsindex 9a14680d5c..bfa09865d5 100644--- a/crates/wasi/src/filesystem/unix.rs+++ b/crates/wasi/src/filesystem/unix.rs@@ -11,8 +11,8 @@ use std::io;use std::os::unix::fs::FileExt;use std::path::Path;-pub use crate::filesystem::primitives::remove_file as remove_file_or_symlink;-pub use crate::filesystem::primitives::symlink;+pub(crate) use crate::filesystem::primitives::remove_file as remove_file_or_symlink;+pub(crate) use crate::filesystem::primitives::symlink;pub(crate) fn get_flags(file: &File) -> io::Result<DescriptorFlags> {let flags = fcntl_getfl(file)?;