-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathqrexec_policy_graph.py
213 lines (192 loc) · 6.47 KB
/
qrexec_policy_graph.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
# -*- 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/>.
import argparse
import json
import pathlib
import sys
from .. import POLICYPATH
from .. import exc
from .. import utils
from ..policy import parser
argparser = argparse.ArgumentParser(description="Graph qrexec policy")
argparser.add_argument(
"--include-ask", action="store_true", help="Include `ask` action in graph"
)
argparser.add_argument(
"--source",
action="store",
nargs="+",
help="Limit graph to calls from *source*",
)
argparser.add_argument(
"--target",
action="store",
nargs="+",
help="Limit graph to calls to *target*",
)
argparser.add_argument(
"--service", action="store", nargs="+", help="Limit graph to *service*"
)
argparser.add_argument(
"--output", action="store", help="Write to *output* instead of stdout"
)
argparser.add_argument(
"--policy-dir",
action="store",
type=pathlib.Path,
default=POLICYPATH,
help="Look for policy in *policy-dir*",
)
argparser.add_argument(
"--system-info",
action="store",
help="Load system information from file instead of querying qubesd",
)
argparser.add_argument(
"--skip-labels",
action="store_true",
help="Do not include service names on the graph, also deduplicate "
"connections.",
)
argparser.add_argument(
"--full-output",
action="store_true",
help="Output complete policy scheme, including detailed action "
"specifications and service arguments.",
)
def handle_single_action(args, action):
"""Get single policy action and output (or not) a line to add"""
if args.skip_labels:
service = ""
else:
service = action.request.service
if args.full_output and action.rule.argument:
service += action.rule.argument
target = action.request.target
# handle forced target=
if action.rule.action.target:
target = action.rule.action.target
if args.target and target not in args.target:
return ""
if args.full_output:
color = "orange" if isinstance(action, parser.AskResolution) else "red"
return (
f' "{action.request.source}" -> "{target}" '
f'[label="{service} {action.rule.action}" color={color}];\n'
)
if isinstance(action, parser.AskResolution):
if args.include_ask:
return f' "{action.request.source}" -> "{target}" [label="{service}" color=orange];\n'
elif isinstance(action, parser.AllowResolution):
return f' "{action.request.source}" -> "{target}" [label="{service}" color=red];\n'
return ""
def main(args=None, output=sys.stdout):
# pylint: disable=too-many-branches,too-many-statements
args = argparser.parse_args(args)
if args.source:
sources = set()
for source in args.source:
try:
sources.add(parser.Source(source))
except exc.PolicySyntaxError:
argparser.error(f"--source {source} is not a valid call source")
args.source = sources
if args.target:
targets = set()
for target in args.target:
try:
targets.add(parser.Redirect(target))
except exc.PolicySyntaxError:
argparser.error(
f"--target {target} is not a valid actual call target"
)
args.target = targets
if args.output:
# pylint: disable=consider-using-with
output = open(args.output, "w", encoding="utf-8")
if args.system_info:
with open(args.system_info, encoding="utf-8") as f_system_info:
system_info = json.load(f_system_info)
else:
system_info = utils.get_system_info()
targets = [
key for key in system_info["domains"] if not key.startswith("uuid")
]
if args.source:
sources = args.source
else:
sources = list(targets)
targets.append("@dispvm")
targets.extend(
"@dispvm:" + dom
for dom in system_info["domains"]
if (
not dom.startswith("uuid:")
and system_info["domains"][dom]["template_for_dispvms"]
)
)
targets.append("@default")
connections = set()
policy = parser.FilePolicy(policy_path=args.policy_dir)
output.write("digraph g {\n")
services = set()
for rule in policy.rules:
services.add((rule.service, rule.argument))
# in case of just wildcard rules above, ensure explicit
# service/arg is considered too
for service in args.service or []:
arg = None
if "+" in service:
service, arg = service.split("+", 1)
arg = "+" + arg
services.add((service, arg))
for service, argument in sorted(
services, key=lambda x: (x[0] or "", x[1] or "")
):
if (
args.service
and service not in args.service
and not service + (argument or "") in args.service
):
continue
for source in sources:
for target in targets:
try:
request = parser.Request(
service,
argument or "+",
source,
target,
system_info=system_info,
)
action = policy.evaluate(request)
line = handle_single_action(args, action)
if line in connections:
continue
if line:
output.write(line)
connections.add(line)
except exc.AccessDenied:
continue
output.write("}\n")
if args.output:
output.close()
if __name__ == "__main__":
sys.exit(main())