searchpolicy.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. import argparse
  3. import policy
  4. parser = argparse.ArgumentParser(
  5. description="SELinux policy rule search tool. Intended to have a similar "
  6. + "API as sesearch, but simplified to use only code availabe in AOSP")
  7. parser.add_argument("policy", help="Path to the SELinux policy to search.", nargs="?")
  8. parser.add_argument("--libpath", dest="libpath", help="Path to the libsepolwrap.so", nargs="?")
  9. tertypes = parser.add_argument_group("TE Rule Types")
  10. tertypes.add_argument("--allow", action="append_const",
  11. const="allow", dest="tertypes",
  12. help="Search allow rules.")
  13. expr = parser.add_argument_group("Expressions")
  14. expr.add_argument("-s", "--source",
  15. help="Source type/role of the TE/RBAC rule.")
  16. expr.add_argument("-t", "--target",
  17. help="Target type/role of the TE/RBAC rule.")
  18. expr.add_argument("-c", "--class", dest="tclass",
  19. help="Comma separated list of object classes")
  20. expr.add_argument("-p", "--perms", metavar="PERMS",
  21. help="Comma separated list of permissions.")
  22. args = parser.parse_args()
  23. if not args.tertypes:
  24. parser.error("Must specify \"--allow\"")
  25. if not args.policy:
  26. parser.error("Must include path to policy")
  27. if not args.libpath:
  28. parser.error("Must include path to libsepolwrap library")
  29. if not (args.source or args.target or args.tclass or args.perms):
  30. parser.error("Must something to filter on, e.g. --source, --target, etc.")
  31. pol = policy.Policy(args.policy, None, args.libpath)
  32. if args.source:
  33. scontext = {args.source}
  34. else:
  35. scontext = set()
  36. if args.target:
  37. tcontext = {args.target}
  38. else:
  39. tcontext = set()
  40. if args.tclass:
  41. tclass = set(args.tclass.split(","))
  42. else:
  43. tclass = set()
  44. if args.perms:
  45. perms = set(args.perms.split(","))
  46. else:
  47. perms = set()
  48. TERules = pol.QueryTERule(scontext=scontext,
  49. tcontext=tcontext,
  50. tclass=tclass,
  51. perms=perms)
  52. # format rules for printing
  53. rules = []
  54. for r in TERules:
  55. if len(r.perms) > 1:
  56. rules.append("allow " + r.sctx + " " + r.tctx + ":" + r.tclass + " { " +
  57. " ".join(r.perms) + " };")
  58. else:
  59. rules.append("allow " + r.sctx + " " + r.tctx + ":" + r.tclass + " " +
  60. " ".join(r.perms) + ";")
  61. for r in sorted(rules):
  62. print r