restorer: reject trees with duplicate or out-of-order node names (#22190)

Co-authored-by: Michael Eischer <michael.eischer@fau.de>
Signed-off-by: xh123637 <3324733895@qq.com>
This commit is contained in:
xh123637andMichael Eischer authored and GitHub committed 2026-11-15 20:16:32 +01:00
1 parent d7aa4145e0
commit 2126756613
3 files changed
+150

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
Bugfix: Reject trees with duplicate or out-of-order node names during `restore`
A tree containing a symlink and a directory with the same name could let
`restic restore` write through the symlink to files outside the restore
target. Restic now rejects trees whose node names are not strictly
increasing during restore and reports an error instead of restoring such
a snapshot.
https://github.com/restic/restic/issues/22180
+18
View File
@@ -168,6 +168,13 @@ func (res *Restorer) traverseTreeInner(ctx context.Context, target, location str
if res.opts.Delete {
filenames = make([]string, 0)
}
// Track the previous node name to enforce the ordering invariant that
// tree writers guarantee: names must be strictly increasing. Trees
// with duplicate or out-of-order names must be rejected, otherwise a
// symlink and a directory sharing a name could make restore write
// through the symlink to files outside the restore target.
lastName := ""
for item := range tree {
if item.Error != nil {
debug.Log("error iterating tree %v: %v", treeID, item.Error)
@@ -177,6 +184,17 @@ func (res *Restorer) traverseTreeInner(ctx context.Context, target, location str
return nil, hasRestored, res.sanitizeError(location, item.Error)
}
node := item.Node
if node.Name <= lastName {
err := fmt.Errorf("node %q, last %q in tree %v: %w", node.Name, lastName, treeID, data.ErrTreeNotOrdered)
err = res.sanitizeError(location, err)
if err != nil {
return nil, hasRestored, err
}
continue
}
lastName = node.Name
if ctx.Err() != nil {
return nil, hasRestored, ctx.Err()
}
+123
View File
@@ -1571,3 +1571,126 @@ func TestRestorerLongPath(t *testing.T) {
_, err = res.VerifyFiles(ctx, tmp, countRestoredFiles, restic.NoopCounter)
rtest.OK(t, err)
}
// saveUnorderedTree saves nodes in the given order without enforcing the
// name ordering that TreeJSONBuilder requires. This simulates trees written
// by clients that bypass restic's ordering checks.
func saveUnorderedTree(t testing.TB, saver restic.BlobSaver, nodes []*data.Node) restic.ID {
t.Helper()
ctx := context.Background()
buf, err := json.Marshal(struct {
Nodes []*data.Node `json:"nodes"`
}{Nodes: nodes})
if err != nil {
t.Fatal(err)
}
id, _, _, err := saver.SaveBlob(ctx, restic.TreeBlob, buf, restic.ID{}, false)
if err != nil {
t.Fatal(err)
}
return id
}
func saveSnapshotWithTree(t testing.TB, repo restic.Repository, treeID restic.ID) *data.Snapshot {
t.Helper()
ctx := context.Background()
sn, err := data.NewSnapshot([]string{"test"}, nil, "", time.Now())
if err != nil {
t.Fatal(err)
}
sn.Tree = &treeID
_, err = data.SaveSnapshot(ctx, repo, sn)
if err != nil {
t.Fatal(err)
}
return sn
}
// TestRestoreDuplicateNodeNames covers https://github.com/restic/restic/issues/22180:
// a tree containing a symlink `a` followed by a directory `a` must not let
// restore write through the symlink to files outside the target.
func TestRestoreDuplicateNodeNames(t *testing.T) {
repo := repository.TestRepository(t)
ctx := t.Context()
uid, gid := uint32(os.Getuid()), uint32(os.Getgid())
var treeID restic.ID
rtest.OK(t, repo.WithBlobUploader(ctx, func(ctx context.Context, uploader restic.BlobSaverWithAsync) error {
contentID := saveFile(t, uploader, "file")
treeID = saveUnorderedTree(t, uploader, []*data.Node{
{
Type: data.NodeTypeFile, Name: "a", Mode: 0644,
UID: uid, GID: gid, Links: 1,
Size: 4, Content: restic.IDs{contentID},
},
{
Type: data.NodeTypeSymlink, Name: "a", Mode: os.ModeSymlink | 0777,
UID: uid, GID: gid, Links: 1, LinkTarget: "some-symlink-target",
},
})
return nil
}))
sn := saveSnapshotWithTree(t, repo, treeID)
target := filepath.Join(rtest.TempDir(t), "target")
res := NewRestorer(repo, sn, Options{})
_, err := res.RestoreTo(ctx, target)
if !errors.Is(err, data.ErrTreeNotOrdered) {
t.Fatalf("RestoreTo returned error %v, want an error wrapping ErrTreeNotOrdered", err)
}
// Ensure that restore filtering does not cause file overwrites
target = filepath.Join(rtest.TempDir(t), "target")
res = NewRestorer(repo, sn, Options{})
res.Error = func(location string, err error) error { return nil }
_, err = res.RestoreTo(ctx, target)
rtest.OK(t, err)
// Ensure file was not replaced by a symlink
path := filepath.Join(target, "a")
fi, err := os.Lstat(path)
rtest.OK(t, err)
rtest.Assert(t, fi.Mode()&os.ModeSymlink == 0, "file was replaced by a symlink")
content, err := os.ReadFile(path)
rtest.OK(t, err)
rtest.Assert(t, string(content) == "file", "file contains %q, want %q", content, "file")
}
// TestRestoreUnorderedNodeNames checks that trees whose names are not in
// strictly increasing order are rejected even without duplicate names.
func TestRestoreUnorderedNodeNames(t *testing.T) {
repo := repository.TestRepository(t)
ctx := t.Context()
uid, gid := uint32(os.Getuid()), uint32(os.Getgid())
var treeID restic.ID
rtest.OK(t, repo.WithBlobUploader(ctx, func(ctx context.Context, uploader restic.BlobSaverWithAsync) error {
contentID := saveFile(t, uploader, "data")
treeID = saveUnorderedTree(t, uploader, []*data.Node{
{
Type: data.NodeTypeFile, Name: "b", Mode: 0644,
UID: uid, GID: gid, Inode: 100, Links: 1,
Size: 4, Content: restic.IDs{contentID},
},
{
Type: data.NodeTypeFile, Name: "a", Mode: 0644,
UID: uid, GID: gid, Inode: 101, Links: 1,
Size: 4, Content: restic.IDs{contentID},
},
})
return nil
}))
sn := saveSnapshotWithTree(t, repo, treeID)
target := filepath.Join(rtest.TempDir(t), "target")
res := NewRestorer(repo, sn, Options{})
_, err := res.RestoreTo(ctx, target)
if !errors.Is(err, data.ErrTreeNotOrdered) {
t.Fatalf("RestoreTo returned error %v, want an error wrapping ErrTreeNotOrdered", err)
}
}