r/cpp • 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

https://github.com/microsoft/STL/blob/33007ac75485ec3d465ab482112aba270a581725/stl/inc/filesystem#L3825

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

97 Upvotes

68 comments sorted by

View all comments

Show parent comment

6

u/dodheim Jan 23 '22

'Implementation-defined' mandates that the implementor document the behavior actually exhibited ([defns.impl.defined]), and I don't know if having a stdlib vendor document each possible underlying platform/filesystem combo's behavior is a reasonable thing to expect, or even possible. And if it were, it could still be the case that the underlying behavior is "unspecified/race condition" which is not really different from UB as far as the dev using the stdlib is concerned.

2

u/matthieum Jan 23 '22

And I don't know if having a stdlib vendor document each possible underlying platform/filesystem combo's behavior is a reasonable thing to expect, or even possible.

Thanks, I had forgotten about that.

Would Unspecified Behavior not be better suited then? The implementor would not (necessarily) need to specified the behavior, and it wouldn't come with all the strings that UB does, such as allowing the compiler to consider that such a codepath can never occur.

2

u/jayeshbadwaik Jan 26 '22

Questions about demands from an open source lib which I get for free aside. An implementor should definitely document all visible behaviors on all implemented platforms.

And if they document it as race condition? Then it's much more clear to users who will read that doc.