Skip to content

Commit

Permalink
Merge pull request moby#454 from thaJeztah/19.03_backport_lgetxattr_p…
Browse files Browse the repository at this point in the history
…anic

[19.03 backport] Fix possible runtime panic in Lgetxattr
  • Loading branch information
tonistiigi authored Jan 23, 2020
2 parents 6949793 + c7cd5d6 commit 69098f0
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pkg/system/xattrs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ import "golang.org/x/sys/unix"
// and associated with the given path in the file system.
// It will returns a nil slice and nil error if the xattr is not set.
func Lgetxattr(path string, attr string) ([]byte, error) {
// Start with a 128 length byte array
dest := make([]byte, 128)
sz, errno := unix.Lgetxattr(path, attr, dest)
if errno == unix.ENODATA {

switch {
case errno == unix.ENODATA:
return nil, nil
}
if errno == unix.ERANGE {
case errno == unix.ERANGE:
// 128 byte array might just not be good enough. A dummy buffer is used
// to get the real size of the xattrs on disk
sz, errno = unix.Lgetxattr(path, attr, []byte{})
if errno != nil {
return nil, errno
}
dest = make([]byte, sz)
sz, errno = unix.Lgetxattr(path, attr, dest)
}
if errno != nil {
if errno != nil {
return nil, errno
}
case errno != nil:
return nil, errno
}

return dest[:sz], nil
}

Expand Down

0 comments on commit 69098f0

Please sign in to comment.