From: Matthew Dempsky Date: Thu, 17 Oct 2019 01:02:35 +0000 (-0700) Subject: syscall: avoid "just past the end" pointers in UnixRights X-Git-Tag: go1.14beta1~714 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ef18d6a929438babb59056aef0e485ca65ff8c25;p=gostls13.git syscall: avoid "just past the end" pointers in UnixRights Caught with -d=checkptr. Updates #22218. Change-Id: Ic0fcff4d2c8d83e4e7f5e0c6d01f03c9c7766c6d Reviewed-on: https://go-review.googlesource.com/c/go/+/201617 Run-TryBot: Matthew Dempsky TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- diff --git a/src/syscall/sockcmsg_linux.go b/src/syscall/sockcmsg_linux.go index 4cb9075ba8..d97667cf7e 100644 --- a/src/syscall/sockcmsg_linux.go +++ b/src/syscall/sockcmsg_linux.go @@ -17,7 +17,7 @@ func UnixCredentials(ucred *Ucred) []byte { h.Level = SOL_SOCKET h.Type = SCM_CREDENTIALS h.SetLen(CmsgLen(SizeofUcred)) - *((*Ucred)(cmsgData(h))) = *ucred + *(*Ucred)(h.data(0)) = *ucred return b } diff --git a/src/syscall/sockcmsg_unix.go b/src/syscall/sockcmsg_unix.go index 71efd46470..4eb0c0b748 100644 --- a/src/syscall/sockcmsg_unix.go +++ b/src/syscall/sockcmsg_unix.go @@ -50,8 +50,8 @@ func CmsgSpace(datalen int) int { return cmsgAlignOf(SizeofCmsghdr) + cmsgAlignOf(datalen) } -func cmsgData(h *Cmsghdr) unsafe.Pointer { - return unsafe.Pointer(uintptr(unsafe.Pointer(h)) + uintptr(cmsgAlignOf(SizeofCmsghdr))) +func (h *Cmsghdr) data(offset uintptr) unsafe.Pointer { + return unsafe.Pointer(uintptr(unsafe.Pointer(h)) + uintptr(cmsgAlignOf(SizeofCmsghdr)) + offset) } // SocketControlMessage represents a socket control message. @@ -94,10 +94,8 @@ func UnixRights(fds ...int) []byte { h.Level = SOL_SOCKET h.Type = SCM_RIGHTS h.SetLen(CmsgLen(datalen)) - data := cmsgData(h) - for _, fd := range fds { - *(*int32)(data) = int32(fd) - data = unsafe.Pointer(uintptr(data) + 4) + for i, fd := range fds { + *(*int32)(h.data(4 * uintptr(i))) = int32(fd) } return b }