Tri Vo | 438684b | 2018-09-29 17:47:10 -0700 | [diff] [blame] | 1 | # Copyright 2018 - The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """Tool to combine SEPolicy mapping file. |
| 16 | |
| 17 | Say, x, y, z are platform SEPolicy versions such that x > y > z. Then given two |
| 18 | mapping files from x to y (top) and y to z (bottom), it's possible to construct |
| 19 | a mapping file from x to z. We do the following to combine two maps. |
| 20 | 1. Add all new types declarations from top to bottom. |
Tri Vo | 7bfd730 | 2019-08-20 15:41:53 -0700 | [diff] [blame^] | 21 | 2. Add all new typeattribute declarations from top to bottom. |
| 22 | 3. Say, a new type "bar" in top is mapped like this "foo_V_v<-bar", then we map |
Tri Vo | 438684b | 2018-09-29 17:47:10 -0700 | [diff] [blame] | 23 | "bar" to whatever "foo" is mapped to in the bottom map. We do this for all new |
| 24 | types in the top map. |
| 25 | |
| 26 | More generally, we can correctly construct x->z from x->y' and y"->z as long as |
| 27 | y">y'. |
| 28 | |
| 29 | This file contains the implementation of combining two mapping files. |
| 30 | """ |
| 31 | import argparse |
| 32 | import re |
| 33 | from mini_parser import MiniCilParser |
| 34 | |
| 35 | def Combine(top, bottom): |
| 36 | bottom.types.update(top.types) |
Tri Vo | 7bfd730 | 2019-08-20 15:41:53 -0700 | [diff] [blame^] | 37 | bottom.typeattributes.update(top.typeattributes) |
Tri Vo | 438684b | 2018-09-29 17:47:10 -0700 | [diff] [blame] | 38 | |
| 39 | for top_ta in top.typeattributesets: |
| 40 | top_type_set = top.typeattributesets[top_ta] |
| 41 | if len(top_type_set) == 1: |
| 42 | continue |
| 43 | |
| 44 | m = re.match(r"(\w+)_\d+_\d+", top_ta) |
| 45 | # Typeattributes in V.v.cil have _V_v suffix, but not in V.v.ignore.cil |
| 46 | bottom_type = m.group(1) if m else top_ta |
| 47 | |
| 48 | for bottom_ta in bottom.rTypeattributesets[bottom_type]: |
| 49 | bottom.typeattributesets[bottom_ta].update(top_type_set) |
| 50 | |
| 51 | return bottom |
| 52 | |
| 53 | if __name__ == "__main__": |
| 54 | parser = argparse.ArgumentParser() |
| 55 | parser.add_argument("-t", "--top-map", dest="top_map", |
| 56 | required=True, help="top map file") |
| 57 | parser.add_argument("-b", "--bottom-map", dest="bottom_map", |
| 58 | required=True, help="bottom map file") |
| 59 | parser.add_argument("-o", "--output-file", dest="output_file", |
| 60 | required=True, help="output map file") |
| 61 | args = parser.parse_args() |
| 62 | |
| 63 | top_map_cil = MiniCilParser(args.top_map) |
| 64 | bottom_map_cil = MiniCilParser(args.bottom_map) |
| 65 | result = Combine(top_map_cil, bottom_map_cil) |
| 66 | |
| 67 | with open(args.output_file, "w") as output: |
| 68 | output.write(result.unparse()) |