1
0
mirror of https://git.zx2c4.com/wireguard-go synced 2024-11-15 01:05:15 +01:00

conn: use CmsgSpace() for ancillary data buf sizing

CmsgLen() does not account for data alignment.

Reviewed-by: Adrian Dewhurst <adrian@tailscale.com>
Signed-off-by: Jordan Whited <jordan@tailscale.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jordan Whited 2023-03-14 20:02:24 -07:00 committed by Jason A. Donenfeld
parent 0ad14a89f5
commit fff53afca7

View File

@ -63,8 +63,9 @@ func pktInfoFromBuf[T unix.Inet4Pktinfo | unix.Inet6Pktinfo](buf []byte) (t T) {
return t return t
} }
// setSrcControl parses the control for PKTINFO and if found updates ep with // setSrcControl sets an IP{V6}_PKTINFO in control based on the source address
// the source information found. // and source ifindex found in ep. control's len will be set to 0 in the event
// that ep is a default value.
func setSrcControl(control *[]byte, ep *StdNetEndpoint) { func setSrcControl(control *[]byte, ep *StdNetEndpoint) {
*control = (*control)[:cap(*control)] *control = (*control)[:cap(*control)]
if len(*control) < int(unsafe.Sizeof(unix.Cmsghdr{})) { if len(*control) < int(unsafe.Sizeof(unix.Cmsghdr{})) {
@ -93,19 +94,20 @@ func setSrcControl(control *[]byte, ep *StdNetEndpoint) {
if ep.SrcIP().IsValid() { if ep.SrcIP().IsValid() {
info.Spec_dst = ep.SrcIP().As4() info.Spec_dst = ep.SrcIP().As4()
} }
*control = (*control)[:unix.CmsgSpace(unix.SizeofInet4Pktinfo)]
} else { } else {
hdr.Level = unix.IPPROTO_IPV6 hdr.Level = unix.IPPROTO_IPV6
hdr.Type = unix.IPV6_PKTINFO hdr.Type = unix.IPV6_PKTINFO
hdr.Len = unix.SizeofCmsghdr + unix.SizeofInet6Pktinfo hdr.SetLen(unix.CmsgLen(unix.SizeofInet6Pktinfo))
info := (*unix.Inet6Pktinfo)(unsafe.Pointer(&(*control)[unix.SizeofCmsghdr])) info := (*unix.Inet6Pktinfo)(unsafe.Pointer(&(*control)[unix.SizeofCmsghdr]))
info.Ifindex = uint32(ep.src.ifidx) info.Ifindex = uint32(ep.src.ifidx)
if ep.SrcIP().IsValid() { if ep.SrcIP().IsValid() {
info.Addr = ep.SrcIP().As16() info.Addr = ep.SrcIP().As16()
} }
*control = (*control)[:unix.CmsgSpace(unix.SizeofInet6Pktinfo)]
} }
*control = (*control)[:hdr.Len]
} }
var srcControlSize = unix.CmsgLen(unix.SizeofInet6Pktinfo) var srcControlSize = unix.CmsgSpace(unix.SizeofInet6Pktinfo)