setools/seinfoflow
2014-07-08 14:28:55 -04:00

116 lines
4.2 KiB
Python
Executable File

#!/usr/bin/python
# Copyright 2014, 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 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
import libapol
import argparse
import sys
parser = argparse.ArgumentParser(description="SELinux policy information flow analysis tool.",
epilog="If no analysis algorithm is selected, all information flow out of the source domain will be calculated.")
parser.add_argument("--version", action="version", version=libapol.__version__)
parser.add_argument("--stats", action="store_true",
help="Display statistics at the end of the analysis.")
settings = parser.add_argument_group("Analysis settings")
settings.add_argument(
"-p", "--policy", help="Path to SELinux policy to analyze.", required=True)
settings.add_argument(
"-m", "--map", help="Path to permission map file.", required=True)
settings.add_argument(
"-s", "--source", help="Source type of the analysis.", required=True, default="")
settings.add_argument(
"-t", "--target", help="Target type of the analysis.", default="")
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",
help="Calculate all paths, with the specified maximum path length. (Expensive)", type=int, metavar="MAX_STEPS")
opts = parser.add_argument_group("Analysis options")
opts.add_argument("-w", "--min_weight",
help="Minimum permission weight. Default is 3.", default=3, type=int)
opts.add_argument("-l", "--limit_flows",
help="Limit to the specified number of flows. Default is unlimited.", default=0, type=int)
opts.add_argument(
"exclude", help="List of excluded types in the analysis.", nargs="*")
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.limit_flows < 0:
parser.error("Limit on information flows cannot be negative.")
try:
p = libapol.SELinuxPolicy(args.policy)
g = libapol.infoflow.InfoFlowAnalysis(
p, args.map, minweight=args.min_weight, exclude=args.exclude)
except Exception as err:
print(err)
sys.exit(1)
if args.shortest_path or args.all_paths:
try:
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)
except Exception as err:
print(err)
sys.exit(1)
flownum = 0
for flownum, path in enumerate(paths, start=1):
print("Flow {0}:".format(flownum))
for stepnum, (src, tgt, rules) in enumerate(path, start=1):
print(" Step {0}: {1} -> {2}".format(stepnum, src, tgt))
for rule in sorted(rules):
print(" ", rule)
print()
if args.limit_flows and flownum >= args.limit_flows:
break
print()
else: # single direct info flow
flownum = 0
for flownum, (src, tgt, rules) in enumerate(g.infoflows(args.source), start=1):
print("Flow {0}: {1} -> {2}".format(flownum, src, tgt))
for rule in sorted(rules):
print(" ", rule)
print()
if args.limit_flows and flownum >= args.limit_flows:
break
print(flownum, "information flow(s) found.\n")
if args.stats:
print("Information flow graph size:")
nnodes, nedges = g.get_stats()
print(" Nodes:", nnodes)
print(" Edges:", nedges)