Skip to content

Commit 8323d68

Browse files
authored
Unrolled build for #141832
Rollup merge of #141832 - workingjubilee:explain-what-toctou-races-are, r=thomcc,ChrisDenton 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. Incidentally, it also fixes the capitalization on the name of Redox OS. Original CVE and advisory: - CVE: https://www.cve.org/CVERecord?id=CVE-2022-21658 - security advisory: https://groups.google.com/g/rustlang-security-announcements/c/R1fZFDhnJVQ?pli=1 - github cross-post: GHSA-r9cc-f5pr-p3j2
2 parents f0999ff + 7f7c415 commit 8323d68

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
@@ -2915,17 +2915,28 @@ pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
29152915
///
29162916
/// # Platform-specific behavior
29172917
///
2918-
/// This function currently corresponds to `openat`, `fdopendir`, `unlinkat` and `lstat` functions
2919-
/// on Unix (except for REDOX) and the `CreateFileW`, `GetFileInformationByHandleEx`,
2920-
/// `SetFileInformationByHandle`, and `NtCreateFile` functions on Windows. Note that, this
2921-
/// [may change in the future][changes].
2918+
/// These implementation details [may change in the future][changes].
2919+
///
2920+
/// - "Unix-like": By default, this function currently corresponds to
2921+
/// `openat`, `fdopendir`, `unlinkat` and `lstat`
2922+
/// on Unix-family platforms, except where noted otherwise.
2923+
/// - "Windows": This function currently corresponds to `CreateFileW`,
2924+
/// `GetFileInformationByHandleEx`, `SetFileInformationByHandle`, and `NtCreateFile`.
2925+
///
2926+
/// ## Time-of-check to time-of-use (TOCTOU) race conditions
2927+
/// On a few platforms there is no way to remove a directory's contents without following symlinks
2928+
/// unless you perform a check and then operate on paths based on that directory.
2929+
/// This allows concurrently-running code to replace the directory with a symlink after the check,
2930+
/// causing a removal to instead operate on a path based on the symlink. This is a TOCTOU race.
2931+
/// By default, `fs::remove_dir_all` protects against a symlink TOCTOU race on all platforms
2932+
/// except the following. It should not be used in security-sensitive contexts on these platforms:
2933+
/// - Miri: Even when emulating targets where the underlying implementation will protect against
2934+
/// TOCTOU races, Miri will not do so.
2935+
/// - Redox OS: This function does not protect against TOCTOU races, as Redox does not implement
2936+
/// the required platform support to do so.
29222937
///
29232938
/// [changes]: io#platform-specific-behavior
29242939
///
2925-
/// On REDOX, as well as when running in Miri for any target, this function is not protected against
2926-
/// time-of-check to time-of-use (TOCTOU) race conditions, and should not be used in
2927-
/// security-sensitive code on those platforms. All other platforms are protected.
2928-
///
29292940
/// # Errors
29302941
///
29312942
/// See [`fs::remove_file`] and [`fs::remove_dir`].

0 commit comments

Comments
 (0)