Repository navigation
Description
On almost all our platforms, we now have crypto/rand backends that ~never fail.
-
On Linux, we primarily use the getrandom(2) system call, which never fails.
- It may block if the pool is not initialized yet at early boot, and may be interrupted by a signal handler if requesting more than 256 bytes, but neither of those surface as errors to the application.
getrandom()was first available in Linux 3.17, released in October 2014. Debian oldstable is on Linux 5.10.getrandom()can be blocked with seccomp. That's a bad (and weird) idea, and the default Docker profile doesn't do that. In that case we fall back to opening/dev/urandom, which might fail if the file is not available or file descriptors run out.
-
On macOS and iOS we use
arc4random()since https://go.dev/cl/569655. From the man page:These functions are always successful, and no return value is reserved to indicate an error.
-
On Windows we use the ProcessPrng function. From the docs:
Always returns TRUE.
-
The BSDs use similar syscalls with similar properties (whether getrandom or getentropy) although we should switch the ones we can to arc4random.
-
On
js/wasmwe use getRandomValues which doesn't have documented failure modes. -
On WASIP1 there's
random_getwhich regrettably has an error return value, making it the one platform (ignoring misconfigured Linux) where there might be errors getting platform random bytes. Since WASI rests on an underlying platform, and every underlying platform has failure-less CSPRNGs, it's hard to imagine whyrandom_getshould actually return an error.
I'm proposing we make crypto/rand throw (irrecoverably crash the program) if an error occurs, and document that the error return values of crypto/rand.Read and crypto/rand.Reader.Read are always nil.
This will free applications from having to do error handling for a condition that essentially can't happen, and that if it did happen is essentially not possible to handle securely by the application.
This will also allow introducing new APIs like a hypothetical String(charset string) string (not part of this proposal) without an error return, making them more usable and appealing.
Based on a suggestion by @rsc.
/cc @golang/security @golang/proposal-review
Activity
daira commentedon Apr 14, 2024
Do you mean crypto/rand should panic on errors? I'm not very familiar with Go but I didn't think it used the terminology of "throwing" errors. I agree, on general language-independent robustness principles, that it should panic.
FiloSottile commentedon Apr 14, 2024
Panics are recoverable, throw is an internal name for fatal errors. Think of it as a call to
exit(1).If we made crypto/rand panic that risks encouraging applications to wrap the calls in defer/recover "for robustness", when really we think it's so unlikely and so unrecoverable that applications shouldn't try.
go/src/runtime/panic.go
Lines 1008 to 1022 in 519f6a0
bradfitz commentedon Apr 14, 2024
I think all our code already panics on crypto/rand errors (via wrapppers that don't return errors) so SGTM 😀
ydnar commentedon Apr 14, 2024
WASI 0.2 random interface thankfully does not return an error: https://github.com/WebAssembly/wasi-random/blob/main/wit/random.wit
icholy commentedon Apr 15, 2024
Sounds like we need a rand v3
Jorropo commentedon Apr 15, 2024
This is the best argument for always using
crandandmrandaliases.||
crypto/rand!=math/rand😉||Jorropo commentedon Apr 16, 2024
Should we keep the
/dev/urandomfallback for 3.17+ ?I would rather be forced to tweak my seccomp config than having to debug rare flaky throws because I incorrectly configured some sandboxing options.
We can't remove
/dev/urandomcompletely on linux without raising the 2.6.32 baseline.FiloSottile commentedon Apr 16, 2024
This is tempting, but I think making decisions based on kernel version is opening a can of worms. I think even the urandom fallback is reasonably reliable: the file is opened only once, so either crypto/rand never works in a given process or it always works (although it might flake across process executions, if you run out of fds before the first Read call, or if the file is removed).
Jorropo commentedon Apr 16, 2024
Ah I thought it opened a new file each time.
Then can we use import time side effects to solve this (open the file in
init) ?It's very unlikely you are running out of fds before main even started running.
I get the std tries to not do that, but the overwhelming majority of cases init will start running, try
getrandom, succeed and do nothing that seems fine to me. (there also is a clear path to solving this if anyone finds it to be an issue, fixing their seccomp config)FiloSottile commentedon Apr 16, 2024
I was thinking about that but I have no intuition as to whether the cost of calling getrandom (to check if it's available) on
init()for every Linux program is acceptable.39 remaining items
Revert "crypto/rand: add randcrash=0 GODEBUG"
crypto/internal/mlkem768: remove crypto/rand.Read error checking
dmitshur commentedon Nov 26, 2024
@FiloSottile Does this change need to be mentioned in Go 1.24 release notes? Thanks.
ianlancetaylor commentedon Nov 26, 2024
I think this does need to be in the release notes. Sent https://go.dev/cl/632036. Thanks.
gopherbot commentedon Nov 26, 2024
Change https://go.dev/cl/632036 mentions this issue:
doc/next: document that crypto/rand.Read never failsdoc/next: document that crypto/rand.Read never fails
gopherbot commentedon Jan 8, 2025
Change https://go.dev/cl/640996 mentions this issue:
crypto/mlkem: drop GenerateKey error returnSkip tests for broken rand.Reader on Go 1.24+.
Removed unnecesary test/method, in the future We should try to avoid …
AB#447604 Picard audit and update dependencies (#107)
Merge commit from fork
fix(security): 移除 GenerateSecureRandomString 中不安全的 math/rand fallback