aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/csource
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2018-05-17 19:57:54 +0200
committerDmitry Vyukov <dvyukov@google.com>2018-05-17 19:57:54 +0200
commit738d58ade0f41fb8ab33066dc0dd486d86383ef5 (patch)
tree1a924306f9af60f1067aa0c9ca0859664d8f3319 /pkg/csource
parent3717901c109694be56bb24593db945e4367ecf14 (diff)
pkg/csource: minimize netdevices and net reset
Add separate options to minimize netdevices setup and net namespace reset. Fixes #581
Diffstat (limited to 'pkg/csource')
-rw-r--r--pkg/csource/common.go9
-rw-r--r--pkg/csource/linux_common.go38
-rw-r--r--pkg/csource/options.go14
-rw-r--r--pkg/csource/options_test.go7
4 files changed, 48 insertions, 20 deletions
diff --git a/pkg/csource/common.go b/pkg/csource/common.go
index 27caac40d..e8fce42a6 100644
--- a/pkg/csource/common.go
+++ b/pkg/csource/common.go
@@ -92,6 +92,12 @@ func defineList(p *prog.Prog, opts Options) ([]string, error) {
if opts.EnableCgroups {
defines = append(defines, "SYZ_ENABLE_CGROUPS")
}
+ if opts.EnableNetdev {
+ defines = append(defines, "SYZ_ENABLE_NETDEV")
+ }
+ if opts.ResetNet {
+ defines = append(defines, "SYZ_RESET_NET_NAMESPACE")
+ }
if opts.UseTmpDir {
defines = append(defines, "SYZ_USE_TMP_DIR")
}
@@ -100,9 +106,6 @@ func defineList(p *prog.Prog, opts Options) ([]string, error) {
}
if opts.WaitRepeat {
defines = append(defines, "SYZ_WAIT_REPEAT")
- // TODO(dvyukov): this should have a separate option,
- // but for now it's bundled with WaitRepeat.
- defines = append(defines, "SYZ_RESET_NET_NAMESPACE")
}
if opts.Debug {
defines = append(defines, "SYZ_DEBUG")
diff --git a/pkg/csource/linux_common.go b/pkg/csource/linux_common.go
index 77ba9b36f..4d168e661 100644
--- a/pkg/csource/linux_common.go
+++ b/pkg/csource/linux_common.go
@@ -60,7 +60,7 @@ var commonHeaderLinux = `
#include <sys/mman.h>
#include <sys/mount.h>
#endif
-#if defined(SYZ_EXECUTOR) || defined(SYZ_TUN_ENABLE)
+#if defined(SYZ_EXECUTOR) || defined(SYZ_TUN_ENABLE) || defined(SYZ_ENABLE_NETDEV)
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
@@ -422,7 +422,7 @@ static void use_temporary_dir()
}
#endif
-#if defined(SYZ_EXECUTOR) || defined(SYZ_TUN_ENABLE)
+#if defined(SYZ_EXECUTOR) || defined(SYZ_TUN_ENABLE) || defined(SYZ_ENABLE_NETDEV)
static void vsnprintf_check(char* str, size_t size, const char* format, va_list args)
{
int rv;
@@ -434,15 +434,6 @@ static void vsnprintf_check(char* str, size_t size, const char* format, va_list
fail("tun: string '%s...' doesn't fit into buffer", str);
}
-static void snprintf_check(char* str, size_t size, const char* format, ...)
-{
- va_list args;
-
- va_start(args, format);
- vsnprintf_check(str, size, format, args);
- va_end(args);
-}
-
#define COMMAND_MAX_LEN 128
#define PATH_PREFIX "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin "
#define PATH_PREFIX_LEN (sizeof(PATH_PREFIX) - 1)
@@ -464,7 +455,9 @@ static void execute_command(bool panic, const char* format, ...)
debug("command '%s': %d\n", &command[0], rv);
}
}
+#endif
+#if defined(SYZ_EXECUTOR) || defined(SYZ_TUN_ENABLE)
static int tunfd = -1;
static int tun_frags_enabled;
@@ -541,11 +534,23 @@ static void initialize_tun(void)
REMOTE_IPV6, REMOTE_MAC, TUN_IFACE);
execute_command(1, "ip link set dev %s up", TUN_IFACE);
}
+#endif
+
+#if defined(SYZ_EXECUTOR) || defined(SYZ_ENABLE_NETDEV)
#define DEV_IPV4 "172.20.20.%d"
#define DEV_IPV6 "fe80::%02hx"
#define DEV_MAC "aa:aa:aa:aa:aa:%02hx"
+static void snprintf_check(char* str, size_t size, const char* format, ...)
+{
+ va_list args;
+
+ va_start(args, format);
+ vsnprintf_check(str, size, format, args);
+ va_end(args);
+}
+
static void initialize_netdevices(void)
{
unsigned i;
@@ -559,10 +564,6 @@ static void initialize_netdevices(void)
"veth0_to_team", "veth1_to_team"};
const char* devmasters[] = {"bridge", "bond", "team"};
-#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 type veth");
@@ -2243,9 +2244,10 @@ static int do_sandbox_none(void)
}
#if defined(SYZ_EXECUTOR) || defined(SYZ_TUN_ENABLE)
initialize_tun();
+#endif
+#if defined(SYZ_EXECUTOR) || defined(SYZ_ENABLE_NETDEV)
initialize_netdevices();
#endif
-
loop();
doexit(1);
}
@@ -2271,6 +2273,8 @@ static int do_sandbox_setuid(void)
fail("unshare(CLONE_NEWNET)");
#if defined(SYZ_EXECUTOR) || defined(SYZ_TUN_ENABLE)
initialize_tun();
+#endif
+#if defined(SYZ_EXECUTOR) || defined(SYZ_ENABLE_NETDEV)
initialize_netdevices();
#endif
@@ -2308,6 +2312,8 @@ static int namespace_sandbox_proc(void* arg)
fail("unshare(CLONE_NEWNET)");
#if defined(SYZ_EXECUTOR) || defined(SYZ_TUN_ENABLE)
initialize_tun();
+#endif
+#if defined(SYZ_EXECUTOR) || defined(SYZ_ENABLE_NETDEV)
initialize_netdevices();
#endif
diff --git a/pkg/csource/options.go b/pkg/csource/options.go
index 7b9ed7050..fd89abcb6 100644
--- a/pkg/csource/options.go
+++ b/pkg/csource/options.go
@@ -27,6 +27,8 @@ type Options struct {
EnableTun bool `json:"tun,omitempty"`
UseTmpDir bool `json:"tmpdir,omitempty"`
EnableCgroups bool `json:"cgroups,omitempty"`
+ EnableNetdev bool `json:"netdev,omitempty"`
+ ResetNet bool `json:"resetnet,omitempty"`
HandleSegv bool `json:"segv,omitempty"`
WaitRepeat bool `json:"waitrepeat,omitempty"`
Debug bool `json:"debug,omitempty"`
@@ -66,6 +68,18 @@ func (opts Options) Check() error {
if opts.EnableCgroups && !opts.UseTmpDir {
return errors.New("EnableCgroups without UseTmpDir")
}
+ if opts.EnableCgroups && !opts.WaitRepeat {
+ return errors.New("EnableCgroups without WaitRepeat")
+ }
+ if opts.EnableNetdev && opts.Sandbox == "" {
+ return errors.New("EnableNetdev without sandbox")
+ }
+ if opts.ResetNet && opts.Sandbox == "" {
+ return errors.New("ResetNet without sandbox")
+ }
+ if opts.ResetNet && !opts.WaitRepeat {
+ return errors.New("ResetNet without WaitRepeat")
+ }
return nil
}
diff --git a/pkg/csource/options_test.go b/pkg/csource/options_test.go
index 9aee780a3..4cccf1c6e 100644
--- a/pkg/csource/options_test.go
+++ b/pkg/csource/options_test.go
@@ -27,7 +27,10 @@ func TestParseOptionsCanned(t *testing.T) {
// so we need to be able to parse old formats.
// nolint: lll
canned := map[string]Options{
- `{"threaded":true,"collide":true,"repeat":true,"procs":10,"sandbox":"namespace","fault":true,"fault_call":1,"fault_nth":2,"tun":true,"tmpdir":true,"cgroups":true,"segv":true,"waitrepeat":true,"debug":true,"repro":true}`: Options{
+ `{"threaded":true,"collide":true,"repeat":true,"procs":10,"sandbox":"namespace",
+ "fault":true,"fault_call":1,"fault_nth":2,"tun":true,"tmpdir":true,"cgroups":true,
+ "netdev":true,"resetnet":true,
+ "segv":true,"waitrepeat":true,"debug":true,"repro":true}`: Options{
Threaded: true,
Collide: true,
Repeat: true,
@@ -39,6 +42,8 @@ func TestParseOptionsCanned(t *testing.T) {
EnableTun: true,
UseTmpDir: true,
EnableCgroups: true,
+ EnableNetdev: true,
+ ResetNet: true,
HandleSegv: true,
WaitRepeat: true,
Debug: true,