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

34

u/14ned LLFIO & Outcome author | Committee WG14 Jan 20 '22

std::filesystem makes 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::filesystem was 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 your llfio::directory_handle instance into reduce() 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_handle on a symlink, it needs you to use symlink_handle for that.

23

u/James20k P2005R0 Jan 20 '22

If nothing else this is an implementation bug against the spec, because it says symlinks aren't followed - but they can be in certain circumstances here

With rust treating it like a security vulnerability due to causing privilege escalations, its probably wise to treat it similarly in those compilers

33

u/tcanens Jan 21 '22

If nothing else this is an implementation bug against the spec

It's not.

45

u/James20k P2005R0 Jan 21 '22

Oh lord, well that's just silly - they might as well all just be no-ops then because the filesystem is always concurrent

15

u/[deleted] Jan 21 '22

If you want POSIXish behavior on systems that don’t natively have POSIXish semantics there is rarely a single syscall solution. Between having race-free behavior and consistent behavior, the standard chose consistent behavior.

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.

13

u/_Js_Kc_ Jan 22 '22

and hopefully enough that secure, footgun-free libraries can be built on top by users who need them

But you can't build footgun-free libraries on top of std::filesystem, you need unlinkat, openat, etc, and std::filesystem doesn't wrap those.

It's not just not footgun-free, it's impossible to use correctly.

21

u/Sopel97 Jan 21 '22

wtf that's wild

13

u/_Js_Kc_ Jan 22 '22

"If another program does something entirely outside of your control, the behavior is undefined."

14

u/James20k P2005R0 Jan 21 '22

On a little more reflection, if the issues here are eventually deemed not security vulnerabilities due to this line in the spec or similar lines of reasoning, in my opinion it seems like the community should start strongly advising against <filesystem> as it is unusable in any context. Any bug or security vulnerability could be sidestepped like this

92

u/redditmodsareshits Jan 21 '22

TLDR, as a C programmer :

  • Rust : We have a race condition bug in our standard filesystem library !

  • C++ : You guys have a concurrency safe standard filesystem library ?

  • C : You guys have a standard filesystem library ?

15

u/muddledgarlic Jan 21 '22

Even though the standard washes its hands of this, that doesn't prevent implementers from dealing with it. To my (novice) understanding, it ought to be possible to mitigate against this without breaking ABI compatibility. It does seem like a good poster child for a change in wording in the standard, however. Perhaps a special case for deletion?

10

u/[deleted] Jan 21 '22

POSIX implementations that have Xxxat functions should be able to fix it if they wish. I don’t know if Windows can because there’s no enumerate directory by HANDLE API; but creating symlinks at all requires admin privies for us.

8

u/BrainIgnition Jan 21 '22

I don’t know if Windows can because there’s no enumerate directory by HANDLE API

Well, there is NtQueryDirectoryFile. Granted, this isn't a Win32 API.

7

u/[deleted] Jan 21 '22

Yeah, not allowed to call that :(.

→ More replies (6)
→ More replies (10)

19

u/jwakely libstdc++ tamer, LWG chair Jan 21 '22

An implementation can conform 100% to the spec and still have a security vulnerability, and still consider it worth fixing. Just because the standard says implementations aren't required to handle this race condition, doesn't mean they can't handle it.

in my opinion it seems like the community should start strongly advising against <filesystem> as it is unusable in any context.

That would be a silly overreaction.

Do you see any evidence that all bugs and security vulnerabilities are being sidestepped because the standard leaves something undefined?

The C and C++ standards say that dereferencing invalid pointers is undefined, but typical implementations still take steps to prevent userspace programs reading kernel memory, stop processes reading each others' memory, randomize address space layout etc etc etc.

10

u/[deleted] Jan 21 '22

Just because the standard says implementations aren't required to handle this race condition, doesn't mean they can't handle it.

I think any std::filesystem operation that we need to implement in terms of multiple syscalls is vulnerable to such "inconsistent" behavior even if not exactly this problem. Even if this is fixed, most user code is going to have similar problems; note that there is no way for a user using std::filesystem themselves to fix this.

Implementations that can should absolutely mitigate this but the overall library being unsuitable for use in such conditions (because it speaks paths rather than fds or HANDLEs) remains.

8

u/jwakely libstdc++ tamer, LWG chair Jan 21 '22

Yes, I agree with all that, but I still think "strongly advising against <filesystem> as it is unusable in any context" is nonsense.

→ More replies (2)

7

u/Minimonium Jan 21 '22

"unusable in any context" is an interesting take.

6

u/Vogtinator Jan 21 '22

It's only unusuable when crossing privilege levels, which is seldom the case.

6

u/matthieum Jan 22 '22

Let's not throw the baby with the bathwater shall we?

The C++ specification essentially has to add this clause, because not all filesystem implementations may provide the tools to do otherwise and therefore it is must be noted there is a risk.

C++ implementers, however, are free (and encouraged) to provide stronger guarantees when permitted. It is, of course, up to them to do so, and there are trade-offs:

  1. There may be performance implications, if doing so requires more syscalls.

  2. The platform may not generally suffer from this issues (I hear Windows 10 doesn't allow symlinks unless one has Admin permissions or uses Developer mode).

This means that one should work with their C++ library developers and make their concerns (and usecases) known so that the developers can make the appropriate trade-offs, provide the appropriate runtime options, etc...

→ More replies (5)
→ More replies (1)

22

u/riking27 Jan 21 '22

This is an extremely bold design choice, seeing as the library was introduced in C++17 and no consumer computing systems have been single process since at least Windows 95.

14

u/14ned LLFIO & Outcome author | Committee WG14 Jan 21 '22

Filesystem was an absolute bear to get standardised, so compromises had to be made to get it through. The fact it landed in 17 doesn't mean it was designed anywhere close to year 2017.

10

u/Minimonium Jan 21 '22

In my limited practice with Python's file locking - a truly safe concurrent filesystem requires a completely different system API. In most cases it's a non issue, in cases when it matters you isolate the environment.

8

u/14ned LLFIO & Outcome author | Committee WG14 Jan 21 '22

Correct. Proposed std::file_handle looks very different to anything else in the standard C++ library. It enables us to expose any stronger guarantees about concurrent filesystem modification implemented by the platform, however.