Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Copyright 2022 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Inseob Kim | f87eb38 | 2022-10-19 18:32:01 +0900 | [diff] [blame^] | 17 | from pathlib import Path |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 18 | import argparse |
| 19 | import glob |
| 20 | import logging |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 21 | import mini_parser |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 22 | import os |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 23 | import policy |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 24 | import shutil |
| 25 | import subprocess |
Inseob Kim | 9eadc83 | 2022-01-25 21:58:03 +0900 | [diff] [blame] | 26 | import sys |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 27 | import tempfile |
| 28 | import zipfile |
| 29 | """This tool generates a mapping file for {ver} core sepolicy.""" |
| 30 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 31 | temp_dir = '' |
Inseob Kim | 9eadc83 | 2022-01-25 21:58:03 +0900 | [diff] [blame] | 32 | compat_cil_template = ";; This file can't be empty.\n" |
| 33 | ignore_cil_template = """;; new_objects - a collection of types that have been introduced that have no |
| 34 | ;; analogue in older policy. Thus, we do not need to map these types to |
| 35 | ;; previous ones. Add here to pass checkapi tests. |
| 36 | (type new_objects) |
| 37 | (typeattribute new_objects) |
| 38 | (typeattributeset new_objects |
| 39 | ( new_objects |
| 40 | %s |
| 41 | )) |
| 42 | """ |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 43 | |
Inseob Kim | 73172d8 | 2022-10-19 18:25:23 +0900 | [diff] [blame] | 44 | SHARED_LIB_EXTENSION = '.dylib' if sys.platform == 'darwin' else '.so' |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 45 | |
| 46 | def check_run(cmd, cwd=None): |
| 47 | if cwd: |
| 48 | logging.debug('Running cmd at %s: %s' % (cwd, cmd)) |
| 49 | else: |
| 50 | logging.debug('Running cmd: %s' % cmd) |
| 51 | subprocess.run(cmd, cwd=cwd, check=True) |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 52 | |
| 53 | |
| 54 | def check_output(cmd): |
| 55 | logging.debug('Running cmd: %s' % cmd) |
| 56 | return subprocess.run(cmd, check=True, stdout=subprocess.PIPE) |
| 57 | |
| 58 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 59 | def get_android_build_top(): |
| 60 | ANDROID_BUILD_TOP = os.getenv('ANDROID_BUILD_TOP') |
| 61 | if not ANDROID_BUILD_TOP: |
| 62 | sys.exit( |
| 63 | 'Error: Missing ANDROID_BUILD_TOP env variable. Please run ' |
| 64 | '\'. build/envsetup.sh; lunch <build target>\'. Exiting script.') |
| 65 | return ANDROID_BUILD_TOP |
| 66 | |
| 67 | |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 68 | def fetch_artifact(branch, build, pattern, destination='.'): |
| 69 | """Fetches build artifacts from Android Build server. |
| 70 | |
| 71 | Args: |
| 72 | branch: string, branch to pull build artifacts from |
| 73 | build: string, build ID or "latest" |
| 74 | pattern: string, pattern of build artifact file name |
| 75 | destination: string, destination to pull build artifact to |
| 76 | """ |
| 77 | fetch_artifact_path = '/google/data/ro/projects/android/fetch_artifact' |
| 78 | cmd = [ |
| 79 | fetch_artifact_path, '--branch', branch, '--target', |
| 80 | 'aosp_arm64-userdebug' |
| 81 | ] |
| 82 | if build == 'latest': |
| 83 | cmd.append('--latest') |
| 84 | else: |
| 85 | cmd.extend(['--bid', build]) |
| 86 | cmd.extend([pattern, destination]) |
| 87 | check_run(cmd) |
| 88 | |
| 89 | |
| 90 | def extract_mapping_file_from_img(img_path, ver, destination='.'): |
| 91 | """ Extracts system/etc/selinux/mapping/{ver}.cil from system.img file. |
| 92 | |
| 93 | Args: |
| 94 | img_path: string, path to system.img file |
| 95 | ver: string, version of designated mapping file |
| 96 | destination: string, destination to pull the mapping file to |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 97 | |
| 98 | Returns: |
| 99 | string, path to extracted mapping file |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 100 | """ |
| 101 | |
| 102 | cmd = [ |
| 103 | 'debugfs', '-R', |
Inseob Kim | 9eadc83 | 2022-01-25 21:58:03 +0900 | [diff] [blame] | 104 | 'cat system/etc/selinux/mapping/10000.0.cil', img_path |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 105 | ] |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 106 | path = os.path.join(destination, '%s.cil' % ver) |
| 107 | with open(path, 'wb') as f: |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 108 | logging.debug('Extracting %s.cil to %s' % (ver, destination)) |
Inseob Kim | bf2a967 | 2022-10-19 18:31:07 +0900 | [diff] [blame] | 109 | f.write(check_output(cmd).stdout.replace(b'10000_0', ver.replace('.', '_').encode())) |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 110 | return path |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 111 | |
| 112 | |
| 113 | def download_mapping_file(branch, build, ver, destination='.'): |
| 114 | """ Downloads system/etc/selinux/mapping/{ver}.cil from Android Build server. |
| 115 | |
| 116 | Args: |
| 117 | branch: string, branch to pull build artifacts from (e.g. "sc-v2-dev") |
| 118 | build: string, build ID or "latest" |
| 119 | ver: string, version of designated mapping file (e.g. "32.0") |
| 120 | destination: string, destination to pull build artifact to |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 121 | |
| 122 | Returns: |
| 123 | string, path to extracted mapping file |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 124 | """ |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 125 | logging.info('Downloading %s mapping file from branch %s build %s...' % |
| 126 | (ver, branch, build)) |
| 127 | artifact_pattern = 'aosp_arm64-img-*.zip' |
| 128 | fetch_artifact(branch, build, artifact_pattern, temp_dir) |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 129 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 130 | # glob must succeed |
| 131 | zip_path = glob.glob(os.path.join(temp_dir, artifact_pattern))[0] |
| 132 | with zipfile.ZipFile(zip_path) as zip_file: |
| 133 | logging.debug('Extracting system.img to %s' % temp_dir) |
| 134 | zip_file.extract('system.img', temp_dir) |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 135 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 136 | system_img_path = os.path.join(temp_dir, 'system.img') |
| 137 | return extract_mapping_file_from_img(system_img_path, ver, destination) |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 138 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 139 | |
| 140 | def build_base_files(target_version): |
| 141 | """ Builds needed base policy files from the source code. |
| 142 | |
| 143 | Args: |
| 144 | target_version: string, target version to gerenate the mapping file |
| 145 | |
| 146 | Returns: |
| 147 | (string, string, string), paths to base policy, old policy, and pub policy |
| 148 | cil |
| 149 | """ |
| 150 | logging.info('building base sepolicy files') |
| 151 | build_top = get_android_build_top() |
| 152 | |
| 153 | cmd = [ |
| 154 | 'build/soong/soong_ui.bash', |
| 155 | '--make-mode', |
| 156 | 'dist', |
| 157 | 'base-sepolicy-files-for-mapping', |
| 158 | 'TARGET_PRODUCT=aosp_arm64', |
| 159 | 'TARGET_BUILD_VARIANT=userdebug', |
| 160 | ] |
| 161 | check_run(cmd, cwd=build_top) |
| 162 | |
| 163 | dist_dir = os.path.join(build_top, 'out', 'dist') |
| 164 | base_policy_path = os.path.join(dist_dir, 'base_plat_sepolicy') |
| 165 | old_policy_path = os.path.join(dist_dir, |
| 166 | '%s_plat_sepolicy' % target_version) |
| 167 | pub_policy_cil_path = os.path.join(dist_dir, 'base_plat_pub_policy.cil') |
| 168 | |
| 169 | return base_policy_path, old_policy_path, pub_policy_cil_path |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 170 | |
| 171 | |
Inseob Kim | 9eadc83 | 2022-01-25 21:58:03 +0900 | [diff] [blame] | 172 | def change_api_level(versioned_type, api_from, api_to): |
| 173 | """ Verifies the API version of versioned_type, and changes it to new API level. |
| 174 | |
| 175 | For example, change_api_level("foo_32_0", "32.0", "31.0") will return |
| 176 | "foo_31_0". |
| 177 | |
| 178 | Args: |
| 179 | versioned_type: string, type with version suffix |
| 180 | api_from: string, api version of versioned_type |
| 181 | api_to: string, new api version for versioned_type |
| 182 | |
| 183 | Returns: |
| 184 | string, a new versioned type |
| 185 | """ |
| 186 | old_suffix = api_from.replace('.', '_') |
| 187 | new_suffix = api_to.replace('.', '_') |
| 188 | if not versioned_type.endswith(old_suffix): |
| 189 | raise ValueError('Version of type %s is different from %s' % |
| 190 | (versioned_type, api_from)) |
| 191 | return versioned_type.removesuffix(old_suffix) + new_suffix |
| 192 | |
| 193 | |
Inseob Kim | f87eb38 | 2022-10-19 18:32:01 +0900 | [diff] [blame^] | 194 | def create_target_compat_modules(bp_path, target_ver): |
| 195 | """ Creates compat modules to Android.bp. |
| 196 | |
| 197 | Args: |
| 198 | bp_path: string, path to Android.bp |
| 199 | target_ver: string, api version to generate |
| 200 | """ |
| 201 | |
| 202 | module_template = """ |
| 203 | se_build_files {{ |
| 204 | name: "{ver}.board.compat.map", |
| 205 | srcs: ["compat/{ver}/{ver}.cil"], |
| 206 | }} |
| 207 | |
| 208 | se_build_files {{ |
| 209 | name: "{ver}.board.compat.cil", |
| 210 | srcs: ["compat/{ver}/{ver}.compat.cil"], |
| 211 | }} |
| 212 | |
| 213 | se_build_files {{ |
| 214 | name: "{ver}.board.ignore.map", |
| 215 | srcs: ["compat/{ver}/{ver}.ignore.cil"], |
| 216 | }} |
| 217 | |
| 218 | se_cil_compat_map {{ |
| 219 | name: "plat_{ver}.cil", |
| 220 | stem: "{ver}.cil", |
| 221 | bottom_half: [":{ver}.board.compat.map{{.plat_private}}"], |
| 222 | }} |
| 223 | |
| 224 | se_cil_compat_map {{ |
| 225 | name: "system_ext_{ver}.cil", |
| 226 | stem: "{ver}.cil", |
| 227 | bottom_half: [":{ver}.board.compat.map{{.system_ext_private}}"], |
| 228 | system_ext_specific: true, |
| 229 | }} |
| 230 | |
| 231 | se_cil_compat_map {{ |
| 232 | name: "product_{ver}.cil", |
| 233 | stem: "{ver}.cil", |
| 234 | bottom_half: [":{ver}.board.compat.map{{.product_private}}"], |
| 235 | product_specific: true, |
| 236 | }} |
| 237 | |
| 238 | se_cil_compat_map {{ |
| 239 | name: "{ver}.ignore.cil", |
| 240 | bottom_half: [":{ver}.board.ignore.map{{.plat_private}}"], |
| 241 | }} |
| 242 | |
| 243 | se_cil_compat_map {{ |
| 244 | name: "system_ext_{ver}.ignore.cil", |
| 245 | stem: "{ver}.ignore.cil", |
| 246 | bottom_half: [":{ver}.board.ignore.map{{.system_ext_private}}"], |
| 247 | system_ext_specific: true, |
| 248 | }} |
| 249 | |
| 250 | se_cil_compat_map {{ |
| 251 | name: "product_{ver}.ignore.cil", |
| 252 | stem: "{ver}.ignore.cil", |
| 253 | bottom_half: [":{ver}.board.ignore.map{{.product_private}}"], |
| 254 | product_specific: true, |
| 255 | }} |
| 256 | |
| 257 | se_compat_cil {{ |
| 258 | name: "{ver}.compat.cil", |
| 259 | srcs: [":{ver}.board.compat.cil{{.plat_private}}"], |
| 260 | }} |
| 261 | |
| 262 | se_compat_cil {{ |
| 263 | name: "system_ext_{ver}.compat.cil", |
| 264 | stem: "{ver}.compat.cil", |
| 265 | srcs: [":{ver}.board.compat.cil{{.system_ext_private}}"], |
| 266 | system_ext_specific: true, |
| 267 | }} |
| 268 | """ |
| 269 | |
| 270 | with open(bp_path, 'a') as f: |
| 271 | f.write(module_template.format(ver=target_ver)) |
| 272 | |
| 273 | |
| 274 | def patch_top_half_of_latest_compat_modules(bp_path, latest_ver, target_ver): |
| 275 | """ Adds top_half property to latest compat modules in Android.bp. |
| 276 | |
| 277 | Args: |
| 278 | bp_path: string, path to Android.bp |
| 279 | latest_ver: string, previous api version |
| 280 | target_ver: string, api version to generate |
| 281 | """ |
| 282 | |
| 283 | modules_to_patch = [ |
| 284 | "plat_{ver}.cil", |
| 285 | "system_ext_{ver}.cil", |
| 286 | "product_{ver}.cil", |
| 287 | "{ver}.ignore.cil", |
| 288 | "system_ext_{ver}.ignore.cil", |
| 289 | "product_{ver}.ignore.cil", |
| 290 | ] |
| 291 | |
| 292 | for module in modules_to_patch: |
| 293 | # set latest_ver module's top_half property to target_ver |
| 294 | # e.g. |
| 295 | # |
| 296 | # se_cil_compat_map { |
| 297 | # name: "plat_33.0.cil", |
| 298 | # top_half: "plat_34.0.cil", <== this |
| 299 | # ... |
| 300 | # } |
| 301 | check_run([ |
| 302 | "bpmodify", |
| 303 | "-m", module.format(ver=latest_ver), |
| 304 | "-property", "top_half", |
| 305 | "-str", module.format(ver=target_ver), |
| 306 | "-w", |
| 307 | bp_path |
| 308 | ]) |
| 309 | |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 310 | def get_args(): |
| 311 | parser = argparse.ArgumentParser() |
| 312 | parser.add_argument( |
| 313 | '--branch', |
| 314 | required=True, |
| 315 | help='Branch to pull build from. e.g. "sc-v2-dev"') |
| 316 | parser.add_argument('--build', required=True, help='Build ID, or "latest"') |
| 317 | parser.add_argument( |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 318 | '--target-version', |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 319 | required=True, |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 320 | help='Target version of designated mapping file. e.g. "32.0"') |
| 321 | parser.add_argument( |
| 322 | '--latest-version', |
| 323 | required=True, |
| 324 | help='Latest version for mapping of newer types. e.g. "31.0"') |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 325 | parser.add_argument( |
| 326 | '-v', |
| 327 | '--verbose', |
| 328 | action='count', |
| 329 | default=0, |
| 330 | help='Increase output verbosity, e.g. "-v", "-vv".') |
| 331 | return parser.parse_args() |
| 332 | |
| 333 | |
| 334 | def main(): |
| 335 | args = get_args() |
| 336 | |
| 337 | verbosity = min(args.verbose, 2) |
| 338 | logging.basicConfig( |
| 339 | format='%(levelname)-8s [%(filename)s:%(lineno)d] %(message)s', |
| 340 | level=(logging.WARNING, logging.INFO, logging.DEBUG)[verbosity]) |
| 341 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 342 | global temp_dir |
| 343 | temp_dir = tempfile.mkdtemp() |
| 344 | |
| 345 | try: |
| 346 | libpath = os.path.join( |
Inseob Kim | 73172d8 | 2022-10-19 18:25:23 +0900 | [diff] [blame] | 347 | os.path.dirname(os.path.realpath(__file__)), 'libsepolwrap' + SHARED_LIB_EXTENSION) |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 348 | if not os.path.exists(libpath): |
| 349 | sys.exit( |
| 350 | 'Error: libsepolwrap does not exist. Is this binary corrupted?\n' |
| 351 | ) |
| 352 | |
| 353 | build_top = get_android_build_top() |
| 354 | sepolicy_path = os.path.join(build_top, 'system', 'sepolicy') |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 355 | |
Inseob Kim | f87eb38 | 2022-10-19 18:32:01 +0900 | [diff] [blame^] | 356 | # Step 0. Create a placeholder files and compat modules |
| 357 | # These are needed to build base policy files below. |
| 358 | compat_bp_path = os.path.join(sepolicy_path, 'compat', 'Android.bp') |
| 359 | create_target_compat_modules(compat_bp_path, args.target_version) |
| 360 | patch_top_half_of_latest_compat_modules(compat_bp_path, args.latest_version, |
| 361 | args.target_version) |
| 362 | |
| 363 | target_compat_path = os.path.join(sepolicy_path, 'private', 'compat', |
| 364 | args.target_version) |
| 365 | target_mapping_file = os.path.join(target_compat_path, |
| 366 | args.target_version + '.cil') |
| 367 | target_compat_file = os.path.join(target_compat_path, |
| 368 | args.target_version + '.compat.cil') |
| 369 | target_ignore_file = os.path.join(target_compat_path, |
| 370 | args.target_version + '.ignore.cil') |
| 371 | Path(target_compat_path).mkdir(parents=True, exist_ok=True) |
| 372 | Path(target_mapping_file).touch() |
| 373 | Path(target_compat_file).touch() |
| 374 | Path(target_ignore_file).touch() |
| 375 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 376 | # Step 1. Download system/etc/selinux/mapping/{ver}.cil, and remove types/typeattributes |
Inseob Kim | 9eadc83 | 2022-01-25 21:58:03 +0900 | [diff] [blame] | 377 | mapping_file = download_mapping_file( |
| 378 | args.branch, args.build, args.target_version, destination=temp_dir) |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 379 | mapping_file_cil = mini_parser.MiniCilParser(mapping_file) |
| 380 | mapping_file_cil.types = set() |
| 381 | mapping_file_cil.typeattributes = set() |
| 382 | |
| 383 | # Step 2. Build base policy files and parse latest mapping files |
| 384 | base_policy_path, old_policy_path, pub_policy_cil_path = build_base_files( |
| 385 | args.target_version) |
| 386 | base_policy = policy.Policy(base_policy_path, None, libpath) |
| 387 | old_policy = policy.Policy(old_policy_path, None, libpath) |
| 388 | pub_policy_cil = mini_parser.MiniCilParser(pub_policy_cil_path) |
| 389 | |
| 390 | all_types = base_policy.GetAllTypes(False) |
| 391 | old_all_types = old_policy.GetAllTypes(False) |
| 392 | pub_types = pub_policy_cil.types |
| 393 | |
| 394 | # Step 3. Find new types and removed types |
| 395 | new_types = pub_types & (all_types - old_all_types) |
| 396 | removed_types = (mapping_file_cil.pubtypes - mapping_file_cil.types) & ( |
| 397 | old_all_types - all_types) |
| 398 | |
| 399 | logging.info('new types: %s' % new_types) |
| 400 | logging.info('removed types: %s' % removed_types) |
| 401 | |
Inseob Kim | 9eadc83 | 2022-01-25 21:58:03 +0900 | [diff] [blame] | 402 | # Step 4. Map new types and removed types appropriately, based on the latest mapping |
| 403 | latest_compat_path = os.path.join(sepolicy_path, 'private', 'compat', |
| 404 | args.latest_version) |
| 405 | latest_mapping_cil = mini_parser.MiniCilParser( |
| 406 | os.path.join(latest_compat_path, args.latest_version + '.cil')) |
| 407 | latest_ignore_cil = mini_parser.MiniCilParser( |
| 408 | os.path.join(latest_compat_path, |
| 409 | args.latest_version + '.ignore.cil')) |
| 410 | |
| 411 | latest_ignored_types = list(latest_ignore_cil.rTypeattributesets.keys()) |
| 412 | latest_removed_types = latest_mapping_cil.types |
| 413 | logging.debug('types ignored in latest policy: %s' % |
| 414 | latest_ignored_types) |
| 415 | logging.debug('types removed in latest policy: %s' % |
| 416 | latest_removed_types) |
| 417 | |
| 418 | target_ignored_types = set() |
| 419 | target_removed_types = set() |
| 420 | invalid_new_types = set() |
| 421 | invalid_mapping_types = set() |
| 422 | invalid_removed_types = set() |
| 423 | |
| 424 | logging.info('starting mapping') |
| 425 | for new_type in new_types: |
| 426 | # Either each new type should be in latest_ignore_cil, or mapped to existing types |
| 427 | if new_type in latest_ignored_types: |
| 428 | logging.debug('adding %s to ignore' % new_type) |
| 429 | target_ignored_types.add(new_type) |
| 430 | elif new_type in latest_mapping_cil.rTypeattributesets: |
| 431 | latest_mapped_types = latest_mapping_cil.rTypeattributesets[ |
| 432 | new_type] |
| 433 | target_mapped_types = {change_api_level(t, args.latest_version, |
| 434 | args.target_version) |
| 435 | for t in latest_mapped_types} |
| 436 | logging.debug('mapping %s to %s' % |
| 437 | (new_type, target_mapped_types)) |
| 438 | |
| 439 | for t in target_mapped_types: |
| 440 | if t not in mapping_file_cil.typeattributesets: |
| 441 | logging.error( |
| 442 | 'Cannot find desired type %s in mapping file' % t) |
| 443 | invalid_mapping_types.add(t) |
| 444 | continue |
| 445 | mapping_file_cil.typeattributesets[t].add(new_type) |
| 446 | else: |
| 447 | logging.error('no mapping information for new type %s' % |
| 448 | new_type) |
| 449 | invalid_new_types.add(new_type) |
| 450 | |
| 451 | for removed_type in removed_types: |
| 452 | # Removed type should be in latest_mapping_cil |
| 453 | if removed_type in latest_removed_types: |
| 454 | logging.debug('adding %s to removed' % removed_type) |
| 455 | target_removed_types.add(removed_type) |
| 456 | else: |
| 457 | logging.error('no mapping information for removed type %s' % |
| 458 | removed_type) |
| 459 | invalid_removed_types.add(removed_type) |
| 460 | |
| 461 | error_msg = '' |
| 462 | |
| 463 | if invalid_new_types: |
| 464 | error_msg += ('The following new types were not in the latest ' |
| 465 | 'mapping: %s\n') % sorted(invalid_new_types) |
| 466 | if invalid_mapping_types: |
| 467 | error_msg += ( |
| 468 | 'The following existing types were not in the ' |
| 469 | 'downloaded mapping file: %s\n') % sorted(invalid_mapping_types) |
| 470 | if invalid_removed_types: |
| 471 | error_msg += ('The following removed types were not in the latest ' |
| 472 | 'mapping: %s\n') % sorted(invalid_removed_types) |
| 473 | |
| 474 | if error_msg: |
| 475 | error_msg += '\n' |
| 476 | error_msg += ('Please make sure the source tree and the build ID is' |
| 477 | ' up to date.\n') |
| 478 | sys.exit(error_msg) |
| 479 | |
| 480 | # Step 5. Write to system/sepolicy/private/compat |
Inseob Kim | 9eadc83 | 2022-01-25 21:58:03 +0900 | [diff] [blame] | 481 | with open(target_mapping_file, 'w') as f: |
| 482 | logging.info('writing %s' % target_mapping_file) |
| 483 | if removed_types: |
| 484 | f.write(';; types removed from current policy\n') |
| 485 | f.write('\n'.join(f'(type {x})' for x in sorted(target_removed_types))) |
| 486 | f.write('\n\n') |
| 487 | f.write(mapping_file_cil.unparse()) |
| 488 | |
| 489 | with open(target_compat_file, 'w') as f: |
| 490 | logging.info('writing %s' % target_compat_file) |
| 491 | f.write(compat_cil_template) |
| 492 | |
| 493 | with open(target_ignore_file, 'w') as f: |
| 494 | logging.info('writing %s' % target_ignore_file) |
| 495 | f.write(ignore_cil_template % |
| 496 | ('\n '.join(sorted(target_ignored_types)))) |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 497 | finally: |
| 498 | logging.info('Deleting temporary dir: {}'.format(temp_dir)) |
| 499 | shutil.rmtree(temp_dir) |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 500 | |
| 501 | |
| 502 | if __name__ == '__main__': |
| 503 | main() |