Skip to content

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

wasi: refuse to rmdir through a symlink with a trailing slash (#14663)

Matthew Suozzo760ee20789a6
diff --git a/crates/test-programs/src/bin/p2_remove_directory.rs b/crates/test-programs/src/bin/p2_remove_directory.rs
index 95dfdb6591..7e5ae4c44b 100644
--- a/crates/test-programs/src/bin/p2_remove_directory.rs
+++ b/crates/test-programs/src/bin/p2_remove_directory.rs
@@ -32,6 +32,23 @@ fn test_remove_directory(dir: &Descriptor) {
dir.remove_directory_at("dir//")
.expect("remove_directory with trailing slashes on a directory should succeed");
+ // Test that removing a directory through a symlink with a trailing slash
+ // fails, like on Linux, and leaves both in place.
+ dir.create_directory_at("dir")
+ .expect("creating a directory");
+ if dir.symlink_at("dir", "dir_link").is_ok() {
+ let err = dir
+ .remove_directory_at("dir_link/")
+ .expect_err("remove_directory through a symlink should fail");
+ assert!(
+ matches!(err, ErrorCode::NotDirectory),
+ "unexpected error {err:?}"
+ );
+ dir.unlink_file_at("dir_link").expect("removing a symlink");
+ }
+ dir.remove_directory_at("dir")
+ .expect("remove_directory on a directory should succeed");
+
// Create a temporary file.
create_file(dir, "file");
diff --git a/crates/wasi/src/filesystem/primitives/mod.rs b/crates/wasi/src/filesystem/primitives/mod.rs
index dca2c1a75e..c57ea17ce6 100644
--- a/crates/wasi/src/filesystem/primitives/mod.rs
+++ b/crates/wasi/src/filesystem/primitives/mod.rs
@@ -293,22 +293,27 @@ pub(crate) fn remove_file(start: &fs::File, path: &Path) -> io::Result<()> {
}
pub(crate) fn remove_dir(start: &fs::File, path: &Path) -> io::Result<()> {
+ // FreeBSD's kernel removes a symlink's target for `link/`, so leave
+ // trailing slashes to the checks below.
#[cfg(target_os = "freebsd")]
- if sys::remove_dir_fast(start, path)? {
+ if !path_has_trailing_slash(path) && sys::remove_dir_fast(start, path)? {
return Ok(());
}
// As with `create_dir`, a trailing slash after a directory names the
// directory itself rather than acting like a trailing slash-dot, so remove
- // it by its plain name. Other paths with a trailing slash, such as
- // symlinks, still resolve through `open_parent` below, which keeps
- // following them within `start`.
+ // it by its plain name. Other paths with a trailing slash still resolve
+ // through `open_parent` below.
if path_has_trailing_slash(path) {
let stripped = strip_dir_suffix(path);
if let Ok((dir, basename)) = open_parent(MaybeOwnedFile::borrowed(start), &stripped) {
let basename = Path::new(basename);
- if stat_unchecked(&dir, basename, FollowSymlinks::No).is_ok_and(|m| m.is_dir()) {
- return remove_dir_unchecked(&dir, basename);
+ match stat_unchecked(&dir, basename, FollowSymlinks::No) {
+ Ok(m) if m.is_dir() => return remove_dir_unchecked(&dir, basename),
+ // Like Linux, fail on a symlink without following it, so its
+ // target is never touched.
+ Ok(m) if m.file_type().is_symlink() => return Err(errors::is_not_directory()),
+ _ => {}
}
}
}
diff --git a/crates/wasi/src/filesystem/primitives/tests/fs_additional.rs b/crates/wasi/src/filesystem/primitives/tests/fs_additional.rs
index 519fadc619..7cab2ff860 100644
--- a/crates/wasi/src/filesystem/primitives/tests/fs_additional.rs
+++ b/crates/wasi/src/filesystem/primitives/tests/fs_additional.rs
@@ -308,6 +308,31 @@ fn remove_dir_trailing_slash() {
assert!(p::remove_dir(&start, Path::new("file/")).is_err());
}
+#[test]
+fn remove_dir_trailing_slash_symlink() {
+ if !symlink_supported() {
+ return;
+ }
+
+ let tmpdir = tmpdir();
+ let start = h::dir_of(&tmpdir);
+ check!(h::create_dir(&start, "dir"));
+ check!(h::create(&start, "file"));
+ check!(h::symlink_dir(&start, "dir", "dir_link"));
+ check!(h::symlink_file(&start, "file", "file_link"));
+ check!(h::symlink_dir(&start, "missing", "dangling"));
+ check!(h::symlink_dir(&start, "..", "up"));
+
+ // Like Linux, the symlink isn't followed, whatever it points to, and both
+ // it and its target stay in place.
+ for path in ["dir_link/", "file_link/", "dangling/", "up/"] {
+ let err = p::remove_dir(&start, Path::new(path)).unwrap_err();
+ assert_eq!(err.kind(), std::io::ErrorKind::NotADirectory, "{path}");
+ }
+ check!(h::symlink_metadata(&start, "dir_link"));
+ check!(h::metadata(&start, "dir"));
+}
+
#[test]
fn trailing_slash_in_dir() {
let tmpdir = tmpdir();