diff options
| author | bors <bors@rust-lang.org> | 2016-11-05 04:32:04 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-11-05 04:32:04 -0700 |
| commit | cae6ab1c458ade22b4c228fcd4195917c96e2465 (patch) | |
| tree | 1bcb9eae7f22e64609f0a3f4abd2fccf41f203f3 /src/libstd | |
| parent | 08839965f96ea9947afe936f13a66a0f4f93db73 (diff) | |
| parent | eba93c3a0f7cf8a0b7447ab22081ccd20824d9e6 (diff) | |
| download | rust-cae6ab1c458ade22b4c228fcd4195917c96e2465.tar.gz rust-cae6ab1c458ade22b4c228fcd4195917c96e2465.zip | |
Auto merge of #37470 - arthurprs:sip-smaller, r=alexcrichton
Don't reuse RandomState seeds cc #36481
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/collections/hash/map.rs | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index fb8a0c3c265..ece51d6d826 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -11,6 +11,7 @@ use self::Entry::*; use self::VacantEntryState::*; +use cell::Cell; use borrow::Borrow; use cmp::max; use fmt::{self, Debug}; @@ -2049,24 +2050,21 @@ impl RandomState { // many hash maps are created on a thread. To solve this performance // trap we cache the first set of randomly generated keys per-thread. // - // In doing this, however, we lose the property that all hash maps have - // nondeterministic iteration order as all of those created on the same - // thread would have the same hash keys. This property has been nice in - // the past as it allows for maximal flexibility in the implementation - // of `HashMap` itself. - // - // The constraint here (if there even is one) is just that maps created - // on the same thread have the same iteration order, and that *may* be - // relied upon even though it is not a documented guarantee at all of - // the `HashMap` type. In any case we've decided that this is reasonable - // for now, so caching keys thread-locally seems fine. - thread_local!(static KEYS: (u64, u64) = { + // Later in #36481 it was discovered that exposing a deterministic + // iteration order allows a form of DOS attack. To counter that we + // increment one of the seeds on every RandomState creation, giving + // every corresponding HashMap a different iteration order. + thread_local!(static KEYS: Cell<(u64, u64)> = { let r = rand::OsRng::new(); let mut r = r.expect("failed to create an OS RNG"); - (r.gen(), r.gen()) + Cell::new((r.gen(), r.gen())) }); - KEYS.with(|&(k0, k1)| RandomState { k0: k0, k1: k1 }) + KEYS.with(|keys| { + let (k0, k1) = keys.get(); + keys.set((k0.wrapping_add(1), k1)); + RandomState { k0: k0, k1: k1 } + }) } } |
