]> Cypherpunks repositories - gostls13.git/commitdiff
net: fix {FileConn, FileListener, FilePacketConn} fd leak to child process.
authorSébastien Paolacci <sebastien.paolacci@gmail.com>
Tue, 4 Sep 2012 19:37:23 +0000 (12:37 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 4 Sep 2012 19:37:23 +0000 (12:37 -0700)
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

src/pkg/net/file.go
src/pkg/os/exec/exec_test.go

index 11c8f77a8254130e6bed3d3d3bf4808d36348539..60911b17d389c9e993819d4dc942cdf6379cfa11 100644 (file)
@@ -12,10 +12,14 @@ import (
 )
 
 func newFileFD(f *os.File) (*netFD, error) {
+       syscall.ForkLock.RLock()
        fd, err := syscall.Dup(int(f.Fd()))
        if err != nil {
+               syscall.ForkLock.RUnlock()
                return nil, os.NewSyscallError("dup", err)
        }
+       syscall.CloseOnExec(fd)
+       syscall.ForkLock.RUnlock()
 
        sotype, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE)
        if err != nil {
index 2cc053e5bcd718621c0e94f3d7e738b71f8d6459..af07452b464c4e83cabedd3c55966cdbcd9ddc66 100644 (file)
@@ -167,6 +167,18 @@ func TestExtraFiles(t *testing.T) {
        }
        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
        // cgo), to make sure none of that potential C code leaks fds.
        ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {