# GHSA-mc8h-8q98-g5hr: Race Condition Enabling Link Following and Time-of-check Time-of-use (TOCTOU)

> A race condition in the remove_dir_all crate allows attackers to trick privileged processes into deleting arbitrary files via symlink manipulation.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Local or same network only
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 24 Feb 2023
- **Also known as:** RUSTSEC-2023-0018

## What is GHSA-mc8h-8q98-g5hr?

GHSA-mc8h-8q98-g5hr is a vulnerability in remove_dir_all, published on 24 Feb 2023. It can only be exploited from the same machine or local network, with no action from the victim.

### Official description

The remove_dir_all crate is a Rust library that offers additional features over the Rust
standard library fs::remove_dir_all function.

It was possible to trick a privileged process doing a recursive delete in an
attacker controlled directory into deleting privileged files, on all operating systems.

For instance, consider deleting a tree called 'etc' in a parent directory
called 'p'. Between calling `remove_dir_all("a")` and remove_dir_all("a")
actually starting its work, the attacker can move 'p' to 'p-prime', and
replace 'p' with a symlink to '/'. Then the privileged process deletes 'p/etc'
which is actually /etc, and now your system is broken. There are some
mitigations for this exact scenario, such as CWD relative file lookup, but
they are not guaranteed - any code using absolute paths will not have that
protection in place.

The same attack could be performed at any point in the directory tree being
deleted: if 'a' contains a child directory called 'etc', attacking the
deletion by replacing 'a' with a link is possible.

The new code in this release mitigates the attack within the directory tree
being deleted by using file-handle relative operations: to open 'a/etc', the
path 'etc' relative to 'a' is opened, where 'a' is represented by a file
descriptor (Unix) or handle (Windows). With the exception of the entry points
into the directory deletion logic, this is robust against manipulation of the
directory hierarchy, and remove_dir_all will only delete files and directories
contained in the tree it is deleting.

The entry path however is a challenge - as described above, there are some
potential mitigations, but since using them must be done by the calling code,
it is hard to be confident about the security properties of the path based
interface.

The new extension trait `RemoveDir` provides an interface where it is much
harder to get it wrong.

`somedir.remove_dir_contents("name-of-child")`.

Callers can then make their own security evaluation about how to securely get
a directory handle. That is still not particularly obvious, and we're going to
follow up with a helper of some sort (probably in the `fs_at` crate). Once
that is available, the path based entry points will get deprecated.

In the interim, processes that might run with elevated privileges should
figure out how to securely identify the directory they are going to delete, to
avoid the initial race. Pragmatically, other processes should be fine with the
path based entry points : this is the same interface `std::fs::remove_dir_all`
offers, and an unprivileged process running in an attacker controlled
directory can't do anything that the attacker can't already do.

## Which versions are affected by GHSA-mc8h-8q98-g5hr?

3 open source packages ship an affected version, including remove_dir_all.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `remove_dir_all` | crates.io | All versions | 0.8.0 |
| `remove_dir_all` | crates.io | All versions | 0.8.0 |
| `remove_dir_all` | crates.io | From 0.0.0-0 | 0.8.0 |

## How do I fix GHSA-mc8h-8q98-g5hr?

Upgrade remove_dir_all to 0.8.0 or later.

## Is GHSA-mc8h-8q98-g5hr being exploited?

There is no known exploitation of GHSA-mc8h-8q98-g5hr in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-mc8h-8q98-g5hr and fix it if it is affected.

What it is: GHSA-mc8h-8q98-g5hr is a vulnerability in remove_dir_all, published on 24 Feb 2023. It can only be exploited from the same machine or local network, with no action from the victim.
Exploitation: There is no known exploitation of GHSA-mc8h-8q98-g5hr in the wild and no public exploit code.

Affected packages and fixed versions:
- remove_dir_all (crates.io): affected all versions; fixed in 0.8.0
- remove_dir_all (crates.io): affected all versions; fixed in 0.8.0
- remove_dir_all (crates.io): affected from 0.0.0-0; fixed in 0.8.0

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-mc8h-8q98-g5hr/ (Markdown: https://insights.sbomify.com/GHSA-mc8h-8q98-g5hr/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-mc8h-8q98-g5hr

---
From sbomify Insights: https://insights.sbomify.com/GHSA-mc8h-8q98-g5hr/
Summaries are generated and may contain mistakes; the official description above is the record.
