Repository navigation
Description
Our getrandom crate is close to initial release, which means we can almost start building on it. This means:
rand_os::OsRngwill become a thin wrapper aroundgetrandom- either we have to map
getrandom::Errorvalues torand_core::Errorvalues, or replace the latter completely (suggested in Tracker: Rand 0.7 #715) - the
rand_oscrate will become trivially small
In #715 we have talked about other possible changes:
- the
getrandomcrate should handle other sources on embedded platforms - we no longer appear to need a fallback in case of OS failure
- therefore
EntropyRngis no longer needed - we could replace
rand::FromEntropy::from_entropywithrand_core::SeedableRng::from_entropy
Here I would like to add a caveat: we have no proof that a fallback entropy source is not needed. @tarcieri has pointed out in #699 that we cannot be confident of the security of JitterRng, however there is also resistance to merely switching to RDRAND (partly since it is not available on all platforms, and in part based on the belief that we shouldn't need any fallback).
What we do not want to do is remove EntropyRng only to find that we need to add it back again, meanwhile there is strong resistance to the idea of including a fallback within getrandom. This means that for now we should either keep EntropyRng (even if it is merely a wrapper) or plan to put any needed fallback within OsRng.
Additionally, it seems clear that we don't want to move the JitterRng code into rand_core, and we cannot make rand_core depend on rand_jitter circularly. The same applies to the rdrand crate. Thus the only option for including a fallback within rand_core would be a direct RDRAND implementation, which we probably also don't want.
Like it or not, this means that we cannot move OsRng or from_entropy into rand_core unless we can prove that we don't need a fallback.
Now, if we want to pursue a no-fallback entropy source, we could try removing all fallbacks for Rand 0.7 and watch for error reports. However, I'm not sure whether we want to do so (personally I agree with @cpeterso that Rand should "just work").
This means we have to look at other options for now:
- Keep
rand_oswith justOsRngeven though this is essentially an empty crate - Move
EntropyRngtorand_os(@burdges suggestion) - Drop
rand_osand go back torand::rngs::OsRng
Proposal: 2: move EntropyRng and FromEntropy to rand_os for now, keeping re-exports in rand, add rdrand as a fallback option, and put both jitter and rdrand behind feature flags (disabled by default).
Activity
newpavlov commentedon Mar 23, 2019
I think it's the other way around: it should be proven that fallback is needed. And even if such fallback is needed I believe it should be handled on per-target basis, i.e. personally I am not against fallbacks as part of
getrandom, but I am against using fallbacks for all targets, especially for those which already "just work". So addition of fallback can be done in future in a backwards compatible way without return ofEntropyRng.As for
rand_osI would prefer to keep it a thin wrapper aroundgetrandomand introduce a separate crate forThreadRng, which will depend directly ongetrandom. BTW maybe it's worth to consider renamingThreadRngtoDefaultRngor something similar?dhardy commentedon Mar 23, 2019
We might be able to add a target-specific fallback to
getrandomusing RDRAND, but it sounds like several users would be quite unhappy about inclusion ofJitterRng, thus your suggestion is limited.Therefore I think we should stick to my suggestion and not remove
EntropyRngfor now (though this could potentially happen after proving we don't need it). The reason we must prove we don't need it is because we already have it, and removing then reintroducing it would be totally unnecessary breakage.ThreadRngwill definitely not be included inrand_oseither way; it includes far more code (a whole CSPRNG plus reseeding logic). And no, lets not call itDefaultRng(far less descriptive, unclear, and unnecessary breakage).newpavlov commentedon Mar 23, 2019
I am not against leaving
EntropyRnginrand, but I think it should be marked as deprecated.As for
ThreadRng, it can usegetrandomdirectly (so it will not depend onEntropyRng) and in case of failure simply continue as usual without reseeding (though it should panic if error happens on initial seeding), so it already should solve problem of "just works" for most users. We should use high-quality CSPRNG forThreadRngeither way, so security-wise it shouldn't be a big issue.Regarding potential renaming of
ThreadRng, the breakage problem can be mostly counteracted with a simple type alias. I think that name should indicate that this RNG is a "default choice" for most users, while the current name has a more focus on how it's implemented. And I thinkDefaultRngwill be more descriptive and clear for new users compared toThreadRng, while implementation details can be covered in documentation.dhardy commentedon Mar 24, 2019
No, you're not understanding me: we shouldn't be telling users to stop using
EntropyRngif there's a chance we might need it again.For the most part,
getrandomwill either work or it won't. If it doesn't work,ThreadRngcannot be initialised (we never want insecure random numbers).ThreadRngreally doesn't have much to do with this discussion. Renaming it is way off topic.dhardy commentedon Mar 24, 2019
@cpeterso and @tarcieri, thanks for adding a 👍 to the discussion, but it doesn't help a lot. I am not afraid of bowing to peer pressure, but rational argument and a clear plan is needed.
If the plan is to remove
EntropyRngand, only if necessary, include a fallback source withingetrandomitself, then I think this is acceptable, though of course it means we cannot use CPU jitter as a fallback (#699), which leaves only RDRAND (where available).Note also that there has not been support for the idea of using secure and insecure "channels" for
ThreadRngwhich means there is a chance the crate will simply fail on some platforms (secure by default, no alternative). If hash-map randomisation uses thegetrandomlib, it may just use a constant key whengetrandomfails (which is roughly what happens now on a couple of platforms).cpeterso commentedon Mar 24, 2019
I have not been following your RNG work closely, so I was reluctant to weigh in. :)
My opinion is that:
getrandomcrate should provide a thin, fallible API (i.e. no fallback) to the OS RNG.randcrate should provide higher-level RNGs that "just work" (i.e. have an internal fallback) and utilities for generating specific random types or distributions.If someone is concerned about an RNG's internal fallback behavior, then they can use the
getrandomcrate directly and handle its errors themselves. Or therandcrate could also provide an infallible API that allows the caller to specify which fallback RNG to use. (Of course, that assumes the fallback RNG is itself infallible and doesn't need a secondary fallback!)I think hash-map randomization should be infallible by default, but perhaps include a fallible option for users that care.
dhardy commentedon Mar 25, 2019
There are no secure, universally available infallible options, that much is for sure. The only way of making hash-map randomisation truly infallible is to allow use of a constant key — though arguably it's still better to use a low-entropy key (system clock) and even better to use a high-entropy-but-low-trust key such as from CPU jitter, if secure options are unavailable.
Representing so many levels of entropy quality is the part we haven't worked out yet — most of the people here are of the opinion that
thread_rngshould require high quality entropy or fail, which doesn't correlate with your view that Rand should "just work".I have suggested before that we reintroduce
weak_rng, however that idea has little support. Thus, even when weaker sources of entropy are available, we appear to have no way of making use of them.[-]getrandom interface, rand_core and fallback[/-][+]getrandom, security and fallback entropy[/+]dhardy commentedon Mar 29, 2019
This thread now seems to be mostly a discussion of entropy security and reliability, so lets make it that.
ThreadRngalso gets brought up here since it is one of the more interesting usage points, so lets list out the possibilities (including stupid ones):WIth options 1-2 discounted we should explore the other two options further. There are several potential uses to consider:
I think we can dismiss 2 as a concern — it appears to involve degrading the security of the entire device when necessary to fit a custom usage. If this is deployed on an OS, that OS can be configured to achieve this goal; on embedded devices we already plan to allow custom entropy sources. It does however mean that attempting to periodically improve security (e.g. what
ReseedingRngdoes) may be useful.To cover these use-cases, possibly two kinds of entropy source are sufficient: secure only, best effort.
Then there's
thread_rng, which from a quick search seems to be used by a bunch of different things (many games, randomised algorithms, unit tests, generating passwords, even authentication keys). Clearly security may be important.It should be noted that hash-map randomisation, probably the thing most needing a fallback, does not use
thread_rng.I think this boils down to three questions:
thread_rng? I agree that these do not need to be added except where we have evidence for their necessity, and think the best place for such fallbacks is within thegetrandomcrate (or possibly a low-level wrapper likegetentropy).getrandomand ignore failures. Is this good enough for general usage, including on servers? Do we want a derived crate forbest_effort_entropy?thread_rng? Currently I see no demand for this, though it's possible we might want to add this in the future.In fact, regardless of these questions, we don't need
EntropyRng, and can moveOsRngintorand_core.newpavlov commentedon Mar 29, 2019
Regarding the need for fallbacks: I would like to highlight that Rust
stddoes not use any fallbacks duringHashMapinitialization process and simply panics if entropy retrieval has failed: unix, windows. The only relevant issue reported is this one, which arises from incorrect kernel behavior. So I think in reality it's reasonable to assume that OS RNG always works and we do not have to go to great lengths as to designrandaround fallbacks.Why would we do that? Doing so will break "RNG abstraction traits" proposition of the crate. Optional dependency of
rand_coreongetrandomshould be enough, no? And keeping a thinrand_oscrate should not be a huge maintenance burden.vks commentedon Mar 29, 2019
I'm not convinced we need fallbacks. It seems like a very niche use case, given that even
stddoes not have fallbacks forHashMap. Applications can always choose to initialize aStdRngthemselves and to fall back to whatever works for them (rand_jitter, hardcoded value, rdrand, time...) if the OS randomness does not work. I don't think Rand has to provide that.What do you think about releasing 0.7 without fallbacks? We could see how many issues occur, and add fallbacks if they are really required.
dhardy commentedon Mar 29, 2019
Sorry if it wasn't clear; this was actually my conclusion at the end of this morning's post. However, it makes sense to have a plan for handling them in case we do need them (especially since we are discussing the removal of the current mechanism for handling fallbacks).
Doing so doesn't break anything, and if we are already making
getrandoman optional dependency (forfrom_entropy) then I don't see any value to keeping the extra crate.newpavlov commentedon Mar 29, 2019
The idea behind
rand_coreis to define interfaces shared between RNGs. I don't see howOsRngfits here and frankly I think it will look clearly out of place. If you are so inclined to deprecaterand_os(though I do not understand why) a better place forOsRngwill berand, but certainly notrand_core.dhardy commentedon Mar 29, 2019
rand_corealready contains significantly more than just interfaces: theBlockRngcode, helpers in theimplsmodule,seed_from_u64(which includes a complete PRNG), and now we're talking about addingfrom_entropy.OsRngrequires no more than a simple wrapper.We should feature-gate this (disabled by default, but enabled automatically in Rand), thus it won't be extra code in libraries only using core functionality, but of course it would be included anyway by most Rand users.
newpavlov commentedon Mar 29, 2019
As you've said yourself some time ago including block stuff into
rand_coremay have been a mistake.Considering that
ThreadRngandSeedableRngimpls will pull entropy directly fromgetrandomI doubtOsRngwill be used directly or indirectly by mostrandusers.dhardy commentedon Mar 31, 2019
Interesting point, though it would require some redesign:
ThreadRngis built onReseedingRngwhich needs an RNG for entropy source (or at least, something that can be passed tofrom_rng). This would be unnecessary if the reseeding code was directly embedded inThreadRng, but that probably doesn't make sense unlessReseedingRngis deprecated.There are not many other users of
ReseedingRng, but I did find two: servo, gotham (both roughly equivalent toThreadRngbut without thread-locality). We could still embed the functionality inThreadRng, butReseedingRngis a nice piece of functionality (including fork protection).dhardy commentedon Apr 2, 2019
If
OsRngis no more than a shim aroundgetrandom, there are consequences:OsRngfrom_entropy()isn't needed when you can just typefrom_rng(OsRng).unwrap()rand_osand have an implementation within Rand to avoid the crate importThis means we have to commit to
OsRngremaining a zero-sized type, but since we are able to build goodgetrandomimplementations for evenwasm_bindgen(storing handles in TLS) I don't think this is a problem.dhardy commentedon Apr 2, 2019
Note:
EntropyRngis perhaps a better name thanOsRng, but the latter is more widely used (searchrand_os), so it's probably better not to rename.vks commentedon Apr 2, 2019
This is a nice way to get rid of
FromEntropy. Note however thatfrom_entropywill panic whilegetrandomwill block if there is not enough entropy, so this is a slight change in behavior.I'm not sure we want that code duplication. On the other hand, it is very little code.
I actually prefer
OsRng, because it makes it clearer where the randomness is coming from.EntropyRngis a bit likeRandomRngand does not say much.newpavlov commentedon Apr 2, 2019
It may be somewhat confusing to have two different types withe the absolutely same name and implementation, but if you don't want
rand_osinranddependency tree, well, then it's not a bad solution. I doubt it will create any problems for users in practice.dhardy commentedon Apr 10, 2019
Status:
rand_core::Errorrand_coremust have an (optional) dependency ongetrandom; in this case I think we should movefrom_entropytoSeedableRng(done in Revise rand_core::Error (alt #771) #800)