blob: 63144dda44a8495bd3dc0191601e877ce89fb102 [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
Inseob Kim4912a242022-07-25 11:30:02 +090018import pkgutil
Dan Cashman91d398d2017-09-26 12:58:29 -070019import policy
20import re
Inseob Kim4912a242022-07-25 11:30:02 +090021import shutil
Dan Cashman91d398d2017-09-26 12:58:29 -070022import sys
Inseob Kim4912a242022-07-25 11:30:02 +090023import tempfile
Inseob Kim3a9ac6f2022-07-19 14:27:36 +090024
25SHARED_LIB_EXTENSION = '.dylib' if sys.platform == 'darwin' else '.so'
Dan Cashman91d398d2017-09-26 12:58:29 -070026
27#############################################################
28# Tests
29#############################################################
30def TestDataTypeViolations(pol):
31 return pol.AssertPathTypesHaveAttr(["/data/"], [], "data_file_type")
32
Nick Kralevichdab131b2018-10-04 11:24:00 -070033def TestSystemTypeViolations(pol):
Steven Morelanda01338d2020-04-27 15:54:54 -070034 partitions = ["/system/", "/system_ext/", "/product/"]
35 exceptions = [
36 # devices before treble don't have a vendor partition
37 "/system/vendor/",
38
39 # overlay files are mounted over vendor
40 "/product/overlay/",
41 "/product/vendor_overlay/",
42 "/system/overlay/",
43 "/system/product/overlay/",
44 "/system/product/vendor_overlay/",
45 "/system/system_ext/overlay/",
46 "/system_ext/overlay/",
47 ]
48
49 return pol.AssertPathTypesHaveAttr(partitions, exceptions, "system_file_type")
Nick Kralevich5e372712018-09-27 10:21:37 -070050
Maciej Żenczykowskib13921c2022-05-21 05:03:29 -070051def TestBpffsTypeViolations(pol):
52 return pol.AssertGenfsFilesystemTypesHaveAttr("bpf", "bpffs_type")
53
Tri Vo4c80c2c2018-03-29 03:42:47 +000054def TestProcTypeViolations(pol):
55 return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type")
56
Dan Cashman91d398d2017-09-26 12:58:29 -070057def TestSysfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070058 ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type")
59 ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070060 "/sys/kernel/tracing"], "sysfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070061 return ret
Dan Cashman91d398d2017-09-26 12:58:29 -070062
63def TestDebugfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070064 ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070065 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070066 "/sys/kernel/tracing"], [], "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070067 return ret
Tri Vo30c3c2a2018-01-10 11:04:06 -080068
Hridya Valsarajuedccaa82021-05-14 14:01:11 -070069def TestTracefsTypeViolations(pol):
70 ret = pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "tracefs_type")
71 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/tracing"], [], "tracefs_type")
72 ret += pol.AssertPathTypesDoNotHaveAttr(["/sys/kernel/debug"],
73 ["/sys/kernel/debug/tracing"], "tracefs_type",
74 [])
75 return ret
76
Tri Vo30c3c2a2018-01-10 11:04:06 -080077def TestVendorTypeViolations(pol):
Steven Morelanda01338d2020-04-27 15:54:54 -070078 partitions = ["/vendor/", "/odm/"]
79 exceptions = [
80 "/vendor/etc/selinux/",
81 "/vendor/odm/etc/selinux/",
82 "/odm/etc/selinux/",
83 ]
84 return pol.AssertPathTypesHaveAttr(partitions, exceptions, "vendor_file_type")
Tri Vo30c3c2a2018-01-10 11:04:06 -080085
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080086def TestCoreDataTypeViolations(pol):
Jeff Vander Stoep370a52f2018-02-08 09:54:59 -080087 return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor",
88 "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type")
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080089
Inseob Kim1b8b1f62020-10-23 15:16:11 +090090def TestPropertyTypeViolations(pol):
91 return pol.AssertPropertyOwnersAreExclusive()
92
Alan Stokes668e74f2020-11-12 18:08:18 +000093def TestAppDataTypeViolations(pol):
94 # Types with the app_data_file_type should only be used for app data files
95 # (/data/data/package.name etc) via seapp_contexts, and never applied
96 # explicitly to other files.
97 partitions = [
98 "/data/",
99 "/vendor/",
100 "/odm/",
101 "/product/",
102 ]
103 exceptions = [
104 # These are used for app data files for the corresponding user and
105 # assorted other files.
106 # TODO(b/172812577): Use different types for the different purposes
107 "shell_data_file",
108 "bluetooth_data_file",
109 "nfc_data_file",
110 "radio_data_file",
111 ]
112 return pol.AssertPathTypesDoNotHaveAttr(partitions, [], "app_data_file_type",
113 exceptions)
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800114def TestDmaHeapDevTypeViolations(pol):
115 return pol.AssertPathTypesHaveAttr(["/dev/dma_heap/"], [],
116 "dmabuf_heap_device_type")
117
Alan Stokes668e74f2020-11-12 18:08:18 +0000118
Inseob Kim1b8b1f62020-10-23 15:16:11 +0900119
Dan Cashman91d398d2017-09-26 12:58:29 -0700120###
121# extend OptionParser to allow the same option flag to be used multiple times.
122# This is used to allow multiple file_contexts files and tests to be
123# specified.
124#
125class MultipleOption(Option):
126 ACTIONS = Option.ACTIONS + ("extend",)
127 STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
128 TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
129 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
130
131 def take_action(self, action, dest, opt, value, values, parser):
132 if action == "extend":
133 values.ensure_value(dest, []).append(value)
134 else:
135 Option.take_action(self, action, dest, opt, value, values, parser)
136
Tri Vo4c80c2c2018-03-29 03:42:47 +0000137Tests = [
Maciej Żenczykowskib13921c2022-05-21 05:03:29 -0700138 "TestBpffsTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000139 "TestDataTypeViolators",
140 "TestProcTypeViolations",
141 "TestSysfsTypeViolations",
Nick Kralevichdab131b2018-10-04 11:24:00 -0700142 "TestSystemTypeViolators",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000143 "TestDebugfsTypeViolations",
Hridya Valsarajuedccaa82021-05-14 14:01:11 -0700144 "TestTracefsTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000145 "TestVendorTypeViolations",
146 "TestCoreDataTypeViolations",
Alan Stokes668e74f2020-11-12 18:08:18 +0000147 "TestPropertyTypeViolations",
148 "TestAppDataTypeViolations",
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800149 "TestDmaHeapDevTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000150]
Dan Cashman91d398d2017-09-26 12:58:29 -0700151
Inseob Kim4912a242022-07-25 11:30:02 +0900152def do_main(libpath):
153 """
154 Args:
155 libpath: string, path to libsepolwrap.so
156 """
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900157 usage = "sepolicy_tests -f vendor_file_contexts -f "
Dan Cashman91d398d2017-09-26 12:58:29 -0700158 usage +="plat_file_contexts -p policy [--test test] [--help]"
159 parser = OptionParser(option_class=MultipleOption, usage=usage)
160 parser.add_option("-f", "--file_contexts", dest="file_contexts",
161 metavar="FILE", action="extend", type="string")
162 parser.add_option("-p", "--policy", dest="policy", metavar="FILE")
Dan Cashman91d398d2017-09-26 12:58:29 -0700163 parser.add_option("-t", "--test", dest="test", action="extend",
164 help="Test options include "+str(Tests))
165
166 (options, args) = parser.parse_args()
167
Dan Cashman91d398d2017-09-26 12:58:29 -0700168 if not options.policy:
169 sys.exit("Must specify monolithic policy file\n" + parser.usage)
170 if not os.path.exists(options.policy):
171 sys.exit("Error: policy file " + options.policy + " does not exist\n"
172 + parser.usage)
173
174 if not options.file_contexts:
175 sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
176 for f in options.file_contexts:
177 if not os.path.exists(f):
178 sys.exit("Error: File_contexts file " + f + " does not exist\n" +
179 parser.usage)
180
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900181 pol = policy.Policy(options.policy, options.file_contexts, libpath)
Dan Cashman91d398d2017-09-26 12:58:29 -0700182
183 results = ""
184 # If an individual test is not specified, run all tests.
Maciej Żenczykowskib13921c2022-05-21 05:03:29 -0700185 if options.test is None or "TestBpffsTypeViolations" in options.test:
186 results += TestBpffsTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700187 if options.test is None or "TestDataTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700188 results += TestDataTypeViolations(pol)
Tri Vo4c80c2c2018-03-29 03:42:47 +0000189 if options.test is None or "TestProcTypeViolations" in options.test:
190 results += TestProcTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700191 if options.test is None or "TestSysfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700192 results += TestSysfsTypeViolations(pol)
Nick Kralevichdab131b2018-10-04 11:24:00 -0700193 if options.test is None or "TestSystemTypeViolations" in options.test:
194 results += TestSystemTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700195 if options.test is None or "TestDebugfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700196 results += TestDebugfsTypeViolations(pol)
Hridya Valsarajuedccaa82021-05-14 14:01:11 -0700197 if options.test is None or "TestTracefsTypeViolations" in options.test:
198 results += TestTracefsTypeViolations(pol)
Tri Vo30c3c2a2018-01-10 11:04:06 -0800199 if options.test is None or "TestVendorTypeViolations" in options.test:
200 results += TestVendorTypeViolations(pol)
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -0800201 if options.test is None or "TestCoreDataTypeViolations" in options.test:
202 results += TestCoreDataTypeViolations(pol)
Inseob Kim1b8b1f62020-10-23 15:16:11 +0900203 if options.test is None or "TestPropertyTypeViolations" in options.test:
204 results += TestPropertyTypeViolations(pol)
Alan Stokes668e74f2020-11-12 18:08:18 +0000205 if options.test is None or "TestAppDataTypeViolations" in options.test:
206 results += TestAppDataTypeViolations(pol)
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800207 if options.test is None or "TestDmaHeapDevTypeViolations" in options.test:
208 results += TestDmaHeapDevTypeViolations(pol)
Dan Cashman91d398d2017-09-26 12:58:29 -0700209
210 if len(results) > 0:
211 sys.exit(results)
Inseob Kim4912a242022-07-25 11:30:02 +0900212
213if __name__ == '__main__':
214 temp_dir = tempfile.mkdtemp()
215 try:
216 libname = "libsepolwrap" + SHARED_LIB_EXTENSION
217 libpath = os.path.join(temp_dir, libname)
218 with open(libpath, "wb") as f:
219 blob = pkgutil.get_data("sepolicy_tests", libname)
220 if not blob:
221 sys.exit("Error: libsepolwrap does not exist. Is this binary corrupted?\n")
222 f.write(blob)
223 do_main(libpath)
224 finally:
225 shutil.rmtree(temp_dir)