-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
runtime: use getrandom(2) for readRandom #68278
Comments
Related Issues (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
I don't think a proposal is necessary as there are no user facing changes? |
On linux at least, the runtime does not normally call getrandom. We use the auxv random data at startup. |
Yes, but I think it's also worth to simplify the fallback path. All this changes will be at most a middle size CL. |
Yeah, no problem, I was not arguing against the proposed change. Simplification is good. Just mentioning it probably won't affect any behavior. |
Given #67001 is accepted, I don't think this needs to be a proposal. Taking out of the proposal process. |
Thanks. I'd like to send a CL to implement this when the next development window opens. |
I think #66821 is about |
Looks like math/rand/v2 uses runtime.rand for the random source of top level functions Lines 254 to 259 in 71f9dbb
Not only that, but runtime.rand is used all over the stdlib, net, os, sync, math/rand, hashing maps in the runtime, etc. Both runtime.rand and the random source for an M (mrandinit) use globalRand that is setup in randinit, which might be using readRandom if there is no startupRand. On linux we seem to always use auxv as startupRand. Lines 245 to 250 in 71f9dbb
I'm not sure if the readRandom path is ever taken for linux. |
Looks like sysauxv has some cases where startupRand is not set, not sure how common it is: Lines 304 to 309 in 71f9dbb
In these cases it will use readRandom. Edit: NVM, I did not saw there was a for loop here. So I think startupRand is always initialized. |
Change https://go.dev/cl/608436 mentions this issue: |
Proposal Details
Currently when we set the random seed on Linux, the runtime's fallback path needs to open, read and then close "/dev/urandom". This requires 3 system calls. A single getrandom does the same thing and is simpler than using
open() -> read() -> closefd()
.#67001 Raised the minimum supported Linux kernel version to 3.17+, so that "getrandom(2)" can be used in the runtime package and we can do all the things in one system call now.
We can wrap getrandom with
internal/runtime/syscall.Syscall6
so that the helper functionreadRandom
will no longer need to open and close "/dev/urandom".Since Go 1.24 will only support Linux kernel 3.17+ (or 3.10+ with getrandom patches), I think a fallback is unnecessary (and a fallback can easily be added if someone needs it).
The text was updated successfully, but these errors were encountered: