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 .rclonelink names escaping the root with --links GHSA-55qp-jrwr-x827 CVE-PENDING

Nick Craig-Wood4d1bd4c71dbe
diff --git a/backend/local/local.go b/backend/local/local.go
index 73aea3bbb..977a3a88e 100644
--- a/backend/local/local.go
+++ b/backend/local/local.go
@@ -445,6 +445,7 @@ var (
errPathEscapes = errors.New("file name is not a path within the local root - check the encoding")
errSymlinkLoop = errors.New("loop detected: points to a parent directory")
errLinkTargetTooLong = errors.New("symlink target is too long to be a path")
+ errLinkNameInvalid = errors.New("translated link needs a file name before the \"" + fs.LinkSuffix + "\" suffix")
)
// NewFs constructs an Fs from the path
@@ -572,30 +573,26 @@ func (f *Fs) caseInsensitive() bool {
return runtime.GOOS == "windows" || runtime.GOOS == "darwin"
}
-// translateLink checks whether the remote is a translated link
-// and returns a new path, removing the suffix as needed,
-// It also returns whether this is a translated link at all
-//
-// for regular files, localPath is returned unchanged
-func translateLink(remote, localPath string) (newLocalPath string, isTranslatedLink bool) {
- isTranslatedLink = strings.HasSuffix(remote, fs.LinkSuffix)
- newLocalPath = strings.TrimSuffix(localPath, fs.LinkSuffix)
- return newLocalPath, isTranslatedLink
-}
-
// newObject makes a half completed Object
func (f *Fs) newObject(remote string) (*Object, error) {
- translatedLink := false
- localPath, err := f.localPath(remote)
+ name, translatedLink := remote, false
+ if f.opt.TranslateSymlinks {
+ name, translatedLink = strings.CutSuffix(remote, fs.LinkSuffix)
+ }
+ if translatedLink {
+ // Check this makes a valid native file name
+ native := f.nativePath(name)
+ switch native[strings.LastIndex(native, string(filepath.Separator))+1:] {
+ case "", ".", "..":
+ return nil, fserrors.NoRetryError(fmt.Errorf("%q: %w", remote, errLinkNameInvalid))
+ }
+ }
+ // Check the name after any suffix removal
+ localPath, err := f.localPath(name)
if err != nil {
return nil, err
}
- if f.opt.TranslateSymlinks {
- // Possibly receive a new name for localPath
- localPath, translatedLink = translateLink(remote, localPath)
- }
-
return &Object{
fs: f,
remote: remote,
@@ -818,6 +815,12 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e
continue
}
fso, err := f.newObjectWithInfo(newRemote, fi)
+ if errors.Is(err, errLinkNameInvalid) {
+ // Skip a badly named ".rclonelink" file
+ fs.Errorf(newRemote, "Listing error: %v", err)
+ _ = accounting.Stats(ctx).Error(err)
+ continue
+ }
if err != nil {
return nil, err
}
@@ -862,14 +865,19 @@ func (f *Fs) cleanRemote(dir, filename string) (remote string) {
return
}
+// nativePath returns name in the OS encoding with OS path separators,
+// relative to f.root, without cleaning it.
+func (f *Fs) nativePath(name string) string {
+ return filepath.FromSlash(f.opt.Enc.FromStandardPath(name))
+}
+
// localPath returns the OS path for the object called name, which is
// always underneath f.root.
//
// It returns errPathEscapes if name resolves outside f.root which can
// happen depending on the encoding.
func (f *Fs) localPath(name string) (string, error) {
- native := filepath.FromSlash(f.opt.Enc.FromStandardPath(name))
- localPath := filepath.Join(f.root, native)
+ localPath := filepath.Join(f.root, f.nativePath(name))
rel, err := filepath.Rel(f.root, localPath)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return "", fserrors.NoRetryError(fmt.Errorf("%q: %w", name, errPathEscapes))
diff --git a/backend/local/local_internal_test.go b/backend/local/local_internal_test.go
index b8525ad90..4fd639a37 100644
--- a/backend/local/local_internal_test.go
+++ b/backend/local/local_internal_test.go
@@ -646,6 +646,122 @@ func TestLocalPath(t *testing.T) {
}
}
+// TestLinkSuffixEscapeBlocked checks that in --links mode a name which
+// passes the root check with its ".rclonelink" suffix can't escape the
+// destination once the suffix is removed: "...rclonelink" must not
+// become "..", the parent of the root.
+func TestLinkSuffixEscapeBlocked(t *testing.T) {
+ skipIfNoSymlinks(t)
+ ctx := context.Background()
+ outer := t.TempDir()
+ newLinksFs := func(dir, encoding string) *Fs {
+ fRaw, err := NewFs(ctx, "local", filepath.Join(outer, dir), configmap.Simple{"encoding": encoding})
+ require.NoError(t, err)
+ f := fRaw.(*Fs)
+ linksMode(f)
+ require.NoError(t, f.Mkdir(ctx, ""))
+ return f
+ }
+ f := newLinksFs("dst", encoder.OS.String())
+ s := newLinksFs("slash", "Slash")
+
+ // Stamp the parent of the destinations so a write to it shows up.
+ before := fstest.Time("2011-12-25T12:59:59Z")
+ require.NoError(t, os.Chtimes(outer, before, before))
+ parentUnchanged := func() {
+ t.Helper()
+ fi, err := os.Lstat(outer)
+ require.NoError(t, err)
+ assert.True(t, fi.ModTime().Equal(before), "the parent of the destination was modified")
+ }
+
+ // A directory called "...rclonelink" is created under its own name
+ // and its metadata goes there, not to the parent of the root which
+ // trimming the suffix off its path would address.
+ remote := ".." + fs.LinkSuffix
+ metadata := fs.Metadata{"mtime": "2001-02-03T04:05:06Z"}
+ _, err := f.MkdirMetadata(ctx, remote, metadata)
+ require.NoError(t, err)
+ parentUnchanged()
+
+ // A file called "...rclonelink" is a link called "..", which the
+ // default encoding stores as fullwidth dots like any other name.
+ require.NoError(t, putFile(ctx, f, remote, "target"))
+ fi, err := os.Lstat(filepath.Join(f.root, ".."))
+ require.NoError(t, err)
+ assert.NotZero(t, fi.Mode()&os.ModeSymlink)
+ parentUnchanged()
+
+ // Ordinary translated links still resolve to the file without the suffix.
+ o, err := f.newObject("sub/link" + fs.LinkSuffix)
+ require.NoError(t, err)
+ assert.True(t, o.translatedLink)
+ assert.Equal(t, filepath.Join(f.root, "sub", "link"), o.path)
+
+ // A name which merely contains the suffix is a regular file.
+ o, err = f.newObject("sub/link" + fs.LinkSuffix + ".txt")
+ require.NoError(t, err)
+ assert.False(t, o.translatedLink)
+ assert.Equal(t, filepath.Join(f.root, "sub", "link"+fs.LinkSuffix+".txt"), o.path)
+
+ // An encoding without Dot leaves ".." as a real ".." once the suffix
+ // is gone, so the name is refused instead.
+ require.ErrorIs(t, putFile(ctx, s, remote, "PWNED"), errLinkNameInvalid)
+ _, err = s.MkdirMetadata(ctx, remote, metadata)
+ require.NoError(t, err)
+ parentUnchanged()
+
+ // As is any other name which doesn't leave a file name once the
+ // suffix is removed and the encoding applied.
+ for _, name := range []string{
+ remote,
+ "sub/.." + fs.LinkSuffix,
+ "sub/." + fs.LinkSuffix,
+ "sub/.." + fs.LinkSuffix,
+ "sub/sub2/.." + fs.LinkSuffix,
+ "sub/." + fs.LinkSuffix,
+ "sub/" + fs.LinkSuffix,
+ fs.LinkSuffix,
+ } {
+ _, err = s.newObject(name)
+ assert.ErrorIs(t, err, errLinkNameInvalid, name)
+ assert.True(t, fserrors.IsNoRetryError(err), name)
+ }
+
+ // The root check still applies to the name of the link.
+ _, err = s.newObject("../link" + fs.LinkSuffix)
+ require.ErrorIs(t, err, errPathEscapes)
+
+ if runtime.GOOS == "windows" {
+ // A backslash is a path separator on Windows, so an encoding
+ // without BackSlash can hide ".." behind one.
+ w := newLinksFs("win", "Slash,Dot")
+ _, err = w.newObject(`sub\..` + fs.LinkSuffix)
+ assert.ErrorIs(t, err, errLinkNameInvalid)
+ }
+}
+
+// TestLinkSuffixListSkipsInvalidName checks that with --links a regular
+// file whose name is just the suffix is reported as an error and skipped
+// rather than failing the listing of its whole directory.
+func TestLinkSuffixListSkipsInvalidName(t *testing.T) {
+ ctx := context.Background()
+ r := fstest.NewRun(t)
+ f := r.Flocal.(*Fs)
+ linksMode(f)
+ when := fstest.Time("2001-02-03T04:05:06Z")
+ r.WriteFile("sub/"+fs.LinkSuffix, "not a link", when)
+ r.WriteFile("sub/file.txt", "hello", when)
+
+ accounting.Stats(ctx).ResetErrors()
+ entries, err := f.List(ctx, "sub")
+ require.NoError(t, err)
+ require.Len(t, entries, 1)
+ assert.Equal(t, "sub/file.txt", entries[0].Remote())
+ assert.Equal(t, int64(1), accounting.Stats(ctx).GetErrors())
+ accounting.Stats(ctx).ResetErrors()
+}
+
// TestLinkSuffixDirectory checks that in --links mode a directory whose
// name ends in ".rclonelink" is an ordinary directory. Only files are
// translated into links, so the suffix must stay in its path rather