Skip to content

Commit fff53af

Browse files
jwhitedzx2c4
authored andcommitted
conn: use CmsgSpace() for ancillary data buf sizing
CmsgLen() does not account for data alignment. Reviewed-by: Adrian Dewhurst <[email protected]> Signed-off-by: Jordan Whited <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 0ad14a8 commit fff53af

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

conn/sticky_linux.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ func pktInfoFromBuf[T unix.Inet4Pktinfo | unix.Inet6Pktinfo](buf []byte) (t T) {
6363
return t
6464
}
6565

66-
// setSrcControl parses the control for PKTINFO and if found updates ep with
67-
// the source information found.
66+
// setSrcControl sets an IP{V6}_PKTINFO in control based on the source address
67+
// and source ifindex found in ep. control's len will be set to 0 in the event
68+
// that ep is a default value.
6869
func setSrcControl(control *[]byte, ep *StdNetEndpoint) {
6970
*control = (*control)[:cap(*control)]
7071
if len(*control) < int(unsafe.Sizeof(unix.Cmsghdr{})) {
@@ -93,19 +94,20 @@ func setSrcControl(control *[]byte, ep *StdNetEndpoint) {
9394
if ep.SrcIP().IsValid() {
9495
info.Spec_dst = ep.SrcIP().As4()
9596
}
97+
*control = (*control)[:unix.CmsgSpace(unix.SizeofInet4Pktinfo)]
9698
} else {
9799
hdr.Level = unix.IPPROTO_IPV6
98100
hdr.Type = unix.IPV6_PKTINFO
99-
hdr.Len = unix.SizeofCmsghdr + unix.SizeofInet6Pktinfo
101+
hdr.SetLen(unix.CmsgLen(unix.SizeofInet6Pktinfo))
100102

101103
info := (*unix.Inet6Pktinfo)(unsafe.Pointer(&(*control)[unix.SizeofCmsghdr]))
102104
info.Ifindex = uint32(ep.src.ifidx)
103105
if ep.SrcIP().IsValid() {
104106
info.Addr = ep.SrcIP().As16()
105107
}
108+
*control = (*control)[:unix.CmsgSpace(unix.SizeofInet6Pktinfo)]
106109
}
107110

108-
*control = (*control)[:hdr.Len]
109111
}
110112

111-
var srcControlSize = unix.CmsgLen(unix.SizeofInet6Pktinfo)
113+
var srcControlSize = unix.CmsgSpace(unix.SizeofInet6Pktinfo)

0 commit comments

Comments
 (0)