mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-01 06:02:32 -08:00
This change confines Taildrop filesystem operations to a persistent os.Root, preventing symlinks or receive-directory replacement from redirecting file access outside the intended directory. It also closes extension-owned roots during manager replacement and shutdown.
Fixes tailscale/corp#51118
Credit to Aditya @adios-404 for reporting
Change-Id: Ica39b3603221b3c7ba4460b176aae9b67eb64556
Change-Id: I37c836325eb319afd55a21a5b553175368a10783
(cherry picked from commit 6c0903552a)
Signed-off-by: Mike Jensen <[email protected]>
108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package taildrop
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"tailscale.com/tstime"
|
|
"tailscale.com/util/must"
|
|
)
|
|
|
|
func TestPutFileSymlink(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, offset := range []int64{0, 2} {
|
|
t.Run(fmt.Sprintf("offset_%d", offset), func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
outside := filepath.Join(t.TempDir(), "outside")
|
|
const original = "original"
|
|
if err := os.WriteFile(outside, []byte(original), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id := clientID("peer")
|
|
if err := os.Symlink(outside, filepath.Join(dir, "file.txt"+id.partialSuffix())); err != nil {
|
|
t.Skipf("cannot create symlinks: %v", err)
|
|
}
|
|
fops := must.Get(newFileOps(dir))
|
|
t.Cleanup(func() { fops.Close() })
|
|
mgr := managerOptions{Logf: t.Logf, fileOps: fops, DirectFileMode: true}.New()
|
|
t.Cleanup(mgr.Shutdown)
|
|
|
|
if _, err := mgr.PutFile(id, "file.txt", strings.NewReader("payload"), offset, 7); err == nil {
|
|
t.Fatal("PutFile succeeded through a symlink outside the Taildrop root")
|
|
}
|
|
got, err := os.ReadFile(outside)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(got) != original {
|
|
t.Errorf("outside file contents = %q; want %q", got, original)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPutFile(t *testing.T) {
|
|
const content = "hello, world"
|
|
|
|
tests := []struct {
|
|
name string
|
|
directFileMode bool
|
|
}{
|
|
{"DirectFileMode", true},
|
|
{"NonDirectFileMode", false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
fops := must.Get(newFileOps(dir))
|
|
t.Cleanup(func() { fops.Close() })
|
|
mgr := managerOptions{
|
|
Logf: t.Logf,
|
|
Clock: tstime.DefaultClock{},
|
|
State: nil,
|
|
fileOps: fops,
|
|
DirectFileMode: tt.directFileMode,
|
|
SendFileNotify: func() {},
|
|
}.New()
|
|
t.Cleanup(mgr.Shutdown)
|
|
|
|
id := clientID("0")
|
|
n, err := mgr.PutFile(id, "file.txt", strings.NewReader(content), 0, int64(len(content)))
|
|
if err != nil {
|
|
t.Fatalf("PutFile error: %v", err)
|
|
}
|
|
if n != int64(len(content)) {
|
|
t.Errorf("wrote %d bytes; want %d", n, len(content))
|
|
}
|
|
|
|
path := filepath.Join(dir, "file.txt")
|
|
|
|
got, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("ReadFile %q: %v", path, err)
|
|
}
|
|
if string(got) != content {
|
|
t.Errorf("file contents = %q; want %q", string(got), content)
|
|
}
|
|
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, entry := range entries {
|
|
if strings.Contains(entry.Name(), ".partial") {
|
|
t.Errorf("unexpected partial file left behind: %s", entry.Name())
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|