1*e4a36f41SAndroid Build Coastguard Worker#!/usr/bin/env python3 2*e4a36f41SAndroid Build Coastguard Worker 3*e4a36f41SAndroid Build Coastguard Worker# Copyright 2021 The Android Open Source Project 4*e4a36f41SAndroid Build Coastguard Worker# 5*e4a36f41SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*e4a36f41SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*e4a36f41SAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*e4a36f41SAndroid Build Coastguard Worker# 9*e4a36f41SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*e4a36f41SAndroid Build Coastguard Worker# 11*e4a36f41SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*e4a36f41SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*e4a36f41SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*e4a36f41SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*e4a36f41SAndroid Build Coastguard Worker# limitations under the License. 16*e4a36f41SAndroid Build Coastguard Worker 17*e4a36f41SAndroid Build Coastguard Workerimport argparse 18*e4a36f41SAndroid Build Coastguard Workerimport re 19*e4a36f41SAndroid Build Coastguard Workerimport sys 20*e4a36f41SAndroid Build Coastguard Worker 21*e4a36f41SAndroid Build Coastguard Worker# A line should look like: 22*e4a36f41SAndroid Build Coastguard Worker# {prop_name} u:object_r:{context_name}:s0 23*e4a36f41SAndroid Build Coastguard Workerline_regex = re.compile(r'^(\S+)\s+u:object_r:([^:]+):s0.*$') 24*e4a36f41SAndroid Build Coastguard Worker 25*e4a36f41SAndroid Build Coastguard Worker# Parses a line in property_contexts and return a (prop, ctx) tuple. 26*e4a36f41SAndroid Build Coastguard Worker# Raises an error for any malformed entries. 27*e4a36f41SAndroid Build Coastguard Workerdef parse_line(line): 28*e4a36f41SAndroid Build Coastguard Worker matched = line_regex.match(line) 29*e4a36f41SAndroid Build Coastguard Worker if not matched: 30*e4a36f41SAndroid Build Coastguard Worker raise ValueError('malformed entry "' + line + '" in property_contexts') 31*e4a36f41SAndroid Build Coastguard Worker 32*e4a36f41SAndroid Build Coastguard Worker return matched.group(1, 2) 33*e4a36f41SAndroid Build Coastguard Worker 34*e4a36f41SAndroid Build Coastguard Workerdef parse_args(): 35*e4a36f41SAndroid Build Coastguard Worker parser = argparse.ArgumentParser( 36*e4a36f41SAndroid Build Coastguard Worker description="Finds any violations in property_contexts, with given allowed prefixes. " 37*e4a36f41SAndroid Build Coastguard Worker "If any violations are found, return a nonzero (failure) exit code.") 38*e4a36f41SAndroid Build Coastguard Worker parser.add_argument("--property-contexts", help="Path to property_contexts file.") 39*e4a36f41SAndroid Build Coastguard Worker parser.add_argument("--allowed-property-prefix", action="extend", nargs="*", 40*e4a36f41SAndroid Build Coastguard Worker help="Allowed property prefixes. If empty, any properties are allowed.") 41*e4a36f41SAndroid Build Coastguard Worker parser.add_argument("--allowed-context-prefix", action="extend", nargs="*", 42*e4a36f41SAndroid Build Coastguard Worker help="Allowed context prefixes. If empty, any contexts are allowed.") 43*e4a36f41SAndroid Build Coastguard Worker parser.add_argument('--strict', action='store_true', 44*e4a36f41SAndroid Build Coastguard Worker help="Make the script fail if any violations are found.") 45*e4a36f41SAndroid Build Coastguard Worker 46*e4a36f41SAndroid Build Coastguard Worker return parser.parse_args() 47*e4a36f41SAndroid Build Coastguard Worker 48*e4a36f41SAndroid Build Coastguard Workerargs = parse_args() 49*e4a36f41SAndroid Build Coastguard Worker 50*e4a36f41SAndroid Build Coastguard Workerviolations = [] 51*e4a36f41SAndroid Build Coastguard Worker 52*e4a36f41SAndroid Build Coastguard Workerwith open(args.property_contexts, 'r') as f: 53*e4a36f41SAndroid Build Coastguard Worker lines = f.read().split('\n') 54*e4a36f41SAndroid Build Coastguard Worker 55*e4a36f41SAndroid Build Coastguard Workerfor line in lines: 56*e4a36f41SAndroid Build Coastguard Worker tokens = line.strip() 57*e4a36f41SAndroid Build Coastguard Worker # if this line empty or a comment, skip 58*e4a36f41SAndroid Build Coastguard Worker if tokens == '' or tokens[0] == '#': 59*e4a36f41SAndroid Build Coastguard Worker continue 60*e4a36f41SAndroid Build Coastguard Worker 61*e4a36f41SAndroid Build Coastguard Worker prop, context = parse_line(line) 62*e4a36f41SAndroid Build Coastguard Worker 63*e4a36f41SAndroid Build Coastguard Worker violated = False 64*e4a36f41SAndroid Build Coastguard Worker 65*e4a36f41SAndroid Build Coastguard Worker if args.allowed_property_prefix and not prop.startswith(tuple(args.allowed_property_prefix)): 66*e4a36f41SAndroid Build Coastguard Worker violated = True 67*e4a36f41SAndroid Build Coastguard Worker 68*e4a36f41SAndroid Build Coastguard Worker if args.allowed_context_prefix and not context.startswith(tuple(args.allowed_context_prefix)): 69*e4a36f41SAndroid Build Coastguard Worker violated = True 70*e4a36f41SAndroid Build Coastguard Worker 71*e4a36f41SAndroid Build Coastguard Worker if violated: 72*e4a36f41SAndroid Build Coastguard Worker violations.append(line) 73*e4a36f41SAndroid Build Coastguard Worker 74*e4a36f41SAndroid Build Coastguard Workerif len(violations) > 0: 75*e4a36f41SAndroid Build Coastguard Worker print('******************************') 76*e4a36f41SAndroid Build Coastguard Worker print('%d violations found:' % len(violations)) 77*e4a36f41SAndroid Build Coastguard Worker print('\n'.join(violations)) 78*e4a36f41SAndroid Build Coastguard Worker print('******************************') 79*e4a36f41SAndroid Build Coastguard Worker print("vendor's and odm's property_contexts MUST use ONLY vendor-prefixed properties.") 80*e4a36f41SAndroid Build Coastguard Worker print('This is enforced by VTS, so please fix such offending properties.') 81*e4a36f41SAndroid Build Coastguard Worker if args.allowed_property_prefix: 82*e4a36f41SAndroid Build Coastguard Worker print('Allowed property prefixes for %s: %s' % (args.property_contexts, args.allowed_property_prefix)) 83*e4a36f41SAndroid Build Coastguard Worker if args.allowed_context_prefix: 84*e4a36f41SAndroid Build Coastguard Worker print('Allowed context prefixes for %s: %s' % (args.property_contexts, args.allowed_context_prefix)) 85*e4a36f41SAndroid Build Coastguard Worker if args.strict: 86*e4a36f41SAndroid Build Coastguard Worker print('You can temporarily disable this check with setting BUILD_BROKEN_VENDOR_PROPERTY_NAMESPACE := true in BoardConfig.mk.') 87*e4a36f41SAndroid Build Coastguard Worker print('But property namespace is enforced by VTS, and you will need to fix such violations to pass VTS.') 88*e4a36f41SAndroid Build Coastguard Worker print('See test/vts-testcase/security/system_property/vts_treble_sys_prop_test.py for the detail of the VTS.') 89*e4a36f41SAndroid Build Coastguard Worker sys.exit(1) 90