Skip to content

Commit 07a1e55

Browse files
jwhitedzx2c4
authored andcommitted
conn: fix getSrcFromControl() iteration
We only expect a single control message in the normal case, but this would loop infinitely if there were more. Reviewed-by: Adrian Dewhurst <[email protected]> Signed-off-by: Jordan Whited <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent fff53af commit 07a1e55

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

conn/sticky_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func getSrcFromControl(control []byte, ep *StdNetEndpoint) {
2525
)
2626

2727
for len(rem) > unix.SizeofCmsghdr {
28-
hdr, data, rem, err = unix.ParseOneSocketControlMessage(control)
28+
hdr, data, rem, err = unix.ParseOneSocketControlMessage(rem)
2929
if err != nil {
3030
return
3131
}

conn/sticky_linux_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,34 @@ func Test_getSrcFromControl(t *testing.T) {
150150
t.Errorf("unexpected ifindex: %d", ep.src.ifidx)
151151
}
152152
})
153+
t.Run("Multiple", func(t *testing.T) {
154+
zeroControl := make([]byte, unix.CmsgSpace(0))
155+
zeroHdr := (*unix.Cmsghdr)(unsafe.Pointer(&zeroControl[0]))
156+
zeroHdr.SetLen(unix.CmsgLen(0))
157+
158+
control := make([]byte, srcControlSize)
159+
hdr := (*unix.Cmsghdr)(unsafe.Pointer(&control[0]))
160+
hdr.Level = unix.IPPROTO_IP
161+
hdr.Type = unix.IP_PKTINFO
162+
hdr.SetLen(unix.CmsgLen(int(unsafe.Sizeof(unix.Inet4Pktinfo{}))))
163+
info := (*unix.Inet4Pktinfo)(unsafe.Pointer(&control[unix.CmsgLen(0)]))
164+
info.Spec_dst = [4]byte{127, 0, 0, 1}
165+
info.Ifindex = 5
166+
167+
combined := make([]byte, 0)
168+
combined = append(combined, zeroControl...)
169+
combined = append(combined, control...)
170+
171+
ep := &StdNetEndpoint{}
172+
getSrcFromControl(combined, ep)
173+
174+
if ep.src.Addr != netip.MustParseAddr("127.0.0.1") {
175+
t.Errorf("unexpected address: %v", ep.src.Addr)
176+
}
177+
if ep.src.ifidx != 5 {
178+
t.Errorf("unexpected ifindex: %d", ep.src.ifidx)
179+
}
180+
})
153181
}
154182

155183
func Test_listenConfig(t *testing.T) {

0 commit comments

Comments
 (0)