sepolicy_tests.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from optparse import OptionParser
  2. from optparse import Option, OptionValueError
  3. import os
  4. import policy
  5. import re
  6. import sys
  7. #############################################################
  8. # Tests
  9. #############################################################
  10. def TestDataTypeViolations(pol):
  11. return pol.AssertPathTypesHaveAttr(["/data/"], [], "data_file_type")
  12. def TestSystemTypeViolations(pol):
  13. return pol.AssertPathTypesHaveAttr(["/system/"], [], "system_file_type")
  14. def TestProcTypeViolations(pol):
  15. return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type")
  16. def TestSysfsTypeViolations(pol):
  17. ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type")
  18. ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/",
  19. "/sys/kernel/tracing"], "sysfs_type")
  20. return ret
  21. def TestDebugfsTypeViolations(pol):
  22. ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type")
  23. ret += pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "debugfs_type")
  24. ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/",
  25. "/sys/kernel/tracing"], [], "debugfs_type")
  26. return ret
  27. def TestVendorTypeViolations(pol):
  28. return pol.AssertPathTypesHaveAttr(["/vendor/"], [], "vendor_file_type")
  29. def TestCoreDataTypeViolations(pol):
  30. return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor",
  31. "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type")
  32. ###
  33. # extend OptionParser to allow the same option flag to be used multiple times.
  34. # This is used to allow multiple file_contexts files and tests to be
  35. # specified.
  36. #
  37. class MultipleOption(Option):
  38. ACTIONS = Option.ACTIONS + ("extend",)
  39. STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
  40. TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
  41. ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
  42. def take_action(self, action, dest, opt, value, values, parser):
  43. if action == "extend":
  44. values.ensure_value(dest, []).append(value)
  45. else:
  46. Option.take_action(self, action, dest, opt, value, values, parser)
  47. Tests = [
  48. "TestDataTypeViolators",
  49. "TestProcTypeViolations",
  50. "TestSysfsTypeViolations",
  51. "TestSystemTypeViolators",
  52. "TestDebugfsTypeViolations",
  53. "TestVendorTypeViolations",
  54. "TestCoreDataTypeViolations",
  55. ]
  56. if __name__ == '__main__':
  57. usage = "sepolicy_tests -l $(ANDROID_HOST_OUT)/lib64/libsepolwrap.so "
  58. usage += "-f vendor_file_contexts -f "
  59. usage +="plat_file_contexts -p policy [--test test] [--help]"
  60. parser = OptionParser(option_class=MultipleOption, usage=usage)
  61. parser.add_option("-f", "--file_contexts", dest="file_contexts",
  62. metavar="FILE", action="extend", type="string")
  63. parser.add_option("-p", "--policy", dest="policy", metavar="FILE")
  64. parser.add_option("-l", "--library-path", dest="libpath", metavar="FILE")
  65. parser.add_option("-t", "--test", dest="test", action="extend",
  66. help="Test options include "+str(Tests))
  67. (options, args) = parser.parse_args()
  68. if not options.libpath:
  69. sys.exit("Must specify path to libsepolwrap library\n" + parser.usage)
  70. if not os.path.exists(options.libpath):
  71. sys.exit("Error: library-path " + options.libpath + " does not exist\n"
  72. + parser.usage)
  73. if not options.policy:
  74. sys.exit("Must specify monolithic policy file\n" + parser.usage)
  75. if not os.path.exists(options.policy):
  76. sys.exit("Error: policy file " + options.policy + " does not exist\n"
  77. + parser.usage)
  78. if not options.file_contexts:
  79. sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
  80. for f in options.file_contexts:
  81. if not os.path.exists(f):
  82. sys.exit("Error: File_contexts file " + f + " does not exist\n" +
  83. parser.usage)
  84. pol = policy.Policy(options.policy, options.file_contexts, options.libpath)
  85. results = ""
  86. # If an individual test is not specified, run all tests.
  87. if options.test is None or "TestDataTypeViolations" in options.test:
  88. results += TestDataTypeViolations(pol)
  89. if options.test is None or "TestProcTypeViolations" in options.test:
  90. results += TestProcTypeViolations(pol)
  91. if options.test is None or "TestSysfsTypeViolations" in options.test:
  92. results += TestSysfsTypeViolations(pol)
  93. if options.test is None or "TestSystemTypeViolations" in options.test:
  94. results += TestSystemTypeViolations(pol)
  95. if options.test is None or "TestDebugfsTypeViolations" in options.test:
  96. results += TestDebugfsTypeViolations(pol)
  97. if options.test is None or "TestVendorTypeViolations" in options.test:
  98. results += TestVendorTypeViolations(pol)
  99. if options.test is None or "TestCoreDataTypeViolations" in options.test:
  100. results += TestCoreDataTypeViolations(pol)
  101. if len(results) > 0:
  102. sys.exit(results)