From 1d19aa5799f95ef214351cfe7e94ffe64247dbee Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 26 Feb 2018 17:43:13 +0100 Subject: executor: simplify initialize_tun Remove executor_pid, enable_tun and setup_tun. --- executor/common_linux.h | 51 ++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 24 deletions(-) (limited to 'executor/common_linux.h') diff --git a/executor/common_linux.h b/executor/common_linux.h index faa35877e..1f8305bce 100644 --- a/executor/common_linux.h +++ b/executor/common_linux.h @@ -274,8 +274,6 @@ static int tun_frags_enabled; // Rest of the packet (if any) will be silently truncated which is fine. #define SYZ_TUN_MAX_PACKET_SIZE 1000 -// sysgen knowns about this constant (maxPids) -#define MAX_PIDS 32 #define TUN_IFACE "syz_tun" #define LOCAL_MAC "aa:aa:aa:aa:aa:aa" @@ -294,11 +292,16 @@ static int tun_frags_enabled; #define IFF_NAPI_FRAGS 0x0020 #endif -static void initialize_tun(int id) -{ - if (id >= MAX_PIDS) - fail("tun: no more than %d executors", MAX_PIDS); +#ifdef SYZ_EXECUTOR +extern bool flag_enable_tun; +#endif +static void initialize_tun(void) +{ +#ifdef SYZ_EXECUTOR + if (!flag_enable_tun) + return; +#endif tunfd = open("/dev/net/tun", O_RDWR | O_NONBLOCK); if (tunfd == -1) { #ifdef SYZ_EXECUTOR @@ -358,7 +361,7 @@ static void initialize_tun(int id) // We test in a separate namespace, which does not have any network devices initially (even lo). // Create/up as many as we can. -static void initialize_netdevices(int id) +static void initialize_netdevices(void) { unsigned i; const char* devtypes[] = {"ip6gretap", "bridge", "vcan", "bond", "veth"}; @@ -367,6 +370,10 @@ static void initialize_netdevices(int id) "ip6tnl0", "ip6gre0", "ip6gretap0", "erspan0", "bond0", "veth0", "veth1"}; +#ifdef SYZ_EXECUTOR + if (!flag_enable_tun) + return; +#endif for (i = 0; i < sizeof(devtypes) / (sizeof(devtypes[0])); i++) execute_command(0, "ip link add dev %s0 type %s", devtypes[i], devtypes[i]); execute_command(0, "ip link add dev veth1 type veth"); @@ -384,15 +391,6 @@ static void initialize_netdevices(int id) execute_command(0, "ip link set dev %s up", devnames[i]); } } - -static void setup_tun(uint64 pid, bool enable_tun) -{ - if (enable_tun) { - initialize_tun(pid); - // TODO(dvyukov): this should be separated from tun and minimized by csource separately. - initialize_netdevices(pid); - } -} #endif #if defined(SYZ_EXECUTOR) || (defined(SYZ_TUN_ENABLE) && (defined(__NR_syz_extract_tcp_res) || defined(SYZ_REPEAT) && defined(SYZ_WAIT_REPEAT))) @@ -749,7 +747,7 @@ static void sandbox_common() #endif #if defined(SYZ_EXECUTOR) || defined(SYZ_SANDBOX_NONE) -static int do_sandbox_none(int executor_pid, bool enable_tun) +static int do_sandbox_none(void) { // CLONE_NEWPID takes effect for the first child of the current process, // so we do it before fork to make the loop "init" process of the namespace. @@ -771,7 +769,9 @@ static int do_sandbox_none(int executor_pid, bool enable_tun) debug("unshare(CLONE_NEWNET): %d\n", errno); } #if defined(SYZ_EXECUTOR) || defined(SYZ_TUN_ENABLE) - setup_tun(executor_pid, enable_tun); + initialize_tun(); + // TODO(dvyukov): this should be separated from tun and minimized by csource separately. + initialize_netdevices(); #endif loop(); @@ -780,7 +780,7 @@ static int do_sandbox_none(int executor_pid, bool enable_tun) #endif #if defined(SYZ_EXECUTOR) || defined(SYZ_SANDBOX_SETUID) -static int do_sandbox_setuid(int executor_pid, bool enable_tun) +static int do_sandbox_setuid(void) { if (unshare(CLONE_NEWPID)) fail("unshare(CLONE_NEWPID)"); @@ -794,7 +794,9 @@ static int do_sandbox_setuid(int executor_pid, bool enable_tun) if (unshare(CLONE_NEWNET)) fail("unshare(CLONE_NEWNET)"); #if defined(SYZ_EXECUTOR) || defined(SYZ_TUN_ENABLE) - setup_tun(executor_pid, enable_tun); + initialize_tun(); + // TODO(dvyukov): this should be separated from tun and minimized by csource separately. + initialize_netdevices(); #endif const int nobody = 65534; @@ -863,7 +865,9 @@ static int namespace_sandbox_proc(void* arg) // which in turn needs to be in the test user namespace. // However, IFF_NAPI_FRAGS will fail as we are not root already. // There does not seem to be a call sequence that would satisfy all of that. - setup_tun((long)arg >> 1, (long)arg & 1); + initialize_tun(); + // TODO(dvyukov): this should be separated from tun and minimized by csource separately. + initialize_netdevices(); #endif if (mkdir("./syz-tmp", 0777)) @@ -929,16 +933,15 @@ static int namespace_sandbox_proc(void* arg) doexit(1); } -static int do_sandbox_namespace(int executor_pid, bool enable_tun) +static int do_sandbox_namespace(void) { int pid; real_uid = getuid(); real_gid = getgid(); mprotect(sandbox_stack, 4096, PROT_NONE); // to catch stack underflows - void* arg = (void*)(long)((executor_pid << 1) | enable_tun); pid = clone(namespace_sandbox_proc, &sandbox_stack[sizeof(sandbox_stack) - 64], - CLONE_NEWUSER | CLONE_NEWPID, arg); + CLONE_NEWUSER | CLONE_NEWPID, 0); if (pid < 0) fail("sandbox clone failed"); return pid; -- cgit mrf-deployment