1
0
mirror of https://github.com/golang/go synced 2024-11-11 23:10:23 -07:00

net: fix {FileConn, FileListener, FilePacketConn} fd leak to child process.

All of them call `newFileFD' which must properly restore close-on-exec on
duplicated fds.

R=golang-dev, bradfitz, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/6445081
This commit is contained in:
Sébastien Paolacci 2012-09-04 12:37:23 -07:00 committed by Brad Fitzpatrick
parent 532dee3842
commit 2836c63459
2 changed files with 16 additions and 0 deletions

View File

@ -12,10 +12,14 @@ import (
) )
func newFileFD(f *os.File) (*netFD, error) { func newFileFD(f *os.File) (*netFD, error) {
syscall.ForkLock.RLock()
fd, err := syscall.Dup(int(f.Fd())) fd, err := syscall.Dup(int(f.Fd()))
if err != nil { if err != nil {
syscall.ForkLock.RUnlock()
return nil, os.NewSyscallError("dup", err) return nil, os.NewSyscallError("dup", err)
} }
syscall.CloseOnExec(fd)
syscall.ForkLock.RUnlock()
sotype, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE) sotype, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE)
if err != nil { if err != nil {

View File

@ -167,6 +167,18 @@ func TestExtraFiles(t *testing.T) {
} }
defer ln.Close() defer ln.Close()
// Make sure duplicated fds don't leak to the child.
f, err := ln.(*net.TCPListener).File()
if err != nil {
t.Fatal(err)
}
defer f.Close()
ln2, err := net.FileListener(f)
if err != nil {
t.Fatal(err)
}
defer ln2.Close()
// Force TLS root certs to be loaded (which might involve // Force TLS root certs to be loaded (which might involve
// cgo), to make sure none of that potential C code leaks fds. // cgo), to make sure none of that potential C code leaks fds.
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {