blob: 1d26dfccabc3f9ff16c36ad22744aa5b4a18c5e4 [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):
Steven Morelanda01338d2020-04-27 15:54:54 -070015 partitions = ["/system/", "/system_ext/", "/product/"]
16 exceptions = [
17 # devices before treble don't have a vendor partition
18 "/system/vendor/",
19
20 # overlay files are mounted over vendor
21 "/product/overlay/",
22 "/product/vendor_overlay/",
23 "/system/overlay/",
24 "/system/product/overlay/",
25 "/system/product/vendor_overlay/",
26 "/system/system_ext/overlay/",
27 "/system_ext/overlay/",
28 ]
29
30 return pol.AssertPathTypesHaveAttr(partitions, exceptions, "system_file_type")
Nick Kralevich5e372712018-09-27 10:21:37 -070031
Tri Vo4c80c2c2018-03-29 03:42:47 +000032def TestProcTypeViolations(pol):
33 return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type")
34
Dan Cashman91d398d2017-09-26 12:58:29 -070035def TestSysfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070036 ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type")
37 ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070038 "/sys/kernel/tracing"], "sysfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070039 return ret
Dan Cashman91d398d2017-09-26 12:58:29 -070040
41def TestDebugfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070042 ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070043 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070044 "/sys/kernel/tracing"], [], "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070045 return ret
Tri Vo30c3c2a2018-01-10 11:04:06 -080046
Hridya Valsarajuedccaa82021-05-14 14:01:11 -070047def TestTracefsTypeViolations(pol):
48 ret = pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "tracefs_type")
49 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/tracing"], [], "tracefs_type")
50 ret += pol.AssertPathTypesDoNotHaveAttr(["/sys/kernel/debug"],
51 ["/sys/kernel/debug/tracing"], "tracefs_type",
52 [])
53 return ret
54
Tri Vo30c3c2a2018-01-10 11:04:06 -080055def TestVendorTypeViolations(pol):
Steven Morelanda01338d2020-04-27 15:54:54 -070056 partitions = ["/vendor/", "/odm/"]
57 exceptions = [
58 "/vendor/etc/selinux/",
59 "/vendor/odm/etc/selinux/",
60 "/odm/etc/selinux/",
61 ]
62 return pol.AssertPathTypesHaveAttr(partitions, exceptions, "vendor_file_type")
Tri Vo30c3c2a2018-01-10 11:04:06 -080063
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080064def TestCoreDataTypeViolations(pol):
Jeff Vander Stoep370a52f2018-02-08 09:54:59 -080065 return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor",
66 "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type")
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080067
Inseob Kim1b8b1f62020-10-23 15:16:11 +090068def TestPropertyTypeViolations(pol):
69 return pol.AssertPropertyOwnersAreExclusive()
70
Alan Stokes668e74f2020-11-12 18:08:18 +000071def TestAppDataTypeViolations(pol):
72 # Types with the app_data_file_type should only be used for app data files
73 # (/data/data/package.name etc) via seapp_contexts, and never applied
74 # explicitly to other files.
75 partitions = [
76 "/data/",
77 "/vendor/",
78 "/odm/",
79 "/product/",
80 ]
81 exceptions = [
82 # These are used for app data files for the corresponding user and
83 # assorted other files.
84 # TODO(b/172812577): Use different types for the different purposes
85 "shell_data_file",
86 "bluetooth_data_file",
87 "nfc_data_file",
88 "radio_data_file",
89 ]
90 return pol.AssertPathTypesDoNotHaveAttr(partitions, [], "app_data_file_type",
91 exceptions)
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -080092def TestDmaHeapDevTypeViolations(pol):
93 return pol.AssertPathTypesHaveAttr(["/dev/dma_heap/"], [],
94 "dmabuf_heap_device_type")
95
Alan Stokes668e74f2020-11-12 18:08:18 +000096
Inseob Kim1b8b1f62020-10-23 15:16:11 +090097
Dan Cashman91d398d2017-09-26 12:58:29 -070098###
99# extend OptionParser to allow the same option flag to be used multiple times.
100# This is used to allow multiple file_contexts files and tests to be
101# specified.
102#
103class MultipleOption(Option):
104 ACTIONS = Option.ACTIONS + ("extend",)
105 STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
106 TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
107 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
108
109 def take_action(self, action, dest, opt, value, values, parser):
110 if action == "extend":
111 values.ensure_value(dest, []).append(value)
112 else:
113 Option.take_action(self, action, dest, opt, value, values, parser)
114
Tri Vo4c80c2c2018-03-29 03:42:47 +0000115Tests = [
116 "TestDataTypeViolators",
117 "TestProcTypeViolations",
118 "TestSysfsTypeViolations",
Nick Kralevichdab131b2018-10-04 11:24:00 -0700119 "TestSystemTypeViolators",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000120 "TestDebugfsTypeViolations",
Hridya Valsarajuedccaa82021-05-14 14:01:11 -0700121 "TestTracefsTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000122 "TestVendorTypeViolations",
123 "TestCoreDataTypeViolations",
Alan Stokes668e74f2020-11-12 18:08:18 +0000124 "TestPropertyTypeViolations",
125 "TestAppDataTypeViolations",
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800126 "TestDmaHeapDevTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000127]
Dan Cashman91d398d2017-09-26 12:58:29 -0700128
129if __name__ == '__main__':
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700130 usage = "sepolicy_tests -l $(ANDROID_HOST_OUT)/lib64/libsepolwrap.so "
Bowgo Tsaiafbcf212018-02-05 17:34:52 +0800131 usage += "-f vendor_file_contexts -f "
Dan Cashman91d398d2017-09-26 12:58:29 -0700132 usage +="plat_file_contexts -p policy [--test test] [--help]"
133 parser = OptionParser(option_class=MultipleOption, usage=usage)
134 parser.add_option("-f", "--file_contexts", dest="file_contexts",
135 metavar="FILE", action="extend", type="string")
136 parser.add_option("-p", "--policy", dest="policy", metavar="FILE")
137 parser.add_option("-l", "--library-path", dest="libpath", metavar="FILE")
138 parser.add_option("-t", "--test", dest="test", action="extend",
139 help="Test options include "+str(Tests))
140
141 (options, args) = parser.parse_args()
142
143 if not options.libpath:
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700144 sys.exit("Must specify path to libsepolwrap library\n" + parser.usage)
Dan Cashman91d398d2017-09-26 12:58:29 -0700145 if not os.path.exists(options.libpath):
146 sys.exit("Error: library-path " + options.libpath + " does not exist\n"
147 + parser.usage)
148
149 if not options.policy:
150 sys.exit("Must specify monolithic policy file\n" + parser.usage)
151 if not os.path.exists(options.policy):
152 sys.exit("Error: policy file " + options.policy + " does not exist\n"
153 + parser.usage)
154
155 if not options.file_contexts:
156 sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
157 for f in options.file_contexts:
158 if not os.path.exists(f):
159 sys.exit("Error: File_contexts file " + f + " does not exist\n" +
160 parser.usage)
161
162 pol = policy.Policy(options.policy, options.file_contexts, options.libpath)
163
164 results = ""
165 # If an individual test is not specified, run all tests.
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700166 if options.test is None or "TestDataTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700167 results += TestDataTypeViolations(pol)
Tri Vo4c80c2c2018-03-29 03:42:47 +0000168 if options.test is None or "TestProcTypeViolations" in options.test:
169 results += TestProcTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700170 if options.test is None or "TestSysfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700171 results += TestSysfsTypeViolations(pol)
Nick Kralevichdab131b2018-10-04 11:24:00 -0700172 if options.test is None or "TestSystemTypeViolations" in options.test:
173 results += TestSystemTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700174 if options.test is None or "TestDebugfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700175 results += TestDebugfsTypeViolations(pol)
Hridya Valsarajuedccaa82021-05-14 14:01:11 -0700176 if options.test is None or "TestTracefsTypeViolations" in options.test:
177 results += TestTracefsTypeViolations(pol)
Tri Vo30c3c2a2018-01-10 11:04:06 -0800178 if options.test is None or "TestVendorTypeViolations" in options.test:
179 results += TestVendorTypeViolations(pol)
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -0800180 if options.test is None or "TestCoreDataTypeViolations" in options.test:
181 results += TestCoreDataTypeViolations(pol)
Inseob Kim1b8b1f62020-10-23 15:16:11 +0900182 if options.test is None or "TestPropertyTypeViolations" in options.test:
183 results += TestPropertyTypeViolations(pol)
Alan Stokes668e74f2020-11-12 18:08:18 +0000184 if options.test is None or "TestAppDataTypeViolations" in options.test:
185 results += TestAppDataTypeViolations(pol)
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800186 if options.test is None or "TestDmaHeapDevTypeViolations" in options.test:
187 results += TestDmaHeapDevTypeViolations(pol)
Dan Cashman91d398d2017-09-26 12:58:29 -0700188
189 if len(results) > 0:
190 sys.exit(results)