diff --git a/route/address.go b/route/address.go index d0e9f415f..279505b10 100644 --- a/route/address.go +++ b/route/address.go @@ -178,13 +178,13 @@ func parseInetAddr(af int, b []byte) (Addr, error) { ) switch af { case syscall.AF_INET: - if len(b) < (off4+1) || len(b) < int(b[0]) { + if len(b) < int(b[0]) { return nil, errInvalidAddr } sockAddrLen := int(b[0]) a := &Inet4Addr{} // sockAddrLen of 0 is valid and represents 0.0.0.0 - if sockAddrLen != 0 { + if sockAddrLen > off4 { // Calculate how many bytes of the address to copy: // either full IPv4 length or the available length. n := off4 + ipv4Len @@ -195,13 +195,13 @@ func parseInetAddr(af int, b []byte) (Addr, error) { } return a, nil case syscall.AF_INET6: - if len(b) < (off6+1) || len(b) < int(b[0]) { + if len(b) < int(b[0]) { return nil, errInvalidAddr } sockAddrLen := int(b[0]) a := &Inet6Addr{} // sockAddrLen of 0 is valid and represents :: - if sockAddrLen != 0 { + if sockAddrLen > off6 { n := off6 + ipv6Len if sockAddrLen < n { n = sockAddrLen diff --git a/route/address_darwin_test.go b/route/address_darwin_test.go index 55accff9d..80f686e97 100644 --- a/route/address_darwin_test.go +++ b/route/address_darwin_test.go @@ -96,6 +96,38 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{ nil, }, }, + // sudo route -n add -inet6 -ifscope en11 -net :: -netmask :: fe80::2d0:4cff:fe10:15d2 + // RTM_ADD: Add Route: len 152, pid: 81198, seq 1, errno 0, ifscope 13, flags: + // locks: inits: + // sockaddrs: + // :: fe80::2d0:4cff:fe10:15d2 :: + { + syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK, + parseKernelInetAddr, + []byte{ + 0x1c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + + 0x1c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0xd0, 0x4c, 0xff, 0xfe, 0x10, 0x15, 0xd2, + 0x00, 0x00, 0x00, 0x00, + + 0x02, 0x1e, 0x00, 0x00, + }, + []Addr{ + &Inet6Addr{}, + &Inet6Addr{IP: [16]byte{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xd0, 0x4c, 0xff, 0xfe, 0x10, 0x15, 0xd2}}, + &Inet6Addr{}, + nil, + nil, + nil, + nil, + nil, + }, + }, // golang/go#70528, the kernel can produce addresses of length 0 { syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,