Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kes: use global math/rand instead of math/rand.New #26

Merged
merged 1 commit into from
Jan 15, 2025

Conversation

aead
Copy link
Member

@aead aead commented Jan 15, 2025

This commit reverts to the global math/rand RNG. The global RNG is safe for current use - due to its source. The default rand.NewSource is not safe for concurrent usage.

This can cause crashes like the following:

panic: runtime error: index out of range [-1]

goroutine 3902089 [running]:
math/rand.(*rngSource).Uint64(...)
	math/rand/rng.go:249
math/rand.(*rngSource).Int63(0x0?)
	math/rand/rng.go:234 +0x85
math/rand.(*Rand).Int63(...)
	math/rand/rand.go:96
math/rand.(*Rand).Int31(...)
	math/rand/rand.go:110
math/rand.(*Rand).Int31n(0x454c860?, 0x1?)
	math/rand/rand.go:142 +0x6a
math/rand.(*Rand).Intn(0xc00f099020?, 0x1d?)
	math/rand/rand.go:183 +0x25
github.com/minio/kms-go/kes.(*loadBalancer).Send(0xc000afeba0, {0x68bcb58, 0x91fb380}, 0xc0192ce720, {0x4805358, 0x4},

This commit reverts to the global `math/rand` RNG.
The global RNG is safe for current use - due to its source.
The default `rand.NewSource` is not safe for concurrent usage.

This can cause crashes like the following:
```
panic: runtime error: index out of range [-1]

goroutine 3902089 [running]:
math/rand.(*rngSource).Uint64(...)
	math/rand/rng.go:249
math/rand.(*rngSource).Int63(0x0?)
	math/rand/rng.go:234 +0x85
math/rand.(*Rand).Int63(...)
	math/rand/rand.go:96
math/rand.(*Rand).Int31(...)
	math/rand/rand.go:110
math/rand.(*Rand).Int31n(0x454c860?, 0x1?)
	math/rand/rand.go:142 +0x6a
math/rand.(*Rand).Intn(0xc00f099020?, 0x1d?)
	math/rand/rand.go:183 +0x25
github.com/minio/kms-go/kes.(*loadBalancer).Send(0xc000afeba0, {0x68bcb58, 0x91fb380}, 0xc0192ce720, {0x4805358, 0x4},
```

Signed-off-by: Andreas Auernhammer <[email protected]>
@aead aead requested a review from vadmeste January 15, 2025 22:29
Copy link
Member

@harshavardhana harshavardhana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use custom rand just you have to use it safely like how it's used in minio-go

@harshavardhana harshavardhana merged commit 4bc9298 into main Jan 15, 2025
5 checks passed
@harshavardhana harshavardhana deleted the kes-random branch January 15, 2025 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants