Skip to content

openvmm: a microVM network prefix of 0 or over 32 panics config validation #1372

Closed

Description

@tangruize

A VM worker whose launch message carries a microVM network identity with an IPv4 prefix of 0 or above 32 panics in validate_machine_config in a debug build (the dev profile aborts). A release build does not panic: it derives a wrong netmask (255.255.255.255 for /0, 128.0.0.0 for /33) and accepts a command line whose virtnet_mask= matches it. Prefixes /31 and /32 are accepted too. Only a launch message that OpenVMM's controller did not build can carry such a prefix: the CLI and the snapshot restore both parse the identity with MicrovmNetworkConfig's FromStr, which accepts only /1 to /30.

Reproduction

Build a microVM Config with the virtio-net device and an identity parsed from 10.0.0.2/24, set prefix_length = 0, and call validate_machine_config. It should return an error; in a debug build it panics with "attempt to shift left with overflow" in MicrovmNetworkConfig::netmask. With 33 it panics with "attempt to subtract with overflow".

Cause

MicrovmNetworkConfig derives MeshPayload, so the worker receives prefix_length unchecked. validate_machine_config calls validate_microvm_command_line, which builds the expected tokens with command_line_fragment_with, which calls netmask(). Nothing checks the prefix first:

// openvmm/openvmm_defs/src/microvm.rs
impl MicrovmNetworkConfig {
    /// Returns the subnet mask derived from `prefix_length`.
    pub fn netmask(&self) -> std::net::Ipv4Addr {
        std::net::Ipv4Addr::from(u32::MAX << (32 - self.prefix_length))
    }
// ...
pub fn validate_machine_config(config: &Config, hypervisor_id: Option<&str>) -> anyhow::Result<()> {
    // ...
    validate_microvm_virtio_reservations()?;
    validate_microvm_command_line(config)?;

Fix

fix.patch adds validate_microvm_network_prefix, which validate_machine_config calls before the command-line check. It rejects a prefix outside 1..=30, the range the CLI parser accepts. The patch leaves netmask unchanged: checked arithmetic there would still let the worker accept an identity with no valid prefix. The test a_network_prefix_outside_1_to_30_is_rejected_before_the_command_line checks that 0, 31, 32, 33 and 255 are rejected and that every parsed /1 to /30 identity passes.

Activity

  1. tangruize commented on Nov 8, 2026

    @tangruize
    ContributorAuthor

    Proposed patch

    Patch against OpenVMM 3cf58a5234ed824435825c9f87187f5129219724. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/openvmm/openvmm_defs/src/microvm.rs b/openvmm/openvmm_defs/src/microvm.rs
    index bb8a07e..11d0c30 100644
    --- a/openvmm/openvmm_defs/src/microvm.rs
    +++ b/openvmm/openvmm_defs/src/microvm.rs
    @@ -1433,6 +1433,19 @@ fn microvm_command_line_ingress(
         }
     }
     
    +/// Checks that a network identity's IPv4 prefix is one the CLI parser
    +/// accepts, /1 to /30. An identity decoded from a mesh message is not checked
    +/// by [`MicrovmNetworkConfig`]'s `FromStr`, and the command-line check derives
    +/// its netmask, which overflows for a prefix of 0 or above 32.
    +fn validate_microvm_network_prefix(network: &MicrovmNetworkConfig) -> anyhow::Result<()> {
    +    anyhow::ensure!(
    +        (1..=30).contains(&network.prefix_length),
    +        "microVM static network prefix /{} is outside the supported range /1 through /30",
    +        network.prefix_length
    +    );
    +    Ok(())
    +}
    +
     fn validate_microvm_command_line(config: &Config) -> anyhow::Result<()> {
         let MachineProfile::Microvm = config.machine_profile else {
             unreachable!("microVM command-line validation requires the microVM profile");
    @@ -1929,6 +1942,9 @@ pub fn validate_machine_config(config: &Config, hypervisor_id: Option<&str>) ->
         };
     
         validate_microvm_virtio_reservations()?;
    +    if let Some(network) = &config.microvm.network {
    +        validate_microvm_network_prefix(network)?;
    +    }
         validate_microvm_command_line(config)?;
         validate_machine_load_mode(config.machine_profile, &config.load_mode)?;
         anyhow::ensure!(
    @@ -2400,6 +2416,30 @@ fn microvm_command_line_limit_includes_the_profile_tokens() {
             assert_eq!(discover(&mut over).unwrap_err().to_string(), limit_error());
         }
     
    +    #[test]
    +    fn a_network_prefix_outside_1_to_30_is_rejected_before_the_command_line() {
    +        let network: MicrovmNetworkConfig = "10.0.0.2/24".parse().unwrap();
    +        for prefix_length in [0, 31, 32, 33, 255] {
    +            let network = MicrovmNetworkConfig {
    +                prefix_length,
    +                ..network.clone()
    +            };
    +            assert!(
    +                validate_microvm_network_prefix(&network)
    +                    .unwrap_err()
    +                    .to_string()
    +                    .contains("outside the supported range"),
    +                "/{prefix_length}"
    +            );
    +        }
    +        for prefix_length in 1..=30 {
    +            let network: MicrovmNetworkConfig =
    +                format!("10.0.0.2/{prefix_length}").parse().unwrap();
    +            validate_microvm_network_prefix(&network).unwrap();
    +            let _ = network.netmask();
    +        }
    +    }
    +
         #[test]
         fn microvm_network_identity_has_portable_profile() {
             let network: MicrovmNetworkConfig = "10.0.0.2/24".parse().unwrap();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions