]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: avoid "just past the end" pointers in UnixRights
authorMatthew Dempsky <mdempsky@google.com>
Thu, 17 Oct 2019 01:02:35 +0000 (18:02 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 17 Oct 2019 18:42:47 +0000 (18:42 +0000)
Caught with -d=checkptr.

Updates #22218.

Change-Id: Ic0fcff4d2c8d83e4e7f5e0c6d01f03c9c7766c6d
Reviewed-on: https://go-review.googlesource.com/c/go/+/201617
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/syscall/sockcmsg_linux.go
src/syscall/sockcmsg_unix.go

index 4cb9075ba8c2b63a0405ea7b51bcd4f8e749d5d5..d97667cf7e93a7a3c795d92b8fdebbc63143a09f 100644 (file)
@@ -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
 }
 
index 71efd464707e611aafd888db22cc7f037eea87fa..4eb0c0b74872978c7f939ca8600f9f72e664b69e 100644 (file)
@@ -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
 }