Diffinery

homeflare/celldpublic

fix(fence): let the node reach its containers through Podman's isolation

CELLD_FENCE_NODE names the node; its connections into the container bridges get the 0x1000 mark.
Jason Schwarzenberger authored 1 parent 051fe80commit 6d0368a32ba0a13b1a3c02fc5cac1645936a4035Browse files
2 files changed+60−2
modifiedHOMEFLARE.md+1−0View file
@@ -30,6 +30,7 @@
3030 | Container images in parts | `celld deploy` read the whole `docker save` output into memory and wrote it in one `PUT`, which has to finish within the bucket client's 30 s; Diffinery's multi-GB runner image timed out against RustFS, failing every redeploy. The save now streams into a multipart upload of 16 MiB parts (`Bucket::put_large`), completed only once the save exits successfully. A node loading the image downloads it in 16 MiB ranges to a temporary file (`Bucket::get_large`) and streams that to the engine's `images/load` (`Docker::post_file`). Test: `CELLD_TEST_S3_ENDPOINT=... cargo test -p celld --lib large_object_round_trip -- --ignored` against any S3 (moto_server works). |
3131 | Fence in another network namespace (`CELLD_FENCE_NETNS`) | Under rootless Podman the fence's `--network host` container can't load nftables rules (`netlink: Operation not permitted`), and the container bridges aren't in the host's namespace anyway: they're in Podman's rootless-netns, which the user owns. With `CELLD_FENCE_NETNS=<path>` the fence joins that namespace (`ns:<path>`) and loads `FENCE_RULES_NETNS`: as `FENCE_RULES`, plus `ct state established,related accept` in forward (the node is another container there, so its replies are forwarded) and port 53 to the bridge gateways (aardvark-dns). Unset keeps upstream's behaviour. |
3232 | Fence exceptions (`CELLD_FENCE_DNAT`, `CELLD_FENCE_ALLOW`) | A container reaching one of the node's own Workers on Cloudflare goes over the Internet. On Homeflare the Worker's hostname resolves to the LAN, which the fence refuses (Diffinery's import agent couldn't report back). `CELLD_FENCE_DNAT=HOST:PORT=HOST:PORT[,...]` rewrites the bridges' new TCP connections to the first endpoint into the second (a nat prerouting chain at -110, ahead of the engine's port forwarding, since a connection is translated once). The rewrite also sets connection mark `0x1000`: Podman (netavark) isolates its networks from one another and drops forwarding from celld's bridge to the edge's network, but accepts connections with that mark ahead of its isolation, as it does for its own port forwarding; the mark stays with the connection, so replies pass. `CELLD_FENCE_ALLOW=HOST:PORT[,...]` accepts the bridges' connections to those endpoints inside the fence's input and forward chains, ahead of the rejects (an accept in a chain of its own wouldn't stop them). A host may be a name; it's resolved to IPv4 each time a container starts, and the fence is installed again when its rules change. Only addresses and numbers reach the rules. Test: `cargo test -p celld --lib fence_rule_tests`. |
33+| The node's own connections into its containers (`CELLD_FENCE_NODE`) | Podman isolates its networks from one another, so the node (on the engine's network) couldn't reach a port a container serves: Diffinery's sandbox server never answered `startAndWaitForPorts`. `CELLD_FENCE_NODE=HOST` (resolved each container start) marks the node's new connections into `celld0`/`celld1` with netavark's `0x1000`, which it accepts ahead of its isolation; replies keep the mark. The rule is in a table of the node's own (`inet celld_node_<host>`), since the main table is shared by every node on the engine and each replaces it. |
3334 | `homeflare:` line in `celld --version` | Lists what this build adds (`HOMEFLARE_FEATURES` in `lib.rs`), so Homeflare sends a config key only to a celld that accepts it. Add a feature there with each patch. |
3435
3536 ## Building
modifiedcrates/celld/container.rs+59−2View file
@@ -286,9 +286,49 @@
286286 } else {
287287 format!(" chain prerouting {{\n type nat hook prerouting priority -110; policy accept;\n{dnat} }}\n")
288288 };
289- Ok(base
289+ let mut rules = base
290290 .replace("#ALLOW\n ", &allow)
291- .replace("#CHAINS\n", &chains))
291+ .replace("#CHAINS\n", &chains);
292+ // homeflare: `CELLD_FENCE_NODE=HOST` is this node's own address on the
293+ // engine's network. Podman isolates its networks from one another, so
294+ // the node's connections into a container (a port it publishes, such as
295+ // a sandbox's server) are dropped on the way to the bridge. They get the
296+ // mark netavark accepts ahead of its isolation (as `CELLD_FENCE_DNAT`
297+ // connections do); it stays with the connection, so replies pass too.
298+ // The rule is in a table of this node's own, named after the host: the
299+ // main table is shared by every node on the engine and each replaces it,
300+ // while each node replaces only its own (its address changes when it
301+ // restarts).
302+ if let Some(node) = std::env::var("CELLD_FENCE_NODE")
303+ .ok()
304+ .map(|s| s.trim().to_string())
305+ .filter(|s| !s.is_empty())
306+ {
307+ let (ip, _) = fence_endpoint(&format!("{node}:1"))
308+ .await
309+ .context("CELLD_FENCE_NODE")?;
310+ let table: String = node
311+ .chars()
312+ .map(|c| {
313+ if c.is_ascii_alphanumeric() {
314+ c.to_ascii_lowercase()
315+ } else {
316+ '_'
317+ }
318+ })
319+ .collect();
320+ rules.push_str(&format!(
321+ "table inet celld_node_{table}\n\
322+ delete table inet celld_node_{table}\n\
323+ table inet celld_node_{table} {{\n\
324+ \x20 chain forward {{\n\
325+ \x20 type filter hook forward priority -11; policy accept;\n\
326+ \x20 ip saddr {ip} oifname {bridges} ct state new ct mark set ct mark | {FENCE_DNAT_MARK:#010x}\n\
327+ \x20 }}\n\
328+ }}\n"
329+ ));
330+ }
331+ Ok(rules)
292332 }
293333
294334 /// homeflare: the network namespace the fence joins instead of the host's,
@@ -1812,6 +1852,23 @@
18121852 )
18131853 };
18141854 assert!(render(FENCE_RULES_NETNS).is_err());
1855+ // The node's own connections into the bridges get the mark, in a
1856+ // table of the node's own.
1857+ unsafe {
1858+ std::env::remove_var("CELLD_FENCE_ALLOW");
1859+ std::env::remove_var("CELLD_FENCE_DNAT");
1860+ std::env::set_var("CELLD_FENCE_NODE", "localhost");
1861+ }
1862+ let node = render(FENCE_RULES_NETNS).unwrap();
1863+ assert!(node.contains("table inet celld_node_localhost {"), "{node}");
1864+ assert!(
1865+ node.contains("delete table inet celld_node_localhost\n"),
1866+ "{node}"
1867+ );
1868+ assert!(node.contains(r#"ip saddr 127.0.0.1 oifname { "celld0", "celld1" } ct state new ct mark set ct mark | 0x00001000"#), "{node}");
1869+ unsafe { std::env::set_var("CELLD_FENCE_NODE", "bad host; flush ruleset") };
1870+ assert!(render(FENCE_RULES_NETNS).is_err());
1871+ unsafe { std::env::remove_var("CELLD_FENCE_NODE") };
18151872 // A hostname target resolves to its IPv4 address.
18161873 unsafe { std::env::set_var("CELLD_FENCE_ALLOW", "localhost:8444") };
18171874 assert!(render(FENCE_RULES_NETNS)