blob: 79c55de9d077f4ce3b5d6d36c3973ed0ef953ff0 [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
Inseob Kim6fa8efd2021-12-29 13:56:14 +090021import distutils.ccompiler
Dan Cashman91d398d2017-09-26 12:58:29 -070022
23#############################################################
24# Tests
25#############################################################
26def TestDataTypeViolations(pol):
27 return pol.AssertPathTypesHaveAttr(["/data/"], [], "data_file_type")
28
Nick Kralevichdab131b2018-10-04 11:24:00 -070029def TestSystemTypeViolations(pol):
Steven Morelanda01338d2020-04-27 15:54:54 -070030 partitions = ["/system/", "/system_ext/", "/product/"]
31 exceptions = [
32 # devices before treble don't have a vendor partition
33 "/system/vendor/",
34
35 # overlay files are mounted over vendor
36 "/product/overlay/",
37 "/product/vendor_overlay/",
38 "/system/overlay/",
39 "/system/product/overlay/",
40 "/system/product/vendor_overlay/",
41 "/system/system_ext/overlay/",
42 "/system_ext/overlay/",
43 ]
44
45 return pol.AssertPathTypesHaveAttr(partitions, exceptions, "system_file_type")
Nick Kralevich5e372712018-09-27 10:21:37 -070046
Maciej Żenczykowskib13921c2022-05-21 05:03:29 -070047def TestBpffsTypeViolations(pol):
48 return pol.AssertGenfsFilesystemTypesHaveAttr("bpf", "bpffs_type")
49
Tri Vo4c80c2c2018-03-29 03:42:47 +000050def TestProcTypeViolations(pol):
51 return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type")
52
Dan Cashman91d398d2017-09-26 12:58:29 -070053def TestSysfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070054 ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type")
55 ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070056 "/sys/kernel/tracing"], "sysfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070057 return ret
Dan Cashman91d398d2017-09-26 12:58:29 -070058
59def TestDebugfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070060 ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070061 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070062 "/sys/kernel/tracing"], [], "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070063 return ret
Tri Vo30c3c2a2018-01-10 11:04:06 -080064
Hridya Valsarajuedccaa82021-05-14 14:01:11 -070065def TestTracefsTypeViolations(pol):
66 ret = pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "tracefs_type")
67 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/tracing"], [], "tracefs_type")
68 ret += pol.AssertPathTypesDoNotHaveAttr(["/sys/kernel/debug"],
69 ["/sys/kernel/debug/tracing"], "tracefs_type",
70 [])
71 return ret
72
Tri Vo30c3c2a2018-01-10 11:04:06 -080073def TestVendorTypeViolations(pol):
Steven Morelanda01338d2020-04-27 15:54:54 -070074 partitions = ["/vendor/", "/odm/"]
75 exceptions = [
76 "/vendor/etc/selinux/",
77 "/vendor/odm/etc/selinux/",
78 "/odm/etc/selinux/",
79 ]
80 return pol.AssertPathTypesHaveAttr(partitions, exceptions, "vendor_file_type")
Tri Vo30c3c2a2018-01-10 11:04:06 -080081
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080082def TestCoreDataTypeViolations(pol):
Jeff Vander Stoep370a52f2018-02-08 09:54:59 -080083 return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor",
84 "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type")
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080085
Inseob Kim1b8b1f62020-10-23 15:16:11 +090086def TestPropertyTypeViolations(pol):
87 return pol.AssertPropertyOwnersAreExclusive()
88
Alan Stokes668e74f2020-11-12 18:08:18 +000089def TestAppDataTypeViolations(pol):
90 # Types with the app_data_file_type should only be used for app data files
91 # (/data/data/package.name etc) via seapp_contexts, and never applied
92 # explicitly to other files.
93 partitions = [
94 "/data/",
95 "/vendor/",
96 "/odm/",
97 "/product/",
98 ]
99 exceptions = [
100 # These are used for app data files for the corresponding user and
101 # assorted other files.
102 # TODO(b/172812577): Use different types for the different purposes
103 "shell_data_file",
104 "bluetooth_data_file",
105 "nfc_data_file",
106 "radio_data_file",
107 ]
108 return pol.AssertPathTypesDoNotHaveAttr(partitions, [], "app_data_file_type",
109 exceptions)
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800110def TestDmaHeapDevTypeViolations(pol):
111 return pol.AssertPathTypesHaveAttr(["/dev/dma_heap/"], [],
112 "dmabuf_heap_device_type")
113
Alan Stokes668e74f2020-11-12 18:08:18 +0000114
Inseob Kim1b8b1f62020-10-23 15:16:11 +0900115
Dan Cashman91d398d2017-09-26 12:58:29 -0700116###
117# extend OptionParser to allow the same option flag to be used multiple times.
118# This is used to allow multiple file_contexts files and tests to be
119# specified.
120#
121class MultipleOption(Option):
122 ACTIONS = Option.ACTIONS + ("extend",)
123 STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
124 TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
125 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
126
127 def take_action(self, action, dest, opt, value, values, parser):
128 if action == "extend":
129 values.ensure_value(dest, []).append(value)
130 else:
131 Option.take_action(self, action, dest, opt, value, values, parser)
132
Tri Vo4c80c2c2018-03-29 03:42:47 +0000133Tests = [
Maciej Żenczykowskib13921c2022-05-21 05:03:29 -0700134 "TestBpffsTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000135 "TestDataTypeViolators",
136 "TestProcTypeViolations",
137 "TestSysfsTypeViolations",
Nick Kralevichdab131b2018-10-04 11:24:00 -0700138 "TestSystemTypeViolators",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000139 "TestDebugfsTypeViolations",
Hridya Valsarajuedccaa82021-05-14 14:01:11 -0700140 "TestTracefsTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000141 "TestVendorTypeViolations",
142 "TestCoreDataTypeViolations",
Alan Stokes668e74f2020-11-12 18:08:18 +0000143 "TestPropertyTypeViolations",
144 "TestAppDataTypeViolations",
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800145 "TestDmaHeapDevTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000146]
Dan Cashman91d398d2017-09-26 12:58:29 -0700147
148if __name__ == '__main__':
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900149 usage = "sepolicy_tests -f vendor_file_contexts -f "
Dan Cashman91d398d2017-09-26 12:58:29 -0700150 usage +="plat_file_contexts -p policy [--test test] [--help]"
151 parser = OptionParser(option_class=MultipleOption, usage=usage)
152 parser.add_option("-f", "--file_contexts", dest="file_contexts",
153 metavar="FILE", action="extend", type="string")
154 parser.add_option("-p", "--policy", dest="policy", metavar="FILE")
Dan Cashman91d398d2017-09-26 12:58:29 -0700155 parser.add_option("-t", "--test", dest="test", action="extend",
156 help="Test options include "+str(Tests))
157
158 (options, args) = parser.parse_args()
159
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900160 libpath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
161 "libsepolwrap" + distutils.ccompiler.new_compiler().shared_lib_extension)
162 if not os.path.exists(libpath):
163 sys.exit("Error: libsepolwrap does not exist. Is this binary corrupted?\n")
Dan Cashman91d398d2017-09-26 12:58:29 -0700164
165 if not options.policy:
166 sys.exit("Must specify monolithic policy file\n" + parser.usage)
167 if not os.path.exists(options.policy):
168 sys.exit("Error: policy file " + options.policy + " does not exist\n"
169 + parser.usage)
170
171 if not options.file_contexts:
172 sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
173 for f in options.file_contexts:
174 if not os.path.exists(f):
175 sys.exit("Error: File_contexts file " + f + " does not exist\n" +
176 parser.usage)
177
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900178 pol = policy.Policy(options.policy, options.file_contexts, libpath)
Dan Cashman91d398d2017-09-26 12:58:29 -0700179
180 results = ""
181 # If an individual test is not specified, run all tests.
Maciej Żenczykowskib13921c2022-05-21 05:03:29 -0700182 if options.test is None or "TestBpffsTypeViolations" in options.test:
183 results += TestBpffsTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700184 if options.test is None or "TestDataTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700185 results += TestDataTypeViolations(pol)
Tri Vo4c80c2c2018-03-29 03:42:47 +0000186 if options.test is None or "TestProcTypeViolations" in options.test:
187 results += TestProcTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700188 if options.test is None or "TestSysfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700189 results += TestSysfsTypeViolations(pol)
Nick Kralevichdab131b2018-10-04 11:24:00 -0700190 if options.test is None or "TestSystemTypeViolations" in options.test:
191 results += TestSystemTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700192 if options.test is None or "TestDebugfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700193 results += TestDebugfsTypeViolations(pol)
Hridya Valsarajuedccaa82021-05-14 14:01:11 -0700194 if options.test is None or "TestTracefsTypeViolations" in options.test:
195 results += TestTracefsTypeViolations(pol)
Tri Vo30c3c2a2018-01-10 11:04:06 -0800196 if options.test is None or "TestVendorTypeViolations" in options.test:
197 results += TestVendorTypeViolations(pol)
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -0800198 if options.test is None or "TestCoreDataTypeViolations" in options.test:
199 results += TestCoreDataTypeViolations(pol)
Inseob Kim1b8b1f62020-10-23 15:16:11 +0900200 if options.test is None or "TestPropertyTypeViolations" in options.test:
201 results += TestPropertyTypeViolations(pol)
Alan Stokes668e74f2020-11-12 18:08:18 +0000202 if options.test is None or "TestAppDataTypeViolations" in options.test:
203 results += TestAppDataTypeViolations(pol)
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800204 if options.test is None or "TestDmaHeapDevTypeViolations" in options.test:
205 results += TestDmaHeapDevTypeViolations(pol)
Dan Cashman91d398d2017-09-26 12:58:29 -0700206
207 if len(results) > 0:
208 sys.exit(results)