Skip to content

Commit 228d338

Browse files
committed
vm: fix deadlock in UnusedTCPPort
If localhost is not configured on a system, UnusedTCPPort will loop forever without producing any errors. By checking EADDRINUSE and ENOACC and then skipping only in these cases, we'd avoid at least the mentioned deadlock. On top of this, this change should catch other errors without locking, like other DNS errors and so on. Signed-off-by: Ivan Gulakov <gulakov@amazon.de>
1 parent f3558db commit 228d338

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

vm/vmimpl/vmimpl.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import (
1414
"io"
1515
"math/big"
1616
"net"
17+
"os"
1718
"os/exec"
1819
"strings"
20+
"syscall"
1921
"time"
2022

2123
"github.com/google/syzkaller/pkg/log"
@@ -241,6 +243,19 @@ func UnusedTCPPort() int {
241243
ln.Close()
242244
return port
243245
}
246+
247+
// Continue searching for a port only if we fail with EADDRINUSE or don't have permissions to use this port.
248+
// Although we exclude ports <1024 in RandomPort(), it's still possible that we can face a restricted port.
249+
var opErr *net.OpError
250+
if errors.As(err, &opErr) && opErr.Op == "listen" {
251+
var syscallErr *os.SyscallError
252+
if errors.As(opErr.Err, &syscallErr) {
253+
if errors.Is(syscallErr.Err, syscall.EADDRINUSE) || errors.Is(syscallErr.Err, syscall.EACCES) {
254+
continue
255+
}
256+
}
257+
}
258+
log.Fatalf("error allocating port localhost:%d: %v", port, err)
244259
}
245260
}
246261

0 commit comments

Comments
 (0)