Skip to content

Commit bec7094

Browse files
ppennaCopilot
andcommitted
microVM: reject explicit host-loopback access on a /30 IPv4 network
With explicit --host-loopback allow, Consomme presents host peers, such as the host clients of the guest's published listeners and of host-loopback forwards, at synthetic addresses inside the guest's subnet, allocated downward from its last usable address, so that the guest answers them from its own address through its NIC. A /30 IPv4 network holds only the gateway and the guest, so the allocation finds no address, and Consomme presents IPv4 host peers at the gateway instead. TCP still works, but the guest's UDP replies to host clients over 127.0.0.1 do not return through the published port, so the bidirectional access that the option promises silently loses IPv4 UDP. OpenVMM accepted the combination at launch without an error or a warning, and at run time only logged a rate-limited warning that its address pool was exhausted, both at a cold boot with --net and at a restore that installs a new policy on a NIC template captured on a /30, although invalid configurations are to be refused, never silently adjusted. Refuse explicit --host-loopback allow on an IPv4 network whose prefix is longer than /29 where the options bind their network policy to an identity, in microvm_egress_policy. Option validation calls it with the identity of --net before OpenVMM opens host resources, and the launch calls it with the snapshot's identity at a restore, before any vCPU runs; a check in validate_microvm_options alone would miss the restore, whose identity --net cannot name. The error names the option and the rule, and microvm_egress_policy now returns anyhow::Result, as its callers do. The IPv6 subnet of a /30, a /126, keeps an address for host peers, but its IPv4 peers would still have none. Every other policy on a /30, private-network ingress without explicit host loopback included, presents no host peer at a synthetic address and stays accepted. Consomme's fallback to the gateway when its pool runs out stays, for its other users and for more host peers than a subnet has addresses. Unit tests check that a /30 refuses explicit host loopback alone, with egress denied, with private-network ingress, and with a UDP forward, both in option validation and when the policy is bound; that a /29 accepts it; that a /30 accepts every other policy; and that a NIC template captured on a /30 refuses it at a restore with --restore-network-policy but takes other policies. The help of --host-loopback, the CLI reference, and the Consomme reference state the rule. See microsoft/nvx#553. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent bcfc47c commit bec7094

4 files changed

Lines changed: 140 additions & 16 deletions

File tree

‎Guide/src/reference/backends/consomme.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ own flows come from the address and port that the guest addressed, such as the
109109
gateway, as connected UDP sockets require. Consomme presents other host
110110
peers to the guest at virtual addresses inside the guest's subnet of their
111111
family, allocated downward from its high end, so that the guest answers them
112-
from its address in that subnet. The endpoint's egress policy evaluates the
113-
guest's traffic to such an address at the host address to which Consomme
114-
routes it, and for host loopback at the gateway of its family, so a peer's
115-
virtual address reaches no host-loopback port that the rules deny at the
116-
gateway. Without ingress `allow`, OpenVMM appends
112+
from its address in that subnet. OpenVMM therefore requires an IPv4 network of
113+
`/29` or larger for explicit `--host-loopback allow`: a `/30` holds only the
114+
gateway and the guest and leaves no address for host peers. The endpoint's
115+
egress policy evaluates the guest's traffic to such an address at the host
116+
address to which Consomme routes it, and for host loopback at the gateway of
117+
its family, so a peer's virtual address reaches no host-loopback port that the
118+
rules deny at the gateway. Without ingress `allow`, OpenVMM appends
117119
`virtnet_ingress=host-loopback` to the microVM command line, and the guest
118120
announces its listeners as it does for ingress `allow`. The endpoint keeps one
119121
host listener per announced port and address family, bound to the family's

‎Guide/src/reference/openvmm/management/cli.md‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,15 @@ describes the source definitions.
214214
loopback at each announced port, never displacing a host listener that
215215
already holds the port. Replies to the host pass regardless of the egress
216216
policy. With `--network-ingress allow`, the listeners published to the
217-
host's private networks admit the host instead. Snapshot capture rejects
218-
explicit `allow`, because its listeners are live host attachments, and so
219-
does a restore, unless it installs a new policy with
220-
`--restore-network-policy`.
217+
host's private networks admit the host instead. Explicit `allow` requires an
218+
IPv4 network of `/29` or larger: Consomme presents host peers to the guest
219+
at addresses of the guest's subnet, which a `/30`, holding only the gateway
220+
and the guest, cannot spare. OpenVMM rejects explicit `allow` on a `/30`
221+
before it opens host resources, and at a restore with
222+
`--restore-network-policy` of a NIC captured on one, before any vCPU runs.
223+
Snapshot capture rejects explicit `allow`, because its listeners are live
224+
host attachments, and so does a restore, unless it installs a new policy
225+
with `--restore-network-policy`.
221226
Omitting the option preserves the existing guest-gateway-to-host-loopback
222227
mapping, from the IPv4 gateway to host IPv4 loopback and from the IPv6
223228
gateway to host IPv6 loopback, without publishing guest ports. `deny` blocks

‎openvmm/openvmm_entry/src/cli_args/microvm.rs‎

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,9 @@ pub struct MicrovmCli {
429429
/// replies to the host pass regardless of the egress policy. With
430430
/// `--network-ingress allow`, the listeners published to the host's
431431
/// private networks admit the host instead. Explicit forwards may publish
432-
/// further ports. Snapshot capture does not support explicit `allow`, and
432+
/// further ports. Explicit `allow` requires an IPv4 network of /29 or
433+
/// larger, whose subnet leaves addresses at which host peers reach the
434+
/// guest. Snapshot capture does not support explicit `allow`, and
433435
/// a restore supports it only with `--restore-network-policy`.
434436
#[clap(long, value_enum, value_name = "ACTION")]
435437
pub host_loopback: Option<MicrovmNetworkActionCli>,
@@ -754,6 +756,12 @@ pub(super) fn parse_endpoint(network: &str) -> Result<EndpointConfigCli, String>
754756
.map_err(|error| format!("invalid microVM network: {error}"))
755757
}
756758

759+
/// Longest IPv4 prefix of a microVM network that explicit `--host-loopback
760+
/// allow` accepts. Consomme presents host peers to the guest at addresses of
761+
/// the guest's subnet, allocated downward from its last usable address, which
762+
/// a /30, holding only the gateway and the guest, cannot spare.
763+
const MICROVM_HOST_LOOPBACK_MAX_PREFIX_LENGTH: u8 = 29;
764+
757765
impl Options {
758766
/// Rejects unsupported microVM combinations before opening host resources.
759767
pub(crate) fn validate_microvm_host_loopback(&self) -> anyhow::Result<()> {
@@ -1391,13 +1399,16 @@ impl Options {
13911399
Ok(())
13921400
}
13931401

1402+
/// Binds the network policy of these options to `network`: the identity
1403+
/// that `--net` names, or at a restore the one that the snapshot recorded.
1404+
///
1405+
/// Refuses explicit `--host-loopback allow` on a network whose prefix is
1406+
/// longer than [`MICROVM_HOST_LOOPBACK_MAX_PREFIX_LENGTH`], which leaves no
1407+
/// address at which Consomme can present host peers to the guest.
13941408
pub(crate) fn microvm_egress_policy(
13951409
&self,
13961410
network: &openvmm_defs::microvm::MicrovmNetworkConfig,
1397-
) -> Result<
1398-
net_backend_resources::egress::EgressPolicy,
1399-
net_backend_resources::egress::InvalidEgressPolicy,
1400-
> {
1411+
) -> anyhow::Result<net_backend_resources::egress::EgressPolicy> {
14011412
use net_backend_resources::egress::EgressPolicyMode;
14021413

14031414
let mode = if !self.microvm.network_egress_allow.is_empty()
@@ -1423,7 +1434,7 @@ impl Options {
14231434
} else {
14241435
EgressPolicyMode::AllowAll
14251436
};
1426-
network.bind_egress_policy(mode).and_then(|policy| {
1437+
let policy = network.bind_egress_policy(mode).and_then(|policy| {
14271438
Ok(policy
14281439
.with_host_loopback(
14291440
self.microvm
@@ -1444,7 +1455,15 @@ impl Options {
14441455
.unwrap_or(MicrovmNetworkActionCli::Deny)
14451456
.into(),
14461457
))
1447-
})
1458+
})?;
1459+
// Binding the policy has checked the prefix, so a longer one is a /30.
1460+
anyhow::ensure!(
1461+
self.microvm.host_loopback != Some(MicrovmNetworkActionCli::Allow)
1462+
|| network.prefix_length <= MICROVM_HOST_LOOPBACK_MAX_PREFIX_LENGTH,
1463+
"--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",
1464+
network.prefix_length
1465+
);
1466+
Ok(policy)
14481467
}
14491468

14501469
/// Returns the publication of the guest's listeners to the host's private
@@ -2595,6 +2614,73 @@ mod tests {
25952614
forwarded.validate_microvm_options().unwrap();
25962615
}
25972616

2617+
#[cfg(guest_arch = "x86_64")]
2618+
#[test]
2619+
fn test_microvm_host_loopback_allow_needs_an_address_for_host_peers() {
2620+
let parse = |arguments: &[&str]| {
2621+
Options::try_parse_from(
2622+
[
2623+
"openvmm",
2624+
"--machine",
2625+
"microvm",
2626+
"--network-profile",
2627+
"portable",
2628+
]
2629+
.into_iter()
2630+
.chain(arguments.iter().copied()),
2631+
)
2632+
.unwrap()
2633+
};
2634+
// Consomme presents host peers at addresses of the guest's subnet,
2635+
// which a /30, holding only the gateway and the guest, cannot spare,
2636+
// whether the host reaches the guest's listeners on loopback, through
2637+
// private-network ingress, or through forwards.
2638+
let slash_30: openvmm_defs::microvm::MicrovmNetworkConfig = "10.0.0.2/30".parse().unwrap();
2639+
for extra in [
2640+
&[][..],
2641+
&["--network-egress", "deny"][..],
2642+
&["--network-ingress", "allow"][..],
2643+
&["--host-loopback-forward", "udp:3000:8080"][..],
2644+
] {
2645+
let options = parse(
2646+
&[
2647+
&["--net", "10.0.0.2/30", "--host-loopback", "allow"][..],
2648+
extra,
2649+
]
2650+
.concat(),
2651+
);
2652+
for error in [
2653+
options.validate_microvm_options().unwrap_err(),
2654+
options.microvm_egress_policy(&slash_30).unwrap_err(),
2655+
] {
2656+
assert!(
2657+
error.to_string().contains(
2658+
"--host-loopback allow requires an IPv4 network of /29 or larger"
2659+
),
2660+
"{extra:?}: {error:#}"
2661+
);
2662+
}
2663+
}
2664+
// A /29 leaves four addresses for host peers.
2665+
let options = parse(&["--net", "10.0.0.2/29", "--host-loopback", "allow"]);
2666+
options.validate_microvm_options().unwrap();
2667+
options
2668+
.microvm_egress_policy(&"10.0.0.2/29".parse().unwrap())
2669+
.unwrap();
2670+
// Without explicit host-loopback access, no host peer reaches the
2671+
// guest at a synthetic address, so a /30 serves every other policy.
2672+
for extra in [
2673+
&[][..],
2674+
&["--host-loopback", "deny"][..],
2675+
&["--network-ingress", "allow"][..],
2676+
&["--network-ingress", "allow", "--host-loopback", "deny"][..],
2677+
] {
2678+
let options = parse(&[&["--net", "10.0.0.2/30"][..], extra].concat());
2679+
options.validate_microvm_options().unwrap();
2680+
options.microvm_egress_policy(&slash_30).unwrap();
2681+
}
2682+
}
2683+
25982684
#[cfg(guest_arch = "x86_64")]
25992685
#[test]
26002686
fn test_microvm_egress_policy_is_typed_and_canonical() {

‎openvmm/openvmm_entry/src/microvm/network.rs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,37 @@ pub(crate) mod tests {
728728
let error = effective_microvm_network(&readdressed, Some(&contract)).unwrap_err();
729729
assert!(error.to_string().contains("from saved state"), "{error:#}");
730730

731+
// A template captured on a /30 cannot give its clones explicit
732+
// host-loopback access, which a cold boot on a /30 cannot have, but
733+
// takes every other policy.
734+
let slash_30: openvmm_defs::microvm::MicrovmNetworkConfig = "10.0.0.2/30".parse().unwrap();
735+
let contract = template_contract_for(&slash_30);
736+
for arguments in [
737+
&["--host-loopback", "allow"][..],
738+
&["--network-ingress", "allow", "--host-loopback", "allow"][..],
739+
] {
740+
let options = restore_options(&[&["--restore-network-policy"][..], arguments].concat());
741+
options.validate_microvm_options().unwrap();
742+
let error = effective_microvm_network(&options, Some(&contract)).unwrap_err();
743+
assert!(
744+
error
745+
.to_string()
746+
.contains("--host-loopback allow requires an IPv4 network of /29 or larger"),
747+
"{arguments:?}: {error:#}"
748+
);
749+
}
750+
for arguments in [
751+
&["--network-egress", "allow"][..],
752+
&["--network-ingress", "allow"][..],
753+
&["--network-egress", "deny", "--host-loopback", "deny"][..],
754+
] {
755+
let options = restore_options(&[&["--restore-network-policy"][..], arguments].concat());
756+
let restored = effective_microvm_network(&options, Some(&contract))
757+
.unwrap()
758+
.unwrap();
759+
assert_eq!(restored.config, slash_30, "{arguments:?}");
760+
}
761+
731762
// A template whose network predates IPv6 keeps no IPv6 identity.
732763
let ipv4_only = network.without_ipv6();
733764
let contract = template_contract_for(&ipv4_only);

0 commit comments

Comments
 (0)