From dbce178a0e6997a799ba38247059dcb8213f4572 Mon Sep 17 00:00:00 2001 From: m00nbsd <42475391+m00nbsd@users.noreply.github.com> Date: Sat, 13 Jun 2020 12:10:16 +0200 Subject: sys/netbsd: support multiple vHCI buses (#1822) --- executor/common_bsd.h | 22 +++++++++++++++++++-- executor/common_usb_netbsd.h | 25 +++++++++-------------- executor/executor_bsd.h | 2 +- pkg/csource/generated.go | 47 +++++++++++++++++++++++++++----------------- pkg/host/host_netbsd.go | 2 +- 5 files changed, 60 insertions(+), 38 deletions(-) diff --git a/executor/common_bsd.h b/executor/common_bsd.h index 82aef4211..070c512bf 100644 --- a/executor/common_bsd.h +++ b/executor/common_bsd.h @@ -17,10 +17,28 @@ #include "common_usb_netbsd.h" #endif #if SYZ_EXECUTOR || SYZ_USB +#include static void setup_usb(void) { - if (chmod("/dev/vhci", 0666)) - fail("failed to chmod /dev/vhci"); + struct dirent* ent; + char path[1024]; + DIR* dir; + + dir = opendir("/dev"); + if (dir == NULL) + fail("failed to open /dev"); + + while ((ent = readdir(dir)) != NULL) { + if (ent->d_type != DT_CHR) + continue; + if (strncmp(ent->d_name, "vhci", 4)) + continue; + snprintf(path, sizeof(path), "/dev/%s", ent->d_name); + if (chmod(path, 0666)) + fail("failed to chmod %s", path); + } + + closedir(dir); } #endif diff --git a/executor/common_usb_netbsd.h b/executor/common_usb_netbsd.h index c6936993f..9ce8f72dc 100644 --- a/executor/common_usb_netbsd.h +++ b/executor/common_usb_netbsd.h @@ -159,7 +159,11 @@ struct usb_qualifier_descriptor { static int vhci_open(void) { - return open("/dev/vhci", O_RDWR); + char path[1024]; + + snprintf(path, sizeof(path), "/dev/vhci%llu", procid); + + return open(path, O_RDWR); } static int vhci_setport(int fd, u_int port) @@ -214,31 +218,22 @@ static volatile long syz_usb_connect_impl(uint64 speed, uint64 dev_len, lookup_connect_out_response_t lookup_connect_response_out) { struct usb_device_index* index; - int portnum, fd, rv; + int fd, rv; bool done; - portnum = procid + 1; - debug("syz_usb_connect: dev: %p\n", dev); if (!dev) { debug("syz_usb_connect: dev is null\n"); return -1; } - if (portnum != 1) { - /* For now, we support only one proc. */ - debug("syz_usb_connect: not proc1 %d\n", (int)procid); - return -1; - } debug("syz_usb_connect: device data:\n"); debug_dump_data(dev, dev_len); fd = vhci_open(); if (fd < 0) { - debug("syz_usb_connect: vhci_open failed with %d\n", fd); - return -1; + fail("syz_usb_connect: vhci_open failed with %d", errno); } - debug("syz_usb_connect: vhci_open success\n"); index = add_usb_index(fd, dev, dev_len); if (!index) { @@ -251,12 +246,10 @@ static volatile long syz_usb_connect_impl(uint64 speed, uint64 dev_len, NONFAILING(analyze_usb_device(index)); #endif - rv = vhci_setport(fd, portnum); + rv = vhci_setport(fd, 1); if (rv != 0) { - debug("syz_usb_connect: vhci_setport failed with %d\n", rv); - goto err; + fail("syz_usb_connect: vhci_setport failed with %d", errno); } - debug("syz_usb_connect: vhci_setport success\n"); rv = vhci_usb_attach(fd); if (rv != 0) { diff --git a/executor/executor_bsd.h b/executor/executor_bsd.h index a9440acec..d3ef16bce 100644 --- a/executor/executor_bsd.h +++ b/executor/executor_bsd.h @@ -74,7 +74,7 @@ static void cover_open(cover_t* cov, bool extra) cover_size = (256 << 10); // maximum size struct kcov_ioc_remote_attach args; args.subsystem = KCOV_REMOTE_VHCI; - args.id = procid + 1; // port number + args.id = KCOV_REMOTE_VHCI_ID(procid, 1); // first port if (ioctl(cov->fd, KCOV_IOC_REMOTE_ATTACH, &args)) fail("ioctl remote attach failed"); } else { diff --git a/pkg/csource/generated.go b/pkg/csource/generated.go index 0d4219de6..fe69f9237 100644 --- a/pkg/csource/generated.go +++ b/pkg/csource/generated.go @@ -1342,7 +1342,11 @@ static bool lookup_control_response(const struct vusb_descriptors* descs, const static int vhci_open(void) { - return open("/dev/vhci", O_RDWR); + char path[1024]; + + snprintf(path, sizeof(path), "/dev/vhci%llu", procid); + + return open(path, O_RDWR); } static int vhci_setport(int fd, u_int port) @@ -1397,31 +1401,22 @@ static volatile long syz_usb_connect_impl(uint64 speed, uint64 dev_len, lookup_connect_out_response_t lookup_connect_response_out) { struct usb_device_index* index; - int portnum, fd, rv; + int fd, rv; bool done; - portnum = procid + 1; - debug("syz_usb_connect: dev: %p\n", dev); if (!dev) { debug("syz_usb_connect: dev is null\n"); return -1; } - if (portnum != 1) { - /* For now, we support only one proc. */ - debug("syz_usb_connect: not proc1 %d\n", (int)procid); - return -1; - } debug("syz_usb_connect: device data:\n"); debug_dump_data(dev, dev_len); fd = vhci_open(); if (fd < 0) { - debug("syz_usb_connect: vhci_open failed with %d\n", fd); - return -1; + fail("syz_usb_connect: vhci_open failed with %d", errno); } - debug("syz_usb_connect: vhci_open success\n"); index = add_usb_index(fd, dev, dev_len); if (!index) { @@ -1434,12 +1429,10 @@ static volatile long syz_usb_connect_impl(uint64 speed, uint64 dev_len, NONFAILING(analyze_usb_device(index)); #endif - rv = vhci_setport(fd, portnum); + rv = vhci_setport(fd, 1); if (rv != 0) { - debug("syz_usb_connect: vhci_setport failed with %d\n", rv); - goto err; + fail("syz_usb_connect: vhci_setport failed with %d", errno); } - debug("syz_usb_connect: vhci_setport success\n"); rv = vhci_usb_attach(fd); if (rv != 0) { @@ -1563,10 +1556,28 @@ static volatile long syz_usb_disconnect(volatile long a0) #endif #if SYZ_EXECUTOR || SYZ_USB +#include static void setup_usb(void) { - if (chmod("/dev/vhci", 0666)) - fail("failed to chmod /dev/vhci"); + struct dirent* ent; + char path[1024]; + DIR* dir; + + dir = opendir("/dev"); + if (dir == NULL) + fail("failed to open /dev"); + + while ((ent = readdir(dir)) != NULL) { + if (ent->d_type != DT_CHR) + continue; + if (strncmp(ent->d_name, "vhci", 4)) + continue; + snprintf(path, sizeof(path), "/dev/%s", ent->d_name); + if (chmod(path, 0666)) + fail("failed to chmod %s", path); + } + + closedir(dir); } #endif diff --git a/pkg/host/host_netbsd.go b/pkg/host/host_netbsd.go index dffdec5d7..aa0a6e88a 100644 --- a/pkg/host/host_netbsd.go +++ b/pkg/host/host_netbsd.go @@ -27,7 +27,7 @@ func init() { } func checkUSBEmulation() string { - if err := osutil.IsAccessible("/dev/vhci"); err != nil { + if err := osutil.IsAccessible("/dev/vhci0"); err != nil { return err.Error() } return "" -- cgit mrf-deployment