Check sandbox gateway DNS instead of requiring ping
Homelab Main / validate (push) Waiting to run Details
Homelab Main / running-update (push) Blocked by required conditions Details
Homelab Main / debian-ops (push) Blocked by required conditions Details

This commit is contained in:
juvenal.diaz 2026-11-06 23:36:09 -06:00
parent dd9933f2f4
commit 22cb1fd01e
3 changed files with 40 additions and 5 deletions

View File

@ -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 `./jeannie network-sandbox isolation-tests` runs the verification and WAN
probe together with additional learning exercises. It checks each VLAN's 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; 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. 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 The final stage temporarily enables WAN using the same guarded probe described

View File

@ -23,6 +23,17 @@ try:
except OSError as e: except OSError as e:
print('UDP_ERROR: '+str(e)); sys.exit(1) 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): 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) 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): def udp_probe(source_pid, destination_pid, destination_ip):
token = secrets.token_hex(16) token = secrets.token_hex(16)
listener = subprocess.Popen(['nsenter', '-t', str(destination_pid), '-n', '--', 'python3', listener = subprocess.Popen(['nsenter', '-t', str(destination_pid), '-n', '--', 'python3',
@ -73,15 +93,21 @@ def main():
for guest, (network, _) in clients.items()} for guest, (network, _) in clients.items()}
pids = {guest: verify.container_pid(guest) for guest in clients} pids = {guest: verify.container_pid(guest) for guest in clients}
gateways = {} gateways = {}
gateway_icmp = {}
for guest, (_, gateway) in clients.items(): 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], result = subprocess.run(['pct', 'exec', str(guest), '--', 'ping', '-c', '1', '-W', '2', gateway],
capture_output=True, text=True, timeout=12) 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]), udp = {'vlan10_to_vlan20': udp_probe(pids[9110], pids[9111], addresses[9111]),
'vlan20_to_vlan10': udp_probe(pids[9111], pids[9110], addresses[9110])} 'vlan20_to_vlan10': udp_probe(pids[9111], pids[9110], addresses[9110])}
print(json.dumps({'gateway_icmp': gateways, 'cross_vlan_udp': udp, print(json.dumps({'gateway_dns': gateways, 'gateway_icmp': gateway_icmp, 'cross_vlan_udp': udp,
'interpretation': 'Gateway replies demonstrate local routing. UDP silence is a sampled result, not proof of universal isolation.'}, indent=2)) '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(x != 'reachable' for x in gateways.values()) or return int(any(not x.startswith('reachable:') for x in gateways.values()) or
any(x.startswith(('failed:', 'inconclusive:')) for x in udp.values())) any(x.startswith(('failed:', 'inconclusive:')) for x in udp.values()))
except (OSError, RuntimeError, ValueError, subprocess.SubprocessError) as exc: except (OSError, RuntimeError, ValueError, subprocess.SubprocessError) as exc:
print(f'Sandbox isolation exercises failed: {exc}', file=sys.stderr) print(f'Sandbox isolation exercises failed: {exc}', file=sys.stderr)

View File

@ -36,3 +36,11 @@ class IsolationExercisesTest(unittest.TestCase):
success = subprocess.CompletedProcess([], 0, stdout='', stderr='') success = subprocess.CompletedProcess([], 0, stdout='', stderr='')
failure = subprocess.CompletedProcess([], 1, stdout='', stderr='nsenter failed') failure = subprocess.CompletedProcess([], 1, stdout='', stderr='nsenter failed')
self.assertTrue(self.probe([success, failure]).startswith('inconclusive:')) 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'))