Skip to content

Commit

Permalink
Fix #968 (potential use-after-free)
Browse files Browse the repository at this point in the history
This is a minimal fix for #968.
  • Loading branch information
dhardy committed Apr 28, 2020
1 parent 353d0e8 commit 1cebc95
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/rngs/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! Thread-local random number generator
use std::cell::UnsafeCell;
use std::ptr::NonNull;
use std::marker::PhantomData;

use super::std::Core;
use crate::rngs::adapter::ReseedingRng;
Expand Down Expand Up @@ -55,8 +55,8 @@ const THREAD_RNG_RESEED_THRESHOLD: u64 = 1024 * 64;
/// [`StdRng`]: crate::rngs::StdRng
#[derive(Copy, Clone, Debug)]
pub struct ThreadRng {
// inner raw pointer implies type is neither Send nor Sync
rng: NonNull<ReseedingRng<Core, OsRng>>,
// type if neither Send nor Sync
_phantom: PhantomData<*const ()>,
}

thread_local!(
Expand All @@ -78,34 +78,41 @@ thread_local!(
///
/// For more information see [`ThreadRng`].
pub fn thread_rng() -> ThreadRng {
let raw = THREAD_RNG_KEY.with(|t| t.get());
let nn = NonNull::new(raw).unwrap();
ThreadRng { rng: nn }
let _phantom = Default::default();
ThreadRng { _phantom }
}

impl Default for ThreadRng {
fn default() -> ThreadRng {
crate::prelude::thread_rng()
thread_rng()
}
}

impl ThreadRng {
#[inline(always)]
fn rng(&mut self) -> &mut ReseedingRng<Core, OsRng> {
let ptr = THREAD_RNG_KEY.with(|rng| rng.get());
unsafe { &mut *ptr }
}
}

impl RngCore for ThreadRng {
#[inline(always)]
fn next_u32(&mut self) -> u32 {
unsafe { self.rng.as_mut().next_u32() }
self.rng().next_u32()
}

#[inline(always)]
fn next_u64(&mut self) -> u64 {
unsafe { self.rng.as_mut().next_u64() }
self.rng().next_u64()
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
unsafe { self.rng.as_mut().fill_bytes(dest) }
self.rng().fill_bytes(dest)
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
unsafe { self.rng.as_mut().try_fill_bytes(dest) }
self.rng().try_fill_bytes(dest)
}
}

Expand Down

0 comments on commit 1cebc95

Please sign in to comment.