2016-11-11 20:33:42 +01:00
|
|
|
#!/usr/bin/env python
|
2018-08-04 05:53:28 +02:00
|
|
|
|
|
|
|
import argparse
|
2017-02-17 19:22:03 +01:00
|
|
|
import collections
|
2018-08-04 05:53:28 +02:00
|
|
|
import logging
|
2016-11-11 20:33:42 +01:00
|
|
|
import os
|
2018-08-03 19:38:41 +02:00
|
|
|
import re
|
2018-08-04 05:53:28 +02:00
|
|
|
import subprocess
|
2016-11-11 20:33:42 +01:00
|
|
|
import textwrap
|
2018-08-04 05:53:28 +02:00
|
|
|
|
2019-09-18 01:37:05 +02:00
|
|
|
from gensyscalls import SupportedArchitectures, SysCallsTxtParser
|
2016-11-11 20:33:42 +01:00
|
|
|
|
|
|
|
|
2017-02-14 22:32:23 +01:00
|
|
|
BPF_JGE = "BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, {0}, {1}, {2})"
|
|
|
|
BPF_ALLOW = "BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW)"
|
2016-11-11 20:33:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SyscallRange(object):
|
|
|
|
def __init__(self, name, value):
|
|
|
|
self.names = [name]
|
|
|
|
self.begin = value
|
|
|
|
self.end = self.begin + 1
|
|
|
|
|
2017-02-14 22:32:23 +01:00
|
|
|
def __str__(self):
|
|
|
|
return "(%s, %s, %s)" % (self.begin, self.end, self.names)
|
|
|
|
|
2016-11-11 20:33:42 +01:00
|
|
|
def add(self, name, value):
|
|
|
|
if value != self.end:
|
|
|
|
raise ValueError
|
|
|
|
self.end += 1
|
|
|
|
self.names.append(name)
|
|
|
|
|
|
|
|
|
2017-12-20 18:19:22 +01:00
|
|
|
def load_syscall_names_from_file(file_path, architecture):
|
|
|
|
parser = SysCallsTxtParser()
|
|
|
|
parser.parse_open_file(open(file_path))
|
|
|
|
return set([x["name"] for x in parser.syscalls if x.get(architecture)])
|
2017-02-17 19:22:03 +01:00
|
|
|
|
|
|
|
|
2017-12-20 18:19:22 +01:00
|
|
|
def merge_names(base_names, whitelist_names, blacklist_names):
|
|
|
|
if bool(blacklist_names - base_names):
|
|
|
|
raise RuntimeError("Blacklist item not in bionic - aborting " + str(
|
2018-08-04 05:53:28 +02:00
|
|
|
blacklist_names - base_names))
|
2017-02-14 22:32:23 +01:00
|
|
|
|
2017-12-20 18:19:22 +01:00
|
|
|
return (base_names - blacklist_names) | whitelist_names
|
2016-11-11 20:33:42 +01:00
|
|
|
|
2017-02-17 19:22:03 +01:00
|
|
|
|
2018-08-04 05:53:28 +02:00
|
|
|
def parse_syscall_NRs(names_path):
|
2016-11-11 20:33:42 +01:00
|
|
|
# The input is now the preprocessed source file. This will contain a lot
|
|
|
|
# of junk from the preprocessor, but our lines will be in the format:
|
|
|
|
#
|
2018-08-04 05:53:28 +02:00
|
|
|
# #define __(ARM_)?NR_${NAME} ${VALUE}
|
|
|
|
#
|
|
|
|
# Where ${VALUE} is a preprocessor expression.
|
2016-11-11 20:33:42 +01:00
|
|
|
|
2018-08-04 05:53:28 +02:00
|
|
|
constant_re = re.compile(
|
|
|
|
r'^\s*#define\s+([A-Za-z_][A-Za-z0-9_]+)\s+(.+)\s*$')
|
|
|
|
token_re = re.compile(r'\b[A-Za-z_][A-Za-z0-9_]+\b')
|
|
|
|
constants = {}
|
|
|
|
with open(names_path) as f:
|
|
|
|
for line in f:
|
|
|
|
m = constant_re.match(line)
|
|
|
|
if not m:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
name = m.group(1)
|
|
|
|
# eval() takes care of any arithmetic that may be done
|
|
|
|
value = eval(token_re.sub(lambda x: str(constants[x.group(0)]),
|
|
|
|
m.group(2)))
|
|
|
|
|
|
|
|
constants[name] = value
|
|
|
|
except:
|
|
|
|
logging.debug('Failed to parse %s', line)
|
|
|
|
pass
|
|
|
|
|
|
|
|
syscalls = {}
|
|
|
|
for name, value in constants.iteritems():
|
|
|
|
if not name.startswith("__NR_") and not name.startswith("__ARM_NR"):
|
2016-11-11 20:33:42 +01:00
|
|
|
continue
|
2018-08-04 05:53:28 +02:00
|
|
|
if name.startswith("__NR_"):
|
|
|
|
# Remote the __NR_ prefix
|
|
|
|
name = name[len("__NR_"):]
|
|
|
|
syscalls[name] = value
|
2016-11-11 20:33:42 +01:00
|
|
|
|
2017-02-14 22:32:23 +01:00
|
|
|
return syscalls
|
|
|
|
|
|
|
|
|
|
|
|
def convert_NRs_to_ranges(syscalls):
|
2016-11-11 20:33:42 +01:00
|
|
|
# Sort the values so we convert to ranges and binary chop
|
|
|
|
syscalls = sorted(syscalls, lambda x, y: cmp(x[1], y[1]))
|
|
|
|
|
|
|
|
# Turn into a list of ranges. Keep the names for the comments
|
|
|
|
ranges = []
|
|
|
|
for name, value in syscalls:
|
|
|
|
if not ranges:
|
|
|
|
ranges.append(SyscallRange(name, value))
|
|
|
|
continue
|
|
|
|
|
|
|
|
last_range = ranges[-1]
|
|
|
|
if last_range.end == value:
|
|
|
|
last_range.add(name, value)
|
|
|
|
else:
|
|
|
|
ranges.append(SyscallRange(name, value))
|
2017-02-14 22:32:23 +01:00
|
|
|
return ranges
|
|
|
|
|
|
|
|
|
|
|
|
# Converts the sorted ranges of allowed syscalls to a binary tree bpf
|
|
|
|
# For a single range, output a simple jump to {fail} or {allow}. We can't set
|
|
|
|
# the jump ranges yet, since we don't know the size of the filter, so use a
|
|
|
|
# placeholder
|
|
|
|
# For multiple ranges, split into two, convert the two halves and output a jump
|
|
|
|
# to the correct half
|
|
|
|
def convert_to_intermediate_bpf(ranges):
|
|
|
|
if len(ranges) == 1:
|
|
|
|
# We will replace {fail} and {allow} with appropriate range jumps later
|
|
|
|
return [BPF_JGE.format(ranges[0].end, "{fail}", "{allow}") +
|
|
|
|
", //" + "|".join(ranges[0].names)]
|
|
|
|
else:
|
|
|
|
half = (len(ranges) + 1) / 2
|
|
|
|
first = convert_to_intermediate_bpf(ranges[:half])
|
|
|
|
second = convert_to_intermediate_bpf(ranges[half:])
|
|
|
|
jump = [BPF_JGE.format(ranges[half].begin, len(first), 0) + ","]
|
|
|
|
return jump + first + second
|
|
|
|
|
2016-11-11 20:33:42 +01:00
|
|
|
|
2017-02-14 22:32:23 +01:00
|
|
|
def convert_ranges_to_bpf(ranges):
|
|
|
|
bpf = convert_to_intermediate_bpf(ranges)
|
2016-11-11 20:33:42 +01:00
|
|
|
|
|
|
|
# Now we know the size of the tree, we can substitute the {fail} and {allow}
|
|
|
|
# placeholders
|
|
|
|
for i, statement in enumerate(bpf):
|
|
|
|
# Replace placeholder with
|
|
|
|
# "distance to jump to fail, distance to jump to allow"
|
|
|
|
# We will add a kill statement and an allow statement after the tree
|
|
|
|
# With bpfs jmp 0 means the next statement, so the distance to the end is
|
|
|
|
# len(bpf) - i - 1, which is where we will put the kill statement, and
|
|
|
|
# then the statement after that is the allow statement
|
|
|
|
if "{fail}" in statement and "{allow}" in statement:
|
2017-01-26 00:20:52 +01:00
|
|
|
bpf[i] = statement.format(fail=str(len(bpf) - i),
|
|
|
|
allow=str(len(bpf) - i - 1))
|
2016-11-11 20:33:42 +01:00
|
|
|
|
2017-01-26 00:20:52 +01:00
|
|
|
# Add the allow calls at the end. If the syscall is not matched, we will
|
|
|
|
# continue. This allows the user to choose to match further syscalls, and
|
|
|
|
# also to choose the action when we want to block
|
2017-02-14 22:32:23 +01:00
|
|
|
bpf.append(BPF_ALLOW + ",")
|
2017-03-22 16:03:51 +01:00
|
|
|
|
|
|
|
# Add check that we aren't off the bottom of the syscalls
|
|
|
|
bpf.insert(0, BPF_JGE.format(ranges[0].begin, 0, str(len(bpf))) + ',')
|
2017-02-14 22:32:23 +01:00
|
|
|
return bpf
|
|
|
|
|
2016-11-11 20:33:42 +01:00
|
|
|
|
2017-12-20 18:19:22 +01:00
|
|
|
def convert_bpf_to_output(bpf, architecture, name_modifier):
|
|
|
|
if name_modifier:
|
|
|
|
name_modifier = name_modifier + "_"
|
|
|
|
else:
|
|
|
|
name_modifier = ""
|
2016-11-11 20:33:42 +01:00
|
|
|
header = textwrap.dedent("""\
|
2018-08-04 05:53:28 +02:00
|
|
|
// File autogenerated by {self_path} - edit at your peril!!
|
2016-11-11 20:33:42 +01:00
|
|
|
|
|
|
|
#include <linux/filter.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2018-08-04 05:53:28 +02:00
|
|
|
#include "seccomp/seccomp_bpfs.h"
|
2017-07-20 22:11:54 +02:00
|
|
|
const sock_filter {architecture}_{suffix}filter[] = {{
|
2018-08-04 05:53:28 +02:00
|
|
|
""").format(self_path=os.path.basename(__file__), architecture=architecture,
|
|
|
|
suffix=name_modifier)
|
2016-11-11 20:33:42 +01:00
|
|
|
|
|
|
|
footer = textwrap.dedent("""\
|
|
|
|
|
|
|
|
}};
|
|
|
|
|
2017-07-20 22:11:54 +02:00
|
|
|
const size_t {architecture}_{suffix}filter_size = sizeof({architecture}_{suffix}filter) / sizeof(struct sock_filter);
|
2017-12-20 18:19:22 +01:00
|
|
|
""").format(architecture=architecture,suffix=name_modifier)
|
2017-02-14 22:32:23 +01:00
|
|
|
return header + "\n".join(bpf) + footer
|
2016-11-11 20:33:42 +01:00
|
|
|
|
|
|
|
|
2018-08-04 05:53:28 +02:00
|
|
|
def construct_bpf(syscalls, architecture, name_modifier):
|
2017-02-14 22:32:23 +01:00
|
|
|
ranges = convert_NRs_to_ranges(syscalls)
|
|
|
|
bpf = convert_ranges_to_bpf(ranges)
|
2017-12-20 18:19:22 +01:00
|
|
|
return convert_bpf_to_output(bpf, architecture, name_modifier)
|
|
|
|
|
|
|
|
|
2018-08-04 05:53:28 +02:00
|
|
|
def gen_policy(name_modifier, out_dir, base_syscall_file, syscall_files, syscall_NRs):
|
2019-09-18 01:37:05 +02:00
|
|
|
for arch in SupportedArchitectures:
|
2018-08-04 05:53:28 +02:00
|
|
|
base_names = load_syscall_names_from_file(base_syscall_file, arch)
|
2017-12-20 18:19:22 +01:00
|
|
|
whitelist_names = set()
|
|
|
|
blacklist_names = set()
|
2018-08-04 05:53:28 +02:00
|
|
|
for f in syscall_files:
|
|
|
|
if "blacklist" in f.lower():
|
|
|
|
blacklist_names |= load_syscall_names_from_file(f, arch)
|
|
|
|
else:
|
|
|
|
whitelist_names |= load_syscall_names_from_file(f, arch)
|
|
|
|
|
|
|
|
allowed_syscalls = []
|
|
|
|
for name in merge_names(base_names, whitelist_names, blacklist_names):
|
|
|
|
try:
|
|
|
|
allowed_syscalls.append((name, syscall_NRs[arch][name]))
|
|
|
|
except:
|
|
|
|
logging.exception("Failed to find %s in %s", name, arch)
|
|
|
|
raise
|
|
|
|
output = construct_bpf(allowed_syscalls, arch, name_modifier)
|
2017-02-14 22:32:23 +01:00
|
|
|
|
|
|
|
# And output policy
|
|
|
|
existing = ""
|
2017-12-20 18:19:22 +01:00
|
|
|
filename_modifier = "_" + name_modifier if name_modifier else ""
|
2018-08-04 05:53:28 +02:00
|
|
|
output_path = os.path.join(out_dir,
|
|
|
|
"{}{}_policy.cpp".format(arch, filename_modifier))
|
|
|
|
with open(output_path, "w") as output_file:
|
|
|
|
output_file.write(output)
|
2016-11-11 20:33:42 +01:00
|
|
|
|
2017-07-20 22:11:54 +02:00
|
|
|
|
|
|
|
def main():
|
2018-08-04 05:53:28 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Generates a seccomp-bpf policy")
|
|
|
|
parser.add_argument("--verbose", "-v", help="Enables verbose logging.")
|
|
|
|
parser.add_argument("--name-modifier",
|
|
|
|
help=("Specifies the name modifier for the policy. "
|
2019-09-18 01:37:05 +02:00
|
|
|
"One of {app,system}."))
|
2018-08-04 05:53:28 +02:00
|
|
|
parser.add_argument("--out-dir",
|
|
|
|
help="The output directory for the policy files")
|
|
|
|
parser.add_argument("base_file", metavar="base-file", type=str,
|
|
|
|
help="The path of the base syscall list (SYSCALLS.TXT).")
|
|
|
|
parser.add_argument("files", metavar="FILE", type=str, nargs="+",
|
|
|
|
help=("The path of the input files. In order to "
|
|
|
|
"simplify the build rules, it can take any of the "
|
|
|
|
"following files: \n"
|
|
|
|
"* /blacklist.*\.txt$/ syscall blacklist.\n"
|
|
|
|
"* /whitelist.*\.txt$/ syscall whitelist.\n"
|
|
|
|
"* otherwise, syscall name-number mapping.\n"))
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.verbose:
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
else:
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
|
|
|
syscall_files = []
|
|
|
|
syscall_NRs = {}
|
|
|
|
for filename in args.files:
|
|
|
|
if filename.lower().endswith('.txt'):
|
|
|
|
syscall_files.append(filename)
|
|
|
|
else:
|
|
|
|
m = re.search(r"libseccomp_gen_syscall_nrs_([^/]+)", filename)
|
|
|
|
syscall_NRs[m.group(1)] = parse_syscall_NRs(filename)
|
|
|
|
|
|
|
|
gen_policy(name_modifier=args.name_modifier, out_dir=args.out_dir,
|
|
|
|
syscall_NRs=syscall_NRs, base_syscall_file=args.base_file,
|
|
|
|
syscall_files=args.files)
|
2017-12-20 18:19:22 +01:00
|
|
|
|
2017-07-20 22:11:54 +02:00
|
|
|
|
2016-11-11 20:33:42 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|