Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 2 | import collections |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 3 | import os |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 4 | import textwrap |
| 5 | from gensyscalls import SysCallsTxtParser |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 6 | from subprocess import Popen, PIPE |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 7 | |
| 8 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 9 | BPF_JGE = "BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, {0}, {1}, {2})" |
| 10 | BPF_ALLOW = "BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW)" |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 11 | |
| 12 | |
| 13 | class SyscallRange(object): |
| 14 | def __init__(self, name, value): |
| 15 | self.names = [name] |
| 16 | self.begin = value |
| 17 | self.end = self.begin + 1 |
| 18 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 19 | def __str__(self): |
| 20 | return "(%s, %s, %s)" % (self.begin, self.end, self.names) |
| 21 | |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 22 | 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 Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 29 | def 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 Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 35 | |
| 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 Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 40 | 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 Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 55 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 56 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 57 | def convert_names_to_NRs(names, header_dir, extra_switches): |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 58 | # 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 Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 62 | "-E", "-nostdinc", "-I" + header_dir, "-Ikernel/uapi/"] |
| 63 | + extra_switches |
| 64 | + ["-"], |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 65 | 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 Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 95 | return syscalls |
| 96 | |
| 97 | |
| 98 | def convert_NRs_to_ranges(syscalls): |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 99 | # 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 Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 114 | return ranges |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 115 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 116 | |
| 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 |
| 123 | def 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 | |
| 136 | def convert_ranges_to_bpf(ranges): |
| 137 | bpf = convert_to_intermediate_bpf(ranges) |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 138 | |
| 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 Lawrence | be8a2af | 2017-01-25 15:20:52 -0800 | [diff] [blame] | 149 | bpf[i] = statement.format(fail=str(len(bpf) - i), |
| 150 | allow=str(len(bpf) - i - 1)) |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 151 | |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 152 | |
Paul Lawrence | be8a2af | 2017-01-25 15:20:52 -0800 | [diff] [blame] | 153 | # 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 Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 156 | bpf.append(BPF_ALLOW + ",") |
Paul Lawrence | 65b47c9 | 2017-03-22 08:03:51 -0700 | [diff] [blame^] | 157 | |
| 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 Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 160 | return bpf |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 161 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 162 | |
| 163 | def convert_bpf_to_output(bpf, architecture): |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 164 | header = textwrap.dedent("""\ |
| 165 | // Autogenerated file - edit at your peril!! |
| 166 | |
| 167 | #include <linux/filter.h> |
| 168 | #include <errno.h> |
| 169 | |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 170 | #include "seccomp_bpfs.h" |
| 171 | const sock_filter {architecture}_filter[] = {{ |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 172 | """).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 Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 180 | return header + "\n".join(bpf) + footer |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 181 | |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 182 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 183 | def construct_bpf(syscall_files, architecture, header_dir, extra_switches): |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 184 | names = get_names(syscall_files, architecture) |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 185 | syscalls = convert_names_to_NRs(names, header_dir, extra_switches) |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 186 | ranges = convert_NRs_to_ranges(syscalls) |
| 187 | bpf = convert_ranges_to_bpf(ranges) |
| 188 | return convert_bpf_to_output(bpf, architecture) |
| 189 | |
| 190 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 191 | ANDROID_SYSCALL_FILES = ["SYSCALLS.TXT", "SECCOMP_WHITELIST.TXT"] |
| 192 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 193 | |
| 194 | POLICY_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 Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 200 | |
| 201 | |
| 202 | def set_dir(): |
| 203 | # Set working directory for predictable results |
| 204 | os.chdir(os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc")) |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 205 | |
| 206 | |
| 207 | def main(): |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 208 | set_dir() |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 209 | for arch, header_path, switches in POLICY_CONFIGS: |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 210 | files = [open(filename) for filename in ANDROID_SYSCALL_FILES] |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 211 | output = construct_bpf(files, arch, header_path, switches) |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 212 | |
| 213 | # And output policy |
| 214 | existing = "" |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 215 | output_path = "seccomp/{}_policy.cpp".format(arch) |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 216 | 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 Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 224 | |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 225 | if __name__ == "__main__": |
| 226 | main() |