Skip to content

Commit 574375f

Browse files
route: use length from message to parse netmask
sizeofSockaddrInet is 16, but first byte of sockaddr specifies the size of the address. 16 works for most cases, except with Netmasks addresses, on Darwin where only the significant bits are in the msg. i.e. ipv4 06 02 00 00 ff ff The above byte sequence is for a sockaddr that is 6 bytes long representing an ipv4 for address that is 255.255.0.0. i.e. ipv6 netmask 0e 1e 00 00 00 00 00 00 ff ff ff ff ff ff 00 00 The above is /48 netmask that should also be parsed as such. Confirmed by using `route monitor`. sources: https://github.com/apple/darwin-xnu/blob/main/bsd/net/route.h https://github.com/apple/darwin-xnu/blob/main/bsd/sys/socket.h#L603
1 parent 9bf379f commit 574375f

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

route/address.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,31 @@ func (a *Inet6Addr) marshal(b []byte) (int, error) {
172172
func parseInetAddr(af int, b []byte) (Addr, error) {
173173
switch af {
174174
case syscall.AF_INET:
175-
if len(b) < sizeofSockaddrInet {
175+
l := int(b[0])
176+
if len(b) < l || l < 4 {
176177
return nil, errInvalidAddr
177178
}
178179
a := &Inet4Addr{}
179-
copy(a.IP[:], b[4:8])
180+
n := 8
181+
if l < 8 {
182+
n = l
183+
}
184+
copy(a.IP[:], b[4:n])
180185
return a, nil
181186
case syscall.AF_INET6:
182-
if len(b) < sizeofSockaddrInet6 {
187+
l := int(b[0])
188+
if len(b) < l || l < 8 {
183189
return nil, errInvalidAddr
184190
}
185-
a := &Inet6Addr{ZoneID: int(nativeEndian.Uint32(b[24:28]))}
186-
copy(a.IP[:], b[8:24])
191+
n := 24
192+
if l < 24 {
193+
n = l
194+
}
195+
a := &Inet6Addr{}
196+
if l >= 28 {
197+
a.ZoneID = int(nativeEndian.Uint32(b[24:28]))
198+
}
199+
copy(a.IP[:], b[8:n])
187200
if a.IP[0] == 0xfe && a.IP[1]&0xc0 == 0x80 || a.IP[0] == 0xff && (a.IP[1]&0x0f == 0x01 || a.IP[1]&0x0f == 0x02) {
188201
// KAME based IPv6 protocol stack usually
189202
// embeds the interface index in the

0 commit comments

Comments
 (0)