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 | ''' |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 31 | Verify that Treble compatibility are not broken. |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 32 | ''' |
Thiébaud Weksteen | dab3b1a | 2023-03-06 13:54:07 +1100 | [diff] [blame] | 33 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 34 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 35 | ############################################################# |
| 36 | # Tests |
| 37 | ############################################################# |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 38 | |
| 39 | ### |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 40 | # Make sure that any new public type introduced in the new policy that was not |
| 41 | # 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] | 42 | def TestNoUnmappedNewTypes(test_policy): |
| 43 | newt = test_policy.alltypes - test_policy.oldalltypes |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 44 | ret = "" |
| 45 | violators = [] |
| 46 | |
| 47 | for n in newt: |
Thiébaud Weksteen | dab3b1a | 2023-03-06 13:54:07 +1100 | [diff] [blame] | 48 | 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] | 49 | violators.append(n) |
| 50 | |
| 51 | if len(violators) > 0: |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 52 | ret += "SELinux: The following public types were found added to the " |
| 53 | ret += "policy without an entry into the compatibility mapping file(s) " |
Tri Vo | 438684b | 2018-09-29 17:47:10 -0700 | [diff] [blame] | 54 | ret += "found in private/compat/V.v/V.v[.ignore].cil, where V.v is the " |
| 55 | ret += "latest API level.\n" |
Tri Vo | 1451938 | 2019-01-06 18:17:32 -0800 | [diff] [blame] | 56 | ret += " ".join(str(x) for x in sorted(violators)) + "\n\n" |
| 57 | ret += "See examples of how to fix this:\n" |
Tri Vo | 462c9c4 | 2019-08-09 10:27:46 -0700 | [diff] [blame] | 58 | ret += "https://android-review.googlesource.com/c/platform/system/sepolicy/+/781036\n" |
| 59 | ret += "https://android-review.googlesource.com/c/platform/system/sepolicy/+/852612\n" |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 60 | return ret |
| 61 | |
| 62 | ### |
| 63 | # Make sure that any public type removed in the current policy has its |
| 64 | # 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] | 65 | def TestNoUnmappedRmTypes(test_policy): |
| 66 | rmt = test_policy.oldalltypes - test_policy.alltypes |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 67 | ret = "" |
| 68 | violators = [] |
| 69 | |
| 70 | for o in rmt: |
Thiébaud Weksteen | dab3b1a | 2023-03-06 13:54:07 +1100 | [diff] [blame] | 71 | 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] | 72 | violators.append(o) |
| 73 | |
| 74 | if len(violators) > 0: |
| 75 | ret += "SELinux: The following formerly public types were removed from " |
| 76 | ret += "policy without a declaration in the compatibility mapping " |
Tri Vo | 438684b | 2018-09-29 17:47:10 -0700 | [diff] [blame] | 77 | ret += "found in private/compat/V.v/V.v[.ignore].cil, where V.v is the " |
| 78 | ret += "latest API level.\n" |
Tri Vo | 1451938 | 2019-01-06 18:17:32 -0800 | [diff] [blame] | 79 | ret += " ".join(str(x) for x in sorted(violators)) + "\n\n" |
| 80 | ret += "See examples of how to fix this:\n" |
Tri Vo | 462c9c4 | 2019-08-09 10:27:46 -0700 | [diff] [blame] | 81 | ret += "https://android-review.googlesource.com/c/platform/system/sepolicy/+/822743\n" |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 82 | return ret |
| 83 | |
Thiébaud Weksteen | dab3b1a | 2023-03-06 13:54:07 +1100 | [diff] [blame] | 84 | def TestTrebleCompatMapping(test_policy): |
| 85 | ret = TestNoUnmappedNewTypes(test_policy) |
| 86 | ret += TestNoUnmappedRmTypes(test_policy) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 87 | return ret |
| 88 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 89 | ### |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 90 | # extend OptionParser to allow the same option flag to be used multiple times. |
| 91 | # This is used to allow multiple file_contexts files and tests to be |
| 92 | # specified. |
| 93 | # |
| 94 | class MultipleOption(Option): |
| 95 | ACTIONS = Option.ACTIONS + ("extend",) |
| 96 | STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",) |
| 97 | TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",) |
| 98 | ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",) |
| 99 | |
| 100 | def take_action(self, action, dest, opt, value, values, parser): |
| 101 | if action == "extend": |
| 102 | values.ensure_value(dest, []).append(value) |
| 103 | else: |
| 104 | Option.take_action(self, action, dest, opt, value, values, parser) |
| 105 | |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame] | 106 | def do_main(libpath): |
| 107 | """ |
| 108 | Args: |
| 109 | libpath: string, path to libsepolwrap.so |
| 110 | """ |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 111 | test_policy = policy.TestPolicy() |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame] | 112 | |
Inseob Kim | 6fa8efd | 2021-12-29 13:56:14 +0900 | [diff] [blame] | 113 | usage = "treble_sepolicy_tests " |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 114 | usage += "-p curr_policy -b base_policy -o old_policy " |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 115 | usage += "-m mapping file [--test test] [--help]" |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 116 | parser = OptionParser(option_class=MultipleOption, usage=usage) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 117 | parser.add_option("-b", "--basepolicy", dest="basepolicy", metavar="FILE") |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 118 | parser.add_option("-u", "--base-pub-policy", dest="base_pub_policy", |
| 119 | metavar="FILE") |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 120 | parser.add_option("-m", "--mapping", dest="mapping", metavar="FILE") |
| 121 | parser.add_option("-o", "--oldpolicy", dest="oldpolicy", metavar="FILE") |
| 122 | parser.add_option("-p", "--policy", dest="policy", metavar="FILE") |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 123 | |
| 124 | (options, args) = parser.parse_args() |
| 125 | |
| 126 | if not options.policy: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 127 | sys.exit("Must specify current monolithic policy file\n" + parser.usage) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 128 | if not os.path.exists(options.policy): |
| 129 | sys.exit("Error: policy file " + options.policy + " does not exist\n" |
| 130 | + parser.usage) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 131 | |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 132 | # Mapping files and public platform policy are only necessary for the |
| 133 | # TrebleCompatMapping test. |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 134 | if not options.basepolicy: |
| 135 | sys.exit("Must specify the current platform-only policy file\n" |
| 136 | + parser.usage) |
| 137 | if not options.mapping: |
| 138 | sys.exit("Must specify a compatibility mapping file\n" |
| 139 | + parser.usage) |
| 140 | if not options.oldpolicy: |
| 141 | sys.exit("Must specify the previous monolithic policy file\n" |
| 142 | + parser.usage) |
| 143 | if not options.base_pub_policy: |
| 144 | sys.exit("Must specify the current platform-only public policy " |
| 145 | + ".cil file\n" + parser.usage) |
| 146 | basepol = policy.Policy(options.basepolicy, None, libpath) |
| 147 | oldpol = policy.Policy(options.oldpolicy, None, libpath) |
| 148 | mapping = mini_parser.MiniCilParser(options.mapping) |
| 149 | pubpol = mini_parser.MiniCilParser(options.base_pub_policy) |
| 150 | test_policy.compatSetup(basepol, oldpol, mapping, pubpol.types) |
Jeff Vander Stoep | fe0910c | 2017-11-20 13:25:47 -0800 | [diff] [blame] | 151 | |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 152 | pol = policy.Policy(options.policy, None, libpath) |
Thiébaud Weksteen | dab3b1a | 2023-03-06 13:54:07 +1100 | [diff] [blame] | 153 | test_policy.setup(pol) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 154 | |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 155 | if DEBUG: |
Thiébaud Weksteen | dab3b1a | 2023-03-06 13:54:07 +1100 | [diff] [blame] | 156 | test_policy.PrintScontexts() |
Jeff Vander Stoep | 1fc0682 | 2017-05-31 15:36:07 -0700 | [diff] [blame] | 157 | |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 158 | results = TestTrebleCompatMapping(test_policy) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 159 | |
| 160 | if len(results) > 0: |
| 161 | sys.exit(results) |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame] | 162 | |
| 163 | if __name__ == '__main__': |
| 164 | temp_dir = tempfile.mkdtemp() |
| 165 | try: |
| 166 | libname = "libsepolwrap" + SHARED_LIB_EXTENSION |
| 167 | libpath = os.path.join(temp_dir, libname) |
| 168 | with open(libpath, "wb") as f: |
| 169 | blob = pkgutil.get_data("treble_sepolicy_tests", libname) |
| 170 | if not blob: |
| 171 | sys.exit("Error: libsepolwrap does not exist. Is this binary corrupted?\n") |
| 172 | f.write(blob) |
| 173 | do_main(libpath) |
| 174 | finally: |
| 175 | shutil.rmtree(temp_dir) |