From 22cb1fd01ecc19eb40ded6346ef920c068373b75 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Fri, 6 Nov 2026 23:36:09 -0600 Subject: [PATCH] Check sandbox gateway DNS instead of requiring ping --- docs/network-sandbox.md | 3 +- scripts/network-sandbox-isolation-host.py | 34 ++++++++++++++++--- .../jeannie/test_network_sandbox_isolation.py | 8 +++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/docs/network-sandbox.md b/docs/network-sandbox.md index c9d0fb8..6b6d879 100644 --- a/docs/network-sandbox.md +++ b/docs/network-sandbox.md @@ -48,7 +48,8 @@ guard before retrying. This test does not enable permanent internet access. `./jeannie network-sandbox isolation-tests` runs the verification and WAN probe together with additional learning exercises. It checks each VLAN's -gateway and sends UDP between temporary client listeners in both directions, +gateway DNS service, reports ICMP separately (the default firewall rejects it), +and sends UDP between temporary client listeners in both directions, after proving each listener works locally. A peer UDP response is a failure; a timeout is only an observation, and a failed local control is inconclusive. The final stage temporarily enables WAN using the same guarded probe described diff --git a/scripts/network-sandbox-isolation-host.py b/scripts/network-sandbox-isolation-host.py index ddc4819..a3543f7 100644 --- a/scripts/network-sandbox-isolation-host.py +++ b/scripts/network-sandbox-isolation-host.py @@ -23,6 +23,17 @@ try: except OSError as e: print('UDP_ERROR: '+str(e)); sys.exit(1) ''' +DNS_CLIENT = '''import socket,sys +query=bytes.fromhex('5a3101000001000000000000096c6f63616c686f73740000010001') +try: + s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM); s.settimeout(3) + s.connect((sys.argv[1],53)); s.send(query); data=s.recv(512) + if len(data)<12 or data[:2]!=query[:2] or not (data[2]&128): + print('DNS_INVALID: malformed or mismatched response'); sys.exit(2) + print('DNS_RESPONSE: rcode='+str(data[3]&15)) +except OSError as e: + print('DNS_ERROR: '+str(e)); sys.exit(1) +''' def udp_connect(pid, ip, port, token): @@ -30,6 +41,15 @@ def udp_connect(pid, ip, port, token): UDP_CLIENT, ip, str(port), token], capture_output=True, text=True, timeout=8) +def gateway_dns(pid, gateway): + result = subprocess.run(['nsenter', '-t', str(pid), '-n', '--', 'python3', '-c', + DNS_CLIENT, gateway], capture_output=True, text=True, timeout=8) + if result.returncode == 0 and result.stdout.startswith('DNS_RESPONSE: '): + return 'reachable: ' + result.stdout.strip() + detail = (result.stdout or result.stderr or 'no diagnostic output').strip().replace('\n', ' ') + return f'inconclusive: DNS probe exit {result.returncode}: {detail[:200]}' + + def udp_probe(source_pid, destination_pid, destination_ip): token = secrets.token_hex(16) listener = subprocess.Popen(['nsenter', '-t', str(destination_pid), '-n', '--', 'python3', @@ -73,15 +93,21 @@ def main(): for guest, (network, _) in clients.items()} pids = {guest: verify.container_pid(guest) for guest in clients} gateways = {} + gateway_icmp = {} for guest, (_, gateway) in clients.items(): + gateways[str(guest)] = gateway_dns(pids[guest], gateway) result = subprocess.run(['pct', 'exec', str(guest), '--', 'ping', '-c', '1', '-W', '2', gateway], capture_output=True, text=True, timeout=12) - gateways[str(guest)] = 'reachable' if result.returncode == 0 else 'failed or inconclusive' + if result.returncode == 0: + gateway_icmp[str(guest)] = 'reply received' + else: + detail = (result.stderr or result.stdout or 'no diagnostic output').strip().replace('\n', ' ') + gateway_icmp[str(guest)] = f'no reply or probe error (exit {result.returncode}): {detail[:160]}' udp = {'vlan10_to_vlan20': udp_probe(pids[9110], pids[9111], addresses[9111]), 'vlan20_to_vlan10': udp_probe(pids[9111], pids[9110], addresses[9110])} - print(json.dumps({'gateway_icmp': gateways, 'cross_vlan_udp': udp, - 'interpretation': 'Gateway replies demonstrate local routing. UDP silence is a sampled result, not proof of universal isolation.'}, indent=2)) - return int(any(x != 'reachable' for x in gateways.values()) or + print(json.dumps({'gateway_dns': gateways, 'gateway_icmp': gateway_icmp, 'cross_vlan_udp': udp, + 'interpretation': 'Gateway DNS replies show the router DNS service responded; its firewall rejects ICMP by default. UDP silence is a sampled result, not proof of universal isolation.'}, indent=2)) + return int(any(not x.startswith('reachable:') for x in gateways.values()) or any(x.startswith(('failed:', 'inconclusive:')) for x in udp.values())) except (OSError, RuntimeError, ValueError, subprocess.SubprocessError) as exc: print(f'Sandbox isolation exercises failed: {exc}', file=sys.stderr) diff --git a/tests/jeannie/test_network_sandbox_isolation.py b/tests/jeannie/test_network_sandbox_isolation.py index f5d663f..04de93b 100644 --- a/tests/jeannie/test_network_sandbox_isolation.py +++ b/tests/jeannie/test_network_sandbox_isolation.py @@ -36,3 +36,11 @@ class IsolationExercisesTest(unittest.TestCase): success = subprocess.CompletedProcess([], 0, stdout='', stderr='') failure = subprocess.CompletedProcess([], 1, stdout='', stderr='nsenter failed') self.assertTrue(self.probe([success, failure]).startswith('inconclusive:')) + + def test_gateway_dns_requires_a_valid_response(self): + reply = subprocess.CompletedProcess([], 0, stdout='DNS_RESPONSE: rcode=0\n', stderr='') + failure = subprocess.CompletedProcess([], 1, stdout='DNS_ERROR: timed out\n', stderr='') + with patch.object(isolation.subprocess, 'run', return_value=reply): + self.assertTrue(isolation.gateway_dns(10, '172.29.210.1').startswith('reachable:')) + with patch.object(isolation.subprocess, 'run', return_value=failure): + self.assertIn('inconclusive', isolation.gateway_dns(10, '172.29.210.1'))