Skip to content

Commit 6532fac

Browse files
committed
reverting back to mathrand
1 parent d482dc5 commit 6532fac

File tree

3 files changed

+9
-43
lines changed

3 files changed

+9
-43
lines changed

child/child.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
package child
55

66
import (
7-
cryptorand "crypto/rand"
87
"errors"
98
"fmt"
109
"io"
1110
"log"
12-
"math/big"
11+
"math/rand"
1312
"os"
1413
"os/exec"
1514
"strings"
@@ -476,15 +475,7 @@ func (c *Child) randomSplay() <-chan time.Time {
476475
}
477476

478477
ns := c.splay.Nanoseconds()
479-
// Use crypto/rand for secure random generation (CWE-338 fix)
480-
max := big.NewInt(ns)
481-
n, err := cryptorand.Int(cryptorand.Reader, max)
482-
if err != nil {
483-
// Fallback to no splay if crypto/rand fails
484-
log.Printf("[WARN] (child) failed to generate secure random splay: %v", err)
485-
return time.After(0)
486-
}
487-
t := time.Duration(n.Int64())
478+
t := time.Duration(rand.Int63n(ns))
488479

489480
c.logger.Printf("[DEBUG] (child) waiting %.2fs for random splay", t.Seconds())
490481

dependency/vault_common.go

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
package dependency
55

66
import (
7-
cryptorand "crypto/rand"
87
"encoding/json"
98
"fmt"
109
"log"
11-
"math/big"
10+
"math/rand"
1211
"sync"
1312
"time"
1413

@@ -186,15 +185,15 @@ func leaseCheckWait(s *Secret, retryCount int) time.Duration {
186185
sleep = sleep / 3.0
187186

188187
// Use some randomness so many clients do not hit Vault simultaneously.
189-
sleep = sleep * (secureRandomFloat64() + 1) / 2.0
188+
sleep = sleep * (rand.Float64() + 1) / 2.0
190189
} else if !rotatingSecret {
191190
// If the secret doesn't have a rotation period, this is a non-renewable leased
192191
// secret.
193192
// For non-renewable leases set the renew duration to use much of the secret
194193
// lease as possible. Use a stagger over the configured threshold
195194
// fraction of the lease duration so that many clients do not hit
196195
// Vault simultaneously.
197-
finalFraction := VaultLeaseRenewalThreshold + (secureRandomFloat64()-0.5)*0.1
196+
finalFraction := VaultLeaseRenewalThreshold + (rand.Float64()-0.5)*0.1
198197
if finalFraction >= 1.0 || finalFraction <= 0.0 {
199198
// If the fraction randomly winds up outside of (0.0-1.0), clamp
200199
// back down to the VaultLeaseRenewalThreshold provided by the user,
@@ -209,25 +208,10 @@ func leaseCheckWait(s *Secret, retryCount int) time.Duration {
209208
return time.Duration(sleep)
210209
}
211210

212-
// secureRandomFloat64 generates a cryptographically secure random float64 in [0.0, 1.0).
213-
// This is thread-safe and used to fix CWE-338.
214-
func secureRandomFloat64() float64 {
215-
max := big.NewInt(1000000)
216-
n, err := cryptorand.Int(cryptorand.Reader, max)
217-
if err != nil {
218-
// Fallback to 0.5 if crypto/rand fails (rare but possible)
219-
log.Printf("[WARN] Failed to generate secure random number: %v", err)
220-
return 0.5
221-
}
222-
return float64(n.Int64()) / 1000000.0
223-
}
224-
225211
// jitter adds randomness to a duration to prevent thundering herd.
226-
// It reduces the duration by up to maxJitter (10%) randomly using crypto/rand.
227-
// This function is thread-safe.
212+
// It reduces the duration by up to maxJitter (10%) randomly.
228213
func jitter(t time.Duration) time.Duration {
229-
randomFloat := secureRandomFloat64()
230-
f := float64(t) * (1.0 - maxJitter*randomFloat)
214+
f := float64(t) * (1.0 - maxJitter*rand.Float64())
231215
return time.Duration(f)
232216
}
233217

watch/view.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
package watch
55

66
import (
7-
cryptorand "crypto/rand"
87
"errors"
98
"fmt"
109
"log"
11-
"math/big"
10+
"math/rand"
1211
"reflect"
1312
"sync"
1413
"time"
@@ -312,15 +311,7 @@ const minDelayBetweenUpdates = time.Millisecond * 100
312311
func rateLimiter(start time.Time) time.Duration {
313312
remaining := minDelayBetweenUpdates - time.Since(start)
314313
if remaining > 0 {
315-
// Use crypto/rand for secure random generation (CWE-338 fix)
316-
max := big.NewInt(20000000) // 0-20ms in nanoseconds
317-
n, err := cryptorand.Int(cryptorand.Reader, max)
318-
if err != nil {
319-
// Fallback to no dither if crypto/rand fails
320-
log.Printf("[WARN] (view) failed to generate secure random dither: %v", err)
321-
return remaining
322-
}
323-
dither := time.Duration(n.Int64())
314+
dither := time.Duration(rand.Int63n(20000000)) // 0-20ms in nanoseconds
324315
return remaining + dither
325316
}
326317
return 0

0 commit comments

Comments
 (0)