Skip to content
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

Fix unsoundness in <BlockRng64 as RngCore>::next_u32, with less unsafe code #1160

Merged
merged 22 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ A [separate changelog is kept for rand_core](rand_core/CHANGELOG.md).

You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.html) useful.

## [0.8.5] - unreleased
## [0.8.5] - 2021-08-20
### Fixes
- Fix build on non-32/64-bit architectures (#1144)
- Fix build on non-32/64-bit architectures (#1144)

### Platform support
- Remove special cases for emscripten (#1142)

### Documentation
- Added docs about rand's use of const generics (#1150)
- Better random chars example (#1157)

## [0.8.4] - 2021-06-15
### Additions
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rand"
version = "0.8.4"
version = "0.8.5"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down
25 changes: 25 additions & 0 deletions rand_chacha/src/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,4 +629,29 @@ mod test {
rng.set_word_pos(0);
assert_eq!(rng.get_word_pos(), 0);
}

#[test]
fn test_chacha_next_u32_vs_next_u64() {
let mut rng1 = ChaChaRng::from_seed(Default::default());
let mut rng2 = rng1.clone();
let mut rng3 = rng1.clone();


let mut a = [0; 16];
(&mut a[..4]).copy_from_slice(&rng1.next_u32().to_le_bytes());
(&mut a[4..12]).copy_from_slice(&rng1.next_u64().to_le_bytes());
(&mut a[12..]).copy_from_slice(&rng1.next_u32().to_le_bytes());

let mut b = [0; 16];
(&mut b[..4]).copy_from_slice(&rng2.next_u32().to_le_bytes());
(&mut b[4..8]).copy_from_slice(&rng2.next_u32().to_le_bytes());
(&mut b[8..]).copy_from_slice(&rng2.next_u64().to_le_bytes());
assert_eq!(a, b);

let mut c = [0; 16];
(&mut c[..8]).copy_from_slice(&rng3.next_u64().to_le_bytes());
(&mut c[8..12]).copy_from_slice(&rng3.next_u32().to_le_bytes());
(&mut c[12..]).copy_from_slice(&rng3.next_u32().to_le_bytes());
assert_eq!(a, b);
vks marked this conversation as resolved.
Show resolved Hide resolved
}
}
4 changes: 4 additions & 0 deletions rand_core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.6.4] - 2021-08-20
### Fixed
- Fix unsoundness in `<BlockRng64 as RngCore>::next_u32` (#1160)

## [0.6.3] - 2021-06-15
### Changed
- Improved bound for `serde` impls on `BlockRng` (#1130)
Expand Down
2 changes: 1 addition & 1 deletion rand_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rand_core"
version = "0.6.3"
version = "0.6.4"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down
21 changes: 9 additions & 12 deletions rand_core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,27 +352,24 @@ where
{
#[inline]
fn next_u32(&mut self) -> u32 {
let mut index = self.index * 2 - self.half_used as usize;
if index >= self.results.as_ref().len() * 2 {
let mut index = self.index - self.half_used as usize;
vks marked this conversation as resolved.
Show resolved Hide resolved
if index >= self.results.as_ref().len() {
self.core.generate(&mut self.results);
self.index = 0;
index = 0;
// `self.half_used` is by definition `false`
self.half_used = false;
index = 0;
}

self.half_used = !self.half_used;
self.index += self.half_used as usize;

// Index as if this is a u32 slice.
unsafe {
let results = &*(self.results.as_ref() as *const [u64] as *const [u32]);
if cfg!(target_endian = "little") {
*results.get_unchecked(index)
} else {
*results.get_unchecked(index ^ 1)
}
}
#[cfg(target_endian = "little")]
let half_used = self.half_used;
#[cfg(not(target_endian = "little"))]
let half_used = !self.half_used;

(self.results.as_ref()[index] >> (32 * (half_used as usize))) as u32
}

#[inline]
Expand Down