-
Notifications
You must be signed in to change notification settings - Fork 39
/
Updater.py
528 lines (442 loc) · 18 KB
/
Updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#!/usr/bin/env python3
"""
Utility library for checking and applying SecureDrop Workstation VM updates.
This library is meant to be called by the SecureDrop launcher, which
is opened by the user when clicking on the desktop, opening sdw-laucher.py
from the parent directory.
"""
import json
import logging
import os
import subprocess
from datetime import datetime, timedelta
from enum import Enum
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
DEFAULT_HOME = ".securedrop_launcher"
FLAG_FILE_STATUS_SD_APP = "/home/user/.securedrop_client/sdw-update-status"
FLAG_FILE_LAST_UPDATED_SD_APP = "/home/user/.securedrop_client/sdw-last-updated"
FLAG_FILE_STATUS_DOM0 = os.path.join(DEFAULT_HOME, "sdw-update-status")
FLAG_FILE_LAST_UPDATED_DOM0 = os.path.join(DEFAULT_HOME, "sdw-last-updated")
LOCK_FILE = "sdw-launcher.lock"
LOG_FILE = "launcher.log"
sdlog = logging.getLogger(__name__)
# The are the TemplateVMs that require full patch level at boot in order to start the client,
# as well as their associated TemplateVMs.
# In the future, we could use qvm-prefs to extract this information.
current_templates = {
"dom0": "dom0",
"fedora": "fedora-31",
"sd-viewer": "sd-viewer-buster-template",
"sd-app": "sd-app-buster-template",
"sd-log": "sd-log-buster-template",
"sd-devices": "sd-devices-buster-template",
"sd-proxy": "sd-proxy-buster-template",
"sd-whonix": "whonix-gw-15",
"sd-gpg": "securedrop-workstation-buster",
}
def get_dom0_path(folder):
return os.path.join(os.path.expanduser("~"), folder)
def check_all_updates():
"""
Check for updates for all vms listed in current_templates above
"""
sdlog.info("Checking for all updates")
for progress_current, vm in enumerate(current_templates.keys()):
# yield the progress percentage for UI updates
progress_percentage = int(((progress_current + 1) / len(current_templates.keys())) * 100)
update_results = check_updates(vm)
yield vm, progress_percentage, update_results
def check_updates(vm):
"""
Check for updates for a single VM
"""
if vm == "dom0":
return _check_updates_dom0()
elif vm == "fedora":
return _check_updates_fedora()
else:
return _check_updates_debian(vm)
def apply_updates(vms):
"""
Apply updates to the TemplateVMs of VM list specified in parameter
"""
sdlog.info("Applying all updates")
for progress_current, vm in enumerate(vms):
upgrade_results = UpdateStatus.UPDATES_FAILED
if vm == "dom0":
upgrade_results = _apply_updates_dom0()
else:
upgrade_results = _apply_updates_vm(vm)
progress_percentage = int(((progress_current + 1) / len(vms)) * 100 - 5)
yield vm, progress_percentage, upgrade_results
def _check_updates_dom0():
"""
Check for dom0 updates
"""
try:
subprocess.check_call(["sudo", "qubes-dom0-update", "--check-only"])
except subprocess.CalledProcessError as e:
sdlog.error("dom0 updates required or cannot check for updates")
sdlog.error(str(e))
return UpdateStatus.UPDATES_REQUIRED
sdlog.info("dom0 is up to date")
return UpdateStatus.UPDATES_OK
def _check_updates_fedora():
"""
Check for updates to the default Fedora TemplateVM. Fedora has a very rapid
release cycle and there are almost always updates to fedora VMs. Let's just
return UPDATES_REQUIRED and always upgrade those VMs, since they no longer
trigger a full workstation reboot on upgrade.
"""
return UpdateStatus.UPDATES_REQUIRED
def _check_updates_debian(vm):
"""
Check for updates for a given Debian-based TemplateVM
"""
updates_required = False
try:
# There is no apt command that uses exit codes in such a way that we can discover if
# updates are required by relying on exit codes.
# Since we don't want to use --pass-io and parse the output, we have to count
# the lines on the vm output
sdlog.info("Checking for updates {}:{}".format(vm, current_templates[vm]))
subprocess.check_call(["qvm-run", current_templates[vm], "sudo apt update"])
subprocess.check_call(
["qvm-run", current_templates[vm], "[[ $(apt list --upgradable | wc -l) -eq 1 ]]"]
)
except subprocess.CalledProcessError as e:
sdlog.error(
"Updates required for {} or cannot check for updates".format(current_templates[vm])
)
sdlog.error(str(e))
updates_required = True
finally:
reboot_status = _safely_shutdown_vm(current_templates[vm])
if reboot_status == UpdateStatus.UPDATES_FAILED:
return reboot_status
if not updates_required:
sdlog.info("{} is up to date".format(current_templates[vm]))
return UpdateStatus.UPDATES_OK
else:
return UpdateStatus.UPDATES_REQUIRED
def _apply_updates_dom0():
"""
Apply updates to dom0. Any update to dom0 will require a reboot after
the upgrade.
"""
sdlog.info("Updating dom0")
try:
subprocess.check_call(["sudo", "qubes-dom0-update", "-y"])
except subprocess.CalledProcessError as e:
sdlog.error("An error has occurred updating dom0. Please contact your administrator.")
sdlog.error(str(e))
return UpdateStatus.UPDATES_FAILED
sdlog.info("dom0 update successful")
return UpdateStatus.REBOOT_REQUIRED
def _apply_updates_vm(vm):
"""
Apply updates to a given TemplateVM. Any update to the base fedora template
will require a reboot after the upgrade.
"""
sdlog.info("Updating {}:{}".format(vm, current_templates[vm]))
try:
subprocess.check_call(
[
"sudo",
"qubesctl",
"--skip-dom0",
"--targets",
current_templates[vm],
"state.sls",
"update.qubes-vm",
]
)
except subprocess.CalledProcessError as e:
sdlog.error(
"An error has occurred updating {}. Please contact your administrator.".format(
current_templates[vm]
)
)
sdlog.error(str(e))
return UpdateStatus.UPDATES_FAILED
sdlog.info("{} update successful".format(current_templates[vm]))
return UpdateStatus.UPDATES_OK
def _write_last_updated_flags_to_disk():
"""
Writes the time of last successful upgrade to dom0 and sd-app
"""
current_date = str(datetime.now().strftime(DATE_FORMAT))
flag_file_sd_app_last_updated = FLAG_FILE_LAST_UPDATED_SD_APP
flag_file_dom0_last_updated = get_dom0_path(FLAG_FILE_LAST_UPDATED_DOM0)
try:
sdlog.info("Setting last updated to {} in sd-app".format(current_date))
subprocess.check_call(
[
"qvm-run",
"sd-app",
"echo '{}' > {}".format(current_date, flag_file_sd_app_last_updated),
]
)
except subprocess.CalledProcessError as e:
sdlog.error("Error writing last updated flag to sd-app")
sdlog.error(str(e))
try:
sdlog.info("Setting last updated to {} in dom0".format(current_date))
if not os.path.exists(os.path.dirname(flag_file_dom0_last_updated)):
os.makedirs(os.path.dirname(flag_file_dom0_last_updated))
with open(flag_file_dom0_last_updated, "w") as f:
f.write(current_date)
except Exception as e:
sdlog.error("Error writing last updated flag to dom0")
sdlog.error(str(e))
def _write_updates_status_flag_to_disk(status):
"""
Writes the latest SecureDrop Workstation update status to disk, on both
dom0 and sd-app for futher processing in the future.
"""
flag_file_path_sd_app = FLAG_FILE_STATUS_SD_APP
flag_file_path_dom0 = get_dom0_path(FLAG_FILE_STATUS_DOM0)
try:
sdlog.info("Setting update flag to {} in sd-app".format(status.value))
subprocess.check_call(
["qvm-run", "sd-app", "echo '{}' > {}".format(status.value, flag_file_path_sd_app)]
)
except subprocess.CalledProcessError as e:
sdlog.error("Error writing update status flag to sd-app")
sdlog.error(str(e))
try:
sdlog.info("Setting update flag to {} in dom0".format(status.value))
if not os.path.exists(os.path.dirname(flag_file_path_dom0)):
os.makedirs(os.path.dirname(flag_file_path_dom0))
current_date = str(datetime.now().strftime(DATE_FORMAT))
with open(flag_file_path_dom0, "w") as f:
flag_contents = {"last_status_update": current_date, "status": status.value}
json.dump(flag_contents, f)
except Exception as e:
sdlog.error("Error writing update status flag to dom0")
sdlog.error(str(e))
def last_required_reboot_performed():
"""
Checks if the dom0 flag file indicates that a reboot is required, and
if so, will check current uptime with the data at which the reboot
was requested. This will be used by the _write_updates_status_flag_to_disk
function to preserve the status UPDATES_REQUIRED instead of updating.
"""
flag_contents = read_dom0_update_flag_from_disk(with_timestamp=True)
# No flag exists on disk (yet)
if flag_contents is None:
return True
if int(flag_contents["status"]) == int(UpdateStatus.REBOOT_REQUIRED.value):
reboot_time = datetime.strptime(flag_contents["last_status_update"], DATE_FORMAT)
boot_time = datetime.now() - _get_uptime()
# The session was started *before* the reboot was requested by
# the launcher, system was not rebooted after previous run
if boot_time < reboot_time:
return False
# system was rebooted after flag was written to disk
else:
return True
# previous run did not require reboot
else:
return True
def _get_uptime():
"""
Returns timedelta containing system (dom0) uptime.
"""
uptime = None
with open("/proc/uptime", "r") as f:
uptime = f.read().split(" ")[0].strip()
uptime = int(float(uptime))
uptime_hours = uptime // 3600
uptime_minutes = (uptime % 3600) // 60
uptime_seconds = uptime % 60
delta = timedelta(hours=uptime_hours, minutes=uptime_minutes, seconds=uptime_seconds)
return delta
def read_dom0_update_flag_from_disk(with_timestamp=False):
"""
Read the last updated SecureDrop Workstation update status from disk
in dom0, and returns the corresponding UpdateStatus. If ivoked the
parameter `with_timestamp=True`, this function will return the full json.
"""
flag_file_path_dom0 = get_dom0_path(FLAG_FILE_STATUS_DOM0)
try:
with open(flag_file_path_dom0, "r") as f:
contents = json.load(f)
for status in UpdateStatus:
if int(contents["status"]) == int(status.value):
if with_timestamp:
return contents
else:
return status
except Exception:
sdlog.info("Cannot read dom0 status flag, assuming first run")
return None
def overall_update_status(results):
"""
Helper method that returns the worst-case status
For now, simple logic for reboot required: If dom0 or fedora updates, a
reboot will be required.
"""
updates_failed = False
updates_required = False
reboot_required = False
# Ensure the user has rebooted after the previous installer run required a reboot
if not last_required_reboot_performed():
return UpdateStatus.REBOOT_REQUIRED
for result in results.values():
if result == UpdateStatus.UPDATES_FAILED:
updates_failed = True
elif result == UpdateStatus.REBOOT_REQUIRED:
reboot_required = True
elif result == UpdateStatus.UPDATES_REQUIRED:
updates_required = True
if updates_failed:
return UpdateStatus.UPDATES_FAILED
elif reboot_required:
return UpdateStatus.REBOOT_REQUIRED
elif updates_required:
return UpdateStatus.UPDATES_REQUIRED
else:
return UpdateStatus.UPDATES_OK
def apply_dom0_state():
"""
Applies the dom0 state to ensure dom0 and AppVMs are properly
Configured. This will *not* enforce configuration inside the AppVMs.
Here, we call qubectl directly (instead of through securedrop-admin) to
ensure it is environment-specific.
"""
sdlog.info("Applying dom0 state")
try:
subprocess.check_call(["sudo", "qubesctl", "--show-output", "state.highstate"])
sdlog.info("Dom0 state applied")
return UpdateStatus.UPDATES_OK
except subprocess.CalledProcessError as e:
sdlog.error("Failed to dom0 state")
sdlog.error(str(e))
return UpdateStatus.UPDATES_FAILED
def shutdown_and_start_vms():
"""
Power cycles the vms to ensure. we should do them all in one shot to reduce complexity
and likelihood of failure. Rebooting the VMs will ensure the TemplateVM
updates are picked up by the AppVM. We must first shut all VMs down to ensure
correct order of operations, as sd-whonix cannot shutdown if sd-proxy is powered
on, for example.
All system AppVMs (sys-net, sys-firewall and sys-usb) need to be restarted.
We use qvm-kill for sys-firewall and sys-net, because a shutdown may fail
if they are currently in use as NetVMs by any of the user's other VMs.
"""
sdw_vms_in_order = [
"sd-app",
"sd-proxy",
"sd-whonix",
"sd-gpg",
"sd-log",
]
# All TemplateVMs minus dom0
sdw_templates = [val for key, val in current_templates.items() if key != "dom0"]
sdlog.info("Shutting down SDW TemplateVMs for updates")
for vm in sdw_templates:
_safely_shutdown_vm(vm)
sdlog.info("Shutting down SDW AppVMs for updates")
for vm in sdw_vms_in_order:
_safely_shutdown_vm(vm)
# System VMs that can be safely shut down (order should not matter, but will
# be respected).
safe_sys_vms_in_order = ["sys-usb", "sys-whonix"]
for vm in safe_sys_vms_in_order:
sdlog.info("Safely shutting down system VM: {}".format(vm))
_safely_shutdown_vm(vm)
# TODO: Use of qvm-kill should be considered unsafe and may have unexpected
# side effects. We should aim for a more graceful shutdown strategy.
unsafe_sys_vms_in_order = ["sys-firewall", "sys-net"]
for vm in unsafe_sys_vms_in_order:
sdlog.info("Killing system VM: {}".format(vm))
try:
subprocess.check_output(["qvm-kill", vm], stderr=subprocess.PIPE)
except subprocess.CalledProcessError as e:
sdlog.error("Error while killing system VM: {}".format(vm))
sdlog.error(str(e))
sdlog.error(str(e.stderr))
all_sys_vms_in_order = safe_sys_vms_in_order + unsafe_sys_vms_in_order
sdlog.info("Starting fedora-based system VMs after updates")
for vm in reversed(all_sys_vms_in_order):
_safely_start_vm(vm)
sdlog.info("Starting SDW VMs after updates")
for vm in reversed(sdw_vms_in_order):
_safely_start_vm(vm)
def _safely_shutdown_vm(vm):
try:
subprocess.check_output(["qvm-shutdown", "--wait", vm], stderr=subprocess.PIPE)
except subprocess.CalledProcessError as e:
sdlog.error("Failed to shut down {}".format(vm))
sdlog.error(str(e))
sdlog.error(str(e.stderr))
return UpdateStatus.UPDATES_FAILED
def _safely_start_vm(vm):
try:
running_vms = subprocess.check_output(
["qvm-ls", "--running", "--raw-list"], stderr=subprocess.PIPE
)
sdlog.info("VMs running before start of {}: {}".format(vm, running_vms))
subprocess.check_output(["qvm-start", "--skip-if-running", vm], stderr=subprocess.PIPE)
except subprocess.CalledProcessError as e:
sdlog.error("Error while starting {}".format(vm))
sdlog.error(str(e))
sdlog.error(str(e.stderr))
def should_launch_updater(interval):
status = read_dom0_update_flag_from_disk(with_timestamp=True)
if _valid_status(status):
if _interval_expired(interval, status):
sdlog.info("Update interval expired: launching updater.")
return True
else:
if status["status"] == UpdateStatus.UPDATES_OK.value:
sdlog.info("Updates OK and interval not expired, launching client.")
return False
elif status["status"] == UpdateStatus.REBOOT_REQUIRED.value:
if last_required_reboot_performed():
sdlog.info("Required reboot performed, updating status and launching client.")
_write_updates_status_flag_to_disk(UpdateStatus.UPDATES_OK)
return False
else:
sdlog.info("Required reboot pending, launching updater")
return True
elif status["status"] == UpdateStatus.UPDATES_REQUIRED.value:
sdlog.info("Updates are required, launching updater.")
return True
elif status["status"] == UpdateStatus.UPDATES_FAILED.value:
sdlog.info("Preceding update failed, launching updater.")
return True
else:
sdlog.info("Update status is unknown, launching updater.")
return True
else:
sdlog.info("Update status not available, launching updater.")
return True
def _valid_status(status):
"""
status should contain 2 items, the update flag and a timestamp.
"""
if isinstance(status, dict) and len(status) == 2:
return True
return False
def _interval_expired(interval, status):
"""
Check if specified update interval has expired.
"""
try:
update_time = datetime.strptime(status["last_status_update"], DATE_FORMAT)
except ValueError:
# Broken timestamp? run the updater.
return True
if (datetime.now() - update_time) < timedelta(seconds=interval):
return False
return True
class UpdateStatus(Enum):
"""
Standardizes return codes for update/upgrade methods
"""
UPDATES_OK = "0"
UPDATES_REQUIRED = "1"
REBOOT_REQUIRED = "2"
UPDATES_FAILED = "3"