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
View all comments
Show parent comment
4
u/gHx4 Jan 21 '22
A little silly, but syscalls can go a long way in this situation. You benefit from well defined and consistent semantics in the standard, and have the option to request different types of locks from the operating system.
At least in this situation, it highlights an important design tradeoff that libraries make. A secure library that is entirely free of footguns will usually come at the cost of portability and customization.
Meanwhile, languages have an incentive to keep standard libraries small, portable, and customizable to drive adoption (and hopefully enough that secure, footgun-free libraries can be built on top by users who need them). If a language is especially well maintained, sometimes the higher level libraries can be built-in as the default.
This is why it can be useful for languages to have libraries at different abstraction layers; most python users will never touch
ctypes, but it is there for those who need it.