diff --git a/Guide/src/reference/backends/consomme.md b/Guide/src/reference/backends/consomme.md index 126718694e3..337b04fcbd1 100644 --- a/Guide/src/reference/backends/consomme.md +++ b/Guide/src/reference/backends/consomme.md @@ -109,11 +109,13 @@ own flows come from the address and port that the guest addressed, such as the gateway, as connected UDP sockets require. Consomme presents other host peers to the guest at virtual addresses inside the guest's subnet of their family, allocated downward from its high end, so that the guest answers them -from its address in that subnet. The endpoint's egress policy evaluates the -guest's traffic to such an address at the host address to which Consomme -routes it, and for host loopback at the gateway of its family, so a peer's -virtual address reaches no host-loopback port that the rules deny at the -gateway. Without ingress `allow`, OpenVMM appends +from its address in that subnet. OpenVMM therefore requires an IPv4 network of +`/29` or larger for explicit `--host-loopback allow`: a `/30` holds only the +gateway and the guest and leaves no address for host peers. The endpoint's +egress policy evaluates the guest's traffic to such an address at the host +address to which Consomme routes it, and for host loopback at the gateway of +its family, so a peer's virtual address reaches no host-loopback port that the +rules deny at the gateway. Without ingress `allow`, OpenVMM appends `virtnet_ingress=host-loopback` to the microVM command line, and the guest announces its listeners as it does for ingress `allow`. The endpoint keeps one host listener per announced port and address family, bound to the family's diff --git a/Guide/src/reference/openvmm/management/cli.md b/Guide/src/reference/openvmm/management/cli.md index 11bda46d1e3..f5ef62c5dea 100644 --- a/Guide/src/reference/openvmm/management/cli.md +++ b/Guide/src/reference/openvmm/management/cli.md @@ -214,10 +214,15 @@ describes the source definitions. loopback at each announced port, never displacing a host listener that already holds the port. Replies to the host pass regardless of the egress policy. With `--network-ingress allow`, the listeners published to the - host's private networks admit the host instead. Snapshot capture rejects - explicit `allow`, because its listeners are live host attachments, and so - does a restore, unless it installs a new policy with - `--restore-network-policy`. + host's private networks admit the host instead. Explicit `allow` requires an + IPv4 network of `/29` or larger: Consomme presents host peers to the guest + at addresses of the guest's subnet, which a `/30`, holding only the gateway + and the guest, cannot spare. OpenVMM rejects explicit `allow` on a `/30` + before it opens host resources, and at a restore with + `--restore-network-policy` of a NIC captured on one, before any vCPU runs. + Snapshot capture rejects explicit `allow`, because its listeners are live + host attachments, and so does a restore, unless it installs a new policy + with `--restore-network-policy`. Omitting the option preserves the existing guest-gateway-to-host-loopback mapping, from the IPv4 gateway to host IPv4 loopback and from the IPv6 gateway to host IPv6 loopback, without publishing guest ports. `deny` blocks diff --git a/openvmm/openvmm_entry/src/cli_args/microvm.rs b/openvmm/openvmm_entry/src/cli_args/microvm.rs index 3900b794681..bad1f86deaa 100644 --- a/openvmm/openvmm_entry/src/cli_args/microvm.rs +++ b/openvmm/openvmm_entry/src/cli_args/microvm.rs @@ -429,7 +429,9 @@ pub struct MicrovmCli { /// replies to the host pass regardless of the egress policy. With /// `--network-ingress allow`, the listeners published to the host's /// private networks admit the host instead. Explicit forwards may publish - /// further ports. Snapshot capture does not support explicit `allow`, and + /// further ports. Explicit `allow` requires an IPv4 network of /29 or + /// larger, whose subnet leaves addresses at which host peers reach the + /// guest. Snapshot capture does not support explicit `allow`, and /// a restore supports it only with `--restore-network-policy`. #[clap(long, value_enum, value_name = "ACTION")] pub host_loopback: Option, @@ -754,6 +756,12 @@ pub(super) fn parse_endpoint(network: &str) -> Result .map_err(|error| format!("invalid microVM network: {error}")) } +/// Longest IPv4 prefix of a microVM network that explicit `--host-loopback +/// allow` accepts. Consomme presents host peers to the guest at addresses of +/// the guest's subnet, allocated downward from its last usable address, which +/// a /30, holding only the gateway and the guest, cannot spare. +const MICROVM_HOST_LOOPBACK_MAX_PREFIX_LENGTH: u8 = 29; + impl Options { /// Rejects unsupported microVM combinations before opening host resources. pub(crate) fn validate_microvm_host_loopback(&self) -> anyhow::Result<()> { @@ -1391,13 +1399,16 @@ impl Options { Ok(()) } + /// Binds the network policy of these options to `network`: the identity + /// that `--net` names, or at a restore the one that the snapshot recorded. + /// + /// Refuses explicit `--host-loopback allow` on a network whose prefix is + /// longer than [`MICROVM_HOST_LOOPBACK_MAX_PREFIX_LENGTH`], which leaves no + /// address at which Consomme can present host peers to the guest. pub(crate) fn microvm_egress_policy( &self, network: &openvmm_defs::microvm::MicrovmNetworkConfig, - ) -> Result< - net_backend_resources::egress::EgressPolicy, - net_backend_resources::egress::InvalidEgressPolicy, - > { + ) -> anyhow::Result { use net_backend_resources::egress::EgressPolicyMode; let mode = if !self.microvm.network_egress_allow.is_empty() @@ -1423,7 +1434,7 @@ impl Options { } else { EgressPolicyMode::AllowAll }; - network.bind_egress_policy(mode).and_then(|policy| { + let policy = network.bind_egress_policy(mode).and_then(|policy| { Ok(policy .with_host_loopback( self.microvm @@ -1444,7 +1455,15 @@ impl Options { .unwrap_or(MicrovmNetworkActionCli::Deny) .into(), )) - }) + })?; + // Binding the policy has checked the prefix, so a longer one is a /30. + anyhow::ensure!( + self.microvm.host_loopback != Some(MicrovmNetworkActionCli::Allow) + || network.prefix_length <= MICROVM_HOST_LOOPBACK_MAX_PREFIX_LENGTH, + "--host-loopback allow requires an IPv4 network of /{MICROVM_HOST_LOOPBACK_MAX_PREFIX_LENGTH} or larger: a /{} holds only the gateway and the guest, and leaves no address at which to present host peers to the guest", + network.prefix_length + ); + Ok(policy) } /// Returns the publication of the guest's listeners to the host's private @@ -2595,6 +2614,73 @@ mod tests { forwarded.validate_microvm_options().unwrap(); } + #[cfg(guest_arch = "x86_64")] + #[test] + fn test_microvm_host_loopback_allow_needs_an_address_for_host_peers() { + let parse = |arguments: &[&str]| { + Options::try_parse_from( + [ + "openvmm", + "--machine", + "microvm", + "--network-profile", + "portable", + ] + .into_iter() + .chain(arguments.iter().copied()), + ) + .unwrap() + }; + // Consomme presents host peers at addresses of the guest's subnet, + // which a /30, holding only the gateway and the guest, cannot spare, + // whether the host reaches the guest's listeners on loopback, through + // private-network ingress, or through forwards. + let slash_30: openvmm_defs::microvm::MicrovmNetworkConfig = "10.0.0.2/30".parse().unwrap(); + for extra in [ + &[][..], + &["--network-egress", "deny"][..], + &["--network-ingress", "allow"][..], + &["--host-loopback-forward", "udp:3000:8080"][..], + ] { + let options = parse( + &[ + &["--net", "10.0.0.2/30", "--host-loopback", "allow"][..], + extra, + ] + .concat(), + ); + for error in [ + options.validate_microvm_options().unwrap_err(), + options.microvm_egress_policy(&slash_30).unwrap_err(), + ] { + assert!( + error.to_string().contains( + "--host-loopback allow requires an IPv4 network of /29 or larger" + ), + "{extra:?}: {error:#}" + ); + } + } + // A /29 leaves four addresses for host peers. + let options = parse(&["--net", "10.0.0.2/29", "--host-loopback", "allow"]); + options.validate_microvm_options().unwrap(); + options + .microvm_egress_policy(&"10.0.0.2/29".parse().unwrap()) + .unwrap(); + // Without explicit host-loopback access, no host peer reaches the + // guest at a synthetic address, so a /30 serves every other policy. + for extra in [ + &[][..], + &["--host-loopback", "deny"][..], + &["--network-ingress", "allow"][..], + &["--network-ingress", "allow", "--host-loopback", "deny"][..], + ] { + let options = parse(&[&["--net", "10.0.0.2/30"][..], extra].concat()); + options.validate_microvm_options().unwrap(); + options.microvm_egress_policy(&slash_30).unwrap(); + } + } + #[cfg(guest_arch = "x86_64")] #[test] fn test_microvm_egress_policy_is_typed_and_canonical() { diff --git a/openvmm/openvmm_entry/src/microvm/network.rs b/openvmm/openvmm_entry/src/microvm/network.rs index 77cd6404838..db84401bd24 100644 --- a/openvmm/openvmm_entry/src/microvm/network.rs +++ b/openvmm/openvmm_entry/src/microvm/network.rs @@ -728,6 +728,36 @@ pub(crate) mod tests { let error = effective_microvm_network(&readdressed, Some(&contract)).unwrap_err(); assert!(error.to_string().contains("from saved state"), "{error:#}"); + // A template captured on a /30 cannot give its clones explicit + // host-loopback access, which a cold boot on a /30 cannot have, but + // takes every other policy. + let slash_30: openvmm_defs::microvm::MicrovmNetworkConfig = "10.0.0.2/30".parse().unwrap(); + let contract = template_contract_for(&slash_30); + for arguments in [ + &["--host-loopback", "allow"][..], + &["--network-ingress", "allow", "--host-loopback", "allow"][..], + ] { + let options = restore_options(&[&["--restore-network-policy"][..], arguments].concat()); + let error = effective_microvm_network(&options, Some(&contract)).unwrap_err(); + assert!( + error + .to_string() + .contains("--host-loopback allow requires an IPv4 network of /29 or larger"), + "{arguments:?}: {error:#}" + ); + } + for arguments in [ + &["--network-egress", "allow"][..], + &["--network-ingress", "allow"][..], + &["--network-egress", "deny", "--host-loopback", "deny"][..], + ] { + let options = restore_options(&[&["--restore-network-policy"][..], arguments].concat()); + let restored = effective_microvm_network(&options, Some(&contract)) + .unwrap() + .unwrap(); + assert_eq!(restored.config, slash_30, "{arguments:?}"); + } + // A template whose network predates IPv6 keeps no IPv6 identity. let ipv4_only = network.without_ipv6(); let contract = template_contract_for(&ipv4_only);