From 83ab5ea5289d640a981823f0e7e635808e4cfc8a Mon Sep 17 00:00:00 2001 From: Bo Chen Date: Mon, 21 Nov 2022 16:17:36 -0800 Subject: [PATCH] virtio-devices: net: Provide custom functions for fuzzing Three functions are added: * 'Tap::new_for_fuzzing()' a custom constructor that creates a dummy `Tap` interface directly from `File` backed by Unix domain socket; * 'Tap::mtu()' a custom function that returns hard-coded mtu; * 'Net::wait_for_epoll_threads()'. Two functions are reused with modifications to work with the dummy 'Tap' interface: * 'Net::new_with_tap()' is made public for fuzzing; * 'Net::activate()' is modified to not call into 'Tap::set_offload()' for fuzzing. Signed-off-by: Bo Chen --- net_util/src/tap.rs | 12 ++++++++++++ virtio-devices/src/net.rs | 8 +++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/net_util/src/tap.rs b/net_util/src/tap.rs index 0bdf0e30f..c795dd8f0 100644 --- a/net_util/src/tap.rs +++ b/net_util/src/tap.rs @@ -305,6 +305,7 @@ impl Tap { unsafe { Self::ioctl_with_ref(&sock, net_gen::sockios::SIOCSIFNETMASK as c_ulong, &ifreq) } } + #[cfg(not(fuzzing))] pub fn mtu(&self) -> Result { let sock = create_unix_socket().map_err(Error::NetUtil)?; @@ -319,6 +320,12 @@ impl Tap { Ok(mtu) } + #[cfg(fuzzing)] + pub fn mtu(&self) -> Result { + // Consistent with the `virtio_devices::net::MIN_MTU` + Ok(1280) + } + pub fn set_mtu(&self, mtu: i32) -> Result<()> { let sock = create_unix_socket().map_err(Error::NetUtil)?; @@ -383,6 +390,11 @@ impl Tap { pub fn get_if_name(&self) -> Vec { self.if_name.clone() } + + #[cfg(fuzzing)] + pub fn new_for_fuzzing(tap_file: File, if_name: Vec) -> Self { + Tap { tap_file, if_name } + } } impl Read for Tap { diff --git a/virtio-devices/src/net.rs b/virtio-devices/src/net.rs index 7ce6e0d85..5ae12aab0 100644 --- a/virtio-devices/src/net.rs +++ b/virtio-devices/src/net.rs @@ -435,7 +435,7 @@ impl VersionMapped for NetState {} impl Net { /// Create a new virtio network device with the given TAP interface. #[allow(clippy::too_many_arguments)] - fn new_with_tap( + pub fn new_with_tap( id: String, taps: Vec, guest_mac: Option, @@ -623,6 +623,11 @@ impl Net { queue_size: self.common.queue_sizes.clone(), } } + + #[cfg(fuzzing)] + pub fn wait_for_epoll_threads(&mut self) { + self.common.wait_for_epoll_threads(); + } } impl Drop for Net { @@ -735,6 +740,7 @@ impl VirtioDevice for Net { .map_err(ActivateError::CreateRateLimiter)?; let tap = taps.remove(0); + #[cfg(not(fuzzing))] tap.set_offload(virtio_features_to_tap_offload(self.common.acked_features)) .map_err(|e| { error!("Error programming tap offload: {:?}", e);