blob: c92be7ae2c133760b43503ab72d4454e0174d50b [file] [log] [blame]
Dan Cashman91d398d2017-09-26 12:58:29 -07001from optparse import OptionParser
2from optparse import Option, OptionValueError
3import os
4import policy
5import re
6import sys
7
8#############################################################
9# Tests
10#############################################################
11def TestDataTypeViolations(pol):
12 return pol.AssertPathTypesHaveAttr(["/data/"], [], "data_file_type")
13
Nick Kralevichdab131b2018-10-04 11:24:00 -070014def TestSystemTypeViolations(pol):
15 return pol.AssertPathTypesHaveAttr(["/system/"], [], "system_file_type")
Nick Kralevich5e372712018-09-27 10:21:37 -070016
Tri Vo4c80c2c2018-03-29 03:42:47 +000017def TestProcTypeViolations(pol):
18 return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type")
19
Dan Cashman91d398d2017-09-26 12:58:29 -070020def TestSysfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070021 ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type")
22 ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070023 "/sys/kernel/tracing"], "sysfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070024 return ret
Dan Cashman91d398d2017-09-26 12:58:29 -070025
26def TestDebugfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070027 ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type")
28 ret += pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "debugfs_type")
29 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070030 "/sys/kernel/tracing"], [], "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070031 return ret
Tri Vo30c3c2a2018-01-10 11:04:06 -080032
33def TestVendorTypeViolations(pol):
34 return pol.AssertPathTypesHaveAttr(["/vendor/"], [], "vendor_file_type")
35
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080036def TestCoreDataTypeViolations(pol):
Jeff Vander Stoep370a52f2018-02-08 09:54:59 -080037 return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor",
38 "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type")
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080039
Inseob Kim1b8b1f62020-10-23 15:16:11 +090040def TestPropertyTypeViolations(pol):
41 return pol.AssertPropertyOwnersAreExclusive()
42
43
Dan Cashman91d398d2017-09-26 12:58:29 -070044###
45# extend OptionParser to allow the same option flag to be used multiple times.
46# This is used to allow multiple file_contexts files and tests to be
47# specified.
48#
49class MultipleOption(Option):
50 ACTIONS = Option.ACTIONS + ("extend",)
51 STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
52 TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
53 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
54
55 def take_action(self, action, dest, opt, value, values, parser):
56 if action == "extend":
57 values.ensure_value(dest, []).append(value)
58 else:
59 Option.take_action(self, action, dest, opt, value, values, parser)
60
Tri Vo4c80c2c2018-03-29 03:42:47 +000061Tests = [
62 "TestDataTypeViolators",
63 "TestProcTypeViolations",
64 "TestSysfsTypeViolations",
Nick Kralevichdab131b2018-10-04 11:24:00 -070065 "TestSystemTypeViolators",
Tri Vo4c80c2c2018-03-29 03:42:47 +000066 "TestDebugfsTypeViolations",
67 "TestVendorTypeViolations",
68 "TestCoreDataTypeViolations",
Inseob Kim1b8b1f62020-10-23 15:16:11 +090069 "TestPropertyTypeViolations"
Tri Vo4c80c2c2018-03-29 03:42:47 +000070]
Dan Cashman91d398d2017-09-26 12:58:29 -070071
72if __name__ == '__main__':
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070073 usage = "sepolicy_tests -l $(ANDROID_HOST_OUT)/lib64/libsepolwrap.so "
Bowgo Tsaiafbcf212018-02-05 17:34:52 +080074 usage += "-f vendor_file_contexts -f "
Dan Cashman91d398d2017-09-26 12:58:29 -070075 usage +="plat_file_contexts -p policy [--test test] [--help]"
76 parser = OptionParser(option_class=MultipleOption, usage=usage)
77 parser.add_option("-f", "--file_contexts", dest="file_contexts",
78 metavar="FILE", action="extend", type="string")
79 parser.add_option("-p", "--policy", dest="policy", metavar="FILE")
80 parser.add_option("-l", "--library-path", dest="libpath", metavar="FILE")
81 parser.add_option("-t", "--test", dest="test", action="extend",
82 help="Test options include "+str(Tests))
83
84 (options, args) = parser.parse_args()
85
86 if not options.libpath:
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070087 sys.exit("Must specify path to libsepolwrap library\n" + parser.usage)
Dan Cashman91d398d2017-09-26 12:58:29 -070088 if not os.path.exists(options.libpath):
89 sys.exit("Error: library-path " + options.libpath + " does not exist\n"
90 + parser.usage)
91
92 if not options.policy:
93 sys.exit("Must specify monolithic policy file\n" + parser.usage)
94 if not os.path.exists(options.policy):
95 sys.exit("Error: policy file " + options.policy + " does not exist\n"
96 + parser.usage)
97
98 if not options.file_contexts:
99 sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
100 for f in options.file_contexts:
101 if not os.path.exists(f):
102 sys.exit("Error: File_contexts file " + f + " does not exist\n" +
103 parser.usage)
104
105 pol = policy.Policy(options.policy, options.file_contexts, options.libpath)
106
107 results = ""
108 # If an individual test is not specified, run all tests.
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700109 if options.test is None or "TestDataTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700110 results += TestDataTypeViolations(pol)
Tri Vo4c80c2c2018-03-29 03:42:47 +0000111 if options.test is None or "TestProcTypeViolations" in options.test:
112 results += TestProcTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700113 if options.test is None or "TestSysfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700114 results += TestSysfsTypeViolations(pol)
Nick Kralevichdab131b2018-10-04 11:24:00 -0700115 if options.test is None or "TestSystemTypeViolations" in options.test:
116 results += TestSystemTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700117 if options.test is None or "TestDebugfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700118 results += TestDebugfsTypeViolations(pol)
Tri Vo30c3c2a2018-01-10 11:04:06 -0800119 if options.test is None or "TestVendorTypeViolations" in options.test:
120 results += TestVendorTypeViolations(pol)
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -0800121 if options.test is None or "TestCoreDataTypeViolations" in options.test:
122 results += TestCoreDataTypeViolations(pol)
Inseob Kim1b8b1f62020-10-23 15:16:11 +0900123 if options.test is None or "TestPropertyTypeViolations" in options.test:
124 results += TestPropertyTypeViolations(pol)
Dan Cashman91d398d2017-09-26 12:58:29 -0700125
126 if len(results) > 0:
127 sys.exit(results)