blob: dad911331fb0cbc3c7ef88c376ac22f51cbfd110 [file] [log] [blame]
Paul Lawrenceeabc3522016-11-11 11:33:42 -08001#!/usr/bin/env python
Paul Lawrence89fa81f2017-02-17 10:22:03 -08002import collections
Paul Lawrenceeabc3522016-11-11 11:33:42 -08003import os
Paul Lawrenceeabc3522016-11-11 11:33:42 -08004import textwrap
5from gensyscalls import SysCallsTxtParser
Paul Lawrence89fa81f2017-02-17 10:22:03 -08006from subprocess import Popen, PIPE
Paul Lawrenceeabc3522016-11-11 11:33:42 -08007
8
Paul Lawrence7ea40902017-02-14 13:32:23 -08009BPF_JGE = "BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, {0}, {1}, {2})"
10BPF_ALLOW = "BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW)"
Paul Lawrenceeabc3522016-11-11 11:33:42 -080011
12
13class SyscallRange(object):
14 def __init__(self, name, value):
15 self.names = [name]
16 self.begin = value
17 self.end = self.begin + 1
18
Paul Lawrence7ea40902017-02-14 13:32:23 -080019 def __str__(self):
20 return "(%s, %s, %s)" % (self.begin, self.end, self.names)
21
Paul Lawrenceeabc3522016-11-11 11:33:42 -080022 def add(self, name, value):
23 if value != self.end:
24 raise ValueError
25 self.end += 1
26 self.names.append(name)
27
28
Victor Hsieh4f02dd52017-12-20 09:19:22 -080029def load_syscall_names_from_file(file_path, architecture):
30 parser = SysCallsTxtParser()
31 parser.parse_open_file(open(file_path))
32 return set([x["name"] for x in parser.syscalls if x.get(architecture)])
Paul Lawrence3dd3d552017-04-12 10:02:54 -070033
Steve Muckleaa3f96c2017-07-20 13:11:54 -070034
Victor Hsieh4f02dd52017-12-20 09:19:22 -080035def merge_names(base_names, whitelist_names, blacklist_names):
36 if bool(blacklist_names - base_names):
37 raise RuntimeError("Blacklist item not in bionic - aborting " + str(
38 blacklist_name - base_names))
Paul Lawrence3dd3d552017-04-12 10:02:54 -070039
Victor Hsieh4f02dd52017-12-20 09:19:22 -080040 return (base_names - blacklist_names) | whitelist_names
Paul Lawrenceeabc3522016-11-11 11:33:42 -080041
Paul Lawrence7ea40902017-02-14 13:32:23 -080042
Paul Lawrence89fa81f2017-02-17 10:22:03 -080043def convert_names_to_NRs(names, header_dir, extra_switches):
Paul Lawrenceeabc3522016-11-11 11:33:42 -080044 # Run preprocessor over the __NR_syscall symbols, including unistd.h,
45 # to get the actual numbers
46 prefix = "__SECCOMP_" # prefix to ensure no name collisions
47 cpp = Popen(["../../prebuilts/clang/host/linux-x86/clang-stable/bin/clang",
Paul Lawrence89fa81f2017-02-17 10:22:03 -080048 "-E", "-nostdinc", "-I" + header_dir, "-Ikernel/uapi/"]
49 + extra_switches
50 + ["-"],
Paul Lawrenceeabc3522016-11-11 11:33:42 -080051 stdin=PIPE, stdout=PIPE)
52 cpp.stdin.write("#include <asm/unistd.h>\n")
53 for name in names:
54 # In SYSCALLS.TXT, there are two arm-specific syscalls whose names start
55 # with __ARM__NR_. These we must simply write out as is.
56 if not name.startswith("__ARM_NR_"):
57 cpp.stdin.write(prefix + name + ", __NR_" + name + "\n")
58 else:
59 cpp.stdin.write(prefix + name + ", " + name + "\n")
60 content = cpp.communicate()[0].split("\n")
61
62 # The input is now the preprocessed source file. This will contain a lot
63 # of junk from the preprocessor, but our lines will be in the format:
64 #
65 # __SECCOMP_${NAME}, (0 + value)
66
67 syscalls = []
68 for line in content:
69 if not line.startswith(prefix):
70 continue
71
72 # We might pick up extra whitespace during preprocessing, so best to strip.
73 name, value = [w.strip() for w in line.split(",")]
74 name = name[len(prefix):]
75
76 # Note that some of the numbers were expressed as base + offset, so we
77 # need to eval, not just int
78 value = eval(value)
79 syscalls.append((name, value))
80
Paul Lawrence7ea40902017-02-14 13:32:23 -080081 return syscalls
82
83
84def convert_NRs_to_ranges(syscalls):
Paul Lawrenceeabc3522016-11-11 11:33:42 -080085 # Sort the values so we convert to ranges and binary chop
86 syscalls = sorted(syscalls, lambda x, y: cmp(x[1], y[1]))
87
88 # Turn into a list of ranges. Keep the names for the comments
89 ranges = []
90 for name, value in syscalls:
91 if not ranges:
92 ranges.append(SyscallRange(name, value))
93 continue
94
95 last_range = ranges[-1]
96 if last_range.end == value:
97 last_range.add(name, value)
98 else:
99 ranges.append(SyscallRange(name, value))
Paul Lawrence7ea40902017-02-14 13:32:23 -0800100 return ranges
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800101
Paul Lawrence7ea40902017-02-14 13:32:23 -0800102
103# Converts the sorted ranges of allowed syscalls to a binary tree bpf
104# For a single range, output a simple jump to {fail} or {allow}. We can't set
105# the jump ranges yet, since we don't know the size of the filter, so use a
106# placeholder
107# For multiple ranges, split into two, convert the two halves and output a jump
108# to the correct half
109def convert_to_intermediate_bpf(ranges):
110 if len(ranges) == 1:
111 # We will replace {fail} and {allow} with appropriate range jumps later
112 return [BPF_JGE.format(ranges[0].end, "{fail}", "{allow}") +
113 ", //" + "|".join(ranges[0].names)]
114 else:
115 half = (len(ranges) + 1) / 2
116 first = convert_to_intermediate_bpf(ranges[:half])
117 second = convert_to_intermediate_bpf(ranges[half:])
118 jump = [BPF_JGE.format(ranges[half].begin, len(first), 0) + ","]
119 return jump + first + second
120
121
122def convert_ranges_to_bpf(ranges):
123 bpf = convert_to_intermediate_bpf(ranges)
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800124
125 # Now we know the size of the tree, we can substitute the {fail} and {allow}
126 # placeholders
127 for i, statement in enumerate(bpf):
128 # Replace placeholder with
129 # "distance to jump to fail, distance to jump to allow"
130 # We will add a kill statement and an allow statement after the tree
131 # With bpfs jmp 0 means the next statement, so the distance to the end is
132 # len(bpf) - i - 1, which is where we will put the kill statement, and
133 # then the statement after that is the allow statement
134 if "{fail}" in statement and "{allow}" in statement:
Paul Lawrencebe8a2af2017-01-25 15:20:52 -0800135 bpf[i] = statement.format(fail=str(len(bpf) - i),
136 allow=str(len(bpf) - i - 1))
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800137
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800138
Paul Lawrencebe8a2af2017-01-25 15:20:52 -0800139 # Add the allow calls at the end. If the syscall is not matched, we will
140 # continue. This allows the user to choose to match further syscalls, and
141 # also to choose the action when we want to block
Paul Lawrence7ea40902017-02-14 13:32:23 -0800142 bpf.append(BPF_ALLOW + ",")
Paul Lawrence65b47c92017-03-22 08:03:51 -0700143
144 # Add check that we aren't off the bottom of the syscalls
145 bpf.insert(0, BPF_JGE.format(ranges[0].begin, 0, str(len(bpf))) + ',')
Paul Lawrence7ea40902017-02-14 13:32:23 -0800146 return bpf
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800147
Paul Lawrence7ea40902017-02-14 13:32:23 -0800148
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800149def convert_bpf_to_output(bpf, architecture, name_modifier):
150 if name_modifier:
151 name_modifier = name_modifier + "_"
152 else:
153 name_modifier = ""
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800154 header = textwrap.dedent("""\
155 // Autogenerated file - edit at your peril!!
156
157 #include <linux/filter.h>
158 #include <errno.h>
159
Paul Lawrencedfe84342017-02-16 09:24:39 -0800160 #include "seccomp_bpfs.h"
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700161 const sock_filter {architecture}_{suffix}filter[] = {{
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800162 """).format(architecture=architecture,suffix=name_modifier)
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800163
164 footer = textwrap.dedent("""\
165
166 }};
167
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700168 const size_t {architecture}_{suffix}filter_size = sizeof({architecture}_{suffix}filter) / sizeof(struct sock_filter);
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800169 """).format(architecture=architecture,suffix=name_modifier)
Paul Lawrence7ea40902017-02-14 13:32:23 -0800170 return header + "\n".join(bpf) + footer
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800171
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800172
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800173def construct_bpf(names, architecture, header_dir, extra_switches,
174 name_modifier):
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800175 syscalls = convert_names_to_NRs(names, header_dir, extra_switches)
Paul Lawrence7ea40902017-02-14 13:32:23 -0800176 ranges = convert_NRs_to_ranges(syscalls)
177 bpf = convert_ranges_to_bpf(ranges)
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800178 return convert_bpf_to_output(bpf, architecture, name_modifier)
Paul Lawrence7ea40902017-02-14 13:32:23 -0800179
180
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800181# final syscalls = base - blacklists + whitelists
182ANDROID_SYSTEM_SYSCALL_FILES = {
183 "base": "SYSCALLS.TXT",
184 "whitelists": [
185 "SECCOMP_WHITELIST_COMMON.TXT",
186 "SECCOMP_WHITELIST_SYSTEM.TXT"],
187 "blacklists": ["SECCOMP_BLACKLIST_COMMON.TXT"]
188}
189
190ANDROID_APP_SYSCALL_FILES = {
191 "base": "SYSCALLS.TXT",
192 "whitelists": [
193 "SECCOMP_WHITELIST_COMMON.TXT",
194 "SECCOMP_WHITELIST_APP.TXT"],
195 "blacklists": ["SECCOMP_BLACKLIST_COMMON.TXT"]
196}
197
198ANDROID_GLOBAL_SYSCALL_FILES = {
199 "base": "SYSCALLS.TXT",
200 "whitelists": [
201 "SECCOMP_WHITELIST_COMMON.TXT",
202 "SECCOMP_WHITELIST_SYSTEM.TXT",
203 "SECCOMP_WHITELIST_APP.TXT",
204 "SECCOMP_WHITELIST_GLOBAL.TXT"],
205 "blacklists": ["SECCOMP_BLACKLIST_COMMON.TXT"]
206}
Paul Lawrence7ea40902017-02-14 13:32:23 -0800207
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800208
209POLICY_CONFIGS = [("arm", "kernel/uapi/asm-arm", []),
210 ("arm64", "kernel/uapi/asm-arm64", []),
211 ("x86", "kernel/uapi/asm-x86", ["-D__i386__"]),
212 ("x86_64", "kernel/uapi/asm-x86", []),
213 ("mips", "kernel/uapi/asm-mips", ["-D_MIPS_SIM=_MIPS_SIM_ABI32"]),
214 ("mips64", "kernel/uapi/asm-mips", ["-D_MIPS_SIM=_MIPS_SIM_ABI64"])]
Paul Lawrence7ea40902017-02-14 13:32:23 -0800215
216
217def set_dir():
218 # Set working directory for predictable results
219 os.chdir(os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc"))
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800220
221
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800222def gen_policy(syscall_files, name_modifier):
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800223 for arch, header_path, switches in POLICY_CONFIGS:
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800224 base_names = load_syscall_names_from_file(syscall_files["base"], arch)
225 whitelist_names = set()
226 for f in syscall_files["whitelists"]:
227 whitelist_names |= load_syscall_names_from_file(f, arch)
228 blacklist_names = set()
229 for f in syscall_files["blacklists"]:
230 blacklist_names |= load_syscall_names_from_file(f, arch)
231
232 names = merge_names(base_names, whitelist_names, blacklist_names)
233 output = construct_bpf(names, arch, header_path, switches, name_modifier)
Paul Lawrence7ea40902017-02-14 13:32:23 -0800234
235 # And output policy
236 existing = ""
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800237 filename_modifier = "_" + name_modifier if name_modifier else ""
238 output_path = "seccomp/{}{}_policy.cpp".format(arch, filename_modifier)
Paul Lawrence7ea40902017-02-14 13:32:23 -0800239 if os.path.isfile(output_path):
240 existing = open(output_path).read()
241 if output == existing:
242 print "File " + output_path + " not changed."
243 else:
244 with open(output_path, "w") as output_file:
245 output_file.write(output)
246 print "Generated file " + output_path
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800247
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700248
249def main():
250 set_dir()
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800251 gen_policy(ANDROID_SYSTEM_SYSCALL_FILES, 'system')
252 gen_policy(ANDROID_APP_SYSCALL_FILES, 'app')
253 gen_policy(ANDROID_GLOBAL_SYSCALL_FILES, 'global')
254
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700255
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800256if __name__ == "__main__":
257 main()