setools/seinfoflow

131 lines
4.9 KiB
Plaintext
Raw Normal View History

2016-02-26 14:12:34 +00:00
#!/usr/bin/env python
# Copyright 2014-2015, Tresys Technology, LLC
2014-07-08 18:28:55 +00:00
#
# This file is part of SETools.
#
# SETools is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SETools. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import print_function
2014-10-25 01:23:13 +00:00
import setools
2014-07-08 18:28:55 +00:00
import argparse
import sys
import logging
2014-07-08 18:28:55 +00:00
parser = argparse.ArgumentParser(
description="SELinux policy information flow analysis tool.",
epilog="If no analysis is selected, all information flow out of the source will be printed.")
2014-10-25 01:23:13 +00:00
parser.add_argument("--version", action="version", version=setools.__version__)
2014-07-08 18:28:55 +00:00
parser.add_argument("--stats", action="store_true",
help="Display statistics at the end of the analysis.")
parser.add_argument("-v", "--verbose", action="store_true",
help="Print extra informational messages")
parser.add_argument("--debug", action="store_true", dest="debug", help="Enable debugging.")
2014-07-08 18:28:55 +00:00
settings = parser.add_argument_group("Analysis settings")
settings.add_argument("-p", "--policy",
help="Path to SELinux policy to analyze.")
settings.add_argument("-m", "--map", required=True,
help="Path to permission map file.")
settings.add_argument("-s", "--source", required=True,
help="Source type of the analysis.")
settings.add_argument("-t", "--target", default="",
help="Target type of the analysis.")
2014-07-08 18:28:55 +00:00
alg = parser.add_argument_group("Analysis algorithm")
alg.add_argument("-S", "--shortest_path", action="store_true",
help="Calculate all shortest paths.")
alg.add_argument("-A", "--all_paths", type=int, metavar="MAX_STEPS",
help="Calculate all paths, with the specified maximum path length. (Expensive)")
2014-07-08 18:28:55 +00:00
opts = parser.add_argument_group("Analysis options")
opts.add_argument("-w", "--min_weight", default=3, type=int,
help="Minimum permission weight. Default is 3.")
opts.add_argument("-l", "--limit_flows", default=0, type=int,
help="Limit to the specified number of flows. Default is unlimited.")
opts.add_argument("exclude", nargs="*",
help="List of excluded types in the analysis.")
2014-07-08 18:28:55 +00:00
args = parser.parse_args()
if not args.target and (args.shortest_path or args.all_paths):
parser.error("The target type must be specified to determine a path.")
if args.target and not (args.shortest_path or args.all_paths):
parser.error("A target type is not used for flows in/out of a type.")
2014-07-08 18:28:55 +00:00
if args.limit_flows < 0:
parser.error("Limit on information flows cannot be negative.")
if args.debug:
2015-03-25 17:40:03 +00:00
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s|%(levelname)s|%(name)s|%(message)s')
elif args.verbose:
2015-03-25 17:40:03 +00:00
logging.basicConfig(level=logging.INFO, format='%(message)s')
else:
logging.basicConfig(level=logging.WARNING, format='%(message)s')
2014-07-08 18:28:55 +00:00
try:
2014-10-25 01:23:13 +00:00
p = setools.SELinuxPolicy(args.policy)
m = setools.PermissionMap(args.map)
g = setools.InfoFlowAnalysis(p, m, min_weight=args.min_weight, exclude=args.exclude)
2014-07-08 18:28:55 +00:00
if args.shortest_path or args.all_paths:
2015-03-31 19:49:08 +00:00
if args.shortest_path:
paths = g.all_shortest_paths(args.source, args.target)
else:
paths = g.all_paths(args.source, args.target, args.all_paths)
2014-07-08 18:28:55 +00:00
flownum = 0
for flownum, path in enumerate(paths, start=1):
print("Flow {0}:".format(flownum))
for stepnum, step in enumerate(path, start=1):
print(" Step {0}: {1} -> {2}".format(stepnum, step.source, step.target))
2014-07-08 18:28:55 +00:00
for rule in sorted(step.rules):
print(" ", rule)
print()
if args.limit_flows and flownum >= args.limit_flows:
break
2014-07-08 18:28:55 +00:00
print()
else: # single direct info flow
flownum = 0
for flownum, flow in enumerate(g.infoflows(args.source), start=1):
print("Flow {0}: {1} -> {2}".format(flownum, flow.source, flow.target))
for rule in sorted(flow.rules):
print(" ", rule)
2014-07-08 18:28:55 +00:00
print()
2014-07-08 18:28:55 +00:00
if args.limit_flows and flownum >= args.limit_flows:
break
2014-07-08 18:28:55 +00:00
print(flownum, "information flow(s) found.")
2014-07-08 18:28:55 +00:00
if args.stats:
print("\nGraph statistics:")
print(g.get_stats())
2014-07-08 18:28:55 +00:00
except Exception as err:
if args.debug:
logging.exception(str(err))
else:
print(err)
2014-07-08 18:28:55 +00:00
sys.exit(1)