"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
local: fix Remove and Rmdir deleting outside the root through a planted symlink
Nick Craig-Wood37b306d5d1a6
diff --git a/backend/local/local.go b/backend/local/local.goindex 76a4b866b..0442223cb 100644--- a/backend/local/local.go+++ b/backend/local/local.go@@ -998,20 +998,34 @@ func (f *Fs) MkdirMetadata(ctx context.Context, dir string, metadata fs.Metadata// Rmdir removes the directory//// If it isn't empty it will return an error-func (f *Fs) Rmdir(ctx context.Context, dir string) error {+func (f *Fs) Rmdir(ctx context.Context, dir string) (err error) {localPath, err := f.localPath(dir)if err != nil {return err}- if fi, err := os.Stat(localPath); err != nil {+ // When translating symlinks go through os.Root so a symlink planted at+ // any parent can't redirect the removal out of the destination, and+ // don't follow the leaf as a symlink is a file in this mode. The root+ // itself is the trusted destination and is removed directly.+ stat, removeFn, chmod := os.Stat, os.Remove, os.Chmod+ if f.opt.TranslateSymlinks && localPath != f.root {+ var root *os.Root+ root, localPath, err = f.osRoot(localPath)+ if err != nil {+ return err+ }+ defer fs.CheckClose(root, &err)+ stat, removeFn, chmod = root.Lstat, root.Remove, root.Chmod+ }+ if fi, err := stat(localPath); err != nil {return err} else if !fi.IsDir() {return fs.ErrorIsFile}- err = os.Remove(localPath)+ err = removeFn(localPath)if runtime.GOOS == "windows" && errors.Is(err, iofs.ErrPermission) { // https://github.com/golang/go/issues/26295- if os.Chmod(localPath, 0o600) == nil {- err = os.Remove(localPath)+ if chmod(localPath, 0o600) == nil {+ err = removeFn(localPath)}}return err@@ -1695,6 +1709,21 @@ func (f *Fs) chtimes(localPath string, atime, mtime time.Time) (err error) {return root.Chtimes(rel, atime, mtime)}+// remove removes the file, symlink or empty directory at localPath. When+// translating symlinks it goes through os.Root so a symlink planted at any+// parent can't redirect the removal out of the destination.+func (f *Fs) remove(localPath string) (err error) {+ if !f.opt.TranslateSymlinks {+ return remove(localPath, os.Remove)+ }+ root, rel, err := f.osRoot(localPath)+ if err != nil {+ return err+ }+ defer fs.CheckClose(root, &err)+ return remove(rel, root.Remove)+}+// mkdirAll makes all the directories needed to store the objectfunc (o *Object) mkdirAll() error {return o.fs.mkdirAll(filepath.Dir(o.path))@@ -1819,7 +1848,7 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, opif err != nil {fs.Logf(o, "Removing partially written file on error: %v", err)- if removeErr := os.Remove(o.path); removeErr != nil {+ if removeErr := o.fs.remove(o.path); removeErr != nil {fs.Errorf(o, "Failed to remove partially written file: %v", removeErr)}return err@@ -1967,7 +1996,7 @@ func (o *Object) lstat() error {// Remove an objectfunc (o *Object) Remove(ctx context.Context) error {o.clearHashCache()- return remove(o.path)+ return o.fs.remove(o.path)}// Metadata returns metadata for an objectdiff --git a/backend/local/local_internal_test.go b/backend/local/local_internal_test.goindex 727c4031b..99108f550 100644--- a/backend/local/local_internal_test.go+++ b/backend/local/local_internal_test.go@@ -594,6 +594,48 @@ func TestMkdirMetadataThroughPlantedSymlinkBlocked(t *testing.T) {require.True(t, os.IsNotExist(err), "a directory escaped the destination into %q", evilDir)}+// TestRemoveThroughPlantedSymlinkBlocked checks Remove and Rmdir can't+// delete anything outside the root through a planted symlink.+func TestRemoveThroughPlantedSymlinkBlocked(t *testing.T) {+ skipIfNoSymlinks(t)+ ctx := context.Background()++ evil := t.TempDir()+ evilFile := filepath.Join(evil, "secret.txt")+ require.NoError(t, os.WriteFile(evilFile, []byte("keep me"), 0600))+ evilDir := filepath.Join(evil, "secret.d")+ require.NoError(t, os.Mkdir(evilDir, 0700))++ r := fstest.NewRun(t)+ f := r.Flocal.(*Fs)+ linksMode(f)++ // Control: in-tree removals work as normal.+ require.NoError(t, putFile(ctx, f, "file.txt", "data"))+ o, err := f.NewObject(ctx, "file.txt")+ require.NoError(t, err)+ require.NoError(t, o.Remove(ctx))+ require.NoError(t, f.Mkdir(ctx, "dir"))+ require.NoError(t, f.Rmdir(ctx, "dir"))++ // The planted symlink dst/pwn -> outside, faithfully reproduced by --links.+ require.NoError(t, putLink(ctx, f, "pwn", evil))++ // Removing the outside file and directory named through the planted+ // symlink must be refused.+ o, err = f.NewObject(ctx, "pwn/secret.txt")+ require.NoError(t, err)+ err = o.Remove(ctx)+ require.Error(t, err, "removing through a planted symlink should be refused")+ _, err = os.Lstat(evilFile)+ require.NoError(t, err, "a file was removed outside the destination at %q", evilFile)++ err = f.Rmdir(ctx, "pwn/secret.d")+ require.Error(t, err, "removing a directory through a planted symlink should be refused")+ _, err = os.Lstat(evilDir)+ require.NoError(t, err, "a directory was removed outside the destination at %q", evilDir)+}+// 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) {diff --git a/backend/local/remove_other.go b/backend/local/remove_other.goindex 25a19eedf..f9e972147 100644--- a/backend/local/remove_other.go+++ b/backend/local/remove_other.go@@ -2,9 +2,7 @@package local-import "os"--// Removes name, retrying on a sharing violation-func remove(name string) error {- return os.Remove(name)+// remove removes name with removeFn, retrying on a sharing violation+func remove(name string, removeFn func(string) error) error {+ return removeFn(name)}diff --git a/backend/local/remove_test.go b/backend/local/remove_test.goindex 631f2dda0..db009ab76 100644--- a/backend/local/remove_test.go+++ b/backend/local/remove_test.go@@ -38,7 +38,7 @@ func TestRemove(t *testing.T) {require.NoError(t, fd.Close())})// delete the open file- err = remove(name)+ err = remove(name, os.Remove)require.NoError(t, err)// check it no longer existsassert.False(t, exists())diff --git a/backend/local/remove_windows.go b/backend/local/remove_windows.goindex 4a4eb8b7c..6b318ddda 100644--- a/backend/local/remove_windows.go+++ b/backend/local/remove_windows.go@@ -10,12 +10,12 @@ import ("golang.org/x/sys/windows")-// Removes name, retrying on a sharing violation-func remove(name string) (err error) {+// remove removes name with removeFn, retrying on a sharing violation+func remove(name string, removeFn func(string) error) (err error) {const maxTries = 10var sleepTime = 1 * time.Millisecondfor i := range maxTries {- err = os.Remove(name)+ err = removeFn(name)if err == nil {break}