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