Skip to content

route: treat short sockaddr lengths as unspecified #231

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

Closed
Closed
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
8 changes: 4 additions & 4 deletions route/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
32 changes: 32 additions & 0 deletions route/address_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:<UP,GATEWAY,DONE,STATIC,IFSCOPE>
// locks: inits:
// sockaddrs: <DST,GATEWAY,NETMASK>
// :: 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,
Expand Down