With -l/--links the local backend faithfully recreates a source ".rclonelink" as a real symlink at the destination. Directory and file metadata writes (chmod/chown/chtimes/btime) are confined to the destination with os.Root so a symlink planted by an untrusted source can't redirect them outside it. The user metadata write (user.* xattrs) was not confined though: setXattr called xattr.LSet on the raw path, which doesn't follow the leaf but does follow any symlink in the parent components. When the destination already contained a planted symlink "pwn" pointing outside the root and the target held an existing child "sub", syncing a source directory "pwn/sub" with --metadata set attacker chosen user.* xattrs on the outside directory, and reported success as the confined mode and time writes had nothing to do. Write the xattrs through a descriptor opened under os.Root when translating symlinks, so a planted symlink can no longer redirect the write out of the destination, while legitimate in-tree writes are unaffected. A write only file which can't be opened for reading is opened for writing instead so its owner can still set xattrs on it. Translated links are themselves the leaf and keep the non-following path based write.
97 lines
2.9 KiB
Go
97 lines
2.9 KiB
Go
//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))
|
|
}
|