aboutsummaryrefslogtreecommitdiffstats
path: root/vm/vmimpl
diff options
context:
space:
mode:
authormischa <meierm@cs.uni-bonn.de>2022-04-14 23:51:45 +0000
committerDmitry Vyukov <dvyukov@google.com>2022-04-19 14:22:57 +0200
commit33fc6ed615959c241493d8079c07253ffcc7f245 (patch)
treeaffb323312ba58e68c37d67df9497175d477664a /vm/vmimpl
parentc334415ef5e147ea13e0f70dce3f665dea3e4de9 (diff)
vm/vmimpl: improve port selection
Selecting the monitor port for qemu using an unseeded pseudo-random generator can lead to failed VM startups, as the code contains race conditions. This happens frequently if multiple instances are started with a script. Using real random ports provided by crypto/rand reduces the risk of failing VM starts.
Diffstat (limited to 'vm/vmimpl')
-rw-r--r--vm/vmimpl/vmimpl.go9
1 files changed, 7 insertions, 2 deletions
diff --git a/vm/vmimpl/vmimpl.go b/vm/vmimpl/vmimpl.go
index 854bef0a0..5ec84b35d 100644
--- a/vm/vmimpl/vmimpl.go
+++ b/vm/vmimpl/vmimpl.go
@@ -8,10 +8,11 @@
package vmimpl
import (
+ "crypto/rand"
"errors"
"fmt"
"io"
- "math/rand"
+ "math/big"
"net"
"os/exec"
"strings"
@@ -169,7 +170,11 @@ func Multiplex(cmd *exec.Cmd, merger *OutputMerger, console io.Closer, timeout t
}
func RandomPort() int {
- return rand.Intn(64<<10-1<<10) + 1<<10
+ n, err := rand.Int(rand.Reader, big.NewInt(64<<10-1<<10))
+ if err != nil {
+ panic(err)
+ }
+ return int(n.Int64()) + 1<<10
}
func UnusedTCPPort() int {