about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-03-29 21:19:47 +0200
committerGitHub <noreply@github.com>2023-03-29 21:19:47 +0200
commit57f1d114cdf891e65b6659af1091e2b5b6b6e723 (patch)
treee9d90684b7b1fede14491574e2545d64fa7579dd
parentf98598c6cd34947efa9e3977338e9bce62d1997c (diff)
parentf6bde0352b52eb3b31c78b7abd70c1be396ccb0f (diff)
downloadrust-57f1d114cdf891e65b6659af1091e2b5b6b6e723.tar.gz
rust-57f1d114cdf891e65b6659af1091e2b5b6b6e723.zip
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`
-rw-r--r--library/std/src/sys/hermit/mod.rs13
1 files changed, 11 insertions, 2 deletions
diff --git a/library/std/src/sys/hermit/mod.rs b/library/std/src/sys/hermit/mod.rs
index 743e93a2fd4..c7cb8466705 100644
--- a/library/std/src/sys/hermit/mod.rs
+++ b/library/std/src/sys/hermit/mod.rs
@@ -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