Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: pass MSG_CMSG_CLOEXEC flag in ReadMsgUnix
As mentioned in golang#42765, calling "recvmsg" syscall on Linux should come with "MSG_CMSG_CLOEXEC" flag. For other systems which not supports "MSG_CMSG_CLOEXEC". ReadMsgUnix() would check the header. If the header type is "syscall.SCM_RIGHTS", then ReadMsgUnix() would parse the SocketControlMessage and call each fd with "syscall.CloseOnExec" Fixes golang#42765
- Loading branch information
Showing
13 changed files
with
210 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package net | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
func (c *UnixConn) readMsg(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) { | ||
var sa syscall.Sockaddr | ||
n, oobn, flags, sa, err = c.fd.readMsg(b, oob, readMsgFlags) | ||
setCloseOnExec(oob[:oobn]) | ||
|
||
switch sa := sa.(type) { | ||
case *syscall.SockaddrUnix: | ||
if sa.Name != "" { | ||
addr = &UnixAddr{Name: sa.Name, Net: sotypeToNet(c.fd.sotype)} | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build linux | ||
// +build linux | ||
|
||
package net | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
const readMsgFlags int = syscall.MSG_CMSG_CLOEXEC | ||
|
||
func setCloseOnExec(oob []byte) { | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build (js && wasm) || windows | ||
// +build js,wasm windows | ||
|
||
package net | ||
|
||
const readMsgFlags int = 0 | ||
|
||
func setCloseOnExec(oob []byte) { | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build aix || darwin || dragonfly || freebsd || netbsd || openbsd || solaris | ||
// +build aix darwin dragonfly freebsd netbsd openbsd solaris | ||
|
||
package net | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
const readMsgFlags int = 0 | ||
|
||
func setCloseOnExec(oob []byte) { | ||
if oobn <= 0 { | ||
return | ||
} | ||
|
||
scms, err := syscall.ParseSocketControlMessage(oob) | ||
if err != nil { | ||
return | ||
} | ||
|
||
for _, scm := range scms { | ||
if scm.Header.Level == syscall.SOL_SOCKET && scm.Header.Type == syscall.SCM_RIGHTS { | ||
fds, err := syscall.ParseUnixRights(&scm) | ||
if err != nil { | ||
continue | ||
} | ||
for _, fd := range fds { | ||
syscall.CloseOnExec(fd) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris | ||
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris | ||
|
||
package net | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestUnixConnReadMsgUnixSCMRightsCloseOnExec(t *testing.T) { | ||
if !testableNetwork("unix") { | ||
t.Skip("not unix system") | ||
} | ||
|
||
scmFile, err := os.Open(os.DevNull) | ||
if err != nil { | ||
t.Fatalf("file open: %v", err) | ||
} | ||
defer scmFile.Close() | ||
|
||
rights := syscall.UnixRights(int(scmFile.Fd())) | ||
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0) | ||
if err != nil { | ||
t.Fatalf("Socketpair: %v", err) | ||
} | ||
|
||
writeFile := os.NewFile(uintptr(fds[0]), "write-socket") | ||
defer writeFile.Close() | ||
readFile := os.NewFile(uintptr(fds[1]), "read-socket") | ||
defer readFile.Close() | ||
|
||
cw, err := FileConn(writeFile) | ||
if err != nil { | ||
t.Fatalf("FileConn: %v", err) | ||
} | ||
defer cw.Close() | ||
cr, err := FileConn(readFile) | ||
if err != nil { | ||
t.Fatalf("FileConn: %v", err) | ||
} | ||
defer cr.Close() | ||
|
||
ucw, ok := cw.(*UnixConn) | ||
if !ok { | ||
t.Fatalf("got %T; want UnixConn", cw) | ||
} | ||
ucr, ok := cr.(*UnixConn) | ||
if !ok { | ||
t.Fatalf("got %T; want UnixConn", cr) | ||
} | ||
|
||
oob := make([]byte, syscall.CmsgSpace(4)) | ||
err = ucw.SetWriteDeadline(time.Now().Add(5 * time.Second)) | ||
if err != nil { | ||
t.Fatalf("Can't set unix connection timeout: %v", err) | ||
} | ||
_, _, err = ucw.WriteMsgUnix(nil, rights, nil) | ||
if err != nil { | ||
t.Fatalf("UnixConn readMsg: %v", err) | ||
} | ||
err = ucr.SetReadDeadline(time.Now().Add(5 * time.Second)) | ||
if err != nil { | ||
t.Fatalf("Can't set unix connection timeout: %v", err) | ||
} | ||
_, oobn, _, _, err := ucr.ReadMsgUnix(nil, oob) | ||
if err != nil { | ||
t.Fatalf("UnixConn readMsg: %v", err) | ||
} | ||
|
||
scms, err := syscall.ParseSocketControlMessage(oob[:oobn]) | ||
if err != nil { | ||
t.Fatalf("ParseSocketControlMessage: %v", err) | ||
} | ||
if len(scms) != 1 { | ||
t.Fatalf("got scms = %#v; expected 1 SocketControlMessage", scms) | ||
} | ||
scm := scms[0] | ||
gotFds, err := syscall.ParseUnixRights(&scm) | ||
if err != nil { | ||
t.Fatalf("syscall.ParseUnixRights: %v", err) | ||
} | ||
if len(gotFds) != 1 { | ||
t.Fatalf("got FDs %#v: wanted only 1 fd", gotFds) | ||
} | ||
defer func() { | ||
if err := syscall.Close(int(gotFds[0])); err != nil { | ||
t.Fatalf("fail to close gotFds: %v", err) | ||
} | ||
}() | ||
|
||
flags, _, errno := syscall.Syscall(syscall.SYS_FCNTL, uintptr(gotFds[0]), uintptr(syscall.F_GETFD), 0) | ||
if errno != 0 { | ||
t.Fatalf("Can't get flags of fd:%#v, with err:%v", gotFds[0], errno) | ||
} | ||
if flags&syscall.FD_CLOEXEC == 0 { | ||
t.Fatalf("got flags %#x, want %#x (FD_CLOEXEC) set", flags, syscall.FD_CLOEXEC) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters