Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | import os |
| 3 | from subprocess import Popen, PIPE |
| 4 | import textwrap |
| 5 | from gensyscalls import SysCallsTxtParser |
| 6 | |
| 7 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 8 | BPF_JGE = "BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, {0}, {1}, {2})" |
| 9 | BPF_ALLOW = "BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW)" |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 10 | |
| 11 | |
| 12 | class SyscallRange(object): |
| 13 | def __init__(self, name, value): |
| 14 | self.names = [name] |
| 15 | self.begin = value |
| 16 | self.end = self.begin + 1 |
| 17 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 18 | def __str__(self): |
| 19 | return "(%s, %s, %s)" % (self.begin, self.end, self.names) |
| 20 | |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 21 | def add(self, name, value): |
| 22 | if value != self.end: |
| 23 | raise ValueError |
| 24 | self.end += 1 |
| 25 | self.names.append(name) |
| 26 | |
| 27 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 28 | def get_names(syscall_files, architecture): |
| 29 | syscalls = [] |
| 30 | for syscall_file in syscall_files: |
| 31 | parser = SysCallsTxtParser() |
| 32 | parser.parse_open_file(syscall_file) |
| 33 | syscalls += parser.syscalls |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 34 | |
| 35 | # Select only elements matching required architecture |
| 36 | syscalls = [x for x in syscalls if architecture in x and x[architecture]] |
| 37 | |
| 38 | # We only want the name |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 39 | return [x["name"] for x in syscalls] |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 40 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 41 | |
| 42 | def convert_names_to_NRs(names, header_dir): |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 43 | # Run preprocessor over the __NR_syscall symbols, including unistd.h, |
| 44 | # to get the actual numbers |
| 45 | prefix = "__SECCOMP_" # prefix to ensure no name collisions |
| 46 | cpp = Popen(["../../prebuilts/clang/host/linux-x86/clang-stable/bin/clang", |
| 47 | "-E", "-nostdinc", "-I" + header_dir, "-Ikernel/uapi/", "-"], |
| 48 | stdin=PIPE, stdout=PIPE) |
| 49 | cpp.stdin.write("#include <asm/unistd.h>\n") |
| 50 | for name in names: |
| 51 | # In SYSCALLS.TXT, there are two arm-specific syscalls whose names start |
| 52 | # with __ARM__NR_. These we must simply write out as is. |
| 53 | if not name.startswith("__ARM_NR_"): |
| 54 | cpp.stdin.write(prefix + name + ", __NR_" + name + "\n") |
| 55 | else: |
| 56 | cpp.stdin.write(prefix + name + ", " + name + "\n") |
| 57 | content = cpp.communicate()[0].split("\n") |
| 58 | |
| 59 | # The input is now the preprocessed source file. This will contain a lot |
| 60 | # of junk from the preprocessor, but our lines will be in the format: |
| 61 | # |
| 62 | # __SECCOMP_${NAME}, (0 + value) |
| 63 | |
| 64 | syscalls = [] |
| 65 | for line in content: |
| 66 | if not line.startswith(prefix): |
| 67 | continue |
| 68 | |
| 69 | # We might pick up extra whitespace during preprocessing, so best to strip. |
| 70 | name, value = [w.strip() for w in line.split(",")] |
| 71 | name = name[len(prefix):] |
| 72 | |
| 73 | # Note that some of the numbers were expressed as base + offset, so we |
| 74 | # need to eval, not just int |
| 75 | value = eval(value) |
| 76 | syscalls.append((name, value)) |
| 77 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 78 | return syscalls |
| 79 | |
| 80 | |
| 81 | def convert_NRs_to_ranges(syscalls): |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 82 | # Sort the values so we convert to ranges and binary chop |
| 83 | syscalls = sorted(syscalls, lambda x, y: cmp(x[1], y[1])) |
| 84 | |
| 85 | # Turn into a list of ranges. Keep the names for the comments |
| 86 | ranges = [] |
| 87 | for name, value in syscalls: |
| 88 | if not ranges: |
| 89 | ranges.append(SyscallRange(name, value)) |
| 90 | continue |
| 91 | |
| 92 | last_range = ranges[-1] |
| 93 | if last_range.end == value: |
| 94 | last_range.add(name, value) |
| 95 | else: |
| 96 | ranges.append(SyscallRange(name, value)) |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 97 | return ranges |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 98 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 99 | |
| 100 | # Converts the sorted ranges of allowed syscalls to a binary tree bpf |
| 101 | # For a single range, output a simple jump to {fail} or {allow}. We can't set |
| 102 | # the jump ranges yet, since we don't know the size of the filter, so use a |
| 103 | # placeholder |
| 104 | # For multiple ranges, split into two, convert the two halves and output a jump |
| 105 | # to the correct half |
| 106 | def convert_to_intermediate_bpf(ranges): |
| 107 | if len(ranges) == 1: |
| 108 | # We will replace {fail} and {allow} with appropriate range jumps later |
| 109 | return [BPF_JGE.format(ranges[0].end, "{fail}", "{allow}") + |
| 110 | ", //" + "|".join(ranges[0].names)] |
| 111 | else: |
| 112 | half = (len(ranges) + 1) / 2 |
| 113 | first = convert_to_intermediate_bpf(ranges[:half]) |
| 114 | second = convert_to_intermediate_bpf(ranges[half:]) |
| 115 | jump = [BPF_JGE.format(ranges[half].begin, len(first), 0) + ","] |
| 116 | return jump + first + second |
| 117 | |
| 118 | |
| 119 | def convert_ranges_to_bpf(ranges): |
| 120 | bpf = convert_to_intermediate_bpf(ranges) |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 121 | |
| 122 | # Now we know the size of the tree, we can substitute the {fail} and {allow} |
| 123 | # placeholders |
| 124 | for i, statement in enumerate(bpf): |
| 125 | # Replace placeholder with |
| 126 | # "distance to jump to fail, distance to jump to allow" |
| 127 | # We will add a kill statement and an allow statement after the tree |
| 128 | # With bpfs jmp 0 means the next statement, so the distance to the end is |
| 129 | # len(bpf) - i - 1, which is where we will put the kill statement, and |
| 130 | # then the statement after that is the allow statement |
| 131 | if "{fail}" in statement and "{allow}" in statement: |
Paul Lawrence | be8a2af | 2017-01-25 15:20:52 -0800 | [diff] [blame] | 132 | bpf[i] = statement.format(fail=str(len(bpf) - i), |
| 133 | allow=str(len(bpf) - i - 1)) |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 134 | |
| 135 | # Add check that we aren't off the bottom of the syscalls |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 136 | bpf.insert(0, BPF_JGE.format(ranges[0].begin, 0, str(len(bpf))) + ',') |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 137 | |
Paul Lawrence | be8a2af | 2017-01-25 15:20:52 -0800 | [diff] [blame] | 138 | # Add the allow calls at the end. If the syscall is not matched, we will |
| 139 | # continue. This allows the user to choose to match further syscalls, and |
| 140 | # also to choose the action when we want to block |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 141 | bpf.append(BPF_ALLOW + ",") |
| 142 | return bpf |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 143 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 144 | |
| 145 | def convert_bpf_to_output(bpf, architecture): |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 146 | header = textwrap.dedent("""\ |
| 147 | // Autogenerated file - edit at your peril!! |
| 148 | |
| 149 | #include <linux/filter.h> |
| 150 | #include <errno.h> |
| 151 | |
| 152 | #include "seccomp_policy.h" |
| 153 | const struct sock_filter {architecture}_filter[] = {{ |
| 154 | """).format(architecture=architecture) |
| 155 | |
| 156 | footer = textwrap.dedent("""\ |
| 157 | |
| 158 | }}; |
| 159 | |
| 160 | const size_t {architecture}_filter_size = sizeof({architecture}_filter) / sizeof(struct sock_filter); |
| 161 | """).format(architecture=architecture) |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 162 | return header + "\n".join(bpf) + footer |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 163 | |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 164 | |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 165 | def construct_bpf(syscall_files, architecture, header_dir): |
| 166 | names = get_names(syscall_files, architecture) |
| 167 | syscalls = convert_names_to_NRs(names, header_dir) |
| 168 | ranges = convert_NRs_to_ranges(syscalls) |
| 169 | bpf = convert_ranges_to_bpf(ranges) |
| 170 | return convert_bpf_to_output(bpf, architecture) |
| 171 | |
| 172 | |
| 173 | android_syscall_files = ["SYSCALLS.TXT", "SECCOMP_WHITELIST.TXT"] |
| 174 | arm_headers = "kernel/uapi/asm-arm" |
| 175 | arm64_headers = "kernel/uapi/asm-arm64" |
| 176 | arm_architecture = "arm" |
| 177 | arm64_architecture = "arm64" |
| 178 | |
| 179 | |
| 180 | ANDROID_SYSCALL_FILES = ["SYSCALLS.TXT", "SECCOMP_WHITELIST.TXT"] |
| 181 | |
| 182 | POLICY_CONFIGS = [("arm", "kernel/uapi/asm-arm"), |
| 183 | ("arm64", "kernel/uapi/asm-arm64")] |
| 184 | |
| 185 | |
| 186 | def set_dir(): |
| 187 | # Set working directory for predictable results |
| 188 | os.chdir(os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc")) |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 189 | |
| 190 | |
| 191 | def main(): |
Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame^] | 192 | set_dir() |
| 193 | for arch, header_path in POLICY_CONFIGS: |
| 194 | files = [open(filename) for filename in ANDROID_SYSCALL_FILES] |
| 195 | output = construct_bpf(files, arch, header_path) |
| 196 | |
| 197 | # And output policy |
| 198 | existing = "" |
| 199 | output_path = "seccomp/{}_policy.c".format(arch) |
| 200 | if os.path.isfile(output_path): |
| 201 | existing = open(output_path).read() |
| 202 | if output == existing: |
| 203 | print "File " + output_path + " not changed." |
| 204 | else: |
| 205 | with open(output_path, "w") as output_file: |
| 206 | output_file.write(output) |
| 207 | print "Generated file " + output_path |
Paul Lawrence | eabc352 | 2016-11-11 11:33:42 -0800 | [diff] [blame] | 208 | |
| 209 | |
| 210 | if __name__ == "__main__": |
| 211 | main() |