feature/taildrop: allow symlink at the receive root

PR #22749 refused a symlink at the receive root itself, but on QNAP and Unraid the platform deliberately selects one.

This change removes that check so the symlink is followed again. os.Root still refuses symlinks inside the root, so a pre-planted symlink in the receive directory cannot redirect writes outside it.

Fixes #22885

Change-Id: Ic547a299b909591486253ec850bdc63cca62d175
Signed-off-by: Mike Jensen <mikej@tailscale.com>
This commit is contained in:
Mike Jensen committed 2026-12-04 12:28:12 -07:00
1 parent 0e8bf23404
commit 652d569867
2 files changed
+68 -17

No files matched your search

+6 -9
View File
@@ -39,19 +39,16 @@ func init() {
}
}
// root opens rootDir, creating it if needed.
// A fresh root is opened per operation so that deletion or replacement of
// rootDir is handled without a cached handle.
// root opens rootDir, creating it if needed. rootDir itself may be a symlink,
// which is how some NAS platforms expose the shared folder, but its target must
// already exist and is never created. A fresh root is opened per operation so
// that deletion, replacement or repointing of rootDir is handled.
func (f *fsFileOps) root() (*os.Root, error) {
fi, err := os.Lstat(f.rootDir)
if err != nil {
if _, err := os.Lstat(f.rootDir); err != nil {
// MkdirAll fails on a dangling symlink without creating its target
if err := os.MkdirAll(f.rootDir, 0o700); err != nil {
return nil, fmt.Errorf("mkdir %q: %w", f.rootDir, err)
}
} else if fi.Mode()&os.ModeSymlink != 0 {
// Refuse to follow a symlink at rootDir itself. os.OpenRoot would
// otherwise resolve it and operate outside the intended directory.
return nil, fmt.Errorf("root %q is a symlink", f.rootDir)
}
root, err := os.OpenRoot(f.rootDir)
if err != nil {
+62 -8
View File
@@ -52,22 +52,28 @@ func TestFSFileOpsSymlinkConfinement(t *testing.T) {
}
}
func TestOpenWriterRootSwap(t *testing.T) {
func TestOpenWriterRootDangling(t *testing.T) {
t.Parallel()
base := t.TempDir()
root := filepath.Join(base, "Taildrop")
outside := filepath.Join(base, "outside")
require.NoError(t, os.Mkdir(root, 0o700))
require.NoError(t, os.Mkdir(outside, 0o700))
fops, err := newFileOps(root)
require.NoError(t, err)
target := filepath.Join(base, "missing")
mustSymlink(t, target, root)
// A dangling root link is an error and its target is never created.
_, err := newFileOps(root)
require.Error(t, err)
require.NoDirExists(t, target)
require.NoError(t, os.Remove(root))
mustSymlink(t, outside, root)
require.NoError(t, os.Mkdir(root, 0o700))
fops, err := newFileOps(root)
require.NoError(t, err)
require.NoError(t, os.Remove(root))
mustSymlink(t, target, root)
_, _, err = fops.OpenWriter("payload.txt", 0, 0o600)
require.Error(t, err)
require.NoFileExists(t, filepath.Join(outside, "payload.txt"))
require.NoDirExists(t, target)
}
func TestOpenWriterResumeSymlink(t *testing.T) {
@@ -196,6 +202,54 @@ func TestFSFileOpsRootRecreated(t *testing.T) {
require.Equal(t, "content", string(got))
}
func TestOpenWriterRootSymlink(t *testing.T) {
t.Parallel()
base := t.TempDir()
root := filepath.Join(base, "Taildrop")
shareA := filepath.Join(base, "shareA")
shareB := filepath.Join(base, "shareB")
outside := filepath.Join(base, "outside")
for _, dir := range []string{shareA, shareB, outside} {
require.NoError(t, os.Mkdir(dir, 0o700))
}
mustSymlink(t, shareA, root)
fops, err := newFileOps(root)
require.NoError(t, err)
// Receive through the symlinked root.
w, path, err := fops.OpenWriter("file.partial", 0, 0o600)
require.NoError(t, err)
require.NoError(t, w.Close())
require.FileExists(t, filepath.Join(shareA, "file.partial"))
_, err = fops.Rename(path, "file")
require.NoError(t, err)
require.FileExists(t, filepath.Join(shareA, "file"))
// Repointing the link takes effect without a restart.
require.NoError(t, os.Remove(root))
mustSymlink(t, shareB, root)
w, _, err = fops.OpenWriter("file.partial", 0, 0o600)
require.NoError(t, err)
require.NoError(t, w.Close())
require.FileExists(t, filepath.Join(shareB, "file.partial"))
require.NoFileExists(t, filepath.Join(shareA, "file.partial"))
// Symlinks inside the root are still refused.
mustSymlink(t, outside, filepath.Join(shareB, "payload.partial"))
_, _, err = fops.OpenWriter("payload.partial", 0, 0o600)
require.Error(t, err)
require.NoFileExists(t, filepath.Join(outside, "payload.partial"))
// Replacing the root with a plain directory is honored.
require.NoError(t, os.Remove(root))
require.NoError(t, os.Mkdir(root, 0o700))
w, _, err = fops.OpenWriter("file.partial", 0, 0o600)
require.NoError(t, err)
require.NoError(t, w.Close())
require.FileExists(t, filepath.Join(root, "file.partial"))
}
func mustSymlink(t *testing.T, target, link string) {
t.Helper()