Thiébaud Weksteen | f24b457 | 2021-11-26 09:12:41 +1100 | [diff] [blame] | 1 | # 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 Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 15 | from optparse import OptionParser |
| 16 | from optparse import Option, OptionValueError |
| 17 | import os |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame] | 18 | import pkgutil |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 19 | import policy |
| 20 | import re |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame] | 21 | import shutil |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 22 | import sys |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame] | 23 | import tempfile |
Inseob Kim | 3a9ac6f | 2022-07-19 14:27:36 +0900 | [diff] [blame] | 24 | |
| 25 | SHARED_LIB_EXTENSION = '.dylib' if sys.platform == 'darwin' else '.so' |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 26 | |
| 27 | ############################################################# |
| 28 | # Tests |
| 29 | ############################################################# |
| 30 | def TestDataTypeViolations(pol): |
| 31 | return pol.AssertPathTypesHaveAttr(["/data/"], [], "data_file_type") |
| 32 | |
Nick Kralevich | dab131b | 2018-10-04 11:24:00 -0700 | [diff] [blame] | 33 | def TestSystemTypeViolations(pol): |
Steven Moreland | a01338d | 2020-04-27 15:54:54 -0700 | [diff] [blame] | 34 | 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/", |
Inseob Kim | 1b6a129 | 2024-08-27 17:46:27 +0900 | [diff] [blame] | 47 | |
| 48 | # adb_keys_file hasn't been a system_file_type |
| 49 | "/product/etc/security/adb_keys", |
| 50 | "/system/product/etc/security/adb_keys", |
Steven Moreland | a01338d | 2020-04-27 15:54:54 -0700 | [diff] [blame] | 51 | ] |
| 52 | |
| 53 | return pol.AssertPathTypesHaveAttr(partitions, exceptions, "system_file_type") |
Nick Kralevich | 5e37271 | 2018-09-27 10:21:37 -0700 | [diff] [blame] | 54 | |
Maciej Żenczykowski | b13921c | 2022-05-21 05:03:29 -0700 | [diff] [blame] | 55 | def TestBpffsTypeViolations(pol): |
| 56 | return pol.AssertGenfsFilesystemTypesHaveAttr("bpf", "bpffs_type") |
| 57 | |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 58 | def TestProcTypeViolations(pol): |
| 59 | return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type") |
| 60 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 61 | def TestSysfsTypeViolations(pol): |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 62 | ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type") |
| 63 | ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/", |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 64 | "/sys/kernel/tracing"], "sysfs_type") |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 65 | return ret |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 66 | |
| 67 | def TestDebugfsTypeViolations(pol): |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 68 | ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type") |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 69 | ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/", |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 70 | "/sys/kernel/tracing"], [], "debugfs_type") |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 71 | return ret |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 72 | |
Hridya Valsaraju | edccaa8 | 2021-05-14 14:01:11 -0700 | [diff] [blame] | 73 | def TestTracefsTypeViolations(pol): |
| 74 | ret = pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "tracefs_type") |
| 75 | ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/tracing"], [], "tracefs_type") |
| 76 | ret += pol.AssertPathTypesDoNotHaveAttr(["/sys/kernel/debug"], |
| 77 | ["/sys/kernel/debug/tracing"], "tracefs_type", |
| 78 | []) |
| 79 | return ret |
| 80 | |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 81 | def TestVendorTypeViolations(pol): |
Steven Moreland | a01338d | 2020-04-27 15:54:54 -0700 | [diff] [blame] | 82 | partitions = ["/vendor/", "/odm/"] |
| 83 | exceptions = [ |
| 84 | "/vendor/etc/selinux/", |
| 85 | "/vendor/odm/etc/selinux/", |
| 86 | "/odm/etc/selinux/", |
| 87 | ] |
| 88 | return pol.AssertPathTypesHaveAttr(partitions, exceptions, "vendor_file_type") |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 89 | |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 90 | def TestCoreDataTypeViolations(pol): |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 91 | ret = pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor", |
Jeff Vander Stoep | 370a52f | 2018-02-08 09:54:59 -0800 | [diff] [blame] | 92 | "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type") |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 93 | ret += pol.AssertPathTypesDoNotHaveAttr(["/data/vendor/", "/data/vendor_ce/", |
| 94 | "/data/vendor_de/"], [], "core_data_file_type") |
| 95 | return ret |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 96 | |
Inseob Kim | 1b8b1f6 | 2020-10-23 15:16:11 +0900 | [diff] [blame] | 97 | def TestPropertyTypeViolations(pol): |
| 98 | return pol.AssertPropertyOwnersAreExclusive() |
| 99 | |
Alan Stokes | 668e74f | 2020-11-12 18:08:18 +0000 | [diff] [blame] | 100 | def TestAppDataTypeViolations(pol): |
| 101 | # Types with the app_data_file_type should only be used for app data files |
| 102 | # (/data/data/package.name etc) via seapp_contexts, and never applied |
| 103 | # explicitly to other files. |
| 104 | partitions = [ |
| 105 | "/data/", |
| 106 | "/vendor/", |
| 107 | "/odm/", |
| 108 | "/product/", |
| 109 | ] |
| 110 | exceptions = [ |
| 111 | # These are used for app data files for the corresponding user and |
| 112 | # assorted other files. |
| 113 | # TODO(b/172812577): Use different types for the different purposes |
| 114 | "shell_data_file", |
| 115 | "bluetooth_data_file", |
| 116 | "nfc_data_file", |
| 117 | "radio_data_file", |
| 118 | ] |
| 119 | return pol.AssertPathTypesDoNotHaveAttr(partitions, [], "app_data_file_type", |
| 120 | exceptions) |
Hridya Valsaraju | 8c9cf62 | 2020-12-14 22:57:49 -0800 | [diff] [blame] | 121 | def TestDmaHeapDevTypeViolations(pol): |
| 122 | return pol.AssertPathTypesHaveAttr(["/dev/dma_heap/"], [], |
| 123 | "dmabuf_heap_device_type") |
| 124 | |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 125 | def TestCoredomainViolations(test_policy): |
| 126 | # verify that all domains launched from /system have the coredomain |
| 127 | # attribute |
| 128 | ret = "" |
Alan Stokes | 668e74f | 2020-11-12 18:08:18 +0000 | [diff] [blame] | 129 | |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 130 | for d in test_policy.alldomains: |
| 131 | domain = test_policy.alldomains[d] |
| 132 | if domain.fromSystem and domain.fromVendor: |
| 133 | ret += "The following domain is system and vendor: " + d + "\n" |
| 134 | |
| 135 | for domain in test_policy.alldomains.values(): |
| 136 | ret += domain.error |
| 137 | |
| 138 | violators = [] |
| 139 | for d in test_policy.alldomains: |
| 140 | domain = test_policy.alldomains[d] |
| 141 | if domain.fromSystem and "coredomain" not in domain.attributes: |
| 142 | violators.append(d); |
| 143 | if len(violators) > 0: |
| 144 | ret += "The following domain(s) must be associated with the " |
| 145 | ret += "\"coredomain\" attribute because they are executed off of " |
| 146 | ret += "/system:\n" |
| 147 | ret += " ".join(str(x) for x in sorted(violators)) + "\n" |
| 148 | |
| 149 | # verify that all domains launched form /vendor do not have the coredomain |
| 150 | # attribute |
| 151 | violators = [] |
| 152 | for d in test_policy.alldomains: |
| 153 | domain = test_policy.alldomains[d] |
| 154 | if domain.fromVendor and "coredomain" in domain.attributes: |
| 155 | violators.append(d) |
| 156 | if len(violators) > 0: |
| 157 | ret += "The following domains must not be associated with the " |
| 158 | ret += "\"coredomain\" attribute because they are executed off of " |
| 159 | ret += "/vendor or /system/vendor:\n" |
| 160 | ret += " ".join(str(x) for x in sorted(violators)) + "\n" |
| 161 | |
| 162 | return ret |
| 163 | |
| 164 | def TestViolatorAttribute(test_policy, attribute): |
| 165 | # TODO(b/113124961): re-enable once all violator attributes are removed. |
| 166 | return "" |
| 167 | |
| 168 | # ret = "" |
| 169 | # return ret |
| 170 | |
| 171 | # violators = test_policy.DomainsWithAttribute(attribute) |
| 172 | # if len(violators) > 0: |
| 173 | # ret += "SELinux: The following domains violate the Treble ban " |
| 174 | # ret += "against use of the " + attribute + " attribute: " |
| 175 | # ret += " ".join(str(x) for x in sorted(violators)) + "\n" |
| 176 | # return ret |
| 177 | |
| 178 | def TestViolatorAttributes(test_policy): |
| 179 | ret = "" |
| 180 | ret += TestViolatorAttribute(test_policy, "socket_between_core_and_vendor_violators") |
| 181 | ret += TestViolatorAttribute(test_policy, "vendor_executes_system_violators") |
| 182 | return ret |
| 183 | |
| 184 | def TestIsolatedAttributeConsistency(test_policy): |
| 185 | permissionAllowList = { |
| 186 | # access given from technical_debt.cil |
| 187 | "codec2_config_prop" : ["file"], |
| 188 | "device_config_nnapi_native_prop":["file"], |
Sandeep Bandaru | 702797d | 2024-09-20 14:55:25 +0000 | [diff] [blame] | 189 | "gpu_device": ["dir"], |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 190 | "hal_allocator_default":["binder", "fd"], |
| 191 | "hal_codec2": ["binder", "fd"], |
| 192 | "hal_codec2_hwservice":["hwservice_manager"], |
| 193 | "hal_graphics_allocator": ["binder", "fd"], |
| 194 | "hal_graphics_allocator_service":["service_manager"], |
| 195 | "hal_graphics_allocator_hwservice":["hwservice_manager"], |
| 196 | "hal_graphics_allocator_server":["binder", "service_manager"], |
| 197 | "hal_graphics_mapper_hwservice":["hwservice_manager"], |
Jooyung Han | 952673d | 2024-01-31 09:32:48 +0900 | [diff] [blame] | 198 | "hal_graphics_mapper_service":["service_manager"], |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 199 | "hal_neuralnetworks": ["binder", "fd"], |
| 200 | "hal_neuralnetworks_service": ["service_manager"], |
| 201 | "hal_neuralnetworks_hwservice":["hwservice_manager"], |
| 202 | "hal_omx_hwservice":["hwservice_manager"], |
| 203 | "hidl_allocator_hwservice":["hwservice_manager"], |
| 204 | "hidl_manager_hwservice":["hwservice_manager"], |
| 205 | "hidl_memory_hwservice":["hwservice_manager"], |
| 206 | "hidl_token_hwservice":["hwservice_manager"], |
| 207 | "hwservicemanager":["binder"], |
| 208 | "hwservicemanager_prop":["file"], |
| 209 | "mediacodec":["binder", "fd"], |
| 210 | "mediaswcodec":["binder", "fd"], |
| 211 | "media_variant_prop":["file"], |
| 212 | "nnapi_ext_deny_product_prop":["file"], |
| 213 | "servicemanager":["fd"], |
Sandeep Bandaru | 702797d | 2024-09-20 14:55:25 +0000 | [diff] [blame] | 214 | "sysfs_gpu": ["file"], |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 215 | "toolbox_exec": ["file"], |
| 216 | # extra types being granted to isolated_compute_app |
| 217 | "isolated_compute_allowed":["service_manager", "chr_file"], |
| 218 | } |
| 219 | |
| 220 | def resolveHalServerSubtype(target): |
| 221 | # permission given as a client in technical_debt.cil |
| 222 | hal_server_attributes = [ |
| 223 | "hal_codec2_server", |
| 224 | "hal_graphics_allocator_server", |
| 225 | "hal_neuralnetworks_server"] |
| 226 | |
| 227 | for attr in hal_server_attributes: |
| 228 | if target in test_policy.pol.QueryTypeAttribute(Type=attr, IsAttr=True): |
| 229 | return attr.rsplit("_", 1)[0] |
| 230 | return target |
| 231 | |
| 232 | def checkIsolatedComputeAllowed(tctx, tclass): |
| 233 | # check if the permission is in isolated_compute_allowed |
| 234 | allowedMemberTypes = test_policy.pol.QueryTypeAttribute(Type="isolated_compute_allowed_service", IsAttr=True) \ |
| 235 | .union(test_policy.pol.QueryTypeAttribute(Type="isolated_compute_allowed_device", IsAttr=True)) |
| 236 | return tctx in allowedMemberTypes and tclass in permissionAllowList["isolated_compute_allowed"] |
| 237 | |
| 238 | def checkPermissions(permissions): |
| 239 | violated_permissions = [] |
| 240 | for perm in permissions: |
| 241 | tctx, tclass, p = perm.split(":") |
| 242 | tctx = resolveHalServerSubtype(tctx) |
| 243 | # check unwanted permissions |
| 244 | if not checkIsolatedComputeAllowed(tctx, tclass) and \ |
| 245 | ( tctx not in permissionAllowList \ |
| 246 | or tclass not in permissionAllowList[tctx] \ |
| 247 | or ( p == "write") \ |
| 248 | or ( p == "rw_file_perms") ): |
| 249 | violated_permissions += [perm] |
| 250 | return violated_permissions |
| 251 | |
| 252 | ret = "" |
| 253 | |
| 254 | isolatedMemberTypes = test_policy.pol.QueryTypeAttribute(Type="isolated_app_all", IsAttr=True) |
| 255 | baseRules = test_policy.pol.QueryExpandedTERule(scontext=["isolated_app"]) |
| 256 | basePermissionSet = set([":".join([rule.tctx, rule.tclass, perm]) |
| 257 | for rule in baseRules for perm in rule.perms]) |
| 258 | for subType in isolatedMemberTypes: |
| 259 | if subType == "isolated_app" : continue |
| 260 | currentTypeRule = test_policy.pol.QueryExpandedTERule(scontext=[subType]) |
| 261 | typePermissionSet = set([":".join([rule.tctx, rule.tclass, perm]) |
| 262 | for rule in currentTypeRule for perm in rule.perms |
| 263 | if not rule.tctx in [subType, subType + "_userfaultfd"]]) |
| 264 | deltaPermissionSet = typePermissionSet.difference(basePermissionSet) |
| 265 | violated_permissions = checkPermissions(list(deltaPermissionSet)) |
| 266 | for perm in violated_permissions: |
| 267 | ret += "allow %s %s:%s %s \n" % (subType, *perm.split(":")) |
| 268 | |
| 269 | if ret: |
| 270 | ret = ("Found prohibited permission granted for isolated like types. " + \ |
| 271 | "Please replace your allow statements that involve \"-isolated_app\" with " + \ |
| 272 | "\"-isolated_app_all\". Violations are shown as the following: \n") + ret |
| 273 | return ret |
Inseob Kim | 1b8b1f6 | 2020-10-23 15:16:11 +0900 | [diff] [blame] | 274 | |
Inseob Kim | 3a9d91c | 2023-09-27 17:39:07 +0900 | [diff] [blame] | 275 | def TestDevTypeViolations(pol): |
| 276 | exceptions = [ |
| 277 | "/dev/socket", |
| 278 | ] |
| 279 | exceptionTypes = [ |
| 280 | "boringssl_self_test_marker", # /dev/boringssl/selftest |
| 281 | "cgroup_rc_file", # /dev/cgroup.rc |
| 282 | "dev_cpu_variant", # /dev/cpu_variant:{arch} |
| 283 | "fscklogs", # /dev/fscklogs |
| 284 | "properties_serial", # /dev/__properties__/properties_serial |
| 285 | "property_info", # /dev/__properties__/property_info |
| 286 | "runtime_event_log_tags_file", # /dev/event-log-tags |
| 287 | ] |
| 288 | return pol.AssertPathTypesHaveAttr(["/dev"], exceptions, |
| 289 | "dev_type", exceptionTypes) |
| 290 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 291 | ### |
| 292 | # extend OptionParser to allow the same option flag to be used multiple times. |
| 293 | # This is used to allow multiple file_contexts files and tests to be |
| 294 | # specified. |
| 295 | # |
| 296 | class MultipleOption(Option): |
| 297 | ACTIONS = Option.ACTIONS + ("extend",) |
| 298 | STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",) |
| 299 | TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",) |
| 300 | ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",) |
| 301 | |
| 302 | def take_action(self, action, dest, opt, value, values, parser): |
| 303 | if action == "extend": |
| 304 | values.ensure_value(dest, []).append(value) |
| 305 | else: |
| 306 | Option.take_action(self, action, dest, opt, value, values, parser) |
| 307 | |
Thiébaud Weksteen | 70cf2cd | 2024-05-14 13:36:34 +1000 | [diff] [blame] | 308 | TEST_NAMES = [ name for name in dir() if name.startswith('Test') ] |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 309 | |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame] | 310 | def do_main(libpath): |
| 311 | """ |
| 312 | Args: |
| 313 | libpath: string, path to libsepolwrap.so |
| 314 | """ |
Inseob Kim | 6fa8efd | 2021-12-29 13:56:14 +0900 | [diff] [blame] | 315 | usage = "sepolicy_tests -f vendor_file_contexts -f " |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 316 | usage +="plat_file_contexts -p policy [--test test] [--help]" |
| 317 | parser = OptionParser(option_class=MultipleOption, usage=usage) |
| 318 | parser.add_option("-f", "--file_contexts", dest="file_contexts", |
| 319 | metavar="FILE", action="extend", type="string") |
| 320 | parser.add_option("-p", "--policy", dest="policy", metavar="FILE") |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 321 | parser.add_option("-t", "--test", dest="test", action="extend", |
Thiébaud Weksteen | 70cf2cd | 2024-05-14 13:36:34 +1000 | [diff] [blame] | 322 | help="Test options include "+str(TEST_NAMES)) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 323 | |
| 324 | (options, args) = parser.parse_args() |
| 325 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 326 | if not options.policy: |
| 327 | sys.exit("Must specify monolithic policy file\n" + parser.usage) |
| 328 | if not os.path.exists(options.policy): |
| 329 | sys.exit("Error: policy file " + options.policy + " does not exist\n" |
| 330 | + parser.usage) |
| 331 | |
| 332 | if not options.file_contexts: |
| 333 | sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage) |
| 334 | for f in options.file_contexts: |
| 335 | if not os.path.exists(f): |
| 336 | sys.exit("Error: File_contexts file " + f + " does not exist\n" + |
| 337 | parser.usage) |
| 338 | |
Inseob Kim | 6fa8efd | 2021-12-29 13:56:14 +0900 | [diff] [blame] | 339 | pol = policy.Policy(options.policy, options.file_contexts, libpath) |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 340 | test_policy = policy.TestPolicy() |
| 341 | test_policy.setup(pol) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 342 | |
| 343 | results = "" |
| 344 | # If an individual test is not specified, run all tests. |
Maciej Żenczykowski | b13921c | 2022-05-21 05:03:29 -0700 | [diff] [blame] | 345 | if options.test is None or "TestBpffsTypeViolations" in options.test: |
| 346 | results += TestBpffsTypeViolations(pol) |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 347 | if options.test is None or "TestDataTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 348 | results += TestDataTypeViolations(pol) |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 349 | if options.test is None or "TestProcTypeViolations" in options.test: |
| 350 | results += TestProcTypeViolations(pol) |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 351 | if options.test is None or "TestSysfsTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 352 | results += TestSysfsTypeViolations(pol) |
Nick Kralevich | dab131b | 2018-10-04 11:24:00 -0700 | [diff] [blame] | 353 | if options.test is None or "TestSystemTypeViolations" in options.test: |
| 354 | results += TestSystemTypeViolations(pol) |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 355 | if options.test is None or "TestDebugfsTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 356 | results += TestDebugfsTypeViolations(pol) |
Hridya Valsaraju | edccaa8 | 2021-05-14 14:01:11 -0700 | [diff] [blame] | 357 | if options.test is None or "TestTracefsTypeViolations" in options.test: |
| 358 | results += TestTracefsTypeViolations(pol) |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 359 | if options.test is None or "TestVendorTypeViolations" in options.test: |
| 360 | results += TestVendorTypeViolations(pol) |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 361 | if options.test is None or "TestCoreDataTypeViolations" in options.test: |
| 362 | results += TestCoreDataTypeViolations(pol) |
Inseob Kim | 1b8b1f6 | 2020-10-23 15:16:11 +0900 | [diff] [blame] | 363 | if options.test is None or "TestPropertyTypeViolations" in options.test: |
| 364 | results += TestPropertyTypeViolations(pol) |
Alan Stokes | 668e74f | 2020-11-12 18:08:18 +0000 | [diff] [blame] | 365 | if options.test is None or "TestAppDataTypeViolations" in options.test: |
| 366 | results += TestAppDataTypeViolations(pol) |
Hridya Valsaraju | 8c9cf62 | 2020-12-14 22:57:49 -0800 | [diff] [blame] | 367 | if options.test is None or "TestDmaHeapDevTypeViolations" in options.test: |
| 368 | results += TestDmaHeapDevTypeViolations(pol) |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 369 | if options.test is None or "TestCoredomainViolations" in options.test: |
| 370 | results += TestCoredomainViolations(test_policy) |
| 371 | if options.test is None or "TestViolatorAttributes" in options.test: |
| 372 | results += TestViolatorAttributes(test_policy) |
| 373 | if options.test is None or "TestIsolatedAttributeConsistency" in options.test: |
| 374 | results += TestIsolatedAttributeConsistency(test_policy) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 375 | |
Inseob Kim | 3a9d91c | 2023-09-27 17:39:07 +0900 | [diff] [blame] | 376 | # dev type test won't be run as default |
| 377 | if options.test and "TestDevTypeViolations" in options.test: |
| 378 | results += TestDevTypeViolations(pol) |
| 379 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 380 | if len(results) > 0: |
| 381 | sys.exit(results) |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame] | 382 | |
| 383 | if __name__ == '__main__': |
| 384 | temp_dir = tempfile.mkdtemp() |
| 385 | try: |
| 386 | libname = "libsepolwrap" + SHARED_LIB_EXTENSION |
| 387 | libpath = os.path.join(temp_dir, libname) |
| 388 | with open(libpath, "wb") as f: |
| 389 | blob = pkgutil.get_data("sepolicy_tests", libname) |
| 390 | if not blob: |
| 391 | sys.exit("Error: libsepolwrap does not exist. Is this binary corrupted?\n") |
| 392 | f.write(blob) |
| 393 | do_main(libpath) |
| 394 | finally: |
| 395 | shutil.rmtree(temp_dir) |