diff options
author | 2021-10-28 13:57:14 +0200 | |
---|---|---|
committer | 2021-10-28 13:57:14 +0200 | |
commit | 1f536ffe98695a150f7126c9584f7e640bf12eb9 (patch) | |
tree | 65f613d66e330fe01be5ab08cae2d1df2f5f0e94 | |
parent | manager: delay boottime updates and simplify (diff) | |
download | wireguard-windows-1f536ffe98695a150f7126c9584f7e640bf12eb9.tar.xz wireguard-windows-1f536ffe98695a150f7126c9584f7e640bf12eb9.zip |
manager: use pre-seeded fastrandn instead of math.rand
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r-- | main.go | 10 | ||||
-rw-r--r-- | manager/updatestate.go | 7 |
2 files changed, 5 insertions, 12 deletions
@@ -6,18 +6,15 @@ package main import ( - "crypto/rand" "debug/pe" "errors" "fmt" "io" "log" - unsafeRand "math/rand" "os" "strconv" "strings" "time" - "unsafe" "golang.org/x/sys/windows" @@ -153,19 +150,12 @@ func pipeFromHandleArgument(handleStr string) (*os.File, error) { return os.NewFile(uintptr(handleInt), "pipe"), nil } -func seedUnsafeRng() { - var seed int64 - rand.Read(unsafe.Slice((*byte)(unsafe.Pointer(&seed)), unsafe.Sizeof(seed))) - unsafeRand.Seed(seed) -} - func main() { if windows.SetDllDirectory("") != nil || windows.SetDefaultDllDirectories(windows.LOAD_LIBRARY_SEARCH_SYSTEM32) != nil { panic("failed to restrict dll search path") } setLogFile() - seedUnsafeRng() checkForWow64() if len(os.Args) <= 1 { diff --git a/manager/updatestate.go b/manager/updatestate.go index 5ea0f661..cb65e7dd 100644 --- a/manager/updatestate.go +++ b/manager/updatestate.go @@ -7,14 +7,17 @@ package manager import ( "log" - unsafeRand "math/rand" "time" + _ "unsafe" "golang.zx2c4.com/wireguard/windows/services" "golang.zx2c4.com/wireguard/windows/updater" "golang.zx2c4.com/wireguard/windows/version" ) +//go:linkname fastrandn runtime.fastrandn +func fastrandn(n uint32) uint32 + type UpdateState uint32 const ( @@ -26,7 +29,7 @@ const ( var updateState = UpdateStateUnknown func jitterSleep(min, max time.Duration) { - time.Sleep(min + time.Duration(unsafeRand.Int63n(int64(max-min+1)))) + time.Sleep(min + time.Millisecond*time.Duration(fastrandn(uint32((max-min+1)/time.Millisecond)))) } func checkForUpdates() { |