Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 2da9c24

Browse files
saschagrunertthaJeztah
authored andcommitted
Fix possible runtime panic in Lgetxattr
If `unix.Lgetxattr` returns an error, then `sz == -1` which will cause a runtime panic if `errno == unix.ERANGE`. Signed-off-by: Sascha Grunert <sgrunert@suse.com> (cherry picked from commit 4138cd22abeaa7d1c49a96fa4c0045feb32b847e) Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Upstream-commit: c7cd5d67267c8302941c75785204a2ceeaf8fd3e Component: engine
1 parent a62d4a6 commit 2da9c24

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

components/engine/pkg/system/xattrs_linux.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,28 @@ import "golang.org/x/sys/unix"
66
// and associated with the given path in the file system.
77
// It will returns a nil slice and nil error if the xattr is not set.
88
func Lgetxattr(path string, attr string) ([]byte, error) {
9+
// Start with a 128 length byte array
910
dest := make([]byte, 128)
1011
sz, errno := unix.Lgetxattr(path, attr, dest)
11-
if errno == unix.ENODATA {
12+
13+
switch {
14+
case errno == unix.ENODATA:
1215
return nil, nil
13-
}
14-
if errno == unix.ERANGE {
16+
case errno == unix.ERANGE:
17+
// 128 byte array might just not be good enough. A dummy buffer is used
18+
// to get the real size of the xattrs on disk
19+
sz, errno = unix.Lgetxattr(path, attr, []byte{})
20+
if errno != nil {
21+
return nil, errno
22+
}
1523
dest = make([]byte, sz)
1624
sz, errno = unix.Lgetxattr(path, attr, dest)
17-
}
18-
if errno != nil {
25+
if errno != nil {
26+
return nil, errno
27+
}
28+
case errno != nil:
1929
return nil, errno
2030
}
21-
2231
return dest[:sz], nil
2332
}
2433

0 commit comments

Comments
 (0)