blob: e940681ad75b850d08e317a7d7f5caddafb434ef [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 Kim3a9ac6f2022-07-19 14:27:36 +090021
22SHARED_LIB_EXTENSION = '.dylib' if sys.platform == 'darwin' else '.so'
Dan Cashman91d398d2017-09-26 12:58:29 -070023
24#############################################################
25# Tests
26#############################################################
27def TestDataTypeViolations(pol):
28 return pol.AssertPathTypesHaveAttr(["/data/"], [], "data_file_type")
29
Nick Kralevichdab131b2018-10-04 11:24:00 -070030def TestSystemTypeViolations(pol):
Steven Morelanda01338d2020-04-27 15:54:54 -070031 partitions = ["/system/", "/system_ext/", "/product/"]
32 exceptions = [
33 # devices before treble don't have a vendor partition
34 "/system/vendor/",
35
36 # overlay files are mounted over vendor
37 "/product/overlay/",
38 "/product/vendor_overlay/",
39 "/system/overlay/",
40 "/system/product/overlay/",
41 "/system/product/vendor_overlay/",
42 "/system/system_ext/overlay/",
43 "/system_ext/overlay/",
44 ]
45
46 return pol.AssertPathTypesHaveAttr(partitions, exceptions, "system_file_type")
Nick Kralevich5e372712018-09-27 10:21:37 -070047
Maciej Żenczykowskib13921c2022-05-21 05:03:29 -070048def TestBpffsTypeViolations(pol):
49 return pol.AssertGenfsFilesystemTypesHaveAttr("bpf", "bpffs_type")
50
Tri Vo4c80c2c2018-03-29 03:42:47 +000051def TestProcTypeViolations(pol):
52 return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type")
53
Dan Cashman91d398d2017-09-26 12:58:29 -070054def TestSysfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070055 ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type")
56 ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070057 "/sys/kernel/tracing"], "sysfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070058 return ret
Dan Cashman91d398d2017-09-26 12:58:29 -070059
60def TestDebugfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070061 ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070062 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070063 "/sys/kernel/tracing"], [], "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070064 return ret
Tri Vo30c3c2a2018-01-10 11:04:06 -080065
Hridya Valsarajuedccaa82021-05-14 14:01:11 -070066def TestTracefsTypeViolations(pol):
67 ret = pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "tracefs_type")
68 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/tracing"], [], "tracefs_type")
69 ret += pol.AssertPathTypesDoNotHaveAttr(["/sys/kernel/debug"],
70 ["/sys/kernel/debug/tracing"], "tracefs_type",
71 [])
72 return ret
73
Tri Vo30c3c2a2018-01-10 11:04:06 -080074def TestVendorTypeViolations(pol):
Steven Morelanda01338d2020-04-27 15:54:54 -070075 partitions = ["/vendor/", "/odm/"]
76 exceptions = [
77 "/vendor/etc/selinux/",
78 "/vendor/odm/etc/selinux/",
79 "/odm/etc/selinux/",
80 ]
81 return pol.AssertPathTypesHaveAttr(partitions, exceptions, "vendor_file_type")
Tri Vo30c3c2a2018-01-10 11:04:06 -080082
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080083def TestCoreDataTypeViolations(pol):
Jeff Vander Stoep370a52f2018-02-08 09:54:59 -080084 return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor",
85 "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type")
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080086
Inseob Kim1b8b1f62020-10-23 15:16:11 +090087def TestPropertyTypeViolations(pol):
88 return pol.AssertPropertyOwnersAreExclusive()
89
Alan Stokes668e74f2020-11-12 18:08:18 +000090def TestAppDataTypeViolations(pol):
91 # Types with the app_data_file_type should only be used for app data files
92 # (/data/data/package.name etc) via seapp_contexts, and never applied
93 # explicitly to other files.
94 partitions = [
95 "/data/",
96 "/vendor/",
97 "/odm/",
98 "/product/",
99 ]
100 exceptions = [
101 # These are used for app data files for the corresponding user and
102 # assorted other files.
103 # TODO(b/172812577): Use different types for the different purposes
104 "shell_data_file",
105 "bluetooth_data_file",
106 "nfc_data_file",
107 "radio_data_file",
108 ]
109 return pol.AssertPathTypesDoNotHaveAttr(partitions, [], "app_data_file_type",
110 exceptions)
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800111def TestDmaHeapDevTypeViolations(pol):
112 return pol.AssertPathTypesHaveAttr(["/dev/dma_heap/"], [],
113 "dmabuf_heap_device_type")
114
Alan Stokes668e74f2020-11-12 18:08:18 +0000115
Inseob Kim1b8b1f62020-10-23 15:16:11 +0900116
Dan Cashman91d398d2017-09-26 12:58:29 -0700117###
118# extend OptionParser to allow the same option flag to be used multiple times.
119# This is used to allow multiple file_contexts files and tests to be
120# specified.
121#
122class MultipleOption(Option):
123 ACTIONS = Option.ACTIONS + ("extend",)
124 STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
125 TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
126 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
127
128 def take_action(self, action, dest, opt, value, values, parser):
129 if action == "extend":
130 values.ensure_value(dest, []).append(value)
131 else:
132 Option.take_action(self, action, dest, opt, value, values, parser)
133
Tri Vo4c80c2c2018-03-29 03:42:47 +0000134Tests = [
Maciej Żenczykowskib13921c2022-05-21 05:03:29 -0700135 "TestBpffsTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000136 "TestDataTypeViolators",
137 "TestProcTypeViolations",
138 "TestSysfsTypeViolations",
Nick Kralevichdab131b2018-10-04 11:24:00 -0700139 "TestSystemTypeViolators",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000140 "TestDebugfsTypeViolations",
Hridya Valsarajuedccaa82021-05-14 14:01:11 -0700141 "TestTracefsTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000142 "TestVendorTypeViolations",
143 "TestCoreDataTypeViolations",
Alan Stokes668e74f2020-11-12 18:08:18 +0000144 "TestPropertyTypeViolations",
145 "TestAppDataTypeViolations",
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800146 "TestDmaHeapDevTypeViolations",
Tri Vo4c80c2c2018-03-29 03:42:47 +0000147]
Dan Cashman91d398d2017-09-26 12:58:29 -0700148
149if __name__ == '__main__':
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900150 usage = "sepolicy_tests -f vendor_file_contexts -f "
Dan Cashman91d398d2017-09-26 12:58:29 -0700151 usage +="plat_file_contexts -p policy [--test test] [--help]"
152 parser = OptionParser(option_class=MultipleOption, usage=usage)
153 parser.add_option("-f", "--file_contexts", dest="file_contexts",
154 metavar="FILE", action="extend", type="string")
155 parser.add_option("-p", "--policy", dest="policy", metavar="FILE")
Dan Cashman91d398d2017-09-26 12:58:29 -0700156 parser.add_option("-t", "--test", dest="test", action="extend",
157 help="Test options include "+str(Tests))
158
159 (options, args) = parser.parse_args()
160
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900161 libpath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
Inseob Kim3a9ac6f2022-07-19 14:27:36 +0900162 "libsepolwrap" + SHARED_LIB_EXTENSION)
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900163 if not os.path.exists(libpath):
164 sys.exit("Error: libsepolwrap does not exist. Is this binary corrupted?\n")
Dan Cashman91d398d2017-09-26 12:58:29 -0700165
166 if not options.policy:
167 sys.exit("Must specify monolithic policy file\n" + parser.usage)
168 if not os.path.exists(options.policy):
169 sys.exit("Error: policy file " + options.policy + " does not exist\n"
170 + parser.usage)
171
172 if not options.file_contexts:
173 sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
174 for f in options.file_contexts:
175 if not os.path.exists(f):
176 sys.exit("Error: File_contexts file " + f + " does not exist\n" +
177 parser.usage)
178
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900179 pol = policy.Policy(options.policy, options.file_contexts, libpath)
Dan Cashman91d398d2017-09-26 12:58:29 -0700180
181 results = ""
182 # If an individual test is not specified, run all tests.
Maciej Żenczykowskib13921c2022-05-21 05:03:29 -0700183 if options.test is None or "TestBpffsTypeViolations" in options.test:
184 results += TestBpffsTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700185 if options.test is None or "TestDataTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700186 results += TestDataTypeViolations(pol)
Tri Vo4c80c2c2018-03-29 03:42:47 +0000187 if options.test is None or "TestProcTypeViolations" in options.test:
188 results += TestProcTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700189 if options.test is None or "TestSysfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700190 results += TestSysfsTypeViolations(pol)
Nick Kralevichdab131b2018-10-04 11:24:00 -0700191 if options.test is None or "TestSystemTypeViolations" in options.test:
192 results += TestSystemTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700193 if options.test is None or "TestDebugfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700194 results += TestDebugfsTypeViolations(pol)
Hridya Valsarajuedccaa82021-05-14 14:01:11 -0700195 if options.test is None or "TestTracefsTypeViolations" in options.test:
196 results += TestTracefsTypeViolations(pol)
Tri Vo30c3c2a2018-01-10 11:04:06 -0800197 if options.test is None or "TestVendorTypeViolations" in options.test:
198 results += TestVendorTypeViolations(pol)
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -0800199 if options.test is None or "TestCoreDataTypeViolations" in options.test:
200 results += TestCoreDataTypeViolations(pol)
Inseob Kim1b8b1f62020-10-23 15:16:11 +0900201 if options.test is None or "TestPropertyTypeViolations" in options.test:
202 results += TestPropertyTypeViolations(pol)
Alan Stokes668e74f2020-11-12 18:08:18 +0000203 if options.test is None or "TestAppDataTypeViolations" in options.test:
204 results += TestAppDataTypeViolations(pol)
Hridya Valsaraju8c9cf622020-12-14 22:57:49 -0800205 if options.test is None or "TestDmaHeapDevTypeViolations" in options.test:
206 results += TestDmaHeapDevTypeViolations(pol)
Dan Cashman91d398d2017-09-26 12:58:29 -0700207
208 if len(results) > 0:
209 sys.exit(results)