Skip to content

Fix bug in FOU netlink parser #827

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 12 additions & 7 deletions fou_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

package netlink
Expand Down Expand Up @@ -171,36 +172,40 @@ func (h *Handle) FouList(fam int) ([]Fou, error) {
func deserializeFouMsg(msg []byte) (Fou, error) {
// we'll skip to byte 4 to first attribute
msg = msg[3:]
var shift int
fou := Fou{}

for {
// attribute header is at least 16 bits
// attribute header is at least 32 bits (16 bit type + 16 bit length)
if len(msg) < 4 {
return fou, ErrAttrHeaderTruncated
}

lgt := int(binary.BigEndian.Uint16(msg[0:2]))
if len(msg) < lgt+4 {
lgt4 := lgt & (^0x3)

// Padding to 4 bytes according to netlink man7
lgtPad := lgt4 + 4
if lgt4 == lgt {
lgtPad = lgt
}

if len(msg) < lgtPad {
return fou, ErrAttrBodyTruncated
}
attr := binary.BigEndian.Uint16(msg[2:4])

shift = lgt + 3
switch attr {
case FOU_ATTR_AF:
fou.Family = int(msg[5])
case FOU_ATTR_PORT:
fou.Port = int(binary.BigEndian.Uint16(msg[5:7]))
// port is 2 bytes
shift = lgt + 2
case FOU_ATTR_IPPROTO:
fou.Protocol = int(msg[5])
case FOU_ATTR_TYPE:
fou.EncapType = int(msg[5])
}

msg = msg[shift:]
msg = msg[lgtPad:]

if len(msg) < 4 {
break
Expand Down
3 changes: 2 additions & 1 deletion fou_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

package netlink
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestFouDeserializeMsg(t *testing.T) {
}

// deserialize truncated attribute header
msg = []byte{3, 1, 0, 0, 5, 0, 2, 0, 2, 0, 0}
msg = []byte{3, 1, 0, 0, 5, 0, 2, 0, 2, 0}
if _, err := deserializeFouMsg(msg); err == nil {
t.Error("expected attribute body truncated error")
} else if err != ErrAttrBodyTruncated {
Expand Down
Loading