aboutsummaryrefslogtreecommitdiffstats
path: root/executor
diff options
context:
space:
mode:
Diffstat (limited to 'executor')
-rw-r--r--executor/common_linux.h106
-rw-r--r--executor/executor.cc2
2 files changed, 104 insertions, 4 deletions
diff --git a/executor/common_linux.h b/executor/common_linux.h
index 1c0f51b5e..0b7e15a63 100644
--- a/executor/common_linux.h
+++ b/executor/common_linux.h
@@ -106,7 +106,7 @@ static bool write_file(const char* file, const char* what, ...)
}
#endif
-#if SYZ_EXECUTOR || SYZ_ENABLE_NETDEV || SYZ_TUN_ENABLE
+#if SYZ_EXECUTOR || SYZ_ENABLE_NETDEV || SYZ_TUN_ENABLE || SYZ_ENABLE_DEVLINK_PCI
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
@@ -165,7 +165,7 @@ static void netlink_done(void)
}
#endif
-static int netlink_send(int sock)
+static int netlink_send_ext(int sock, uint16 reply_type, int* reply_len)
{
if (nlmsg.pos > nlmsg.buf + sizeof(nlmsg.buf) || nlmsg.nesting)
fail("nlmsg overflow/bad nesting");
@@ -178,6 +178,12 @@ static int netlink_send(int sock)
if (n != hdr->nlmsg_len)
fail("short netlink write: %d/%d", n, hdr->nlmsg_len);
n = recv(sock, nlmsg.buf, sizeof(nlmsg.buf), 0);
+ if (n < sizeof(struct nlmsghdr))
+ fail("short netlink read: %d", n);
+ if (reply_len && hdr->nlmsg_type == reply_type) {
+ *reply_len = n;
+ return 0;
+ }
if (n < sizeof(struct nlmsghdr) + sizeof(struct nlmsgerr))
fail("short netlink read: %d", n);
if (hdr->nlmsg_type != NLMSG_ERROR)
@@ -185,6 +191,11 @@ static int netlink_send(int sock)
return -((struct nlmsgerr*)(hdr + 1))->error;
}
+static int netlink_send(int sock)
+{
+ return netlink_send_ext(sock, 0, NULL);
+}
+
#if SYZ_EXECUTOR || SYZ_ENABLE_NETDEV
static void netlink_add_device_impl(const char* type, const char* name)
{
@@ -238,6 +249,7 @@ static void netlink_add_hsr(int sock, const char* name, const char* slave1, cons
}
#endif
+#if SYZ_EXECUTOR || SYZ_ENABLE_NETDEV || SYZ_TUN_ENABLE
static void netlink_device_change(int sock, const char* name, bool up,
const char* master, const void* mac, int macsize)
{
@@ -289,6 +301,7 @@ static void netlink_add_addr6(int sock, const char* dev, const char* addr)
debug("netlink: add addr %s dev %s: %s\n", addr, dev, strerror(err));
(void)err;
}
+#endif
#if SYZ_EXECUTOR || SYZ_TUN_ENABLE
static void netlink_add_neigh(int sock, const char* name,
@@ -422,6 +435,83 @@ static void initialize_tun(void)
}
#endif
+#if SYZ_EXECUTOR || __NR_syz_init_net_socket || SYZ_ENABLE_DEVLINK_PCI
+const int kInitNetNsFd = 239; // see kMaxFd
+#endif
+
+#if SYZ_EXECUTOR || SYZ_ENABLE_DEVLINK_PCI
+
+#include <linux/genetlink.h>
+
+#define DEVLINK_FAMILY_NAME "devlink"
+
+#define DEVLINK_CMD_RELOAD 37
+#define DEVLINK_ATTR_BUS_NAME 1
+#define DEVLINK_ATTR_DEV_NAME 2
+#define DEVLINK_ATTR_NETNS_FD 137
+
+static void netlink_devlink_netns_move(const char* bus_name, const char* dev_name, int netns_fd)
+{
+ struct genlmsghdr genlhdr;
+ struct nlattr* attr;
+ int sock, err, n;
+ uint16 id = 0;
+
+ sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
+ if (sock == -1)
+ fail("socket(AF_NETLINK) failed\n");
+ memset(&genlhdr, 0, sizeof(genlhdr));
+ genlhdr.cmd = CTRL_CMD_GETFAMILY;
+ netlink_init(GENL_ID_CTRL, 0, &genlhdr, sizeof(genlhdr));
+ netlink_attr(CTRL_ATTR_FAMILY_NAME, DEVLINK_FAMILY_NAME, strlen(DEVLINK_FAMILY_NAME) + 1);
+ err = netlink_send_ext(sock, GENL_ID_CTRL, &n);
+ if (err) {
+ debug("netlink: failed to get devlink family id: %s\n", strerror(err));
+ goto error;
+ }
+ attr = (struct nlattr*)(nlmsg.buf + NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(genlhdr)));
+ for (; (char*)attr < nlmsg.buf + n; attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) {
+ if (attr->nla_type == CTRL_ATTR_FAMILY_ID) {
+ id = *(uint16*)(attr + 1);
+ break;
+ }
+ }
+ if (!id) {
+ debug("netlink: failed to parse message for devlink family id\n");
+ goto error;
+ }
+ recv(sock, nlmsg.buf, sizeof(nlmsg.buf), 0); /* recv ack */
+ memset(&genlhdr, 0, sizeof(genlhdr));
+ genlhdr.cmd = DEVLINK_CMD_RELOAD;
+ netlink_init(id, 0, &genlhdr, sizeof(genlhdr));
+ netlink_attr(DEVLINK_ATTR_BUS_NAME, bus_name, strlen(bus_name) + 1);
+ netlink_attr(DEVLINK_ATTR_DEV_NAME, dev_name, strlen(dev_name) + 1);
+ netlink_attr(DEVLINK_ATTR_NETNS_FD, &netns_fd, sizeof(netns_fd));
+ netlink_send(sock);
+error:
+ close(sock);
+}
+
+static void initialize_devlink_pci(void)
+{
+#if SYZ_EXECUTOR
+ if (!flag_enable_devlink_pci)
+ return;
+#endif
+ int netns = open("/proc/self/ns/net", O_RDONLY);
+ if (netns == -1)
+ fail("open(/proc/self/ns/net) failed");
+ int ret = setns(kInitNetNsFd, 0);
+ if (ret == -1)
+ fail("set_ns(init_netns_fd) failed");
+ netlink_devlink_netns_move("pci", "0000:00:10.0", netns);
+ ret = setns(netns, 0);
+ if (ret == -1)
+ fail("set_ns(this_netns_fd) failed");
+ close(netns);
+}
+#endif
+
#if SYZ_EXECUTOR || SYZ_ENABLE_NETDEV
#include <arpa/inet.h>
#include <errno.h>
@@ -909,7 +999,6 @@ static long syz_open_pts(volatile long a0, volatile long a1)
#include <sys/types.h>
#include <unistd.h>
-const int kInitNetNsFd = 239; // see kMaxFd
// syz_init_net_socket opens a socket in init net namespace.
// Used for families that can only be created in init net namespace.
static long syz_init_net_socket(volatile long domain, volatile long type, volatile long proto)
@@ -1911,7 +2000,7 @@ static void sandbox_common()
setpgrp();
setsid();
-#if SYZ_EXECUTOR || __NR_syz_init_net_socket
+#if SYZ_EXECUTOR || __NR_syz_init_net_socket || SYZ_ENABLE_DEVLINK_PCI
int netns = open("/proc/self/ns/net", O_RDONLY);
if (netns == -1)
fail("open(/proc/self/ns/net) failed");
@@ -2049,6 +2138,9 @@ static int do_sandbox_none(void)
if (unshare(CLONE_NEWNET)) {
debug("unshare(CLONE_NEWNET): %d\n", errno);
}
+#if SYZ_EXECUTOR || SYZ_ENABLE_DEVLINK_PCI
+ initialize_devlink_pci();
+#endif
#if SYZ_EXECUTOR || SYZ_TUN_ENABLE
initialize_tun();
#endif
@@ -2083,6 +2175,9 @@ static int do_sandbox_setuid(void)
if (unshare(CLONE_NEWNET)) {
debug("unshare(CLONE_NEWNET): %d\n", errno);
}
+#if SYZ_EXECUTOR || SYZ_ENABLE_DEVLINK_PCI
+ initialize_devlink_pci();
+#endif
#if SYZ_EXECUTOR || SYZ_TUN_ENABLE
initialize_tun();
#endif
@@ -2135,6 +2230,9 @@ static int namespace_sandbox_proc(void* arg)
// because we want the tun device in the test namespace.
if (unshare(CLONE_NEWNET))
fail("unshare(CLONE_NEWNET)");
+#if SYZ_EXECUTOR || SYZ_ENABLE_DEVLINK_PCI
+ initialize_devlink_pci();
+#endif
#if SYZ_EXECUTOR || SYZ_TUN_ENABLE
// We setup tun here as it needs to be in the test net namespace,
// which in turn needs to be in the test user namespace.
diff --git a/executor/executor.cc b/executor/executor.cc
index 30d497ea9..2016a8bf5 100644
--- a/executor/executor.cc
+++ b/executor/executor.cc
@@ -118,6 +118,7 @@ static bool flag_enable_net_dev;
static bool flag_enable_net_reset;
static bool flag_enable_cgroups;
static bool flag_enable_close_fds;
+static bool flag_enable_devlink_pci;
static bool flag_collect_cover;
static bool flag_dedup_cover;
@@ -481,6 +482,7 @@ void parse_env_flags(uint64 flags)
flag_enable_net_reset = flags & (1 << 8);
flag_enable_cgroups = flags & (1 << 9);
flag_enable_close_fds = flags & (1 << 10);
+ flag_enable_devlink_pci = flags & (1 << 11);
}
#if SYZ_EXECUTOR_USES_FORK_SERVER