Jooyung Han | 23d1e62 | 2023-04-04 18:03:07 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright 2023 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 | """ A tool to test APEX file_contexts |
| 17 | |
| 18 | Usage: |
| 19 | $ deapexer list -Z foo.apex > /tmp/fc |
| 20 | $ apex_sepolicy_tests -f /tmp/fc |
| 21 | """ |
| 22 | |
| 23 | |
| 24 | import argparse |
| 25 | import os |
| 26 | import pathlib |
| 27 | import pkgutil |
| 28 | import re |
| 29 | import sys |
| 30 | import tempfile |
| 31 | from dataclasses import dataclass |
| 32 | from typing import List |
| 33 | |
| 34 | import policy |
| 35 | |
| 36 | |
| 37 | SHARED_LIB_EXTENSION = '.dylib' if sys.platform == 'darwin' else '.so' |
| 38 | LIBSEPOLWRAP = "libsepolwrap" + SHARED_LIB_EXTENSION |
| 39 | |
| 40 | |
| 41 | @dataclass |
| 42 | class Is: |
| 43 | """Exact matcher for a path.""" |
| 44 | path: str |
| 45 | |
| 46 | |
| 47 | @dataclass |
| 48 | class Glob: |
| 49 | """Path matcher with pathlib.PurePath.match""" |
| 50 | pattern: str |
| 51 | |
| 52 | |
| 53 | @dataclass |
| 54 | class Regex: |
| 55 | """Path matcher with re.match""" |
| 56 | pattern: str |
| 57 | |
| 58 | |
| 59 | Matcher = Is | Glob | Regex |
| 60 | |
| 61 | @dataclass |
| 62 | class AllowRead: |
| 63 | """Rule checking if scontext can read the entity""" |
| 64 | tclass: str |
| 65 | scontext: set[str] |
| 66 | |
| 67 | |
Jooyung Han | 92bfb37 | 2023-09-08 14:28:40 +0900 | [diff] [blame^] | 68 | @dataclass |
| 69 | class ResolveType: |
| 70 | """Rule checking if type can be resolved""" |
| 71 | pass |
| 72 | |
| 73 | |
| 74 | Rule = AllowRead | ResolveType |
Jooyung Han | 23d1e62 | 2023-04-04 18:03:07 +0900 | [diff] [blame] | 75 | |
| 76 | |
| 77 | def match_path(path: str, matcher: Matcher) -> bool: |
| 78 | """True if path matches with the given matcher""" |
| 79 | match matcher: |
| 80 | case Is(target): |
| 81 | return path == target |
| 82 | case Glob(pattern): |
| 83 | return pathlib.PurePath(path).match(pattern) |
| 84 | case Regex(pattern): |
| 85 | return re.match(pattern, path) |
| 86 | |
| 87 | |
| 88 | def check_rule(pol, path: str, tcontext: str, rule: Rule) -> List[str]: |
| 89 | """Returns error message if scontext can't read the target""" |
Jooyung Han | 3e592f2 | 2023-06-05 10:47:20 +0900 | [diff] [blame] | 90 | errors = [] |
Jooyung Han | 23d1e62 | 2023-04-04 18:03:07 +0900 | [diff] [blame] | 91 | match rule: |
| 92 | case AllowRead(tclass, scontext): |
Jooyung Han | 61b46b6 | 2023-05-31 17:41:28 +0900 | [diff] [blame] | 93 | # Test every source in scontext(set) |
| 94 | for s in scontext: |
| 95 | te_rules = list(pol.QueryTERule(scontext={s}, |
| 96 | tcontext={tcontext}, |
| 97 | tclass={tclass}, |
| 98 | perms={'read'})) |
| 99 | if len(te_rules) > 0: |
Jooyung Han | 3e592f2 | 2023-06-05 10:47:20 +0900 | [diff] [blame] | 100 | continue # no errors |
Jooyung Han | 23d1e62 | 2023-04-04 18:03:07 +0900 | [diff] [blame] | 101 | |
Jooyung Han | 3e592f2 | 2023-06-05 10:47:20 +0900 | [diff] [blame] | 102 | errors.append(f"Error: {path}: {s} can't read. (tcontext={tcontext})") |
Jooyung Han | 92bfb37 | 2023-09-08 14:28:40 +0900 | [diff] [blame^] | 103 | case ResolveType(): |
| 104 | if tcontext not in pol.GetAllTypes(False): |
| 105 | errors.append(f"Error: {path}: tcontext({tcontext}) is unknown") |
Jooyung Han | 3e592f2 | 2023-06-05 10:47:20 +0900 | [diff] [blame] | 106 | return errors |
Jooyung Han | 23d1e62 | 2023-04-04 18:03:07 +0900 | [diff] [blame] | 107 | |
| 108 | |
Jooyung Han | 92bfb37 | 2023-09-08 14:28:40 +0900 | [diff] [blame^] | 109 | target_specific_rules = [ |
| 110 | (Glob('*'), ResolveType()), |
| 111 | ] |
| 112 | |
| 113 | |
| 114 | generic_rules = [ |
Jooyung Han | 23d1e62 | 2023-04-04 18:03:07 +0900 | [diff] [blame] | 115 | # permissions |
| 116 | (Is('./etc/permissions/'), AllowRead('dir', {'system_server'})), |
| 117 | (Glob('./etc/permissions/*.xml'), AllowRead('file', {'system_server'})), |
| 118 | # init scripts with optional SDK version (e.g. foo.rc, foo.32rc) |
| 119 | (Regex('\./etc/.*\.\d*rc'), AllowRead('file', {'init'})), |
| 120 | # vintf fragments |
| 121 | (Is('./etc/vintf/'), AllowRead('dir', {'servicemanager', 'apexd'})), |
| 122 | (Glob('./etc/vintf/*.xml'), AllowRead('file', {'servicemanager', 'apexd'})), |
| 123 | # ./ and apex_manifest.pb |
| 124 | (Is('./apex_manifest.pb'), AllowRead('file', {'linkerconfig', 'apexd'})), |
| 125 | (Is('./'), AllowRead('dir', {'linkerconfig', 'apexd'})), |
Jooyung Han | babd060 | 2023-04-24 15:34:49 +0900 | [diff] [blame] | 126 | # linker.config.pb |
| 127 | (Is('./etc/linker.config.pb'), AllowRead('file', {'linkerconfig'})), |
Jooyung Han | 23d1e62 | 2023-04-04 18:03:07 +0900 | [diff] [blame] | 128 | ] |
| 129 | |
| 130 | |
Jooyung Han | 92bfb37 | 2023-09-08 14:28:40 +0900 | [diff] [blame^] | 131 | all_rules = target_specific_rules + generic_rules |
| 132 | |
| 133 | |
| 134 | def check_line(pol: policy.Policy, line: str, rules) -> List[str]: |
Jooyung Han | 23d1e62 | 2023-04-04 18:03:07 +0900 | [diff] [blame] | 135 | """Parses a file_contexts line and runs checks""" |
| 136 | # skip empty/comment line |
| 137 | line = line.strip() |
| 138 | if line == '' or line[0] == '#': |
| 139 | return [] |
| 140 | |
| 141 | # parse |
| 142 | split = line.split() |
| 143 | if len(split) != 2: |
| 144 | return [f"Error: invalid file_contexts: {line}"] |
| 145 | path, context = split[0], split[1] |
| 146 | if len(context.split(':')) != 4: |
| 147 | return [f"Error: invalid file_contexts: {line}"] |
| 148 | tcontext = context.split(':')[2] |
| 149 | |
| 150 | # check rules |
| 151 | errors = [] |
| 152 | for matcher, rule in rules: |
| 153 | if match_path(path, matcher): |
| 154 | errors.extend(check_rule(pol, path, tcontext, rule)) |
| 155 | return errors |
| 156 | |
| 157 | |
| 158 | def extract_data(name, temp_dir): |
| 159 | out_path = os.path.join(temp_dir, name) |
| 160 | with open(out_path, 'wb') as f: |
| 161 | blob = pkgutil.get_data('apex_sepolicy_tests', name) |
| 162 | if not blob: |
| 163 | sys.exit(f"Error: {name} does not exist. Is this binary corrupted?\n") |
| 164 | f.write(blob) |
| 165 | return out_path |
| 166 | |
| 167 | |
| 168 | def do_main(work_dir): |
| 169 | """Do testing""" |
| 170 | parser = argparse.ArgumentParser() |
Jooyung Han | 92bfb37 | 2023-09-08 14:28:40 +0900 | [diff] [blame^] | 171 | parser.add_argument('--all', action='store_true', help='tests ALL aspects') |
Jooyung Han | 23d1e62 | 2023-04-04 18:03:07 +0900 | [diff] [blame] | 172 | parser.add_argument('-f', '--file_contexts', help='output of "deapexer list -Z"') |
| 173 | args = parser.parse_args() |
| 174 | |
| 175 | lib_path = extract_data(LIBSEPOLWRAP, work_dir) |
| 176 | policy_path = extract_data('precompiled_sepolicy', work_dir) |
| 177 | pol = policy.Policy(policy_path, None, lib_path) |
| 178 | |
Jooyung Han | 92bfb37 | 2023-09-08 14:28:40 +0900 | [diff] [blame^] | 179 | if args.all: |
| 180 | rules = all_rules |
| 181 | else: |
| 182 | rules = generic_rules |
| 183 | |
Jooyung Han | 23d1e62 | 2023-04-04 18:03:07 +0900 | [diff] [blame] | 184 | errors = [] |
| 185 | with open(args.file_contexts, 'rt', encoding='utf-8') as file_contexts: |
| 186 | for line in file_contexts: |
Jooyung Han | 92bfb37 | 2023-09-08 14:28:40 +0900 | [diff] [blame^] | 187 | errors.extend(check_line(pol, line, rules)) |
Jooyung Han | 23d1e62 | 2023-04-04 18:03:07 +0900 | [diff] [blame] | 188 | if len(errors) > 0: |
| 189 | sys.exit('\n'.join(errors)) |
| 190 | |
| 191 | |
| 192 | if __name__ == '__main__': |
| 193 | with tempfile.TemporaryDirectory() as temp_dir: |
| 194 | do_main(temp_dir) |