diff --git a/changelog/unreleased/issue-22178 b/changelog/unreleased/issue-22178 new file mode 100644 index 000000000..f837fcb46 --- /dev/null +++ b/changelog/unreleased/issue-22178 @@ -0,0 +1,12 @@ +Bugfix: Fix file owner when `restore --ownership-by-name` cannot resolve a name + +When restoring with `--ownership-by-name`, user or group names that could not +be resolved on the restore host (or were not recorded in the snapshot) caused +restic to change the owner of the restored file to uid/gid 0. Combined with +restored setuid/setgid bits this could produce setuid-root files. + +Restic now falls back to the numeric uid/gid stored in the snapshot and prints +a warning when a name cannot be resolved. + +https://github.com/restic/restic/issues/22178 +https://github.com/restic/restic/pull/22185 diff --git a/internal/fs/node.go b/internal/fs/node.go index f2371e1fc..29c157558 100644 --- a/internal/fs/node.go +++ b/internal/fs/node.go @@ -130,21 +130,27 @@ func lookupUsername(uid uint32) string { return username } +type idLookupResult struct { + id uint32 + found bool +} + var ( - userNameLookupCache = make(map[string]uint32) + userNameLookupCache = make(map[string]idLookupResult) userNameLookupCacheMutex = sync.RWMutex{} ) -// Cached uid lookup by user name. Returns 0 when no id can be found. +// Cached uid lookup by user name. The second return value is false when no +// id can be found for the name. // //nolint:revive // capitalization is correct as is -func lookupUid(userName string) uint32 { +func lookupUid(userName string) (uint32, bool) { userNameLookupCacheMutex.RLock() - uid, ok := userNameLookupCache[userName] + res, ok := userNameLookupCache[userName] userNameLookupCacheMutex.RUnlock() if ok { - return uid + return res.id, res.found } u, err := user.Lookup(userName) @@ -152,15 +158,15 @@ func lookupUid(userName string) uint32 { var s int s, err = strconv.Atoi(u.Uid) if err == nil { - uid = uint32(s) + res = idLookupResult{id: uint32(s), found: true} } } userNameLookupCacheMutex.Lock() - userNameLookupCache[userName] = uid + userNameLookupCache[userName] = res userNameLookupCacheMutex.Unlock() - return uid + return res.id, res.found } var ( @@ -191,18 +197,19 @@ func lookupGroup(gid uint32) string { } var ( - groupNameLookupCache = make(map[string]uint32) + groupNameLookupCache = make(map[string]idLookupResult) groupNameLookupCacheMutex = sync.RWMutex{} ) -// Cached uid lookup by group name. Returns 0 when no id can be found. -func lookupGid(groupName string) uint32 { +// Cached gid lookup by group name. The second return value is false when no +// id can be found for the name. +func lookupGid(groupName string) (uint32, bool) { groupNameLookupCacheMutex.RLock() - gid, ok := groupNameLookupCache[groupName] + res, ok := groupNameLookupCache[groupName] groupNameLookupCacheMutex.RUnlock() if ok { - return gid + return res.id, res.found } g, err := user.LookupGroup(groupName) @@ -210,15 +217,15 @@ func lookupGid(groupName string) uint32 { var s int s, err = strconv.Atoi(g.Gid) if err == nil { - gid = uint32(s) + res = idLookupResult{id: uint32(s), found: true} } } groupNameLookupCacheMutex.Lock() - groupNameLookupCache[groupName] = gid + groupNameLookupCache[groupName] = res groupNameLookupCacheMutex.Unlock() - return gid + return res.id, res.found } // NodeCreateAt creates the node at the given path but does NOT restore node meta data. @@ -313,7 +320,7 @@ func NodeRestoreMetadata(node *data.Node, path string, warn func(msg string), xa func nodeRestoreMetadata(node *data.Node, path string, warn func(msg string), xattrSelectFilter func(xattrName string) bool, ownershipByName bool) error { var firsterr error - if err := lchown(path, node, ownershipByName); err != nil { + if err := lchown(path, node, ownershipByName, warn); err != nil { firsterr = errors.WithStack(err) } diff --git a/internal/fs/node_unix.go b/internal/fs/node_unix.go index 539b220db..d41d1a691 100644 --- a/internal/fs/node_unix.go +++ b/internal/fs/node_unix.go @@ -3,19 +3,32 @@ package fs import ( + "fmt" "os" "github.com/restic/restic/internal/data" ) -func lchown(name string, node *data.Node, lookupByName bool) error { - var uid, gid uint32 +func lchown(name string, node *data.Node, lookupByName bool, warn func(msg string)) error { + uid := node.UID + gid := node.GID if lookupByName { - uid = lookupUid(node.User) - gid = lookupGid(node.Group) - } else { - uid = node.UID - gid = node.GID + // Fall back to the numeric ids stored in the snapshot if a name is + // missing or cannot be resolved on this host. + if node.User != "" { + if id, ok := lookupUid(node.User); ok { + uid = id + } else if warn != nil { + warn(fmt.Sprintf("cannot resolve user %q for %v, using uid %d", node.User, name, uid)) + } + } + if node.Group != "" { + if id, ok := lookupGid(node.Group); ok { + gid = id + } else if warn != nil { + warn(fmt.Sprintf("cannot resolve group %q for %v, using gid %d", node.Group, name, gid)) + } + } } return os.Lchown(name, int(uid), int(gid)) diff --git a/internal/fs/node_unix_test.go b/internal/fs/node_unix_test.go index 6f861d91c..51db002a7 100644 --- a/internal/fs/node_unix_test.go +++ b/internal/fs/node_unix_test.go @@ -167,7 +167,7 @@ func TestLchown(t *testing.T) { GID: uint32(gid), } - err = lchown(f, n, false) + err = lchown(f, n, false, nil) rtest.OK(t, err) }) @@ -180,7 +180,37 @@ func TestLchown(t *testing.T) { Group: group.Name, } - err = lchown(f, n, true) + err = lchown(f, n, true, nil) + rtest.OK(t, err) + }) + + t.Run("unresolvable names fall back to UID/GID", func(t *testing.T) { + n := &data.Node{ + UID: uint32(uid), + GID: uint32(gid), + User: "restic-no-such-user", + Group: "restic-no-such-group", + } + + var warnings []string + err = lchown(f, n, true, func(msg string) { warnings = append(warnings, msg) }) + rtest.OK(t, err) + rtest.Equals(t, 2, len(warnings)) + + fi, err := os.Lstat(f) + rtest.OK(t, err) + st := fi.Sys().(*syscall.Stat_t) + rtest.Equals(t, uint32(uid), st.Uid) + rtest.Equals(t, uint32(gid), st.Gid) + }) + + t.Run("empty names fall back to UID/GID", func(t *testing.T) { + n := &data.Node{ + UID: uint32(uid), + GID: uint32(gid), + } + + err = lchown(f, n, true, func(msg string) { t.Errorf("unexpected warning: %v", msg) }) rtest.OK(t, err) }) } diff --git a/internal/fs/node_windows.go b/internal/fs/node_windows.go index e60043c2b..1d4c3f801 100644 --- a/internal/fs/node_windows.go +++ b/internal/fs/node_windows.go @@ -38,7 +38,7 @@ func mknod(_ string, _ uint32, _ uint64) (err error) { } // Windows doesn't need lchown -func lchown(_ string, _ *data.Node, _ bool) (err error) { +func lchown(_ string, _ *data.Node, _ bool, _ func(msg string)) (err error) { return nil }