Skip to content

Commit 6fd8c00

Browse files
david415cixtor
authored andcommitted
syscall: add bounds checking and error returns to ParseNetlinkMessage
Fixes #16681 Change-Id: I6ff7ec81fe48ab06be3aae5b7ff92e9dc70960c3 Reviewed-on: https://go-review.googlesource.com/26990 Run-TryBot: Mikio Hara <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Mikio Hara <[email protected]>
1 parent 33e63eb commit 6fd8c00

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/syscall/netlink_linux.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ func ParseNetlinkMessage(b []byte) ([]NetlinkMessage, error) {
129129

130130
func netlinkMessageHeaderAndData(b []byte) (*NlMsghdr, []byte, int, error) {
131131
h := (*NlMsghdr)(unsafe.Pointer(&b[0]))
132-
if int(h.Len) < NLMSG_HDRLEN || int(h.Len) > len(b) {
132+
l := nlmAlignOf(int(h.Len))
133+
if int(h.Len) < NLMSG_HDRLEN || l > len(b) {
133134
return nil, nil, 0, EINVAL
134135
}
135-
return h, b[NLMSG_HDRLEN:], nlmAlignOf(int(h.Len)), nil
136+
return h, b[NLMSG_HDRLEN:], l, nil
136137
}
137138

138139
// NetlinkRouteAttr represents a netlink route attribute.

src/syscall/syscall_linux_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,31 @@ func deathSignalChild() {
138138
fmt.Println("not ok")
139139
os.Exit(1)
140140
}
141+
142+
func TestParseNetlinkMessage(t *testing.T) {
143+
for i, b := range [][]byte{
144+
{103, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 11, 0, 1, 0, 0, 0, 0, 5, 8, 0, 3,
145+
0, 8, 0, 6, 0, 0, 0, 0, 1, 63, 0, 10, 0, 69, 16, 0, 59, 39, 82, 64, 0, 64, 6, 21, 89, 127, 0, 0,
146+
1, 127, 0, 0, 1, 230, 228, 31, 144, 32, 186, 155, 211, 185, 151, 209, 179, 128, 24, 1, 86,
147+
53, 119, 0, 0, 1, 1, 8, 10, 0, 17, 234, 12, 0, 17, 189, 126, 107, 106, 108, 107, 106, 13, 10,
148+
},
149+
{106, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 11, 0, 1, 0, 0, 0, 0, 3, 8, 0, 3,
150+
0, 8, 0, 6, 0, 0, 0, 0, 1, 66, 0, 10, 0, 69, 0, 0, 62, 230, 255, 64, 0, 64, 6, 85, 184, 127, 0, 0,
151+
1, 127, 0, 0, 1, 237, 206, 31, 144, 73, 197, 128, 65, 250, 60, 192, 97, 128, 24, 1, 86, 253, 21, 0,
152+
0, 1, 1, 8, 10, 0, 51, 106, 89, 0, 51, 102, 198, 108, 104, 106, 108, 107, 104, 108, 107, 104, 10,
153+
},
154+
{102, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 11, 0, 1, 0, 0, 0, 0, 1, 8, 0, 3, 0,
155+
8, 0, 6, 0, 0, 0, 0, 1, 62, 0, 10, 0, 69, 0, 0, 58, 231, 2, 64, 0, 64, 6, 85, 185, 127, 0, 0, 1, 127,
156+
0, 0, 1, 237, 206, 31, 144, 73, 197, 128, 86, 250, 60, 192, 97, 128, 24, 1, 86, 104, 64, 0, 0, 1, 1, 8,
157+
10, 0, 52, 198, 200, 0, 51, 135, 232, 101, 115, 97, 103, 103, 10,
158+
},
159+
} {
160+
m, err := syscall.ParseNetlinkMessage(b)
161+
if err != syscall.EINVAL {
162+
t.Errorf("#%d: got %v; want EINVAL", i, err)
163+
}
164+
if m != nil {
165+
t.Errorf("#%d: got %v; want nil", i, m)
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)