Skip to content

Commit 57f1d11

Browse files
authored
Rollup merge of #107387 - joboet:hermit_random, r=ChrisDenton
Use random `HashMap` keys on Hermit Initializing the keys with random data provided by the libOS avoids HashDOS attacks and similar issues. CC `@stlankes`
2 parents f98598c + f6bde03 commit 57f1d11

File tree

1 file changed

+11
-2
lines changed
  • library/std/src/sys/hermit

1 file changed

+11
-2
lines changed

library/std/src/sys/hermit/mod.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,18 @@ pub fn abort_internal() -> ! {
7575
}
7676
}
7777

78-
// FIXME: just a workaround to test the system
7978
pub fn hashmap_random_keys() -> (u64, u64) {
80-
(1, 2)
79+
let mut buf = [0; 16];
80+
let mut slice = &mut buf[..];
81+
while !slice.is_empty() {
82+
let res = cvt(unsafe { abi::read_entropy(slice.as_mut_ptr(), slice.len(), 0) })
83+
.expect("failed to generate random hashmap keys");
84+
slice = &mut slice[res as usize..];
85+
}
86+
87+
let key1 = buf[..8].try_into().unwrap();
88+
let key2 = buf[8..].try_into().unwrap();
89+
(u64::from_ne_bytes(key1), u64::from_ne_bytes(key2))
8190
}
8291

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

0 commit comments

Comments
 (0)