Skip to content

Commit 8262e7b

Browse files
committed
refactor(driver-vm): extract shared nftables ruleset crate
Extracts the VM driver's per-sandbox NAT + default-deny nftables ruleset generation into a new openshell-nft-ruleset crate so other compute drivers can share the same ruleset shape instead of reimplementing it. No behavior change to the VM driver. Related: NVIDIA#2255 Signed-off-by: Eric Curtin <[email protected]>
1 parent d556748 commit 8262e7b

6 files changed

Lines changed: 173 additions & 50 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ These pipelines connect skills into end-to-end workflows. Individual skill files
4444
| `crates/openshell-driver-docker/` | Docker compute driver | In-process `ComputeDriver` backend for local Docker sandbox containers |
4545
| `crates/openshell-driver-podman/` | Podman compute driver | In-process `ComputeDriver` backend for local Podman sandbox containers |
4646
| `crates/openshell-driver-vm/` | VM compute driver | Standalone libkrun-backed `ComputeDriver` subprocess (embeds its own rootfs + runtime) |
47+
| `crates/openshell-nft-ruleset/` | nftables rulesets | Shared per-sandbox NAT + default-deny nftables ruleset generation used by compute drivers |
4748
| `crates/openshell-prover/` | Policy prover | Policy verification and proof generation |
4849
| `crates/openshell-server-macros/` | Server macros | Compile-time helpers for gateway RPC authorization |
4950
| `crates/openshell-supervisor-middleware/` | Middleware runtime | Generic middleware registry, remote service integration, and chain execution |

‎Cargo.lock‎

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openshell-driver-vm/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ path = "src/main.rs"
2020

2121
[dependencies]
2222
openshell-core = { path = "../openshell-core", default-features = false }
23+
openshell-nft-ruleset = { path = "../openshell-nft-ruleset" }
2324
openshell-policy = { path = "../openshell-policy" }
2425
openshell-vfio = { path = "../openshell-vfio" }
2526

‎crates/openshell-driver-vm/src/nft_ruleset.rs‎

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,27 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::fmt::Write;
5-
6-
/// Sanitize a TAP device name for use as an nftables table name suffix.
7-
/// Assumes device names match `vmtap-[a-f0-9]+` (driver-controlled).
8-
fn sanitize_table_name(device: &str) -> String {
9-
device.replace('-', "_")
10-
}
4+
//! Thin VM-driver-specific wrapper over the shared `openshell-nft-ruleset`
5+
//! generator. The generic ruleset shape (NAT + default-deny forward/input
6+
//! scoped to the gateway port) lives in `openshell-nft-ruleset` so the
7+
//! oci driver can reuse it for veth-based networking without pulling in
8+
//! the VM driver's `bollard`/`oci-client`/libkrun dependencies.
9+
//!
10+
//! The table prefix `openshell_vm` is preserved so existing deployments see
11+
//! no change in the nftables tables this driver creates, and so the VM and
12+
//! oci drivers can never collide on a table name if both manage
13+
//! interfaces on the same host.
14+
15+
const TABLE_PREFIX: &str = "openshell_vm";
1116

1217
/// Return the nftables table name for a TAP device.
1318
pub fn teardown_table_name(device: &str) -> String {
14-
format!("openshell_vm_{}", sanitize_table_name(device))
19+
openshell_nft_ruleset::teardown_table_name(TABLE_PREFIX, device)
1520
}
1621

1722
/// Generate the nftables ruleset for VM TAP networking.
1823
pub fn generate_tap_ruleset(tap_device: &str, subnet: &str, gateway_port: u16) -> String {
19-
let table_name = teardown_table_name(tap_device);
20-
let mut ruleset = String::with_capacity(512);
21-
22-
writeln!(ruleset, "table ip {table_name} {{").unwrap();
23-
writeln!(ruleset, " chain postrouting {{").unwrap();
24-
writeln!(
25-
ruleset,
26-
" type nat hook postrouting priority 100; policy accept;"
27-
)
28-
.unwrap();
29-
writeln!(ruleset, " ip saddr {subnet} masquerade").unwrap();
30-
writeln!(ruleset, " }}").unwrap();
31-
writeln!(ruleset, " chain forward {{").unwrap();
32-
writeln!(
33-
ruleset,
34-
" type filter hook forward priority 0; policy accept;"
35-
)
36-
.unwrap();
37-
writeln!(ruleset, " iifname \"{tap_device}\" accept").unwrap();
38-
writeln!(
39-
ruleset,
40-
" oifname \"{tap_device}\" ct state related,established accept"
41-
)
42-
.unwrap();
43-
writeln!(ruleset, " oifname \"{tap_device}\" drop").unwrap();
44-
writeln!(ruleset, " }}").unwrap();
45-
writeln!(ruleset, " chain input {{").unwrap();
46-
writeln!(
47-
ruleset,
48-
" type filter hook input priority 0; policy accept;"
49-
)
50-
.unwrap();
51-
writeln!(
52-
ruleset,
53-
" iifname \"{tap_device}\" tcp dport {gateway_port} accept"
54-
)
55-
.unwrap();
56-
writeln!(ruleset, " iifname \"{tap_device}\" drop").unwrap();
57-
writeln!(ruleset, " }}").unwrap();
58-
writeln!(ruleset, "}}").unwrap();
59-
60-
ruleset
24+
openshell_nft_ruleset::generate_ruleset(TABLE_PREFIX, tap_device, subnet, gateway_port)
6125
}
6226

6327
#[cfg(test)]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
[package]
5+
name = "openshell-nft-ruleset"
6+
description = "Shared nftables ruleset generation for per-sandbox network isolation"
7+
version.workspace = true
8+
edition.workspace = true
9+
rust-version.workspace = true
10+
license.workspace = true
11+
repository.workspace = true
12+
13+
[lib]
14+
name = "openshell_nft_ruleset"
15+
path = "src/lib.rs"
16+
17+
[lints]
18+
workspace = true
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Shared nftables ruleset generation for per-sandbox network isolation.
5+
//!
6+
//! This crate contains pure, dependency-free string generation only. Callers
7+
//! own actually invoking `nft` (typically via `nft -f -` on stdin) and
8+
//! managing the lifecycle of the host-side network interface (TAP device for
9+
//! the VM driver, veth pair for the oci driver).
10+
//!
11+
//! The generated ruleset is intentionally minimal and symmetric across
12+
//! backends:
13+
//! - `postrouting`: NAT (masquerade) traffic leaving the sandbox subnet.
14+
//! - `forward`: default-deny; only allow traffic initiated by the sandbox
15+
//! interface and its related/established return traffic.
16+
//! - `input`: default-deny; only allow the sandbox to reach the gateway's
17+
//! listen port on the host.
18+
19+
use std::fmt::Write;
20+
21+
/// Sanitize an interface name for use as an nftables table name suffix.
22+
/// Assumes interface names are driver-controlled and contain only
23+
/// alphanumerics and hyphens (e.g. `vmtap-<hex>`, `oshveth-<hex>`).
24+
fn sanitize_table_name(interface: &str) -> String {
25+
interface.replace('-', "_")
26+
}
27+
28+
/// Return the nftables table name for a given table prefix and interface.
29+
///
30+
/// The prefix namespaces tables per-backend (e.g. `openshell_vm`,
31+
/// `openshell_oci`) so that two drivers managing interfaces on the same
32+
/// host can never collide on a table name, even if (implausibly) they picked
33+
/// the same interface name.
34+
#[must_use]
35+
pub fn teardown_table_name(table_prefix: &str, interface: &str) -> String {
36+
format!("{table_prefix}_{}", sanitize_table_name(interface))
37+
}
38+
39+
/// Generate the nftables ruleset scoping a sandbox's network interface.
40+
///
41+
/// `table_prefix` should be a short, driver-unique identifier (for example
42+
/// `openshell_vm` or `openshell_oci`). `interface` is the host-side
43+
/// interface for this sandbox (a TAP device or the host end of a veth pair).
44+
/// `subnet` is the sandbox's point-to-point or bridge subnet in CIDR form.
45+
/// `gateway_port` is the only TCP port on the host the sandbox is allowed to
46+
/// reach.
47+
#[must_use]
48+
pub fn generate_ruleset(
49+
table_prefix: &str,
50+
interface: &str,
51+
subnet: &str,
52+
gateway_port: u16,
53+
) -> String {
54+
let table_name = teardown_table_name(table_prefix, interface);
55+
let mut ruleset = String::with_capacity(512);
56+
57+
writeln!(ruleset, "table ip {table_name} {{").unwrap();
58+
writeln!(ruleset, " chain postrouting {{").unwrap();
59+
writeln!(
60+
ruleset,
61+
" type nat hook postrouting priority 100; policy accept;"
62+
)
63+
.unwrap();
64+
writeln!(ruleset, " ip saddr {subnet} masquerade").unwrap();
65+
writeln!(ruleset, " }}").unwrap();
66+
writeln!(ruleset, " chain forward {{").unwrap();
67+
writeln!(
68+
ruleset,
69+
" type filter hook forward priority 0; policy accept;"
70+
)
71+
.unwrap();
72+
writeln!(ruleset, " iifname \"{interface}\" accept").unwrap();
73+
writeln!(
74+
ruleset,
75+
" oifname \"{interface}\" ct state related,established accept"
76+
)
77+
.unwrap();
78+
writeln!(ruleset, " oifname \"{interface}\" drop").unwrap();
79+
writeln!(ruleset, " }}").unwrap();
80+
writeln!(ruleset, " chain input {{").unwrap();
81+
writeln!(
82+
ruleset,
83+
" type filter hook input priority 0; policy accept;"
84+
)
85+
.unwrap();
86+
writeln!(
87+
ruleset,
88+
" iifname \"{interface}\" tcp dport {gateway_port} accept"
89+
)
90+
.unwrap();
91+
writeln!(ruleset, " iifname \"{interface}\" drop").unwrap();
92+
writeln!(ruleset, " }}").unwrap();
93+
writeln!(ruleset, "}}").unwrap();
94+
95+
ruleset
96+
}
97+
98+
#[cfg(test)]
99+
mod tests {
100+
use super::*;
101+
102+
#[test]
103+
fn generates_setup_ruleset() {
104+
let ruleset = generate_ruleset("openshell_vm", "vmtap-abcd", "10.0.128.0/30", 8080);
105+
assert!(ruleset.contains("table ip openshell_vm_vmtap_abcd {"));
106+
assert!(ruleset.contains("type nat hook postrouting priority 100; policy accept;"));
107+
assert!(ruleset.contains("ip saddr 10.0.128.0/30 masquerade"));
108+
assert!(ruleset.contains("type filter hook forward priority 0; policy accept;"));
109+
assert!(ruleset.contains("iifname \"vmtap-abcd\" accept"));
110+
assert!(ruleset.contains("oifname \"vmtap-abcd\" ct state related,established accept"));
111+
assert!(ruleset.contains("oifname \"vmtap-abcd\" drop"));
112+
assert!(ruleset.contains("type filter hook input priority 0; policy accept;"));
113+
assert!(ruleset.contains("iifname \"vmtap-abcd\" tcp dport 8080 accept"));
114+
}
115+
116+
#[test]
117+
fn table_name_sanitizes_interface_name() {
118+
let ruleset = generate_ruleset("openshell_vm", "vmtap-abc-123", "10.0.128.0/30", 8080);
119+
assert!(ruleset.contains("table ip openshell_vm_vmtap_abc_123 {"));
120+
}
121+
122+
#[test]
123+
fn teardown_command_targets_correct_table() {
124+
let cmd = teardown_table_name("openshell_vm", "vmtap-abcd");
125+
assert_eq!(cmd, "openshell_vm_vmtap_abcd");
126+
}
127+
128+
#[test]
129+
fn different_prefixes_never_collide_on_the_same_interface_name() {
130+
let vm_table = teardown_table_name("openshell_vm", "shared-iface");
131+
let oci_table = teardown_table_name("openshell_oci", "shared-iface");
132+
assert_ne!(vm_table, oci_table);
133+
}
134+
}

0 commit comments

Comments
 (0)