-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
internal.py
286 lines (251 loc) · 9.66 KB
/
internal.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
# -*- encoding: utf-8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2017 Marek Marczykowski-Górecki
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
""" Internal interface for dom0 components to communicate with qubesd. """
import asyncio
import json
import subprocess
import qubes.api
import qubes.api.admin
import qubes.vm.adminvm
import qubes.vm.dispvm
def get_system_info(app):
system_info = {
"domains": {
domain.name: {
"tags": list(domain.tags),
"type": domain.__class__.__name__,
"template_for_dispvms": getattr(
domain, "template_for_dispvms", False
),
"default_dispvm": (
domain.default_dispvm.name
if getattr(domain, "default_dispvm", None)
else None
),
"icon": str(domain.label.icon),
"guivm": (
domain.guivm.name
if getattr(domain, "guivm", None)
else None
),
"power_state": domain.get_power_state(),
"uuid": str(domain.uuid),
}
for domain in app.domains
}
}
return system_info
class QubesInternalAPI(qubes.api.AbstractQubesAPI):
"""Communication interface for dom0 components,
by design the input here is trusted."""
SOCKNAME = "/var/run/qubesd.internal.sock"
@qubes.api.method("internal.GetSystemInfo", no_payload=True)
async def getsysteminfo(self):
self.enforce(self.dest.name == "dom0")
self.enforce(not self.arg)
system_info = get_system_info(self.app)
return json.dumps(system_info)
@qubes.api.method(
"internal.vm.volume.ImportBegin", scope="local", write=True
)
async def vm_volume_import(self, untrusted_payload):
"""Begin importing volume data. Payload is either size of new data
in bytes, or empty. If empty, the current volume's size will be used.
Returns size and path to where data should be written.
Triggered by scripts in /etc/qubes-rpc:
admin.vm.volume.Import, admin.vm.volume.ImportWithSize.
When the script finish importing, it will trigger
internal.vm.volume.ImportEnd (with either b'ok' or b'fail' as a
payload) and response from that call will be actually send to the
caller.
"""
self.enforce(self.arg in self.dest.volumes.keys())
if untrusted_payload:
original_method = "admin.vm.volume.ImportWithSize"
else:
original_method = "admin.vm.volume.Import"
self.src.fire_event(
"admin-permission:" + original_method,
pre_event=True,
dest=self.dest,
arg=self.arg,
)
if not self.dest.is_halted():
raise qubes.exc.QubesVMNotHaltedError(self.dest)
requested_size = (
self.validate_size(untrusted_payload) if untrusted_payload else None
)
del untrusted_payload
path = await self.dest.storage.import_data(self.arg, requested_size)
self.enforce(" " not in path)
if requested_size is None:
size = self.dest.volumes[self.arg].size
else:
size = requested_size
# when we know the action is allowed, inform extensions that it will
# be performed
self.dest.fire_event(
"domain-volume-import-begin", volume=self.arg, size=size
)
return "{} {}".format(size, path)
@qubes.api.method("internal.vm.volume.ImportEnd")
async def vm_volume_import_end(self, untrusted_payload):
"""
This is second half of admin.vm.volume.Import handling. It is called
when actual import is finished. Response from this method is sent do
the client (as a response for admin.vm.volume.Import call).
The payload is either 'ok', or 'fail\n<error message>'.
"""
self.enforce(self.arg in self.dest.volumes.keys())
success = untrusted_payload == b"ok"
try:
await self.dest.storage.import_data_end(self.arg, success=success)
except:
self.dest.fire_event(
"domain-volume-import-end", volume=self.arg, success=False
)
raise
self.dest.fire_event(
"domain-volume-import-end", volume=self.arg, success=success
)
if not success:
error = ""
parts = untrusted_payload.decode("ascii").split("\n", 1)
if len(parts) > 1:
error = parts[1]
raise qubes.exc.QubesException(
"Data import failed: {}".format(error)
)
@qubes.api.method("internal.SuspendPre", no_payload=True)
async def suspend_pre(self):
"""
Method called before host system goes to sleep.
:return:
"""
# first notify all VMs
processes = []
for vm in self.app.domains:
if isinstance(vm, qubes.vm.adminvm.AdminVM):
continue
if not vm.is_running():
continue
if not vm.features.check_with_template("qrexec", False):
continue
try:
proc = await vm.run_service(
"qubes.SuspendPreAll",
user="root",
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
processes.append(proc)
except qubes.exc.QubesException as e:
vm.log.warning("Failed to run qubes.SuspendPreAll: %s", str(e))
if processes:
done, _ = await asyncio.wait(
[
asyncio.create_task(
asyncio.wait_for(p.wait(), qubes.config.suspend_timeout)
)
for p in processes
]
)
for task in done:
try:
task.result()
except asyncio.TimeoutError:
self.app.log.warning(
"some qube timed out after %d seconds on %s call",
qubes.config.suspend_timeout,
"qubes.SuspendPreAll",
)
coros = []
# then suspend/pause VMs
for vm in self.app.domains:
if isinstance(vm, qubes.vm.adminvm.AdminVM):
continue
if vm.is_running():
coros.append(asyncio.create_task(vm.suspend()))
if coros:
done, _ = await asyncio.wait(coros)
failed = ""
for coro in done:
try:
coro.result()
except Exception as e: # pylint: disable=broad-except
failed += f"\n{e!s}"
if failed:
raise qubes.exc.QubesException(
"Failed to suspend some qubes: {}".format(failed)
)
@qubes.api.method("internal.SuspendPost", no_payload=True)
async def suspend_post(self):
"""
Method called after host system wake up from sleep.
:return:
"""
coros = []
# first resume/unpause VMs
for vm in self.app.domains:
if isinstance(vm, qubes.vm.adminvm.AdminVM):
continue
if vm.get_power_state() in ["Paused", "Suspended"]:
coros.append(asyncio.create_task(vm.resume()))
if coros:
await asyncio.wait(coros)
# then notify all VMs
processes = []
for vm in self.app.domains:
if isinstance(vm, qubes.vm.adminvm.AdminVM):
continue
if not vm.is_running():
continue
if not vm.features.check_with_template("qrexec", False):
continue
try:
proc = await vm.run_service(
"qubes.SuspendPostAll",
user="root",
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
processes.append(proc)
except qubes.exc.QubesException as e:
vm.log.warning("Failed to run qubes.SuspendPostAll: %s", str(e))
if processes:
done, _ = await asyncio.wait(
[
asyncio.create_task(
asyncio.wait_for(p.wait(), qubes.config.suspend_timeout)
)
for p in processes
]
)
for task in done:
try:
task.result()
except asyncio.TimeoutError:
self.app.log.warning(
"some qube timed out after %d seconds on %s call",
qubes.config.suspend_timeout,
"qubes.SuspendPostAll",
)