Skip to content

Commit

Permalink
go-fuzz: set fd inheritance properly for Go 1.17+ on Windows
Browse files Browse the repository at this point in the history
During Go 1.17 development, fd inheritance on Windows was changed in:
    CL 288297 - "syscall: restrict inherited handles on Windows"
    https://golang.org/cl/288297

Running go-fuzz with Go 1.17 on at least some Windows versions caused errors like:
    "write to testee failed: write |1: The pipe is being closed"

The fix is to properly set SysProcAttr.AdditionalInheritedHandles, which is modeled
after the suggestion from Jason Donenfeld in CL 320050:
    https://go-review.googlesource.com/c/go/+/320050/-1..3#message-ed1be75fda3d32c5ff2bd037b951a875cb07c3db

Fixes dvyukov#328
  • Loading branch information
thepudds authored and craiggwilson committed Jun 19, 2022
1 parent a217d9b commit 8b8c0a8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ language: go
matrix:
include:
- os: linux
go: "1.16.x"
go: "1.17.x"
- os: linux
go: "1.15.x"
go: "1.16.x"
- os: linux
go: tip
- os: linux
go: tip
env: SET_GO111MODULE=auto
- os: osx
go: "1.16.x"
go: "1.17.x"
- os: osx
go: "1.15.x"
go: "1.16.x"
- os: osx
go: tip
- os: osx
go: tip
env: SET_GO111MODULE=auto
- os: windows
go: "1.16.x"
go: "1.17.x"
- os: windows
go: "1.15.x"
go: "1.16.x"

# Install coreutils for the 'timeout(1)' utility on windows and osx.
before_install:
Expand Down
23 changes: 23 additions & 0 deletions go-fuzz/sys_go116_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2015 go-fuzz project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

//go:build !go1.17
// +build !go1.17

package main

import (
"fmt"
"os"
"os/exec"
"syscall"
)

func setupCommMapping(cmd *exec.Cmd, comm *Mapping, rOut, wIn *os.File) {
syscall.SetHandleInformation(syscall.Handle(comm.mapping), syscall.HANDLE_FLAG_INHERIT, 1)
syscall.SetHandleInformation(syscall.Handle(rOut.Fd()), syscall.HANDLE_FLAG_INHERIT, 1)
syscall.SetHandleInformation(syscall.Handle(wIn.Fd()), syscall.HANDLE_FLAG_INHERIT, 1)
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_FUZZ_COMM_FD=%v", comm.mapping))
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_FUZZ_IN_FD=%v", rOut.Fd()))
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_FUZZ_OUT_FD=%v", wIn.Fd()))
}
31 changes: 31 additions & 0 deletions go-fuzz/sys_go117_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2015 go-fuzz project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

//go:build go1.17
// +build go1.17

package main

import (
"fmt"
"os"
"os/exec"
"syscall"
)

func setupCommMapping(cmd *exec.Cmd, comm *Mapping, rOut, wIn *os.File) {
syscall.SetHandleInformation(syscall.Handle(comm.mapping), syscall.HANDLE_FLAG_INHERIT, 1)
syscall.SetHandleInformation(syscall.Handle(rOut.Fd()), syscall.HANDLE_FLAG_INHERIT, 1)
syscall.SetHandleInformation(syscall.Handle(wIn.Fd()), syscall.HANDLE_FLAG_INHERIT, 1)
// Setting AdditionalInheritedHandles is required in Go 1.17+.
cmd.SysProcAttr = &syscall.SysProcAttr{
AdditionalInheritedHandles: []syscall.Handle{
syscall.Handle(wIn.Fd()),
syscall.Handle(rOut.Fd()),
syscall.Handle(comm.mapping),
},
}
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_FUZZ_COMM_FD=%v", comm.mapping))
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_FUZZ_IN_FD=%v", rOut.Fd()))
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_FUZZ_OUT_FD=%v", wIn.Fd()))
}
11 changes: 0 additions & 11 deletions go-fuzz/sys_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
package main

import (
"fmt"
"log"
"os"
"os/exec"
"reflect"
"syscall"
"unsafe"
Expand Down Expand Up @@ -47,12 +45,3 @@ func (m *Mapping) destroy() {
syscall.UnmapViewOfFile(m.addr)
syscall.CloseHandle(m.mapping)
}

func setupCommMapping(cmd *exec.Cmd, comm *Mapping, rOut, wIn *os.File) {
syscall.SetHandleInformation(syscall.Handle(comm.mapping), syscall.HANDLE_FLAG_INHERIT, 1)
syscall.SetHandleInformation(syscall.Handle(rOut.Fd()), syscall.HANDLE_FLAG_INHERIT, 1)
syscall.SetHandleInformation(syscall.Handle(wIn.Fd()), syscall.HANDLE_FLAG_INHERIT, 1)
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_FUZZ_COMM_FD=%v", comm.mapping))
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_FUZZ_IN_FD=%v", rOut.Fd()))
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_FUZZ_OUT_FD=%v", wIn.Fd()))
}

0 comments on commit 8b8c0a8

Please sign in to comment.