1#!/usr/bin/env python3
2"""VM library and trust-boundary regressions, without a hypervisor."""
3import importlib.util
4import json
5import os
6import pty
7import select
8from pathlib import Path
9import socket
10import struct
11import tempfile
12import threading
13import unittest
14from unittest.mock import patch
15import sys
16from types import SimpleNamespace
17sys.path.insert(0, str(Path(__file__).parent))
18import vms
19spec = importlib.util.spec_from_file_location('vm_host', Path(__file__).with_name('dashboard-host.py'))
20host = importlib.util.module_from_spec(spec)
21spec.loader.exec_module(host)
22
23
24class VMTests(unittest.TestCase):
25 def test_media_replaces_a_guest_locked_cd_and_persists_until_ejected(self):
26 with tempfile.TemporaryDirectory() as temporary:
27 directory = Path(temporary).resolve()
28 installer = directory / 'installer.iso'
29 installer.write_bytes(b'installer')
30 vm = directory / 'fixture'
31 vm.mkdir()
32 seed = vm / 'seed.iso'
33 seed.write_bytes(b'setup')
34 definition = f'''<domain><os><type arch="x86_64" machine="q35"/></os>
35 <devices><disk type="file" device="cdrom"><driver name="qemu" type="raw"/>
36 <source file="{seed}"/><target dev="sdb" bus="sata"/><readonly/>
37 </disk></devices></domain>'''
38
39 def virsh(action, *args):
40 nonlocal definition
41 if action == 'dumpxml':
42 return definition
43 if action == 'domstate':
44 return 'running'
45 self.assertEqual(action, 'update-device')
46 self.assertIn('--live', args)
47 self.assertIn('--config', args)
48 if '--force' not in args:
49 raise vms.subprocess.CalledProcessError(1, action, stderr='tray locked')
50 cd = vms.ET.parse(args[1]).getroot()
51 self.assertIsNotNone(cd.find('readonly'))
52 definition = '<domain><devices>' + vms.ET.tostring(cd, encoding='unicode') + '</devices></domain>'
53 return ''
54
55 with patch.object(vms, 'DISKS', directory), patch.object(vms, 'resolve_image', return_value=installer), \
56 patch.object(vms, 'virsh', side_effect=virsh):
57 vms.change_media({'name': 'fixture', 'image': 'installer.iso'})
58 attached = Path(vms.ET.fromstring(definition).find('./devices/disk/source').get('file'))
59 self.assertEqual(attached.read_bytes(), installer.read_bytes())
60 self.assertFalse(seed.exists())
61 vms.change_media({'name': 'fixture', 'image': ''})
62 self.assertIsNone(vms.ET.fromstring(definition).find('./devices/disk/source'))
63 self.assertFalse(attached.exists())
64 self.assertTrue(installer.exists())
65
66 def test_secure_preset_retains_efi_state_but_generates_new_login(self):
67 with tempfile.TemporaryDirectory() as temporary:
68 root = Path(temporary).resolve()
69 disks = root / 'vms'
70 source = disks / 'template'
71 source.mkdir(parents=True)
72 (source / 'disk.qcow2').write_bytes(b'prepared disk')
73 (source / 'nvram.fd').write_bytes(b'prepared signed EFI variables')
74 code = root / 'code.fd'
75 code.write_bytes(b'firmware')
76 defaults = root / 'vars.fd'
77 defaults.write_bytes(b'fresh EFI variables')
78 xml = f'''<domain><metadata><vm xmlns="{vms.NS[1:-1]}">
79 <platform>windows</platform><hardwareProfile>windows-aarch64-nvme</hardwareProfile>
80 </vm></metadata><memory>8388608</memory><vcpu>4</vcpu>
81 <os><type arch="aarch64">hvm</type><loader>{code}</loader><nvram>{source / 'nvram.fd'}</nvram></os>
82 <devices><disk device="disk"><source file="{source / 'disk.qcow2'}"/></disk></devices></domain>'''
83 def command(*args, **kwargs):
84 if args[0] == 'cp':
85 with os.fdopen(os.dup(int(args[-2].split('/')[-1])), 'rb') as incoming:
86 Path(args[-1]).write_bytes(incoming.read())
87 elif args[:2] == ('qemu-img', 'convert'):
88 Path(args[-1]).write_bytes(Path(args[-2]).read_bytes())
89 return json.dumps({'format': 'qcow2', 'virtual-size': 1024, 'format-specific': {'data': {}}})
90 def virsh(*args, **kwargs):
91 return {'domstate': 'shut off', 'dumpxml': xml, 'pool-list': 'vms', 'pool-info': 'State: active'}.get(args[0], '')
92 with patch.object(vms, 'DISKS', disks), patch.object(vms, 'PRESETS', root / 'presets'), \
93 patch.object(vms, 'command', side_effect=command), patch.object(vms, 'virsh', side_effect=virsh), \
94 patch.object(vms, 'secure_firmware', return_value=(code, defaults)), \
95 patch.dict(os.environ, {'SNOWGLOBE_VM_WINDOWS_ARM_EMULATOR': '/qemu-system-aarch64'}):
96 vms.save_preset({'name': 'template', 'id': 'prepared', 'os': 'Windows 11 Pro ARM64',
97 'description': '', 'setup': 'windows-v4'})
98 for name in ['first', 'second']:
99 vms.create({'name': name, 'description': '', 'image': 'prepared', 'mode': 'preset',
100 'owner': 'fixture', 'username': name, 'vcpus': 4, 'memory': 8 * 2**30,
101 'disk': 1024, 'start': False, 'autostart': False})
102 self.assertEqual((disks / name / 'nvram.fd').read_bytes(), (source / 'nvram.fd').read_bytes())
103 first = json.loads((disks / 'first' / 'access.json').read_text())
104 second = json.loads((disks / 'second' / 'access.json').read_text())
105 self.assertNotEqual(first['password'], second['password'])
106 self.assertEqual(first['username'], 'first')
107 self.assertEqual(second['username'], 'second')
108
109 def test_windows_setup_closes_milestone_files(self):
110 for stage in ['ready', 'failed', 'restarting', 'personalizing']:
111 def reply(action, name, payload, *args):
112 command = json.loads(payload)
113 if command['execute'] == 'guest-file-close':
114 self.assertEqual(command['arguments']['handle'], 7)
115 return '{"return": {}}'
116 if command['arguments']['path'].endswith('\\' + stage):
117 return '{"return": 7}'
118 raise vms.subprocess.CalledProcessError(1, 'virsh')
119 with self.subTest(stage=stage), patch.object(vms, 'virsh', side_effect=reply) as call:
120 self.assertEqual(vms.windows_setup('fixture'), stage)
121 self.assertEqual(json.loads(call.call_args.args[2])['execute'], 'guest-file-close')
122 with patch.object(vms, 'virsh', side_effect=vms.subprocess.CalledProcessError(1, 'virsh')):
123 self.assertEqual(vms.windows_setup('fixture'), 'starting')
124
125 def test_guest_channel_uses_libvirts_truncated_domain_name(self):
126 name = 'window-stream-win11-test'
127 path = f'/run/libvirt/qemu/channel/175-{name[:20]}/{vms.GUEST_CHANNEL}'
128 xml = f'<domain id="175"><devices><channel type="unix"><source mode="bind" path="{path}"/><target name="{vms.GUEST_CHANNEL}"/></channel></devices></domain>'
129 with patch.object(sys, 'argv', ['vms.py', 'guest', json.dumps({'name': name})]), \
130 patch.object(vms, 'virsh', side_effect=lambda action, *_: xml if action == 'dumpxml' else 'running'):
131 self.assertEqual(vms.main(), {'path': path})
132 for invalid in ['/tmp/display.sock', path.replace('175-', '176-'), path.replace(name[:20], name)]:
133 with self.subTest(path=invalid), patch.object(vms, 'virsh', side_effect=lambda action, *_: xml.replace(path, invalid) if action == 'dumpxml' else 'running'):
134 with self.assertRaises(ValueError):
135 vms.main()
136
137 def test_nested_installers_are_separate_from_prepared_guests(self):
138 with tempfile.TemporaryDirectory() as temporary:
139 root = Path(temporary).resolve()
140 installers = root / 'Install Disks'
141 installers.mkdir()
142 (installers / 'ubuntu-amd64.ISO').write_bytes(b'installer')
143 (installers / 'ubuntu-arm64.iso').write_bytes(b'installer')
144 (installers / 'not-a-preset.qcow2').write_bytes(b'disk')
145 (root / 'restore.ipsw').write_bytes(b'restore')
146 (installers / 'escaped.iso').symlink_to(root / 'restore.ipsw')
147 presets = root / 'Presets'
148 presets.mkdir()
149 (presets / 'ignored.iso').write_bytes(b'not an installer')
150 with patch.multiple(vms, IMAGES=root, UPLOADS=installers, PRESETS=presets):
151 library = vms.library()
152 self.assertEqual(len(library['installers']), 2)
153 self.assertEqual(library['presets'], [])
154 self.assertEqual({item['arch'] for item in library['installers']}, {'x86_64', 'aarch64'})
155 self.assertTrue(all(item['volume'].startswith('media:Install Disks/') for item in library['installers']))
156
157 def test_image_ids_and_parent_symlinks_cannot_escape(self):
158 for identity in ['../secret.iso', 'media:Install Disks/../../secret.iso', '/etc/shadow', 'upload:/secret.iso']:
159 with self.subTest(identity=identity), self.assertRaises(ValueError):
160 vms.validate_image_id(identity)
161 with tempfile.TemporaryDirectory() as temporary:
162 root = Path(temporary).resolve()
163 (root / 'outside').mkdir()
164 (root / 'outside' / 'secret.iso').write_bytes(b'secret')
165 (root / 'inside').symlink_to(root / 'outside', target_is_directory=True)
166 with self.assertRaises(OSError):
167 vms.open_regular(root / 'inside' / 'secret.iso')
168
169 def test_modern_create_rejects_incomplete_or_arbitrary_hardware(self):
170 payload = {'name': 'ubuntu', 'owner': 'fixture-user', 'username': 'fixture', 'description': '', 'image': 'media:Install Disks/ubuntu.iso',
171 'mode': 'iso', 'firmware': 'uefi', 'platform': 'linux', 'vcpus': 2,
172 'memory': 2**30, 'disk': 20 * 2**30, 'autostart': False, 'start': False}
173 with patch.object(vms, 'node', return_value={'cpus': 8, 'memory': 16 * 2**30}):
174 vms.validate('create', payload)
175 for changes in [{'mode': 'xml'}, {'firmware': '/etc/shadow'}, {'vcpus': True}, {'image': '../escape'}, {'argv': ['sh']}]:
176 with self.subTest(changes=changes), self.assertRaises(ValueError):
177 vms.validate('create', {**payload, **changes})
178 with self.assertRaises(ValueError):
179 vms.validate('create', {'name': 'ubuntu'})
180
181 def test_known_installers_choose_their_tested_hardware(self):
182 linux = {'platform': 'linux', 'firmware': 'uefi'}
183 windows = {'platform': 'windows', 'firmware': 'uefi'}
184 cases = {
185 'Windows XP Professional SP3 x86.iso': 'windows-legacy-ide',
186 'Windows Vista SP2 x64.iso': 'windows-legacy-ide',
187 'Windows 7 Professional.ISO': 'windows-legacy-ide',
188 'Windows 8 RTM x64.iso': 'windows-q35-bios',
189 'Windows 10 22H2 English x64.iso': 'windows-q35-uefi',
190 'Windows 11 26H2 English x64.iso': 'windows-q35-secure',
191 'Windows 11 26H2 English arm64.iso': 'windows-aarch64-nvme',
192 'Fedora-Workstation-Live-44-1.7.aarch64.iso': 'linux-aarch64-virtio',
193 }
194 for name, expected in cases.items():
195 with self.subTest(name=name):
196 spec = windows if name.startswith('Windows') else linux
197 self.assertEqual(vms.installer_profile(Path(name), spec), expected)
198 self.assertEqual(vms.profile('linux-aarch64-virtio')['video'], 'virtio')
199 self.assertEqual(vms.profile('linux-aarch64-virtio')['keyboard'], 'usb')
200 self.assertEqual(vms.profile('linux-aarch64-virtio')['serial'], 'ttyAMA0')
201 self.assertEqual(vms.profile('windows-q35-secure')['cpu_mode'], 'host-passthrough')
202 self.assertEqual(vms.profile('windows-q35-secure')['clock'], 'utc')
203
204 def test_existing_presets_receive_a_safe_profile(self):
205 with tempfile.TemporaryDirectory() as temporary:
206 presets = Path(temporary).resolve()
207 directory = presets / 'ubuntu'
208 directory.mkdir()
209 (directory / 'preset.json').write_text(json.dumps({
210 'id': 'ubuntu', 'os': 'Ubuntu', 'description': '', 'firmware': 'uefi',
211 'platform': 'linux', 'createdAt': 0, 'recommended': {'vcpus': 2, 'memory': 1, 'disk': 1},
212 }))
213 (directory / 'disk.qcow2').write_bytes(b'fixture')
214 details = {'format': 'qcow2', 'virtual-size': 123, 'format-specific': {'data': {}}}
215 with patch.object(vms, 'PRESETS', presets), patch.object(vms, 'command', return_value=json.dumps(details)):
216 preset = vms.read_preset('ubuntu')
217 self.assertEqual(preset['hardwareProfile'], 'linux-x86-virtio')
218 self.assertEqual(preset['arch'], 'x86_64')
219
220 def test_windows_seed_has_only_validated_clone_identity(self):
221 with tempfile.TemporaryDirectory() as temporary:
222 directory = Path(temporary)
223 calls = []
224 with patch.object(vms, 'command', side_effect=lambda *args, **kwargs: calls.append(args) or ''), \
225 patch.object(vms.secrets, 'token_urlsafe', return_value='generated-password'), \
226 patch.object(vms.secrets, 'token_hex', return_value='1234abcd'):
227 vms.windows_seed(directory, 'snow')
228 self.assertEqual(json.loads((directory / 'access.json').read_text()), {
229 'username': 'snow', 'password': 'generated-password', 'hostname': 'sgvm-1234abcd',
230 })
231 self.assertEqual(oct((directory / 'access.json').stat().st_mode & 0o777), '0o600')
232 self.assertEqual(calls[0][0:7], ('genisoimage', '-quiet', '-output', str(directory / 'seed.iso'), '-volid', 'SNOWGLOBE', '-joliet'))
233 self.assertEqual(Path(calls[0][-1]).name, 'SNOWGLOB.INI')
234 with self.assertRaisesRegex(ValueError, 'Windows'):
235 vms.windows_seed(directory, 'bad\\nname')
236
237 def test_one_boot_windows_seed_does_not_claim_a_new_computer_name(self):
238 with tempfile.TemporaryDirectory() as temporary:
239 directory = Path(temporary)
240 seeds = []
241 with patch.object(vms, 'command', side_effect=lambda *args, **kwargs: seeds.append(Path(args[-1]).read_text()) or ''):
242 vms.windows_seed(directory, 'fixture', rename=False)
243 self.assertNotIn('HOSTNAME=', seeds[0])
244 access = json.loads((directory / 'access.json').read_text())
245 self.assertEqual(access['hostname'], '')
246 self.assertEqual(access['username'], 'fixture')
247 self.assertGreaterEqual(len(access['password']), 12)
248
249 def test_nixos_seed_uses_mutable_gdm_path_and_native_sshd(self):
250 with tempfile.TemporaryDirectory() as temporary:
251 directory = Path(temporary)
252 captured = {}
253 def image(*args, **kwargs):
254 captured['config'] = json.loads(Path(args[-2]).read_text().removeprefix('#cloud-config\n'))
255 return ''
256 with patch.object(vms, 'command', side_effect=image), \
257 patch.object(vms.subprocess, 'run', return_value=SimpleNamespace(stdout='$6$hash\n')), \
258 patch.object(vms.secrets, 'token_urlsafe', return_value='generated-password'), \
259 patch.object(vms.secrets, 'token_hex', side_effect=['1234abcd', 'instance']):
260 vms.linux_seed(directory, 'snow', 'NixOS', 'ttyAMA0')
261 config = captured['config']
262 self.assertEqual(config['bootcmd'][0], ['rm', '-f', '/etc/gdm/custom.conf'])
263 self.assertIn('/run/current-system/sw/bin/getent', config['bootcmd'][1][-1])
264 self.assertEqual(config['write_files'][:2], [{
265 'path': '/etc/gdm/custom.conf', 'content': '[daemon]\nAutomaticLoginEnable=true\nAutomaticLogin=snow\n',
266 }, {
267 'path': '/var/lib/snowglobe/hostname',
268 'content': 'snowglobe-vm-1234abcd\n',
269 }])
270 self.assertEqual(config['runcmd'][0][-1], 'sshd')
271 self.assertEqual(config['runcmd'][1][-1], 'serial-getty@ttyAMA0.service')
272
273 def test_console_cannot_choose_an_arbitrary_network_target(self):
274 xml = "<domain><uuid>fixture-vm</uuid><devices><graphics type='vnc' listen='127.0.0.1' port='5901'/></devices></domain>"
275 def virsh(*args, **kwargs):
276 return 'running' if args[0] == 'domstate' else xml
277 with patch.object(vms, 'virsh', side_effect=virsh):
278 self.assertEqual(vms.console_target('ubuntu'), {'port': 5901, 'uuid': 'fixture-vm'})
279 for address in ['0.0.0.0', '192.168.0.1', 'localhost']:
280 xml = xml.replace('127.0.0.1', address)
281 with self.assertRaises(ValueError):
282 vms.console_target('ubuntu')
283 xml = xml.replace(address, '127.0.0.1')
284 xml = xml.replace('5901', '22')
285 with self.assertRaises(ValueError):
286 vms.console_target('ubuntu')
287
288 def test_running_guests_cannot_be_published_as_presets(self):
289 with patch.object(vms, 'virsh', return_value='running'):
290 with self.assertRaisesRegex(ValueError, 'Shut down'):
291 vms.save_preset({'name': 'ubuntu'})
292
293 def test_screen_waits_for_startup_without_following_a_replacement_vm(self):
294 with socket.socket() as reservation:
295 reservation.bind(('127.0.0.1', 0))
296 port = reservation.getsockname()[1]
297 with socket.socket() as display:
298 retry, listening = threading.Event(), threading.Event()
299 errors = []
300 class StartingHost:
301 calls = 0
302 def handle(self, request):
303 self.calls += 1
304 if self.calls == 2:
305 retry.set()
306 if not listening.wait(2):
307 raise TimeoutError('Display did not start')
308 return {'port': port, 'uuid': 'original-vm'}
309 target = StartingHost()
310 client, server = socket.socketpair()
311 client.settimeout(2)
312 def run():
313 with server:
314 try:
315 host.stream_console(server, target, {'operation': 'vm.console'})
316 except Exception as error:
317 errors.append(error)
318 worker = threading.Thread(target=run)
319 worker.start()
320 try:
321 self.assertTrue(retry.wait(2))
322 display.bind(('127.0.0.1', port))
323 display.listen()
324 display.settimeout(2)
325 listening.set()
326 with display.accept()[0] as guest:
327 length = struct.unpack('!I', client.recv(4))[0]
328 self.assertEqual(json.loads(client.recv(length)), {'value': None})
329 guest.sendall(b'RFB 003.008\n')
330 self.assertEqual(client.recv(12), b'RFB 003.008\n')
331 finally:
332 listening.set()
333 client.close()
334 worker.join(2)
335 self.assertFalse(worker.is_alive())
336 self.assertEqual(errors, [])
337 with patch.object(host.socket, 'create_connection', side_effect=ConnectionRefusedError) as connect:
338 target = StartingHost()
339 with patch.object(target, 'handle', side_effect=[{'port': port, 'uuid': 'original-vm'}, {'port': port, 'uuid': 'replacement-vm'}]):
340 with self.assertRaisesRegex(host.Rejected, 'VM changed'):
341 host.stream_console(None, target, {'operation': 'vm.console'})
342 self.assertEqual(connect.call_count, 1)
343
344 def test_serial_target_cannot_choose_a_host_file(self):
345 xml = "<domain><devices><console type='pty'><source path='/dev/pts/3'/><target type='serial'/></console></devices></domain>"
346 def virsh(*args, **kwargs):
347 return 'running' if args[0] == 'domstate' else xml
348 with patch.object(sys, 'argv', ['vms.py', 'serial', json.dumps({'name': 'ubuntu'})]), patch.object(vms, 'virsh', side_effect=virsh):
349 self.assertEqual(vms.main(), {'path': '/dev/pts/3'})
350 for path in ['/etc/shadow', '/dev/null', '/dev/pts/../tty', '/dev/pts/3/link']:
351 xml = xml.replace('/dev/pts/3', path)
352 with self.assertRaises(ValueError):
353 vms.main()
354 xml = xml.replace(path, '/dev/pts/3')
355
356 def test_serial_transport_preserves_control_c_in_both_directions(self):
357 master, slave = pty.openpty()
358 client, server = socket.socketpair()
359 client.settimeout(2)
360 errors = []
361 class SerialHost:
362 def handle(self, request):
363 return {'path': os.ttyname(slave)}
364 def run():
365 with server:
366 try:
367 host.stream_console(server, SerialHost(), {'operation': 'vm.serial'})
368 except Exception as error:
369 errors.append(error)
370 worker = threading.Thread(target=run)
371 worker.start()
372 try:
373 length = struct.unpack('!I', client.recv(4))[0]
374 self.assertEqual(json.loads(client.recv(length)), {'value': None})
375 other_client, other_server = socket.socketpair()
376 with other_client, other_server, self.assertRaisesRegex(host.Rejected, 'already open'):
377 host.stream_console(other_server, SerialHost(), {'operation': 'vm.serial'})
378 client.sendall(b'\x03')
379 self.assertTrue(select.select([master], [], [], 2)[0])
380 self.assertEqual(os.read(master, 100), b'\x03')
381 os.write(master, b'guest prompt> ')
382 self.assertEqual(client.recv(100), b'guest prompt> ')
383 finally:
384 client.close()
385 worker.join(2)
386 os.close(master)
387 os.close(slave)
388 self.assertFalse(worker.is_alive())
389 self.assertEqual(errors, [])
390
391 def test_upload_stream_is_bounded_atomic_and_cleans_interruption(self):
392 def response(sock):
393 length = struct.unpack('!I', sock.recv(4))[0]
394 data = bytearray()
395 while len(data) < length:
396 data.extend(sock.recv(length - len(data)))
397 return json.loads(data)
398 with tempfile.TemporaryDirectory() as temporary, patch.object(vms, 'UPLOADS', Path(temporary).resolve()):
399 for complete in [False, True]:
400 client, server = socket.socketpair()
401 errors = []
402 def run():
403 with server:
404 try:
405 host.stream_upload(server, {'operation': 'vm.upload', 'payload': {'volume': 'ubuntu.iso', 'size': 32768}})
406 except Exception as error:
407 errors.append(error)
408 worker = threading.Thread(target=run)
409 worker.start()
410 with client:
411 self.assertEqual(response(client), {'value': None})
412 client.sendall(b'I' * (32768 if complete else 100))
413 client.shutdown(socket.SHUT_WR)
414 if complete:
415 self.assertEqual(response(client)['value']['volume'], 'upload:ubuntu.iso')
416 worker.join(2)
417 self.assertFalse(worker.is_alive())
418 if complete:
419 self.assertEqual((Path(temporary).resolve() / 'ubuntu.iso').read_bytes(), b'I' * 32768)
420 else:
421 self.assertTrue(errors)
422 self.assertEqual(list(Path(temporary).resolve().iterdir()), [])
423 with self.assertRaises(host.Rejected):
424 host.stream_upload(None, {'operation': 'vm.upload', 'payload': {'volume': 'ubuntu.iso', 'size': 32768}})
425 for filename in ['../ubuntu.iso', '/etc/shadow.iso', 'ubuntu.img']:
426 with self.assertRaises(ValueError):
427 vms.validate('upload', {'volume': filename, 'size': 32768})
428
429
430if __name__ == '__main__':
431 unittest.main()