From b6d7b1396ec874c3b00f6c84cd4301a17c56c8ed Mon Sep 17 00:00:00 2001 From: Mikio Hara Date: Tue, 16 Feb 2016 22:16:06 +0900 Subject: [PATCH] ipv6: fix potential misaligned memory access Also makes use of encoding/binary package. Change-Id: I0faf7d55bf3340e84b7d7cf1c77ab3886c728a07 Reviewed-on: https://go-review.googlesource.com/19533 Run-TryBot: Mikio Hara TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- ipv6/control_rfc2292_unix.go | 3 +-- ipv6/control_rfc3542_unix.go | 12 ++++-------- ipv6/header.go | 3 ++- ipv6/helper.go | 14 ++++++++++++++ 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/ipv6/control_rfc2292_unix.go b/ipv6/control_rfc2292_unix.go index ce201ce3..80ec2e2f 100644 --- a/ipv6/control_rfc2292_unix.go +++ b/ipv6/control_rfc2292_unix.go @@ -20,8 +20,7 @@ func marshal2292HopLimit(b []byte, cm *ControlMessage) []byte { m.SetLen(syscall.CmsgLen(4)) if cm != nil { data := b[syscall.CmsgLen(0):] - // TODO(mikio): fix potential misaligned memory access - *(*int32)(unsafe.Pointer(&data[:4][0])) = int32(cm.HopLimit) + nativeEndian.PutUint32(data[:4], uint32(cm.HopLimit)) } return b[syscall.CmsgSpace(4):] } diff --git a/ipv6/control_rfc3542_unix.go b/ipv6/control_rfc3542_unix.go index e55c4aa9..f344d16d 100644 --- a/ipv6/control_rfc3542_unix.go +++ b/ipv6/control_rfc3542_unix.go @@ -20,15 +20,13 @@ func marshalTrafficClass(b []byte, cm *ControlMessage) []byte { m.SetLen(syscall.CmsgLen(4)) if cm != nil { data := b[syscall.CmsgLen(0):] - // TODO(mikio): fix potential misaligned memory access - *(*int32)(unsafe.Pointer(&data[:4][0])) = int32(cm.TrafficClass) + nativeEndian.PutUint32(data[:4], uint32(cm.TrafficClass)) } return b[syscall.CmsgSpace(4):] } func parseTrafficClass(cm *ControlMessage, b []byte) { - // TODO(mikio): fix potential misaligned memory access - cm.TrafficClass = int(*(*int32)(unsafe.Pointer(&b[:4][0]))) + cm.TrafficClass = int(nativeEndian.Uint32(b[:4])) } func marshalHopLimit(b []byte, cm *ControlMessage) []byte { @@ -38,15 +36,13 @@ func marshalHopLimit(b []byte, cm *ControlMessage) []byte { m.SetLen(syscall.CmsgLen(4)) if cm != nil { data := b[syscall.CmsgLen(0):] - // TODO(mikio): fix potential misaligned memory access - *(*int32)(unsafe.Pointer(&data[:4][0])) = int32(cm.HopLimit) + nativeEndian.PutUint32(data[:4], uint32(cm.HopLimit)) } return b[syscall.CmsgSpace(4):] } func parseHopLimit(cm *ControlMessage, b []byte) { - // TODO(mikio): fix potential misaligned memory access - cm.HopLimit = int(*(*int32)(unsafe.Pointer(&b[:4][0]))) + cm.HopLimit = int(nativeEndian.Uint32(b[:4])) } func marshalPacketInfo(b []byte, cm *ControlMessage) []byte { diff --git a/ipv6/header.go b/ipv6/header.go index ad73776b..e05cb08b 100644 --- a/ipv6/header.go +++ b/ipv6/header.go @@ -5,6 +5,7 @@ package ipv6 import ( + "encoding/binary" "fmt" "net" ) @@ -42,7 +43,7 @@ func ParseHeader(b []byte) (*Header, error) { Version: int(b[0]) >> 4, TrafficClass: int(b[0]&0x0f)<<4 | int(b[1])>>4, FlowLabel: int(b[1]&0x0f)<<16 | int(b[2])<<8 | int(b[3]), - PayloadLen: int(b[4])<<8 | int(b[5]), + PayloadLen: int(binary.BigEndian.Uint16(b[4:6])), NextHeader: int(b[6]), HopLimit: int(b[7]), } diff --git a/ipv6/helper.go b/ipv6/helper.go index 4a6f1069..53b99990 100644 --- a/ipv6/helper.go +++ b/ipv6/helper.go @@ -5,8 +5,10 @@ package ipv6 import ( + "encoding/binary" "errors" "net" + "unsafe" ) var ( @@ -15,8 +17,20 @@ var ( errInvalidConnType = errors.New("invalid conn type") errOpNoSupport = errors.New("operation not supported") errNoSuchInterface = errors.New("no such interface") + + nativeEndian binary.ByteOrder ) +func init() { + i := uint32(1) + b := (*[4]byte)(unsafe.Pointer(&i)) + if b[0] == 1 { + nativeEndian = binary.LittleEndian + } else { + nativeEndian = binary.BigEndian + } +} + func boolint(b bool) int { if b { return 1