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 MkdirMetadata finding directories outside the root through a planted symlink

Nick Craig-Woodb000741c9810
diff --git a/backend/local/local.go b/backend/local/local.go
index 69cec91dd..76a4b866b 100644
--- a/backend/local/local.go
+++ b/backend/local/local.go
@@ -960,13 +960,13 @@ func (f *Fs) MkdirMetadata(ctx context.Context, dir string, metadata fs.Metadata
if err != nil {
return nil, err
}
- fi, err := f.lstat(localPath)
+ fi, err := f.lstatRoot(localPath)
if errors.Is(err, os.ErrNotExist) {
err := f.Mkdir(ctx, dir)
if err != nil {
return nil, fmt.Errorf("mkdir metadata: failed make directory: %w", err)
}
- fi, err = f.lstat(localPath)
+ fi, err = f.lstatRoot(localPath)
if err != nil {
return nil, fmt.Errorf("mkdir metadata: failed to read info: %w", err)
}
@@ -1607,6 +1607,22 @@ func (f *Fs) mkdirAll(localPath string) (err error) {
return root.MkdirAll(rel, 0777)
}
+// lstatRoot returns the info for localPath without following a symlink at
+// the leaf. When translating symlinks it goes through os.Root so a symlink
+// planted at any parent can't make a path outside the destination look like
+// it exists, which would let its metadata be written there.
+func (f *Fs) lstatRoot(localPath string) (fi os.FileInfo, err error) {
+ if !f.opt.TranslateSymlinks {
+ return f.lstat(localPath)
+ }
+ root, rel, err := f.osRoot(localPath)
+ if err != nil {
+ return nil, err
+ }
+ defer fs.CheckClose(root, &err)
+ return root.Lstat(rel)
+}
+
// openFile opens localPath with the given flags and perm. When translating
// symlinks it goes through os.Root so a symlink planted at the path, or at any
// parent, is never followed out of the destination. The returned file is
diff --git a/backend/local/local_internal_test.go b/backend/local/local_internal_test.go
index 2b07ca9d2..727c4031b 100644
--- a/backend/local/local_internal_test.go
+++ b/backend/local/local_internal_test.go
@@ -560,6 +560,40 @@ func TestDirBTimeThroughPlantedSymlinkBlocked(t *testing.T) {
require.Equal(t, before, readBTime(), "btime escaped through planted symlink to %q", evilDir)
}
+// TestMkdirMetadataThroughPlantedSymlinkBlocked checks MkdirMetadata can't
+// be fooled into treating a directory outside the root, reached through a
+// planted symlink, as the directory to apply metadata to.
+func TestMkdirMetadataThroughPlantedSymlinkBlocked(t *testing.T) {
+ skipIfNoSymlinks(t)
+ ctx := context.Background()
+
+ evil := t.TempDir()
+ evilDir := filepath.Join(evil, "secret.d")
+ require.NoError(t, os.Mkdir(evilDir, 0700))
+ require.NoError(t, os.Mkdir(filepath.Join(evilDir, "sub"), 0700))
+
+ r := fstest.NewRun(t)
+ f := r.Flocal.(*Fs)
+ linksMode(f)
+
+ // Control: an existing in-tree directory is found as normal.
+ require.NoError(t, f.Mkdir(ctx, "sub"))
+ _, err := f.MkdirMetadata(ctx, "sub", nil)
+ require.NoError(t, err)
+
+ // The planted symlink dst/pwn -> outside, faithfully reproduced by --links.
+ require.NoError(t, putLink(ctx, f, "pwn", evilDir))
+
+ // "pwn/sub" exists outside the root but must not be found through the
+ // planted symlink, and must not be created there either.
+ _, err = f.MkdirMetadata(ctx, "pwn/sub", nil)
+ require.Error(t, err, "finding a directory through a planted symlink should be refused")
+ _, err = f.MkdirMetadata(ctx, "pwn/new", nil)
+ require.Error(t, err, "making a directory through a planted symlink should be refused")
+ _, err = os.Lstat(filepath.Join(evilDir, "new"))
+ require.True(t, os.IsNotExist(err), "a directory escaped the destination into %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) {