Skip to content

Commit 9223b3e

Browse files
authored
Merge pull request #257 from espadolini/mathrand
Use math/rand/v2 rather than crypto/rand
2 parents cc5b0e6 + 9a2bcdd commit 9223b3e

4 files changed

Lines changed: 21 additions & 11 deletions

File tree

.golangci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ linters:
88
enable:
99
- copyloopvar # Detects places where loop variables are copied.
1010
- dupword # Detects duplicate words.
11+
- depguard # Allows or rejects certain package imports.
1112
- errorlint # Detects code that may cause problems with Go 1.13 error wrapping.
1213
- gocritic # Metalinter; detects bugs, performance, and styling issues.
1314
- gosec # Detects security problems.
@@ -22,6 +23,16 @@ linters:
2223
- unconvert # Detects unnecessary type conversions.
2324
- usetesting # Reports uses of functions with replacement inside the testing package.
2425
settings:
26+
depguard:
27+
rules:
28+
fipscompliance:
29+
deny:
30+
- pkg: crypto/
31+
desc: Using crypto/... requires consumers to potentially have to care about FIPS compliance, so it should be avoided if possible.
32+
mathrandv1:
33+
deny:
34+
- pkg: math/rand$
35+
desc: Use math/rand/v2.
2536
govet:
2637
enable-all: true
2738
settings:

go-selinux/selinux_linux.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package selinux
33
import (
44
"bufio"
55
"bytes"
6-
"crypto/rand"
7-
"encoding/binary"
86
"errors"
97
"fmt"
108
"io"
119
"io/fs"
1210
"math/big"
11+
"math/rand/v2"
1312
"os"
1413
"os/user"
1514
"path/filepath"
@@ -954,16 +953,16 @@ func mcsDelete(mcs string) {
954953

955954
func uniqMcs(catRange uint32) string {
956955
var (
957-
n uint32
958956
c1, c2 uint32
959957
mcs string
960958
)
961959

962960
for {
963-
_ = binary.Read(rand.Reader, binary.LittleEndian, &n)
964-
c1 = n % catRange
965-
_ = binary.Read(rand.Reader, binary.LittleEndian, &n)
966-
c2 = n % catRange
961+
//#nosec G404 -- using slightly more predictable MCS labels won't affect security, so it's fine to use math/rand/v2 here.
962+
{
963+
c1 = rand.Uint32N(catRange)
964+
c2 = rand.Uint32N(catRange)
965+
}
967966
if c1 == c2 {
968967
continue
969968
} else if c1 > c2 {

pkg/pwalk/pwalk_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package pwalk
22

33
import (
44
"errors"
5-
"math/rand"
5+
"math/rand/v2"
66
"os"
77
"path/filepath"
88
"runtime"
@@ -231,6 +231,6 @@ func cbReadFile(path string, info os.FileInfo, _ error) error {
231231
}
232232

233233
func cbRandomSleep(_ string, _ os.FileInfo, _ error) error {
234-
time.Sleep(time.Duration(rand.Intn(500)) * time.Microsecond) //nolint:gosec // ignore G404: Use of weak random number generator
234+
time.Sleep(rand.N(500 * time.Microsecond)) //#nosec G404 -- this is test code
235235
return nil
236236
}

pkg/pwalkdir/pwalkdir_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package pwalkdir
33
import (
44
"errors"
55
"io/fs"
6-
"math/rand"
6+
"math/rand/v2"
77
"os"
88
"path/filepath"
99
"runtime"
@@ -231,6 +231,6 @@ func cbReadFile(path string, e fs.DirEntry, _ error) error {
231231
}
232232

233233
func cbRandomSleep(_ string, _ fs.DirEntry, _ error) error {
234-
time.Sleep(time.Duration(rand.Intn(500)) * time.Microsecond) //nolint:gosec // ignore G404: Use of weak random number generator
234+
time.Sleep(rand.N(500 * time.Microsecond)) //#nosec G404 -- this is test code
235235
return nil
236236
}

0 commit comments

Comments
 (0)