-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsepolicy_decompiler.py
182 lines (135 loc) · 4.24 KB
/
sepolicy_decompiler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import shutil
import sys
import os
from sepolicy import *
from sepolicy_mld import *
from sepolicy_macros import *
class SepolicyDecompiler:
def __init__(self, cil_paths, property_contexts_path,
file_contexts_path, hwservice_contexts_path, output_path):
self.cil_paths = cil_paths
self.property_contexts_path = property_contexts_path
self.file_contexts_path = file_contexts_path
self.hwservice_contexts_path = hwservice_contexts_path
self.output_path = output_path
self.genfs_contexts_rules = []
self.mld = MultiLevelDict()
self.types = {}
def read_cil_line(self, line):
line = line.replace('(', '') \
.replace(')', '') \
.replace('\n', '')
parts = line.split(' ')
# Ignore empty lines
if len(parts) == 1 and parts[0] == '':
return
parts = [sanitize_type(part) for part in parts]
if parts[0] == 'genfscon':
if parts[2].startswith('"') and parts[2].endswith('"'):
parts[2] = parts[2][1:len(parts[2]) - 1]
rule = Rule(parts)
if parts[0] == 'genfscon':
self.genfs_contexts_rules.append(rule)
return
self.mld.add(rule)
def read_cil(self, path):
with open(path) as file:
for line in file:
self.read_cil_line(line)
def read_cils(self):
for path in self.cil_paths:
self.read_cil(path)
def process_macros(self):
match_and_replace_macros(self.mld)
def group_rule(self, rule):
type_name = extract_type(rule.main_type)
if type_name not in self.types:
self.types[type_name] = Type(type_name)
type = self.types[type_name]
type.add_rule(rule)
def group_into_types(self):
match = Match([])
rules = self.mld.get(match)
for rule in rules:
self.group_rule(rule)
def get_simple_type_filename(self, rule):
keyword = rule.parts[0]
file_name = None
if keyword == 'type':
if 'dev_type' in rule.varargs:
file_name = 'device'
elif ('file_type' in rule.varargs) or ('fs_type' in rule.varargs):
file_name = 'file'
elif keyword.endswith('_prop'):
file_name = 'property'
return file_name
def write_rule_to_file(self, rule, file):
s = str(rule)
if s == '':
return
file.write(s)
file.write('\n');
def write_rules_to_file(self, type, file):
for rule in type.rules:
self.write_rule_to_file(rule, file)
def output_type(self, type_name):
type = self.types[type_name]
file_name = None
if len(type.rules) == 1:
rule = type.rules[0]
file_name = self.get_simple_type_filename(rule)
if file_name is None:
file_name = type_name
extension = '.te'
file_name = file_name[:(255 - len(extension))]
file_name += extension
path = os.path.join(self.output_path, file_name)
with open(path, 'a') as file:
self.write_rules_to_file(type, file)
def output_types(self):
for type_name in self.types:
self.output_type(type_name)
def copy_contexts(self, input_path, output_path):
lines = []
with open(input_path, 'r') as file:
for line in file:
if line.startswith('#') or line == '\n':
continue
line = re.sub(r'\s+', ' ', line).strip() + '\n'
lines.append(line)
lines.sort()
with open(output_path, 'w') as file:
for line in lines:
file.write(line)
def output_property_contexts(self):
path = os.path.join(self.output_path, 'property_contexts')
self.copy_contexts(self.property_contexts_path, path)
def output_file_contexts(self):
path = os.path.join(self.output_path, 'file_contexts')
self.copy_contexts(self.file_contexts_path, path)
def output_hwservice_contexts(self):
path = os.path.join(self.output_path, 'hwservice_contexts')
shutil.copy(self.hwservice_contexts_path, path)
def output_genfs_contexts(self):
path = os.path.join(self.output_path, 'genfs_contexts')
with open(path, 'w') as file:
for rule in self.genfs_contexts_rules:
self.write_rule_to_file(rule, file)
def create_output_dir(self):
os.makedirs(self.output_path, exist_ok=True)
def clean_output_dir(self):
for root, dirs, files in os.walk(self.output_path):
for f in files:
path = os.path.join(root, f)
os.unlink(path)
for d in dirs:
path = os.path.join(root, d)
shutil.rmtree(path)
def output(self):
self.create_output_dir()
self.clean_output_dir()
self.output_types()
self.output_property_contexts()
self.output_file_contexts()
self.output_hwservice_contexts()
self.output_genfs_contexts()