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 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 15 | from optparse import OptionParser |
| 16 | from optparse import Option, OptionValueError |
| 17 | import os |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 18 | import mini_parser |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame^] | 19 | import pkgutil |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 20 | import policy |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 21 | from policy import MatchPathPrefix |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 22 | import re |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame^] | 23 | import shutil |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 24 | import sys |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame^] | 25 | import tempfile |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 26 | |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 27 | DEBUG=False |
Inseob Kim | 3a9ac6f | 2022-07-19 14:27:36 +0900 | [diff] [blame] | 28 | SHARED_LIB_EXTENSION = '.dylib' if sys.platform == 'darwin' else '.so' |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 29 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 30 | ''' |
| 31 | Use file_contexts and policy to verify Treble requirements |
| 32 | are not violated. |
| 33 | ''' |
Joel Galenson | b0d74a1 | 2020-07-27 09:30:34 -0700 | [diff] [blame] | 34 | coredomainAllowlist = { |
Steven Moreland | 000ec93 | 2020-04-02 16:20:31 -0700 | [diff] [blame] | 35 | # TODO: how do we make sure vendor_init doesn't have bad coupling with |
| 36 | # /vendor? It is the only system process which is not coredomain. |
Tom Cherry | 9c77804 | 2018-01-25 11:31:09 -0800 | [diff] [blame] | 37 | 'vendor_init', |
Joel Galenson | b0d74a1 | 2020-07-27 09:30:34 -0700 | [diff] [blame] | 38 | # TODO(b/152813275): need to avoid allowlist for rootdir |
Steven Moreland | 000ec93 | 2020-04-02 16:20:31 -0700 | [diff] [blame] | 39 | "modprobe", |
| 40 | "slideshow", |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 41 | } |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 42 | |
| 43 | class scontext: |
| 44 | def __init__(self): |
| 45 | self.fromSystem = False |
| 46 | self.fromVendor = False |
| 47 | self.coredomain = False |
| 48 | self.appdomain = False |
| 49 | self.attributes = set() |
| 50 | self.entrypoints = [] |
| 51 | self.entrypointpaths = [] |
Steven Moreland | 000ec93 | 2020-04-02 16:20:31 -0700 | [diff] [blame] | 52 | self.error = "" |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 53 | |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 54 | def PrintScontexts(): |
| 55 | for d in sorted(alldomains.keys()): |
| 56 | sctx = alldomains[d] |
Thiébaud Weksteen | f24b457 | 2021-11-26 09:12:41 +1100 | [diff] [blame] | 57 | print(d) |
| 58 | print("\tcoredomain="+str(sctx.coredomain)) |
| 59 | print("\tappdomain="+str(sctx.appdomain)) |
| 60 | print("\tfromSystem="+str(sctx.fromSystem)) |
| 61 | print("\tfromVendor="+str(sctx.fromVendor)) |
| 62 | print("\tattributes="+str(sctx.attributes)) |
| 63 | print("\tentrypoints="+str(sctx.entrypoints)) |
| 64 | print("\tentrypointpaths=") |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 65 | if sctx.entrypointpaths is not None: |
| 66 | for path in sctx.entrypointpaths: |
Thiébaud Weksteen | f24b457 | 2021-11-26 09:12:41 +1100 | [diff] [blame] | 67 | print("\t\t"+str(path)) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 68 | |
| 69 | alldomains = {} |
| 70 | coredomains = set() |
| 71 | appdomains = set() |
| 72 | vendordomains = set() |
Jeff Vander Stoep | 370a52f | 2018-02-08 09:54:59 -0800 | [diff] [blame] | 73 | pol = None |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 74 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 75 | # compat vars |
| 76 | alltypes = set() |
| 77 | oldalltypes = set() |
| 78 | compatMapping = None |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 79 | pubtypes = set() |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 80 | |
| 81 | # Distinguish between PRODUCT_FULL_TREBLE and PRODUCT_FULL_TREBLE_OVERRIDE |
| 82 | FakeTreble = False |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 83 | |
| 84 | def GetAllDomains(pol): |
| 85 | global alldomains |
| 86 | for result in pol.QueryTypeAttribute("domain", True): |
| 87 | alldomains[result] = scontext() |
| 88 | |
| 89 | def GetAppDomains(): |
| 90 | global appdomains |
| 91 | global alldomains |
| 92 | for d in alldomains: |
| 93 | # The application of the "appdomain" attribute is trusted because core |
| 94 | # selinux policy contains neverallow rules that enforce that only zygote |
| 95 | # and runas spawned processes may transition to processes that have |
| 96 | # the appdomain attribute. |
| 97 | if "appdomain" in alldomains[d].attributes: |
| 98 | alldomains[d].appdomain = True |
| 99 | appdomains.add(d) |
| 100 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 101 | def GetCoreDomains(): |
| 102 | global alldomains |
| 103 | global coredomains |
| 104 | for d in alldomains: |
Steven Moreland | 000ec93 | 2020-04-02 16:20:31 -0700 | [diff] [blame] | 105 | domain = alldomains[d] |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 106 | # TestCoredomainViolations will verify if coredomain was incorrectly |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 107 | # applied. |
Steven Moreland | 000ec93 | 2020-04-02 16:20:31 -0700 | [diff] [blame] | 108 | if "coredomain" in domain.attributes: |
| 109 | domain.coredomain = True |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 110 | coredomains.add(d) |
| 111 | # check whether domains are executed off of /system or /vendor |
Joel Galenson | b0d74a1 | 2020-07-27 09:30:34 -0700 | [diff] [blame] | 112 | if d in coredomainAllowlist: |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 113 | continue |
Steven Moreland | 000ec93 | 2020-04-02 16:20:31 -0700 | [diff] [blame] | 114 | # TODO(b/153112003): add checks to prevent app domains from being |
| 115 | # incorrectly labeled as coredomain. Apps don't have entrypoints as |
| 116 | # they're always dynamically transitioned to by zygote. |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 117 | if d in appdomains: |
| 118 | continue |
Steven Moreland | 000ec93 | 2020-04-02 16:20:31 -0700 | [diff] [blame] | 119 | # TODO(b/153112747): need to handle cases where there is a dynamic |
| 120 | # transition OR there happens to be no context in AOSP files. |
| 121 | if not domain.entrypointpaths: |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 122 | continue |
Steven Moreland | 000ec93 | 2020-04-02 16:20:31 -0700 | [diff] [blame] | 123 | |
| 124 | for path in domain.entrypointpaths: |
| 125 | vendor = any(MatchPathPrefix(path, prefix) for prefix in |
| 126 | ["/vendor", "/odm"]) |
| 127 | system = any(MatchPathPrefix(path, prefix) for prefix in |
| 128 | ["/init", "/system_ext", "/product" ]) |
| 129 | |
| 130 | # only mark entrypoint as system if it is not in legacy /system/vendor |
| 131 | if MatchPathPrefix(path, "/system/vendor"): |
| 132 | vendor = True |
| 133 | elif MatchPathPrefix(path, "/system"): |
| 134 | system = True |
| 135 | |
| 136 | if not vendor and not system: |
| 137 | domain.error += "Unrecognized entrypoint for " + d + " at " + path + "\n" |
| 138 | |
| 139 | domain.fromSystem = domain.fromSystem or system |
| 140 | domain.fromVendor = domain.fromVendor or vendor |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 141 | |
| 142 | ### |
| 143 | # Add the entrypoint type and path(s) to each domain. |
| 144 | # |
| 145 | def GetDomainEntrypoints(pol): |
| 146 | global alldomains |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 147 | for x in pol.QueryExpandedTERule(tclass=set(["file"]), perms=set(["entrypoint"])): |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 148 | if not x.sctx in alldomains: |
| 149 | continue |
| 150 | alldomains[x.sctx].entrypoints.append(str(x.tctx)) |
| 151 | # postinstall_file represents a special case specific to A/B OTAs. |
| 152 | # Update_engine mounts a partition and relabels it postinstall_file. |
| 153 | # There is no file_contexts entry associated with postinstall_file |
| 154 | # so skip the lookup. |
| 155 | if x.tctx == "postinstall_file": |
| 156 | continue |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 157 | entrypointpath = pol.QueryFc(x.tctx) |
| 158 | if not entrypointpath: |
| 159 | continue |
| 160 | alldomains[x.sctx].entrypointpaths.extend(entrypointpath) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 161 | ### |
| 162 | # Get attributes associated with each domain |
| 163 | # |
| 164 | def GetAttributes(pol): |
| 165 | global alldomains |
| 166 | for domain in alldomains: |
| 167 | for result in pol.QueryTypeAttribute(domain, False): |
| 168 | alldomains[domain].attributes.add(result) |
| 169 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 170 | def GetAllTypes(pol, oldpol): |
| 171 | global alltypes |
| 172 | global oldalltypes |
| 173 | alltypes = pol.GetAllTypes(False) |
| 174 | oldalltypes = oldpol.GetAllTypes(False) |
| 175 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 176 | def setup(pol): |
| 177 | GetAllDomains(pol) |
| 178 | GetAttributes(pol) |
| 179 | GetDomainEntrypoints(pol) |
| 180 | GetAppDomains() |
| 181 | GetCoreDomains() |
| 182 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 183 | # setup for the policy compatibility tests |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 184 | def compatSetup(pol, oldpol, mapping, types): |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 185 | global compatMapping |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 186 | global pubtypes |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 187 | |
| 188 | GetAllTypes(pol, oldpol) |
| 189 | compatMapping = mapping |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 190 | pubtypes = types |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 191 | |
| 192 | def DomainsWithAttribute(attr): |
| 193 | global alldomains |
| 194 | domains = [] |
| 195 | for domain in alldomains: |
| 196 | if attr in alldomains[domain].attributes: |
| 197 | domains.append(domain) |
| 198 | return domains |
| 199 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 200 | ############################################################# |
| 201 | # Tests |
| 202 | ############################################################# |
| 203 | def TestCoredomainViolations(): |
| 204 | global alldomains |
| 205 | # verify that all domains launched from /system have the coredomain |
| 206 | # attribute |
| 207 | ret = "" |
Steven Moreland | 000ec93 | 2020-04-02 16:20:31 -0700 | [diff] [blame] | 208 | |
| 209 | for d in alldomains: |
| 210 | domain = alldomains[d] |
| 211 | if domain.fromSystem and domain.fromVendor: |
| 212 | ret += "The following domain is system and vendor: " + d + "\n" |
| 213 | |
| 214 | for domain in alldomains.values(): |
| 215 | ret += domain.error |
| 216 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 217 | violators = [] |
| 218 | for d in alldomains: |
| 219 | domain = alldomains[d] |
| 220 | if domain.fromSystem and "coredomain" not in domain.attributes: |
| 221 | violators.append(d); |
| 222 | if len(violators) > 0: |
| 223 | ret += "The following domain(s) must be associated with the " |
| 224 | ret += "\"coredomain\" attribute because they are executed off of " |
| 225 | ret += "/system:\n" |
| 226 | ret += " ".join(str(x) for x in sorted(violators)) + "\n" |
| 227 | |
| 228 | # verify that all domains launched form /vendor do not have the coredomain |
| 229 | # attribute |
| 230 | violators = [] |
| 231 | for d in alldomains: |
| 232 | domain = alldomains[d] |
| 233 | if domain.fromVendor and "coredomain" in domain.attributes: |
| 234 | violators.append(d) |
| 235 | if len(violators) > 0: |
| 236 | ret += "The following domains must not be associated with the " |
| 237 | ret += "\"coredomain\" attribute because they are executed off of " |
| 238 | ret += "/vendor or /system/vendor:\n" |
| 239 | ret += " ".join(str(x) for x in sorted(violators)) + "\n" |
| 240 | |
| 241 | return ret |
| 242 | |
| 243 | ### |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 244 | # Make sure that any new public type introduced in the new policy that was not |
| 245 | # present in the old policy has been recorded in the mapping file. |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 246 | def TestNoUnmappedNewTypes(): |
| 247 | global alltypes |
| 248 | global oldalltypes |
| 249 | global compatMapping |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 250 | global pubtypes |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 251 | newt = alltypes - oldalltypes |
| 252 | ret = "" |
| 253 | violators = [] |
| 254 | |
| 255 | for n in newt: |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 256 | if n in pubtypes and compatMapping.rTypeattributesets.get(n) is None: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 257 | violators.append(n) |
| 258 | |
| 259 | if len(violators) > 0: |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 260 | ret += "SELinux: The following public types were found added to the " |
| 261 | ret += "policy without an entry into the compatibility mapping file(s) " |
Tri Vo | 438684b | 2018-09-29 17:47:10 -0700 | [diff] [blame] | 262 | ret += "found in private/compat/V.v/V.v[.ignore].cil, where V.v is the " |
| 263 | ret += "latest API level.\n" |
Tri Vo | 1451938 | 2019-01-06 18:17:32 -0800 | [diff] [blame] | 264 | ret += " ".join(str(x) for x in sorted(violators)) + "\n\n" |
| 265 | ret += "See examples of how to fix this:\n" |
Tri Vo | 462c9c4 | 2019-08-09 10:27:46 -0700 | [diff] [blame] | 266 | ret += "https://android-review.googlesource.com/c/platform/system/sepolicy/+/781036\n" |
| 267 | ret += "https://android-review.googlesource.com/c/platform/system/sepolicy/+/852612\n" |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 268 | return ret |
| 269 | |
| 270 | ### |
| 271 | # Make sure that any public type removed in the current policy has its |
| 272 | # declaration added to the mapping file for use in non-platform policy |
| 273 | def TestNoUnmappedRmTypes(): |
| 274 | global alltypes |
| 275 | global oldalltypes |
| 276 | global compatMapping |
| 277 | rmt = oldalltypes - alltypes |
| 278 | ret = "" |
| 279 | violators = [] |
| 280 | |
| 281 | for o in rmt: |
| 282 | if o in compatMapping.pubtypes and not o in compatMapping.types: |
| 283 | violators.append(o) |
| 284 | |
| 285 | if len(violators) > 0: |
| 286 | ret += "SELinux: The following formerly public types were removed from " |
| 287 | ret += "policy without a declaration in the compatibility mapping " |
Tri Vo | 438684b | 2018-09-29 17:47:10 -0700 | [diff] [blame] | 288 | ret += "found in private/compat/V.v/V.v[.ignore].cil, where V.v is the " |
| 289 | ret += "latest API level.\n" |
Tri Vo | 1451938 | 2019-01-06 18:17:32 -0800 | [diff] [blame] | 290 | ret += " ".join(str(x) for x in sorted(violators)) + "\n\n" |
| 291 | ret += "See examples of how to fix this:\n" |
Tri Vo | 462c9c4 | 2019-08-09 10:27:46 -0700 | [diff] [blame] | 292 | ret += "https://android-review.googlesource.com/c/platform/system/sepolicy/+/822743\n" |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 293 | return ret |
| 294 | |
| 295 | def TestTrebleCompatMapping(): |
| 296 | ret = TestNoUnmappedNewTypes() |
| 297 | ret += TestNoUnmappedRmTypes() |
| 298 | return ret |
| 299 | |
| 300 | def TestViolatorAttribute(attribute): |
| 301 | global FakeTreble |
| 302 | ret = "" |
| 303 | if FakeTreble: |
| 304 | return ret |
| 305 | |
| 306 | violators = DomainsWithAttribute(attribute) |
| 307 | if len(violators) > 0: |
| 308 | ret += "SELinux: The following domains violate the Treble ban " |
| 309 | ret += "against use of the " + attribute + " attribute: " |
| 310 | ret += " ".join(str(x) for x in sorted(violators)) + "\n" |
| 311 | return ret |
| 312 | |
| 313 | def TestViolatorAttributes(): |
Steven Moreland | 5c0a0a8 | 2019-05-13 17:06:50 -0700 | [diff] [blame] | 314 | ret = "" |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 315 | ret += TestViolatorAttribute("socket_between_core_and_vendor_violators") |
| 316 | ret += TestViolatorAttribute("vendor_executes_system_violators") |
| 317 | return ret |
| 318 | |
Jeff Vander Stoep | 370a52f | 2018-02-08 09:54:59 -0800 | [diff] [blame] | 319 | # TODO move this to sepolicy_tests |
| 320 | def TestCoreDataTypeViolations(): |
| 321 | global pol |
| 322 | return pol.AssertPathTypesDoNotHaveAttr(["/data/vendor/", "/data/vendor_ce/", |
| 323 | "/data/vendor_de/"], [], "core_data_file_type") |
| 324 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 325 | ### |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 326 | # extend OptionParser to allow the same option flag to be used multiple times. |
| 327 | # This is used to allow multiple file_contexts files and tests to be |
| 328 | # specified. |
| 329 | # |
| 330 | class MultipleOption(Option): |
| 331 | ACTIONS = Option.ACTIONS + ("extend",) |
| 332 | STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",) |
| 333 | TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",) |
| 334 | ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",) |
| 335 | |
| 336 | def take_action(self, action, dest, opt, value, values, parser): |
| 337 | if action == "extend": |
| 338 | values.ensure_value(dest, []).append(value) |
| 339 | else: |
| 340 | Option.take_action(self, action, dest, opt, value, values, parser) |
| 341 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 342 | Tests = {"CoredomainViolations": TestCoredomainViolations, |
Jeff Vander Stoep | 370a52f | 2018-02-08 09:54:59 -0800 | [diff] [blame] | 343 | "CoreDatatypeViolations": TestCoreDataTypeViolations, |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 344 | "TrebleCompatMapping": TestTrebleCompatMapping, |
| 345 | "ViolatorAttributes": TestViolatorAttributes} |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 346 | |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame^] | 347 | def do_main(libpath): |
| 348 | """ |
| 349 | Args: |
| 350 | libpath: string, path to libsepolwrap.so |
| 351 | """ |
| 352 | global pol, FakeTreble |
| 353 | |
Inseob Kim | 6fa8efd | 2021-12-29 13:56:14 +0900 | [diff] [blame] | 354 | usage = "treble_sepolicy_tests " |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 355 | usage += "-f nonplat_file_contexts -f plat_file_contexts " |
| 356 | usage += "-p curr_policy -b base_policy -o old_policy " |
| 357 | usage +="-m mapping file [--test test] [--help]" |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 358 | parser = OptionParser(option_class=MultipleOption, usage=usage) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 359 | parser.add_option("-b", "--basepolicy", dest="basepolicy", metavar="FILE") |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 360 | parser.add_option("-u", "--base-pub-policy", dest="base_pub_policy", |
| 361 | metavar="FILE") |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 362 | parser.add_option("-f", "--file_contexts", dest="file_contexts", |
| 363 | metavar="FILE", action="extend", type="string") |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 364 | parser.add_option("-m", "--mapping", dest="mapping", metavar="FILE") |
| 365 | parser.add_option("-o", "--oldpolicy", dest="oldpolicy", metavar="FILE") |
| 366 | parser.add_option("-p", "--policy", dest="policy", metavar="FILE") |
| 367 | parser.add_option("-t", "--test", dest="tests", action="extend", |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 368 | help="Test options include "+str(Tests)) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 369 | parser.add_option("--fake-treble", action="store_true", dest="faketreble", |
| 370 | default=False) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 371 | |
| 372 | (options, args) = parser.parse_args() |
| 373 | |
| 374 | if not options.policy: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 375 | sys.exit("Must specify current monolithic policy file\n" + parser.usage) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 376 | if not os.path.exists(options.policy): |
| 377 | sys.exit("Error: policy file " + options.policy + " does not exist\n" |
| 378 | + parser.usage) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 379 | if not options.file_contexts: |
| 380 | sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage) |
| 381 | for f in options.file_contexts: |
| 382 | if not os.path.exists(f): |
| 383 | sys.exit("Error: File_contexts file " + f + " does not exist\n" + |
| 384 | parser.usage) |
| 385 | |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 386 | # Mapping files and public platform policy are only necessary for the |
| 387 | # TrebleCompatMapping test. |
Thiébaud Weksteen | f24b457 | 2021-11-26 09:12:41 +1100 | [diff] [blame] | 388 | if options.tests is None or options.tests == "TrebleCompatMapping": |
Jeff Vander Stoep | fe0910c | 2017-11-20 13:25:47 -0800 | [diff] [blame] | 389 | if not options.basepolicy: |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 390 | sys.exit("Must specify the current platform-only policy file\n" |
| 391 | + parser.usage) |
Jeff Vander Stoep | fe0910c | 2017-11-20 13:25:47 -0800 | [diff] [blame] | 392 | if not options.mapping: |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 393 | sys.exit("Must specify a compatibility mapping file\n" |
| 394 | + parser.usage) |
Jeff Vander Stoep | fe0910c | 2017-11-20 13:25:47 -0800 | [diff] [blame] | 395 | if not options.oldpolicy: |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 396 | sys.exit("Must specify the previous monolithic policy file\n" |
| 397 | + parser.usage) |
| 398 | if not options.base_pub_policy: |
| 399 | sys.exit("Must specify the current platform-only public policy " |
| 400 | + ".cil file\n" + parser.usage) |
Inseob Kim | 6fa8efd | 2021-12-29 13:56:14 +0900 | [diff] [blame] | 401 | basepol = policy.Policy(options.basepolicy, None, libpath) |
| 402 | oldpol = policy.Policy(options.oldpolicy, None, libpath) |
Jeff Vander Stoep | fe0910c | 2017-11-20 13:25:47 -0800 | [diff] [blame] | 403 | mapping = mini_parser.MiniCilParser(options.mapping) |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 404 | pubpol = mini_parser.MiniCilParser(options.base_pub_policy) |
| 405 | compatSetup(basepol, oldpol, mapping, pubpol.types) |
Jeff Vander Stoep | fe0910c | 2017-11-20 13:25:47 -0800 | [diff] [blame] | 406 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 407 | if options.faketreble: |
| 408 | FakeTreble = True |
| 409 | |
Inseob Kim | 6fa8efd | 2021-12-29 13:56:14 +0900 | [diff] [blame] | 410 | pol = policy.Policy(options.policy, options.file_contexts, libpath) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 411 | setup(pol) |
| 412 | |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 413 | if DEBUG: |
| 414 | PrintScontexts() |
| 415 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 416 | results = "" |
| 417 | # If an individual test is not specified, run all tests. |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 418 | if options.tests is None: |
| 419 | for t in Tests.values(): |
| 420 | results += t() |
| 421 | else: |
| 422 | for tn in options.tests: |
| 423 | t = Tests.get(tn) |
| 424 | if t: |
| 425 | results += t() |
| 426 | else: |
| 427 | err = "Error: unknown test: " + tn + "\n" |
| 428 | err += "Available tests:\n" |
| 429 | for tn in Tests.keys(): |
| 430 | err += tn + "\n" |
| 431 | sys.exit(err) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 432 | |
| 433 | if len(results) > 0: |
| 434 | sys.exit(results) |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame^] | 435 | |
| 436 | if __name__ == '__main__': |
| 437 | temp_dir = tempfile.mkdtemp() |
| 438 | try: |
| 439 | libname = "libsepolwrap" + SHARED_LIB_EXTENSION |
| 440 | libpath = os.path.join(temp_dir, libname) |
| 441 | with open(libpath, "wb") as f: |
| 442 | blob = pkgutil.get_data("treble_sepolicy_tests", libname) |
| 443 | if not blob: |
| 444 | sys.exit("Error: libsepolwrap does not exist. Is this binary corrupted?\n") |
| 445 | f.write(blob) |
| 446 | do_main(libpath) |
| 447 | finally: |
| 448 | shutil.rmtree(temp_dir) |