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 | |
| 17 | import argparse |
| 18 | import glob |
| 19 | import logging |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 20 | import mini_parser |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 21 | import os |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 22 | import policy |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 23 | import shutil |
| 24 | import subprocess |
Inseob Kim | 9eadc83 | 2022-01-25 21:58:03 +0900 | [diff] [blame] | 25 | import sys |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 26 | import tempfile |
| 27 | import zipfile |
| 28 | """This tool generates a mapping file for {ver} core sepolicy.""" |
| 29 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 30 | temp_dir = '' |
Inseob Kim | 9eadc83 | 2022-01-25 21:58:03 +0900 | [diff] [blame] | 31 | compat_cil_template = ";; This file can't be empty.\n" |
| 32 | ignore_cil_template = """;; new_objects - a collection of types that have been introduced that have no |
| 33 | ;; analogue in older policy. Thus, we do not need to map these types to |
| 34 | ;; previous ones. Add here to pass checkapi tests. |
| 35 | (type new_objects) |
| 36 | (typeattribute new_objects) |
| 37 | (typeattributeset new_objects |
| 38 | ( new_objects |
| 39 | %s |
| 40 | )) |
| 41 | """ |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 42 | |
Inseob Kim | 73172d8 | 2022-10-19 18:25:23 +0900 | [diff] [blame] | 43 | SHARED_LIB_EXTENSION = '.dylib' if sys.platform == 'darwin' else '.so' |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 44 | |
| 45 | def check_run(cmd, cwd=None): |
| 46 | if cwd: |
| 47 | logging.debug('Running cmd at %s: %s' % (cwd, cmd)) |
| 48 | else: |
| 49 | logging.debug('Running cmd: %s' % cmd) |
| 50 | subprocess.run(cmd, cwd=cwd, check=True) |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 51 | |
| 52 | |
| 53 | def check_output(cmd): |
| 54 | logging.debug('Running cmd: %s' % cmd) |
| 55 | return subprocess.run(cmd, check=True, stdout=subprocess.PIPE) |
| 56 | |
| 57 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 58 | def get_android_build_top(): |
| 59 | ANDROID_BUILD_TOP = os.getenv('ANDROID_BUILD_TOP') |
| 60 | if not ANDROID_BUILD_TOP: |
| 61 | sys.exit( |
| 62 | 'Error: Missing ANDROID_BUILD_TOP env variable. Please run ' |
| 63 | '\'. build/envsetup.sh; lunch <build target>\'. Exiting script.') |
| 64 | return ANDROID_BUILD_TOP |
| 65 | |
| 66 | |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 67 | def fetch_artifact(branch, build, pattern, destination='.'): |
| 68 | """Fetches build artifacts from Android Build server. |
| 69 | |
| 70 | Args: |
| 71 | branch: string, branch to pull build artifacts from |
| 72 | build: string, build ID or "latest" |
| 73 | pattern: string, pattern of build artifact file name |
| 74 | destination: string, destination to pull build artifact to |
| 75 | """ |
| 76 | fetch_artifact_path = '/google/data/ro/projects/android/fetch_artifact' |
| 77 | cmd = [ |
| 78 | fetch_artifact_path, '--branch', branch, '--target', |
| 79 | 'aosp_arm64-userdebug' |
| 80 | ] |
| 81 | if build == 'latest': |
| 82 | cmd.append('--latest') |
| 83 | else: |
| 84 | cmd.extend(['--bid', build]) |
| 85 | cmd.extend([pattern, destination]) |
| 86 | check_run(cmd) |
| 87 | |
| 88 | |
| 89 | def extract_mapping_file_from_img(img_path, ver, destination='.'): |
| 90 | """ Extracts system/etc/selinux/mapping/{ver}.cil from system.img file. |
| 91 | |
| 92 | Args: |
| 93 | img_path: string, path to system.img file |
| 94 | ver: string, version of designated mapping file |
| 95 | destination: string, destination to pull the mapping file to |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 96 | |
| 97 | Returns: |
| 98 | string, path to extracted mapping file |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 99 | """ |
| 100 | |
| 101 | cmd = [ |
| 102 | 'debugfs', '-R', |
Inseob Kim | 9eadc83 | 2022-01-25 21:58:03 +0900 | [diff] [blame] | 103 | 'cat system/etc/selinux/mapping/10000.0.cil', img_path |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 104 | ] |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 105 | path = os.path.join(destination, '%s.cil' % ver) |
| 106 | with open(path, 'wb') as f: |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 107 | logging.debug('Extracting %s.cil to %s' % (ver, destination)) |
Inseob Kim | bf2a967 | 2022-10-19 18:31:07 +0900 | [diff] [blame^] | 108 | 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] | 109 | return path |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 110 | |
| 111 | |
| 112 | def download_mapping_file(branch, build, ver, destination='.'): |
| 113 | """ Downloads system/etc/selinux/mapping/{ver}.cil from Android Build server. |
| 114 | |
| 115 | Args: |
| 116 | branch: string, branch to pull build artifacts from (e.g. "sc-v2-dev") |
| 117 | build: string, build ID or "latest" |
| 118 | ver: string, version of designated mapping file (e.g. "32.0") |
| 119 | destination: string, destination to pull build artifact to |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 120 | |
| 121 | Returns: |
| 122 | string, path to extracted mapping file |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 123 | """ |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 124 | logging.info('Downloading %s mapping file from branch %s build %s...' % |
| 125 | (ver, branch, build)) |
| 126 | artifact_pattern = 'aosp_arm64-img-*.zip' |
| 127 | fetch_artifact(branch, build, artifact_pattern, temp_dir) |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 128 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 129 | # glob must succeed |
| 130 | zip_path = glob.glob(os.path.join(temp_dir, artifact_pattern))[0] |
| 131 | with zipfile.ZipFile(zip_path) as zip_file: |
| 132 | logging.debug('Extracting system.img to %s' % temp_dir) |
| 133 | zip_file.extract('system.img', temp_dir) |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 134 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 135 | system_img_path = os.path.join(temp_dir, 'system.img') |
| 136 | return extract_mapping_file_from_img(system_img_path, ver, destination) |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 137 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 138 | |
| 139 | def build_base_files(target_version): |
| 140 | """ Builds needed base policy files from the source code. |
| 141 | |
| 142 | Args: |
| 143 | target_version: string, target version to gerenate the mapping file |
| 144 | |
| 145 | Returns: |
| 146 | (string, string, string), paths to base policy, old policy, and pub policy |
| 147 | cil |
| 148 | """ |
| 149 | logging.info('building base sepolicy files') |
| 150 | build_top = get_android_build_top() |
| 151 | |
| 152 | cmd = [ |
| 153 | 'build/soong/soong_ui.bash', |
| 154 | '--make-mode', |
| 155 | 'dist', |
| 156 | 'base-sepolicy-files-for-mapping', |
| 157 | 'TARGET_PRODUCT=aosp_arm64', |
| 158 | 'TARGET_BUILD_VARIANT=userdebug', |
| 159 | ] |
| 160 | check_run(cmd, cwd=build_top) |
| 161 | |
| 162 | dist_dir = os.path.join(build_top, 'out', 'dist') |
| 163 | base_policy_path = os.path.join(dist_dir, 'base_plat_sepolicy') |
| 164 | old_policy_path = os.path.join(dist_dir, |
| 165 | '%s_plat_sepolicy' % target_version) |
| 166 | pub_policy_cil_path = os.path.join(dist_dir, 'base_plat_pub_policy.cil') |
| 167 | |
| 168 | return base_policy_path, old_policy_path, pub_policy_cil_path |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 169 | |
| 170 | |
Inseob Kim | 9eadc83 | 2022-01-25 21:58:03 +0900 | [diff] [blame] | 171 | def change_api_level(versioned_type, api_from, api_to): |
| 172 | """ Verifies the API version of versioned_type, and changes it to new API level. |
| 173 | |
| 174 | For example, change_api_level("foo_32_0", "32.0", "31.0") will return |
| 175 | "foo_31_0". |
| 176 | |
| 177 | Args: |
| 178 | versioned_type: string, type with version suffix |
| 179 | api_from: string, api version of versioned_type |
| 180 | api_to: string, new api version for versioned_type |
| 181 | |
| 182 | Returns: |
| 183 | string, a new versioned type |
| 184 | """ |
| 185 | old_suffix = api_from.replace('.', '_') |
| 186 | new_suffix = api_to.replace('.', '_') |
| 187 | if not versioned_type.endswith(old_suffix): |
| 188 | raise ValueError('Version of type %s is different from %s' % |
| 189 | (versioned_type, api_from)) |
| 190 | return versioned_type.removesuffix(old_suffix) + new_suffix |
| 191 | |
| 192 | |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 193 | def get_args(): |
| 194 | parser = argparse.ArgumentParser() |
| 195 | parser.add_argument( |
| 196 | '--branch', |
| 197 | required=True, |
| 198 | help='Branch to pull build from. e.g. "sc-v2-dev"') |
| 199 | parser.add_argument('--build', required=True, help='Build ID, or "latest"') |
| 200 | parser.add_argument( |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 201 | '--target-version', |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 202 | required=True, |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 203 | help='Target version of designated mapping file. e.g. "32.0"') |
| 204 | parser.add_argument( |
| 205 | '--latest-version', |
| 206 | required=True, |
| 207 | help='Latest version for mapping of newer types. e.g. "31.0"') |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 208 | parser.add_argument( |
| 209 | '-v', |
| 210 | '--verbose', |
| 211 | action='count', |
| 212 | default=0, |
| 213 | help='Increase output verbosity, e.g. "-v", "-vv".') |
| 214 | return parser.parse_args() |
| 215 | |
| 216 | |
| 217 | def main(): |
| 218 | args = get_args() |
| 219 | |
| 220 | verbosity = min(args.verbose, 2) |
| 221 | logging.basicConfig( |
| 222 | format='%(levelname)-8s [%(filename)s:%(lineno)d] %(message)s', |
| 223 | level=(logging.WARNING, logging.INFO, logging.DEBUG)[verbosity]) |
| 224 | |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 225 | global temp_dir |
| 226 | temp_dir = tempfile.mkdtemp() |
| 227 | |
| 228 | try: |
| 229 | libpath = os.path.join( |
Inseob Kim | 73172d8 | 2022-10-19 18:25:23 +0900 | [diff] [blame] | 230 | os.path.dirname(os.path.realpath(__file__)), 'libsepolwrap' + SHARED_LIB_EXTENSION) |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 231 | if not os.path.exists(libpath): |
| 232 | sys.exit( |
| 233 | 'Error: libsepolwrap does not exist. Is this binary corrupted?\n' |
| 234 | ) |
| 235 | |
| 236 | build_top = get_android_build_top() |
| 237 | sepolicy_path = os.path.join(build_top, 'system', 'sepolicy') |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 238 | |
| 239 | # 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] | 240 | mapping_file = download_mapping_file( |
| 241 | args.branch, args.build, args.target_version, destination=temp_dir) |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 242 | mapping_file_cil = mini_parser.MiniCilParser(mapping_file) |
| 243 | mapping_file_cil.types = set() |
| 244 | mapping_file_cil.typeattributes = set() |
| 245 | |
| 246 | # Step 2. Build base policy files and parse latest mapping files |
| 247 | base_policy_path, old_policy_path, pub_policy_cil_path = build_base_files( |
| 248 | args.target_version) |
| 249 | base_policy = policy.Policy(base_policy_path, None, libpath) |
| 250 | old_policy = policy.Policy(old_policy_path, None, libpath) |
| 251 | pub_policy_cil = mini_parser.MiniCilParser(pub_policy_cil_path) |
| 252 | |
| 253 | all_types = base_policy.GetAllTypes(False) |
| 254 | old_all_types = old_policy.GetAllTypes(False) |
| 255 | pub_types = pub_policy_cil.types |
| 256 | |
| 257 | # Step 3. Find new types and removed types |
| 258 | new_types = pub_types & (all_types - old_all_types) |
| 259 | removed_types = (mapping_file_cil.pubtypes - mapping_file_cil.types) & ( |
| 260 | old_all_types - all_types) |
| 261 | |
| 262 | logging.info('new types: %s' % new_types) |
| 263 | logging.info('removed types: %s' % removed_types) |
| 264 | |
Inseob Kim | 9eadc83 | 2022-01-25 21:58:03 +0900 | [diff] [blame] | 265 | # Step 4. Map new types and removed types appropriately, based on the latest mapping |
| 266 | latest_compat_path = os.path.join(sepolicy_path, 'private', 'compat', |
| 267 | args.latest_version) |
| 268 | latest_mapping_cil = mini_parser.MiniCilParser( |
| 269 | os.path.join(latest_compat_path, args.latest_version + '.cil')) |
| 270 | latest_ignore_cil = mini_parser.MiniCilParser( |
| 271 | os.path.join(latest_compat_path, |
| 272 | args.latest_version + '.ignore.cil')) |
| 273 | |
| 274 | latest_ignored_types = list(latest_ignore_cil.rTypeattributesets.keys()) |
| 275 | latest_removed_types = latest_mapping_cil.types |
| 276 | logging.debug('types ignored in latest policy: %s' % |
| 277 | latest_ignored_types) |
| 278 | logging.debug('types removed in latest policy: %s' % |
| 279 | latest_removed_types) |
| 280 | |
| 281 | target_ignored_types = set() |
| 282 | target_removed_types = set() |
| 283 | invalid_new_types = set() |
| 284 | invalid_mapping_types = set() |
| 285 | invalid_removed_types = set() |
| 286 | |
| 287 | logging.info('starting mapping') |
| 288 | for new_type in new_types: |
| 289 | # Either each new type should be in latest_ignore_cil, or mapped to existing types |
| 290 | if new_type in latest_ignored_types: |
| 291 | logging.debug('adding %s to ignore' % new_type) |
| 292 | target_ignored_types.add(new_type) |
| 293 | elif new_type in latest_mapping_cil.rTypeattributesets: |
| 294 | latest_mapped_types = latest_mapping_cil.rTypeattributesets[ |
| 295 | new_type] |
| 296 | target_mapped_types = {change_api_level(t, args.latest_version, |
| 297 | args.target_version) |
| 298 | for t in latest_mapped_types} |
| 299 | logging.debug('mapping %s to %s' % |
| 300 | (new_type, target_mapped_types)) |
| 301 | |
| 302 | for t in target_mapped_types: |
| 303 | if t not in mapping_file_cil.typeattributesets: |
| 304 | logging.error( |
| 305 | 'Cannot find desired type %s in mapping file' % t) |
| 306 | invalid_mapping_types.add(t) |
| 307 | continue |
| 308 | mapping_file_cil.typeattributesets[t].add(new_type) |
| 309 | else: |
| 310 | logging.error('no mapping information for new type %s' % |
| 311 | new_type) |
| 312 | invalid_new_types.add(new_type) |
| 313 | |
| 314 | for removed_type in removed_types: |
| 315 | # Removed type should be in latest_mapping_cil |
| 316 | if removed_type in latest_removed_types: |
| 317 | logging.debug('adding %s to removed' % removed_type) |
| 318 | target_removed_types.add(removed_type) |
| 319 | else: |
| 320 | logging.error('no mapping information for removed type %s' % |
| 321 | removed_type) |
| 322 | invalid_removed_types.add(removed_type) |
| 323 | |
| 324 | error_msg = '' |
| 325 | |
| 326 | if invalid_new_types: |
| 327 | error_msg += ('The following new types were not in the latest ' |
| 328 | 'mapping: %s\n') % sorted(invalid_new_types) |
| 329 | if invalid_mapping_types: |
| 330 | error_msg += ( |
| 331 | 'The following existing types were not in the ' |
| 332 | 'downloaded mapping file: %s\n') % sorted(invalid_mapping_types) |
| 333 | if invalid_removed_types: |
| 334 | error_msg += ('The following removed types were not in the latest ' |
| 335 | 'mapping: %s\n') % sorted(invalid_removed_types) |
| 336 | |
| 337 | if error_msg: |
| 338 | error_msg += '\n' |
| 339 | error_msg += ('Please make sure the source tree and the build ID is' |
| 340 | ' up to date.\n') |
| 341 | sys.exit(error_msg) |
| 342 | |
| 343 | # Step 5. Write to system/sepolicy/private/compat |
| 344 | target_compat_path = os.path.join(sepolicy_path, 'private', 'compat', |
| 345 | args.target_version) |
| 346 | target_mapping_file = os.path.join(target_compat_path, |
| 347 | args.target_version + '.cil') |
| 348 | target_compat_file = os.path.join(target_compat_path, |
| 349 | args.target_version + '.compat.cil') |
| 350 | target_ignore_file = os.path.join(target_compat_path, |
| 351 | args.target_version + '.ignore.cil') |
| 352 | |
| 353 | with open(target_mapping_file, 'w') as f: |
| 354 | logging.info('writing %s' % target_mapping_file) |
| 355 | if removed_types: |
| 356 | f.write(';; types removed from current policy\n') |
| 357 | f.write('\n'.join(f'(type {x})' for x in sorted(target_removed_types))) |
| 358 | f.write('\n\n') |
| 359 | f.write(mapping_file_cil.unparse()) |
| 360 | |
| 361 | with open(target_compat_file, 'w') as f: |
| 362 | logging.info('writing %s' % target_compat_file) |
| 363 | f.write(compat_cil_template) |
| 364 | |
| 365 | with open(target_ignore_file, 'w') as f: |
| 366 | logging.info('writing %s' % target_ignore_file) |
| 367 | f.write(ignore_cil_template % |
| 368 | ('\n '.join(sorted(target_ignored_types)))) |
Inseob Kim | cbc95ea | 2022-01-21 19:32:53 +0900 | [diff] [blame] | 369 | finally: |
| 370 | logging.info('Deleting temporary dir: {}'.format(temp_dir)) |
| 371 | shutil.rmtree(temp_dir) |
Inseob Kim | 29e357e | 2022-01-17 16:37:51 +0900 | [diff] [blame] | 372 | |
| 373 | |
| 374 | if __name__ == '__main__': |
| 375 | main() |