blob: 0a87a134802a0efaf350114153b2fac1b391948f [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
Tri Vo4c80c2c2018-03-29 03:42:47 +000047def TestProcTypeViolations(pol):
48 return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type")
49
Dan Cashman91d398d2017-09-26 12:58:29 -070050def TestSysfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070051 ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type")
52 ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070053 "/sys/kernel/tracing"], "sysfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070054 return ret
Dan Cashman91d398d2017-09-26 12:58:29 -070055
56def TestDebugfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070057 ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070058 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070059 "/sys/kernel/tracing"], [], "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070060 return ret
Tri Vo30c3c2a2018-01-10 11:04:06 -080061
Hridya Valsarajuedccaa82021-05-14 14:01:11 -070062def TestTracefsTypeViolations(pol):
63 ret = pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "tracefs_type")
64 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/tracing"], [], "tracefs_type")
65 ret += pol.AssertPathTypesDoNotHaveAttr(["/sys/kernel/debug"],
66 ["/sys/kernel/debug/tracing"], "tracefs_type",
67 [])
68 return ret
69
Tri Vo30c3c2a2018-01-10 11:04:06 -080070def TestVendorTypeViolations(pol):
Steven Morelanda01338d2020-04-27 15:54:54 -070071 partitions = ["/vendor/", "/odm/"]
72 exceptions = [
73 "/vendor/etc/selinux/",
74 "/vendor/odm/etc/selinux/",
75 "/odm/etc/selinux/",
76 ]
77 return pol.AssertPathTypesHaveAttr(partitions, exceptions, "vendor_file_type")
Tri Vo30c3c2a2018-01-10 11:04:06 -080078
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080079def TestCoreDataTypeViolations(pol):
Jeff Vander Stoep370a52f2018-02-08 09:54:59 -080080 return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor",
81 "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type")
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080082
Inseob Kim1b8b1f62020-10-23 15:16:11 +090083def TestPropertyTypeViolations(pol):
84 return pol.AssertPropertyOwnersAreExclusive()
85
Alan Stokes668e74f2020-11-12 18:08:18 +000086def TestAppDataTypeViolations(pol):
87 # Types with the app_data_file_type should only be used for app data files
88 # (/data/data/package.name etc) via seapp_contexts, and never applied
89 # explicitly to other files.
90 partitions = [
91 "/data/",
92 "/vendor/",
93 "/odm/",
94 "/product/",
95 ]
96 exceptions = [
97 # These are used for app data files for the corresponding user and
98 # assorted other files.
99 # TODO(b/172812577): Use different types for the different purposes
100 "shell_data_file",
101 "bluetooth_data_file",
102 "nfc_data_file",
103 "radio_data_file",
104 ]
105 return pol.AssertPathTypesDoNotHaveAttr(partitions, [], "app_data_file_type",
106 exceptions)
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800107def TestDmaHeapDevTypeViolations(pol):
108 return pol.AssertPathTypesHaveAttr(["/dev/dma_heap/"], [],
109 "dmabuf_heap_device_type")
110
Alan Stokes668e74f2020-11-12 18:08:18 +0000111
Inseob Kim1b8b1f62020-10-23 15:16:11 +0900112
Dan Cashman91d398d2017-09-26 12:58:29 -0700113###
114# extend OptionParser to allow the same option flag to be used multiple times.
115# This is used to allow multiple file_contexts files and tests to be
116# specified.
117#
118class MultipleOption(Option):
119 ACTIONS = Option.ACTIONS + ("extend",)
120 STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
121 TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
122 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
123
124 def take_action(self, action, dest, opt, value, values, parser):
125 if action == "extend":
126 values.ensure_value(dest, []).append(value)
127 else:
128 Option.take_action(self, action, dest, opt, value, values, parser)
129
Tri Vo4c80c2c2018-03-29 03:42:47 +0000130Tests = [
131 "TestDataTypeViolators",
132 "TestProcTypeViolations",
133 "TestSysfsTypeViolations",
Nick Kralevichdab131b2018-10-04 11:24:00 -0700134 "TestSystemTypeViolators",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000135 "TestDebugfsTypeViolations",
Hridya Valsarajuedccaa82021-05-14 14:01:11 -0700136 "TestTracefsTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000137 "TestVendorTypeViolations",
138 "TestCoreDataTypeViolations",
Alan Stokes668e74f2020-11-12 18:08:18 +0000139 "TestPropertyTypeViolations",
140 "TestAppDataTypeViolations",
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800141 "TestDmaHeapDevTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000142]
Dan Cashman91d398d2017-09-26 12:58:29 -0700143
144if __name__ == '__main__':
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900145 usage = "sepolicy_tests -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")
Dan Cashman91d398d2017-09-26 12:58:29 -0700151 parser.add_option("-t", "--test", dest="test", action="extend",
152 help="Test options include "+str(Tests))
153
154 (options, args) = parser.parse_args()
155
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900156 libpath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
157 "libsepolwrap" + distutils.ccompiler.new_compiler().shared_lib_extension)
158 if not os.path.exists(libpath):
159 sys.exit("Error: libsepolwrap does not exist. Is this binary corrupted?\n")
Dan Cashman91d398d2017-09-26 12:58:29 -0700160
161 if not options.policy:
162 sys.exit("Must specify monolithic policy file\n" + parser.usage)
163 if not os.path.exists(options.policy):
164 sys.exit("Error: policy file " + options.policy + " does not exist\n"
165 + parser.usage)
166
167 if not options.file_contexts:
168 sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
169 for f in options.file_contexts:
170 if not os.path.exists(f):
171 sys.exit("Error: File_contexts file " + f + " does not exist\n" +
172 parser.usage)
173
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900174 pol = policy.Policy(options.policy, options.file_contexts, libpath)
Dan Cashman91d398d2017-09-26 12:58:29 -0700175
176 results = ""
177 # If an individual test is not specified, run all tests.
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700178 if options.test is None or "TestDataTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700179 results += TestDataTypeViolations(pol)
Tri Vo4c80c2c2018-03-29 03:42:47 +0000180 if options.test is None or "TestProcTypeViolations" in options.test:
181 results += TestProcTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700182 if options.test is None or "TestSysfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700183 results += TestSysfsTypeViolations(pol)
Nick Kralevichdab131b2018-10-04 11:24:00 -0700184 if options.test is None or "TestSystemTypeViolations" in options.test:
185 results += TestSystemTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700186 if options.test is None or "TestDebugfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700187 results += TestDebugfsTypeViolations(pol)
Hridya Valsarajuedccaa82021-05-14 14:01:11 -0700188 if options.test is None or "TestTracefsTypeViolations" in options.test:
189 results += TestTracefsTypeViolations(pol)
Tri Vo30c3c2a2018-01-10 11:04:06 -0800190 if options.test is None or "TestVendorTypeViolations" in options.test:
191 results += TestVendorTypeViolations(pol)
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -0800192 if options.test is None or "TestCoreDataTypeViolations" in options.test:
193 results += TestCoreDataTypeViolations(pol)
Inseob Kim1b8b1f62020-10-23 15:16:11 +0900194 if options.test is None or "TestPropertyTypeViolations" in options.test:
195 results += TestPropertyTypeViolations(pol)
Alan Stokes668e74f2020-11-12 18:08:18 +0000196 if options.test is None or "TestAppDataTypeViolations" in options.test:
197 results += TestAppDataTypeViolations(pol)
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800198 if options.test is None or "TestDmaHeapDevTypeViolations" in options.test:
199 results += TestDmaHeapDevTypeViolations(pol)
Dan Cashman91d398d2017-09-26 12:58:29 -0700200
201 if len(results) > 0:
202 sys.exit(results)