-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request from GHSA-mc8h-8q98-g5hr
* Fix TOCTOU races This involves a new safe API that works from a file descriptor rather than a path. The existing APIs are preserved but no longer recommended. * Bump version to indicate API breaks The crate features have changed, in particular parallel is now not defaulted-on. * Rearrange changelog
- Loading branch information
1 parent
be7a410
commit 7247a8b
Showing
13 changed files
with
1,052 additions
and
466 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,59 @@ | ||
[package] | ||
authors = [ | ||
"Erin P. <[email protected]>", | ||
"Robert C. <[email protected]>", | ||
"Erin P. <[email protected]>", | ||
"Robert C. <[email protected]>", | ||
] | ||
categories = ["filesystem"] | ||
description = "A safe, reliable implementation of remove_dir_all for Windows" | ||
edition = "2018" | ||
include = [ | ||
"Cargo.toml", | ||
"LICENCE-APACHE", | ||
"LICENCE-MIT", | ||
"src/**/*", | ||
"README.md", | ||
"Cargo.toml", | ||
"LICENCE-APACHE", | ||
"LICENCE-MIT", | ||
"src/**/*", | ||
"README.md", | ||
] | ||
keywords = ["utility", "filesystem", "remove_dir", "windows"] | ||
license = "MIT OR Apache-2.0" | ||
name = "remove_dir_all" | ||
readme = "README.md" | ||
repository = "https://github.com/XAMPPRocky/remove_dir_all.git" | ||
version = "0.7.1-alpha.0" | ||
version = "0.8.0-alpha.0" | ||
|
||
[features] | ||
default = ["parallel"] | ||
parallel = ["rayon"] | ||
default = [] | ||
log = ["dep:log"] | ||
parallel = ["dep:rayon"] | ||
|
||
[dependencies] | ||
cfg-if = "1.0.0" | ||
fs_at = { version = "0.1" } | ||
lazy_static = "1.4.0" | ||
log = { version = "0.4.11", optional = true } | ||
normpath = "1.0.1" | ||
rayon = { version = "1.4", optional = true } | ||
|
||
[target.'cfg(windows)'.dependencies] | ||
log = "0.4.11" | ||
rayon = { version = "1.4", optional = true } | ||
aligned = "0.4.1" | ||
|
||
[target.'cfg(windows)'.dependencies.windows-sys] | ||
version = "0.45.0" | ||
features = [ | ||
"Win32_Foundation", | ||
"Win32_Storage_FileSystem", | ||
"Win32_System_IO", | ||
"Win32_System_Ioctl", | ||
"Win32_System_SystemServices", | ||
"Win32_System_Threading", | ||
] | ||
version = "0.45.0" | ||
|
||
[target.'cfg(not(windows))'.dependencies] | ||
libc = "0.2" | ||
cvt = "0.1.1" | ||
|
||
[dev-dependencies] | ||
doc-comment = "0.3" | ||
env_logger = "0.9.0" | ||
env_logger = "0.10.0" | ||
tempfile = "3.1" | ||
test-log = "0.2" | ||
log = "0.4.11" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
use std::{ | ||
ffi::OsStr, | ||
fs::File, | ||
io::{ErrorKind, Result}, | ||
path::Path, | ||
}; | ||
|
||
#[cfg(feature = "parallel")] | ||
use rayon::prelude::*; | ||
|
||
mod io; | ||
mod path_components; | ||
|
||
cfg_if::cfg_if! { | ||
if #[cfg(windows)] { | ||
mod win; | ||
pub(crate) use win::WindowsIo as OsIo; | ||
} else { | ||
mod unix; | ||
pub(crate) use unix::UnixIo as OsIo; | ||
} | ||
} | ||
|
||
impl super::RemoveDir for std::fs::File { | ||
fn remove_dir_contents(&mut self, debug_root: Option<&Path>) -> Result<()> { | ||
// thunk over to the free version adding in the os-specific IO trait impl | ||
let debug_root = match debug_root { | ||
None => PathComponents::Path(Path::new("")), | ||
Some(debug_root) => PathComponents::Path(debug_root), | ||
}; | ||
_remove_dir_contents::<OsIo>(self, &debug_root) | ||
} | ||
} | ||
|
||
/// Entry point for deprecated function | ||
pub(crate) fn _ensure_empty_dir_path<I: io::Io, P: AsRef<Path>>(path: P) -> Result<()> { | ||
// This is as TOCTOU safe as we can make it. Attacks via link replacements | ||
// in interior components of the path is still possible. if the create | ||
// succeeds, mission accomplished. if the create fails, open the dir | ||
// (subject to races again), and then proceed to delete the contents via the | ||
// descriptor. | ||
match std::fs::create_dir(&path) { | ||
Err(e) if e.kind() == ErrorKind::AlreadyExists => { | ||
// Exists and is a dir. Open it | ||
let mut existing_dir = I::open_dir(path.as_ref())?; | ||
existing_dir.remove_dir_contents(Some(path.as_ref())) | ||
} | ||
otherwise => otherwise, | ||
} | ||
} | ||
|
||
// Deprecated entry point | ||
pub(crate) fn _remove_dir_contents_path<I: io::Io, P: AsRef<Path>>(path: P) -> Result<()> { | ||
let mut d = I::open_dir(path.as_ref())?; | ||
_remove_dir_contents::<I>(&mut d, &PathComponents::Path(path.as_ref())) | ||
} | ||
|
||
/// exterior lifetime interface to dir removal | ||
fn _remove_dir_contents<I: io::Io>(d: &mut File, debug_root: &PathComponents<'_>) -> Result<()> { | ||
let owned_handle = I::duplicate_fd(d)?; | ||
remove_dir_contents_recursive::<I>(owned_handle, debug_root) | ||
} | ||
|
||
/// deprecated interface | ||
pub(crate) fn remove_dir_all_path<I: io::Io, P: AsRef<Path>>(path: P) -> Result<()> { | ||
let p = path.as_ref(); | ||
// Opportunity 1 for races | ||
let d = I::open_dir(p)?; | ||
let debug_root = PathComponents::Path(if p.has_root() { p } else { Path::new(".") }); | ||
remove_dir_contents_recursive::<OsIo>(d, &debug_root)?; | ||
// Opportunity 2 for races | ||
std::fs::remove_dir(&path) | ||
} | ||
|
||
use crate::RemoveDir; | ||
|
||
use self::path_components::PathComponents; | ||
|
||
// Core workhorse, heading towards this being able to be tasks. | ||
#[allow(clippy::map_identity)] | ||
fn remove_dir_contents_recursive<I: io::Io>( | ||
mut d: File, | ||
debug_root: &PathComponents<'_>, | ||
) -> Result<()> { | ||
#[cfg(feature = "log")] | ||
log::trace!("scanning {}", &debug_root); | ||
// We take a os level clone of the FD so that there are no lifetime | ||
// concerns. It would *not* be ok to do readdir on one file twice | ||
// concurrently because of shared kernel state. | ||
let dirfd = I::duplicate_fd(&mut d)?; | ||
cfg_if::cfg_if! { | ||
if #[cfg(feature = "parallel")] { | ||
let iter = fs_at::read_dir(&mut d)?; | ||
let iter = iter.par_bridge(); | ||
} else { | ||
let mut iter = fs_at::read_dir(&mut d)?; | ||
} | ||
} | ||
|
||
iter.try_for_each(|dir_entry| -> Result<()> { | ||
let dir_entry = dir_entry?; | ||
let name = dir_entry.name(); | ||
if name == OsStr::new(".") || name == OsStr::new("..") { | ||
return Ok(()); | ||
} | ||
let dir_path = Path::new(name); | ||
let dir_debug_root = PathComponents::Component(debug_root, dir_path); | ||
// Windows optimised: open everything always, which is not bad for | ||
// linux, and portable to OS's and FS's that don't expose inode type in | ||
// the readdir entries. | ||
|
||
let mut opts = fs_at::OpenOptions::default(); | ||
opts.read(true) | ||
.write(fs_at::OpenOptionsWriteMode::Write) | ||
.follow(false); | ||
|
||
let child_result = opts.open_dir_at(&dirfd, name); | ||
let is_dir = match child_result { | ||
Err(e) if !I::is_eloop(&e) => return Err(e), | ||
Err(_) => false, | ||
Ok(child_file) => { | ||
let metadata = child_file.metadata()?; | ||
let is_dir = metadata.is_dir(); | ||
I::clear_readonly(&child_file, &dir_debug_root, &metadata)?; | ||
|
||
if is_dir { | ||
remove_dir_contents_recursive::<I>(child_file, &dir_debug_root)?; | ||
#[cfg(feature = "log")] | ||
log::trace!("rmdir: {}", &dir_debug_root); | ||
let opts = fs_at::OpenOptions::default(); | ||
opts.rmdir_at(&dirfd, name).map_err(|e| { | ||
#[cfg(feature = "log")] | ||
log::debug!("error removing {}", dir_debug_root); | ||
e | ||
})?; | ||
} | ||
is_dir | ||
} | ||
}; | ||
if !is_dir { | ||
#[cfg(feature = "log")] | ||
log::trace!("unlink: {}", &dir_debug_root); | ||
opts.unlink_at(&dirfd, name).map_err(|e| { | ||
#[cfg(feature = "log")] | ||
log::debug!("error removing {}", dir_debug_root); | ||
e | ||
})?; | ||
} | ||
|
||
#[cfg(feature = "log")] | ||
log::trace!("removed {}", dir_debug_root); | ||
Ok(()) | ||
})?; | ||
#[cfg(feature = "log")] | ||
log::trace!("scanned {}", &debug_root); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//! Private trait to deal with OS variance | ||
use std::{ | ||
fmt::Debug, | ||
fs::{File, Metadata}, | ||
io, | ||
path::Path, | ||
}; | ||
|
||
use super::path_components::PathComponents; | ||
|
||
pub(crate) trait Io { | ||
type UniqueIdentifier: PartialEq + Debug; | ||
|
||
fn duplicate_fd(f: &mut File) -> io::Result<File>; | ||
|
||
fn open_dir(p: &Path) -> io::Result<File>; | ||
fn unique_identifier(d: &File) -> io::Result<Self::UniqueIdentifier>; | ||
|
||
fn clear_readonly( | ||
f: &File, | ||
dir_debug_root: &'_ PathComponents<'_>, | ||
metadata: &Metadata, | ||
) -> io::Result<()>; | ||
|
||
fn is_eloop(e: &io::Error) -> bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use std::{fmt::Display, path::Path}; | ||
|
||
/// Print a path that is broken into segments. | ||
// explicitly typed to avoid type recursion. 'a is the smallest lifetime present | ||
// : that of the child. | ||
pub(crate) enum PathComponents<'a> { | ||
Path(&'a Path), | ||
Component(&'a PathComponents<'a>, &'a Path), | ||
} | ||
|
||
impl<'p> Display for PathComponents<'p> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
PathComponents::Path(p) => p.display().fmt(f), | ||
PathComponents::Component(p, c) => { | ||
p.fmt(f)?; | ||
f.write_str("/")?; | ||
c.display().fmt(f) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.