Skip to content

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

Closed

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.

Activity

  1. safern commented on Oct 21, 2019

    @safern
    Member
  2. transferred this issue fromdotnet/corefxon Feb 1, 2020
  3. added
    untriaged
    on Feb 23, 2020
  4. added this to the Future milestone on Feb 27, 2020
  5. removed
    untriaged
    on Feb 27, 2020
  6. JeremyKuhne commented on Feb 27, 2020

    @JeremyKuhne
    Member

    Triage: Any prototypes to improve this are welcome. We also can consider improvements for non-OSX platforms. Performance is a consideration, of course.

  7. joshudson commented on Feb 27, 2020

    @joshudson
    ContributorAuthor

    I can fix this for Linux immediately. I can't fix the Windows case for set-top NAS boxes. Better to just document the race condition?

  8. JeremyKuhne commented on Mar 5, 2020

    @JeremyKuhne
    Member

    I can fix this for Linux immediately

    @joshudson Presuming you can validate perf, please do :)

    Better to just document the race condition?

    If it isn't solvable, sure.

  9. joshudson commented on Mar 7, 2020

    @joshudson
    ContributorAuthor

    Working on this, I found the following in File.Move():

            Interop.Sys.FileStatus sourceStat, destStat;
            if (Interop.Sys.LStat(sourceFullPath, out sourceStat) == 0 && // source file exists
               (Interop.Sys.LStat(destFullPath, out destStat) != 0 || // dest file does not exist
                    sourceStat.Ino == destStat.Ino) && // source and dest are the same file on that device
    

    The only way this if statement can ever be true is if you have memory corruption, or the jitter gets smart enough to elide the zero-initialization of destStat since it's initialized by out (at which point if we still supported CAS I'd be filing a security bug, but we don't).

    I'm taking this block out anyway because I need to push the test down to native code.

  10. joshudson commented on Mar 8, 2020

    @joshudson
    ContributorAuthor

    Well runtime doesn't build out of the box on buster yet. I've got a not-ready-yet checkpoint while I work on this if anybody wants a preview of the fix.
    checkpoint.zip

  11. danmoseley commented on Mar 8, 2020

    @danmoseley
    Contributor

    Is there an issue for not building on Buster @joshudson

  12. joshudson commented on Mar 8, 2020

    @joshudson
    ContributorAuthor

    @danmosemsft : Nope. Looks intentional. Somebody upgrade the cmake version demand, and buster doesn't have a high enough version yet.

  13. joshudson commented on Mar 8, 2020

    @joshudson
    ContributorAuthor

    So I finally figured out what that if block above is actually doing. I had somehow managed to exclude it from consideration because of how broken the behavior is. "if the destination doesn't exist try the rename"; thus completely defeating the point of the link and remove operation below.

    So this issue exists for File.Move as well as Directory.Move.

  14. 6 remaining items

  15. jkotas commented on Mar 11, 2020

    @jkotas
    Member

    @janvorli What is the minimum kernel version that we need?

  16. added a commit that references this issue on Mar 11, 2020
  17. joshudson commented on Mar 11, 2020

    @joshudson
    ContributorAuthor

    As it stands, the PR accomplishes the following:

    1. The corresponding race condition for File.Move() is removed assuming at actually was a file and not a directory and hard links can be used.
    2. File.Move() with an actual file is faster due to requiring two fewer managed->native transitions. The system call count required is the same.
    3. Calling Directory.Move() is faster due to requiring one fewer managed->native transitions.
    4. Calling Directory.Move() to change the case of a directory on a case sensitive file system now works again.
    5. Directory.Move() no longer clobbers a file by the name of a target in the face of a transient network glitch (network-filesystem*).
    6. The PR adds latent code to fix the race condition for both files and directories for almost all Linux local filesystems. The code is opportunistically activated whenever glibc version is high enough. It would be trivial to opportunistically activate at runtime if #include <sys/syscall.h> could be resolved from Kernel 3.15 or newer.
    7. Any remaining race conditions are tightened as a consequence of having the check and the action in the same piece of native code. Also, splitting with a SIGSTOP is harder, but not impossible.

    * I have actually seen this fail in production, once. We lost data.

  18. joshudson commented on Apr 7, 2020

    @joshudson
    ContributorAuthor

    Can I get someone to check this? I thought I got all the build issues cleaned up but there's one broken build because the build infrastructure faulted.

  19. joshudson commented on Apr 11, 2020

    @joshudson
    ContributorAuthor

    @jkotas : Somebody needs to look at the failing test cases on the PR. The test cases are actually testing for the behavior being broken on Mac OSX.

  20. joshudson commented on May 22, 2021

    @joshudson
    ContributorAuthor

    Due to changes in the way github works, the branch containing the fix has changed from github.com/joshudson/runtime/master to github.com/joshudson/runtime/master to github.com/joshudson/runtime/dmove

  21. dotnet-policy-service commented on Nov 21, 2025

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  22. added
    backlog-cleanup-candidate
    on Nov 21, 2025
  23. dotnet-policy-service commented on Dec 5, 2025

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  24. removed this from the Future milestone on Dec 5, 2025
  25. locked and limited conversation to collaborators on Jan 5, 2026
  26. removed
    backlog-cleanup-candidate
    on Jan 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions