blob: c966423c9939fdd8e74977a4b8ed950fa94a7e6f [file] [log] [blame]
Thiébaud Weksteenf24b4572021-11-26 09:12:41 +11001# 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 Stoepbdfc0302017-05-25 09:53:47 -070015from optparse import OptionParser
16from optparse import Option, OptionValueError
17import os
Dan Cashman91d398d2017-09-26 12:58:29 -070018import mini_parser
Inseob Kim4912a242022-07-25 11:30:02 +090019import pkgutil
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070020import policy
Dan Cashman91d398d2017-09-26 12:58:29 -070021from policy import MatchPathPrefix
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070022import re
Inseob Kim4912a242022-07-25 11:30:02 +090023import shutil
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070024import sys
Inseob Kim4912a242022-07-25 11:30:02 +090025import tempfile
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070026
Jeff Vander Stoep1fc06822017-05-31 15:36:07 -070027DEBUG=False
Inseob Kim3a9ac6f2022-07-19 14:27:36 +090028SHARED_LIB_EXTENSION = '.dylib' if sys.platform == 'darwin' else '.so'
Jeff Vander Stoep1fc06822017-05-31 15:36:07 -070029
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070030'''
31Use file_contexts and policy to verify Treble requirements
32are not violated.
33'''
Joel Galensonb0d74a12020-07-27 09:30:34 -070034coredomainAllowlist = {
Steven Moreland000ec932020-04-02 16:20:31 -070035 # 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 Cherry9c778042018-01-25 11:31:09 -080037 'vendor_init',
Joel Galensonb0d74a12020-07-27 09:30:34 -070038 # TODO(b/152813275): need to avoid allowlist for rootdir
Steven Moreland000ec932020-04-02 16:20:31 -070039 "modprobe",
40 "slideshow",
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070041 }
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070042
43class 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 Moreland000ec932020-04-02 16:20:31 -070052 self.error = ""
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070053
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070054
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +110055class TestPolicy:
56 """A policy loaded in memory with its domains easily accessible."""
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070057
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +110058 def __init__(self):
59 self.alldomains = {}
60 self.coredomains = set()
61 self.appdomains = set()
62 self.vendordomains = set()
63 self.pol = None
Dan Cashman91d398d2017-09-26 12:58:29 -070064
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +110065 # compat vars
66 self.alltypes = set()
67 self.oldalltypes = set()
68 self.compatMapping = None
69 self.pubtypes = set()
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070070
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +110071 # Distinguish between PRODUCT_FULL_TREBLE and PRODUCT_FULL_TREBLE_OVERRIDE
72 self.FakeTreble = False
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070073
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +110074 def GetAllDomains(self):
75 for result in self.pol.QueryTypeAttribute("domain", True):
76 self.alldomains[result] = scontext()
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070077
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +110078 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 Moreland000ec932020-04-02 16:20:31 -070087
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +110088 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 Moreland000ec932020-04-02 16:20:31 -0700108
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100109 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 Moreland000ec932020-04-02 16:20:31 -0700114
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100115 # 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 Moreland000ec932020-04-02 16:20:31 -0700120
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100121 if not vendor and not system:
122 domain.error += "Unrecognized entrypoint for " + d + " at " + path + "\n"
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700123
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100124 domain.fromSystem = domain.fromSystem or system
125 domain.fromVendor = domain.fromVendor or vendor
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700126
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100127 ###
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 Cashman91d398d2017-09-26 12:58:29 -0700145
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100146 ###
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 Stoepbdfc0302017-05-25 09:53:47 -0700153
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100154 def setup(self, pol):
155 self.pol = pol
156 self.GetAllDomains()
157 self.GetAttributes()
158 self.GetDomainEntrypoints()
159 self.GetAppDomains()
160 self.GetCoreDomains()
Dan Cashman91d398d2017-09-26 12:58:29 -0700161
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100162 def GetAllTypes(self, basepol, oldpol):
163 self.alltypes = basepol.GetAllTypes(False)
164 self.oldalltypes = oldpol.GetAllTypes(False)
Dan Cashman91d398d2017-09-26 12:58:29 -0700165
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100166 # 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 Cashman91d398d2017-09-26 12:58:29 -0700194
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700195#############################################################
196# Tests
197#############################################################
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100198def TestCoredomainViolations(test_policy):
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700199 # verify that all domains launched from /system have the coredomain
200 # attribute
201 ret = ""
Steven Moreland000ec932020-04-02 16:20:31 -0700202
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100203 for d in test_policy.alldomains:
204 domain = test_policy.alldomains[d]
Steven Moreland000ec932020-04-02 16:20:31 -0700205 if domain.fromSystem and domain.fromVendor:
206 ret += "The following domain is system and vendor: " + d + "\n"
207
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100208 for domain in test_policy.alldomains.values():
Steven Moreland000ec932020-04-02 16:20:31 -0700209 ret += domain.error
210
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700211 violators = []
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100212 for d in test_policy.alldomains:
213 domain = test_policy.alldomains[d]
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700214 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 Weksteendab3b1a2023-03-06 13:54:07 +1100225 for d in test_policy.alldomains:
226 domain = test_policy.alldomains[d]
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700227 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 Voe3f4f772018-09-28 17:21:08 -0700238# 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 Weksteendab3b1a2023-03-06 13:54:07 +1100240def TestNoUnmappedNewTypes(test_policy):
241 newt = test_policy.alltypes - test_policy.oldalltypes
Dan Cashman91d398d2017-09-26 12:58:29 -0700242 ret = ""
243 violators = []
244
245 for n in newt:
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100246 if n in test_policy.pubtypes and test_policy.compatMapping.rTypeattributesets.get(n) is None:
Dan Cashman91d398d2017-09-26 12:58:29 -0700247 violators.append(n)
248
249 if len(violators) > 0:
Tri Voe3f4f772018-09-28 17:21:08 -0700250 ret += "SELinux: The following public types were found added to the "
251 ret += "policy without an entry into the compatibility mapping file(s) "
Tri Vo438684b2018-09-29 17:47:10 -0700252 ret += "found in private/compat/V.v/V.v[.ignore].cil, where V.v is the "
253 ret += "latest API level.\n"
Tri Vo14519382019-01-06 18:17:32 -0800254 ret += " ".join(str(x) for x in sorted(violators)) + "\n\n"
255 ret += "See examples of how to fix this:\n"
Tri Vo462c9c42019-08-09 10:27:46 -0700256 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 Cashman91d398d2017-09-26 12:58:29 -0700258 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 Weksteendab3b1a2023-03-06 13:54:07 +1100263def TestNoUnmappedRmTypes(test_policy):
264 rmt = test_policy.oldalltypes - test_policy.alltypes
Dan Cashman91d398d2017-09-26 12:58:29 -0700265 ret = ""
266 violators = []
267
268 for o in rmt:
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100269 if o in test_policy.compatMapping.pubtypes and not o in test_policy.compatMapping.types:
Dan Cashman91d398d2017-09-26 12:58:29 -0700270 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 Vo438684b2018-09-29 17:47:10 -0700275 ret += "found in private/compat/V.v/V.v[.ignore].cil, where V.v is the "
276 ret += "latest API level.\n"
Tri Vo14519382019-01-06 18:17:32 -0800277 ret += " ".join(str(x) for x in sorted(violators)) + "\n\n"
278 ret += "See examples of how to fix this:\n"
Tri Vo462c9c42019-08-09 10:27:46 -0700279 ret += "https://android-review.googlesource.com/c/platform/system/sepolicy/+/822743\n"
Dan Cashman91d398d2017-09-26 12:58:29 -0700280 return ret
281
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100282def TestTrebleCompatMapping(test_policy):
283 ret = TestNoUnmappedNewTypes(test_policy)
284 ret += TestNoUnmappedRmTypes(test_policy)
Dan Cashman91d398d2017-09-26 12:58:29 -0700285 return ret
286
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100287def TestViolatorAttribute(test_policy, attribute):
Dan Cashman91d398d2017-09-26 12:58:29 -0700288 ret = ""
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100289 if test_policy.FakeTreble:
Dan Cashman91d398d2017-09-26 12:58:29 -0700290 return ret
291
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100292 violators = test_policy.DomainsWithAttribute(attribute)
Dan Cashman91d398d2017-09-26 12:58:29 -0700293 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 Weksteendab3b1a2023-03-06 13:54:07 +1100299def TestViolatorAttributes(test_policy):
Steven Moreland5c0a0a82019-05-13 17:06:50 -0700300 ret = ""
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100301 ret += TestViolatorAttribute(test_policy, "socket_between_core_and_vendor_violators")
302 ret += TestViolatorAttribute(test_policy, "vendor_executes_system_violators")
Dan Cashman91d398d2017-09-26 12:58:29 -0700303 return ret
304
Jeff Vander Stoep370a52f2018-02-08 09:54:59 -0800305# TODO move this to sepolicy_tests
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100306def TestCoreDataTypeViolations(test_policy):
307 return test_policy.pol.AssertPathTypesDoNotHaveAttr(["/data/vendor/", "/data/vendor_ce/",
Jeff Vander Stoep370a52f2018-02-08 09:54:59 -0800308 "/data/vendor_de/"], [], "core_data_file_type")
309
Dan Cashman91d398d2017-09-26 12:58:29 -0700310###
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700311# 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#
315class 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 Cashman91d398d2017-09-26 12:58:29 -0700327Tests = {"CoredomainViolations": TestCoredomainViolations,
Jeff Vander Stoep370a52f2018-02-08 09:54:59 -0800328 "CoreDatatypeViolations": TestCoreDataTypeViolations,
Dan Cashman91d398d2017-09-26 12:58:29 -0700329 "TrebleCompatMapping": TestTrebleCompatMapping,
330 "ViolatorAttributes": TestViolatorAttributes}
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700331
Inseob Kim4912a242022-07-25 11:30:02 +0900332def do_main(libpath):
333 """
334 Args:
335 libpath: string, path to libsepolwrap.so
336 """
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100337 test_policy = TestPolicy()
Inseob Kim4912a242022-07-25 11:30:02 +0900338
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900339 usage = "treble_sepolicy_tests "
Dan Cashman91d398d2017-09-26 12:58:29 -0700340 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 Stoepbdfc0302017-05-25 09:53:47 -0700343 parser = OptionParser(option_class=MultipleOption, usage=usage)
Dan Cashman91d398d2017-09-26 12:58:29 -0700344 parser.add_option("-b", "--basepolicy", dest="basepolicy", metavar="FILE")
Tri Voe3f4f772018-09-28 17:21:08 -0700345 parser.add_option("-u", "--base-pub-policy", dest="base_pub_policy",
346 metavar="FILE")
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700347 parser.add_option("-f", "--file_contexts", dest="file_contexts",
348 metavar="FILE", action="extend", type="string")
Dan Cashman91d398d2017-09-26 12:58:29 -0700349 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 Stoepbdfc0302017-05-25 09:53:47 -0700353 help="Test options include "+str(Tests))
Dan Cashman91d398d2017-09-26 12:58:29 -0700354 parser.add_option("--fake-treble", action="store_true", dest="faketreble",
355 default=False)
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700356
357 (options, args) = parser.parse_args()
358
359 if not options.policy:
Dan Cashman91d398d2017-09-26 12:58:29 -0700360 sys.exit("Must specify current monolithic policy file\n" + parser.usage)
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700361 if not os.path.exists(options.policy):
362 sys.exit("Error: policy file " + options.policy + " does not exist\n"
363 + parser.usage)
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700364 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 Voe3f4f772018-09-28 17:21:08 -0700371 # Mapping files and public platform policy are only necessary for the
372 # TrebleCompatMapping test.
Thiébaud Weksteenf24b4572021-11-26 09:12:41 +1100373 if options.tests is None or options.tests == "TrebleCompatMapping":
Jeff Vander Stoepfe0910c2017-11-20 13:25:47 -0800374 if not options.basepolicy:
Tri Voe3f4f772018-09-28 17:21:08 -0700375 sys.exit("Must specify the current platform-only policy file\n"
376 + parser.usage)
Jeff Vander Stoepfe0910c2017-11-20 13:25:47 -0800377 if not options.mapping:
Tri Voe3f4f772018-09-28 17:21:08 -0700378 sys.exit("Must specify a compatibility mapping file\n"
379 + parser.usage)
Jeff Vander Stoepfe0910c2017-11-20 13:25:47 -0800380 if not options.oldpolicy:
Tri Voe3f4f772018-09-28 17:21:08 -0700381 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 Kim6fa8efd2021-12-29 13:56:14 +0900386 basepol = policy.Policy(options.basepolicy, None, libpath)
387 oldpol = policy.Policy(options.oldpolicy, None, libpath)
Jeff Vander Stoepfe0910c2017-11-20 13:25:47 -0800388 mapping = mini_parser.MiniCilParser(options.mapping)
Tri Voe3f4f772018-09-28 17:21:08 -0700389 pubpol = mini_parser.MiniCilParser(options.base_pub_policy)
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100390 test_policy.compatSetup(basepol, oldpol, mapping, pubpol.types)
Jeff Vander Stoepfe0910c2017-11-20 13:25:47 -0800391
Dan Cashman91d398d2017-09-26 12:58:29 -0700392 if options.faketreble:
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100393 test_policy.FakeTreble = True
Dan Cashman91d398d2017-09-26 12:58:29 -0700394
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900395 pol = policy.Policy(options.policy, options.file_contexts, libpath)
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100396 test_policy.setup(pol)
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700397
Jeff Vander Stoep1fc06822017-05-31 15:36:07 -0700398 if DEBUG:
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100399 test_policy.PrintScontexts()
Jeff Vander Stoep1fc06822017-05-31 15:36:07 -0700400
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700401 results = ""
402 # If an individual test is not specified, run all tests.
Dan Cashman91d398d2017-09-26 12:58:29 -0700403 if options.tests is None:
404 for t in Tests.values():
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100405 results += t(test_policy)
Dan Cashman91d398d2017-09-26 12:58:29 -0700406 else:
407 for tn in options.tests:
408 t = Tests.get(tn)
409 if t:
Thiébaud Weksteendab3b1a2023-03-06 13:54:07 +1100410 results += t(test_policy)
Dan Cashman91d398d2017-09-26 12:58:29 -0700411 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 Stoepbdfc0302017-05-25 09:53:47 -0700417
418 if len(results) > 0:
419 sys.exit(results)
Inseob Kim4912a242022-07-25 11:30:02 +0900420
421if __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)