git clone https://garage44.eu/jeroen/midas.gitcartopolis-agent · 2026-11-21T19:38:58Z
fix(setup): a node looks names up at the machine's resolvers and nowhere else The machine firewall accepted port 53, udp and tcp, to any host before it dropped the private ranges and rejected IPv6: a node could speak to every LAN host on 53, and tunnel over DNS. The Mac rule already named the gateway only. Found by the review before the code went public. `midas setup` now reads the machine's resolvers (resolvectl dns, else /etc/resolv.conf, never a loopback stub) and allows 53 to those addresses only, v4 and v6 each. When none is found the old rule stands, said so in the rules file and on stderr, so a machine does not lose name lookups for a resolver it names in a way this does not read.
docs/node.md | 4 +--
src/setup.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 78 insertions(+), 8 deletions(-)
diff --git a/docs/node.md b/docs/node.md
index 4a3a8f1..51581b7 100644
--- a/docs/node.md
+++ b/docs/node.md
@@ -7,8 +7,8 @@ command does it, and the same command updates it later.
- a user `midas-node` that runs the node — never your own account;
- a firewall on your machine that keeps the node off your home network and off
- the machine itself (it may reach the hub and the internet), in force from
- boot;
+ the machine itself (it may reach the hub, the resolvers your machine used
+ when it was set up, and the internet), in force from boot;
- the node itself: a rootless container (Linux) or a virtual machine (Mac),
started at boot, limited in memory and CPUs;
- a small service, as root, that sends the hub what the machine's chip draws
diff --git a/src/setup.rs b/src/setup.rs
index 4f01b5d..183c79b 100644
--- a/src/setup.rs
+++ b/src/setup.rs
@@ -305,14 +305,61 @@ fn is_private(ip: &std::net::IpAddr) -> bool {
}
}
+/// The resolvers this machine uses: systemd-resolved's upstreams (`resolvectl
+/// dns`), else `/etc/resolv.conf`'s. A loopback stub (127.0.0.53) is nobody
+/// the node can reach and is left out.
+pub fn resolvers() -> Vec<std::net::IpAddr> {
+ let resolvectl = Command::new("resolvectl").arg("dns").output().ok()
+ .filter(|o| o.status.success()).map(|o| String::from_utf8_lossy(&o.stdout).into_owned()).unwrap_or_default();
+ let resolv_conf = fs::read_to_string("/etc/resolv.conf").unwrap_or_default();
+ parse_resolvers(&resolvectl, &resolv_conf)
+}
+
+fn parse_resolvers(resolvectl: &str, resolv_conf: &str) -> Vec<std::net::IpAddr> {
+ let addr = |w: &str| w.split('%').next().unwrap_or("").split('#').next().unwrap_or("").parse::<std::net::IpAddr>().ok();
+ let mut found: Vec<std::net::IpAddr> = resolvectl.lines()
+ .filter_map(|l| l.split_once(':').map(|(_, rest)| rest))
+ .flat_map(|rest| rest.split_whitespace().filter_map(addr).collect::<Vec<_>>())
+ .collect();
+ if found.is_empty() {
+ found = resolv_conf.lines().filter_map(|l| {
+ let mut w = l.split_whitespace();
+ (w.next() == Some("nameserver")).then(|| w.next()).flatten().and_then(addr)
+ }).collect();
+ }
+ found.retain(|a| !a.is_loopback() && !a.is_unspecified());
+ found.sort();
+ found.dedup();
+ found
+}
+
/// The firewall, as an nft file. `tunnel_if` is the interface the node's
/// address is on, when this machine is not the hub (on the hub that interface
/// also carries every other node's traffic to the hub, so it is left alone).
-pub fn firewall(uid: u32, c: &NodeConf, h: &FromHub, addrs: &[Addr], tunnel_if: Option<&str>) -> String {
+pub fn firewall(uid: u32, c: &NodeConf, h: &FromHub, addrs: &[Addr], tunnel_if: Option<&str>, resolvers: &[std::net::IpAddr]) -> String {
let (hub_ip, hub_port) = c.hub.rsplit_once(':').unwrap_or((&c.hub, "4791"));
let own4: Vec<String> = addrs.iter().filter(|a| a.ip.is_ipv4() && !is_private(&a.ip)).map(|a| a.ip.to_string()).collect();
let set = |v: &[String]| format!("{{ {} }}", v.join(", "));
let t = table(&c.node);
+ // Name lookups go to the machine's own resolvers and nowhere else: a rule
+ // that let port 53 through to any host, v4 or v6, let a node reach every
+ // LAN host on it (2026-11-21). Only when none can be found at all does the
+ // old rule stand, said so in the file.
+ let dns4: Vec<String> = resolvers.iter().filter(|a| a.is_ipv4()).map(|a| a.to_string()).collect();
+ let dns6: Vec<String> = resolvers.iter().filter(|a| a.is_ipv6()).map(|a| a.to_string()).collect();
+ let dns = if dns4.is_empty() && dns6.is_empty() {
+ " # Name lookups: no resolver was found at setup, so to any host — run `midas setup` again once one is.\n\
+ \x20 meta l4proto { udp, tcp } th dport 53 accept\n".to_string()
+ } else {
+ let mut s = " # Name lookups, to the resolvers this machine used at setup and no other host.\n".to_string();
+ if !dns4.is_empty() {
+ s += &format!(" ip daddr {} meta l4proto {{ udp, tcp }} th dport 53 accept\n", set(&dns4));
+ }
+ if !dns6.is_empty() {
+ s += &format!(" ip6 daddr {} meta l4proto {{ udp, tcp }} th dport 53 accept\n", set(&dns6));
+ }
+ s
+ };
let mut out = format!(
"# Written by `midas setup {node}` — run it again rather than editing this.\n\
@@ -332,8 +379,7 @@ pub fn firewall(uid: u32, c: &NodeConf, h: &FromHub, addrs: &[Addr], tunnel_if:
\x20 ct state established,related accept\n\
\x20 # The hub: its API, and git (ssh) from its mirror.\n\
\x20 ip daddr {hub_ip} tcp dport {{ {hub_port}, 22 }} accept\n\
- \x20 # Name lookups, to whichever resolver the machine uses (often the LAN's router).\n\
- \x20 meta l4proto {{ udp, tcp }} th dport 53 accept\n",
+ {dns}",
node = c.node, user = c.user,
);
// The machine's own public addresses: what the internet sees of it (its web
@@ -603,7 +649,11 @@ pub fn setup(a: Args) -> Result<i32> {
// 2. The firewall, loaded now and at every boot, before the network.
let rules_path = format!("/etc/midas/firewall-{node}.nft");
- write(&rules_path, &firewall(uid, &c, &h, &addrs, (!is_hub).then_some(tunnel.as_str())), 0o644)?;
+ let dns = resolvers();
+ if dns.is_empty() {
+ eprintln!("midas: no resolver found (resolvectl dns, /etc/resolv.conf): the node may look names up at any host");
+ }
+ write(&rules_path, &firewall(uid, &c, &h, &addrs, (!is_hub).then_some(tunnel.as_str()), &dns), 0o644)?;
run(Command::new(&nft).arg("-c").arg("-f").arg(&rules_path)).context("the firewall rules did not check")?;
let unit = firewall_unit(&node);
write(&format!("/etc/systemd/system/{unit}"), &format!(
@@ -812,7 +862,8 @@ mod tests {
3: wlan0 inet 192.168.1.20/24 brd 192.168.1.255 scope global wlan0\n\
3: wlan0 inet6 2a02:a46:1234:5600:1:2:3:4/64 scope global dynamic\n\
7: wg0 inet 10.44.0.2/24 scope global wg0\n");
- let f = firewall(991, &conf(), &hub(), &addrs, Some("wg0"));
+ let dns = ["192.168.1.1".parse().unwrap(), "2001:db8::53".parse().unwrap()];
+ let f = firewall(991, &conf(), &hub(), &addrs, Some("wg0"), &dns);
assert!(f.contains("table inet midas_gpu_1 {"));
assert!(f.contains("meta skuid != 991 accept"));
assert!(f.contains("ip daddr 10.44.0.1 tcp dport { 4791, 22 } accept"));
@@ -821,6 +872,25 @@ mod tests {
assert!(f.contains("iifname \"wg0\" ip daddr 10.44.0.2 tcp dport 2223 accept"));
assert!(f.contains("iifname \"wg0\" drop"));
assert!(!f.contains("192.168.1.20") && !f.contains("2a02:"));
+ // Name lookups: the machine's resolvers, before the private ranges are dropped, and no other host.
+ let (r4, r6, drop) = (f.find("ip daddr { 192.168.1.1 } meta l4proto { udp, tcp } th dport 53 accept").unwrap(),
+ f.find("ip6 daddr { 2001:db8::53 } meta l4proto { udp, tcp } th dport 53 accept").unwrap(),
+ f.find("ip daddr @private4 drop").unwrap());
+ assert!(r4 < drop && r6 < drop);
+ assert!(!f.contains(" meta l4proto { udp, tcp } th dport 53 accept"), "no port 53 to any host");
+ let open = firewall(991, &conf(), &hub(), &addrs, Some("wg0"), &[]);
+ assert!(open.contains("no resolver was found") && open.contains("meta l4proto { udp, tcp } th dport 53 accept"));
+ }
+
+ #[test]
+ fn resolvers_come_from_resolved_else_resolv_conf_and_never_a_loopback_stub() {
+ let resolved = "Global:\nLink 2 (ens3): 1.1.1.1 2606:4700:4700::1111\nLink 5 (wg0):\nLink 3 (wlan0): 192.168.1.1%wlan0 fe80::1%wlan0\n";
+ let conf = "# generated\nnameserver 127.0.0.53\nnameserver 192.168.1.1\noptions edns0\n";
+ let got: Vec<String> = parse_resolvers(resolved, conf).iter().map(|a| a.to_string()).collect();
+ assert_eq!(got, ["1.1.1.1", "192.168.1.1", "2606:4700:4700::1111", "fe80::1"]);
+ let got: Vec<String> = parse_resolvers("", conf).iter().map(|a| a.to_string()).collect();
+ assert_eq!(got, ["192.168.1.1"], "the stub is nobody the node can reach");
+ assert!(parse_resolvers("Global:\n", "nameserver 127.0.0.11\n").is_empty());
}
#[test]
@@ -828,7 +898,7 @@ mod tests {
let addrs = parse_addrs(
"2: ens3 inet 203.0.113.17/32 scope global ens3\n\
5: wg0 inet 10.44.0.1/24 scope global wg0\n");
- let f = firewall(990, &conf(), &hub(), &addrs, None);
+ let f = firewall(990, &conf(), &hub(), &addrs, None, &["203.0.113.53".parse().unwrap()]);
assert!(!f.contains("chain input"));
assert!(f.contains("ct original ip daddr { 203.0.113.17 } ct original proto-dst { 80, 443 } accept"));
assert!(f.contains("ip daddr { 203.0.113.17 } drop"));