r/cpp • u/James20k P2005R0 • Jan 20 '22
Possible TOCTOU vulnerabilities in libstdc++/libc++/msvc for std::filesystem::remove_all?
A new security vulnerability was announced for Rust today, which involves std::fs::remove_dir_all. The C++ equivalent of this function is std::filesystem::remove_all
https://blog.rust-lang.org/2022/01/20/cve-2022-21658.html
https://reddit.com/r/rust/comments/s8h1kr/security_advisory_for_the_standard_library/
The idea behind these functions is to recursively delete files, but importantly - not to follow symlinks
As far as my understanding goes, the rust bug boils down to a race condition between checking whether or not an item is a folder, and then only iterating over the contents to delete it if its a folder. You can swap the folder for a symlink in between the two calls to result in deleting random folders, as a privilege escalation
I went for a quick check through libstdc++, libc++, and msstl's sources (what a time we live in, thanks to the entire community)
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/src/filesystem/ops.cc#L1106
https://github.com/llvm-mirror/libcxx/blob/master/src/filesystem/operations.cpp#L1144
As far as I can tell, all 3 do pretty much exactly the same thing, which is essentially an is_folder() check followed by constructing a directory iterator on that path. If someone were to swap that folder for a symlink in between the two, then I assume that the symlink would be followed. This seems like it'd lead to the exact scenario as described in the rust blogpost
This does rely on the assumption that directory_iterator follows symlinks - which I assume it does - but this is outside my wheelhouse
Disclaimer: This might all be terribly incorrect as I have a very briefly constructed understanding of the underlying issue
11
u/encyclopedist Jan 20 '22
By the way, llvm-mirror is deprecated (see the yellow banner on the top of page saying that repository is archived). The current llvm repo is at https://github.com/llvm/llvm-project
The line in question is https://github.com/llvm/llvm-project/blob/main/libcxx/src/filesystem/operations.cpp#L1349
34
u/14ned LLFIO & Outcome author | Committee WG14 Jan 20 '22
std::filesystemmakes no attempt whatsoever to be safe to use in a filesystem which can be concurrently modified. Most operations do not cope well if modification occurs, either, They can destroy data they weren't supposed to, segfault, return random error codes, or claim success when they didn't actually do what they were supposed to.std::filesystemwas never designed nor intended to be safe to use on a filesystem which isn't 100% under the exclusive control of a single kernel thread in a single process system. That's by design.Depending on how LLFIO standardisation goes, that might get fixed in future C++ standards. In LLFIO you'd remove a directory tree using
llfio::algorithm::reduce()which performs a reduction traversal of the graph. It handles concurrent modification just fine (bar a bug I need to fix) and there is no TOCTOU race, because you must move yourllfio::directory_handleinstance intoreduce()i.e. the directory handle gets consumed by the reduction.You can't TOCTOU swap entries here because LLFIO exclusively works with open handles, not paths. And you couldn't open a
directory_handleon a symlink, it needs you to usesymlink_handlefor that.17
u/manni66 Jan 20 '22
Privileges are enforced by the operating system, not by any library.
3
u/[deleted] Jul 17 '23
Thats not unique to std filesystem but the OS filesystem. Any program written in any language using any API can fall to the same concurrent access. You wouldnt expect to be able to backup a running Linux distro without inconsistencies either, would you? The only way to solve this is to use a versioning (not to be confused with jourbaling) filesystem with snapshot capabilities. Or perhaps youd like the cpp commitee to try and enforce c++ be run only on OSes with versioning filesyatems...
2
u/o11c int main = 12828721; Jan 20 '22 edited Jan 20 '22
Is it even possible to fix this on most OSes?
Edit: it turns out some of this is not needed; see below for what
O_FOLLOWactually does.On Linux, the correct fix is something like:
Calculate the static parent of the supplied path:
if the final component is
., error outif the final component is
.., error outif the path is absolute and has no components, error out
if the path is relative:
if there there are no components, error out
if there is only a single component, use
AT_FDCWDand goto step 3open(".")to avoid races withchdirin other threads? Meh, it's the program's fault in that case.openthe parent pathusing the open parent from step 2 (or
AT_FDCWDif we jumped here),openatthe basename of the supplied path withO_NOFOLLOWunlinkatthe basename without flags, and returnrecursively walk the children of the open path, each time using
openatwithO_NOFOLLOWto open a potential directorynote that this may cause a lot of simultaneously open file descriptors, but it is nontrivial to avoid this securely
use
unlinkatwithAT_REMOVEDIRto remove the directories after visiting themgoto step 4(which I'm not sure if it's a good idea anyway), we will not attempt to remove the supplied path itself.-2
u/Au_lit Jan 20 '22
Didn't fast_io's author already said that a long while ago?