/usr/lib/python3/dist-packages/setools/infoflow.py is in python3-setools 4.0.1-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright 2014-2015, Tresys Technology, LLC
#
# This file is part of SETools.
#
# SETools 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.
#
# SETools 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 SETools. If not, see
# <http://www.gnu.org/licenses/>.
#
import itertools
import logging
import networkx as nx
from networkx.exception import NetworkXError, NetworkXNoPath
from .descriptors import EdgeAttrIntMax, EdgeAttrList
__all__ = ['InfoFlowAnalysis']
class InfoFlowAnalysis(object):
"""Information flow analysis."""
def __init__(self, policy, perm_map, min_weight=1, exclude=None):
"""
Parameters:
policy The policy to analyze.
perm_map The permission map or path to the permission map file.
minweight The minimum permission weight to include in the analysis.
(default is 1)
exclude The types excluded from the information flow analysis.
(default is none)
"""
self.log = logging.getLogger(__name__)
self.policy = policy
self.min_weight = min_weight
self.perm_map = perm_map
self.exclude = exclude
self.rebuildgraph = True
self.rebuildsubgraph = True
self.G = nx.DiGraph()
self.subG = None
@property
def min_weight(self):
return self._min_weight
@min_weight.setter
def min_weight(self, weight):
if not 1 <= weight <= 10:
raise ValueError(
"Min information flow weight must be an integer 1-10.")
self._min_weight = weight
self.rebuildsubgraph = True
@property
def perm_map(self):
return self._perm_map
@perm_map.setter
def perm_map(self, perm_map):
self._perm_map = perm_map
self.rebuildgraph = True
self.rebuildsubgraph = True
@property
def exclude(self):
return self._exclude
@exclude.setter
def exclude(self, types):
if types:
self._exclude = [self.policy.lookup_type(t) for t in types]
else:
self._exclude = []
self.rebuildsubgraph = True
def shortest_path(self, source, target):
"""
Generator which yields one shortest path between the source
and target types (there may be more).
Parameters:
source The source type.
target The target type.
Yield: generator(steps)
steps Yield: tuple(source, target, rules)
source The source type for this step of the information flow.
target The target type for this step of the information flow.
rules The list of rules creating this information flow step.
"""
s = self.policy.lookup_type(source)
t = self.policy.lookup_type(target)
if self.rebuildsubgraph:
self._build_subgraph()
self.log.info("Generating one shortest information flow path from {0} to {1}...".
format(s, t))
try:
yield self.__generate_steps(nx.shortest_path(self.subG, s, t))
except (NetworkXNoPath, NetworkXError):
# NetworkXError: the type is valid but not in graph, e.g.
# excluded or disconnected due to min weight
# NetworkXNoPath: no paths or the target type is
# not in the graph
pass
def all_paths(self, source, target, maxlen=2):
"""
Generator which yields all paths between the source and target
up to the specified maximum path length. This algorithm
tends to get very expensive above 3-5 steps, depending
on the policy complexity.
Parameters:
source The source type.
target The target type.
maxlen Maximum length of paths.
Yield: generator(steps)
steps Yield: tuple(source, target, rules)
source The source type for this step of the information flow.
target The target type for this step of the information flow.
rules The list of rules creating this information flow step.
"""
if maxlen < 1:
raise ValueError("Maximum path length must be positive.")
s = self.policy.lookup_type(source)
t = self.policy.lookup_type(target)
if self.rebuildsubgraph:
self._build_subgraph()
self.log.info("Generating all information flow paths from {0} to {1}, max length {2}...".
format(s, t, maxlen))
try:
for path in nx.all_simple_paths(self.subG, s, t, maxlen):
yield self.__generate_steps(path)
except (NetworkXNoPath, NetworkXError):
# NetworkXError: the type is valid but not in graph, e.g.
# excluded or disconnected due to min weight
# NetworkXNoPath: no paths or the target type is
# not in the graph
pass
def all_shortest_paths(self, source, target):
"""
Generator which yields all shortest paths between the source
and target types.
Parameters:
source The source type.
target The target type.
Yield: generator(steps)
steps Yield: tuple(source, target, rules)
source The source type for this step of the information flow.
target The target type for this step of the information flow.
rules The list of rules creating this information flow step.
"""
s = self.policy.lookup_type(source)
t = self.policy.lookup_type(target)
if self.rebuildsubgraph:
self._build_subgraph()
self.log.info("Generating all shortest information flow paths from {0} to {1}...".
format(s, t))
try:
for path in nx.all_shortest_paths(self.subG, s, t):
yield self.__generate_steps(path)
except (NetworkXNoPath, NetworkXError, KeyError):
# NetworkXError: the type is valid but not in graph, e.g.
# excluded or disconnected due to min weight
# NetworkXNoPath: no paths or the target type is
# not in the graph
# KeyError: work around NetworkX bug
# when the source node is not in the graph
pass
def infoflows(self, type_, out=True):
"""
Generator which yields all information flows in/out of a
specified source type.
Parameters:
source The starting type.
Keyword Parameters:
out If true, information flows out of the type will
be returned. If false, information flows in to the
type will be returned. Default is true.
Yield: generator(steps)
steps A generator that returns the tuple of
source, target, and rules for each
information flow.
"""
s = self.policy.lookup_type(type_)
if self.rebuildsubgraph:
self._build_subgraph()
self.log.info("Generating all information flows {0} {1}".
format("out of" if out else "into", s))
if out:
flows = self.subG.out_edges_iter(s)
else:
flows = self.subG.in_edges_iter(s)
try:
for source, target in flows:
yield Edge(self.subG, source, target)
except NetworkXError:
# NetworkXError: the type is valid but not in graph, e.g.
# excluded or disconnected due to min weight
pass
def get_stats(self): # pragma: no cover
"""
Get the information flow graph statistics.
Return: str
"""
if self.rebuildgraph:
self._build_graph()
return nx.info(self.G)
#
# Internal functions follow
#
def __generate_steps(self, path):
"""
Generator which returns the source, target, and associated rules
for each information flow step.
Parameter:
path A list of graph node names representing an information flow path.
Yield: tuple(source, target, rules)
source The source type for this step of the information flow.
target The target type for this step of the information flow.
rules The list of rules creating this information flow step.
"""
for s in range(1, len(path)):
yield Edge(self.subG, path[s - 1], path[s])
#
#
# Graph building functions
#
#
# 1. _build_graph determines the flow in each direction for each TE
# rule and then expands the rule. All information flows are
# included in this main graph: memory is traded off for efficiency
# as the main graph should only need to be rebuilt if permission
# weights change.
# 2. _build_subgraph derives a subgraph which removes all excluded
# types (nodes) and edges (information flows) which are below the
# minimum weight. This subgraph is rebuilt only if the main graph
# is rebuilt or the minimum weight or excluded types change.
def _build_graph(self):
self.G.clear()
self.G.name = "Information flow graph for {0}.".format(self.policy)
self.perm_map.map_policy(self.policy)
self.log.info("Building information flow graph from {0}...".format(self.policy))
for rule in self.policy.terules():
if rule.ruletype != "allow":
continue
(rweight, wweight) = self.perm_map.rule_weight(rule)
for s, t in itertools.product(rule.source.expand(), rule.target.expand()):
# only add flows if they actually flow
# in or out of the source type type
if s != t:
if wweight:
edge = Edge(self.G, s, t, create=True)
edge.rules.append(rule)
edge.weight = wweight
if rweight:
edge = Edge(self.G, t, s, create=True)
edge.rules.append(rule)
edge.weight = rweight
self.rebuildgraph = False
self.rebuildsubgraph = True
self.log.info("Completed building information flow graph.")
self.log.debug("Graph stats: nodes: {0}, edges: {1}.".format(
nx.number_of_nodes(self.G),
nx.number_of_edges(self.G)))
def _build_subgraph(self):
if self.rebuildgraph:
self._build_graph()
self.log.info("Building information flow subgraph...")
self.log.debug("Excluding {0!r}".format(self.exclude))
self.log.debug("Min weight {0}".format(self.min_weight))
# delete excluded types from subgraph
nodes = [n for n in self.G.nodes() if n not in self.exclude]
self.subG = self.G.subgraph(nodes)
# delete edges below minimum weight.
# no need if weight is 1, since that
# does not exclude any edges.
if self.min_weight > 1:
delete_list = []
for s, t in self.subG.edges_iter():
edge = Edge(self.subG, s, t)
if edge.weight < self.min_weight:
delete_list.append(edge)
self.subG.remove_edges_from(delete_list)
self.rebuildsubgraph = False
self.log.info("Completed building information flow subgraph.")
self.log.debug("Subgraph stats: nodes: {0}, edges: {1}.".format(
nx.number_of_nodes(self.subG),
nx.number_of_edges(self.subG)))
class Edge(object):
"""
A graph edge. Also used for returning information flow steps.
Parameters:
graph The NetworkX graph.
source The source type of the edge.
target The target type of the edge.
Keyword Parameters:
create (T/F) create the edge if it does not exist.
The default is False.
"""
rules = EdgeAttrList('rules')
# use capacity to store the info flow weight so
# we can use network flow algorithms naturally.
# The weight for each edge is 1 since each info
# flow step is no more costly than another
# (see below add_edge() call)
weight = EdgeAttrIntMax('capacity')
def __init__(self, graph, source, target, create=False):
self.G = graph
self.source = source
self.target = target
if not self.G.has_edge(source, target):
if create:
self.G.add_edge(source, target, weight=1)
self.rules = None
self.weight = None
else:
raise ValueError("Edge does not exist in graph")
def __getitem__(self, key):
# This is implemented so this object can be used in NetworkX
# functions that operate on (source, target) tuples
if isinstance(key, slice):
return [self._index_to_item(i) for i in range(* key.indices(2))]
else:
return self._index_to_item(key)
def _index_to_item(self, index):
"""Return source or target based on index."""
if index == 0:
return self.source
elif index == 1:
return self.target
else:
raise IndexError("Invalid index (edges only have 2 items): {0}".format(index))
|