Skip to content

Commit b107c09

Browse files
ppennaCopilot
andcommitted
sandbox: record the network's options and build its policy at each start
The sandbox's network was `host::sandbox::SandboxNetwork`, one of the old config types: `sandbox_network` returned it, the launch carried it, and `config.json` stored it in the serde form of the old plan's network, from which `start` read it back. `nvx-rs run` and `start` converted it into the `NetworkConfig` of `host::config` that their VMs take. `sandbox_network` now returns that `NetworkConfig`, after the checks that `SandboxNetwork::new` made: the guest network, and that OpenVMM's options can express the policy, with `host::network::openvmm_arguments`. The configuration records the CLI's own network options in place of the policy, as a `NetworkRecord`: `--net`, `--network-egress`, `--network-ingress`, `--host-loopback`, `--network-proxy` and the `--host-loopback-forward` options as given, and the egress rules in OpenVMM's form, as the rule options gave them or a policy file compiled them, so that no start reads that file again. Each start, and the configuration that `sandbox run` writes and reads back, rebuilds the options with the parser's own receiver and builds the network from them with `sandbox_network`, as provision did. `provision` and `sandbox run` pass the record in `ProvisionConfig`, beside the other options that provision records. The checks of the network, their order and their messages do not change. The record requires every field, an option that was not given as `null`, and `Config::new` builds the network as it reads the record, so a record whose options the parser or `sandbox_network` refuses fails as `sandbox configuration is malformed` when read, as the old plan's network did. The record changes form, so the configuration moves to format 10. Nothing in this workstream is deployed, so format 9 is refused rather than migrated, as format 8 was, and the refusal of an unsupported format now also says to deprovision the sandbox and provision it again. This is the second step of SL3.10b, whose split was approved once its measured scope came to about ten times its estimate: SL3.10d moves the launch off `SandboxLaunch`, `SandboxSettings` and `SandboxUpper`. Tests: the CLI's lifecycle cases of a network with a policy file, host loopback and forwards, and of a proxy-only network, check the new record, and their starts still launch OpenVMM with the same network options; `old_configuration_formats_are_refused_without_migration` refuses a format-9 configuration with the advice, which it lacked before; the format checks of `malformed_configuration`, `configurations_of_every_format_start_and_exec` and the `sandbox-ram-upper` scenario take format 10, as does the fake `nvx-rs` of that scenario's tests, whose old format is now 9, and that of the `sandbox-gpt-layer` scenario's tests; `malformed_configuration` also refuses network records without a field, of a gateway address, with another egress or with a broken rule as malformed, all of which the record would otherwise read or refuse later; and the restore test of `run`'s network options takes the `NetworkConfig` directly. The case matrix's row of the `sandbox-ram-upper` scenario says that it checks format 10. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 07c508c commit b107c09

16 files changed

Lines changed: 240 additions & 116 deletions

File tree

‎doc/acceptance-case-matrix.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ suite in its shards, and the public sandbox scenarios in its guest and sandbox j
340340
| `sandbox-gpt-layer` | kept | The [cold cells](#cold-boots) of a GPT disk, over a scratch upper and over a RAM upper, through `run`, and `provision` and `start` |
341341
| `sandbox-lifecycle` | kept | A [cold cell](#cold-boots): the EROFS layer over a scratch upper, through `provision` and `start`, which also reattach to the VM from a new process for each command |
342342
| `sandbox-proxy-only` | kept | Its proxy-only network, over the EROFS layer and a scratch upper; it is no [cold cell](#cold-boots) of its own |
343-
| `sandbox-ram-upper` | kept | A [cold cell](#cold-boots): the EROFS layer over a RAM upper, through `run`, and `provision` and `start` |
343+
| `sandbox-ram-upper` | kept | A [cold cell](#cold-boots): the EROFS layer over a RAM upper, through `run`, and `provision` and `start`. Since SL3.10c, it checks that `provision` records the configuration's format 10 |
344344
| `scratch-snapshot` | kept | |
345345
| `smp` | kept | |
346346
| `smp-lapic` | kept | Runs only when a run names it |

‎doc/run.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ inside a writable part of a share. On Windows, it refuses a path that only a
12931293
verbatim `\\?\` path names, such as that of a directory whose name ends in a
12941294
period or a space, which the path without the prefix would not name. The
12951295
shares' children follow their resolved host paths. A managed sandbox stores
1296-
the plan in its typed configuration, in format 9. Each `start` checks it again,
1296+
the plan in its typed configuration, in format 10. Each `start` checks it again,
12971297
as `run` does once it has verified the distro layer: it refuses a host path
12981298
that no longer holds a directory or regular file as recorded, a recorded path
12991299
whose file identity changed, and shares whose host paths came to overlap, such

‎nvx-rs/README.md‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,15 +2879,16 @@ The state directory is the directory of the sandbox's
28792879
sandbox shares, and `config.json` is the CLI's own record beside them. It also
28802880
records the [distro binding](#distro-bindings) and the
28812881
[scratch binding](#checked-handoff) of the sandbox, and its network as the
2882-
typed `SandboxNetwork`, in the serde form of the plan's network. The typed
2883-
configuration names one EROFS or GPT image by path and identity, and a scratch
2884-
upper by path or a RAM upper with its cap from 1 through 90 percent, 50 by
2885-
default. `nvx-rs` refuses earlier configuration formats rather than migrating
2886-
them:
2882+
network options from which each `start` builds the policy again, as `provision`
2883+
built it. The typed configuration names one EROFS or GPT image by path and
2884+
identity, and a scratch upper by path or a RAM upper with its cap from 1 through
2885+
90 percent, 50 by default. `nvx-rs` refuses earlier configuration formats, such
2886+
as format 9, rather than migrating them, and says to deprovision the sandbox and
2887+
provision it again:
28872888

28882889
| File | Contents |
28892890
| --- | --- |
2890-
| `config.json` | The provisioned sandbox, in format 9 only: `image` holds its format, path and identity; `upper` is tagged as scratch with its path or RAM with `max_memory_percent`. `distro_binding` binds the image, and `scratch_binding` binds scratch or is `null` for RAM. The export and network are typed, `null` when absent. A sandbox that `snapshot restore` provisioned also records its snapshot's name under `source_snapshot`, the absolute path of the snapshot's store under `source_store`, and the program and arguments of the workload that each restore runs under `workload`; one that boots cold records none of them. An atomic pretty-JSON write uses mode 0600, and start reads the same strict typed fields without a legacy reader. |
2891+
| `config.json` | The provisioned sandbox, in format 10 only: `image` holds its format, path and identity; `upper` is tagged as scratch with its path or RAM with `max_memory_percent`. `distro_binding` binds the image, and `scratch_binding` binds scratch or is `null` for RAM. The export is typed, `null` when absent. `network`, `null` without `--net`, holds `--net`, `--network-egress`, `--network-ingress`, `--host-loopback`, `--network-proxy`, each `null` when not given, and the `--host-loopback-forward` options, as given, with the egress rules in OpenVMM's form, from a policy file's compile if one was given. A sandbox that `snapshot restore` provisioned also records its snapshot's name under `source_snapshot`, the absolute path of the snapshot's store under `source_store`, and the program and arguments of the workload that each restore runs under `workload`; one that boots cold records none of them. An atomic pretty-JSON write uses mode 0600, and start reads the same strict typed fields without a legacy reader. |
28912892
| `sandbox.lock` | The store's lock, which serializes the lifecycle transitions |
28922893
| `exec.lock` | The exec lock, which an `exec` holds from before it finds the running VM until its workload ends, so that one `exec` at a time runs in the sandbox; it stays from the first `exec` until `deprovision` |
28932894
| `launch.json` | The store's journal of a start in progress, or of one that was interrupted |

‎nvx-rs/src/host/cli/run.rs‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ use super::{Exit, Result, check_interrupt, output};
6363
use crate::ErrorCode;
6464
use crate::activation::{MachineIdMode, View};
6565
use crate::host::artifacts::{ArtifactDir, Guest, require_artifact};
66-
use crate::host::config::{
67-
Boot, ConsoleAttachment, NetworkConfig, NicConfig, RootConfig, VmConfig,
68-
};
66+
use crate::host::config::{Boot, ConsoleAttachment, NicConfig, RootConfig, VmConfig};
6967
use crate::host::filesystem::{
7068
self, ExportPlan, ExportRequest, Mapping, Owner, PathList, ReservedTargets, Target,
7169
};
@@ -643,17 +641,7 @@ fn cold_vm_config(
643641
rules: &NativeRules,
644642
filesystem: Option<ExportPlan>,
645643
) -> Result<VmConfig> {
646-
let network = sandbox_network(&options.network, rules)?
647-
.map(|network| -> Result<NetworkConfig> {
648-
Ok(NetworkConfig {
649-
guest_network: Some(
650-
crate::net::Ipv4Cidr::parse(network.guest_network())
651-
.map_err(|error| Exit::error(ErrorCode::Unsupported, error.to_string()))?,
652-
),
653-
policy: network.policy().clone(),
654-
})
655-
})
656-
.transpose()?;
644+
let network = sandbox_network(&options.network, rules)?;
657645
let config = VmConfig {
658646
processors: options.processors,
659647
memory_mib: memory_mib(options),

‎nvx-rs/src/host/cli/sandbox/lifecycle/mod.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ use crate::host::cli::artifacts::{Located, locate};
3131
use crate::host::cli::deadline::RETRY_INTERVAL;
3232
use crate::host::cli::sandbox::lock::LifecycleLock;
3333
use crate::host::cli::sandbox::state::{
34-
Config, RecordedDistro, RecordedScratch, Source, read_config, serialize_launch, write_json,
34+
Config, NetworkRecord, RecordedDistro, RecordedScratch, Source, read_config, serialize_launch,
35+
write_json,
3536
};
3637
use crate::host::cli::sandbox::{Artifacts, error_with_cause};
3738
use crate::host::cli::session::Session;
@@ -68,6 +69,8 @@ pub(crate) struct ProvisionConfig<'a> {
6869
pub(crate) cmdline: &'a str,
6970
/// The RAM upper's cap, or the default when omitted.
7071
pub(crate) ram_upper_percent: Option<u8>,
72+
/// The network options, or `None` without `--net`.
73+
pub(crate) network: Option<&'a NetworkRecord>,
7174
}
7275

7376
/// The control features that the layered sandbox root must provide for the requests of

‎nvx-rs/src/host/cli/sandbox/lifecycle/start.rs‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::host::cli::download::Hold;
2424
use crate::host::cli::exec_lock::ExecLock;
2525
use crate::host::cli::sandbox::lock::LifecycleLock;
2626
use crate::host::cli::sandbox::state::{
27-
Config, RecordedDistro, RecordedScratch, Source, read_config, serialize_launch,
27+
Config, NetworkRecord, RecordedDistro, RecordedScratch, Source, read_config, serialize_launch,
2828
};
2929
use crate::host::cli::sandbox::{
3030
Artifacts, Options, error_with_cause, hypervisor, path_string, refuse_own_files,
@@ -538,9 +538,10 @@ pub(crate) fn exec_workload(
538538
/// the workload ended, or nothing for a dry run, which launches nothing.
539539
///
540540
/// The VM is the one that `start` launches for the configuration that `provision` records for
541-
/// `launch`, whose distro layer `distro` is at the path that `provision` records, with the options
542-
/// of `options`: the run serializes that configuration and reads it back as `start` does, then
543-
/// launches it as `start` does, but in a temporary VM directory rather than a state directory.
541+
/// `launch`, whose distro layer `distro` is at the path that `provision` records, and `network`,
542+
/// the record of its network options, if any, with the options of `options`: the run serializes
543+
/// that configuration and reads it back as `start` does, then launches it as `start` does, but in
544+
/// a temporary VM directory rather than a state directory.
544545
/// Before it downloads or launches anything, it refuses what `start` refuses of the configuration
545546
/// and `exec` of `request`, after a missing artifact; it then refuses the shares that `start`
546547
/// refuses, with its VM directory in place of a state directory, and those of the OpenVMM that it
@@ -555,6 +556,7 @@ pub(crate) fn exec_workload(
555556
pub(crate) fn run(
556557
launch: &SandboxLaunch,
557558
distro: &RecordedDistro,
559+
network: Option<&NetworkRecord>,
558560
artifacts: &Artifacts<'_>,
559561
options: &Options,
560562
request: &ExecRequest,
@@ -566,6 +568,7 @@ pub(crate) fn run(
566568
memory_mib: options.memory_mib,
567569
cmdline: &options.cmdline,
568570
ram_upper_percent: options.ram_upper_percent,
571+
network,
569572
};
570573
let recorded = record(launch, distro, &provision).and_then(|(config, launch)| {
571574
// As `start` does, refuse the user's own nvx_* tokens, and the command line that the

‎nvx-rs/src/host/cli/sandbox/lifecycle/start/tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ fn run_checks_again_that_its_shares_do_not_overlap() {
5656
memory_mib: 256,
5757
cmdline: "",
5858
ram_upper_percent: None,
59+
network: None,
5960
};
6061
record(&launch, &distro, &config).unwrap();
6162

‎nvx-rs/src/host/cli/sandbox/lifecycle/start/tests/restore.rs‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use crate::host::cli::sandbox::sandbox_network;
1919
use crate::host::cli::sandbox::state::{RecordedDistro, serialize_launch, write_json};
2020
use crate::host::cli::snapshots::{self, Name};
2121
use crate::host::cli::store::StateDir;
22-
use crate::host::config::NetworkConfig;
2322
use crate::host::filesystem::{Bind, ExportChild, ExportPlan, Owner};
2423
use crate::host::network::{Access, NetworkPolicy};
2524
use crate::host::paths::resolve;
@@ -31,7 +30,6 @@ use crate::host::sandbox::options::sandbox_mounts;
3130
use crate::host::sandbox::{SandboxLaunch, SandboxSettings, SandboxUpper};
3231
use crate::host::snapshot::tests::RestoreFixture;
3332
use crate::host::snapshot::{DESCRIPTION_FILE, KEY_FILE, OPENVMM_DIR};
34-
use crate::net::Ipv4Cidr;
3533
use crate::protocol::root::RootImage;
3634
use crate::protocol::{
3735
APP_ACTIVATE, APP_EXEC, APP_EXIT, APP_HEADER_LEN, APP_MAPS, APP_READY, APP_STDOUT, OUTER_DATA,
@@ -385,12 +383,8 @@ fn a_one_off_restore_installs_the_network_options_of_run_as_its_own_policy() {
385383
.nic();
386384
let network = run::restored_network(options.network, nic).unwrap();
387385
let rules = network.egress_rules().unwrap();
388-
let network = sandbox_network(&network, &rules).unwrap().unwrap();
389386
let mut config = fixture.restore.config.clone();
390-
config.network = Some(NetworkConfig {
391-
guest_network: Some(Ipv4Cidr::parse(network.guest_network()).unwrap()),
392-
policy: network.policy().clone(),
393-
});
387+
config.network = sandbox_network(&network, &rules).unwrap();
394388
fixture.start("state", config, "ready", 30.0).unwrap();
395389
// The guest learns the DNS server that the policy lets it reach, and announces its listeners,
396390
// which the policy publishes on host loopback.
@@ -691,6 +685,7 @@ fn deprovision_removes_a_restored_sandboxs_view_only_under_its_snapshots_lock()
691685
memory_mib: 512,
692686
cmdline: "",
693687
ram_upper_percent: None,
688+
network: None,
694689
};
695690
let distro = RecordedDistro::new(&launch, binding).unwrap();
696691
let text = serialize_launch(&launch, &distro, None, &options, Some(&source)).unwrap();
@@ -742,6 +737,7 @@ fn a_restored_start_refuses_a_share_of_its_snapshot_store() {
742737
memory_mib: 512,
743738
cmdline: "",
744739
ram_upper_percent: None,
740+
network: None,
745741
};
746742
let distro = RecordedDistro::new(&launch, binding).unwrap();
747743
let source = fixture.source();

‎nvx-rs/src/host/cli/sandbox/mod.rs‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ pub(super) use self::options::{
4545
HYPERVISORS, MAX_MEMORY_MIB, MOUNT_OWNERS, OPTIONS, hypervisor, parse, path_text,
4646
};
4747
pub(crate) use self::options::{Options, path_string};
48-
use self::state::RecordedDistro;
4948
pub(crate) use self::state::Source;
49+
use self::state::{NetworkRecord, RecordedDistro};
5050

5151
/// The artifacts of a sandbox, found when an operation first needs them.
5252
pub(crate) struct Artifacts<'a> {
@@ -293,8 +293,9 @@ fn run_from(root: Option<&str>, options: Options, source: Option<&Source>) -> Re
293293
// `provision` records its binding, which `start` checks without reading the layer again.
294294
let binding = validate_sandbox_systemd_policy(&launch)?;
295295
// The network's policy is checked before anything launches or is recorded.
296-
let launch = launch.with_network(sandbox_network(&options.network, &rules)?);
297-
Some((launch, binding))
296+
sandbox_network(&options.network, &rules)?;
297+
let network = NetworkRecord::new(&options.network, &rules);
298+
Some((launch, binding, network))
298299
} else {
299300
None
300301
};
@@ -317,7 +318,7 @@ fn run_from(root: Option<&str>, options: Options, source: Option<&Source>) -> Re
317318
inherit_default_environment: options.inherit_default_environment,
318319
};
319320
match (operation, launch) {
320-
(SandboxOperation::Provision, Some((launch, binding))) => {
321+
(SandboxOperation::Provision, Some((launch, binding, network))) => {
321322
// `provision` records the layer at its resolved path, where `start` checks the
322323
// binding, with the manifest beside that path.
323324
let distro = RecordedDistro::new(&launch, binding)?;
@@ -339,6 +340,7 @@ fn run_from(root: Option<&str>, options: Options, source: Option<&Source>) -> Re
339340
memory_mib: options.memory_mib,
340341
cmdline: &options.cmdline,
341342
ram_upper_percent: options.ram_upper_percent,
343+
network: network.as_ref(),
342344
},
343345
source,
344346
options.timeout,
@@ -377,7 +379,7 @@ fn run_from(root: Option<&str>, options: Options, source: Option<&Source>) -> Re
377379
(SandboxOperation::Deprovision, _) => {
378380
lifecycle::deprovision(state_dir("deprovision")?, &artifacts, options.timeout)
379381
}
380-
(SandboxOperation::Run, Some((launch, binding))) => {
382+
(SandboxOperation::Run, Some((launch, binding, network))) => {
381383
if options.state_dir.is_some() {
382384
return Err(Exit::error(
383385
ErrorCode::Unsupported,
@@ -387,8 +389,15 @@ fn run_from(root: Option<&str>, options: Options, source: Option<&Source>) -> Re
387389
// A run launches the layer at the path that a provision would record.
388390
let distro = RecordedDistro::new(&launch, binding)?;
389391
let held = early.transpose()?.unwrap_or_else(Hold::none);
390-
let Some(result) =
391-
lifecycle::run(&launch, &distro, &artifacts, &options, &request, held)?
392+
let Some(result) = lifecycle::run(
393+
&launch,
394+
&distro,
395+
network.as_ref(),
396+
&artifacts,
397+
&options,
398+
&request,
399+
held,
400+
)?
392401
else {
393402
return Ok(());
394403
};

‎nvx-rs/src/host/cli/sandbox/options.rs‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ use crate::ErrorCode;
77
use crate::host::cli::args::{Parser, Receiver, Spec, Syntax, choice, integer};
88
use crate::host::cli::network::{NativeRules, NetworkOptions};
99
use crate::host::cli::{Exit, Result};
10+
use crate::host::config::NetworkConfig;
1011
use crate::host::network::{
1112
Access, EgressRule, EgressRules, HostLoopback, LoopbackForward, NetworkPolicy, NetworkProxy,
13+
openvmm_arguments,
1214
};
1315
use crate::host::paths;
1416
use crate::host::sandbox::distro::DistroFormat;
1517
use crate::host::sandbox::mount::{MountOwner, PolicyKind};
1618
use crate::host::sandbox::options::{PolicyPathOption, SandboxOperation};
1719
use crate::host::sandbox::{
18-
DEFAULT_ENTRYPOINT, DEFAULT_HOSTNAME, DEFAULT_WORKLOAD_IDENTITY, SandboxLayer, SandboxNetwork,
20+
DEFAULT_ENTRYPOINT, DEFAULT_HOSTNAME, DEFAULT_WORKLOAD_IDENTITY, SandboxLayer,
1921
parse_workload_identity,
2022
};
2123
use crate::net::{Ipv4Cidr, guest_gateway};
@@ -402,8 +404,8 @@ pub(crate) fn path_string(path: &Path) -> Result<String> {
402404

403405
/// Returns the network that `network` describes, with `rules`, the egress
404406
/// rules that [`NetworkOptions::egress_rules`] resolved: the typed policy at the guest network of
405-
/// `--net`, which [`SandboxNetwork::new`] checks before any launch, or `None` without `--net`, which
406-
/// no other network option may then accompany.
407+
/// `--net`, which it checks before any launch, as OpenVMM lowers it, or `None` without `--net`,
408+
/// which no other network option may then accompany.
407409
///
408410
/// Omitted options take OpenVMM's defaults: egress allowed, ingress denied, and host-loopback
409411
/// access from the guest alone, [`HostLoopback::Outbound`]. As OpenVMM does, the policy takes
@@ -413,7 +415,7 @@ pub(crate) fn path_string(path: &Path) -> Result<String> {
413415
pub(in crate::host::cli) fn sandbox_network(
414416
network: &NetworkOptions,
415417
rules: &NativeRules,
416-
) -> Result<Option<SandboxNetwork>> {
418+
) -> Result<Option<NetworkConfig>> {
417419
let Some(net) = &network.net else {
418420
let network_options = network.network_egress.is_some()
419421
|| network.network_ingress.is_some()
@@ -433,7 +435,8 @@ pub(in crate::host::cli) fn sandbox_network(
433435
};
434436
// `Ipv4Cidr` gives a guest network's refusal, and one that it accepts has a gateway: its
435437
// network's first address.
436-
Ipv4Cidr::parse(net).map_err(|message| Exit::error(ErrorCode::Unsupported, message))?;
438+
let guest_network =
439+
Ipv4Cidr::parse(net).map_err(|message| Exit::error(ErrorCode::Unsupported, message))?;
437440
let gateway = guest_gateway(net).expect("a guest network that parses has a gateway");
438441
if network.network_egress.is_none() && (!rules.allow.is_empty() || !rules.deny.is_empty()) {
439442
return Err(Exit::error(
@@ -478,5 +481,10 @@ pub(in crate::host::cli) fn sandbox_network(
478481
.map_err(|error| refused("--network-proxy", error))?;
479482
policy.proxy = Some(proxy);
480483
}
481-
Ok(Some(SandboxNetwork::new(net, policy)?))
484+
// A policy that OpenVMM's options cannot express is refused before anything launches.
485+
openvmm_arguments(Some(&policy), net)?;
486+
Ok(Some(NetworkConfig {
487+
guest_network: Some(guest_network),
488+
policy,
489+
}))
482490
}

0 commit comments

Comments
 (0)