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 user metadata escaping the root through a planted symlink GHSA-r5wv-vqqh-2r9c CVE-PENDING

Nick Craig-Woodc0bf21991efa
diff --git a/backend/local/local.go b/backend/local/local.go
index d80756300..69cec91dd 100644
--- a/backend/local/local.go
+++ b/backend/local/local.go
@@ -1607,10 +1607,10 @@ func (f *Fs) mkdirAll(localPath string) (err error) {
return root.MkdirAll(rel, 0777)
}
-// openFile opens localPath for writing. 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 independent of the
-// root, which is closed before returning.
+// 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
+// independent of the root, which is closed before returning.
func (f *Fs) openFile(localPath string, flags int, perm os.FileMode) (fi *os.File, err error) {
if !f.opt.TranslateSymlinks {
return file.OpenFile(localPath, flags, perm)
diff --git a/backend/local/xattr.go b/backend/local/xattr.go
index 8ca8a7f35..6644b27c1 100644
--- a/backend/local/xattr.go
+++ b/backend/local/xattr.go
@@ -4,6 +4,7 @@ package local
import (
"fmt"
+ "os"
"strings"
"syscall"
@@ -95,6 +96,11 @@ func (o *Object) setXattr(metadata fs.Metadata) (err error) {
if !xattrSupported || o.fs.xattrSupported.Load() == 0 {
return nil
}
+ // When translating symlinks (-l/--links) write the xattrs through a
+ // descriptor opened under os.Root so a symlink planted in the destination
+ // can't redirect the write outside it (CWE-59). A translated link is
+ // itself the leaf, so it keeps the non-following path based write.
+ var fd *os.File
for k, value := range metadata {
k = strings.ToLower(k)
if _, found := systemMetadataInfo[k]; found {
@@ -102,9 +108,27 @@ func (o *Object) setXattr(metadata fs.Metadata) (err error) {
}
k = xattrPrefix + k
v := []byte(value)
- if o.fs.opt.FollowSymlinks {
+ switch {
+ case o.fs.opt.TranslateSymlinks && !o.translatedLink:
+ if fd == nil {
+ fd, err = o.fs.openFile(o.path, os.O_RDONLY, 0)
+ if os.IsPermission(err) {
+ // A write only file can't be opened for reading but its
+ // owner may still set xattrs on it, so try opening it for
+ // writing instead (which can't succeed for a directory).
+ if wfd, werr := o.fs.openFile(o.path, os.O_WRONLY, 0); werr == nil {
+ fd, err = wfd, nil
+ }
+ }
+ if err != nil {
+ return fmt.Errorf("failed to open for xattr: %w", err)
+ }
+ defer fs.CheckClose(fd, &err)
+ }
+ err = xattr.FSet(fd, k, v)
+ case o.fs.opt.FollowSymlinks:
err = xattr.Set(o.path, k, v)
- } else {
+ default:
err = xattr.LSet(o.path, k, v)
}
if err != nil {
diff --git a/backend/local/xattr_internal_test.go b/backend/local/xattr_internal_test.go
new file mode 100644
index 000000000..0b7f4bc3f
--- /dev/null
+++ b/backend/local/xattr_internal_test.go
@@ -0,0 +1,96 @@
+//go:build !openbsd && !plan9
+
+package local
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/pkg/xattr"
+ "github.com/rclone/rclone/fs"
+ "github.com/rclone/rclone/fstest"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// TestXattrThroughPlantedSymlinkBlocked checks user metadata (xattrs)
+// can't be written outside the root through a planted symlink with
+// --links, while in-tree writes still work.
+func TestXattrThroughPlantedSymlinkBlocked(t *testing.T) {
+ if !xattrSupported {
+ t.Skip("xattrs not supported on this OS")
+ }
+ skipIfNoSymlinks(t)
+ ctx := context.Background()
+
+ r := fstest.NewRun(t)
+ f := r.Flocal.(*Fs)
+ linksMode(f)
+
+ metadata := fs.Metadata{
+ "pwned": "attacker-controlled-value",
+ }
+
+ // Control: an in-tree directory gets the xattr as normal.
+ _, err := f.MkdirMetadata(ctx, "sub", metadata)
+ require.NoError(t, err)
+ if f.xattrSupported.Load() == 0 {
+ t.Skip("xattrs not supported on this filesystem")
+ }
+ v, err := xattr.LGet(filepath.Join(f.root, "sub"), "user.pwned")
+ require.NoError(t, err)
+ assert.Equal(t, "attacker-controlled-value", string(v))
+
+ // A directory outside the destination, with a pre-existing child,
+ // whose xattrs the attacker targets.
+ evil := t.TempDir()
+ evilDir := filepath.Join(evil, "secret.d")
+ require.NoError(t, os.Mkdir(evilDir, 0700))
+ evilSub := filepath.Join(evilDir, "sub")
+ require.NoError(t, os.Mkdir(evilSub, 0700))
+
+ // The planted symlink dst/pwn -> outside, faithfully reproduced by --links.
+ require.NoError(t, putLink(ctx, f, "pwn", evilDir))
+
+ // The source now presents "pwn/sub" as a directory with attacker-chosen
+ // user metadata. Applying it must not reach through the planted symlink.
+ _, err = f.MkdirMetadata(ctx, "pwn/sub", metadata)
+ require.Error(t, err, "applying metadata through a planted symlink should be refused")
+
+ _, err = xattr.LGet(evilSub, "user.pwned")
+ require.Error(t, err, "xattr escaped through planted symlink to %q", evilSub)
+}
+
+// TestXattrWriteOnlyFile checks user metadata can still be set on a
+// write-only file with --links, where the confined open can't read it.
+func TestXattrWriteOnlyFile(t *testing.T) {
+ if !xattrSupported {
+ t.Skip("xattrs not supported on this OS")
+ }
+ skipIfNoSymlinks(t)
+ ctx := context.Background()
+
+ r := fstest.NewRun(t)
+ f := r.Flocal.(*Fs)
+ linksMode(f)
+
+ require.NoError(t, putFile(ctx, f, "wo.txt", "write only"))
+ path := filepath.Join(f.root, "wo.txt")
+ require.NoError(t, os.Chmod(path, 0200))
+
+ obj, err := f.NewObject(ctx, "wo.txt")
+ require.NoError(t, err)
+ o := obj.(*Object)
+ err = o.setXattr(fs.Metadata{"potato": "chips"})
+ require.NoError(t, err)
+ if f.xattrSupported.Load() == 0 {
+ t.Skip("xattrs not supported on this filesystem")
+ }
+ // Reading xattrs needs read permission
+ require.NoError(t, os.Chmod(path, 0600))
+ v, err := xattr.LGet(path, "user.potato")
+ require.NoError(t, err)
+ assert.Equal(t, "chips", string(v))
+}