Skip to content

Commit 2dab271

Browse files
hurricanehrndzgopherbot
authored andcommitted
route: treat short sockaddr lengths as unspecified
Previously, we enforced minimum length requirements for sockaddr, but the route command can legitimately parse 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), as discern by monitoring the route command. To replicate the issue, prior to the fix, execute the following: First: route -n monitor Next: sudo route -n add -inet6 -ifscope en11 -net :: \ -netmask :: fe80::2d0:4cff:fe10:15d2 The route command that is actively monitoring will print something such as: 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 :: Prior to the fix, if you had attempted parse the above message, PareRIB would have returned errInvalidAddr which is clearly false. Fixes golang/go#71557 Change-Id: Iec86cc9b05a765b6e67e95a4e30ff31f66f3d17e GitHub-Last-Rev: 396d8a2 GitHub-Pull-Request: #231 Reviewed-on: https://go-review.googlesource.com/c/net/+/646556 Commit-Queue: Ian Lance Taylor <iant@google.com> Reviewed-by: Damien Neil <dneil@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
1 parent b914489 commit 2dab271

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)