Skip to content

Commit

Permalink
syscall: avoid "just past the end" pointers in UnixRights
Browse files Browse the repository at this point in the history
Caught with -d=checkptr.

Updates #22218.

Change-Id: Ic0fcff4d2c8d83e4e7f5e0c6d01f03c9c7766c6d
Reviewed-on: https://go-review.googlesource.com/c/go/+/201617
Run-TryBot: Matthew Dempsky <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
mdempsky committed Oct 17, 2019
1 parent 3972f97 commit ef18d6a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/syscall/sockcmsg_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 4 additions & 6 deletions src/syscall/sockcmsg_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit ef18d6a

Please sign in to comment.