Skip to content

OsRng run-time failure / the rand_os crate / modularity #681

Closed

Description

@dhardy

Roughly, rand could be modularised as follows:

component dependencies
rand_core
PRNG crates core
rand_os core + std
JitterRng core + std
EntropyRng core + os + jitter + std
ThreadRng core + os + jitter + entropy + std + hc
seq core
distributions core
Rng core + seq + distributions

Roughly 30% of the current code comes under non-linear distributions, which is its own story (#290). For the rest I don't think there's sufficient utility for more crates.

I regret accepting #643 as-is; it "promises" to support all std platforms yet fails on WASM with a compile error (#674, #678) and on any unsupported platforms. The reasons that is an issue are convoluted (probably involving users depending on rand with default features yet not needing them all), but the conclusion of #678 is that Rand should always build for wasm32-unknown-unknown, failing if necessary at run-time.

We used to use run-time failure within EntropyRng but disable OsRng at compile time where unneeded. Practical solutions are:

  1. Restore the old behavior: rand_os must compile but without the OsRng struct on unsupported platforms. EntropyRng is currently available on all platforms but with run-time failure when no source is available, however it requires a complex cfg pattern in order to disable the missing OsRng dependency when not available; it would therefore make sense to have this in the same crate.
  2. Enable OsRng on WASM, and use run-time failure on missing implementation.
  3. Enable OsRng on all platforms, and use run-time failure on missing implementation.

I think the cleanest solution would be (1), but @tarcieri @naftulikay I understand there would be resistance to moving JitterRng into rand_os. Is this really justified? I guess in part this depends on whether the RFC proposed in #648 gets accepted, though this will be a while coming.

Otherwise we can do (2) or (3); the difference is that (2) would cause build failures on any platform where Rand is used with std/default features but OsRng is unavailable. I'm not sure whether this is a good idea?

I suppose (2) is most sensible now.

CC @newpavlov @burdges

Activity

  1. tarcieri commented on Jan 6, 2019

    @tarcieri
    Contributor

    I understand there would be resistance to moving JitterRng into rand_os. Is this really justified?

    I do not consider JitterRng as meeting the contract of an OsRng. It is bespoke Rust code, as opposed to a system-provided CSRNG. Personally I would like something where if an OS / platform-specific RNG is not available, it will refuse to compile, as opposed to falling back to JitterRng.

  2. dhardy commented on Jan 6, 2019

    @dhardy
    MemberAuthor

    Personally I would like something where if an OS / platform-specific RNG is not available, it will refuse to compile

    The current compile_error! is not an option due to the required cfgs being over-complex, but (1) is close enough.

    I am not proposing to adjust OsRng in any other way; simply to add EntropyRng and JitterRng to the same crate.

  3. newpavlov commented on Jan 6, 2019

    @newpavlov
    Member

    the conclusion of #678 is that Rand should always build for wasm32-unknown-unknown, failing if necessary at run-time.

    I disagree with it. I believe that rand_os should fail at compile time for wasm32-unknown-unknown if none of stdweb or wasm_bindgen features are enabled, or if they are enabled both.

    The breakage caused by splitting code into rand_os is indeed unfortunate, but I think we better do the following:

    • publish rand_os v0.1.1 which will use stdweb as a "default"
    • yank rand_os v0.1.0
    • publish rand_os v0.2.0 which will use compile error if both or none of the features are enabled
  4. CryZe commented on Jan 6, 2019

    @CryZe

    You can't have target specific features with cargo atm, therefore it is impossible to correctly implement failing to compile at the moment. I have indirect dependencies on rand through criterion and parking_lot. You can't realistically disable the rand_os dependency (via rand's std feature as far as I understand) for both of these crates. Therefore my use case will always (or at least until cargo can support target specific features) compile in rand_os without using stdweb or wasm_bindgen. So failing to compile is not a realistic option until then.

    What is needed is either a split between the -unknown and a potential -web target and / or fixing cargo.

  5. dhardy commented on Jan 6, 2019

    @dhardy
    MemberAuthor

    I disagree with it.

    Noted, but the alternative is to keep telling those who complain about broken CI that they need more complex set-up to add the required feature on WASM... I guess this is possible, but it's a support nightmare.

    @CryZe you can probably do this — force your CI tests to depend on WASM + stdweb (or -bindgen). However if you don't use Rand directly, you will need to add it (as a dev-dependency or via another crate only used for testing).

    publish rand_os v0.2.0 which will use compile error if both or none of the features are enabled

    Well, I disagree with that, because there is a plan to eventually make the two compatible.

    What is needed is either a split between the -unknown and a potential -web target and / or fixing cargo.

    Agreed; however for now we have to support wasm32-unknown-unknown, and for that I think we're justified in deviating a little from how we normally do things.

  6. CryZe commented on Jan 6, 2019

    @CryZe

    It's not my tests that are broken, it's the actual compile (the tests work just fine). For my actual compile, I can't activate either stdweb or wasm-bindgen feature, because I'm not using them (I'm using a different binding generator, so this is kind of the truly "unknown" use case where I'm fine with rand not having a notion of an OS RNG). Also my crate does not exercise any code paths in the wasm compiled code that uses rand_os. However it gets compiled regardless because criterion is a dev-dependency, so cargo not separating features across different dependency kinds or targets is a big problem.

  7. dhardy commented on Jan 6, 2019

    @dhardy
    MemberAuthor

    But you could work around this by adding one of the features in your build?

  8. tarcieri commented on Jan 6, 2019

    @tarcieri
    Contributor

    You can't have target specific features with cargo atm, therefore it is impossible to correctly implement failing to compile at the moment.

    I'm confused why this can't be done with combinations of cfg(all(...)) / cfg(all(not(...), ...)), feature, target, and compile_error!. It seems possible to implement correctly to me, albeit a bit onerous (and also seems like something to this effect is already happening).

  9. CryZe commented on Jan 6, 2019

    @CryZe

    The stdweb feature doesn't work as I'm not using cargo web (this wouldn't make sense for my unknown use case):

    error: custom attribute panicked
      --> C:\Users\Christopher Serr\.cargo\registry\src\github.com-1ecc6299db9ec823\stdweb-0.4.12\src\webcore\macros.rs:32:9
       |
    32 |         #[$crate::private::js_raw_attr]
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |
      ::: C:\Users\Christopher Serr\.cargo\registry\src\github.com-1ecc6299db9ec823\stdweb-0.4.12\src\webcore\initialization.rs:20:5
       |
    20 |     stdweb_internal_runtime_initialize!( __js_raw_asm );
       |     ---------------------------------------------------- in this macro invocation
       |
       = help: message: you need to use `cargo-web` to compile your project for the `wasm32-unknown-unknown` target
    

    However I just tried the wasm-bindgen feature too and it does seem like it does not conflict with anything the unknown use case needs. So I'm actually fine with that.

    I'm confused why this can't be done with combinations of cfg(all(...)) / cfg(all(not(...), ...)), feature, target, and compile_error!.

    Cargo propagates features of any cfg combination in the Cargo.toml to all dev-dependencies / (normal-)dependencies and all cfg combinations. They are all in a shared feature space, the cfg'ing or dev-dependency-ing doesn't do anything. This is a bug which is not fixed. Therefore within your actual code your cfg will be against the wrong feature flags, that are actually coming from a different target of a different dependency kind. So here it is the dev-dependency criterion compiled for the host that needs OsRng, but the actual wasm32-unknown-unknown target that your code is targeting doesn't need or want OsRng, but criterion's rand/std feature gets propagated to rand regardless and thus OsRng gets compiled in, even though it's only needed in the dev-dependency on the host (x86, not wasm).

  10. burdges commented on Jan 7, 2019

    @burdges
    Contributor

    I'd prefer if ThreadRng remained cryptographic, so I'm now more curious about insecurities in the fallback to JitterRng. I suppose ThreadRng: CryptoRng could become platform dependent though?

    I've no opinion on rand_os being a separate crate, but reverting that decision sounds doable. I love combining all this mess into one crate regardless, so I'd say throw JitterRng, EntropyRng, etc. all into rand_os even if not required. OsRng would never call JitterRng of course, but JitterRng exists due to OS shortcomings, so rand_os makes sense. Anyone writing an OS PRNG could reuse this code inside their OS by providing a different build target for the kernel itself.

    I think runtime failures sound perfectly acceptable here too. impl RngCore for OsRng could just call debug_assert_eq!(true,false,"OsRng unavailable!"); everywhere. It triggers in debug builds and CI only if you use OsRng. I'm curious though if pub type OsRng = !; or pub enum OsRng {} provides a compile time solution, preferably omitting OsRng: RngCore or OsRng: CryptoRng in those cases.

  11. newpavlov commented on Jan 7, 2019

    @newpavlov
    Member

    IIRC jitter fallback is only needed for Windows. One option will be to incorporate it into Windows OS RNG implementation inside rand_os (feature gated maybe?). This way we can remove EntropyRng altogether and use OsRng directly.

    I'm now more curious about insecurities in the fallback to JitterRng

    In my understanding JitterRng should be cryptographically secure, the main issue with it is its performance. Citing JitterRng docs:

    A consequence is that it is orders of magnitude slower than OsRng and PRNGs (about 103..106 slower).

  12. dhardy commented on Jan 7, 2019

    @dhardy
    MemberAuthor

    Interesting point about JitterRng only being needed for Windows. We added it because of one observed problem, though don't know in general how widely useful it is — certainly, most of the time it will not be used.

    We could put it behind a feature gate which is disabled by default — most people won't notice the difference, and those that do can consider enabling it.

    I'd prefer if ThreadRng remained cryptographic

    It will — at the very least we will avoid known security problems and only use things we have reason to believe are secure. Whether or not this meets your standards of security is another matter; currently we don't have formal standards and I don't really want to go there.

  13. dhardy commented on Jan 8, 2019

    @dhardy
    MemberAuthor

    Before we get too carried away (#685) we should discuss what we actually want. One possible option, compatible with @vks's "rand is just a shim" goal, is:

    • keep rand_os for now
    • add rand_entropy containing JitterRng, EntropyRng and FromEntropy
    • if we are ever successful with the RFC in OsRng crate? (or optional PRNG dependencies) #648, we can deprecate the rand_os crate and add OsRng (as a simple wrapper around the std function) into rand_entropy

    Rationale:

    • some people appear to want OsRng and no more; this lets them keep that
    • we can make rand_entropy our "get your entropy from here!" crate for users wanting seeding/random bytes without the rest of rand
    • JitterRng is really just a backup option which may not always be compiled in anyway; it doesn't really need to be promoted to its own crate

    Limitation:

    • if anyone wants to implement their entropy source via JitterRng but with a custom nanosecond timer, and use that in EntropyRng, it might be more difficult — though this isn't necessarily a problem (we need some configuration to enable no_std sources anyway)
  14. newpavlov commented on Jan 8, 2019

    @newpavlov
    Member

    add rand_entropy containing JitterRng, EntropyRng and FromEntropy

    What is a rationale for having EntropyRng assuming rand_os will incorporate feature-gated JitterRng fallback for windows? Why can't we seed RNGs directly from OsRng?

    if we are ever successful with the RFC in #648, we can deprecate the rand_os crate and add OsRng (as a simple wrapper around the std function) into rand_entropy

    If we'll get getrandom-like funciton into std, we will still need rand_os to implement rand_core traits, I highly doubt we will get RngCore into std. Though rand_os will become much-much thinner, as most of the complexity will move into std.

    JitterRng is really just a backup option which may not always be compiled in anyway; it doesn't really need to be promoted to its own crate

    I disagree. JitterRng is quite self-contained and fits into a separate crate really nicely, someone may want to use it independently, e.g. for bare-metal targets. Also in my opinion having it as a optional dependency will be nicer and more explicit compared to a feature gated module.

  15. dhardy commented on Jan 8, 2019

    @dhardy
    MemberAuthor

    What is a rationale for having EntropyRng assuming rand_os will incorporate feature-gated JitterRng fallback for windows? Why can't we seed RNGs directly from OsRng?

    But why do we want to roll the fall-back into the same RNG? It's not simply a wrapper around the OS RNG any more. Same goes for embedded device-specific RNGs and RDRAND — which implies we might want to change how the recent SGX implementation works.

    As I said already, I'd prefer to keep JitterRng generally-applicable but put it behind a feature-gate if not everyone wants it.

    someone may want to use it independently

    This is already possible. The entire reason EntropyRng exists is to allow a flexible cross-platform entropy source, even where there is no OS (and OsRng). We're not there yet, but it should be possible to achieve that by plugging in a high-resolution timer or an external RNG, or even with a local entropy collector/pool. But that is #579.

    However, I guess it still works nicely as a separate crate, though with the number of crates we're collecting I'm wondering again about using multiple repos (core / entropy stuff / rngs / sampling).

  16. 19 remaining items

  17. newpavlov commented on Jan 16, 2019

    @newpavlov
    Member

    I personally like getrandom name. The crate will have to be mostly std-dependable (except SGX rdrand-based back-end and always erroring fallback). I am not sure how exactly it should be incorporated into std, so let's leave it for RFC discussion.

    I really think the jitter stuff belongs there if we want it, rather than as an RNG.

    No, I think it certainly shouldn't be a part of getrandom (or whatever it will be named) crate. The idea is that in future users will be able to overwrite getrandom function definition at application level if needed, e.g. by using function with Jitter fallback.

    Overall I don't quite understand the reluctance toward splitting Jitter RNG into its own crate. It's a self-contained algorithm, and however we will use it in future, a separate crate will not result in any substantial amount of harm, while being easier to add, move or remove.

    The second point of this is to step back and look at what Rand is

    Thus we can have getrandom for cryptographic libs and other low-level usage, and the rand* crates for higher-level random number stuff.

    I believe we will still need RngCore::try_fill_bytes and CryptoRng even after introduction of getrandom (as a separate crate or in the std). Don't forget that crypto crates can be generic over RNGs. For example some may use ThreadRng to speed-up key generation, or use a PRNG for deterministic testing. So I am mostly happy with the current rand_core. (well, except the block part)

    The one issue I see here is that CSPRNGs like ChaCha may be used both by crypto libs and as PRNGs

    I hope in future we will use a single ChaCha implementation for both PRNG and stream cipher. So rand_core::block can be moved into RustCrypto/utils.

  18. dhardy commented on Jan 16, 2019

    @dhardy
    MemberAuthor

    The idea is that in future users will be able to overwrite getrandom function definition at application level if needed, e.g. by using function with Jitter fallback.

    You're now saying you don't think the jitter fallback should be used on Windows by default, but somehow applications which might need it should enable the fallback, and do so by overwriting the secure_random function they want to use as their default random source?

    My reluctance is that I am not convinced we will want to keep JitterRng as an RNG; if it is used to implement secure_random then only a byte-filling-function is required.

    Additionally, I'm not sure whether we should make applications provide a timer to JitterRng then provide that to implement secure_random on certain no_std platforms, or include the jitter code and simply make the application specify the timer.

    Don't forget that crypto crates can be generic over RNGs.

    You point me to an example using only fill_bytes, which could use a simpler trait, and I wonder if this should example should panic on error? However, having only a single RngCore trait to implement for RNGs certainly makes it easier to share CSPRNG code.

  19. tarcieri commented on Jan 16, 2019

    @tarcieri
    Contributor

    I'd be all for a getrandom crate, particularly if it were free of JitterRng fallback. Perhaps rand_os could wrap getrandom and handle the JitterRng fallback?

    Concretely I'd only want the existing OS random functions (getrandom on Linux, RtlGenRandom or BCryptGenRandom on Windows, "safe" arc4random etc), and RDRAND on SGX targets, with no fallbacks like JitterRng.

    Speaking of RDRAND, it seems like that would be preferable to falling back to JitterRng if available, unless there are performance concerns.

  20. tarcieri commented on Jan 16, 2019

    @tarcieri
    Contributor

    Also am I the only one who finds the idea of JitterRng scary in the age of microarchitectural covert channels?

  21. dhardy commented on Jan 16, 2019

    @dhardy
    MemberAuthor

    falling back to JitterRng if available, unless there are performance concerns.

    Have you any idea how slow JitterRng is?

    @tarcieri I am not well versed in side-channel attacks, and don't know where to start answering such a question, aside from the obvious: some type of local hardware or software bug needs to be present, and if that can already read your memory then everything is vulnerable. How practical it is to guess the output of a generator based on nano-second jitter I don't know, but you are right that we shouldn't fully trust it, and RDRAND is likely preferable (that may also not be fully trusted, but if you don't trust your CPU then secure entropy sources aren't your only problem).

    We now have a slight architectural problem though because @nagisa's RDRAND implementation is dependent on rand_core but we are now discussing using it from a lower-level getrandom crate not dependent rand_core.

  22. tarcieri commented on Jan 16, 2019

    @tarcieri
    Contributor

    @dhardy an example of a potential attack vector is attacker-controlled JavaScript running cotenant on the same CPU as a Rust program.

    As we saw last year, covert channels are exploitable from web browsers.

    So the question for JitterRng to truly be a CryptoRng, in my opinion, is: can an attacker-controlled cotenant program, such as an attacker-controlled JavaScript running in the same browser, either learn anything about or influence JitterRng's output?

  23. newpavlov commented on Jan 16, 2019

    @newpavlov
    Member

    @tarcieri
    I think that it's highly unlikely that such attack is practical, as there is just too much stuff happens on a machine constantly at the same time which influence timings in unpredictable ways, and if you'll add an additional load timings will just become less predictable. Though I have vague concerns regarding simplicity of the whitening scheme used in JitterRng, to be safe I would prefer to see something like k12 to consume timings and emit entropy, it could even help with the performance issues a bit if we'll take a less conservative stance on the amount of required "true" entropy.

    @dhardy

    My reluctance is that I am not convinced we will want to keep JitterRng as an RNG;

    We can make rand_core an optional dependency for JitterRng, i.e. RngCore implementation will be feature gated (but probably enabled by default). The same goes for rdrand.

    You point me to an example using only fill_bytes, which could use a simpler trait, and I wonder if this should example should panic on error?

    In the linked case it will panic, which seems is not a problem for dalek developers. But some of the crypto crate developers (e.g. @briansmith and @ctz IIUC) prefer to expose every potential error even if it's highly unlikely. And I don't think that try_fill_bytes takes too much space, considering that it does not get exposed in higher-level code.

  24. tarcieri commented on Jan 16, 2019

    @tarcieri
    Contributor

    Have you any idea how slow JitterRng is?

    I know next to nothing about the current JitterRng implementation, apologies. I can learn, but my goal is to avoid it.

    RDRAND implementation is dependent on rand_core but we are now discussing using it from a lower-level getrandom crate not dependent rand_core.

    Perhaps it should be moved to getrandom as well?

    I think that it's highly unlikely that such attack is practical

    I'd prefer it be a guarantee as opposed to a best guess. A more concrete question to ask is: is the current TRNG algorithm designed to be sidechannel resistant?

    A quick search for papers on this topic pulls up:

    It seems provable sidechannel resistance for TRNGs is a thing.

  25. dhardy commented on Jan 17, 2019

    @dhardy
    MemberAuthor

    @tarcieri JitterRng uses the high-resolution part of nano-second timers. As far as I can tell, JavaScript cannot even access nanosecond timers in the browser. And as @newpavlov says, if it adds extra delays to "manipulate" the output, it will not make the output any easier to predict.

    Your first article on GAROs sounds like it is observational rather than a definitive proof? I didn't read the whole article.

    @newpavlov probably we should have an issue on JitterRng improvements; this isn't the first suggestion I've seen (the other relating to sources of timing delta entropy).

    Summary: JitterRng is very likely secure vs JavaScript side-channel attacks. It could have some improvements (at least regarding performance) and ought to have a security review.

    But as to what to do, it might still be best not to use a jitter-based-rng in our getrandom crate, but instead use RDRAND as a backup option. Should this be enabled by default on all supported targets?

    We can make rand_core an optional dependency for JitterRng, i.e. RngCore implementation will be feature gated (but probably enabled by default). The same goes for rdrand.

    This prevents us from making rand_core depend (optionally) on getrandom, which would be nice: we could move the from_entropy method into SeedableRng.

  26. tarcieri commented on Jan 17, 2019

    @tarcieri
    Contributor

    I made a separate issue for JitterRng if you'd like to continue the discussion there: #699

  27. dhardy commented on Jan 17, 2019

    @dhardy
    MemberAuthor

    @newpavlov I think this means we should either delete JitterRng or move it to a new repository, however we may as well migrate away from using it before removing the code from rand. That means closing #685, though if you like I can make a new repo for that code (jitter_rng or rand_jitter).

    Ideally I'd like another backup source in place before removing it however.

  28. newpavlov commented on Jan 17, 2019

    @newpavlov
    Member

    I don't mind moving rand_jitter into a separate repository, but do you plan to publish rand v0.7? Because JitterRng is a part of public API. I highly doubt anyone has used it directly, but still strictly speaking removing it for v0.6 will be breakage of compatibility promise. So I think it will be better to merge #685 and decide its fate later in #699. I don't think we need two versions of JitterRng, one in the separate repo, and one as part of rand.

  29. dhardy commented on Jan 17, 2019

    @dhardy
    MemberAuthor

    Agreed, we shouldn't remove it before 0.7, and even then we might want a deprecation warning.

    Fair point, the crate name doesn't need to change, so there's no reason we can't merge #685 now.

  30. dhardy commented on Mar 23, 2019

    @dhardy
    MemberAuthor

    I think the time has come to close this thread. It has several progeny:

    The continuation of the remainder of this discussion can move to #760.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions