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 |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 19 | import re |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame] | 20 | import shutil |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 21 | import sys |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame] | 22 | import tempfile |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 23 | |
| 24 | ''' |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 25 | Verify that Treble compatibility are not broken. |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 26 | ''' |
ThiƩbaud Weksteen | dab3b1a | 2023-03-06 13:54:07 +1100 | [diff] [blame] | 27 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 28 | |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 29 | ############################################################# |
| 30 | # Tests |
| 31 | ############################################################# |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 32 | |
| 33 | ### |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 34 | # Make sure that any new public type introduced in the new policy that was not |
| 35 | # present in the old policy has been recorded in the mapping file. |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 36 | def TestNoUnmappedNewTypes(base_pub_policy, old_pub_policy, mapping): |
| 37 | newt = base_pub_policy.types - old_pub_policy.types |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 38 | ret = "" |
| 39 | violators = [] |
| 40 | |
| 41 | for n in newt: |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 42 | if mapping.rTypeattributesets.get(n) is None: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 43 | violators.append(n) |
| 44 | |
| 45 | if len(violators) > 0: |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 46 | ret += "SELinux: The following public types were found added to the " |
| 47 | ret += "policy without an entry into the compatibility mapping file(s) " |
Tri Vo | 438684b | 2018-09-29 17:47:10 -0700 | [diff] [blame] | 48 | ret += "found in private/compat/V.v/V.v[.ignore].cil, where V.v is the " |
| 49 | ret += "latest API level.\n" |
Tri Vo | 1451938 | 2019-01-06 18:17:32 -0800 | [diff] [blame] | 50 | ret += " ".join(str(x) for x in sorted(violators)) + "\n\n" |
| 51 | ret += "See examples of how to fix this:\n" |
Tri Vo | 462c9c4 | 2019-08-09 10:27:46 -0700 | [diff] [blame] | 52 | ret += "https://android-review.googlesource.com/c/platform/system/sepolicy/+/781036\n" |
| 53 | ret += "https://android-review.googlesource.com/c/platform/system/sepolicy/+/852612\n" |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 54 | return ret |
| 55 | |
| 56 | ### |
| 57 | # Make sure that any public type removed in the current policy has its |
| 58 | # declaration added to the mapping file for use in non-platform policy |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 59 | def TestNoUnmappedRmTypes(base_pub_policy, old_pub_policy, mapping): |
| 60 | rmt = old_pub_policy.types - base_pub_policy.types |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 61 | ret = "" |
| 62 | violators = [] |
| 63 | |
| 64 | for o in rmt: |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 65 | if o in mapping.pubtypes and not o in mapping.types: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 66 | violators.append(o) |
| 67 | |
| 68 | if len(violators) > 0: |
| 69 | ret += "SELinux: The following formerly public types were removed from " |
| 70 | ret += "policy without a declaration in the compatibility mapping " |
Tri Vo | 438684b | 2018-09-29 17:47:10 -0700 | [diff] [blame] | 71 | ret += "found in private/compat/V.v/V.v[.ignore].cil, where V.v is the " |
| 72 | ret += "latest API level.\n" |
Tri Vo | 1451938 | 2019-01-06 18:17:32 -0800 | [diff] [blame] | 73 | ret += " ".join(str(x) for x in sorted(violators)) + "\n\n" |
| 74 | ret += "See examples of how to fix this:\n" |
Tri Vo | 462c9c4 | 2019-08-09 10:27:46 -0700 | [diff] [blame] | 75 | ret += "https://android-review.googlesource.com/c/platform/system/sepolicy/+/822743\n" |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 76 | return ret |
| 77 | |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 78 | def TestTrebleCompatMapping(base_pub_policy, old_pub_policy, mapping): |
| 79 | ret = TestNoUnmappedNewTypes(base_pub_policy, old_pub_policy, mapping) |
| 80 | ret += TestNoUnmappedRmTypes(base_pub_policy, old_pub_policy, mapping) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 81 | return ret |
| 82 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 83 | ### |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 84 | # extend OptionParser to allow the same option flag to be used multiple times. |
| 85 | # This is used to allow multiple file_contexts files and tests to be |
| 86 | # specified. |
| 87 | # |
| 88 | class MultipleOption(Option): |
| 89 | ACTIONS = Option.ACTIONS + ("extend",) |
| 90 | STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",) |
| 91 | TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",) |
| 92 | ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",) |
| 93 | |
| 94 | def take_action(self, action, dest, opt, value, values, parser): |
| 95 | if action == "extend": |
| 96 | values.ensure_value(dest, []).append(value) |
| 97 | else: |
| 98 | Option.take_action(self, action, dest, opt, value, values, parser) |
| 99 | |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 100 | def do_main(): |
Inseob Kim | 6fa8efd | 2021-12-29 13:56:14 +0900 | [diff] [blame] | 101 | usage = "treble_sepolicy_tests " |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 102 | usage += "-b base_pub_policy -o old_pub_policy " |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 103 | usage += "-m mapping file [--test test] [--help]" |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 104 | parser = OptionParser(option_class=MultipleOption, usage=usage) |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 105 | parser.add_option("-b", "--base-pub-policy", dest="base_pub_policy", |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 106 | metavar="FILE") |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 107 | parser.add_option("-m", "--mapping", dest="mapping", metavar="FILE") |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 108 | parser.add_option("-o", "--old-pub-policy", dest="old_pub_policy", |
| 109 | metavar="FILE") |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 110 | |
| 111 | (options, args) = parser.parse_args() |
| 112 | |
Tri Vo | e3f4f77 | 2018-09-28 17:21:08 -0700 | [diff] [blame] | 113 | # Mapping files and public platform policy are only necessary for the |
| 114 | # TrebleCompatMapping test. |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 115 | if not options.mapping: |
| 116 | sys.exit("Must specify a compatibility mapping file\n" |
| 117 | + parser.usage) |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 118 | if not options.old_pub_policy: |
| 119 | sys.exit("Must specify the previous public policy .cil file\n" |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 120 | + parser.usage) |
| 121 | if not options.base_pub_policy: |
| 122 | sys.exit("Must specify the current platform-only public policy " |
| 123 | + ".cil file\n" + parser.usage) |
Inseob Kim | eb0d40a | 2023-09-04 19:02:53 +0900 | [diff] [blame] | 124 | mapping = mini_parser.MiniCilParser(options.mapping) |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 125 | base_pub_policy = mini_parser.MiniCilParser(options.base_pub_policy) |
| 126 | old_pub_policy = mini_parser.MiniCilParser(options.old_pub_policy) |
Jeff Vander Stoep | fe0910c | 2017-11-20 13:25:47 -0800 | [diff] [blame] | 127 | |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 128 | results = TestTrebleCompatMapping(base_pub_policy, old_pub_policy, mapping) |
Jeff Vander Stoep | bdfc030 | 2017-05-25 09:53:47 -0700 | [diff] [blame] | 129 | |
| 130 | if len(results) > 0: |
| 131 | sys.exit(results) |
Inseob Kim | 4912a24 | 2022-07-25 11:30:02 +0900 | [diff] [blame] | 132 | |
| 133 | if __name__ == '__main__': |
Inseob Kim | 0d49b9b | 2023-09-06 18:01:53 +0900 | [diff] [blame^] | 134 | do_main() |