blob: a8e551ed24b20a28cade148e8bcb828660f046a9 [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
Paul Lawrence7ea40902017-02-14 13:32:23 -080029def get_names(syscall_files, architecture):
30 syscalls = []
31 for syscall_file in syscall_files:
32 parser = SysCallsTxtParser()
33 parser.parse_open_file(syscall_file)
34 syscalls += parser.syscalls
Paul Lawrenceeabc3522016-11-11 11:33:42 -080035
36 # Select only elements matching required architecture
37 syscalls = [x for x in syscalls if architecture in x and x[architecture]]
38
39 # We only want the name
Paul Lawrence89fa81f2017-02-17 10:22:03 -080040 names = [x["name"] for x in syscalls]
41
42 # Check for duplicates
43 dups = [name for name, count in collections.Counter(names).items() if count > 1]
44
45 # x86 has duplicate socketcall entries, so hard code for this
46 if architecture == "x86":
47 dups.remove("socketcall")
48
49 if len(dups) > 0:
50 print "Duplicate entries found - aborting ", dups
51 exit(-1)
52
53 # Remove remaining duplicates
54 return list(set(names))
Paul Lawrenceeabc3522016-11-11 11:33:42 -080055
Paul Lawrence7ea40902017-02-14 13:32:23 -080056
Paul Lawrence89fa81f2017-02-17 10:22:03 -080057def convert_names_to_NRs(names, header_dir, extra_switches):
Paul Lawrenceeabc3522016-11-11 11:33:42 -080058 # Run preprocessor over the __NR_syscall symbols, including unistd.h,
59 # to get the actual numbers
60 prefix = "__SECCOMP_" # prefix to ensure no name collisions
61 cpp = Popen(["../../prebuilts/clang/host/linux-x86/clang-stable/bin/clang",
Paul Lawrence89fa81f2017-02-17 10:22:03 -080062 "-E", "-nostdinc", "-I" + header_dir, "-Ikernel/uapi/"]
63 + extra_switches
64 + ["-"],
Paul Lawrenceeabc3522016-11-11 11:33:42 -080065 stdin=PIPE, stdout=PIPE)
66 cpp.stdin.write("#include <asm/unistd.h>\n")
67 for name in names:
68 # In SYSCALLS.TXT, there are two arm-specific syscalls whose names start
69 # with __ARM__NR_. These we must simply write out as is.
70 if not name.startswith("__ARM_NR_"):
71 cpp.stdin.write(prefix + name + ", __NR_" + name + "\n")
72 else:
73 cpp.stdin.write(prefix + name + ", " + name + "\n")
74 content = cpp.communicate()[0].split("\n")
75
76 # The input is now the preprocessed source file. This will contain a lot
77 # of junk from the preprocessor, but our lines will be in the format:
78 #
79 # __SECCOMP_${NAME}, (0 + value)
80
81 syscalls = []
82 for line in content:
83 if not line.startswith(prefix):
84 continue
85
86 # We might pick up extra whitespace during preprocessing, so best to strip.
87 name, value = [w.strip() for w in line.split(",")]
88 name = name[len(prefix):]
89
90 # Note that some of the numbers were expressed as base + offset, so we
91 # need to eval, not just int
92 value = eval(value)
93 syscalls.append((name, value))
94
Paul Lawrence7ea40902017-02-14 13:32:23 -080095 return syscalls
96
97
98def convert_NRs_to_ranges(syscalls):
Paul Lawrenceeabc3522016-11-11 11:33:42 -080099 # Sort the values so we convert to ranges and binary chop
100 syscalls = sorted(syscalls, lambda x, y: cmp(x[1], y[1]))
101
102 # Turn into a list of ranges. Keep the names for the comments
103 ranges = []
104 for name, value in syscalls:
105 if not ranges:
106 ranges.append(SyscallRange(name, value))
107 continue
108
109 last_range = ranges[-1]
110 if last_range.end == value:
111 last_range.add(name, value)
112 else:
113 ranges.append(SyscallRange(name, value))
Paul Lawrence7ea40902017-02-14 13:32:23 -0800114 return ranges
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800115
Paul Lawrence7ea40902017-02-14 13:32:23 -0800116
117# Converts the sorted ranges of allowed syscalls to a binary tree bpf
118# For a single range, output a simple jump to {fail} or {allow}. We can't set
119# the jump ranges yet, since we don't know the size of the filter, so use a
120# placeholder
121# For multiple ranges, split into two, convert the two halves and output a jump
122# to the correct half
123def convert_to_intermediate_bpf(ranges):
124 if len(ranges) == 1:
125 # We will replace {fail} and {allow} with appropriate range jumps later
126 return [BPF_JGE.format(ranges[0].end, "{fail}", "{allow}") +
127 ", //" + "|".join(ranges[0].names)]
128 else:
129 half = (len(ranges) + 1) / 2
130 first = convert_to_intermediate_bpf(ranges[:half])
131 second = convert_to_intermediate_bpf(ranges[half:])
132 jump = [BPF_JGE.format(ranges[half].begin, len(first), 0) + ","]
133 return jump + first + second
134
135
136def convert_ranges_to_bpf(ranges):
137 bpf = convert_to_intermediate_bpf(ranges)
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800138
139 # Now we know the size of the tree, we can substitute the {fail} and {allow}
140 # placeholders
141 for i, statement in enumerate(bpf):
142 # Replace placeholder with
143 # "distance to jump to fail, distance to jump to allow"
144 # We will add a kill statement and an allow statement after the tree
145 # With bpfs jmp 0 means the next statement, so the distance to the end is
146 # len(bpf) - i - 1, which is where we will put the kill statement, and
147 # then the statement after that is the allow statement
148 if "{fail}" in statement and "{allow}" in statement:
Paul Lawrencebe8a2af2017-01-25 15:20:52 -0800149 bpf[i] = statement.format(fail=str(len(bpf) - i),
150 allow=str(len(bpf) - i - 1))
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800151
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800152
Paul Lawrencebe8a2af2017-01-25 15:20:52 -0800153 # Add the allow calls at the end. If the syscall is not matched, we will
154 # continue. This allows the user to choose to match further syscalls, and
155 # also to choose the action when we want to block
Paul Lawrence7ea40902017-02-14 13:32:23 -0800156 bpf.append(BPF_ALLOW + ",")
Paul Lawrence65b47c92017-03-22 08:03:51 -0700157
158 # Add check that we aren't off the bottom of the syscalls
159 bpf.insert(0, BPF_JGE.format(ranges[0].begin, 0, str(len(bpf))) + ',')
Paul Lawrence7ea40902017-02-14 13:32:23 -0800160 return bpf
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800161
Paul Lawrence7ea40902017-02-14 13:32:23 -0800162
163def convert_bpf_to_output(bpf, architecture):
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800164 header = textwrap.dedent("""\
165 // Autogenerated file - edit at your peril!!
166
167 #include <linux/filter.h>
168 #include <errno.h>
169
Paul Lawrencedfe84342017-02-16 09:24:39 -0800170 #include "seccomp_bpfs.h"
171 const sock_filter {architecture}_filter[] = {{
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800172 """).format(architecture=architecture)
173
174 footer = textwrap.dedent("""\
175
176 }};
177
178 const size_t {architecture}_filter_size = sizeof({architecture}_filter) / sizeof(struct sock_filter);
179 """).format(architecture=architecture)
Paul Lawrence7ea40902017-02-14 13:32:23 -0800180 return header + "\n".join(bpf) + footer
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800181
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800182
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800183def construct_bpf(syscall_files, architecture, header_dir, extra_switches):
Paul Lawrence7ea40902017-02-14 13:32:23 -0800184 names = get_names(syscall_files, architecture)
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800185 syscalls = convert_names_to_NRs(names, header_dir, extra_switches)
Paul Lawrence7ea40902017-02-14 13:32:23 -0800186 ranges = convert_NRs_to_ranges(syscalls)
187 bpf = convert_ranges_to_bpf(ranges)
188 return convert_bpf_to_output(bpf, architecture)
189
190
Paul Lawrence7ea40902017-02-14 13:32:23 -0800191ANDROID_SYSCALL_FILES = ["SYSCALLS.TXT", "SECCOMP_WHITELIST.TXT"]
192
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800193
194POLICY_CONFIGS = [("arm", "kernel/uapi/asm-arm", []),
195 ("arm64", "kernel/uapi/asm-arm64", []),
196 ("x86", "kernel/uapi/asm-x86", ["-D__i386__"]),
197 ("x86_64", "kernel/uapi/asm-x86", []),
198 ("mips", "kernel/uapi/asm-mips", ["-D_MIPS_SIM=_MIPS_SIM_ABI32"]),
199 ("mips64", "kernel/uapi/asm-mips", ["-D_MIPS_SIM=_MIPS_SIM_ABI64"])]
Paul Lawrence7ea40902017-02-14 13:32:23 -0800200
201
202def set_dir():
203 # Set working directory for predictable results
204 os.chdir(os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc"))
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800205
206
207def main():
Paul Lawrence7ea40902017-02-14 13:32:23 -0800208 set_dir()
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800209 for arch, header_path, switches in POLICY_CONFIGS:
Paul Lawrence7ea40902017-02-14 13:32:23 -0800210 files = [open(filename) for filename in ANDROID_SYSCALL_FILES]
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800211 output = construct_bpf(files, arch, header_path, switches)
Paul Lawrence7ea40902017-02-14 13:32:23 -0800212
213 # And output policy
214 existing = ""
Paul Lawrencedfe84342017-02-16 09:24:39 -0800215 output_path = "seccomp/{}_policy.cpp".format(arch)
Paul Lawrence7ea40902017-02-14 13:32:23 -0800216 if os.path.isfile(output_path):
217 existing = open(output_path).read()
218 if output == existing:
219 print "File " + output_path + " not changed."
220 else:
221 with open(output_path, "w") as output_file:
222 output_file.write(output)
223 print "Generated file " + output_path
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800224
Paul Lawrenceeabc3522016-11-11 11:33:42 -0800225if __name__ == "__main__":
226 main()