diff options
| -rw-r--r-- | vm/vmimpl/vmimpl.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/vm/vmimpl/vmimpl.go b/vm/vmimpl/vmimpl.go index b95bd2a18..8106d6166 100644 --- a/vm/vmimpl/vmimpl.go +++ b/vm/vmimpl/vmimpl.go @@ -14,8 +14,10 @@ import ( "io" "math/big" "net" + "os" "os/exec" "strings" + "syscall" "time" "github.com/google/syzkaller/pkg/log" @@ -241,6 +243,19 @@ func UnusedTCPPort() int { ln.Close() return port } + + // Continue searching for a port only if we fail with EADDRINUSE or don't have permissions to use this port. + // Although we exclude ports <1024 in RandomPort(), it's still possible that we can face a restricted port. + var opErr *net.OpError + if errors.As(err, &opErr) && opErr.Op == "listen" { + var syscallErr *os.SyscallError + if errors.As(opErr.Err, &syscallErr) { + if errors.Is(syscallErr.Err, syscall.EADDRINUSE) || errors.Is(syscallErr.Err, syscall.EACCES) { + continue + } + } + } + log.Fatalf("error allocating port localhost:%d: %v", port, err) } } |
