You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.IO.Directory.Move claims to throw when the target already exists; however it does not do so reliably. #31228
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.
Triage: Any prototypes to improve this are welcome. We also can consider improvements for non-OSX platforms. Performance is a consideration, of course.
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.
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
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.
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.
File.Move() with an actual file is faster due to requiring two fewer managed->native transitions. The system call count required is the same.
Calling Directory.Move() is faster due to requiring one fewer managed->native transitions.
Calling Directory.Move() to change the case of a directory on a case sensitive file system now works again.
Directory.Move() no longer clobbers a file by the name of a target in the face of a transient network glitch (network-filesystem*).
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.
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.
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.
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
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 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.
Activity
safern commentedon Oct 21, 2019
cc: @carlossanlop @JeremyKuhne
JeremyKuhne commentedon Feb 27, 2020
Triage: Any prototypes to improve this are welcome. We also can consider improvements for non-OSX platforms. Performance is a consideration, of course.
joshudson commentedon Feb 27, 2020
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?
JeremyKuhne commentedon Mar 5, 2020
@joshudson Presuming you can validate perf, please do :)
If it isn't solvable, sure.
joshudson commentedon Mar 7, 2020
Working on this, I found the following in File.Move():
The only way this
ifstatement can ever be true is if you have memory corruption, or the jitter gets smart enough to elide the zero-initialization ofdestStatsince it's initialized byout(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.
joshudson commentedon Mar 8, 2020
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
danmoseley commentedon Mar 8, 2020
Is there an issue for not building on Buster @joshudson
joshudson commentedon Mar 8, 2020
@danmosemsft : Nope. Looks intentional. Somebody upgrade the cmake version demand, and buster doesn't have a high enough version yet.
joshudson commentedon Mar 8, 2020
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.
6 remaining items
jkotas commentedon Mar 11, 2020
@janvorli What is the minimum kernel version that we need?
dotnet#31228 remove race conditions from File.Move and Directory.Move…
joshudson commentedon Mar 11, 2020
As it stands, the PR accomplishes the following:
#include <sys/syscall.h>could be resolved from Kernel 3.15 or newer.SIGSTOPis harder, but not impossible.* I have actually seen this fail in production, once. We lost data.
joshudson commentedon Apr 7, 2020
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.
joshudson commentedon Apr 11, 2020
@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.
joshudson commentedon May 22, 2021
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
dotnet-policy-service commentedon Nov 21, 2025
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.
dotnet-policy-service commentedon Dec 5, 2025
This issue will now be closed since it had been marked
no-recent-activitybut 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.