Skip to content

Commit 0cf4f0f

Browse files
library: explain TOCTOU races in fs::remove_dir_all
In the previous description it said there was a TOCTOU race but did not explain exactly what the problem was. I sat down with the CVE, reviewed its text, and created this explanation. This context should hopefully help people understand the actual risk as-such.
1 parent 251cda5 commit 0cf4f0f

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

library/std/src/fs.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,17 +2851,28 @@ pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
28512851
///
28522852
/// # Platform-specific behavior
28532853
///
2854-
/// This function currently corresponds to `openat`, `fdopendir`, `unlinkat` and `lstat` functions
2855-
/// on Unix (except for REDOX) and the `CreateFileW`, `GetFileInformationByHandleEx`,
2856-
/// `SetFileInformationByHandle`, and `NtCreateFile` functions on Windows. Note that, this
2857-
/// [may change in the future][changes].
2854+
/// These implementation details [may change in the future][changes].
2855+
///
2856+
/// - "Unix-like": By default, this function currently corresponds to
2857+
/// `openat`, `fdopendir`, `unlinkat` and `lstat`
2858+
/// on Unix-family platforms, except where noted otherwise.
2859+
/// - "Windows": This function currently corresponds to `CreateFileW`,
2860+
/// `GetFileInformationByHandleEx`, `SetFileInformationByHandle`, and `NtCreateFile`.
2861+
///
2862+
/// ## Time-of-check to time-of-use (TOCTOU) race conditions
2863+
/// On a few platforms there is no way to only remove a symlink, instead of following it,
2864+
/// without performing a check and then conditionally operating on a file or directory.
2865+
/// This allows any concurrently-running code to add a symlink after the check is performed,
2866+
/// causing a removal to instead target the resolved path of the symlink. This is a TOCTOU race.
2867+
/// By default, `fs::remove_dir_all` protects against a symlink TOCTOU race on all platforms
2868+
/// except the following. It should not be used in security-sensitive contexts on these platforms:
2869+
/// - Miri: Even when emulating targets where the underlying implementation will protect against
2870+
/// TOCTOU races, Miri will not do so.
2871+
/// - Redox OS: This function does not protect against TOCTOU races, as Redox does not implement
2872+
/// the require platform support to do so.
28582873
///
28592874
/// [changes]: io#platform-specific-behavior
28602875
///
2861-
/// On REDOX, as well as when running in Miri for any target, this function is not protected against
2862-
/// time-of-check to time-of-use (TOCTOU) race conditions, and should not be used in
2863-
/// security-sensitive code on those platforms. All other platforms are protected.
2864-
///
28652876
/// # Errors
28662877
///
28672878
/// See [`fs::remove_file`] and [`fs::remove_dir`].

0 commit comments

Comments
 (0)