Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

local: fix DirMove creating the destination parent through a planted symlink

Nick Craig-Wood5c5a3199d739
diff --git a/backend/local/local.go b/backend/local/local.go
index 0442223cb..ce7709514 100644
--- a/backend/local/local.go
+++ b/backend/local/local.go
@@ -1202,9 +1202,14 @@ func (f *Fs) DirMove(ctx context.Context, src fs.Fs, srcRemote, dstRemote string
return fs.ErrorDirExists
}
- // Create parent of destination
+ // Create parent of destination. The root itself is the trusted
+ // destination so its parent, which lies outside it, is created directly.
dstParentPath := filepath.Dir(dstPath)
- err = file.MkdirAll(dstParentPath, 0777)
+ if dstPath == f.root {
+ err = file.MkdirAll(dstParentPath, 0777)
+ } else {
+ err = f.mkdirAll(dstParentPath)
+ }
if err != nil {
return err
}
diff --git a/backend/local/local_internal_test.go b/backend/local/local_internal_test.go
index 99108f550..680f8d3e3 100644
--- a/backend/local/local_internal_test.go
+++ b/backend/local/local_internal_test.go
@@ -636,6 +636,33 @@ func TestRemoveThroughPlantedSymlinkBlocked(t *testing.T) {
require.NoError(t, err, "a directory was removed outside the destination at %q", evilDir)
}
+// TestDirMoveThroughPlantedSymlinkBlocked checks DirMove can't move a
+// directory out of the root through a planted symlink.
+func TestDirMoveThroughPlantedSymlinkBlocked(t *testing.T) {
+ skipIfNoSymlinks(t)
+ ctx := context.Background()
+
+ evil := t.TempDir()
+
+ r := fstest.NewRun(t)
+ f := r.Flocal.(*Fs)
+ linksMode(f)
+
+ // Control: an in-tree directory move works as normal.
+ require.NoError(t, f.Mkdir(ctx, "dir"))
+ require.NoError(t, f.DirMove(ctx, f, "dir", "sub/moved"))
+
+ // The planted symlink dst/pwn -> outside, faithfully reproduced by --links.
+ require.NoError(t, putLink(ctx, f, "pwn", evil))
+
+ err := f.DirMove(ctx, f, "sub/moved", "pwn/moved")
+ require.Error(t, err, "moving a directory through a planted symlink should be refused")
+ _, err = os.Lstat(filepath.Join(evil, "moved"))
+ require.True(t, os.IsNotExist(err), "a directory escaped the destination into %q", evil)
+ _, err = os.Lstat(filepath.Join(f.root, "sub", "moved"))
+ require.NoError(t, err, "the directory should still be in the destination")
+}
+
// TestEncodingEscapeBlocked checks that a name from a malicious source can't
// be decoded into path syntax which writes outside the destination.
func TestEncodingEscapeBlocked(t *testing.T) {