Skip to content

Commit b693669

Browse files
committed
feat(entropy): use aws-lc-rs for random bytes
aws-lc-rs is a cryptographic library based on the aws-lc native library. Currently, we are using OsRng from rand crate for getting random bytes to fulfill guest requests. This commit switches from rand crate to aws-lc-rs. The latter is in the process of getting FIPS certification which might be of use to use in the future. Signed-off-by: Babis Chalios <[email protected]>
1 parent a9fba5d commit b693669

File tree

5 files changed

+91
-33
lines changed

5 files changed

+91
-33
lines changed

Cargo.lock

Lines changed: 83 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/seccomp/aarch64-unknown-linux-musl.json

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
},
105105
{
106106
"syscall": "getrandom",
107-
"comment": "getrandom is used by virtio-rng to initialize the rand crate"
107+
"comment": "getrandom is used by aws-lc library which we consume in virtio-rng"
108108
},
109109
{
110110
"syscall": "accept4",
@@ -206,16 +206,7 @@
206206
},
207207
{
208208
"syscall": "madvise",
209-
"comment": "Used by the VirtIO balloon device and by musl for some customer workloads",
210-
"args": [
211-
{
212-
"index": 2,
213-
"type": "dword",
214-
"op": "eq",
215-
"val": 4,
216-
"comment": "libc::MADV_DONTNEED"
217-
}
218-
]
209+
"comment": "Used by the VirtIO balloon device and by musl for some customer workloads. It is also used by aws-lc during random number generation. They setup a memory page that mark with MADV_WIPEONFORK to be able to detect forks. They also call it with -1 to see if madvise is supported in certain platforms."
219210
},
220211
{
221212
"syscall": "mmap",

resources/seccomp/x86_64-unknown-linux-musl.json

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
},
105105
{
106106
"syscall": "getrandom",
107-
"comment": "getrandom is used by virtio-rng to initialize the rand crate"
107+
"comment": "getrandom is used by aws-lc library which we consume in virtio-rng"
108108
},
109109
{
110110
"syscall": "accept4",
@@ -206,16 +206,7 @@
206206
},
207207
{
208208
"syscall": "madvise",
209-
"comment": "Used by the VirtIO balloon device and by musl for some customer workloads",
210-
"args": [
211-
{
212-
"index": 2,
213-
"type": "dword",
214-
"op": "eq",
215-
"val": 4,
216-
"comment": "libc::MADV_DONTNEED"
217-
}
218-
]
209+
"comment": "Used by the VirtIO balloon device and by musl for some customer workloads. It is also used by aws-lc during random number generation. They setup a memory page that mark with MADV_WIPEONFORK to be able to detect forks. They also call it with -1 to see if madvise is supported in certain platforms."
219210
},
220211
{
221212
"syscall": "mmap",

src/vmm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
license = "Apache-2.0"
77

88
[dependencies]
9+
aws-lc-rs = "1.0.2"
910
bitflags = "2.0.2"
1011
derive_more = { version = "0.99.17", default-features = false, features = ["from", "display"] }
1112
event-manager = "0.3.0"
@@ -15,7 +16,6 @@ lazy_static = "1.4.0"
1516
libc = "0.2.117"
1617
linux-loader = "0.8.1"
1718
log = "0.4.17"
18-
rand = "0.8.5"
1919
serde = { version = "1.0.136", features = ["derive"] }
2020
serde_json = "1.0.78"
2121
timerfd = "1.2.0"

src/vmm/src/devices/virtio/rng/device.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ use std::io;
55
use std::sync::atomic::AtomicUsize;
66
use std::sync::Arc;
77

8+
use aws_lc_rs::rand;
89
use logger::{debug, error, IncMetric, METRICS};
9-
use rand::rngs::OsRng;
10-
use rand::RngCore;
1110
use rate_limiter::{RateLimiter, TokenType};
1211
use utils::eventfd::EventFd;
1312
use utils::vm_memory::{GuestMemoryError, GuestMemoryMmap};
@@ -28,7 +27,7 @@ pub enum Error {
2827
#[error("Bad guest memory buffer: {0}")]
2928
GuestMemory(#[from] GuestMemoryError),
3029
#[error("Could not get random bytes: {0}")]
31-
Random(#[from] rand::Error),
30+
Random(#[from] aws_lc_rs::error::Unspecified),
3231
}
3332

3433
type Result<T> = std::result::Result<T, Error>;
@@ -105,7 +104,7 @@ impl Entropy {
105104

106105
fn handle_one(&self, iovec: &mut IoVecBufferMut) -> Result<u32> {
107106
let mut rand_bytes = vec![0; iovec.len()];
108-
OsRng.try_fill_bytes(&mut rand_bytes).map_err(|err| {
107+
rand::fill(&mut rand_bytes).map_err(|err| {
109108
METRICS.entropy.host_rng_fails.inc();
110109
err
111110
})?;

0 commit comments

Comments
 (0)