Skip to content

Commit

Permalink
Rollup merge of #107387 - joboet:hermit_random, r=ChrisDenton
Browse files Browse the repository at this point in the history
Use random `HashMap` keys on Hermit

Initializing the keys with random data provided by the libOS avoids HashDOS attacks and similar issues.

CC `@stlankes`
  • Loading branch information
matthiaskrgr authored Mar 29, 2023
2 parents f98598c + f6bde03 commit 57f1d11
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions library/std/src/sys/hermit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,18 @@ pub fn abort_internal() -> ! {
}
}

// FIXME: just a workaround to test the system
pub fn hashmap_random_keys() -> (u64, u64) {
(1, 2)
let mut buf = [0; 16];
let mut slice = &mut buf[..];
while !slice.is_empty() {
let res = cvt(unsafe { abi::read_entropy(slice.as_mut_ptr(), slice.len(), 0) })
.expect("failed to generate random hashmap keys");
slice = &mut slice[res as usize..];
}

let key1 = buf[..8].try_into().unwrap();
let key2 = buf[8..].try_into().unwrap();
(u64::from_ne_bytes(key1), u64::from_ne_bytes(key2))
}

// This function is needed by the panic runtime. The symbol is named in
Expand Down

0 comments on commit 57f1d11

Please sign in to comment.