Jeongik Cha | ef26afd | 2019-02-07 15:51:47 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2019 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 | # |
Felix | 3769a20 | 2020-05-31 01:17:34 +0200 | [diff] [blame^] | 17 | import argparse |
| 18 | import json |
Jeongik Cha | ef26afd | 2019-02-07 15:51:47 +0900 | [diff] [blame] | 19 | import os |
| 20 | import subprocess |
Jeongik Cha | ef26afd | 2019-02-07 15:51:47 +0900 | [diff] [blame] | 21 | import sys |
Jeongik Cha | ef26afd | 2019-02-07 15:51:47 +0900 | [diff] [blame] | 22 | |
Felix | 3769a20 | 2020-05-31 01:17:34 +0200 | [diff] [blame^] | 23 | from collections import defaultdict |
| 24 | from glob import glob |
| 25 | |
| 26 | def parse_args(): |
| 27 | """Parse commandline arguments.""" |
| 28 | parser = argparse.ArgumentParser(description='Find sharedUserId violators') |
| 29 | parser.add_argument('--product_out', help='PRODUCT_OUT directory', |
| 30 | default=os.environ.get("PRODUCT_OUT")) |
| 31 | parser.add_argument('--aapt', help='Path to aapt or aapt2', |
| 32 | default="aapt2") |
| 33 | parser.add_argument('--copy_out_system', help='TARGET_COPY_OUT_SYSTEM', |
| 34 | default="system") |
| 35 | parser.add_argument('--copy_out_vendor', help='TARGET_COPY_OUT_VENDOR', |
| 36 | default="vendor") |
| 37 | parser.add_argument('--copy_out_product', help='TARGET_COPY_OUT_PRODUCT', |
| 38 | default="product") |
| 39 | parser.add_argument('--copy_out_system_ext', help='TARGET_COPY_OUT_SYSTEM_EXT', |
| 40 | default="system_ext") |
| 41 | return parser.parse_args() |
Jeongik Cha | ef26afd | 2019-02-07 15:51:47 +0900 | [diff] [blame] | 42 | |
Jeongik Cha | b806c44 | 2019-02-15 15:21:00 +0900 | [diff] [blame] | 43 | def execute(cmd): |
| 44 | p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 45 | out, err = map(lambda b: b.decode('utf-8'), p.communicate()) |
| 46 | return p.returncode == 0, out, err |
| 47 | |
| 48 | def make_aapt_cmds(file): |
| 49 | return [aapt + ' dump ' + file + ' --file AndroidManifest.xml', |
Jeongik Cha | ef26afd | 2019-02-07 15:51:47 +0900 | [diff] [blame] | 50 | aapt + ' dump xmltree ' + file + ' --file AndroidManifest.xml'] |
Jeongik Cha | ef26afd | 2019-02-07 15:51:47 +0900 | [diff] [blame] | 51 | |
| 52 | def extract_shared_uid(file): |
Jeongik Cha | b806c44 | 2019-02-15 15:21:00 +0900 | [diff] [blame] | 53 | for cmd in make_aapt_cmds(file): |
| 54 | success, manifest, error_msg = execute(cmd) |
| 55 | if success: |
| 56 | break |
| 57 | else: |
| 58 | print(error_msg, file=sys.stderr) |
| 59 | sys.exit() |
Jeongik Cha | b806c44 | 2019-02-15 15:21:00 +0900 | [diff] [blame] | 60 | |
| 61 | for l in manifest.split('\n'): |
Jeongik Cha | ef26afd | 2019-02-07 15:51:47 +0900 | [diff] [blame] | 62 | if "sharedUserId" in l: |
| 63 | return l.split('"')[-2] |
| 64 | return None |
| 65 | |
| 66 | |
Felix | 3769a20 | 2020-05-31 01:17:34 +0200 | [diff] [blame^] | 67 | args = parse_args() |
| 68 | |
| 69 | product_out = args.product_out |
| 70 | aapt = args.aapt |
| 71 | |
| 72 | partitions = ( |
| 73 | ("system", args.copy_out_system), |
| 74 | ("vendor", args.copy_out_vendor), |
| 75 | ("product", args.copy_out_product), |
| 76 | ("system_ext", args.copy_out_system_ext), |
| 77 | ) |
Jeongik Cha | ef26afd | 2019-02-07 15:51:47 +0900 | [diff] [blame] | 78 | |
| 79 | shareduid_app_dict = defaultdict(list) |
| 80 | |
Felix | 3769a20 | 2020-05-31 01:17:34 +0200 | [diff] [blame^] | 81 | for part, location in partitions: |
| 82 | for f in glob(os.path.join(product_out, location, "*", "*", "*.apk")): |
Jeongik Cha | ef26afd | 2019-02-07 15:51:47 +0900 | [diff] [blame] | 83 | apk_file = os.path.basename(f) |
| 84 | shared_uid = extract_shared_uid(f) |
| 85 | |
| 86 | if shared_uid is None: |
| 87 | continue |
Felix | 3769a20 | 2020-05-31 01:17:34 +0200 | [diff] [blame^] | 88 | shareduid_app_dict[shared_uid].append((part, apk_file)) |
Jeongik Cha | ef26afd | 2019-02-07 15:51:47 +0900 | [diff] [blame] | 89 | |
| 90 | |
| 91 | output = defaultdict(lambda: defaultdict(list)) |
| 92 | |
| 93 | for uid, app_infos in shareduid_app_dict.items(): |
| 94 | partitions = {p for p, _ in app_infos} |
| 95 | if len(partitions) > 1: |
| 96 | for part in partitions: |
| 97 | output[uid][part].extend([a for p, a in app_infos if p == part]) |
| 98 | |
| 99 | print(json.dumps(output, indent=2, sort_keys=True)) |