diff --git a/Cargo.lock b/Cargo.lock index a24b213eb7f0..ce48d16b53c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1420,6 +1420,7 @@ dependencies = [ "mmds", "net_gen", "proptest", + "rand", "rate_limiter", "seccompiler", "serde", diff --git a/src/firecracker/Cargo.toml b/src/firecracker/Cargo.toml index 8cd6e1e6f55c..51f9096bdb47 100644 --- a/src/firecracker/Cargo.toml +++ b/src/firecracker/Cargo.toml @@ -8,6 +8,11 @@ description = "Firecracker enables you to deploy workloads in lightweight virtua homepage = "https://firecracker-microvm.github.io/" license = "Apache-2.0" +[features] +default = ["aws-lc-rs"] +aws-lc-rs = ["vmm/aws-lc-rs"] +rand = ["vmm/rand"] + [dependencies] event-manager = "0.3.0" libc = "0.2.117" @@ -20,7 +25,7 @@ mmds = { path = "../mmds" } seccompiler = { path = "../seccompiler" } snapshot = { path = "../snapshot"} utils = { path = "../utils" } -vmm = { path = "../vmm" } +vmm = { path = "../vmm", default-features = false } [dev-dependencies] cargo_toml = "0.13.0" diff --git a/src/vmm/Cargo.toml b/src/vmm/Cargo.toml index a18a98e392c1..9a2b51ab70d1 100644 --- a/src/vmm/Cargo.toml +++ b/src/vmm/Cargo.toml @@ -5,8 +5,14 @@ authors = ["Amazon Firecracker team "] edition = "2021" license = "Apache-2.0" +[features] +default = [] +aws-lc-rs = ["dep:aws-lc-rs"] +rand = ["dep:rand"] + [dependencies] -aws-lc-rs = "1.0.2" +aws-lc-rs = { version = "1.0.2", optional = true } +rand = { version = "0.8.5", optional = true } bitflags = "2.0.2" derive_more = { version = "0.99.17", default-features = false, features = ["from", "display"] } event-manager = "0.3.0" diff --git a/src/vmm/src/devices/virtio/rng/device.rs b/src/vmm/src/devices/virtio/rng/device.rs index 1d9cc34f089b..3ab37eac842c 100644 --- a/src/vmm/src/devices/virtio/rng/device.rs +++ b/src/vmm/src/devices/virtio/rng/device.rs @@ -5,7 +5,11 @@ use std::io; use std::sync::atomic::AtomicUsize; use std::sync::Arc; -use aws_lc_rs::rand; +#[cfg(feature = "aws-lc-rs")] +use aws_lc_rs::{error::Unspecified as RandomError, rand}; +#[cfg(feature = "rand")] +use rand::{rngs::OsRng, Error as RandomError, RngCore}; + use logger::{debug, error, IncMetric, METRICS}; use rate_limiter::{RateLimiter, TokenType}; use utils::eventfd::EventFd; @@ -27,7 +31,7 @@ pub enum Error { #[error("Bad guest memory buffer: {0}")] GuestMemory(#[from] GuestMemoryError), #[error("Could not get random bytes: {0}")] - Random(#[from] aws_lc_rs::error::Unspecified), + Random(#[from] RandomError), } type Result = std::result::Result; @@ -109,10 +113,17 @@ impl Entropy { } let mut rand_bytes = vec![0; iovec.len()]; + + #[cfg(feature = "aws-lc-rs")] rand::fill(&mut rand_bytes).map_err(|err| { METRICS.entropy.host_rng_fails.inc(); err })?; + #[cfg(feature = "rand")] + OsRng.try_fill_bytes(&mut rand_bytes).map_err(|err| { + METRICS.entropy.host_rng_fails.inc(); + err + })?; // It is ok to unwrap here. We are writing `iovec.len()` bytes at offset 0. Ok(iovec.write_at(&rand_bytes, 0).unwrap().try_into().unwrap())