Skip to content

Commit 085b4ee

Browse files
route: treat short sockaddr lengths as unspecified
Previously, we enforced minimum length requirements for sockaddr, but the kernel can legitimately return shorter lengths. This change treats any sockaddr with length less than the address offset as an unspecified address (0.0.0.0 for IPv4 or :: for IPv6). Fixes golang/go#71557
1 parent ebd23f8 commit 085b4ee

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

route/address.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,13 @@ func parseInetAddr(af int, b []byte) (Addr, error) {
178178
)
179179
switch af {
180180
case syscall.AF_INET:
181-
if len(b) < (off4+1) || len(b) < int(b[0]) {
181+
if len(b) < int(b[0]) {
182182
return nil, errInvalidAddr
183183
}
184184
sockAddrLen := int(b[0])
185185
a := &Inet4Addr{}
186186
// sockAddrLen of 0 is valid and represents 0.0.0.0
187-
if sockAddrLen != 0 {
187+
if sockAddrLen > off4 {
188188
// Calculate how many bytes of the address to copy:
189189
// either full IPv4 length or the available length.
190190
n := off4 + ipv4Len
@@ -195,13 +195,13 @@ func parseInetAddr(af int, b []byte) (Addr, error) {
195195
}
196196
return a, nil
197197
case syscall.AF_INET6:
198-
if len(b) < (off6+1) || len(b) < int(b[0]) {
198+
if len(b) < int(b[0]) {
199199
return nil, errInvalidAddr
200200
}
201201
sockAddrLen := int(b[0])
202202
a := &Inet6Addr{}
203203
// sockAddrLen of 0 is valid and represents ::
204-
if sockAddrLen != 0 {
204+
if sockAddrLen > off6 {
205205
n := off6 + ipv6Len
206206
if sockAddrLen < n {
207207
n = sockAddrLen

route/address_darwin_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,38 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
9696
nil,
9797
},
9898
},
99+
// sudo route -n add -inet6 -ifscope en11 -net :: -netmask :: fe80::2d0:4cff:fe10:15d2
100+
// RTM_ADD: Add Route: len 152, pid: 81198, seq 1, errno 0, ifscope 13, flags:<UP,GATEWAY,DONE,STATIC,IFSCOPE>
101+
// locks: inits:
102+
// sockaddrs: <DST,GATEWAY,NETMASK>
103+
// :: fe80::2d0:4cff:fe10:15d2 ::
104+
{
105+
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
106+
parseKernelInetAddr,
107+
[]byte{
108+
0x1c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
110+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111+
0x00, 0x00, 0x00, 0x00,
112+
113+
0x1c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114+
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115+
0x02, 0xd0, 0x4c, 0xff, 0xfe, 0x10, 0x15, 0xd2,
116+
0x00, 0x00, 0x00, 0x00,
117+
118+
0x02, 0x1e, 0x00, 0x00,
119+
},
120+
[]Addr{
121+
&Inet6Addr{},
122+
&Inet6Addr{IP: [16]byte{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xd0, 0x4c, 0xff, 0xfe, 0x10, 0x15, 0xd2}},
123+
&Inet6Addr{},
124+
nil,
125+
nil,
126+
nil,
127+
nil,
128+
nil,
129+
},
130+
},
99131
// golang/go#70528, the kernel can produce addresses of length 0
100132
{
101133
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,

0 commit comments

Comments
 (0)