Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont include xattr in extended link reading #278

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions filesystem/squashfs/inode.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,12 @@ func parseExtendedSymlink(b []byte) (*extendedSymlink, int, error) {
s := &extendedSymlink{
links: binary.LittleEndian.Uint32(b[0:4]),
}
targetSize := int(binary.LittleEndian.Uint32(b[4:8]))
// account for the synlink target, plus 4 bytes for the xattr index after it
extra = int(binary.LittleEndian.Uint32(b[4:8])) + 4
if len(b[target:]) > extra {
s.target = string(b[8 : 8+extra])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was reading 4 bytes to much (i.e. it was reading the xattr data as part of the target)

s.xAttrIndex = binary.LittleEndian.Uint32(b[8+extra : 8+extra+4])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was reading after the xattr data, so 4 bytes into the next section so it was just a random int

extra = targetSize + 4
if len(b) >= extra+target {
s.target = string(b[target : target+targetSize])
s.xAttrIndex = binary.LittleEndian.Uint32(b[target+targetSize : target+targetSize+4])
extra = 0
}
return s, extra, nil
Expand Down
62 changes: 57 additions & 5 deletions filesystem/squashfs/inode_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package squashfs

import (
"bytes"
"encoding/binary"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -383,13 +384,64 @@ func TestBasicSymlink(t *testing.T) {
})
}

//nolint:unused,revive // keep for future when we implement it and will need t
func TestExtendedSymlink(t *testing.T) {
// when we have more data with which to work
s := &extendedSymlink{
links: 1,
target: "/a/b/c/d/ef/g/h",
xAttrIndex: 46,
}
b, err := testGetInodeMetabytes()
if err != nil {
t.Fatal(err)
}

inodeB := binary.LittleEndian.AppendUint32(b[testBasicSymlinkStart:testBasicSymlinkEnd], s.xAttrIndex)

t.Run("toBytes", func(t *testing.T) {
b := s.toBytes()
if !bytes.Equal(b, inodeB) {
t.Errorf("mismatched output, actual then expected")
t.Logf("% x", b)
t.Logf("% x", inodeB)
}
})
t.Run("Size", func(t *testing.T) {
size := s.size()
if size != 0 {
t.Errorf("mismatched sizes, actual %d expected %d", size, 0)
}
})

tests := []struct {
b []byte
sym *extendedSymlink
ext int
err error
}{
{inodeB, s, 0, nil},
{inodeB[:7], nil, 0, fmt.Errorf("received %d bytes instead of expected minimal %d", 7, 8)},
{inodeB[:20], &extendedSymlink{links: 1}, 19, nil},
}

// func (i extendedSymlink) toBytes() []byte {
// func (i extendedSymlink) size() int64 {
// func parseExtendedSymlink(b []byte) (*extendedSymlink, error) {
t.Run("parse", func(t *testing.T) {
for i, tt := range tests {
sym, ext, err := parseExtendedSymlink(tt.b)
switch {
case (err == nil && tt.err != nil) || (err != nil && tt.err == nil) || (err != nil && tt.err != nil && !strings.HasPrefix(err.Error(), tt.err.Error())):
t.Errorf("%d: mismatched error, actual then expected", i)
t.Logf("%v", err)
t.Logf("%v", tt.err)
case tt.ext != ext:
t.Errorf("%d: mismatched extra, actual then expected", i)
t.Logf("%v", ext)
t.Logf("%v", tt.ext)
case (sym == nil && tt.sym != nil) || (sym != nil && tt.sym == nil) || (sym != nil && tt.sym != nil && *sym != *tt.sym):
t.Errorf("%d: mismatched results, actual then expected", i)
t.Logf("%#v", *sym)
t.Logf("%#v", *tt.sym)
}
}
})
}

//nolint:unused,revive // keep for future when we implement it and will need t
Expand Down
2 changes: 1 addition & 1 deletion filesystem/squashfs/squashfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func (fs *FileSystem) hydrateDirectoryEntries(entries []*directoryEntryRaw) ([]*
body, header := in.getBody(), in.getHeader()
xattrIndex, has := body.xattrIndex()
xattrs := map[string]string{}
if has && xattrIndex != noXattrInodeFlag {
if has {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When i was tracing down the bug i noticed all xattrIndex already do this check or return false so i removed it

xattrs, err = fs.xattrs.find(int(xattrIndex))
if err != nil {
return nil, fmt.Errorf("error reading xattrs for %s: %v", e.name, err)
Expand Down
Loading