Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 1 | from optparse import OptionParser |
| 2 | from optparse import Option, OptionValueError |
| 3 | import os |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 4 | import mini_parser |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 5 | import policy |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 6 | from policy import MatchPathPrefix |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 7 | import re |
| 8 | import sys |
| 9 | |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 10 | DEBUG=False |
| 11 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 12 | ''' |
| 13 | Use file_contexts and policy to verify Treble requirements |
| 14 | are not violated. |
| 15 | ''' |
| 16 | ### |
| 17 | # Differentiate between domains that are part of the core Android platform and |
| 18 | # domains introduced by vendors |
| 19 | coreAppdomain = { |
| 20 | 'bluetooth', |
| 21 | 'ephemeral_app', |
| 22 | 'isolated_app', |
| 23 | 'nfc', |
| 24 | 'platform_app', |
| 25 | 'priv_app', |
| 26 | 'radio', |
| 27 | 'shared_relro', |
| 28 | 'shell', |
| 29 | 'system_app', |
| 30 | 'untrusted_app', |
| 31 | 'untrusted_app_25', |
| 32 | 'untrusted_v2_app', |
| 33 | } |
| 34 | coredomainWhitelist = { |
| 35 | 'adbd', |
| 36 | 'kernel', |
| 37 | 'postinstall', |
| 38 | 'postinstall_dexopt', |
| 39 | 'recovery', |
| 40 | 'system_server', |
| 41 | } |
| 42 | coredomainWhitelist |= coreAppdomain |
| 43 | |
| 44 | class scontext: |
| 45 | def __init__(self): |
| 46 | self.fromSystem = False |
| 47 | self.fromVendor = False |
| 48 | self.coredomain = False |
| 49 | self.appdomain = False |
| 50 | self.attributes = set() |
| 51 | self.entrypoints = [] |
| 52 | self.entrypointpaths = [] |
| 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] |
| 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=" |
| 65 | if sctx.entrypointpaths is not None: |
| 66 | for path in sctx.entrypointpaths: |
| 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() |
| 73 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 74 | # compat vars |
| 75 | alltypes = set() |
| 76 | oldalltypes = set() |
| 77 | compatMapping = None |
| 78 | |
| 79 | # Distinguish between PRODUCT_FULL_TREBLE and PRODUCT_FULL_TREBLE_OVERRIDE |
| 80 | FakeTreble = False |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 81 | |
| 82 | def GetAllDomains(pol): |
| 83 | global alldomains |
| 84 | for result in pol.QueryTypeAttribute("domain", True): |
| 85 | alldomains[result] = scontext() |
| 86 | |
| 87 | def GetAppDomains(): |
| 88 | global appdomains |
| 89 | global alldomains |
| 90 | for d in alldomains: |
| 91 | # The application of the "appdomain" attribute is trusted because core |
| 92 | # selinux policy contains neverallow rules that enforce that only zygote |
| 93 | # and runas spawned processes may transition to processes that have |
| 94 | # the appdomain attribute. |
| 95 | if "appdomain" in alldomains[d].attributes: |
| 96 | alldomains[d].appdomain = True |
| 97 | appdomains.add(d) |
| 98 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 99 | def GetCoreDomains(): |
| 100 | global alldomains |
| 101 | global coredomains |
| 102 | for d in alldomains: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 103 | # TestCoredomainViolations will verify if coredomain was incorrectly |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 104 | # applied. |
| 105 | if "coredomain" in alldomains[d].attributes: |
| 106 | alldomains[d].coredomain = True |
| 107 | coredomains.add(d) |
| 108 | # check whether domains are executed off of /system or /vendor |
| 109 | if d in coredomainWhitelist: |
| 110 | continue |
| 111 | # TODO, add checks to prevent app domains from being incorrectly |
| 112 | # labeled as coredomain. Apps don't have entrypoints as they're always |
| 113 | # dynamically transitioned to by zygote. |
| 114 | if d in appdomains: |
| 115 | continue |
| 116 | if not alldomains[d].entrypointpaths: |
| 117 | continue |
| 118 | for path in alldomains[d].entrypointpaths: |
| 119 | # Processes with entrypoint on /system |
| 120 | if ((MatchPathPrefix(path, "/system") and not |
| 121 | MatchPathPrefix(path, "/system/vendor")) or |
| 122 | MatchPathPrefix(path, "/init") or |
| 123 | MatchPathPrefix(path, "/charger")): |
| 124 | alldomains[d].fromSystem = True |
| 125 | # Processes with entrypoint on /vendor or /system/vendor |
| 126 | if (MatchPathPrefix(path, "/vendor") or |
| 127 | MatchPathPrefix(path, "/system/vendor")): |
| 128 | alldomains[d].fromVendor = True |
| 129 | |
| 130 | ### |
| 131 | # Add the entrypoint type and path(s) to each domain. |
| 132 | # |
| 133 | def GetDomainEntrypoints(pol): |
| 134 | global alldomains |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 135 | for x in pol.QueryExpandedTERule(tclass=set(["file"]), perms=set(["entrypoint"])): |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 136 | if not x.sctx in alldomains: |
| 137 | continue |
| 138 | alldomains[x.sctx].entrypoints.append(str(x.tctx)) |
| 139 | # postinstall_file represents a special case specific to A/B OTAs. |
| 140 | # Update_engine mounts a partition and relabels it postinstall_file. |
| 141 | # There is no file_contexts entry associated with postinstall_file |
| 142 | # so skip the lookup. |
| 143 | if x.tctx == "postinstall_file": |
| 144 | continue |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 145 | entrypointpath = pol.QueryFc(x.tctx) |
| 146 | if not entrypointpath: |
| 147 | continue |
| 148 | alldomains[x.sctx].entrypointpaths.extend(entrypointpath) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 149 | ### |
| 150 | # Get attributes associated with each domain |
| 151 | # |
| 152 | def GetAttributes(pol): |
| 153 | global alldomains |
| 154 | for domain in alldomains: |
| 155 | for result in pol.QueryTypeAttribute(domain, False): |
| 156 | alldomains[domain].attributes.add(result) |
| 157 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 158 | def GetAllTypes(pol, oldpol): |
| 159 | global alltypes |
| 160 | global oldalltypes |
| 161 | alltypes = pol.GetAllTypes(False) |
| 162 | oldalltypes = oldpol.GetAllTypes(False) |
| 163 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 164 | def setup(pol): |
| 165 | GetAllDomains(pol) |
| 166 | GetAttributes(pol) |
| 167 | GetDomainEntrypoints(pol) |
| 168 | GetAppDomains() |
| 169 | GetCoreDomains() |
| 170 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 171 | # setup for the policy compatibility tests |
| 172 | def compatSetup(pol, oldpol, mapping): |
| 173 | global compatMapping |
| 174 | |
| 175 | GetAllTypes(pol, oldpol) |
| 176 | compatMapping = mapping |
| 177 | |
| 178 | def DomainsWithAttribute(attr): |
| 179 | global alldomains |
| 180 | domains = [] |
| 181 | for domain in alldomains: |
| 182 | if attr in alldomains[domain].attributes: |
| 183 | domains.append(domain) |
| 184 | return domains |
| 185 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 186 | ############################################################# |
| 187 | # Tests |
| 188 | ############################################################# |
| 189 | def TestCoredomainViolations(): |
| 190 | global alldomains |
| 191 | # verify that all domains launched from /system have the coredomain |
| 192 | # attribute |
| 193 | ret = "" |
| 194 | violators = [] |
| 195 | for d in alldomains: |
| 196 | domain = alldomains[d] |
| 197 | if domain.fromSystem and "coredomain" not in domain.attributes: |
| 198 | violators.append(d); |
| 199 | if len(violators) > 0: |
| 200 | ret += "The following domain(s) must be associated with the " |
| 201 | ret += "\"coredomain\" attribute because they are executed off of " |
| 202 | ret += "/system:\n" |
| 203 | ret += " ".join(str(x) for x in sorted(violators)) + "\n" |
| 204 | |
| 205 | # verify that all domains launched form /vendor do not have the coredomain |
| 206 | # attribute |
| 207 | violators = [] |
| 208 | for d in alldomains: |
| 209 | domain = alldomains[d] |
| 210 | if domain.fromVendor and "coredomain" in domain.attributes: |
| 211 | violators.append(d) |
| 212 | if len(violators) > 0: |
| 213 | ret += "The following domains must not be associated with the " |
| 214 | ret += "\"coredomain\" attribute because they are executed off of " |
| 215 | ret += "/vendor or /system/vendor:\n" |
| 216 | ret += " ".join(str(x) for x in sorted(violators)) + "\n" |
| 217 | |
| 218 | return ret |
| 219 | |
| 220 | ### |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 221 | # Make sure that any new type introduced in the new policy that was not present |
| 222 | # in the old policy has been recorded in the mapping file. |
| 223 | def TestNoUnmappedNewTypes(): |
| 224 | global alltypes |
| 225 | global oldalltypes |
| 226 | global compatMapping |
| 227 | newt = alltypes - oldalltypes |
| 228 | ret = "" |
| 229 | violators = [] |
| 230 | |
| 231 | for n in newt: |
| 232 | if compatMapping.rTypeattributesets.get(n) is None: |
| 233 | violators.append(n) |
| 234 | |
| 235 | if len(violators) > 0: |
| 236 | ret += "SELinux: The following types were found added to the policy " |
| 237 | ret += "without an entry into the compatibility mapping file(s) found " |
| 238 | ret += "in private/compat/" + compatMapping.apiLevel + "/" |
| 239 | ret += compatMapping.apiLevel + "[.ignore].cil/n" |
| 240 | ret += " ".join(str(x) for x in sorted(violators)) + "\n" |
| 241 | return ret |
| 242 | |
| 243 | ### |
| 244 | # Make sure that any public type removed in the current policy has its |
| 245 | # declaration added to the mapping file for use in non-platform policy |
| 246 | def TestNoUnmappedRmTypes(): |
| 247 | global alltypes |
| 248 | global oldalltypes |
| 249 | global compatMapping |
| 250 | rmt = oldalltypes - alltypes |
| 251 | ret = "" |
| 252 | violators = [] |
| 253 | |
| 254 | for o in rmt: |
| 255 | if o in compatMapping.pubtypes and not o in compatMapping.types: |
| 256 | violators.append(o) |
| 257 | |
| 258 | if len(violators) > 0: |
| 259 | ret += "SELinux: The following formerly public types were removed from " |
| 260 | ret += "policy without a declaration in the compatibility mapping " |
| 261 | ret += "file(s) found in prebuilts/api/" + compatMapping.apiLevel + "/\n" |
| 262 | ret += " ".join(str(x) for x in sorted(violators)) + "\n" |
| 263 | return ret |
| 264 | |
| 265 | def TestTrebleCompatMapping(): |
| 266 | ret = TestNoUnmappedNewTypes() |
| 267 | ret += TestNoUnmappedRmTypes() |
| 268 | return ret |
| 269 | |
| 270 | def TestViolatorAttribute(attribute): |
| 271 | global FakeTreble |
| 272 | ret = "" |
| 273 | if FakeTreble: |
| 274 | return ret |
| 275 | |
| 276 | violators = DomainsWithAttribute(attribute) |
| 277 | if len(violators) > 0: |
| 278 | ret += "SELinux: The following domains violate the Treble ban " |
| 279 | ret += "against use of the " + attribute + " attribute: " |
| 280 | ret += " ".join(str(x) for x in sorted(violators)) + "\n" |
| 281 | return ret |
| 282 | |
| 283 | def TestViolatorAttributes(): |
| 284 | ret = TestViolatorAttribute("binder_in_vendor_violators") |
| 285 | ret += TestViolatorAttribute("socket_between_core_and_vendor_violators") |
| 286 | ret += TestViolatorAttribute("vendor_executes_system_violators") |
| 287 | return ret |
| 288 | |
| 289 | ### |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 290 | # extend OptionParser to allow the same option flag to be used multiple times. |
| 291 | # This is used to allow multiple file_contexts files and tests to be |
| 292 | # specified. |
| 293 | # |
| 294 | class MultipleOption(Option): |
| 295 | ACTIONS = Option.ACTIONS + ("extend",) |
| 296 | STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",) |
| 297 | TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",) |
| 298 | ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",) |
| 299 | |
| 300 | def take_action(self, action, dest, opt, value, values, parser): |
| 301 | if action == "extend": |
| 302 | values.ensure_value(dest, []).append(value) |
| 303 | else: |
| 304 | Option.take_action(self, action, dest, opt, value, values, parser) |
| 305 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 306 | Tests = {"CoredomainViolations": TestCoredomainViolations, |
| 307 | "TrebleCompatMapping": TestTrebleCompatMapping, |
| 308 | "ViolatorAttributes": TestViolatorAttributes} |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 309 | |
| 310 | if __name__ == '__main__': |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 311 | usage = "treble_sepolicy_tests.py -l out/host/linux-x86/lib64 " |
| 312 | usage += "-f nonplat_file_contexts -f plat_file_contexts " |
| 313 | usage += "-p curr_policy -b base_policy -o old_policy " |
| 314 | usage +="-m mapping file [--test test] [--help]" |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 315 | parser = OptionParser(option_class=MultipleOption, usage=usage) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 316 | parser.add_option("-b", "--basepolicy", dest="basepolicy", metavar="FILE") |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 317 | parser.add_option("-f", "--file_contexts", dest="file_contexts", |
| 318 | metavar="FILE", action="extend", type="string") |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 319 | parser.add_option("-l", "--library-path", dest="libpath", metavar="FILE") |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 320 | parser.add_option("-m", "--mapping", dest="mapping", metavar="FILE") |
| 321 | parser.add_option("-o", "--oldpolicy", dest="oldpolicy", metavar="FILE") |
| 322 | parser.add_option("-p", "--policy", dest="policy", metavar="FILE") |
| 323 | parser.add_option("-t", "--test", dest="tests", action="extend", |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 324 | help="Test options include "+str(Tests)) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 325 | parser.add_option("--fake-treble", action="store_true", dest="faketreble", |
| 326 | default=False) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 327 | |
| 328 | (options, args) = parser.parse_args() |
| 329 | |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 330 | if not options.libpath: |
| 331 | sys.exit("Must specify path to host libraries\n" + parser.usage) |
| 332 | if not os.path.exists(options.libpath): |
| 333 | sys.exit("Error: library-path " + options.libpath + " does not exist\n" |
| 334 | + parser.usage) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 335 | if not options.basepolicy: |
| 336 | sys.exit("Must specify the current platform-only policy file\n" + parser.usage) |
| 337 | if not options.mapping: |
| 338 | sys.exit("Must specify a compatibility mapping file\n" + parser.usage) |
| 339 | if not options.oldpolicy: |
| 340 | sys.exit("Must specify the previous monolithic policy file\n" + parser.usage) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 341 | if not options.policy: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 342 | sys.exit("Must specify current monolithic policy file\n" + parser.usage) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 343 | if not os.path.exists(options.policy): |
| 344 | sys.exit("Error: policy file " + options.policy + " does not exist\n" |
| 345 | + parser.usage) |
| 346 | |
| 347 | if not options.file_contexts: |
| 348 | sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage) |
| 349 | for f in options.file_contexts: |
| 350 | if not os.path.exists(f): |
| 351 | sys.exit("Error: File_contexts file " + f + " does not exist\n" + |
| 352 | parser.usage) |
| 353 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 354 | if options.faketreble: |
| 355 | FakeTreble = True |
| 356 | |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 357 | pol = policy.Policy(options.policy, options.file_contexts, options.libpath) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 358 | setup(pol) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 359 | basepol = policy.Policy(options.basepolicy, None, options.libpath) |
| 360 | oldpol = policy.Policy(options.oldpolicy, None, options.libpath) |
| 361 | mapping = mini_parser.MiniCilParser(options.mapping) |
| 362 | compatSetup(basepol, oldpol, mapping) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 363 | |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 364 | if DEBUG: |
| 365 | PrintScontexts() |
| 366 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 367 | results = "" |
| 368 | # If an individual test is not specified, run all tests. |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame^] | 369 | if options.tests is None: |
| 370 | for t in Tests.values(): |
| 371 | results += t() |
| 372 | else: |
| 373 | for tn in options.tests: |
| 374 | t = Tests.get(tn) |
| 375 | if t: |
| 376 | results += t() |
| 377 | else: |
| 378 | err = "Error: unknown test: " + tn + "\n" |
| 379 | err += "Available tests:\n" |
| 380 | for tn in Tests.keys(): |
| 381 | err += tn + "\n" |
| 382 | sys.exit(err) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 383 | |
| 384 | if len(results) > 0: |
| 385 | sys.exit(results) |