1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2025-01-03 13:45:20 +00:00

fs: sanitize path in fd based xattr reading on Linux

The xattrs are read from /proc/self/fd/%d but instead show the original
file path to make the error message more useful.
This commit is contained in:
Michael Eischer 2024-11-16 19:03:23 +01:00
parent 6e4c7f9b50
commit 67c0387cfe

View file

@ -16,16 +16,33 @@ import (
)
func linuxFdPath(fd uintptr) string {
// A file handle opened using O_PATH on Linux cannot be used to read xattrs.
// A file handle opened using O_PATH on Linux cannot be directly used to read xattrs.
// However, the file descriptor objects in the procfs filesystem
// can be used in place of the original file and therefore allow xattr access.
return fmt.Sprintf("/proc/self/fd/%d", int(fd))
}
// replaceXattrErrorPath replaces the path in the given xattr.Error
// For other error types it is a no-op. This is intended to replace
// a linuxFdPath with the original path.
func replaceXattrErrorPath(err error, path string) error {
switch e := err.(type) {
case nil:
return nil
case *xattr.Error:
e.Path = path
return e
default:
return err
}
}
// getxattr retrieves extended attribute data associated with path.
func fgetxattr(f *os.File, name string) (b []byte, err error) {
if runtime.GOOS == "linux" {
b, err = xattr.Get(linuxFdPath(f.Fd()), name)
err = replaceXattrErrorPath(err, f.Name())
} else {
b, err = xattr.FGet(f, name)
}
@ -43,6 +60,7 @@ func getxattr(path string, name string) (b []byte, err error) {
func flistxattr(f *os.File) (l []string, err error) {
if runtime.GOOS == "linux" {
l, err = xattr.List(linuxFdPath(f.Fd()))
err = replaceXattrErrorPath(err, f.Name())
} else {
l, err = xattr.FList(f)
}