1#! /usr/bin/python -Es 2# Copyright (C) 2011 Red Hat 3# see file 'COPYING' for use and warranty information 4# 5# setrans is a tool for analyzing process transistions in SELinux policy 6# 7# This program is free software; you can redistribute it and/or 8# modify it under the terms of the GNU General Public License as 9# published by the Free Software Foundation; either version 2 of 10# the License, or (at your option) any later version. 11# 12# This program is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with this program; if not, write to the Free Software 19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 20# 02111-1307 USA 21# 22# 23import sepolicy 24__all__ = ['setrans'] 25 26 27def _entrypoint(src): 28 trans = sepolicy.search([sepolicy.ALLOW], {sepolicy.SOURCE: src}) 29 return map(lambda y: y[sepolicy.TARGET], filter(lambda x: "entrypoint" in x[sepolicy.PERMS], trans)) 30 31 32def _get_trans(src): 33 return sepolicy.search([sepolicy.TRANSITION], {sepolicy.SOURCE: src, sepolicy.CLASS: "process"}) 34 35 36class setrans: 37 38 def __init__(self, source, dest=None): 39 self.seen = [] 40 self.sdict = {} 41 self.source = source 42 self.dest = dest 43 self._process(self.source) 44 45 def _process(self, source): 46 if source in self.sdict: 47 return self.sdict[source] 48 self.sdict[source] = {} 49 trans = _get_trans(source) 50 if not trans: 51 return 52 self.sdict[source]["name"] = source 53 if not self.dest: 54 self.sdict[source]["map"] = trans 55 else: 56 self.sdict[source]["map"] = map(lambda y: y, filter(lambda x: x["transtype"] == self.dest, trans)) 57 self.sdict[source]["child"] = map(lambda y: y["transtype"], filter(lambda x: x["transtype"] not in [self.dest, source], trans)) 58 for s in self.sdict[source]["child"]: 59 self._process(s) 60 61 def out(self, name, header=""): 62 buf = "" 63 if name in self.seen: 64 return buf 65 self.seen.append(name) 66 67 if "map" in self.sdict[name]: 68 for t in self.sdict[name]["map"]: 69 cond = sepolicy.get_conditionals(t["source"], t["transtype"], "process", ["transition"]) 70 if cond: 71 buf += "%s%s @ %s --> %s %s\n" % (header, t["source"], t["target"], t["transtype"], sepolicy.get_conditionals_format_text(cond)) 72 else: 73 buf += "%s%s @ %s --> %s\n" % (header, t["source"], t["target"], t["transtype"]) 74 75 if "child" in self.sdict[name]: 76 for x in self.sdict[name]["child"]: 77 buf += self.out(x, "%s%s ... " % (header, name)) 78 return buf 79 80 def output(self): 81 self.seen = [] 82 print(self.out(self.source)) 83