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