blob: a05d8f220f70d606c4b03620dea0744dc58b258c [file] [log] [blame]
ThiƩbaud Weksteenf24b4572021-11-26 09:12:41 +11001# Copyright 2021 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Dan Cashman91d398d2017-09-26 12:58:29 -070015from optparse import OptionParser
16from optparse import Option, OptionValueError
17import os
18import policy
19import re
20import sys
21
22#############################################################
23# Tests
24#############################################################
25def TestDataTypeViolations(pol):
26 return pol.AssertPathTypesHaveAttr(["/data/"], [], "data_file_type")
27
Nick Kralevichdab131b2018-10-04 11:24:00 -070028def TestSystemTypeViolations(pol):
Steven Morelanda01338d2020-04-27 15:54:54 -070029 partitions = ["/system/", "/system_ext/", "/product/"]
30 exceptions = [
31 # devices before treble don't have a vendor partition
32 "/system/vendor/",
33
34 # overlay files are mounted over vendor
35 "/product/overlay/",
36 "/product/vendor_overlay/",
37 "/system/overlay/",
38 "/system/product/overlay/",
39 "/system/product/vendor_overlay/",
40 "/system/system_ext/overlay/",
41 "/system_ext/overlay/",
42 ]
43
44 return pol.AssertPathTypesHaveAttr(partitions, exceptions, "system_file_type")
Nick Kralevich5e372712018-09-27 10:21:37 -070045
Tri Vo4c80c2c2018-03-29 03:42:47 +000046def TestProcTypeViolations(pol):
47 return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type")
48
Dan Cashman91d398d2017-09-26 12:58:29 -070049def TestSysfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070050 ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type")
51 ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070052 "/sys/kernel/tracing"], "sysfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070053 return ret
Dan Cashman91d398d2017-09-26 12:58:29 -070054
55def TestDebugfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070056 ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070057 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070058 "/sys/kernel/tracing"], [], "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070059 return ret
Tri Vo30c3c2a2018-01-10 11:04:06 -080060
Hridya Valsarajuedccaa82021-05-14 14:01:11 -070061def TestTracefsTypeViolations(pol):
62 ret = pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "tracefs_type")
63 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/tracing"], [], "tracefs_type")
64 ret += pol.AssertPathTypesDoNotHaveAttr(["/sys/kernel/debug"],
65 ["/sys/kernel/debug/tracing"], "tracefs_type",
66 [])
67 return ret
68
Tri Vo30c3c2a2018-01-10 11:04:06 -080069def TestVendorTypeViolations(pol):
Steven Morelanda01338d2020-04-27 15:54:54 -070070 partitions = ["/vendor/", "/odm/"]
71 exceptions = [
72 "/vendor/etc/selinux/",
73 "/vendor/odm/etc/selinux/",
74 "/odm/etc/selinux/",
75 ]
76 return pol.AssertPathTypesHaveAttr(partitions, exceptions, "vendor_file_type")
Tri Vo30c3c2a2018-01-10 11:04:06 -080077
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080078def TestCoreDataTypeViolations(pol):
Jeff Vander Stoep370a52f2018-02-08 09:54:59 -080079 return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor",
80 "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type")
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080081
Inseob Kim1b8b1f62020-10-23 15:16:11 +090082def TestPropertyTypeViolations(pol):
83 return pol.AssertPropertyOwnersAreExclusive()
84
Alan Stokes668e74f2020-11-12 18:08:18 +000085def TestAppDataTypeViolations(pol):
86 # Types with the app_data_file_type should only be used for app data files
87 # (/data/data/package.name etc) via seapp_contexts, and never applied
88 # explicitly to other files.
89 partitions = [
90 "/data/",
91 "/vendor/",
92 "/odm/",
93 "/product/",
94 ]
95 exceptions = [
96 # These are used for app data files for the corresponding user and
97 # assorted other files.
98 # TODO(b/172812577): Use different types for the different purposes
99 "shell_data_file",
100 "bluetooth_data_file",
101 "nfc_data_file",
102 "radio_data_file",
103 ]
104 return pol.AssertPathTypesDoNotHaveAttr(partitions, [], "app_data_file_type",
105 exceptions)
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800106def TestDmaHeapDevTypeViolations(pol):
107 return pol.AssertPathTypesHaveAttr(["/dev/dma_heap/"], [],
108 "dmabuf_heap_device_type")
109
Alan Stokes668e74f2020-11-12 18:08:18 +0000110
Inseob Kim1b8b1f62020-10-23 15:16:11 +0900111
Dan Cashman91d398d2017-09-26 12:58:29 -0700112###
113# extend OptionParser to allow the same option flag to be used multiple times.
114# This is used to allow multiple file_contexts files and tests to be
115# specified.
116#
117class MultipleOption(Option):
118 ACTIONS = Option.ACTIONS + ("extend",)
119 STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
120 TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
121 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
122
123 def take_action(self, action, dest, opt, value, values, parser):
124 if action == "extend":
125 values.ensure_value(dest, []).append(value)
126 else:
127 Option.take_action(self, action, dest, opt, value, values, parser)
128
Tri Vo4c80c2c2018-03-29 03:42:47 +0000129Tests = [
130 "TestDataTypeViolators",
131 "TestProcTypeViolations",
132 "TestSysfsTypeViolations",
Nick Kralevichdab131b2018-10-04 11:24:00 -0700133 "TestSystemTypeViolators",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000134 "TestDebugfsTypeViolations",
Hridya Valsarajuedccaa82021-05-14 14:01:11 -0700135 "TestTracefsTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000136 "TestVendorTypeViolations",
137 "TestCoreDataTypeViolations",
Alan Stokes668e74f2020-11-12 18:08:18 +0000138 "TestPropertyTypeViolations",
139 "TestAppDataTypeViolations",
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800140 "TestDmaHeapDevTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000141]
Dan Cashman91d398d2017-09-26 12:58:29 -0700142
143if __name__ == '__main__':
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700144 usage = "sepolicy_tests -l $(ANDROID_HOST_OUT)/lib64/libsepolwrap.so "
Bowgo Tsaiafbcf212018-02-05 17:34:52 +0800145 usage += "-f vendor_file_contexts -f "
Dan Cashman91d398d2017-09-26 12:58:29 -0700146 usage +="plat_file_contexts -p policy [--test test] [--help]"
147 parser = OptionParser(option_class=MultipleOption, usage=usage)
148 parser.add_option("-f", "--file_contexts", dest="file_contexts",
149 metavar="FILE", action="extend", type="string")
150 parser.add_option("-p", "--policy", dest="policy", metavar="FILE")
151 parser.add_option("-l", "--library-path", dest="libpath", metavar="FILE")
152 parser.add_option("-t", "--test", dest="test", action="extend",
153 help="Test options include "+str(Tests))
154
155 (options, args) = parser.parse_args()
156
157 if not options.libpath:
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700158 sys.exit("Must specify path to libsepolwrap library\n" + parser.usage)
Dan Cashman91d398d2017-09-26 12:58:29 -0700159 if not os.path.exists(options.libpath):
160 sys.exit("Error: library-path " + options.libpath + " does not exist\n"
161 + parser.usage)
162
163 if not options.policy:
164 sys.exit("Must specify monolithic policy file\n" + parser.usage)
165 if not os.path.exists(options.policy):
166 sys.exit("Error: policy file " + options.policy + " does not exist\n"
167 + parser.usage)
168
169 if not options.file_contexts:
170 sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
171 for f in options.file_contexts:
172 if not os.path.exists(f):
173 sys.exit("Error: File_contexts file " + f + " does not exist\n" +
174 parser.usage)
175
176 pol = policy.Policy(options.policy, options.file_contexts, options.libpath)
177
178 results = ""
179 # If an individual test is not specified, run all tests.
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700180 if options.test is None or "TestDataTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700181 results += TestDataTypeViolations(pol)
Tri Vo4c80c2c2018-03-29 03:42:47 +0000182 if options.test is None or "TestProcTypeViolations" in options.test:
183 results += TestProcTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700184 if options.test is None or "TestSysfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700185 results += TestSysfsTypeViolations(pol)
Nick Kralevichdab131b2018-10-04 11:24:00 -0700186 if options.test is None or "TestSystemTypeViolations" in options.test:
187 results += TestSystemTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700188 if options.test is None or "TestDebugfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700189 results += TestDebugfsTypeViolations(pol)
Hridya Valsarajuedccaa82021-05-14 14:01:11 -0700190 if options.test is None or "TestTracefsTypeViolations" in options.test:
191 results += TestTracefsTypeViolations(pol)
Tri Vo30c3c2a2018-01-10 11:04:06 -0800192 if options.test is None or "TestVendorTypeViolations" in options.test:
193 results += TestVendorTypeViolations(pol)
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -0800194 if options.test is None or "TestCoreDataTypeViolations" in options.test:
195 results += TestCoreDataTypeViolations(pol)
Inseob Kim1b8b1f62020-10-23 15:16:11 +0900196 if options.test is None or "TestPropertyTypeViolations" in options.test:
197 results += TestPropertyTypeViolations(pol)
Alan Stokes668e74f2020-11-12 18:08:18 +0000198 if options.test is None or "TestAppDataTypeViolations" in options.test:
199 results += TestAppDataTypeViolations(pol)
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800200 if options.test is None or "TestDmaHeapDevTypeViolations" in options.test:
201 results += TestDmaHeapDevTypeViolations(pol)
Dan Cashman91d398d2017-09-26 12:58:29 -0700202
203 if len(results) > 0:
204 sys.exit(results)