|
| 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