fs: fall back to numeric ids when --ownership-by-name lookup fails (#22185)

Signed-off-by: Dean Chen <862469039@qq.com>
This commit is contained in:
Dean Chen authored and GitHub committed 2026-11-15 19:30:21 +00:00
1 parent 2126756613
commit 5cda063cbb
5 files changed
+89 -27

No files matched your search

+12
View File
@@ -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
+24 -17
View File
@@ -130,21 +130,27 @@ func lookupUsername(uid uint32) string {
return username return username
} }
type idLookupResult struct {
id uint32
found bool
}
var ( var (
userNameLookupCache = make(map[string]uint32) userNameLookupCache = make(map[string]idLookupResult)
userNameLookupCacheMutex = sync.RWMutex{} 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 //nolint:revive // capitalization is correct as is
func lookupUid(userName string) uint32 { func lookupUid(userName string) (uint32, bool) {
userNameLookupCacheMutex.RLock() userNameLookupCacheMutex.RLock()
uid, ok := userNameLookupCache[userName] res, ok := userNameLookupCache[userName]
userNameLookupCacheMutex.RUnlock() userNameLookupCacheMutex.RUnlock()
if ok { if ok {
return uid return res.id, res.found
} }
u, err := user.Lookup(userName) u, err := user.Lookup(userName)
@@ -152,15 +158,15 @@ func lookupUid(userName string) uint32 {
var s int var s int
s, err = strconv.Atoi(u.Uid) s, err = strconv.Atoi(u.Uid)
if err == nil { if err == nil {
uid = uint32(s) res = idLookupResult{id: uint32(s), found: true}
} }
} }
userNameLookupCacheMutex.Lock() userNameLookupCacheMutex.Lock()
userNameLookupCache[userName] = uid userNameLookupCache[userName] = res
userNameLookupCacheMutex.Unlock() userNameLookupCacheMutex.Unlock()
return uid return res.id, res.found
} }
var ( var (
@@ -191,18 +197,19 @@ func lookupGroup(gid uint32) string {
} }
var ( var (
groupNameLookupCache = make(map[string]uint32) groupNameLookupCache = make(map[string]idLookupResult)
groupNameLookupCacheMutex = sync.RWMutex{} groupNameLookupCacheMutex = sync.RWMutex{}
) )
// Cached uid lookup by group name. Returns 0 when no id can be found. // Cached gid lookup by group name. The second return value is false when no
func lookupGid(groupName string) uint32 { // id can be found for the name.
func lookupGid(groupName string) (uint32, bool) {
groupNameLookupCacheMutex.RLock() groupNameLookupCacheMutex.RLock()
gid, ok := groupNameLookupCache[groupName] res, ok := groupNameLookupCache[groupName]
groupNameLookupCacheMutex.RUnlock() groupNameLookupCacheMutex.RUnlock()
if ok { if ok {
return gid return res.id, res.found
} }
g, err := user.LookupGroup(groupName) g, err := user.LookupGroup(groupName)
@@ -210,15 +217,15 @@ func lookupGid(groupName string) uint32 {
var s int var s int
s, err = strconv.Atoi(g.Gid) s, err = strconv.Atoi(g.Gid)
if err == nil { if err == nil {
gid = uint32(s) res = idLookupResult{id: uint32(s), found: true}
} }
} }
groupNameLookupCacheMutex.Lock() groupNameLookupCacheMutex.Lock()
groupNameLookupCache[groupName] = gid groupNameLookupCache[groupName] = res
groupNameLookupCacheMutex.Unlock() groupNameLookupCacheMutex.Unlock()
return gid return res.id, res.found
} }
// NodeCreateAt creates the node at the given path but does NOT restore node meta data. // 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 { func nodeRestoreMetadata(node *data.Node, path string, warn func(msg string), xattrSelectFilter func(xattrName string) bool, ownershipByName bool) error {
var firsterr 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) firsterr = errors.WithStack(err)
} }
+20 -7
View File
@@ -3,19 +3,32 @@
package fs package fs
import ( import (
"fmt"
"os" "os"
"github.com/restic/restic/internal/data" "github.com/restic/restic/internal/data"
) )
func lchown(name string, node *data.Node, lookupByName bool) error { func lchown(name string, node *data.Node, lookupByName bool, warn func(msg string)) error {
var uid, gid uint32 uid := node.UID
gid := node.GID
if lookupByName { if lookupByName {
uid = lookupUid(node.User) // Fall back to the numeric ids stored in the snapshot if a name is
gid = lookupGid(node.Group) // missing or cannot be resolved on this host.
} else { if node.User != "" {
uid = node.UID if id, ok := lookupUid(node.User); ok {
gid = node.GID 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)) return os.Lchown(name, int(uid), int(gid))
+32 -2
View File
@@ -167,7 +167,7 @@ func TestLchown(t *testing.T) {
GID: uint32(gid), GID: uint32(gid),
} }
err = lchown(f, n, false) err = lchown(f, n, false, nil)
rtest.OK(t, err) rtest.OK(t, err)
}) })
@@ -180,7 +180,37 @@ func TestLchown(t *testing.T) {
Group: group.Name, 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) rtest.OK(t, err)
}) })
} }
+1 -1
View File
@@ -38,7 +38,7 @@ func mknod(_ string, _ uint32, _ uint64) (err error) {
} }
// Windows doesn't need lchown // 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 return nil
} }