blob: ff1a348179fa4d63a785d0ea7ce10dbfbce5838a [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
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070019import re
Inseob Kim4912a242022-07-25 11:30:02 +090020import shutil
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070021import sys
Inseob Kim4912a242022-07-25 11:30:02 +090022import tempfile
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070023
24'''
Inseob Kimeb0d40a2023-09-04 19:02:53 +090025Verify that Treble compatibility are not broken.
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070026'''
ThiƩbaud Weksteendab3b1a2023-03-06 13:54:07 +110027
Dan Cashman91d398d2017-09-26 12:58:29 -070028
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070029#############################################################
30# Tests
31#############################################################
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070032
33###
Tri Voe3f4f772018-09-28 17:21:08 -070034# 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 Kim0d49b9b2023-09-06 18:01:53 +090036def TestNoUnmappedNewTypes(base_pub_policy, old_pub_policy, mapping):
37 newt = base_pub_policy.types - old_pub_policy.types
Dan Cashman91d398d2017-09-26 12:58:29 -070038 ret = ""
39 violators = []
40
41 for n in newt:
Inseob Kim0d49b9b2023-09-06 18:01:53 +090042 if mapping.rTypeattributesets.get(n) is None:
Dan Cashman91d398d2017-09-26 12:58:29 -070043 violators.append(n)
44
45 if len(violators) > 0:
Tri Voe3f4f772018-09-28 17:21:08 -070046 ret += "SELinux: The following public types were found added to the "
47 ret += "policy without an entry into the compatibility mapping file(s) "
Tri Vo438684b2018-09-29 17:47:10 -070048 ret += "found in private/compat/V.v/V.v[.ignore].cil, where V.v is the "
49 ret += "latest API level.\n"
Tri Vo14519382019-01-06 18:17:32 -080050 ret += " ".join(str(x) for x in sorted(violators)) + "\n\n"
51 ret += "See examples of how to fix this:\n"
Tri Vo462c9c42019-08-09 10:27:46 -070052 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 Cashman91d398d2017-09-26 12:58:29 -070054 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 Kim0d49b9b2023-09-06 18:01:53 +090059def TestNoUnmappedRmTypes(base_pub_policy, old_pub_policy, mapping):
60 rmt = old_pub_policy.types - base_pub_policy.types
Dan Cashman91d398d2017-09-26 12:58:29 -070061 ret = ""
62 violators = []
63
64 for o in rmt:
Inseob Kim0d49b9b2023-09-06 18:01:53 +090065 if o in mapping.pubtypes and not o in mapping.types:
Dan Cashman91d398d2017-09-26 12:58:29 -070066 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 Vo438684b2018-09-29 17:47:10 -070071 ret += "found in private/compat/V.v/V.v[.ignore].cil, where V.v is the "
72 ret += "latest API level.\n"
Tri Vo14519382019-01-06 18:17:32 -080073 ret += " ".join(str(x) for x in sorted(violators)) + "\n\n"
74 ret += "See examples of how to fix this:\n"
Tri Vo462c9c42019-08-09 10:27:46 -070075 ret += "https://android-review.googlesource.com/c/platform/system/sepolicy/+/822743\n"
Dan Cashman91d398d2017-09-26 12:58:29 -070076 return ret
77
Inseob Kim0d49b9b2023-09-06 18:01:53 +090078def 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 Cashman91d398d2017-09-26 12:58:29 -070081 return ret
82
Dan Cashman91d398d2017-09-26 12:58:29 -070083###
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -070084# 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#
88class 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 Kim0d49b9b2023-09-06 18:01:53 +0900100def do_main():
Inseob Kim6fa8efd2021-12-29 13:56:14 +0900101 usage = "treble_sepolicy_tests "
Inseob Kim0d49b9b2023-09-06 18:01:53 +0900102 usage += "-b base_pub_policy -o old_pub_policy "
Inseob Kimeb0d40a2023-09-04 19:02:53 +0900103 usage += "-m mapping file [--test test] [--help]"
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700104 parser = OptionParser(option_class=MultipleOption, usage=usage)
Inseob Kim0d49b9b2023-09-06 18:01:53 +0900105 parser.add_option("-b", "--base-pub-policy", dest="base_pub_policy",
Tri Voe3f4f772018-09-28 17:21:08 -0700106 metavar="FILE")
Dan Cashman91d398d2017-09-26 12:58:29 -0700107 parser.add_option("-m", "--mapping", dest="mapping", metavar="FILE")
Inseob Kim0d49b9b2023-09-06 18:01:53 +0900108 parser.add_option("-o", "--old-pub-policy", dest="old_pub_policy",
109 metavar="FILE")
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700110
111 (options, args) = parser.parse_args()
112
Tri Voe3f4f772018-09-28 17:21:08 -0700113 # Mapping files and public platform policy are only necessary for the
114 # TrebleCompatMapping test.
Inseob Kimeb0d40a2023-09-04 19:02:53 +0900115 if not options.mapping:
116 sys.exit("Must specify a compatibility mapping file\n"
117 + parser.usage)
Inseob Kim0d49b9b2023-09-06 18:01:53 +0900118 if not options.old_pub_policy:
119 sys.exit("Must specify the previous public policy .cil file\n"
Inseob Kimeb0d40a2023-09-04 19:02:53 +0900120 + 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 Kimeb0d40a2023-09-04 19:02:53 +0900124 mapping = mini_parser.MiniCilParser(options.mapping)
Inseob Kim0d49b9b2023-09-06 18:01:53 +0900125 base_pub_policy = mini_parser.MiniCilParser(options.base_pub_policy)
126 old_pub_policy = mini_parser.MiniCilParser(options.old_pub_policy)
Jeff Vander Stoepfe0910c2017-11-20 13:25:47 -0800127
Inseob Kim0d49b9b2023-09-06 18:01:53 +0900128 results = TestTrebleCompatMapping(base_pub_policy, old_pub_policy, mapping)
Jeff Vander Stoepbdfc0302017-05-25 09:53:47 -0700129
130 if len(results) > 0:
131 sys.exit(results)
Inseob Kim4912a242022-07-25 11:30:02 +0900132
133if __name__ == '__main__':
Inseob Kim0d49b9b2023-09-06 18:01:53 +0900134 do_main()