Skip to content

System.IO.Directory.Move claims to throw when the target already exists; however it does not do so reliably. #31228

@joshudson

Description

@joshudson

Starting with .NET Core 3.0, the Move method throws an IOException in all platforms when the destDirName already exists. In .NET Core 2.2 and previous versions, the exception was only thrown on Windows, and other platforms could either fail or overwrite the destDirName. See C++ rename.

Unfortunately, the fix was made by inserting a call to FileExists(), which is broken. Even if FileExists() worked, this is subject to a TOCTOU bug. As a consequence, rename targets can be randomly overwritten.

This can be actually fixed in Linux by using the renameat2() system call; however I could find no solution for OSX.

This problem can also occur on Windows when talking to set-top NAS boxes. Two computers may be required to reproduce.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions