ok github.com/google/syzkaller/dashboard/app (cached) ? github.com/google/syzkaller/dashboard/dashapi [no test files] ? github.com/google/syzkaller/executor [no test files] ok github.com/google/syzkaller/pkg/ast (cached) ok github.com/google/syzkaller/pkg/bisect (cached) ok github.com/google/syzkaller/pkg/build (cached) ? github.com/google/syzkaller/pkg/cmdprof [no test files] ok github.com/google/syzkaller/pkg/compiler (cached) ok github.com/google/syzkaller/pkg/config (cached) ? github.com/google/syzkaller/pkg/cover [no test files] --- FAIL: TestGenerate (1.39s) --- FAIL: TestGenerate/netbsd/amd64 (0.04s) csource_test.go:67: seed=1590078626916916782 --- FAIL: TestGenerate/netbsd/amd64/0 (0.40s) csource_test.go:123: opts: {Threaded:false Collide:false Repeat:true RepeatTimes:0 Procs:0 Sandbox:none Fault:false FaultCall:0 FaultNth:0 Leak:false NetInjection:false NetDevices:false NetReset:false Cgroups:false BinfmtMisc:false CloseFDs:false KCSAN:false DevlinkPCI:false USB:false UseTmpDir:true HandleSegv:false Repro:false Trace:false} program: r0 = _lwp_self() _lwp_create(&(0x7f0000000100)={0x400f0020, &(0x7f0000000080)={0x0, &(0x7f0000000000)={0x400f0023, 0x0, {[0x7, 0x7, 0x101, 0x1f]}, {0x1, 0x400, 0x6}, {0x1, 0x80000000, '\x00'}}, {[0x3, 0x5, 0x1, 0x35f7192e]}, {0xd, 0x100000000, 0x5}, {0x6, 0x10001, '\x00'}}, {[0xf1b, 0x401, 0x69, 0x100]}, {0x1, 0x0, 0x16ac7582ecd3534}, {0x1, 0x400, '-\x00'}}, 0x60, &(0x7f0000000180)=0x0) r2 = _lwp_self() _lwp_create(&(0x7f00000001c0)={0x2, 0x0, {[0x9, 0x3ae, 0x3, 0xc699]}, {0x81, 0x2, 0x1}, {0x9, 0x7, '\x00'}}, 0x40, &(0x7f0000000240)=0x0) r4 = _lwp_self() _lwp_unpark_all(&(0x7f0000000280)=[r0, r1, r2, r3, r4], 0x5, &(0x7f00000002c0)=0x4) _lwp_create(&(0x7f0000000380)={0x8, &(0x7f0000000300)={0x400f002c, 0x0, {[0x80000000, 0xa706, 0x0, 0x8]}, {0x1f, 0x6, 0x1}, {0x101, 0x5, '\x00'}}, {[0x2, 0x9, 0x10000, 0x1]}, {0x2, 0xffffffff, 0x5}, {0x20, 0xfff, '\x00'}}, 0x40, &(0x7f0000000400)=0x0) _lwp_kill(r5, 0x8) compat_50__lwp_park(&(0x7f0000000440)={0x101, 0x8}, 0x0, 0x0, &(0x7f0000000480)=0x1) shmctl$SHM_LOCK(0x0, 0x3) syz_emit_ethernet(0x54, &(0x7f0000000000)="afe8d2383c3ef2ff5f6e8c9a4d35c602198766ff84e3acfd84f778353f61842e8a198d7e500cce4b813690338589cca07acde0e9834fd09b00f3146ba0ddb9ac17ae011fb861af258325476c9621388eca4b11c0") syz_execute_func(&(0x7f0000000080)="3642833dd70b586a463e640febb19b000000c4c3f97c6d4ca6f344a6c443857fc618fdc403116f26fa447ba9642e470f12fc64f20f116200") syz_extract_tcp_res(&(0x7f00000000c0), 0x3, 0xc5d) r6 = syz_usb_connect(0xfffffffffffffeff, 0x78, &(0x7f0000000100)="935e057aa8ddaece20394d86c45c4a50f7278a8791b0cdbcf6e832b9052f3ab99063705506c66eb3374aad5e1fded7515c8cb7fe2c216994ffb89e915496a207404168e86ba82f7c0d4964e7e756ac689919cf9f4bf87d3e3b5174605c8f256d28204e428bf295ff312e2e27a1c2afb893dbe7867060c650", &(0x7f0000000180)="2b06574b32d8eb1d7479431c2ab53fc2d24cbf1c4368a824c8cd8217f44d51b11889f929d65e7900cccb5166453ffe7fa1f328df9695a4457b58f13191e90ca794186e763217e923bdc4f20434eba96616cc3b27ea87d54a6c434d77b737d18d0d8d537dce330d2759be67e22e1dc617f517147faeb44e343d630b33d1d31606b2a3971efcd03e4d87b5e600323723abfef81aa71bea16bdf9561aca75ce3265de3ac8a1590a61ede73a7b6943ddf3e3fcf19e478f55859db78d6baa46b7a7e2159f6f4f20ccb18d5c41fb430249") syz_usb_disconnect(r6) csource_test.go:124: failed to build program: // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static unsigned long long procid; static void kill_and_wait(int pid, int* status) { kill(pid, SIGKILL); while (waitpid(-1, status, 0) != pid) { } } static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void use_temporary_dir(void) { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } static void remove_dir(const char* dir) { DIR* dp; struct dirent* ep; dp = opendir(dir); if (dp == NULL) exit(1); while ((ep = readdir(dp))) { if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue; char filename[FILENAME_MAX]; snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } if (unlink(filename)) exit(1); } closedir(dp); if (rmdir(dir)) exit(1); } /* -------------------------------------------------------------------------- */ /* * Redefinitions to match the linux types used in common_usb.h. */ struct usb_endpoint_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; uint16_t wMaxPacketSize; uint8_t bInterval; uint8_t bRefresh; uint8_t bSynchAddress; } __attribute__((packed)); struct usb_device_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint16_t idVendor; uint16_t idProduct; uint16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; } __attribute__((packed)); struct usb_config_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; } __attribute__((packed)); struct usb_interface_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bNumEndpoints; uint8_t bInterfaceClass; uint8_t bInterfaceSubClass; uint8_t bInterfaceProtocol; uint8_t iInterface; } __attribute__((packed)); struct usb_ctrlrequest { uint8_t bRequestType; uint8_t bRequest; uint16_t wValue; uint16_t wIndex; uint16_t wLength; } __attribute__((packed)); struct usb_qualifier_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint8_t bNumConfigurations; uint8_t bRESERVED; } __attribute__((packed)); #define USB_TYPE_MASK (0x03 << 5) #define USB_TYPE_STANDARD (0x00 << 5) #define USB_TYPE_CLASS (0x01 << 5) #define USB_TYPE_VENDOR (0x02 << 5) #define USB_TYPE_RESERVED (0x03 << 5) #define USB_DT_DEVICE 0x01 #define USB_DT_CONFIG 0x02 #define USB_DT_STRING 0x03 #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_DEVICE_QUALIFIER 0x06 #define USB_DT_OTHER_SPEED_CONFIG 0x07 #define USB_DT_INTERFACE_POWER 0x08 #define USB_DT_OTG 0x09 #define USB_DT_DEBUG 0x0a #define USB_DT_INTERFACE_ASSOCIATION 0x0b #define USB_DT_SECURITY 0x0c #define USB_DT_KEY 0x0d #define USB_DT_ENCRYPTION_TYPE 0x0e #define USB_DT_BOS 0x0f #define USB_DT_DEVICE_CAPABILITY 0x10 #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 #define USB_DT_WIRE_ADAPTER 0x21 #define USB_DT_RPIPE 0x22 #define USB_DT_CS_RADIO_CONTROL 0x23 #define USB_DT_PIPE_USAGE 0x24 #define USB_DT_SS_ENDPOINT_COMP 0x30 #define USB_DT_SSP_ISOC_ENDPOINT_COMP 0x31 #define USB_REQ_GET_STATUS 0x00 #define USB_REQ_CLEAR_FEATURE 0x01 #define USB_REQ_SET_FEATURE 0x03 #define USB_REQ_SET_ADDRESS 0x05 #define USB_REQ_GET_DESCRIPTOR 0x06 #define USB_REQ_SET_DESCRIPTOR 0x07 #define USB_REQ_GET_CONFIGURATION 0x08 #define USB_REQ_SET_CONFIGURATION 0x09 #define USB_REQ_GET_INTERFACE 0x0A #define USB_REQ_SET_INTERFACE 0x0B #define USB_REQ_SYNCH_FRAME 0x0C #define USB_REQ_SET_SEL 0x30 #define USB_REQ_SET_ISOCH_DELAY 0x31 #define USB_REQ_SET_ENCRYPTION 0x0D #define USB_REQ_GET_ENCRYPTION 0x0E #define USB_REQ_RPIPE_ABORT 0x0E #define USB_REQ_SET_HANDSHAKE 0x0F #define USB_REQ_RPIPE_RESET 0x0F #define USB_REQ_GET_HANDSHAKE 0x10 #define USB_REQ_SET_CONNECTION 0x11 #define USB_REQ_SET_SECURITY_DATA 0x12 #define USB_REQ_GET_SECURITY_DATA 0x13 #define USB_REQ_SET_WUSB_DATA 0x14 #define USB_REQ_LOOPBACK_DATA_WRITE 0x15 #define USB_REQ_LOOPBACK_DATA_READ 0x16 #define USB_REQ_SET_INTERFACE_DS 0x17 #define USB_REQ_GET_PARTNER_PDO 20 #define USB_REQ_GET_BATTERY_STATUS 21 #define USB_REQ_SET_PDO 22 #define USB_REQ_GET_VDM 23 #define USB_REQ_SEND_VDM 24 #define USB_MAX_IFACE_NUM 4 #define USB_MAX_EP_NUM 32 #define USB_MAX_FDS 6 struct usb_endpoint_index { struct usb_endpoint_descriptor desc; int handle; }; struct usb_iface_index { struct usb_interface_descriptor* iface; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bInterfaceClass; struct usb_endpoint_index eps[USB_MAX_EP_NUM]; int eps_num; }; struct usb_device_index { struct usb_device_descriptor* dev; struct usb_config_descriptor* config; uint8_t bDeviceClass; uint8_t bMaxPower; int config_length; struct usb_iface_index ifaces[USB_MAX_IFACE_NUM]; int ifaces_num; int iface_cur; }; struct usb_info { int fd; struct usb_device_index index; }; static struct usb_info usb_devices[USB_MAX_FDS]; static int usb_devices_num; static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index) { if (length < sizeof(*index->dev) + sizeof(*index->config)) return false; memset(index, 0, sizeof(*index)); index->dev = (struct usb_device_descriptor*)buffer; index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev)); index->bDeviceClass = index->dev->bDeviceClass; index->bMaxPower = index->config->bMaxPower; index->config_length = length - sizeof(*index->dev); index->iface_cur = -1; size_t offset = 0; while (true) { if (offset + 1 >= length) break; uint8_t desc_length = buffer[offset]; uint8_t desc_type = buffer[offset + 1]; if (desc_length <= 2) break; if (offset + desc_length > length) break; if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) { struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset); index->ifaces[index->ifaces_num].iface = iface; index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber; index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting; index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass; index->ifaces_num++; } if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) { struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1]; if (iface->eps_num < USB_MAX_EP_NUM) { memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc)); iface->eps_num++; } } offset += desc_length; } return true; } static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len) { int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED); if (i >= USB_MAX_FDS) return NULL; int rv = 0; rv = parse_usb_descriptor(dev, dev_len, &usb_devices[i].index); if (!rv) return NULL; __atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE); return &usb_devices[i].index; } static struct usb_device_index* lookup_usb_index(int fd) { int i; for (i = 0; i < USB_MAX_FDS; i++) { if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd) { return &usb_devices[i].index; } } return NULL; } struct vusb_connect_string_descriptor { uint32_t len; char* str; } __attribute__((packed)); struct vusb_connect_descriptors { uint32_t qual_len; char* qual; uint32_t bos_len; char* bos; uint32_t strs_len; struct vusb_connect_string_descriptor strs[0]; } __attribute__((packed)); static const char default_string[] = { 8, USB_DT_STRING, 's', 0, 'y', 0, 'z', 0 }; static const char default_lang_id[] = { 4, USB_DT_STRING, 0x09, 0x04 }; static bool lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, char** response_data, uint32_t* response_length) { struct usb_device_index* index = lookup_usb_index(fd); uint8_t str_idx; if (!index) return false; switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_GET_DESCRIPTOR: switch (ctrl->wValue >> 8) { case USB_DT_DEVICE: *response_data = (char*)index->dev; *response_length = sizeof(*index->dev); return true; case USB_DT_CONFIG: *response_data = (char*)index->config; *response_length = index->config_length; return true; case USB_DT_STRING: str_idx = (uint8_t)ctrl->wValue; if (descs && str_idx < descs->strs_len) { *response_data = descs->strs[str_idx].str; *response_length = descs->strs[str_idx].len; return true; } if (str_idx == 0) { *response_data = (char*)&default_lang_id[0]; *response_length = default_lang_id[0]; return true; } *response_data = (char*)&default_string[0]; *response_length = default_string[0]; return true; case USB_DT_BOS: *response_data = descs->bos; *response_length = descs->bos_len; return true; case USB_DT_DEVICE_QUALIFIER: if (!descs->qual) { struct usb_qualifier_descriptor* qual = (struct usb_qualifier_descriptor*)response_data; qual->bLength = sizeof(*qual); qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; qual->bcdUSB = index->dev->bcdUSB; qual->bDeviceClass = index->dev->bDeviceClass; qual->bDeviceSubClass = index->dev->bDeviceSubClass; qual->bDeviceProtocol = index->dev->bDeviceProtocol; qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0; qual->bNumConfigurations = index->dev->bNumConfigurations; qual->bRESERVED = 0; *response_length = sizeof(*qual); return true; } *response_data = descs->qual; *response_length = descs->qual_len; return true; default: break; } break; default: break; } break; default: break; } return false; } typedef bool (*lookup_connect_out_response_t)(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done); static bool lookup_connect_response_out_generic(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done) { switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_SET_CONFIGURATION: *done = true; return true; default: break; } break; } return false; } /* -------------------------------------------------------------------------- */ static int vhci_open(void) { return open("/dev/vhci", O_RDWR); } static int vhci_setport(int fd, u_int port) { struct vhci_ioc_set_port args; args.port = port; return ioctl(fd, VHCI_IOC_SET_PORT, &args); } static int vhci_usb_attach(int fd) { return ioctl(fd, VHCI_IOC_USB_ATTACH, NULL); } static int vhci_usb_recv(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = read(fd, ptr, size); if (done < 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } static int vhci_usb_send(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = write(fd, ptr, size); if (done <= 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } /* -------------------------------------------------------------------------- */ static volatile long syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev, const struct vusb_connect_descriptors* descs, lookup_connect_out_response_t lookup_connect_response_out) { struct usb_device_index* index; int portnum, fd, rv; bool done; portnum = procid + 1; if (!dev) { return -1; } if (portnum != 1) { /* For now, we support only one proc. */ return -1; } fd = vhci_open(); if (fd < 0) { return -1; } index = add_usb_index(fd, dev, dev_len); if (!index) { goto err; } rv = vhci_setport(fd, portnum); if (rv != 0) { goto err; } rv = vhci_usb_attach(fd); if (rv != 0) { goto err; } done = false; while (!done) { vhci_request_t req; rv = vhci_usb_recv(fd, &req, sizeof(req)); if (rv != 0) { goto err; } if (req.type != VHCI_REQ_CTRL) { goto err; } char* response_data = NULL; uint32_t response_length = 0; char data[4096]; if (req.u.ctrl.bmRequestType & UE_DIR_IN) { bool response_found = false; response_found = lookup_connect_response_in(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &response_data, &response_length); if (!response_found) { goto err; } } else { if (!lookup_connect_response_out(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &done)) { goto err; } response_data = NULL; response_length = UGETW(req.u.ctrl.wLength); } if ((req.u.ctrl.bmRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD && req.u.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) { /* TODO: possibly revisit */ } if (response_length > sizeof(data)) response_length = 0; if ((uint32_t)UGETW(req.u.ctrl.wLength) < response_length) response_length = UGETW(req.u.ctrl.wLength); if (response_data) memcpy(data, response_data, response_length); else memset(data, 0, response_length); if (req.u.ctrl.bmRequestType & UE_DIR_IN) { if (response_length > 0) { vhci_response_t res; res.size = response_length; rv = vhci_usb_send(fd, &res, sizeof(res)); if (rv == 0) rv = vhci_usb_send(fd, data, response_length); } } else { rv = vhci_usb_recv(fd, data, response_length); } if (rv < 0) { goto err; } } sleep_ms(200); return fd; err: close(fd); return -1; } static volatile long syz_usb_connect(volatile long a0, volatile long a1, volatile long a2, volatile long a3) { uint64_t speed = a0; uint64_t dev_len = a1; const char* dev = (const char*)a2; const struct vusb_connect_descriptors* descs = (const struct vusb_connect_descriptors*)a3; return syz_usb_connect_impl(speed, dev_len, dev, descs, &lookup_connect_response_out_generic); } static volatile long syz_usb_disconnect(volatile long a0) { int fd = a0; int rv = close(fd); sleep_ms(200); return rv; } static void sandbox_common() { if (setsid() == -1) exit(1); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 8 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 0; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); } static void loop(); static int do_sandbox_none(void) { sandbox_common(); loop(); return 0; } static long syz_execute_func(volatile long text) { volatile long p[8] = {0}; (void)p; asm volatile("" ::"r"(0l), "r"(1l), "r"(2l), "r"(3l), "r"(4l), "r"(5l), "r"(6l), "r"(7l), "r"(8l), "r"(9l), "r"(10l), "r"(11l), "r"(12l), "r"(13l)); ((void (*)(void))(text))(); return 0; } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter; for (iter = 0;; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5 * 1000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } #ifndef SYS__lwp_create #define SYS__lwp_create 309 #endif #ifndef SYS__lwp_kill #define SYS__lwp_kill 318 #endif #ifndef SYS__lwp_self #define SYS__lwp_self 311 #endif #ifndef SYS__lwp_unpark_all #define SYS__lwp_unpark_all 322 #endif #ifndef SYS_compat_50__lwp_park #define SYS_compat_50__lwp_park 320 #endif #ifndef SYS_mmap #define SYS_mmap 197 #endif #ifndef SYS_shmctl #define SYS_shmctl 443 #endif uint64_t r[7] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xffffffffffffffff}; void execute_one(void) { intptr_t res = 0; res = syscall(SYS__lwp_self); if (res != -1) r[0] = res; *(uint32_t*)0x20000100 = 0x400f0020; *(uint64_t*)0x20000108 = 0x20000080; *(uint32_t*)0x20000080 = 0; *(uint64_t*)0x20000088 = 0x20000000; *(uint32_t*)0x20000000 = 0x400f0023; *(uint64_t*)0x20000008 = 0; *(uint32_t*)0x20000010 = 7; *(uint32_t*)0x20000014 = 7; *(uint32_t*)0x20000018 = 0x101; *(uint32_t*)0x2000001c = 0x1f; *(uint64_t*)0x20000020 = 1; *(uint64_t*)0x20000028 = 0x400; *(uint32_t*)0x20000030 = 6; *(uint64_t*)0x20000038 = 1; *(uint64_t*)0x20000040 = 0x80000000; memcpy((void*)0x20000048, "\000", 1); *(uint32_t*)0x20000090 = 3; *(uint32_t*)0x20000094 = 5; *(uint32_t*)0x20000098 = 1; *(uint32_t*)0x2000009c = 0x35f7192e; *(uint64_t*)0x200000a0 = 0xd; *(uint64_t*)0x200000a8 = 0x100000000; *(uint32_t*)0x200000b0 = 5; *(uint64_t*)0x200000b8 = 6; *(uint64_t*)0x200000c0 = 0x10001; memcpy((void*)0x200000c8, "\000", 1); *(uint32_t*)0x20000110 = 0xf1b; *(uint32_t*)0x20000114 = 0x401; *(uint32_t*)0x20000118 = 0x69; *(uint32_t*)0x2000011c = 0x100; *(uint64_t*)0x20000120 = 1; *(uint64_t*)0x20000128 = 0; *(uint32_t*)0x20000130 = 0x2ecd3534; *(uint64_t*)0x20000138 = 1; *(uint64_t*)0x20000140 = 0x400; memcpy((void*)0x20000148, "-\000", 2); res = syscall(SYS__lwp_create, 0x20000100ul, 0x60ul, 0x20000180ul); if (res != -1) r[1] = *(uint32_t*)0x20000180; res = syscall(SYS__lwp_self); if (res != -1) r[2] = res; *(uint32_t*)0x200001c0 = 2; *(uint64_t*)0x200001c8 = 0; *(uint32_t*)0x200001d0 = 9; *(uint32_t*)0x200001d4 = 0x3ae; *(uint32_t*)0x200001d8 = 3; *(uint32_t*)0x200001dc = 0xc699; *(uint64_t*)0x200001e0 = 0x81; *(uint64_t*)0x200001e8 = 2; *(uint32_t*)0x200001f0 = 1; *(uint64_t*)0x200001f8 = 9; *(uint64_t*)0x20000200 = 7; memcpy((void*)0x20000208, "\000", 1); res = syscall(SYS__lwp_create, 0x200001c0ul, 0x40ul, 0x20000240ul); if (res != -1) r[3] = *(uint32_t*)0x20000240; res = syscall(SYS__lwp_self); if (res != -1) r[4] = res; *(uint32_t*)0x20000280 = r[0]; *(uint32_t*)0x20000284 = r[1]; *(uint32_t*)0x20000288 = r[2]; *(uint32_t*)0x2000028c = r[3]; *(uint32_t*)0x20000290 = r[4]; *(uint64_t*)0x200002c0 = 4; syscall(SYS__lwp_unpark_all, 0x20000280ul, 5ul, 0x200002c0ul); *(uint32_t*)0x20000380 = 8; *(uint64_t*)0x20000388 = 0x20000300; *(uint32_t*)0x20000300 = 0x400f002c; *(uint64_t*)0x20000308 = 0; *(uint32_t*)0x20000310 = 0x80000000; *(uint32_t*)0x20000314 = 0xa706; *(uint32_t*)0x20000318 = 0; *(uint32_t*)0x2000031c = 8; *(uint64_t*)0x20000320 = 0x1f; *(uint64_t*)0x20000328 = 6; *(uint32_t*)0x20000330 = 1; *(uint64_t*)0x20000338 = 0x101; *(uint64_t*)0x20000340 = 5; memcpy((void*)0x20000348, "\000", 1); *(uint32_t*)0x20000390 = 2; *(uint32_t*)0x20000394 = 9; *(uint32_t*)0x20000398 = 0x10000; *(uint32_t*)0x2000039c = 1; *(uint64_t*)0x200003a0 = 2; *(uint64_t*)0x200003a8 = 0xffffffff; *(uint32_t*)0x200003b0 = 5; *(uint64_t*)0x200003b8 = 0x20; *(uint64_t*)0x200003c0 = 0xfff; memcpy((void*)0x200003c8, "\000", 1); res = syscall(SYS__lwp_create, 0x20000380ul, 0x40ul, 0x20000400ul); if (res != -1) r[5] = *(uint32_t*)0x20000400; syscall(SYS__lwp_kill, r[5], 8); *(uint32_t*)0x20000440 = 0x101; *(uint64_t*)0x20000448 = 8; *(uint64_t*)0x20000480 = 1; syscall(SYS_compat_50__lwp_park, 0x20000440ul, 0, 0ul, 0x20000480ul); syscall(SYS_shmctl, 0, 3ul, 0); memcpy((void*)0x20000000, "\xaf\xe8\xd2\x38\x3c\x3e\xf2\xff\x5f\x6e\x8c\x9a\x4d\x35\xc6\x02\x19\x87\x66\xff\x84\xe3\xac\xfd\x84\xf7\x78\x35\x3f\x61\x84\x2e\x8a\x19\x8d\x7e\x50\x0c\xce\x4b\x81\x36\x90\x33\x85\x89\xcc\xa0\x7a\xcd\xe0\xe9\x83\x4f\xd0\x9b\x00\xf3\x14\x6b\xa0\xdd\xb9\xac\x17\xae\x01\x1f\xb8\x61\xaf\x25\x83\x25\x47\x6c\x96\x21\x38\x8e\xca\x4b\x11\xc0", 84); memcpy((void*)0x20000080, "\x36\x42\x83\x3d\xd7\x0b\x58\x6a\x46\x3e\x64\x0f\xeb\xb1\x9b\x00\x00\x00\xc4\xc3\xf9\x7c\x6d\x4c\xa6\xf3\x44\xa6\xc4\x43\x85\x7f\xc6\x18\xfd\xc4\x03\x11\x6f\x26\xfa\x44\x7b\xa9\x64\x2e\x47\x0f\x12\xfc\x64\xf2\x0f\x11\x62\x00", 56); syz_execute_func(0x20000080); memcpy((void*)0x20000100, "\x93\x5e\x05\x7a\xa8\xdd\xae\xce\x20\x39\x4d\x86\xc4\x5c\x4a\x50\xf7\x27\x8a\x87\x91\xb0\xcd\xbc\xf6\xe8\x32\xb9\x05\x2f\x3a\xb9\x90\x63\x70\x55\x06\xc6\x6e\xb3\x37\x4a\xad\x5e\x1f\xde\xd7\x51\x5c\x8c\xb7\xfe\x2c\x21\x69\x94\xff\xb8\x9e\x91\x54\x96\xa2\x07\x40\x41\x68\xe8\x6b\xa8\x2f\x7c\x0d\x49\x64\xe7\xe7\x56\xac\x68\x99\x19\xcf\x9f\x4b\xf8\x7d\x3e\x3b\x51\x74\x60\x5c\x8f\x25\x6d\x28\x20\x4e\x42\x8b\xf2\x95\xff\x31\x2e\x2e\x27\xa1\xc2\xaf\xb8\x93\xdb\xe7\x86\x70\x60\xc6\x50", 120); memcpy((void*)0x20000180, "\x2b\x06\x57\x4b\x32\xd8\xeb\x1d\x74\x79\x43\x1c\x2a\xb5\x3f\xc2\xd2\x4c\xbf\x1c\x43\x68\xa8\x24\xc8\xcd\x82\x17\xf4\x4d\x51\xb1\x18\x89\xf9\x29\xd6\x5e\x79\x00\xcc\xcb\x51\x66\x45\x3f\xfe\x7f\xa1\xf3\x28\xdf\x96\x95\xa4\x45\x7b\x58\xf1\x31\x91\xe9\x0c\xa7\x94\x18\x6e\x76\x32\x17\xe9\x23\xbd\xc4\xf2\x04\x34\xeb\xa9\x66\x16\xcc\x3b\x27\xea\x87\xd5\x4a\x6c\x43\x4d\x77\xb7\x37\xd1\x8d\x0d\x8d\x53\x7d\xce\x33\x0d\x27\x59\xbe\x67\xe2\x2e\x1d\xc6\x17\xf5\x17\x14\x7f\xae\xb4\x4e\x34\x3d\x63\x0b\x33\xd1\xd3\x16\x06\xb2\xa3\x97\x1e\xfc\xd0\x3e\x4d\x87\xb5\xe6\x00\x32\x37\x23\xab\xfe\xf8\x1a\xa7\x1b\xea\x16\xbd\xf9\x56\x1a\xca\x75\xce\x32\x65\xde\x3a\xc8\xa1\x59\x0a\x61\xed\xe7\x3a\x7b\x69\x43\xdd\xf3\xe3\xfc\xf1\x9e\x47\x8f\x55\x85\x9d\xb7\x8d\x6b\xaa\x46\xb7\xa7\xe2\x15\x9f\x6f\x4f\x20\xcc\xb1\x8d\x5c\x41\xfb\x43\x02\x49", 206); res = syz_usb_connect(0xfffffffffffffeff, 0x78, 0x20000100, 0x20000180); if (res != -1) r[6] = res; syz_usb_disconnect(r[6]); } int main(void) { syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul, 0ul); use_temporary_dir(); do_sandbox_none(); return 0; } : In function 'syz_usb_connect_impl': :546:63: error: unknown type name 'usb_ctrlrequest' :551:55: error: unknown type name 'usb_ctrlrequest' compiler invocation: /syzkaller/netbsd/src/../tools/bin/x86_64--netbsd-g++ [-o /tmp/syz-executor727192031 -DGOOS_netbsd=1 -DGOARCH_amd64=1 -DHOSTGOOS_linux=1 -x c - -m64 --sysroot /syzkaller/netbsd/src/../dest/ -O2 -pthread -Wall -Werror -Wparentheses -Wframe-larger-than=16384] --- FAIL: TestGenerate/netbsd/amd64/10 (0.40s) csource_test.go:123: opts: {Threaded:true Collide:false Repeat:true RepeatTimes:0 Procs:0 Sandbox:none Fault:false FaultCall:0 FaultNth:0 Leak:false NetInjection:false NetDevices:false NetReset:false Cgroups:false BinfmtMisc:false CloseFDs:false KCSAN:false DevlinkPCI:false USB:false UseTmpDir:false HandleSegv:false Repro:false Trace:false} program: r0 = _lwp_self() _lwp_create(&(0x7f0000000100)={0x400f0020, &(0x7f0000000080)={0x0, &(0x7f0000000000)={0x400f0023, 0x0, {[0x7, 0x7, 0x101, 0x1f]}, {0x1, 0x400, 0x6}, {0x1, 0x80000000, '\x00'}}, {[0x3, 0x5, 0x1, 0x35f7192e]}, {0xd, 0x100000000, 0x5}, {0x6, 0x10001, '\x00'}}, {[0xf1b, 0x401, 0x69, 0x100]}, {0x1, 0x0, 0x16ac7582ecd3534}, {0x1, 0x400, '-\x00'}}, 0x60, &(0x7f0000000180)=0x0) r2 = _lwp_self() _lwp_create(&(0x7f00000001c0)={0x2, 0x0, {[0x9, 0x3ae, 0x3, 0xc699]}, {0x81, 0x2, 0x1}, {0x9, 0x7, '\x00'}}, 0x40, &(0x7f0000000240)=0x0) r4 = _lwp_self() _lwp_unpark_all(&(0x7f0000000280)=[r0, r1, r2, r3, r4], 0x5, &(0x7f00000002c0)=0x4) _lwp_create(&(0x7f0000000380)={0x8, &(0x7f0000000300)={0x400f002c, 0x0, {[0x80000000, 0xa706, 0x0, 0x8]}, {0x1f, 0x6, 0x1}, {0x101, 0x5, '\x00'}}, {[0x2, 0x9, 0x10000, 0x1]}, {0x2, 0xffffffff, 0x5}, {0x20, 0xfff, '\x00'}}, 0x40, &(0x7f0000000400)=0x0) _lwp_kill(r5, 0x8) compat_50__lwp_park(&(0x7f0000000440)={0x101, 0x8}, 0x0, 0x0, &(0x7f0000000480)=0x1) shmctl$SHM_LOCK(0x0, 0x3) syz_emit_ethernet(0x54, &(0x7f0000000000)="afe8d2383c3ef2ff5f6e8c9a4d35c602198766ff84e3acfd84f778353f61842e8a198d7e500cce4b813690338589cca07acde0e9834fd09b00f3146ba0ddb9ac17ae011fb861af258325476c9621388eca4b11c0") syz_execute_func(&(0x7f0000000080)="3642833dd70b586a463e640febb19b000000c4c3f97c6d4ca6f344a6c443857fc618fdc403116f26fa447ba9642e470f12fc64f20f116200") syz_extract_tcp_res(&(0x7f00000000c0), 0x3, 0xc5d) r6 = syz_usb_connect(0xfffffffffffffeff, 0x78, &(0x7f0000000100)="935e057aa8ddaece20394d86c45c4a50f7278a8791b0cdbcf6e832b9052f3ab99063705506c66eb3374aad5e1fded7515c8cb7fe2c216994ffb89e915496a207404168e86ba82f7c0d4964e7e756ac689919cf9f4bf87d3e3b5174605c8f256d28204e428bf295ff312e2e27a1c2afb893dbe7867060c650", &(0x7f0000000180)="2b06574b32d8eb1d7479431c2ab53fc2d24cbf1c4368a824c8cd8217f44d51b11889f929d65e7900cccb5166453ffe7fa1f328df9695a4457b58f13191e90ca794186e763217e923bdc4f20434eba96616cc3b27ea87d54a6c434d77b737d18d0d8d537dce330d2759be67e22e1dc617f517147faeb44e343d630b33d1d31606b2a3971efcd03e4d87b5e600323723abfef81aa71bea16bdf9561aca75ce3265de3ac8a1590a61ede73a7b6943ddf3e3fcf19e478f55859db78d6baa46b7a7e2159f6f4f20ccb18d5c41fb430249") syz_usb_disconnect(r6) csource_test.go:124: failed to build program: // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static unsigned long long procid; static void kill_and_wait(int pid, int* status) { kill(pid, SIGKILL); while (waitpid(-1, status, 0) != pid) { } } static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void thread_start(void* (*fn)(void*), void* arg) { pthread_t th; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 128 << 10); int i; for (i = 0; i < 100; i++) { if (pthread_create(&th, &attr, fn, arg) == 0) { pthread_attr_destroy(&attr); return; } if (errno == EAGAIN) { usleep(50); continue; } break; } exit(1); } typedef struct { pthread_mutex_t mu; pthread_cond_t cv; int state; } event_t; static void event_init(event_t* ev) { if (pthread_mutex_init(&ev->mu, 0)) exit(1); if (pthread_cond_init(&ev->cv, 0)) exit(1); ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { pthread_mutex_lock(&ev->mu); if (ev->state) exit(1); ev->state = 1; pthread_mutex_unlock(&ev->mu); pthread_cond_broadcast(&ev->cv); } static void event_wait(event_t* ev) { pthread_mutex_lock(&ev->mu); while (!ev->state) pthread_cond_wait(&ev->cv, &ev->mu); pthread_mutex_unlock(&ev->mu); } static int event_isset(event_t* ev) { pthread_mutex_lock(&ev->mu); int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; pthread_mutex_lock(&ev->mu); for (;;) { if (ev->state) break; uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; pthread_cond_timedwait(&ev->cv, &ev->mu, &ts); now = current_time_ms(); if (now - start > timeout) break; } int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } /* -------------------------------------------------------------------------- */ /* * Redefinitions to match the linux types used in common_usb.h. */ struct usb_endpoint_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; uint16_t wMaxPacketSize; uint8_t bInterval; uint8_t bRefresh; uint8_t bSynchAddress; } __attribute__((packed)); struct usb_device_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint16_t idVendor; uint16_t idProduct; uint16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; } __attribute__((packed)); struct usb_config_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; } __attribute__((packed)); struct usb_interface_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bNumEndpoints; uint8_t bInterfaceClass; uint8_t bInterfaceSubClass; uint8_t bInterfaceProtocol; uint8_t iInterface; } __attribute__((packed)); struct usb_ctrlrequest { uint8_t bRequestType; uint8_t bRequest; uint16_t wValue; uint16_t wIndex; uint16_t wLength; } __attribute__((packed)); struct usb_qualifier_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint8_t bNumConfigurations; uint8_t bRESERVED; } __attribute__((packed)); #define USB_TYPE_MASK (0x03 << 5) #define USB_TYPE_STANDARD (0x00 << 5) #define USB_TYPE_CLASS (0x01 << 5) #define USB_TYPE_VENDOR (0x02 << 5) #define USB_TYPE_RESERVED (0x03 << 5) #define USB_DT_DEVICE 0x01 #define USB_DT_CONFIG 0x02 #define USB_DT_STRING 0x03 #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_DEVICE_QUALIFIER 0x06 #define USB_DT_OTHER_SPEED_CONFIG 0x07 #define USB_DT_INTERFACE_POWER 0x08 #define USB_DT_OTG 0x09 #define USB_DT_DEBUG 0x0a #define USB_DT_INTERFACE_ASSOCIATION 0x0b #define USB_DT_SECURITY 0x0c #define USB_DT_KEY 0x0d #define USB_DT_ENCRYPTION_TYPE 0x0e #define USB_DT_BOS 0x0f #define USB_DT_DEVICE_CAPABILITY 0x10 #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 #define USB_DT_WIRE_ADAPTER 0x21 #define USB_DT_RPIPE 0x22 #define USB_DT_CS_RADIO_CONTROL 0x23 #define USB_DT_PIPE_USAGE 0x24 #define USB_DT_SS_ENDPOINT_COMP 0x30 #define USB_DT_SSP_ISOC_ENDPOINT_COMP 0x31 #define USB_REQ_GET_STATUS 0x00 #define USB_REQ_CLEAR_FEATURE 0x01 #define USB_REQ_SET_FEATURE 0x03 #define USB_REQ_SET_ADDRESS 0x05 #define USB_REQ_GET_DESCRIPTOR 0x06 #define USB_REQ_SET_DESCRIPTOR 0x07 #define USB_REQ_GET_CONFIGURATION 0x08 #define USB_REQ_SET_CONFIGURATION 0x09 #define USB_REQ_GET_INTERFACE 0x0A #define USB_REQ_SET_INTERFACE 0x0B #define USB_REQ_SYNCH_FRAME 0x0C #define USB_REQ_SET_SEL 0x30 #define USB_REQ_SET_ISOCH_DELAY 0x31 #define USB_REQ_SET_ENCRYPTION 0x0D #define USB_REQ_GET_ENCRYPTION 0x0E #define USB_REQ_RPIPE_ABORT 0x0E #define USB_REQ_SET_HANDSHAKE 0x0F #define USB_REQ_RPIPE_RESET 0x0F #define USB_REQ_GET_HANDSHAKE 0x10 #define USB_REQ_SET_CONNECTION 0x11 #define USB_REQ_SET_SECURITY_DATA 0x12 #define USB_REQ_GET_SECURITY_DATA 0x13 #define USB_REQ_SET_WUSB_DATA 0x14 #define USB_REQ_LOOPBACK_DATA_WRITE 0x15 #define USB_REQ_LOOPBACK_DATA_READ 0x16 #define USB_REQ_SET_INTERFACE_DS 0x17 #define USB_REQ_GET_PARTNER_PDO 20 #define USB_REQ_GET_BATTERY_STATUS 21 #define USB_REQ_SET_PDO 22 #define USB_REQ_GET_VDM 23 #define USB_REQ_SEND_VDM 24 #define USB_MAX_IFACE_NUM 4 #define USB_MAX_EP_NUM 32 #define USB_MAX_FDS 6 struct usb_endpoint_index { struct usb_endpoint_descriptor desc; int handle; }; struct usb_iface_index { struct usb_interface_descriptor* iface; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bInterfaceClass; struct usb_endpoint_index eps[USB_MAX_EP_NUM]; int eps_num; }; struct usb_device_index { struct usb_device_descriptor* dev; struct usb_config_descriptor* config; uint8_t bDeviceClass; uint8_t bMaxPower; int config_length; struct usb_iface_index ifaces[USB_MAX_IFACE_NUM]; int ifaces_num; int iface_cur; }; struct usb_info { int fd; struct usb_device_index index; }; static struct usb_info usb_devices[USB_MAX_FDS]; static int usb_devices_num; static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index) { if (length < sizeof(*index->dev) + sizeof(*index->config)) return false; memset(index, 0, sizeof(*index)); index->dev = (struct usb_device_descriptor*)buffer; index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev)); index->bDeviceClass = index->dev->bDeviceClass; index->bMaxPower = index->config->bMaxPower; index->config_length = length - sizeof(*index->dev); index->iface_cur = -1; size_t offset = 0; while (true) { if (offset + 1 >= length) break; uint8_t desc_length = buffer[offset]; uint8_t desc_type = buffer[offset + 1]; if (desc_length <= 2) break; if (offset + desc_length > length) break; if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) { struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset); index->ifaces[index->ifaces_num].iface = iface; index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber; index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting; index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass; index->ifaces_num++; } if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) { struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1]; if (iface->eps_num < USB_MAX_EP_NUM) { memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc)); iface->eps_num++; } } offset += desc_length; } return true; } static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len) { int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED); if (i >= USB_MAX_FDS) return NULL; int rv = 0; rv = parse_usb_descriptor(dev, dev_len, &usb_devices[i].index); if (!rv) return NULL; __atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE); return &usb_devices[i].index; } static struct usb_device_index* lookup_usb_index(int fd) { int i; for (i = 0; i < USB_MAX_FDS; i++) { if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd) { return &usb_devices[i].index; } } return NULL; } struct vusb_connect_string_descriptor { uint32_t len; char* str; } __attribute__((packed)); struct vusb_connect_descriptors { uint32_t qual_len; char* qual; uint32_t bos_len; char* bos; uint32_t strs_len; struct vusb_connect_string_descriptor strs[0]; } __attribute__((packed)); static const char default_string[] = { 8, USB_DT_STRING, 's', 0, 'y', 0, 'z', 0 }; static const char default_lang_id[] = { 4, USB_DT_STRING, 0x09, 0x04 }; static bool lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, char** response_data, uint32_t* response_length) { struct usb_device_index* index = lookup_usb_index(fd); uint8_t str_idx; if (!index) return false; switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_GET_DESCRIPTOR: switch (ctrl->wValue >> 8) { case USB_DT_DEVICE: *response_data = (char*)index->dev; *response_length = sizeof(*index->dev); return true; case USB_DT_CONFIG: *response_data = (char*)index->config; *response_length = index->config_length; return true; case USB_DT_STRING: str_idx = (uint8_t)ctrl->wValue; if (descs && str_idx < descs->strs_len) { *response_data = descs->strs[str_idx].str; *response_length = descs->strs[str_idx].len; return true; } if (str_idx == 0) { *response_data = (char*)&default_lang_id[0]; *response_length = default_lang_id[0]; return true; } *response_data = (char*)&default_string[0]; *response_length = default_string[0]; return true; case USB_DT_BOS: *response_data = descs->bos; *response_length = descs->bos_len; return true; case USB_DT_DEVICE_QUALIFIER: if (!descs->qual) { struct usb_qualifier_descriptor* qual = (struct usb_qualifier_descriptor*)response_data; qual->bLength = sizeof(*qual); qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; qual->bcdUSB = index->dev->bcdUSB; qual->bDeviceClass = index->dev->bDeviceClass; qual->bDeviceSubClass = index->dev->bDeviceSubClass; qual->bDeviceProtocol = index->dev->bDeviceProtocol; qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0; qual->bNumConfigurations = index->dev->bNumConfigurations; qual->bRESERVED = 0; *response_length = sizeof(*qual); return true; } *response_data = descs->qual; *response_length = descs->qual_len; return true; default: break; } break; default: break; } break; default: break; } return false; } typedef bool (*lookup_connect_out_response_t)(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done); static bool lookup_connect_response_out_generic(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done) { switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_SET_CONFIGURATION: *done = true; return true; default: break; } break; } return false; } /* -------------------------------------------------------------------------- */ static int vhci_open(void) { return open("/dev/vhci", O_RDWR); } static int vhci_setport(int fd, u_int port) { struct vhci_ioc_set_port args; args.port = port; return ioctl(fd, VHCI_IOC_SET_PORT, &args); } static int vhci_usb_attach(int fd) { return ioctl(fd, VHCI_IOC_USB_ATTACH, NULL); } static int vhci_usb_recv(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = read(fd, ptr, size); if (done < 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } static int vhci_usb_send(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = write(fd, ptr, size); if (done <= 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } /* -------------------------------------------------------------------------- */ static volatile long syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev, const struct vusb_connect_descriptors* descs, lookup_connect_out_response_t lookup_connect_response_out) { struct usb_device_index* index; int portnum, fd, rv; bool done; portnum = procid + 1; if (!dev) { return -1; } if (portnum != 1) { /* For now, we support only one proc. */ return -1; } fd = vhci_open(); if (fd < 0) { return -1; } index = add_usb_index(fd, dev, dev_len); if (!index) { goto err; } rv = vhci_setport(fd, portnum); if (rv != 0) { goto err; } rv = vhci_usb_attach(fd); if (rv != 0) { goto err; } done = false; while (!done) { vhci_request_t req; rv = vhci_usb_recv(fd, &req, sizeof(req)); if (rv != 0) { goto err; } if (req.type != VHCI_REQ_CTRL) { goto err; } char* response_data = NULL; uint32_t response_length = 0; char data[4096]; if (req.u.ctrl.bmRequestType & UE_DIR_IN) { bool response_found = false; response_found = lookup_connect_response_in(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &response_data, &response_length); if (!response_found) { goto err; } } else { if (!lookup_connect_response_out(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &done)) { goto err; } response_data = NULL; response_length = UGETW(req.u.ctrl.wLength); } if ((req.u.ctrl.bmRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD && req.u.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) { /* TODO: possibly revisit */ } if (response_length > sizeof(data)) response_length = 0; if ((uint32_t)UGETW(req.u.ctrl.wLength) < response_length) response_length = UGETW(req.u.ctrl.wLength); if (response_data) memcpy(data, response_data, response_length); else memset(data, 0, response_length); if (req.u.ctrl.bmRequestType & UE_DIR_IN) { if (response_length > 0) { vhci_response_t res; res.size = response_length; rv = vhci_usb_send(fd, &res, sizeof(res)); if (rv == 0) rv = vhci_usb_send(fd, data, response_length); } } else { rv = vhci_usb_recv(fd, data, response_length); } if (rv < 0) { goto err; } } sleep_ms(200); return fd; err: close(fd); return -1; } static volatile long syz_usb_connect(volatile long a0, volatile long a1, volatile long a2, volatile long a3) { uint64_t speed = a0; uint64_t dev_len = a1; const char* dev = (const char*)a2; const struct vusb_connect_descriptors* descs = (const struct vusb_connect_descriptors*)a3; return syz_usb_connect_impl(speed, dev_len, dev, descs, &lookup_connect_response_out_generic); } static volatile long syz_usb_disconnect(volatile long a0) { int fd = a0; int rv = close(fd); sleep_ms(200); return rv; } static void sandbox_common() { if (setsid() == -1) exit(1); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 8 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 0; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); } static void loop(); static int do_sandbox_none(void) { sandbox_common(); loop(); return 0; } static long syz_execute_func(volatile long text) { volatile long p[8] = {0}; (void)p; asm volatile("" ::"r"(0l), "r"(1l), "r"(2l), "r"(3l), "r"(4l), "r"(5l), "r"(6l), "r"(7l), "r"(8l), "r"(9l), "r"(10l), "r"(11l), "r"(12l), "r"(13l)); ((void (*)(void))(text))(); return 0; } struct thread_t { int created, call; event_t ready, done; }; static struct thread_t threads[16]; static void execute_call(int call); static int running; static void* thr(void* arg) { struct thread_t* th = (struct thread_t*)arg; for (;;) { event_wait(&th->ready); event_reset(&th->ready); execute_call(th->call); __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED); event_set(&th->done); } return 0; } static void execute_one(void) { int i, call, thread; for (call = 0; call < 15; call++) { for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); thread++) { struct thread_t* th = &threads[thread]; if (!th->created) { th->created = 1; event_init(&th->ready); event_init(&th->done); event_set(&th->done); thread_start(thr, th); } if (!event_isset(&th->done)) continue; event_reset(&th->done); th->call = call; __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED); event_set(&th->ready); event_timedwait(&th->done, 45 + (call == 13 ? 3000 : 0) + (call == 14 ? 300 : 0)); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter; for (iter = 0;; iter++) { int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5 * 1000) continue; kill_and_wait(pid, &status); break; } } } #ifndef SYS__lwp_create #define SYS__lwp_create 309 #endif #ifndef SYS__lwp_kill #define SYS__lwp_kill 318 #endif #ifndef SYS__lwp_self #define SYS__lwp_self 311 #endif #ifndef SYS__lwp_unpark_all #define SYS__lwp_unpark_all 322 #endif #ifndef SYS_compat_50__lwp_park #define SYS_compat_50__lwp_park 320 #endif #ifndef SYS_mmap #define SYS_mmap 197 #endif #ifndef SYS_shmctl #define SYS_shmctl 443 #endif uint64_t r[7] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: res = syscall(SYS__lwp_self); if (res != -1) r[0] = res; break; case 1: *(uint32_t*)0x20000100 = 0x400f0020; *(uint64_t*)0x20000108 = 0x20000080; *(uint32_t*)0x20000080 = 0; *(uint64_t*)0x20000088 = 0x20000000; *(uint32_t*)0x20000000 = 0x400f0023; *(uint64_t*)0x20000008 = 0; *(uint32_t*)0x20000010 = 7; *(uint32_t*)0x20000014 = 7; *(uint32_t*)0x20000018 = 0x101; *(uint32_t*)0x2000001c = 0x1f; *(uint64_t*)0x20000020 = 1; *(uint64_t*)0x20000028 = 0x400; *(uint32_t*)0x20000030 = 6; *(uint64_t*)0x20000038 = 1; *(uint64_t*)0x20000040 = 0x80000000; memcpy((void*)0x20000048, "\000", 1); *(uint32_t*)0x20000090 = 3; *(uint32_t*)0x20000094 = 5; *(uint32_t*)0x20000098 = 1; *(uint32_t*)0x2000009c = 0x35f7192e; *(uint64_t*)0x200000a0 = 0xd; *(uint64_t*)0x200000a8 = 0x100000000; *(uint32_t*)0x200000b0 = 5; *(uint64_t*)0x200000b8 = 6; *(uint64_t*)0x200000c0 = 0x10001; memcpy((void*)0x200000c8, "\000", 1); *(uint32_t*)0x20000110 = 0xf1b; *(uint32_t*)0x20000114 = 0x401; *(uint32_t*)0x20000118 = 0x69; *(uint32_t*)0x2000011c = 0x100; *(uint64_t*)0x20000120 = 1; *(uint64_t*)0x20000128 = 0; *(uint32_t*)0x20000130 = 0x2ecd3534; *(uint64_t*)0x20000138 = 1; *(uint64_t*)0x20000140 = 0x400; memcpy((void*)0x20000148, "-\000", 2); res = syscall(SYS__lwp_create, 0x20000100ul, 0x60ul, 0x20000180ul); if (res != -1) r[1] = *(uint32_t*)0x20000180; break; case 2: res = syscall(SYS__lwp_self); if (res != -1) r[2] = res; break; case 3: *(uint32_t*)0x200001c0 = 2; *(uint64_t*)0x200001c8 = 0; *(uint32_t*)0x200001d0 = 9; *(uint32_t*)0x200001d4 = 0x3ae; *(uint32_t*)0x200001d8 = 3; *(uint32_t*)0x200001dc = 0xc699; *(uint64_t*)0x200001e0 = 0x81; *(uint64_t*)0x200001e8 = 2; *(uint32_t*)0x200001f0 = 1; *(uint64_t*)0x200001f8 = 9; *(uint64_t*)0x20000200 = 7; memcpy((void*)0x20000208, "\000", 1); res = syscall(SYS__lwp_create, 0x200001c0ul, 0x40ul, 0x20000240ul); if (res != -1) r[3] = *(uint32_t*)0x20000240; break; case 4: res = syscall(SYS__lwp_self); if (res != -1) r[4] = res; break; case 5: *(uint32_t*)0x20000280 = r[0]; *(uint32_t*)0x20000284 = r[1]; *(uint32_t*)0x20000288 = r[2]; *(uint32_t*)0x2000028c = r[3]; *(uint32_t*)0x20000290 = r[4]; *(uint64_t*)0x200002c0 = 4; syscall(SYS__lwp_unpark_all, 0x20000280ul, 5ul, 0x200002c0ul); break; case 6: *(uint32_t*)0x20000380 = 8; *(uint64_t*)0x20000388 = 0x20000300; *(uint32_t*)0x20000300 = 0x400f002c; *(uint64_t*)0x20000308 = 0; *(uint32_t*)0x20000310 = 0x80000000; *(uint32_t*)0x20000314 = 0xa706; *(uint32_t*)0x20000318 = 0; *(uint32_t*)0x2000031c = 8; *(uint64_t*)0x20000320 = 0x1f; *(uint64_t*)0x20000328 = 6; *(uint32_t*)0x20000330 = 1; *(uint64_t*)0x20000338 = 0x101; *(uint64_t*)0x20000340 = 5; memcpy((void*)0x20000348, "\000", 1); *(uint32_t*)0x20000390 = 2; *(uint32_t*)0x20000394 = 9; *(uint32_t*)0x20000398 = 0x10000; *(uint32_t*)0x2000039c = 1; *(uint64_t*)0x200003a0 = 2; *(uint64_t*)0x200003a8 = 0xffffffff; *(uint32_t*)0x200003b0 = 5; *(uint64_t*)0x200003b8 = 0x20; *(uint64_t*)0x200003c0 = 0xfff; memcpy((void*)0x200003c8, "\000", 1); res = syscall(SYS__lwp_create, 0x20000380ul, 0x40ul, 0x20000400ul); if (res != -1) r[5] = *(uint32_t*)0x20000400; break; case 7: syscall(SYS__lwp_kill, r[5], 8); break; case 8: *(uint32_t*)0x20000440 = 0x101; *(uint64_t*)0x20000448 = 8; *(uint64_t*)0x20000480 = 1; syscall(SYS_compat_50__lwp_park, 0x20000440ul, 0, 0ul, 0x20000480ul); break; case 9: syscall(SYS_shmctl, 0, 3ul, 0); break; case 10: memcpy((void*)0x20000000, "\xaf\xe8\xd2\x38\x3c\x3e\xf2\xff\x5f\x6e\x8c\x9a\x4d\x35\xc6\x02\x19\x87\x66\xff\x84\xe3\xac\xfd\x84\xf7\x78\x35\x3f\x61\x84\x2e\x8a\x19\x8d\x7e\x50\x0c\xce\x4b\x81\x36\x90\x33\x85\x89\xcc\xa0\x7a\xcd\xe0\xe9\x83\x4f\xd0\x9b\x00\xf3\x14\x6b\xa0\xdd\xb9\xac\x17\xae\x01\x1f\xb8\x61\xaf\x25\x83\x25\x47\x6c\x96\x21\x38\x8e\xca\x4b\x11\xc0", 84); break; case 11: memcpy((void*)0x20000080, "\x36\x42\x83\x3d\xd7\x0b\x58\x6a\x46\x3e\x64\x0f\xeb\xb1\x9b\x00\x00\x00\xc4\xc3\xf9\x7c\x6d\x4c\xa6\xf3\x44\xa6\xc4\x43\x85\x7f\xc6\x18\xfd\xc4\x03\x11\x6f\x26\xfa\x44\x7b\xa9\x64\x2e\x47\x0f\x12\xfc\x64\xf2\x0f\x11\x62\x00", 56); syz_execute_func(0x20000080); break; case 12: break; case 13: memcpy((void*)0x20000100, "\x93\x5e\x05\x7a\xa8\xdd\xae\xce\x20\x39\x4d\x86\xc4\x5c\x4a\x50\xf7\x27\x8a\x87\x91\xb0\xcd\xbc\xf6\xe8\x32\xb9\x05\x2f\x3a\xb9\x90\x63\x70\x55\x06\xc6\x6e\xb3\x37\x4a\xad\x5e\x1f\xde\xd7\x51\x5c\x8c\xb7\xfe\x2c\x21\x69\x94\xff\xb8\x9e\x91\x54\x96\xa2\x07\x40\x41\x68\xe8\x6b\xa8\x2f\x7c\x0d\x49\x64\xe7\xe7\x56\xac\x68\x99\x19\xcf\x9f\x4b\xf8\x7d\x3e\x3b\x51\x74\x60\x5c\x8f\x25\x6d\x28\x20\x4e\x42\x8b\xf2\x95\xff\x31\x2e\x2e\x27\xa1\xc2\xaf\xb8\x93\xdb\xe7\x86\x70\x60\xc6\x50", 120); memcpy((void*)0x20000180, "\x2b\x06\x57\x4b\x32\xd8\xeb\x1d\x74\x79\x43\x1c\x2a\xb5\x3f\xc2\xd2\x4c\xbf\x1c\x43\x68\xa8\x24\xc8\xcd\x82\x17\xf4\x4d\x51\xb1\x18\x89\xf9\x29\xd6\x5e\x79\x00\xcc\xcb\x51\x66\x45\x3f\xfe\x7f\xa1\xf3\x28\xdf\x96\x95\xa4\x45\x7b\x58\xf1\x31\x91\xe9\x0c\xa7\x94\x18\x6e\x76\x32\x17\xe9\x23\xbd\xc4\xf2\x04\x34\xeb\xa9\x66\x16\xcc\x3b\x27\xea\x87\xd5\x4a\x6c\x43\x4d\x77\xb7\x37\xd1\x8d\x0d\x8d\x53\x7d\xce\x33\x0d\x27\x59\xbe\x67\xe2\x2e\x1d\xc6\x17\xf5\x17\x14\x7f\xae\xb4\x4e\x34\x3d\x63\x0b\x33\xd1\xd3\x16\x06\xb2\xa3\x97\x1e\xfc\xd0\x3e\x4d\x87\xb5\xe6\x00\x32\x37\x23\xab\xfe\xf8\x1a\xa7\x1b\xea\x16\xbd\xf9\x56\x1a\xca\x75\xce\x32\x65\xde\x3a\xc8\xa1\x59\x0a\x61\xed\xe7\x3a\x7b\x69\x43\xdd\xf3\xe3\xfc\xf1\x9e\x47\x8f\x55\x85\x9d\xb7\x8d\x6b\xaa\x46\xb7\xa7\xe2\x15\x9f\x6f\x4f\x20\xcc\xb1\x8d\x5c\x41\xfb\x43\x02\x49", 206); res = syz_usb_connect(0xfffffffffffffeff, 0x78, 0x20000100, 0x20000180); if (res != -1) r[6] = res; break; case 14: syz_usb_disconnect(r[6]); break; } } int main(void) { syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul, 0ul); do_sandbox_none(); return 0; } : In function 'syz_usb_connect_impl': :596:63: error: unknown type name 'usb_ctrlrequest' :601:55: error: unknown type name 'usb_ctrlrequest' compiler invocation: /syzkaller/netbsd/src/../tools/bin/x86_64--netbsd-g++ [-o /tmp/syz-executor005609906 -DGOOS_netbsd=1 -DGOARCH_amd64=1 -DHOSTGOOS_linux=1 -x c - -m64 --sysroot /syzkaller/netbsd/src/../dest/ -O2 -pthread -Wall -Werror -Wparentheses -Wframe-larger-than=16384] --- FAIL: TestGenerate/netbsd/amd64/12 (0.40s) csource_test.go:123: opts: {Threaded:true Collide:false Repeat:true RepeatTimes:0 Procs:0 Sandbox:none Fault:false FaultCall:0 FaultNth:0 Leak:false NetInjection:false NetDevices:false NetReset:false Cgroups:false BinfmtMisc:false CloseFDs:false KCSAN:false DevlinkPCI:false USB:false UseTmpDir:true HandleSegv:false Repro:true Trace:false} program: r0 = _lwp_self() _lwp_create(&(0x7f0000000100)={0x400f0020, &(0x7f0000000080)={0x0, &(0x7f0000000000)={0x400f0023, 0x0, {[0x7, 0x7, 0x101, 0x1f]}, {0x1, 0x400, 0x6}, {0x1, 0x80000000, '\x00'}}, {[0x3, 0x5, 0x1, 0x35f7192e]}, {0xd, 0x100000000, 0x5}, {0x6, 0x10001, '\x00'}}, {[0xf1b, 0x401, 0x69, 0x100]}, {0x1, 0x0, 0x16ac7582ecd3534}, {0x1, 0x400, '-\x00'}}, 0x60, &(0x7f0000000180)=0x0) r2 = _lwp_self() _lwp_create(&(0x7f00000001c0)={0x2, 0x0, {[0x9, 0x3ae, 0x3, 0xc699]}, {0x81, 0x2, 0x1}, {0x9, 0x7, '\x00'}}, 0x40, &(0x7f0000000240)=0x0) r4 = _lwp_self() _lwp_unpark_all(&(0x7f0000000280)=[r0, r1, r2, r3, r4], 0x5, &(0x7f00000002c0)=0x4) _lwp_create(&(0x7f0000000380)={0x8, &(0x7f0000000300)={0x400f002c, 0x0, {[0x80000000, 0xa706, 0x0, 0x8]}, {0x1f, 0x6, 0x1}, {0x101, 0x5, '\x00'}}, {[0x2, 0x9, 0x10000, 0x1]}, {0x2, 0xffffffff, 0x5}, {0x20, 0xfff, '\x00'}}, 0x40, &(0x7f0000000400)=0x0) _lwp_kill(r5, 0x8) compat_50__lwp_park(&(0x7f0000000440)={0x101, 0x8}, 0x0, 0x0, &(0x7f0000000480)=0x1) shmctl$SHM_LOCK(0x0, 0x3) syz_emit_ethernet(0x54, &(0x7f0000000000)="afe8d2383c3ef2ff5f6e8c9a4d35c602198766ff84e3acfd84f778353f61842e8a198d7e500cce4b813690338589cca07acde0e9834fd09b00f3146ba0ddb9ac17ae011fb861af258325476c9621388eca4b11c0") syz_execute_func(&(0x7f0000000080)="3642833dd70b586a463e640febb19b000000c4c3f97c6d4ca6f344a6c443857fc618fdc403116f26fa447ba9642e470f12fc64f20f116200") syz_extract_tcp_res(&(0x7f00000000c0), 0x3, 0xc5d) r6 = syz_usb_connect(0xfffffffffffffeff, 0x78, &(0x7f0000000100)="935e057aa8ddaece20394d86c45c4a50f7278a8791b0cdbcf6e832b9052f3ab99063705506c66eb3374aad5e1fded7515c8cb7fe2c216994ffb89e915496a207404168e86ba82f7c0d4964e7e756ac689919cf9f4bf87d3e3b5174605c8f256d28204e428bf295ff312e2e27a1c2afb893dbe7867060c650", &(0x7f0000000180)="2b06574b32d8eb1d7479431c2ab53fc2d24cbf1c4368a824c8cd8217f44d51b11889f929d65e7900cccb5166453ffe7fa1f328df9695a4457b58f13191e90ca794186e763217e923bdc4f20434eba96616cc3b27ea87d54a6c434d77b737d18d0d8d537dce330d2759be67e22e1dc617f517147faeb44e343d630b33d1d31606b2a3971efcd03e4d87b5e600323723abfef81aa71bea16bdf9561aca75ce3265de3ac8a1590a61ede73a7b6943ddf3e3fcf19e478f55859db78d6baa46b7a7e2159f6f4f20ccb18d5c41fb430249") syz_usb_disconnect(r6) csource_test.go:124: failed to build program: // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static unsigned long long procid; static void kill_and_wait(int pid, int* status) { kill(pid, SIGKILL); while (waitpid(-1, status, 0) != pid) { } } static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void use_temporary_dir(void) { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } static void remove_dir(const char* dir) { DIR* dp; struct dirent* ep; dp = opendir(dir); if (dp == NULL) exit(1); while ((ep = readdir(dp))) { if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue; char filename[FILENAME_MAX]; snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } if (unlink(filename)) exit(1); } closedir(dp); if (rmdir(dir)) exit(1); } static void thread_start(void* (*fn)(void*), void* arg) { pthread_t th; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 128 << 10); int i; for (i = 0; i < 100; i++) { if (pthread_create(&th, &attr, fn, arg) == 0) { pthread_attr_destroy(&attr); return; } if (errno == EAGAIN) { usleep(50); continue; } break; } exit(1); } typedef struct { pthread_mutex_t mu; pthread_cond_t cv; int state; } event_t; static void event_init(event_t* ev) { if (pthread_mutex_init(&ev->mu, 0)) exit(1); if (pthread_cond_init(&ev->cv, 0)) exit(1); ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { pthread_mutex_lock(&ev->mu); if (ev->state) exit(1); ev->state = 1; pthread_mutex_unlock(&ev->mu); pthread_cond_broadcast(&ev->cv); } static void event_wait(event_t* ev) { pthread_mutex_lock(&ev->mu); while (!ev->state) pthread_cond_wait(&ev->cv, &ev->mu); pthread_mutex_unlock(&ev->mu); } static int event_isset(event_t* ev) { pthread_mutex_lock(&ev->mu); int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; pthread_mutex_lock(&ev->mu); for (;;) { if (ev->state) break; uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; pthread_cond_timedwait(&ev->cv, &ev->mu, &ts); now = current_time_ms(); if (now - start > timeout) break; } int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } /* -------------------------------------------------------------------------- */ /* * Redefinitions to match the linux types used in common_usb.h. */ struct usb_endpoint_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; uint16_t wMaxPacketSize; uint8_t bInterval; uint8_t bRefresh; uint8_t bSynchAddress; } __attribute__((packed)); struct usb_device_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint16_t idVendor; uint16_t idProduct; uint16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; } __attribute__((packed)); struct usb_config_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; } __attribute__((packed)); struct usb_interface_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bNumEndpoints; uint8_t bInterfaceClass; uint8_t bInterfaceSubClass; uint8_t bInterfaceProtocol; uint8_t iInterface; } __attribute__((packed)); struct usb_ctrlrequest { uint8_t bRequestType; uint8_t bRequest; uint16_t wValue; uint16_t wIndex; uint16_t wLength; } __attribute__((packed)); struct usb_qualifier_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint8_t bNumConfigurations; uint8_t bRESERVED; } __attribute__((packed)); #define USB_TYPE_MASK (0x03 << 5) #define USB_TYPE_STANDARD (0x00 << 5) #define USB_TYPE_CLASS (0x01 << 5) #define USB_TYPE_VENDOR (0x02 << 5) #define USB_TYPE_RESERVED (0x03 << 5) #define USB_DT_DEVICE 0x01 #define USB_DT_CONFIG 0x02 #define USB_DT_STRING 0x03 #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_DEVICE_QUALIFIER 0x06 #define USB_DT_OTHER_SPEED_CONFIG 0x07 #define USB_DT_INTERFACE_POWER 0x08 #define USB_DT_OTG 0x09 #define USB_DT_DEBUG 0x0a #define USB_DT_INTERFACE_ASSOCIATION 0x0b #define USB_DT_SECURITY 0x0c #define USB_DT_KEY 0x0d #define USB_DT_ENCRYPTION_TYPE 0x0e #define USB_DT_BOS 0x0f #define USB_DT_DEVICE_CAPABILITY 0x10 #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 #define USB_DT_WIRE_ADAPTER 0x21 #define USB_DT_RPIPE 0x22 #define USB_DT_CS_RADIO_CONTROL 0x23 #define USB_DT_PIPE_USAGE 0x24 #define USB_DT_SS_ENDPOINT_COMP 0x30 #define USB_DT_SSP_ISOC_ENDPOINT_COMP 0x31 #define USB_REQ_GET_STATUS 0x00 #define USB_REQ_CLEAR_FEATURE 0x01 #define USB_REQ_SET_FEATURE 0x03 #define USB_REQ_SET_ADDRESS 0x05 #define USB_REQ_GET_DESCRIPTOR 0x06 #define USB_REQ_SET_DESCRIPTOR 0x07 #define USB_REQ_GET_CONFIGURATION 0x08 #define USB_REQ_SET_CONFIGURATION 0x09 #define USB_REQ_GET_INTERFACE 0x0A #define USB_REQ_SET_INTERFACE 0x0B #define USB_REQ_SYNCH_FRAME 0x0C #define USB_REQ_SET_SEL 0x30 #define USB_REQ_SET_ISOCH_DELAY 0x31 #define USB_REQ_SET_ENCRYPTION 0x0D #define USB_REQ_GET_ENCRYPTION 0x0E #define USB_REQ_RPIPE_ABORT 0x0E #define USB_REQ_SET_HANDSHAKE 0x0F #define USB_REQ_RPIPE_RESET 0x0F #define USB_REQ_GET_HANDSHAKE 0x10 #define USB_REQ_SET_CONNECTION 0x11 #define USB_REQ_SET_SECURITY_DATA 0x12 #define USB_REQ_GET_SECURITY_DATA 0x13 #define USB_REQ_SET_WUSB_DATA 0x14 #define USB_REQ_LOOPBACK_DATA_WRITE 0x15 #define USB_REQ_LOOPBACK_DATA_READ 0x16 #define USB_REQ_SET_INTERFACE_DS 0x17 #define USB_REQ_GET_PARTNER_PDO 20 #define USB_REQ_GET_BATTERY_STATUS 21 #define USB_REQ_SET_PDO 22 #define USB_REQ_GET_VDM 23 #define USB_REQ_SEND_VDM 24 #define USB_MAX_IFACE_NUM 4 #define USB_MAX_EP_NUM 32 #define USB_MAX_FDS 6 struct usb_endpoint_index { struct usb_endpoint_descriptor desc; int handle; }; struct usb_iface_index { struct usb_interface_descriptor* iface; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bInterfaceClass; struct usb_endpoint_index eps[USB_MAX_EP_NUM]; int eps_num; }; struct usb_device_index { struct usb_device_descriptor* dev; struct usb_config_descriptor* config; uint8_t bDeviceClass; uint8_t bMaxPower; int config_length; struct usb_iface_index ifaces[USB_MAX_IFACE_NUM]; int ifaces_num; int iface_cur; }; struct usb_info { int fd; struct usb_device_index index; }; static struct usb_info usb_devices[USB_MAX_FDS]; static int usb_devices_num; static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index) { if (length < sizeof(*index->dev) + sizeof(*index->config)) return false; memset(index, 0, sizeof(*index)); index->dev = (struct usb_device_descriptor*)buffer; index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev)); index->bDeviceClass = index->dev->bDeviceClass; index->bMaxPower = index->config->bMaxPower; index->config_length = length - sizeof(*index->dev); index->iface_cur = -1; size_t offset = 0; while (true) { if (offset + 1 >= length) break; uint8_t desc_length = buffer[offset]; uint8_t desc_type = buffer[offset + 1]; if (desc_length <= 2) break; if (offset + desc_length > length) break; if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) { struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset); index->ifaces[index->ifaces_num].iface = iface; index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber; index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting; index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass; index->ifaces_num++; } if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) { struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1]; if (iface->eps_num < USB_MAX_EP_NUM) { memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc)); iface->eps_num++; } } offset += desc_length; } return true; } static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len) { int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED); if (i >= USB_MAX_FDS) return NULL; int rv = 0; rv = parse_usb_descriptor(dev, dev_len, &usb_devices[i].index); if (!rv) return NULL; __atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE); return &usb_devices[i].index; } static struct usb_device_index* lookup_usb_index(int fd) { int i; for (i = 0; i < USB_MAX_FDS; i++) { if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd) { return &usb_devices[i].index; } } return NULL; } struct vusb_connect_string_descriptor { uint32_t len; char* str; } __attribute__((packed)); struct vusb_connect_descriptors { uint32_t qual_len; char* qual; uint32_t bos_len; char* bos; uint32_t strs_len; struct vusb_connect_string_descriptor strs[0]; } __attribute__((packed)); static const char default_string[] = { 8, USB_DT_STRING, 's', 0, 'y', 0, 'z', 0 }; static const char default_lang_id[] = { 4, USB_DT_STRING, 0x09, 0x04 }; static bool lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, char** response_data, uint32_t* response_length) { struct usb_device_index* index = lookup_usb_index(fd); uint8_t str_idx; if (!index) return false; switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_GET_DESCRIPTOR: switch (ctrl->wValue >> 8) { case USB_DT_DEVICE: *response_data = (char*)index->dev; *response_length = sizeof(*index->dev); return true; case USB_DT_CONFIG: *response_data = (char*)index->config; *response_length = index->config_length; return true; case USB_DT_STRING: str_idx = (uint8_t)ctrl->wValue; if (descs && str_idx < descs->strs_len) { *response_data = descs->strs[str_idx].str; *response_length = descs->strs[str_idx].len; return true; } if (str_idx == 0) { *response_data = (char*)&default_lang_id[0]; *response_length = default_lang_id[0]; return true; } *response_data = (char*)&default_string[0]; *response_length = default_string[0]; return true; case USB_DT_BOS: *response_data = descs->bos; *response_length = descs->bos_len; return true; case USB_DT_DEVICE_QUALIFIER: if (!descs->qual) { struct usb_qualifier_descriptor* qual = (struct usb_qualifier_descriptor*)response_data; qual->bLength = sizeof(*qual); qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; qual->bcdUSB = index->dev->bcdUSB; qual->bDeviceClass = index->dev->bDeviceClass; qual->bDeviceSubClass = index->dev->bDeviceSubClass; qual->bDeviceProtocol = index->dev->bDeviceProtocol; qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0; qual->bNumConfigurations = index->dev->bNumConfigurations; qual->bRESERVED = 0; *response_length = sizeof(*qual); return true; } *response_data = descs->qual; *response_length = descs->qual_len; return true; default: break; } break; default: break; } break; default: break; } return false; } typedef bool (*lookup_connect_out_response_t)(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done); static bool lookup_connect_response_out_generic(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done) { switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_SET_CONFIGURATION: *done = true; return true; default: break; } break; } return false; } /* -------------------------------------------------------------------------- */ static int vhci_open(void) { return open("/dev/vhci", O_RDWR); } static int vhci_setport(int fd, u_int port) { struct vhci_ioc_set_port args; args.port = port; return ioctl(fd, VHCI_IOC_SET_PORT, &args); } static int vhci_usb_attach(int fd) { return ioctl(fd, VHCI_IOC_USB_ATTACH, NULL); } static int vhci_usb_recv(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = read(fd, ptr, size); if (done < 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } static int vhci_usb_send(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = write(fd, ptr, size); if (done <= 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } /* -------------------------------------------------------------------------- */ static volatile long syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev, const struct vusb_connect_descriptors* descs, lookup_connect_out_response_t lookup_connect_response_out) { struct usb_device_index* index; int portnum, fd, rv; bool done; portnum = procid + 1; if (!dev) { return -1; } if (portnum != 1) { /* For now, we support only one proc. */ return -1; } fd = vhci_open(); if (fd < 0) { return -1; } index = add_usb_index(fd, dev, dev_len); if (!index) { goto err; } rv = vhci_setport(fd, portnum); if (rv != 0) { goto err; } rv = vhci_usb_attach(fd); if (rv != 0) { goto err; } done = false; while (!done) { vhci_request_t req; rv = vhci_usb_recv(fd, &req, sizeof(req)); if (rv != 0) { goto err; } if (req.type != VHCI_REQ_CTRL) { goto err; } char* response_data = NULL; uint32_t response_length = 0; char data[4096]; if (req.u.ctrl.bmRequestType & UE_DIR_IN) { bool response_found = false; response_found = lookup_connect_response_in(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &response_data, &response_length); if (!response_found) { goto err; } } else { if (!lookup_connect_response_out(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &done)) { goto err; } response_data = NULL; response_length = UGETW(req.u.ctrl.wLength); } if ((req.u.ctrl.bmRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD && req.u.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) { /* TODO: possibly revisit */ } if (response_length > sizeof(data)) response_length = 0; if ((uint32_t)UGETW(req.u.ctrl.wLength) < response_length) response_length = UGETW(req.u.ctrl.wLength); if (response_data) memcpy(data, response_data, response_length); else memset(data, 0, response_length); if (req.u.ctrl.bmRequestType & UE_DIR_IN) { if (response_length > 0) { vhci_response_t res; res.size = response_length; rv = vhci_usb_send(fd, &res, sizeof(res)); if (rv == 0) rv = vhci_usb_send(fd, data, response_length); } } else { rv = vhci_usb_recv(fd, data, response_length); } if (rv < 0) { goto err; } } sleep_ms(200); return fd; err: close(fd); return -1; } static volatile long syz_usb_connect(volatile long a0, volatile long a1, volatile long a2, volatile long a3) { uint64_t speed = a0; uint64_t dev_len = a1; const char* dev = (const char*)a2; const struct vusb_connect_descriptors* descs = (const struct vusb_connect_descriptors*)a3; return syz_usb_connect_impl(speed, dev_len, dev, descs, &lookup_connect_response_out_generic); } static volatile long syz_usb_disconnect(volatile long a0) { int fd = a0; int rv = close(fd); sleep_ms(200); return rv; } static void sandbox_common() { if (setsid() == -1) exit(1); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 8 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 0; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); } static void loop(); static int do_sandbox_none(void) { sandbox_common(); loop(); return 0; } static long syz_execute_func(volatile long text) { volatile long p[8] = {0}; (void)p; asm volatile("" ::"r"(0l), "r"(1l), "r"(2l), "r"(3l), "r"(4l), "r"(5l), "r"(6l), "r"(7l), "r"(8l), "r"(9l), "r"(10l), "r"(11l), "r"(12l), "r"(13l)); ((void (*)(void))(text))(); return 0; } struct thread_t { int created, call; event_t ready, done; }; static struct thread_t threads[16]; static void execute_call(int call); static int running; static void* thr(void* arg) { struct thread_t* th = (struct thread_t*)arg; for (;;) { event_wait(&th->ready); event_reset(&th->ready); execute_call(th->call); __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED); event_set(&th->done); } return 0; } static void execute_one(void) { if (write(1, "executing program\n", sizeof("executing program\n") - 1)) { } int i, call, thread; for (call = 0; call < 15; call++) { for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); thread++) { struct thread_t* th = &threads[thread]; if (!th->created) { th->created = 1; event_init(&th->ready); event_init(&th->done); event_set(&th->done); thread_start(thr, th); } if (!event_isset(&th->done)) continue; event_reset(&th->done); th->call = call; __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED); event_set(&th->ready); event_timedwait(&th->done, 45 + (call == 13 ? 3000 : 0) + (call == 14 ? 300 : 0)); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter; for (iter = 0;; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5 * 1000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } #ifndef SYS__lwp_create #define SYS__lwp_create 309 #endif #ifndef SYS__lwp_kill #define SYS__lwp_kill 318 #endif #ifndef SYS__lwp_self #define SYS__lwp_self 311 #endif #ifndef SYS__lwp_unpark_all #define SYS__lwp_unpark_all 322 #endif #ifndef SYS_compat_50__lwp_park #define SYS_compat_50__lwp_park 320 #endif #ifndef SYS_mmap #define SYS_mmap 197 #endif #ifndef SYS_shmctl #define SYS_shmctl 443 #endif uint64_t r[7] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: res = syscall(SYS__lwp_self); if (res != -1) r[0] = res; break; case 1: *(uint32_t*)0x20000100 = 0x400f0020; *(uint64_t*)0x20000108 = 0x20000080; *(uint32_t*)0x20000080 = 0; *(uint64_t*)0x20000088 = 0x20000000; *(uint32_t*)0x20000000 = 0x400f0023; *(uint64_t*)0x20000008 = 0; *(uint32_t*)0x20000010 = 7; *(uint32_t*)0x20000014 = 7; *(uint32_t*)0x20000018 = 0x101; *(uint32_t*)0x2000001c = 0x1f; *(uint64_t*)0x20000020 = 1; *(uint64_t*)0x20000028 = 0x400; *(uint32_t*)0x20000030 = 6; *(uint64_t*)0x20000038 = 1; *(uint64_t*)0x20000040 = 0x80000000; memcpy((void*)0x20000048, "\000", 1); *(uint32_t*)0x20000090 = 3; *(uint32_t*)0x20000094 = 5; *(uint32_t*)0x20000098 = 1; *(uint32_t*)0x2000009c = 0x35f7192e; *(uint64_t*)0x200000a0 = 0xd; *(uint64_t*)0x200000a8 = 0x100000000; *(uint32_t*)0x200000b0 = 5; *(uint64_t*)0x200000b8 = 6; *(uint64_t*)0x200000c0 = 0x10001; memcpy((void*)0x200000c8, "\000", 1); *(uint32_t*)0x20000110 = 0xf1b; *(uint32_t*)0x20000114 = 0x401; *(uint32_t*)0x20000118 = 0x69; *(uint32_t*)0x2000011c = 0x100; *(uint64_t*)0x20000120 = 1; *(uint64_t*)0x20000128 = 0; *(uint32_t*)0x20000130 = 0x2ecd3534; *(uint64_t*)0x20000138 = 1; *(uint64_t*)0x20000140 = 0x400; memcpy((void*)0x20000148, "-\000", 2); res = syscall(SYS__lwp_create, 0x20000100ul, 0x60ul, 0x20000180ul); if (res != -1) r[1] = *(uint32_t*)0x20000180; break; case 2: res = syscall(SYS__lwp_self); if (res != -1) r[2] = res; break; case 3: *(uint32_t*)0x200001c0 = 2; *(uint64_t*)0x200001c8 = 0; *(uint32_t*)0x200001d0 = 9; *(uint32_t*)0x200001d4 = 0x3ae; *(uint32_t*)0x200001d8 = 3; *(uint32_t*)0x200001dc = 0xc699; *(uint64_t*)0x200001e0 = 0x81; *(uint64_t*)0x200001e8 = 2; *(uint32_t*)0x200001f0 = 1; *(uint64_t*)0x200001f8 = 9; *(uint64_t*)0x20000200 = 7; memcpy((void*)0x20000208, "\000", 1); res = syscall(SYS__lwp_create, 0x200001c0ul, 0x40ul, 0x20000240ul); if (res != -1) r[3] = *(uint32_t*)0x20000240; break; case 4: res = syscall(SYS__lwp_self); if (res != -1) r[4] = res; break; case 5: *(uint32_t*)0x20000280 = r[0]; *(uint32_t*)0x20000284 = r[1]; *(uint32_t*)0x20000288 = r[2]; *(uint32_t*)0x2000028c = r[3]; *(uint32_t*)0x20000290 = r[4]; *(uint64_t*)0x200002c0 = 4; syscall(SYS__lwp_unpark_all, 0x20000280ul, 5ul, 0x200002c0ul); break; case 6: *(uint32_t*)0x20000380 = 8; *(uint64_t*)0x20000388 = 0x20000300; *(uint32_t*)0x20000300 = 0x400f002c; *(uint64_t*)0x20000308 = 0; *(uint32_t*)0x20000310 = 0x80000000; *(uint32_t*)0x20000314 = 0xa706; *(uint32_t*)0x20000318 = 0; *(uint32_t*)0x2000031c = 8; *(uint64_t*)0x20000320 = 0x1f; *(uint64_t*)0x20000328 = 6; *(uint32_t*)0x20000330 = 1; *(uint64_t*)0x20000338 = 0x101; *(uint64_t*)0x20000340 = 5; memcpy((void*)0x20000348, "\000", 1); *(uint32_t*)0x20000390 = 2; *(uint32_t*)0x20000394 = 9; *(uint32_t*)0x20000398 = 0x10000; *(uint32_t*)0x2000039c = 1; *(uint64_t*)0x200003a0 = 2; *(uint64_t*)0x200003a8 = 0xffffffff; *(uint32_t*)0x200003b0 = 5; *(uint64_t*)0x200003b8 = 0x20; *(uint64_t*)0x200003c0 = 0xfff; memcpy((void*)0x200003c8, "\000", 1); res = syscall(SYS__lwp_create, 0x20000380ul, 0x40ul, 0x20000400ul); if (res != -1) r[5] = *(uint32_t*)0x20000400; break; case 7: syscall(SYS__lwp_kill, r[5], 8); break; case 8: *(uint32_t*)0x20000440 = 0x101; *(uint64_t*)0x20000448 = 8; *(uint64_t*)0x20000480 = 1; syscall(SYS_compat_50__lwp_park, 0x20000440ul, 0, 0ul, 0x20000480ul); break; case 9: syscall(SYS_shmctl, 0, 3ul, 0); break; case 10: memcpy((void*)0x20000000, "\xaf\xe8\xd2\x38\x3c\x3e\xf2\xff\x5f\x6e\x8c\x9a\x4d\x35\xc6\x02\x19\x87\x66\xff\x84\xe3\xac\xfd\x84\xf7\x78\x35\x3f\x61\x84\x2e\x8a\x19\x8d\x7e\x50\x0c\xce\x4b\x81\x36\x90\x33\x85\x89\xcc\xa0\x7a\xcd\xe0\xe9\x83\x4f\xd0\x9b\x00\xf3\x14\x6b\xa0\xdd\xb9\xac\x17\xae\x01\x1f\xb8\x61\xaf\x25\x83\x25\x47\x6c\x96\x21\x38\x8e\xca\x4b\x11\xc0", 84); break; case 11: memcpy((void*)0x20000080, "\x36\x42\x83\x3d\xd7\x0b\x58\x6a\x46\x3e\x64\x0f\xeb\xb1\x9b\x00\x00\x00\xc4\xc3\xf9\x7c\x6d\x4c\xa6\xf3\x44\xa6\xc4\x43\x85\x7f\xc6\x18\xfd\xc4\x03\x11\x6f\x26\xfa\x44\x7b\xa9\x64\x2e\x47\x0f\x12\xfc\x64\xf2\x0f\x11\x62\x00", 56); syz_execute_func(0x20000080); break; case 12: break; case 13: memcpy((void*)0x20000100, "\x93\x5e\x05\x7a\xa8\xdd\xae\xce\x20\x39\x4d\x86\xc4\x5c\x4a\x50\xf7\x27\x8a\x87\x91\xb0\xcd\xbc\xf6\xe8\x32\xb9\x05\x2f\x3a\xb9\x90\x63\x70\x55\x06\xc6\x6e\xb3\x37\x4a\xad\x5e\x1f\xde\xd7\x51\x5c\x8c\xb7\xfe\x2c\x21\x69\x94\xff\xb8\x9e\x91\x54\x96\xa2\x07\x40\x41\x68\xe8\x6b\xa8\x2f\x7c\x0d\x49\x64\xe7\xe7\x56\xac\x68\x99\x19\xcf\x9f\x4b\xf8\x7d\x3e\x3b\x51\x74\x60\x5c\x8f\x25\x6d\x28\x20\x4e\x42\x8b\xf2\x95\xff\x31\x2e\x2e\x27\xa1\xc2\xaf\xb8\x93\xdb\xe7\x86\x70\x60\xc6\x50", 120); memcpy((void*)0x20000180, "\x2b\x06\x57\x4b\x32\xd8\xeb\x1d\x74\x79\x43\x1c\x2a\xb5\x3f\xc2\xd2\x4c\xbf\x1c\x43\x68\xa8\x24\xc8\xcd\x82\x17\xf4\x4d\x51\xb1\x18\x89\xf9\x29\xd6\x5e\x79\x00\xcc\xcb\x51\x66\x45\x3f\xfe\x7f\xa1\xf3\x28\xdf\x96\x95\xa4\x45\x7b\x58\xf1\x31\x91\xe9\x0c\xa7\x94\x18\x6e\x76\x32\x17\xe9\x23\xbd\xc4\xf2\x04\x34\xeb\xa9\x66\x16\xcc\x3b\x27\xea\x87\xd5\x4a\x6c\x43\x4d\x77\xb7\x37\xd1\x8d\x0d\x8d\x53\x7d\xce\x33\x0d\x27\x59\xbe\x67\xe2\x2e\x1d\xc6\x17\xf5\x17\x14\x7f\xae\xb4\x4e\x34\x3d\x63\x0b\x33\xd1\xd3\x16\x06\xb2\xa3\x97\x1e\xfc\xd0\x3e\x4d\x87\xb5\xe6\x00\x32\x37\x23\xab\xfe\xf8\x1a\xa7\x1b\xea\x16\xbd\xf9\x56\x1a\xca\x75\xce\x32\x65\xde\x3a\xc8\xa1\x59\x0a\x61\xed\xe7\x3a\x7b\x69\x43\xdd\xf3\xe3\xfc\xf1\x9e\x47\x8f\x55\x85\x9d\xb7\x8d\x6b\xaa\x46\xb7\xa7\xe2\x15\x9f\x6f\x4f\x20\xcc\xb1\x8d\x5c\x41\xfb\x43\x02\x49", 206); res = syz_usb_connect(0xfffffffffffffeff, 0x78, 0x20000100, 0x20000180); if (res != -1) r[6] = res; break; case 14: syz_usb_disconnect(r[6]); break; } } int main(void) { syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul, 0ul); use_temporary_dir(); do_sandbox_none(); return 0; } : In function 'syz_usb_connect_impl': :637:63: error: unknown type name 'usb_ctrlrequest' :642:55: error: unknown type name 'usb_ctrlrequest' compiler invocation: /syzkaller/netbsd/src/../tools/bin/x86_64--netbsd-g++ [-o /tmp/syz-executor137909888 -DGOOS_netbsd=1 -DGOARCH_amd64=1 -DHOSTGOOS_linux=1 -x c - -m64 --sysroot /syzkaller/netbsd/src/../dest/ -O2 -pthread -Wall -Werror -Wparentheses -Wframe-larger-than=16384] --- FAIL: TestGenerate/netbsd/amd64/14 (0.41s) csource_test.go:123: opts: {Threaded:true Collide:true Repeat:true RepeatTimes:0 Procs:2 Sandbox:none Fault:false FaultCall:0 FaultNth:0 Leak:false NetInjection:false NetDevices:false NetReset:false Cgroups:false BinfmtMisc:false CloseFDs:false KCSAN:false DevlinkPCI:false USB:false UseTmpDir:true HandleSegv:false Repro:true Trace:false} program: r0 = _lwp_self() _lwp_create(&(0x7f0000000100)={0x400f0020, &(0x7f0000000080)={0x0, &(0x7f0000000000)={0x400f0023, 0x0, {[0x7, 0x7, 0x101, 0x1f]}, {0x1, 0x400, 0x6}, {0x1, 0x80000000, '\x00'}}, {[0x3, 0x5, 0x1, 0x35f7192e]}, {0xd, 0x100000000, 0x5}, {0x6, 0x10001, '\x00'}}, {[0xf1b, 0x401, 0x69, 0x100]}, {0x1, 0x0, 0x16ac7582ecd3534}, {0x1, 0x400, '-\x00'}}, 0x60, &(0x7f0000000180)=0x0) r2 = _lwp_self() _lwp_create(&(0x7f00000001c0)={0x2, 0x0, {[0x9, 0x3ae, 0x3, 0xc699]}, {0x81, 0x2, 0x1}, {0x9, 0x7, '\x00'}}, 0x40, &(0x7f0000000240)=0x0) r4 = _lwp_self() _lwp_unpark_all(&(0x7f0000000280)=[r0, r1, r2, r3, r4], 0x5, &(0x7f00000002c0)=0x4) _lwp_create(&(0x7f0000000380)={0x8, &(0x7f0000000300)={0x400f002c, 0x0, {[0x80000000, 0xa706, 0x0, 0x8]}, {0x1f, 0x6, 0x1}, {0x101, 0x5, '\x00'}}, {[0x2, 0x9, 0x10000, 0x1]}, {0x2, 0xffffffff, 0x5}, {0x20, 0xfff, '\x00'}}, 0x40, &(0x7f0000000400)=0x0) _lwp_kill(r5, 0x8) compat_50__lwp_park(&(0x7f0000000440)={0x101, 0x8}, 0x0, 0x0, &(0x7f0000000480)=0x1) shmctl$SHM_LOCK(0x0, 0x3) syz_emit_ethernet(0x54, &(0x7f0000000000)="afe8d2383c3ef2ff5f6e8c9a4d35c602198766ff84e3acfd84f778353f61842e8a198d7e500cce4b813690338589cca07acde0e9834fd09b00f3146ba0ddb9ac17ae011fb861af258325476c9621388eca4b11c0") syz_execute_func(&(0x7f0000000080)="3642833dd70b586a463e640febb19b000000c4c3f97c6d4ca6f344a6c443857fc618fdc403116f26fa447ba9642e470f12fc64f20f116200") syz_extract_tcp_res(&(0x7f00000000c0), 0x3, 0xc5d) r6 = syz_usb_connect(0xfffffffffffffeff, 0x78, &(0x7f0000000100)="935e057aa8ddaece20394d86c45c4a50f7278a8791b0cdbcf6e832b9052f3ab99063705506c66eb3374aad5e1fded7515c8cb7fe2c216994ffb89e915496a207404168e86ba82f7c0d4964e7e756ac689919cf9f4bf87d3e3b5174605c8f256d28204e428bf295ff312e2e27a1c2afb893dbe7867060c650", &(0x7f0000000180)="2b06574b32d8eb1d7479431c2ab53fc2d24cbf1c4368a824c8cd8217f44d51b11889f929d65e7900cccb5166453ffe7fa1f328df9695a4457b58f13191e90ca794186e763217e923bdc4f20434eba96616cc3b27ea87d54a6c434d77b737d18d0d8d537dce330d2759be67e22e1dc617f517147faeb44e343d630b33d1d31606b2a3971efcd03e4d87b5e600323723abfef81aa71bea16bdf9561aca75ce3265de3ac8a1590a61ede73a7b6943ddf3e3fcf19e478f55859db78d6baa46b7a7e2159f6f4f20ccb18d5c41fb430249") syz_usb_disconnect(r6) csource_test.go:124: failed to build program: // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static unsigned long long procid; static void kill_and_wait(int pid, int* status) { kill(pid, SIGKILL); while (waitpid(-1, status, 0) != pid) { } } static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void use_temporary_dir(void) { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } static void remove_dir(const char* dir) { DIR* dp; struct dirent* ep; dp = opendir(dir); if (dp == NULL) exit(1); while ((ep = readdir(dp))) { if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue; char filename[FILENAME_MAX]; snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } if (unlink(filename)) exit(1); } closedir(dp); if (rmdir(dir)) exit(1); } static void thread_start(void* (*fn)(void*), void* arg) { pthread_t th; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 128 << 10); int i; for (i = 0; i < 100; i++) { if (pthread_create(&th, &attr, fn, arg) == 0) { pthread_attr_destroy(&attr); return; } if (errno == EAGAIN) { usleep(50); continue; } break; } exit(1); } typedef struct { pthread_mutex_t mu; pthread_cond_t cv; int state; } event_t; static void event_init(event_t* ev) { if (pthread_mutex_init(&ev->mu, 0)) exit(1); if (pthread_cond_init(&ev->cv, 0)) exit(1); ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { pthread_mutex_lock(&ev->mu); if (ev->state) exit(1); ev->state = 1; pthread_mutex_unlock(&ev->mu); pthread_cond_broadcast(&ev->cv); } static void event_wait(event_t* ev) { pthread_mutex_lock(&ev->mu); while (!ev->state) pthread_cond_wait(&ev->cv, &ev->mu); pthread_mutex_unlock(&ev->mu); } static int event_isset(event_t* ev) { pthread_mutex_lock(&ev->mu); int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; pthread_mutex_lock(&ev->mu); for (;;) { if (ev->state) break; uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; pthread_cond_timedwait(&ev->cv, &ev->mu, &ts); now = current_time_ms(); if (now - start > timeout) break; } int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } /* -------------------------------------------------------------------------- */ /* * Redefinitions to match the linux types used in common_usb.h. */ struct usb_endpoint_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; uint16_t wMaxPacketSize; uint8_t bInterval; uint8_t bRefresh; uint8_t bSynchAddress; } __attribute__((packed)); struct usb_device_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint16_t idVendor; uint16_t idProduct; uint16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; } __attribute__((packed)); struct usb_config_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; } __attribute__((packed)); struct usb_interface_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bNumEndpoints; uint8_t bInterfaceClass; uint8_t bInterfaceSubClass; uint8_t bInterfaceProtocol; uint8_t iInterface; } __attribute__((packed)); struct usb_ctrlrequest { uint8_t bRequestType; uint8_t bRequest; uint16_t wValue; uint16_t wIndex; uint16_t wLength; } __attribute__((packed)); struct usb_qualifier_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint8_t bNumConfigurations; uint8_t bRESERVED; } __attribute__((packed)); #define USB_TYPE_MASK (0x03 << 5) #define USB_TYPE_STANDARD (0x00 << 5) #define USB_TYPE_CLASS (0x01 << 5) #define USB_TYPE_VENDOR (0x02 << 5) #define USB_TYPE_RESERVED (0x03 << 5) #define USB_DT_DEVICE 0x01 #define USB_DT_CONFIG 0x02 #define USB_DT_STRING 0x03 #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_DEVICE_QUALIFIER 0x06 #define USB_DT_OTHER_SPEED_CONFIG 0x07 #define USB_DT_INTERFACE_POWER 0x08 #define USB_DT_OTG 0x09 #define USB_DT_DEBUG 0x0a #define USB_DT_INTERFACE_ASSOCIATION 0x0b #define USB_DT_SECURITY 0x0c #define USB_DT_KEY 0x0d #define USB_DT_ENCRYPTION_TYPE 0x0e #define USB_DT_BOS 0x0f #define USB_DT_DEVICE_CAPABILITY 0x10 #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 #define USB_DT_WIRE_ADAPTER 0x21 #define USB_DT_RPIPE 0x22 #define USB_DT_CS_RADIO_CONTROL 0x23 #define USB_DT_PIPE_USAGE 0x24 #define USB_DT_SS_ENDPOINT_COMP 0x30 #define USB_DT_SSP_ISOC_ENDPOINT_COMP 0x31 #define USB_REQ_GET_STATUS 0x00 #define USB_REQ_CLEAR_FEATURE 0x01 #define USB_REQ_SET_FEATURE 0x03 #define USB_REQ_SET_ADDRESS 0x05 #define USB_REQ_GET_DESCRIPTOR 0x06 #define USB_REQ_SET_DESCRIPTOR 0x07 #define USB_REQ_GET_CONFIGURATION 0x08 #define USB_REQ_SET_CONFIGURATION 0x09 #define USB_REQ_GET_INTERFACE 0x0A #define USB_REQ_SET_INTERFACE 0x0B #define USB_REQ_SYNCH_FRAME 0x0C #define USB_REQ_SET_SEL 0x30 #define USB_REQ_SET_ISOCH_DELAY 0x31 #define USB_REQ_SET_ENCRYPTION 0x0D #define USB_REQ_GET_ENCRYPTION 0x0E #define USB_REQ_RPIPE_ABORT 0x0E #define USB_REQ_SET_HANDSHAKE 0x0F #define USB_REQ_RPIPE_RESET 0x0F #define USB_REQ_GET_HANDSHAKE 0x10 #define USB_REQ_SET_CONNECTION 0x11 #define USB_REQ_SET_SECURITY_DATA 0x12 #define USB_REQ_GET_SECURITY_DATA 0x13 #define USB_REQ_SET_WUSB_DATA 0x14 #define USB_REQ_LOOPBACK_DATA_WRITE 0x15 #define USB_REQ_LOOPBACK_DATA_READ 0x16 #define USB_REQ_SET_INTERFACE_DS 0x17 #define USB_REQ_GET_PARTNER_PDO 20 #define USB_REQ_GET_BATTERY_STATUS 21 #define USB_REQ_SET_PDO 22 #define USB_REQ_GET_VDM 23 #define USB_REQ_SEND_VDM 24 #define USB_MAX_IFACE_NUM 4 #define USB_MAX_EP_NUM 32 #define USB_MAX_FDS 6 struct usb_endpoint_index { struct usb_endpoint_descriptor desc; int handle; }; struct usb_iface_index { struct usb_interface_descriptor* iface; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bInterfaceClass; struct usb_endpoint_index eps[USB_MAX_EP_NUM]; int eps_num; }; struct usb_device_index { struct usb_device_descriptor* dev; struct usb_config_descriptor* config; uint8_t bDeviceClass; uint8_t bMaxPower; int config_length; struct usb_iface_index ifaces[USB_MAX_IFACE_NUM]; int ifaces_num; int iface_cur; }; struct usb_info { int fd; struct usb_device_index index; }; static struct usb_info usb_devices[USB_MAX_FDS]; static int usb_devices_num; static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index) { if (length < sizeof(*index->dev) + sizeof(*index->config)) return false; memset(index, 0, sizeof(*index)); index->dev = (struct usb_device_descriptor*)buffer; index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev)); index->bDeviceClass = index->dev->bDeviceClass; index->bMaxPower = index->config->bMaxPower; index->config_length = length - sizeof(*index->dev); index->iface_cur = -1; size_t offset = 0; while (true) { if (offset + 1 >= length) break; uint8_t desc_length = buffer[offset]; uint8_t desc_type = buffer[offset + 1]; if (desc_length <= 2) break; if (offset + desc_length > length) break; if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) { struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset); index->ifaces[index->ifaces_num].iface = iface; index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber; index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting; index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass; index->ifaces_num++; } if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) { struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1]; if (iface->eps_num < USB_MAX_EP_NUM) { memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc)); iface->eps_num++; } } offset += desc_length; } return true; } static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len) { int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED); if (i >= USB_MAX_FDS) return NULL; int rv = 0; rv = parse_usb_descriptor(dev, dev_len, &usb_devices[i].index); if (!rv) return NULL; __atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE); return &usb_devices[i].index; } static struct usb_device_index* lookup_usb_index(int fd) { int i; for (i = 0; i < USB_MAX_FDS; i++) { if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd) { return &usb_devices[i].index; } } return NULL; } struct vusb_connect_string_descriptor { uint32_t len; char* str; } __attribute__((packed)); struct vusb_connect_descriptors { uint32_t qual_len; char* qual; uint32_t bos_len; char* bos; uint32_t strs_len; struct vusb_connect_string_descriptor strs[0]; } __attribute__((packed)); static const char default_string[] = { 8, USB_DT_STRING, 's', 0, 'y', 0, 'z', 0 }; static const char default_lang_id[] = { 4, USB_DT_STRING, 0x09, 0x04 }; static bool lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, char** response_data, uint32_t* response_length) { struct usb_device_index* index = lookup_usb_index(fd); uint8_t str_idx; if (!index) return false; switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_GET_DESCRIPTOR: switch (ctrl->wValue >> 8) { case USB_DT_DEVICE: *response_data = (char*)index->dev; *response_length = sizeof(*index->dev); return true; case USB_DT_CONFIG: *response_data = (char*)index->config; *response_length = index->config_length; return true; case USB_DT_STRING: str_idx = (uint8_t)ctrl->wValue; if (descs && str_idx < descs->strs_len) { *response_data = descs->strs[str_idx].str; *response_length = descs->strs[str_idx].len; return true; } if (str_idx == 0) { *response_data = (char*)&default_lang_id[0]; *response_length = default_lang_id[0]; return true; } *response_data = (char*)&default_string[0]; *response_length = default_string[0]; return true; case USB_DT_BOS: *response_data = descs->bos; *response_length = descs->bos_len; return true; case USB_DT_DEVICE_QUALIFIER: if (!descs->qual) { struct usb_qualifier_descriptor* qual = (struct usb_qualifier_descriptor*)response_data; qual->bLength = sizeof(*qual); qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; qual->bcdUSB = index->dev->bcdUSB; qual->bDeviceClass = index->dev->bDeviceClass; qual->bDeviceSubClass = index->dev->bDeviceSubClass; qual->bDeviceProtocol = index->dev->bDeviceProtocol; qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0; qual->bNumConfigurations = index->dev->bNumConfigurations; qual->bRESERVED = 0; *response_length = sizeof(*qual); return true; } *response_data = descs->qual; *response_length = descs->qual_len; return true; default: break; } break; default: break; } break; default: break; } return false; } typedef bool (*lookup_connect_out_response_t)(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done); static bool lookup_connect_response_out_generic(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done) { switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_SET_CONFIGURATION: *done = true; return true; default: break; } break; } return false; } /* -------------------------------------------------------------------------- */ static int vhci_open(void) { return open("/dev/vhci", O_RDWR); } static int vhci_setport(int fd, u_int port) { struct vhci_ioc_set_port args; args.port = port; return ioctl(fd, VHCI_IOC_SET_PORT, &args); } static int vhci_usb_attach(int fd) { return ioctl(fd, VHCI_IOC_USB_ATTACH, NULL); } static int vhci_usb_recv(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = read(fd, ptr, size); if (done < 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } static int vhci_usb_send(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = write(fd, ptr, size); if (done <= 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } /* -------------------------------------------------------------------------- */ static volatile long syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev, const struct vusb_connect_descriptors* descs, lookup_connect_out_response_t lookup_connect_response_out) { struct usb_device_index* index; int portnum, fd, rv; bool done; portnum = procid + 1; if (!dev) { return -1; } if (portnum != 1) { /* For now, we support only one proc. */ return -1; } fd = vhci_open(); if (fd < 0) { return -1; } index = add_usb_index(fd, dev, dev_len); if (!index) { goto err; } rv = vhci_setport(fd, portnum); if (rv != 0) { goto err; } rv = vhci_usb_attach(fd); if (rv != 0) { goto err; } done = false; while (!done) { vhci_request_t req; rv = vhci_usb_recv(fd, &req, sizeof(req)); if (rv != 0) { goto err; } if (req.type != VHCI_REQ_CTRL) { goto err; } char* response_data = NULL; uint32_t response_length = 0; char data[4096]; if (req.u.ctrl.bmRequestType & UE_DIR_IN) { bool response_found = false; response_found = lookup_connect_response_in(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &response_data, &response_length); if (!response_found) { goto err; } } else { if (!lookup_connect_response_out(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &done)) { goto err; } response_data = NULL; response_length = UGETW(req.u.ctrl.wLength); } if ((req.u.ctrl.bmRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD && req.u.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) { /* TODO: possibly revisit */ } if (response_length > sizeof(data)) response_length = 0; if ((uint32_t)UGETW(req.u.ctrl.wLength) < response_length) response_length = UGETW(req.u.ctrl.wLength); if (response_data) memcpy(data, response_data, response_length); else memset(data, 0, response_length); if (req.u.ctrl.bmRequestType & UE_DIR_IN) { if (response_length > 0) { vhci_response_t res; res.size = response_length; rv = vhci_usb_send(fd, &res, sizeof(res)); if (rv == 0) rv = vhci_usb_send(fd, data, response_length); } } else { rv = vhci_usb_recv(fd, data, response_length); } if (rv < 0) { goto err; } } sleep_ms(200); return fd; err: close(fd); return -1; } static volatile long syz_usb_connect(volatile long a0, volatile long a1, volatile long a2, volatile long a3) { uint64_t speed = a0; uint64_t dev_len = a1; const char* dev = (const char*)a2; const struct vusb_connect_descriptors* descs = (const struct vusb_connect_descriptors*)a3; return syz_usb_connect_impl(speed, dev_len, dev, descs, &lookup_connect_response_out_generic); } static volatile long syz_usb_disconnect(volatile long a0) { int fd = a0; int rv = close(fd); sleep_ms(200); return rv; } static void sandbox_common() { if (setsid() == -1) exit(1); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 8 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 0; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); } static void loop(); static int do_sandbox_none(void) { sandbox_common(); loop(); return 0; } static long syz_execute_func(volatile long text) { volatile long p[8] = {0}; (void)p; asm volatile("" ::"r"(0l), "r"(1l), "r"(2l), "r"(3l), "r"(4l), "r"(5l), "r"(6l), "r"(7l), "r"(8l), "r"(9l), "r"(10l), "r"(11l), "r"(12l), "r"(13l)); ((void (*)(void))(text))(); return 0; } struct thread_t { int created, call; event_t ready, done; }; static struct thread_t threads[16]; static void execute_call(int call); static int running; static void* thr(void* arg) { struct thread_t* th = (struct thread_t*)arg; for (;;) { event_wait(&th->ready); event_reset(&th->ready); execute_call(th->call); __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED); event_set(&th->done); } return 0; } static void execute_one(void) { if (write(1, "executing program\n", sizeof("executing program\n") - 1)) { } int i, call, thread; int collide = 0; again: for (call = 0; call < 15; call++) { for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); thread++) { struct thread_t* th = &threads[thread]; if (!th->created) { th->created = 1; event_init(&th->ready); event_init(&th->done); event_set(&th->done); thread_start(thr, th); } if (!event_isset(&th->done)) continue; event_reset(&th->done); th->call = call; __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED); event_set(&th->ready); if (collide && (call % 2) == 0) break; event_timedwait(&th->done, 45 + (call == 13 ? 3000 : 0) + (call == 14 ? 300 : 0)); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); if (!collide) { collide = 1; goto again; } } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter; for (iter = 0;; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5 * 1000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } #ifndef SYS__lwp_create #define SYS__lwp_create 309 #endif #ifndef SYS__lwp_kill #define SYS__lwp_kill 318 #endif #ifndef SYS__lwp_self #define SYS__lwp_self 311 #endif #ifndef SYS__lwp_unpark_all #define SYS__lwp_unpark_all 322 #endif #ifndef SYS_compat_50__lwp_park #define SYS_compat_50__lwp_park 320 #endif #ifndef SYS_mmap #define SYS_mmap 197 #endif #ifndef SYS_shmctl #define SYS_shmctl 443 #endif uint64_t r[7] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: res = syscall(SYS__lwp_self); if (res != -1) r[0] = res; break; case 1: *(uint32_t*)0x20000100 = 0x400f0020; *(uint64_t*)0x20000108 = 0x20000080; *(uint32_t*)0x20000080 = 0; *(uint64_t*)0x20000088 = 0x20000000; *(uint32_t*)0x20000000 = 0x400f0023; *(uint64_t*)0x20000008 = 0; *(uint32_t*)0x20000010 = 7; *(uint32_t*)0x20000014 = 7; *(uint32_t*)0x20000018 = 0x101; *(uint32_t*)0x2000001c = 0x1f; *(uint64_t*)0x20000020 = 1; *(uint64_t*)0x20000028 = 0x400; *(uint32_t*)0x20000030 = 6; *(uint64_t*)0x20000038 = 1; *(uint64_t*)0x20000040 = 0x80000000; memcpy((void*)0x20000048, "\000", 1); *(uint32_t*)0x20000090 = 3; *(uint32_t*)0x20000094 = 5; *(uint32_t*)0x20000098 = 1; *(uint32_t*)0x2000009c = 0x35f7192e; *(uint64_t*)0x200000a0 = 0xd; *(uint64_t*)0x200000a8 = 0x100000000; *(uint32_t*)0x200000b0 = 5; *(uint64_t*)0x200000b8 = 6; *(uint64_t*)0x200000c0 = 0x10001; memcpy((void*)0x200000c8, "\000", 1); *(uint32_t*)0x20000110 = 0xf1b; *(uint32_t*)0x20000114 = 0x401; *(uint32_t*)0x20000118 = 0x69; *(uint32_t*)0x2000011c = 0x100; *(uint64_t*)0x20000120 = 1; *(uint64_t*)0x20000128 = 0; *(uint32_t*)0x20000130 = 0x2ecd3534; *(uint64_t*)0x20000138 = 1; *(uint64_t*)0x20000140 = 0x400; memcpy((void*)0x20000148, "-\000", 2); res = syscall(SYS__lwp_create, 0x20000100ul, 0x60ul, 0x20000180ul); if (res != -1) r[1] = *(uint32_t*)0x20000180; break; case 2: res = syscall(SYS__lwp_self); if (res != -1) r[2] = res; break; case 3: *(uint32_t*)0x200001c0 = 2; *(uint64_t*)0x200001c8 = 0; *(uint32_t*)0x200001d0 = 9; *(uint32_t*)0x200001d4 = 0x3ae; *(uint32_t*)0x200001d8 = 3; *(uint32_t*)0x200001dc = 0xc699; *(uint64_t*)0x200001e0 = 0x81; *(uint64_t*)0x200001e8 = 2; *(uint32_t*)0x200001f0 = 1; *(uint64_t*)0x200001f8 = 9; *(uint64_t*)0x20000200 = 7; memcpy((void*)0x20000208, "\000", 1); res = syscall(SYS__lwp_create, 0x200001c0ul, 0x40ul, 0x20000240ul); if (res != -1) r[3] = *(uint32_t*)0x20000240; break; case 4: res = syscall(SYS__lwp_self); if (res != -1) r[4] = res; break; case 5: *(uint32_t*)0x20000280 = r[0]; *(uint32_t*)0x20000284 = r[1]; *(uint32_t*)0x20000288 = r[2]; *(uint32_t*)0x2000028c = r[3]; *(uint32_t*)0x20000290 = r[4]; *(uint64_t*)0x200002c0 = 4; syscall(SYS__lwp_unpark_all, 0x20000280ul, 5ul, 0x200002c0ul); break; case 6: *(uint32_t*)0x20000380 = 8; *(uint64_t*)0x20000388 = 0x20000300; *(uint32_t*)0x20000300 = 0x400f002c; *(uint64_t*)0x20000308 = 0; *(uint32_t*)0x20000310 = 0x80000000; *(uint32_t*)0x20000314 = 0xa706; *(uint32_t*)0x20000318 = 0; *(uint32_t*)0x2000031c = 8; *(uint64_t*)0x20000320 = 0x1f; *(uint64_t*)0x20000328 = 6; *(uint32_t*)0x20000330 = 1; *(uint64_t*)0x20000338 = 0x101; *(uint64_t*)0x20000340 = 5; memcpy((void*)0x20000348, "\000", 1); *(uint32_t*)0x20000390 = 2; *(uint32_t*)0x20000394 = 9; *(uint32_t*)0x20000398 = 0x10000; *(uint32_t*)0x2000039c = 1; *(uint64_t*)0x200003a0 = 2; *(uint64_t*)0x200003a8 = 0xffffffff; *(uint32_t*)0x200003b0 = 5; *(uint64_t*)0x200003b8 = 0x20; *(uint64_t*)0x200003c0 = 0xfff; memcpy((void*)0x200003c8, "\000", 1); res = syscall(SYS__lwp_create, 0x20000380ul, 0x40ul, 0x20000400ul); if (res != -1) r[5] = *(uint32_t*)0x20000400; break; case 7: syscall(SYS__lwp_kill, r[5], 8); break; case 8: *(uint32_t*)0x20000440 = 0x101; *(uint64_t*)0x20000448 = 8; *(uint64_t*)0x20000480 = 1; syscall(SYS_compat_50__lwp_park, 0x20000440ul, 0, 0ul, 0x20000480ul); break; case 9: syscall(SYS_shmctl, 0, 3ul, 0); break; case 10: memcpy((void*)0x20000000, "\xaf\xe8\xd2\x38\x3c\x3e\xf2\xff\x5f\x6e\x8c\x9a\x4d\x35\xc6\x02\x19\x87\x66\xff\x84\xe3\xac\xfd\x84\xf7\x78\x35\x3f\x61\x84\x2e\x8a\x19\x8d\x7e\x50\x0c\xce\x4b\x81\x36\x90\x33\x85\x89\xcc\xa0\x7a\xcd\xe0\xe9\x83\x4f\xd0\x9b\x00\xf3\x14\x6b\xa0\xdd\xb9\xac\x17\xae\x01\x1f\xb8\x61\xaf\x25\x83\x25\x47\x6c\x96\x21\x38\x8e\xca\x4b\x11\xc0", 84); break; case 11: memcpy((void*)0x20000080, "\x36\x42\x83\x3d\xd7\x0b\x58\x6a\x46\x3e\x64\x0f\xeb\xb1\x9b\x00\x00\x00\xc4\xc3\xf9\x7c\x6d\x4c\xa6\xf3\x44\xa6\xc4\x43\x85\x7f\xc6\x18\xfd\xc4\x03\x11\x6f\x26\xfa\x44\x7b\xa9\x64\x2e\x47\x0f\x12\xfc\x64\xf2\x0f\x11\x62\x00", 56); syz_execute_func(0x20000080); break; case 12: break; case 13: memcpy((void*)0x20000100, "\x93\x5e\x05\x7a\xa8\xdd\xae\xce\x20\x39\x4d\x86\xc4\x5c\x4a\x50\xf7\x27\x8a\x87\x91\xb0\xcd\xbc\xf6\xe8\x32\xb9\x05\x2f\x3a\xb9\x90\x63\x70\x55\x06\xc6\x6e\xb3\x37\x4a\xad\x5e\x1f\xde\xd7\x51\x5c\x8c\xb7\xfe\x2c\x21\x69\x94\xff\xb8\x9e\x91\x54\x96\xa2\x07\x40\x41\x68\xe8\x6b\xa8\x2f\x7c\x0d\x49\x64\xe7\xe7\x56\xac\x68\x99\x19\xcf\x9f\x4b\xf8\x7d\x3e\x3b\x51\x74\x60\x5c\x8f\x25\x6d\x28\x20\x4e\x42\x8b\xf2\x95\xff\x31\x2e\x2e\x27\xa1\xc2\xaf\xb8\x93\xdb\xe7\x86\x70\x60\xc6\x50", 120); memcpy((void*)0x20000180, "\x2b\x06\x57\x4b\x32\xd8\xeb\x1d\x74\x79\x43\x1c\x2a\xb5\x3f\xc2\xd2\x4c\xbf\x1c\x43\x68\xa8\x24\xc8\xcd\x82\x17\xf4\x4d\x51\xb1\x18\x89\xf9\x29\xd6\x5e\x79\x00\xcc\xcb\x51\x66\x45\x3f\xfe\x7f\xa1\xf3\x28\xdf\x96\x95\xa4\x45\x7b\x58\xf1\x31\x91\xe9\x0c\xa7\x94\x18\x6e\x76\x32\x17\xe9\x23\xbd\xc4\xf2\x04\x34\xeb\xa9\x66\x16\xcc\x3b\x27\xea\x87\xd5\x4a\x6c\x43\x4d\x77\xb7\x37\xd1\x8d\x0d\x8d\x53\x7d\xce\x33\x0d\x27\x59\xbe\x67\xe2\x2e\x1d\xc6\x17\xf5\x17\x14\x7f\xae\xb4\x4e\x34\x3d\x63\x0b\x33\xd1\xd3\x16\x06\xb2\xa3\x97\x1e\xfc\xd0\x3e\x4d\x87\xb5\xe6\x00\x32\x37\x23\xab\xfe\xf8\x1a\xa7\x1b\xea\x16\xbd\xf9\x56\x1a\xca\x75\xce\x32\x65\xde\x3a\xc8\xa1\x59\x0a\x61\xed\xe7\x3a\x7b\x69\x43\xdd\xf3\xe3\xfc\xf1\x9e\x47\x8f\x55\x85\x9d\xb7\x8d\x6b\xaa\x46\xb7\xa7\xe2\x15\x9f\x6f\x4f\x20\xcc\xb1\x8d\x5c\x41\xfb\x43\x02\x49", 206); res = syz_usb_connect(0xfffffffffffffeff, 0x78, 0x20000100, 0x20000180); if (res != -1) r[6] = res; break; case 14: syz_usb_disconnect(r[6]); break; } } int main(void) { syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul, 0ul); for (procid = 0; procid < 2; procid++) { if (fork() == 0) { use_temporary_dir(); do_sandbox_none(); } } sleep(1000000); return 0; } : In function 'syz_usb_connect_impl': :637:63: error: unknown type name 'usb_ctrlrequest' :642:55: error: unknown type name 'usb_ctrlrequest' compiler invocation: /syzkaller/netbsd/src/../tools/bin/x86_64--netbsd-g++ [-o /tmp/syz-executor792321897 -DGOOS_netbsd=1 -DGOARCH_amd64=1 -DHOSTGOOS_linux=1 -x c - -m64 --sysroot /syzkaller/netbsd/src/../dest/ -O2 -pthread -Wall -Werror -Wparentheses -Wframe-larger-than=16384] --- FAIL: TestGenerate/netbsd/amd64/8 (0.42s) csource_test.go:123: opts: {Threaded:true Collide:false Repeat:true RepeatTimes:0 Procs:0 Sandbox:setuid Fault:false FaultCall:0 FaultNth:0 Leak:false NetInjection:false NetDevices:false NetReset:false Cgroups:false BinfmtMisc:false CloseFDs:false KCSAN:false DevlinkPCI:false USB:false UseTmpDir:true HandleSegv:false Repro:false Trace:false} program: r0 = _lwp_self() _lwp_create(&(0x7f0000000100)={0x400f0020, &(0x7f0000000080)={0x0, &(0x7f0000000000)={0x400f0023, 0x0, {[0x7, 0x7, 0x101, 0x1f]}, {0x1, 0x400, 0x6}, {0x1, 0x80000000, '\x00'}}, {[0x3, 0x5, 0x1, 0x35f7192e]}, {0xd, 0x100000000, 0x5}, {0x6, 0x10001, '\x00'}}, {[0xf1b, 0x401, 0x69, 0x100]}, {0x1, 0x0, 0x16ac7582ecd3534}, {0x1, 0x400, '-\x00'}}, 0x60, &(0x7f0000000180)=0x0) r2 = _lwp_self() _lwp_create(&(0x7f00000001c0)={0x2, 0x0, {[0x9, 0x3ae, 0x3, 0xc699]}, {0x81, 0x2, 0x1}, {0x9, 0x7, '\x00'}}, 0x40, &(0x7f0000000240)=0x0) r4 = _lwp_self() _lwp_unpark_all(&(0x7f0000000280)=[r0, r1, r2, r3, r4], 0x5, &(0x7f00000002c0)=0x4) _lwp_create(&(0x7f0000000380)={0x8, &(0x7f0000000300)={0x400f002c, 0x0, {[0x80000000, 0xa706, 0x0, 0x8]}, {0x1f, 0x6, 0x1}, {0x101, 0x5, '\x00'}}, {[0x2, 0x9, 0x10000, 0x1]}, {0x2, 0xffffffff, 0x5}, {0x20, 0xfff, '\x00'}}, 0x40, &(0x7f0000000400)=0x0) _lwp_kill(r5, 0x8) compat_50__lwp_park(&(0x7f0000000440)={0x101, 0x8}, 0x0, 0x0, &(0x7f0000000480)=0x1) shmctl$SHM_LOCK(0x0, 0x3) syz_emit_ethernet(0x54, &(0x7f0000000000)="afe8d2383c3ef2ff5f6e8c9a4d35c602198766ff84e3acfd84f778353f61842e8a198d7e500cce4b813690338589cca07acde0e9834fd09b00f3146ba0ddb9ac17ae011fb861af258325476c9621388eca4b11c0") syz_execute_func(&(0x7f0000000080)="3642833dd70b586a463e640febb19b000000c4c3f97c6d4ca6f344a6c443857fc618fdc403116f26fa447ba9642e470f12fc64f20f116200") syz_extract_tcp_res(&(0x7f00000000c0), 0x3, 0xc5d) r6 = syz_usb_connect(0xfffffffffffffeff, 0x78, &(0x7f0000000100)="935e057aa8ddaece20394d86c45c4a50f7278a8791b0cdbcf6e832b9052f3ab99063705506c66eb3374aad5e1fded7515c8cb7fe2c216994ffb89e915496a207404168e86ba82f7c0d4964e7e756ac689919cf9f4bf87d3e3b5174605c8f256d28204e428bf295ff312e2e27a1c2afb893dbe7867060c650", &(0x7f0000000180)="2b06574b32d8eb1d7479431c2ab53fc2d24cbf1c4368a824c8cd8217f44d51b11889f929d65e7900cccb5166453ffe7fa1f328df9695a4457b58f13191e90ca794186e763217e923bdc4f20434eba96616cc3b27ea87d54a6c434d77b737d18d0d8d537dce330d2759be67e22e1dc617f517147faeb44e343d630b33d1d31606b2a3971efcd03e4d87b5e600323723abfef81aa71bea16bdf9561aca75ce3265de3ac8a1590a61ede73a7b6943ddf3e3fcf19e478f55859db78d6baa46b7a7e2159f6f4f20ccb18d5c41fb430249") syz_usb_disconnect(r6) csource_test.go:124: failed to build program: // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static unsigned long long procid; static void kill_and_wait(int pid, int* status) { kill(pid, SIGKILL); while (waitpid(-1, status, 0) != pid) { } } static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void use_temporary_dir(void) { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } static void remove_dir(const char* dir) { DIR* dp; struct dirent* ep; dp = opendir(dir); if (dp == NULL) exit(1); while ((ep = readdir(dp))) { if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue; char filename[FILENAME_MAX]; snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } if (unlink(filename)) exit(1); } closedir(dp); if (rmdir(dir)) exit(1); } static void thread_start(void* (*fn)(void*), void* arg) { pthread_t th; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 128 << 10); int i; for (i = 0; i < 100; i++) { if (pthread_create(&th, &attr, fn, arg) == 0) { pthread_attr_destroy(&attr); return; } if (errno == EAGAIN) { usleep(50); continue; } break; } exit(1); } typedef struct { pthread_mutex_t mu; pthread_cond_t cv; int state; } event_t; static void event_init(event_t* ev) { if (pthread_mutex_init(&ev->mu, 0)) exit(1); if (pthread_cond_init(&ev->cv, 0)) exit(1); ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { pthread_mutex_lock(&ev->mu); if (ev->state) exit(1); ev->state = 1; pthread_mutex_unlock(&ev->mu); pthread_cond_broadcast(&ev->cv); } static void event_wait(event_t* ev) { pthread_mutex_lock(&ev->mu); while (!ev->state) pthread_cond_wait(&ev->cv, &ev->mu); pthread_mutex_unlock(&ev->mu); } static int event_isset(event_t* ev) { pthread_mutex_lock(&ev->mu); int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; pthread_mutex_lock(&ev->mu); for (;;) { if (ev->state) break; uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; pthread_cond_timedwait(&ev->cv, &ev->mu, &ts); now = current_time_ms(); if (now - start > timeout) break; } int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } /* -------------------------------------------------------------------------- */ /* * Redefinitions to match the linux types used in common_usb.h. */ struct usb_endpoint_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; uint16_t wMaxPacketSize; uint8_t bInterval; uint8_t bRefresh; uint8_t bSynchAddress; } __attribute__((packed)); struct usb_device_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint16_t idVendor; uint16_t idProduct; uint16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; } __attribute__((packed)); struct usb_config_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; } __attribute__((packed)); struct usb_interface_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bNumEndpoints; uint8_t bInterfaceClass; uint8_t bInterfaceSubClass; uint8_t bInterfaceProtocol; uint8_t iInterface; } __attribute__((packed)); struct usb_ctrlrequest { uint8_t bRequestType; uint8_t bRequest; uint16_t wValue; uint16_t wIndex; uint16_t wLength; } __attribute__((packed)); struct usb_qualifier_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint8_t bNumConfigurations; uint8_t bRESERVED; } __attribute__((packed)); #define USB_TYPE_MASK (0x03 << 5) #define USB_TYPE_STANDARD (0x00 << 5) #define USB_TYPE_CLASS (0x01 << 5) #define USB_TYPE_VENDOR (0x02 << 5) #define USB_TYPE_RESERVED (0x03 << 5) #define USB_DT_DEVICE 0x01 #define USB_DT_CONFIG 0x02 #define USB_DT_STRING 0x03 #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_DEVICE_QUALIFIER 0x06 #define USB_DT_OTHER_SPEED_CONFIG 0x07 #define USB_DT_INTERFACE_POWER 0x08 #define USB_DT_OTG 0x09 #define USB_DT_DEBUG 0x0a #define USB_DT_INTERFACE_ASSOCIATION 0x0b #define USB_DT_SECURITY 0x0c #define USB_DT_KEY 0x0d #define USB_DT_ENCRYPTION_TYPE 0x0e #define USB_DT_BOS 0x0f #define USB_DT_DEVICE_CAPABILITY 0x10 #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 #define USB_DT_WIRE_ADAPTER 0x21 #define USB_DT_RPIPE 0x22 #define USB_DT_CS_RADIO_CONTROL 0x23 #define USB_DT_PIPE_USAGE 0x24 #define USB_DT_SS_ENDPOINT_COMP 0x30 #define USB_DT_SSP_ISOC_ENDPOINT_COMP 0x31 #define USB_REQ_GET_STATUS 0x00 #define USB_REQ_CLEAR_FEATURE 0x01 #define USB_REQ_SET_FEATURE 0x03 #define USB_REQ_SET_ADDRESS 0x05 #define USB_REQ_GET_DESCRIPTOR 0x06 #define USB_REQ_SET_DESCRIPTOR 0x07 #define USB_REQ_GET_CONFIGURATION 0x08 #define USB_REQ_SET_CONFIGURATION 0x09 #define USB_REQ_GET_INTERFACE 0x0A #define USB_REQ_SET_INTERFACE 0x0B #define USB_REQ_SYNCH_FRAME 0x0C #define USB_REQ_SET_SEL 0x30 #define USB_REQ_SET_ISOCH_DELAY 0x31 #define USB_REQ_SET_ENCRYPTION 0x0D #define USB_REQ_GET_ENCRYPTION 0x0E #define USB_REQ_RPIPE_ABORT 0x0E #define USB_REQ_SET_HANDSHAKE 0x0F #define USB_REQ_RPIPE_RESET 0x0F #define USB_REQ_GET_HANDSHAKE 0x10 #define USB_REQ_SET_CONNECTION 0x11 #define USB_REQ_SET_SECURITY_DATA 0x12 #define USB_REQ_GET_SECURITY_DATA 0x13 #define USB_REQ_SET_WUSB_DATA 0x14 #define USB_REQ_LOOPBACK_DATA_WRITE 0x15 #define USB_REQ_LOOPBACK_DATA_READ 0x16 #define USB_REQ_SET_INTERFACE_DS 0x17 #define USB_REQ_GET_PARTNER_PDO 20 #define USB_REQ_GET_BATTERY_STATUS 21 #define USB_REQ_SET_PDO 22 #define USB_REQ_GET_VDM 23 #define USB_REQ_SEND_VDM 24 #define USB_MAX_IFACE_NUM 4 #define USB_MAX_EP_NUM 32 #define USB_MAX_FDS 6 struct usb_endpoint_index { struct usb_endpoint_descriptor desc; int handle; }; struct usb_iface_index { struct usb_interface_descriptor* iface; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bInterfaceClass; struct usb_endpoint_index eps[USB_MAX_EP_NUM]; int eps_num; }; struct usb_device_index { struct usb_device_descriptor* dev; struct usb_config_descriptor* config; uint8_t bDeviceClass; uint8_t bMaxPower; int config_length; struct usb_iface_index ifaces[USB_MAX_IFACE_NUM]; int ifaces_num; int iface_cur; }; struct usb_info { int fd; struct usb_device_index index; }; static struct usb_info usb_devices[USB_MAX_FDS]; static int usb_devices_num; static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index) { if (length < sizeof(*index->dev) + sizeof(*index->config)) return false; memset(index, 0, sizeof(*index)); index->dev = (struct usb_device_descriptor*)buffer; index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev)); index->bDeviceClass = index->dev->bDeviceClass; index->bMaxPower = index->config->bMaxPower; index->config_length = length - sizeof(*index->dev); index->iface_cur = -1; size_t offset = 0; while (true) { if (offset + 1 >= length) break; uint8_t desc_length = buffer[offset]; uint8_t desc_type = buffer[offset + 1]; if (desc_length <= 2) break; if (offset + desc_length > length) break; if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) { struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset); index->ifaces[index->ifaces_num].iface = iface; index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber; index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting; index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass; index->ifaces_num++; } if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) { struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1]; if (iface->eps_num < USB_MAX_EP_NUM) { memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc)); iface->eps_num++; } } offset += desc_length; } return true; } static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len) { int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED); if (i >= USB_MAX_FDS) return NULL; int rv = 0; rv = parse_usb_descriptor(dev, dev_len, &usb_devices[i].index); if (!rv) return NULL; __atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE); return &usb_devices[i].index; } static struct usb_device_index* lookup_usb_index(int fd) { int i; for (i = 0; i < USB_MAX_FDS; i++) { if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd) { return &usb_devices[i].index; } } return NULL; } struct vusb_connect_string_descriptor { uint32_t len; char* str; } __attribute__((packed)); struct vusb_connect_descriptors { uint32_t qual_len; char* qual; uint32_t bos_len; char* bos; uint32_t strs_len; struct vusb_connect_string_descriptor strs[0]; } __attribute__((packed)); static const char default_string[] = { 8, USB_DT_STRING, 's', 0, 'y', 0, 'z', 0 }; static const char default_lang_id[] = { 4, USB_DT_STRING, 0x09, 0x04 }; static bool lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, char** response_data, uint32_t* response_length) { struct usb_device_index* index = lookup_usb_index(fd); uint8_t str_idx; if (!index) return false; switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_GET_DESCRIPTOR: switch (ctrl->wValue >> 8) { case USB_DT_DEVICE: *response_data = (char*)index->dev; *response_length = sizeof(*index->dev); return true; case USB_DT_CONFIG: *response_data = (char*)index->config; *response_length = index->config_length; return true; case USB_DT_STRING: str_idx = (uint8_t)ctrl->wValue; if (descs && str_idx < descs->strs_len) { *response_data = descs->strs[str_idx].str; *response_length = descs->strs[str_idx].len; return true; } if (str_idx == 0) { *response_data = (char*)&default_lang_id[0]; *response_length = default_lang_id[0]; return true; } *response_data = (char*)&default_string[0]; *response_length = default_string[0]; return true; case USB_DT_BOS: *response_data = descs->bos; *response_length = descs->bos_len; return true; case USB_DT_DEVICE_QUALIFIER: if (!descs->qual) { struct usb_qualifier_descriptor* qual = (struct usb_qualifier_descriptor*)response_data; qual->bLength = sizeof(*qual); qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; qual->bcdUSB = index->dev->bcdUSB; qual->bDeviceClass = index->dev->bDeviceClass; qual->bDeviceSubClass = index->dev->bDeviceSubClass; qual->bDeviceProtocol = index->dev->bDeviceProtocol; qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0; qual->bNumConfigurations = index->dev->bNumConfigurations; qual->bRESERVED = 0; *response_length = sizeof(*qual); return true; } *response_data = descs->qual; *response_length = descs->qual_len; return true; default: break; } break; default: break; } break; default: break; } return false; } typedef bool (*lookup_connect_out_response_t)(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done); static bool lookup_connect_response_out_generic(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done) { switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_SET_CONFIGURATION: *done = true; return true; default: break; } break; } return false; } /* -------------------------------------------------------------------------- */ static int vhci_open(void) { return open("/dev/vhci", O_RDWR); } static int vhci_setport(int fd, u_int port) { struct vhci_ioc_set_port args; args.port = port; return ioctl(fd, VHCI_IOC_SET_PORT, &args); } static int vhci_usb_attach(int fd) { return ioctl(fd, VHCI_IOC_USB_ATTACH, NULL); } static int vhci_usb_recv(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = read(fd, ptr, size); if (done < 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } static int vhci_usb_send(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = write(fd, ptr, size); if (done <= 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } /* -------------------------------------------------------------------------- */ static volatile long syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev, const struct vusb_connect_descriptors* descs, lookup_connect_out_response_t lookup_connect_response_out) { struct usb_device_index* index; int portnum, fd, rv; bool done; portnum = procid + 1; if (!dev) { return -1; } if (portnum != 1) { /* For now, we support only one proc. */ return -1; } fd = vhci_open(); if (fd < 0) { return -1; } index = add_usb_index(fd, dev, dev_len); if (!index) { goto err; } rv = vhci_setport(fd, portnum); if (rv != 0) { goto err; } rv = vhci_usb_attach(fd); if (rv != 0) { goto err; } done = false; while (!done) { vhci_request_t req; rv = vhci_usb_recv(fd, &req, sizeof(req)); if (rv != 0) { goto err; } if (req.type != VHCI_REQ_CTRL) { goto err; } char* response_data = NULL; uint32_t response_length = 0; char data[4096]; if (req.u.ctrl.bmRequestType & UE_DIR_IN) { bool response_found = false; response_found = lookup_connect_response_in(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &response_data, &response_length); if (!response_found) { goto err; } } else { if (!lookup_connect_response_out(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &done)) { goto err; } response_data = NULL; response_length = UGETW(req.u.ctrl.wLength); } if ((req.u.ctrl.bmRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD && req.u.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) { /* TODO: possibly revisit */ } if (response_length > sizeof(data)) response_length = 0; if ((uint32_t)UGETW(req.u.ctrl.wLength) < response_length) response_length = UGETW(req.u.ctrl.wLength); if (response_data) memcpy(data, response_data, response_length); else memset(data, 0, response_length); if (req.u.ctrl.bmRequestType & UE_DIR_IN) { if (response_length > 0) { vhci_response_t res; res.size = response_length; rv = vhci_usb_send(fd, &res, sizeof(res)); if (rv == 0) rv = vhci_usb_send(fd, data, response_length); } } else { rv = vhci_usb_recv(fd, data, response_length); } if (rv < 0) { goto err; } } sleep_ms(200); return fd; err: close(fd); return -1; } static volatile long syz_usb_connect(volatile long a0, volatile long a1, volatile long a2, volatile long a3) { uint64_t speed = a0; uint64_t dev_len = a1; const char* dev = (const char*)a2; const struct vusb_connect_descriptors* descs = (const struct vusb_connect_descriptors*)a3; return syz_usb_connect_impl(speed, dev_len, dev, descs, &lookup_connect_response_out_generic); } static volatile long syz_usb_disconnect(volatile long a0) { int fd = a0; int rv = close(fd); sleep_ms(200); return rv; } static void sandbox_common() { if (setsid() == -1) exit(1); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 8 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 0; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); } static void loop(); static int wait_for_loop(int pid) { if (pid < 0) exit(1); int status = 0; while (waitpid(-1, &status, WUNTRACED) != pid) { } return WEXITSTATUS(status); } static int do_sandbox_setuid(void) { int pid = fork(); if (pid != 0) return wait_for_loop(pid); sandbox_common(); char pwbuf[1024]; struct passwd *pw, pwres; if (getpwnam_r("nobody", &pwres, pwbuf, sizeof(pwbuf), &pw) != 0 || !pw) exit(1); if (setgroups(0, NULL)) exit(1); if (setgid(pw->pw_gid)) exit(1); if (setuid(pw->pw_uid)) exit(1); loop(); exit(1); } static long syz_execute_func(volatile long text) { volatile long p[8] = {0}; (void)p; asm volatile("" ::"r"(0l), "r"(1l), "r"(2l), "r"(3l), "r"(4l), "r"(5l), "r"(6l), "r"(7l), "r"(8l), "r"(9l), "r"(10l), "r"(11l), "r"(12l), "r"(13l)); ((void (*)(void))(text))(); return 0; } struct thread_t { int created, call; event_t ready, done; }; static struct thread_t threads[16]; static void execute_call(int call); static int running; static void* thr(void* arg) { struct thread_t* th = (struct thread_t*)arg; for (;;) { event_wait(&th->ready); event_reset(&th->ready); execute_call(th->call); __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED); event_set(&th->done); } return 0; } static void execute_one(void) { int i, call, thread; for (call = 0; call < 15; call++) { for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); thread++) { struct thread_t* th = &threads[thread]; if (!th->created) { th->created = 1; event_init(&th->ready); event_init(&th->done); event_set(&th->done); thread_start(thr, th); } if (!event_isset(&th->done)) continue; event_reset(&th->done); th->call = call; __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED); event_set(&th->ready); event_timedwait(&th->done, 45 + (call == 13 ? 3000 : 0) + (call == 14 ? 300 : 0)); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter; for (iter = 0;; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5 * 1000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } #ifndef SYS__lwp_create #define SYS__lwp_create 309 #endif #ifndef SYS__lwp_kill #define SYS__lwp_kill 318 #endif #ifndef SYS__lwp_self #define SYS__lwp_self 311 #endif #ifndef SYS__lwp_unpark_all #define SYS__lwp_unpark_all 322 #endif #ifndef SYS_compat_50__lwp_park #define SYS_compat_50__lwp_park 320 #endif #ifndef SYS_mmap #define SYS_mmap 197 #endif #ifndef SYS_shmctl #define SYS_shmctl 443 #endif uint64_t r[7] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: res = syscall(SYS__lwp_self); if (res != -1) r[0] = res; break; case 1: *(uint32_t*)0x20000100 = 0x400f0020; *(uint64_t*)0x20000108 = 0x20000080; *(uint32_t*)0x20000080 = 0; *(uint64_t*)0x20000088 = 0x20000000; *(uint32_t*)0x20000000 = 0x400f0023; *(uint64_t*)0x20000008 = 0; *(uint32_t*)0x20000010 = 7; *(uint32_t*)0x20000014 = 7; *(uint32_t*)0x20000018 = 0x101; *(uint32_t*)0x2000001c = 0x1f; *(uint64_t*)0x20000020 = 1; *(uint64_t*)0x20000028 = 0x400; *(uint32_t*)0x20000030 = 6; *(uint64_t*)0x20000038 = 1; *(uint64_t*)0x20000040 = 0x80000000; memcpy((void*)0x20000048, "\000", 1); *(uint32_t*)0x20000090 = 3; *(uint32_t*)0x20000094 = 5; *(uint32_t*)0x20000098 = 1; *(uint32_t*)0x2000009c = 0x35f7192e; *(uint64_t*)0x200000a0 = 0xd; *(uint64_t*)0x200000a8 = 0x100000000; *(uint32_t*)0x200000b0 = 5; *(uint64_t*)0x200000b8 = 6; *(uint64_t*)0x200000c0 = 0x10001; memcpy((void*)0x200000c8, "\000", 1); *(uint32_t*)0x20000110 = 0xf1b; *(uint32_t*)0x20000114 = 0x401; *(uint32_t*)0x20000118 = 0x69; *(uint32_t*)0x2000011c = 0x100; *(uint64_t*)0x20000120 = 1; *(uint64_t*)0x20000128 = 0; *(uint32_t*)0x20000130 = 0x2ecd3534; *(uint64_t*)0x20000138 = 1; *(uint64_t*)0x20000140 = 0x400; memcpy((void*)0x20000148, "-\000", 2); res = syscall(SYS__lwp_create, 0x20000100ul, 0x60ul, 0x20000180ul); if (res != -1) r[1] = *(uint32_t*)0x20000180; break; case 2: res = syscall(SYS__lwp_self); if (res != -1) r[2] = res; break; case 3: *(uint32_t*)0x200001c0 = 2; *(uint64_t*)0x200001c8 = 0; *(uint32_t*)0x200001d0 = 9; *(uint32_t*)0x200001d4 = 0x3ae; *(uint32_t*)0x200001d8 = 3; *(uint32_t*)0x200001dc = 0xc699; *(uint64_t*)0x200001e0 = 0x81; *(uint64_t*)0x200001e8 = 2; *(uint32_t*)0x200001f0 = 1; *(uint64_t*)0x200001f8 = 9; *(uint64_t*)0x20000200 = 7; memcpy((void*)0x20000208, "\000", 1); res = syscall(SYS__lwp_create, 0x200001c0ul, 0x40ul, 0x20000240ul); if (res != -1) r[3] = *(uint32_t*)0x20000240; break; case 4: res = syscall(SYS__lwp_self); if (res != -1) r[4] = res; break; case 5: *(uint32_t*)0x20000280 = r[0]; *(uint32_t*)0x20000284 = r[1]; *(uint32_t*)0x20000288 = r[2]; *(uint32_t*)0x2000028c = r[3]; *(uint32_t*)0x20000290 = r[4]; *(uint64_t*)0x200002c0 = 4; syscall(SYS__lwp_unpark_all, 0x20000280ul, 5ul, 0x200002c0ul); break; case 6: *(uint32_t*)0x20000380 = 8; *(uint64_t*)0x20000388 = 0x20000300; *(uint32_t*)0x20000300 = 0x400f002c; *(uint64_t*)0x20000308 = 0; *(uint32_t*)0x20000310 = 0x80000000; *(uint32_t*)0x20000314 = 0xa706; *(uint32_t*)0x20000318 = 0; *(uint32_t*)0x2000031c = 8; *(uint64_t*)0x20000320 = 0x1f; *(uint64_t*)0x20000328 = 6; *(uint32_t*)0x20000330 = 1; *(uint64_t*)0x20000338 = 0x101; *(uint64_t*)0x20000340 = 5; memcpy((void*)0x20000348, "\000", 1); *(uint32_t*)0x20000390 = 2; *(uint32_t*)0x20000394 = 9; *(uint32_t*)0x20000398 = 0x10000; *(uint32_t*)0x2000039c = 1; *(uint64_t*)0x200003a0 = 2; *(uint64_t*)0x200003a8 = 0xffffffff; *(uint32_t*)0x200003b0 = 5; *(uint64_t*)0x200003b8 = 0x20; *(uint64_t*)0x200003c0 = 0xfff; memcpy((void*)0x200003c8, "\000", 1); res = syscall(SYS__lwp_create, 0x20000380ul, 0x40ul, 0x20000400ul); if (res != -1) r[5] = *(uint32_t*)0x20000400; break; case 7: syscall(SYS__lwp_kill, r[5], 8); break; case 8: *(uint32_t*)0x20000440 = 0x101; *(uint64_t*)0x20000448 = 8; *(uint64_t*)0x20000480 = 1; syscall(SYS_compat_50__lwp_park, 0x20000440ul, 0, 0ul, 0x20000480ul); break; case 9: syscall(SYS_shmctl, 0, 3ul, 0); break; case 10: memcpy((void*)0x20000000, "\xaf\xe8\xd2\x38\x3c\x3e\xf2\xff\x5f\x6e\x8c\x9a\x4d\x35\xc6\x02\x19\x87\x66\xff\x84\xe3\xac\xfd\x84\xf7\x78\x35\x3f\x61\x84\x2e\x8a\x19\x8d\x7e\x50\x0c\xce\x4b\x81\x36\x90\x33\x85\x89\xcc\xa0\x7a\xcd\xe0\xe9\x83\x4f\xd0\x9b\x00\xf3\x14\x6b\xa0\xdd\xb9\xac\x17\xae\x01\x1f\xb8\x61\xaf\x25\x83\x25\x47\x6c\x96\x21\x38\x8e\xca\x4b\x11\xc0", 84); break; case 11: memcpy((void*)0x20000080, "\x36\x42\x83\x3d\xd7\x0b\x58\x6a\x46\x3e\x64\x0f\xeb\xb1\x9b\x00\x00\x00\xc4\xc3\xf9\x7c\x6d\x4c\xa6\xf3\x44\xa6\xc4\x43\x85\x7f\xc6\x18\xfd\xc4\x03\x11\x6f\x26\xfa\x44\x7b\xa9\x64\x2e\x47\x0f\x12\xfc\x64\xf2\x0f\x11\x62\x00", 56); syz_execute_func(0x20000080); break; case 12: break; case 13: memcpy((void*)0x20000100, "\x93\x5e\x05\x7a\xa8\xdd\xae\xce\x20\x39\x4d\x86\xc4\x5c\x4a\x50\xf7\x27\x8a\x87\x91\xb0\xcd\xbc\xf6\xe8\x32\xb9\x05\x2f\x3a\xb9\x90\x63\x70\x55\x06\xc6\x6e\xb3\x37\x4a\xad\x5e\x1f\xde\xd7\x51\x5c\x8c\xb7\xfe\x2c\x21\x69\x94\xff\xb8\x9e\x91\x54\x96\xa2\x07\x40\x41\x68\xe8\x6b\xa8\x2f\x7c\x0d\x49\x64\xe7\xe7\x56\xac\x68\x99\x19\xcf\x9f\x4b\xf8\x7d\x3e\x3b\x51\x74\x60\x5c\x8f\x25\x6d\x28\x20\x4e\x42\x8b\xf2\x95\xff\x31\x2e\x2e\x27\xa1\xc2\xaf\xb8\x93\xdb\xe7\x86\x70\x60\xc6\x50", 120); memcpy((void*)0x20000180, "\x2b\x06\x57\x4b\x32\xd8\xeb\x1d\x74\x79\x43\x1c\x2a\xb5\x3f\xc2\xd2\x4c\xbf\x1c\x43\x68\xa8\x24\xc8\xcd\x82\x17\xf4\x4d\x51\xb1\x18\x89\xf9\x29\xd6\x5e\x79\x00\xcc\xcb\x51\x66\x45\x3f\xfe\x7f\xa1\xf3\x28\xdf\x96\x95\xa4\x45\x7b\x58\xf1\x31\x91\xe9\x0c\xa7\x94\x18\x6e\x76\x32\x17\xe9\x23\xbd\xc4\xf2\x04\x34\xeb\xa9\x66\x16\xcc\x3b\x27\xea\x87\xd5\x4a\x6c\x43\x4d\x77\xb7\x37\xd1\x8d\x0d\x8d\x53\x7d\xce\x33\x0d\x27\x59\xbe\x67\xe2\x2e\x1d\xc6\x17\xf5\x17\x14\x7f\xae\xb4\x4e\x34\x3d\x63\x0b\x33\xd1\xd3\x16\x06\xb2\xa3\x97\x1e\xfc\xd0\x3e\x4d\x87\xb5\xe6\x00\x32\x37\x23\xab\xfe\xf8\x1a\xa7\x1b\xea\x16\xbd\xf9\x56\x1a\xca\x75\xce\x32\x65\xde\x3a\xc8\xa1\x59\x0a\x61\xed\xe7\x3a\x7b\x69\x43\xdd\xf3\xe3\xfc\xf1\x9e\x47\x8f\x55\x85\x9d\xb7\x8d\x6b\xaa\x46\xb7\xa7\xe2\x15\x9f\x6f\x4f\x20\xcc\xb1\x8d\x5c\x41\xfb\x43\x02\x49", 206); res = syz_usb_connect(0xfffffffffffffeff, 0x78, 0x20000100, 0x20000180); if (res != -1) r[6] = res; break; case 14: syz_usb_disconnect(r[6]); break; } } int main(void) { syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul, 0ul); use_temporary_dir(); do_sandbox_setuid(); return 0; } : In function 'syz_usb_connect_impl': :637:63: error: unknown type name 'usb_ctrlrequest' :642:55: error: unknown type name 'usb_ctrlrequest' compiler invocation: /syzkaller/netbsd/src/../tools/bin/x86_64--netbsd-g++ [-o /tmp/syz-executor565828532 -DGOOS_netbsd=1 -DGOARCH_amd64=1 -DHOSTGOOS_linux=1 -x c - -m64 --sysroot /syzkaller/netbsd/src/../dest/ -O2 -pthread -Wall -Werror -Wparentheses -Wframe-larger-than=16384] --- FAIL: TestGenerate/netbsd/amd64/13 (0.42s) csource_test.go:123: opts: {Threaded:true Collide:false Repeat:true RepeatTimes:0 Procs:0 Sandbox:none Fault:false FaultCall:0 FaultNth:0 Leak:false NetInjection:false NetDevices:false NetReset:false Cgroups:false BinfmtMisc:false CloseFDs:false KCSAN:false DevlinkPCI:false USB:false UseTmpDir:true HandleSegv:false Repro:false Trace:true} program: r0 = _lwp_self() _lwp_create(&(0x7f0000000100)={0x400f0020, &(0x7f0000000080)={0x0, &(0x7f0000000000)={0x400f0023, 0x0, {[0x7, 0x7, 0x101, 0x1f]}, {0x1, 0x400, 0x6}, {0x1, 0x80000000, '\x00'}}, {[0x3, 0x5, 0x1, 0x35f7192e]}, {0xd, 0x100000000, 0x5}, {0x6, 0x10001, '\x00'}}, {[0xf1b, 0x401, 0x69, 0x100]}, {0x1, 0x0, 0x16ac7582ecd3534}, {0x1, 0x400, '-\x00'}}, 0x60, &(0x7f0000000180)=0x0) r2 = _lwp_self() _lwp_create(&(0x7f00000001c0)={0x2, 0x0, {[0x9, 0x3ae, 0x3, 0xc699]}, {0x81, 0x2, 0x1}, {0x9, 0x7, '\x00'}}, 0x40, &(0x7f0000000240)=0x0) r4 = _lwp_self() _lwp_unpark_all(&(0x7f0000000280)=[r0, r1, r2, r3, r4], 0x5, &(0x7f00000002c0)=0x4) _lwp_create(&(0x7f0000000380)={0x8, &(0x7f0000000300)={0x400f002c, 0x0, {[0x80000000, 0xa706, 0x0, 0x8]}, {0x1f, 0x6, 0x1}, {0x101, 0x5, '\x00'}}, {[0x2, 0x9, 0x10000, 0x1]}, {0x2, 0xffffffff, 0x5}, {0x20, 0xfff, '\x00'}}, 0x40, &(0x7f0000000400)=0x0) _lwp_kill(r5, 0x8) compat_50__lwp_park(&(0x7f0000000440)={0x101, 0x8}, 0x0, 0x0, &(0x7f0000000480)=0x1) shmctl$SHM_LOCK(0x0, 0x3) syz_emit_ethernet(0x54, &(0x7f0000000000)="afe8d2383c3ef2ff5f6e8c9a4d35c602198766ff84e3acfd84f778353f61842e8a198d7e500cce4b813690338589cca07acde0e9834fd09b00f3146ba0ddb9ac17ae011fb861af258325476c9621388eca4b11c0") syz_execute_func(&(0x7f0000000080)="3642833dd70b586a463e640febb19b000000c4c3f97c6d4ca6f344a6c443857fc618fdc403116f26fa447ba9642e470f12fc64f20f116200") syz_extract_tcp_res(&(0x7f00000000c0), 0x3, 0xc5d) r6 = syz_usb_connect(0xfffffffffffffeff, 0x78, &(0x7f0000000100)="935e057aa8ddaece20394d86c45c4a50f7278a8791b0cdbcf6e832b9052f3ab99063705506c66eb3374aad5e1fded7515c8cb7fe2c216994ffb89e915496a207404168e86ba82f7c0d4964e7e756ac689919cf9f4bf87d3e3b5174605c8f256d28204e428bf295ff312e2e27a1c2afb893dbe7867060c650", &(0x7f0000000180)="2b06574b32d8eb1d7479431c2ab53fc2d24cbf1c4368a824c8cd8217f44d51b11889f929d65e7900cccb5166453ffe7fa1f328df9695a4457b58f13191e90ca794186e763217e923bdc4f20434eba96616cc3b27ea87d54a6c434d77b737d18d0d8d537dce330d2759be67e22e1dc617f517147faeb44e343d630b33d1d31606b2a3971efcd03e4d87b5e600323723abfef81aa71bea16bdf9561aca75ce3265de3ac8a1590a61ede73a7b6943ddf3e3fcf19e478f55859db78d6baa46b7a7e2159f6f4f20ccb18d5c41fb430249") syz_usb_disconnect(r6) csource_test.go:124: failed to build program: // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static unsigned long long procid; static void kill_and_wait(int pid, int* status) { kill(pid, SIGKILL); while (waitpid(-1, status, 0) != pid) { } } static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void use_temporary_dir(void) { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } static void remove_dir(const char* dir) { DIR* dp; struct dirent* ep; dp = opendir(dir); if (dp == NULL) exit(1); while ((ep = readdir(dp))) { if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue; char filename[FILENAME_MAX]; snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } if (unlink(filename)) exit(1); } closedir(dp); if (rmdir(dir)) exit(1); } static void thread_start(void* (*fn)(void*), void* arg) { pthread_t th; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 128 << 10); int i; for (i = 0; i < 100; i++) { if (pthread_create(&th, &attr, fn, arg) == 0) { pthread_attr_destroy(&attr); return; } if (errno == EAGAIN) { usleep(50); continue; } break; } exit(1); } typedef struct { pthread_mutex_t mu; pthread_cond_t cv; int state; } event_t; static void event_init(event_t* ev) { if (pthread_mutex_init(&ev->mu, 0)) exit(1); if (pthread_cond_init(&ev->cv, 0)) exit(1); ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { pthread_mutex_lock(&ev->mu); if (ev->state) exit(1); ev->state = 1; pthread_mutex_unlock(&ev->mu); pthread_cond_broadcast(&ev->cv); } static void event_wait(event_t* ev) { pthread_mutex_lock(&ev->mu); while (!ev->state) pthread_cond_wait(&ev->cv, &ev->mu); pthread_mutex_unlock(&ev->mu); } static int event_isset(event_t* ev) { pthread_mutex_lock(&ev->mu); int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; pthread_mutex_lock(&ev->mu); for (;;) { if (ev->state) break; uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; pthread_cond_timedwait(&ev->cv, &ev->mu, &ts); now = current_time_ms(); if (now - start > timeout) break; } int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } /* -------------------------------------------------------------------------- */ /* * Redefinitions to match the linux types used in common_usb.h. */ struct usb_endpoint_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; uint16_t wMaxPacketSize; uint8_t bInterval; uint8_t bRefresh; uint8_t bSynchAddress; } __attribute__((packed)); struct usb_device_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint16_t idVendor; uint16_t idProduct; uint16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; } __attribute__((packed)); struct usb_config_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; } __attribute__((packed)); struct usb_interface_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bNumEndpoints; uint8_t bInterfaceClass; uint8_t bInterfaceSubClass; uint8_t bInterfaceProtocol; uint8_t iInterface; } __attribute__((packed)); struct usb_ctrlrequest { uint8_t bRequestType; uint8_t bRequest; uint16_t wValue; uint16_t wIndex; uint16_t wLength; } __attribute__((packed)); struct usb_qualifier_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint8_t bNumConfigurations; uint8_t bRESERVED; } __attribute__((packed)); #define USB_TYPE_MASK (0x03 << 5) #define USB_TYPE_STANDARD (0x00 << 5) #define USB_TYPE_CLASS (0x01 << 5) #define USB_TYPE_VENDOR (0x02 << 5) #define USB_TYPE_RESERVED (0x03 << 5) #define USB_DT_DEVICE 0x01 #define USB_DT_CONFIG 0x02 #define USB_DT_STRING 0x03 #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_DEVICE_QUALIFIER 0x06 #define USB_DT_OTHER_SPEED_CONFIG 0x07 #define USB_DT_INTERFACE_POWER 0x08 #define USB_DT_OTG 0x09 #define USB_DT_DEBUG 0x0a #define USB_DT_INTERFACE_ASSOCIATION 0x0b #define USB_DT_SECURITY 0x0c #define USB_DT_KEY 0x0d #define USB_DT_ENCRYPTION_TYPE 0x0e #define USB_DT_BOS 0x0f #define USB_DT_DEVICE_CAPABILITY 0x10 #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 #define USB_DT_WIRE_ADAPTER 0x21 #define USB_DT_RPIPE 0x22 #define USB_DT_CS_RADIO_CONTROL 0x23 #define USB_DT_PIPE_USAGE 0x24 #define USB_DT_SS_ENDPOINT_COMP 0x30 #define USB_DT_SSP_ISOC_ENDPOINT_COMP 0x31 #define USB_REQ_GET_STATUS 0x00 #define USB_REQ_CLEAR_FEATURE 0x01 #define USB_REQ_SET_FEATURE 0x03 #define USB_REQ_SET_ADDRESS 0x05 #define USB_REQ_GET_DESCRIPTOR 0x06 #define USB_REQ_SET_DESCRIPTOR 0x07 #define USB_REQ_GET_CONFIGURATION 0x08 #define USB_REQ_SET_CONFIGURATION 0x09 #define USB_REQ_GET_INTERFACE 0x0A #define USB_REQ_SET_INTERFACE 0x0B #define USB_REQ_SYNCH_FRAME 0x0C #define USB_REQ_SET_SEL 0x30 #define USB_REQ_SET_ISOCH_DELAY 0x31 #define USB_REQ_SET_ENCRYPTION 0x0D #define USB_REQ_GET_ENCRYPTION 0x0E #define USB_REQ_RPIPE_ABORT 0x0E #define USB_REQ_SET_HANDSHAKE 0x0F #define USB_REQ_RPIPE_RESET 0x0F #define USB_REQ_GET_HANDSHAKE 0x10 #define USB_REQ_SET_CONNECTION 0x11 #define USB_REQ_SET_SECURITY_DATA 0x12 #define USB_REQ_GET_SECURITY_DATA 0x13 #define USB_REQ_SET_WUSB_DATA 0x14 #define USB_REQ_LOOPBACK_DATA_WRITE 0x15 #define USB_REQ_LOOPBACK_DATA_READ 0x16 #define USB_REQ_SET_INTERFACE_DS 0x17 #define USB_REQ_GET_PARTNER_PDO 20 #define USB_REQ_GET_BATTERY_STATUS 21 #define USB_REQ_SET_PDO 22 #define USB_REQ_GET_VDM 23 #define USB_REQ_SEND_VDM 24 #define USB_MAX_IFACE_NUM 4 #define USB_MAX_EP_NUM 32 #define USB_MAX_FDS 6 struct usb_endpoint_index { struct usb_endpoint_descriptor desc; int handle; }; struct usb_iface_index { struct usb_interface_descriptor* iface; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bInterfaceClass; struct usb_endpoint_index eps[USB_MAX_EP_NUM]; int eps_num; }; struct usb_device_index { struct usb_device_descriptor* dev; struct usb_config_descriptor* config; uint8_t bDeviceClass; uint8_t bMaxPower; int config_length; struct usb_iface_index ifaces[USB_MAX_IFACE_NUM]; int ifaces_num; int iface_cur; }; struct usb_info { int fd; struct usb_device_index index; }; static struct usb_info usb_devices[USB_MAX_FDS]; static int usb_devices_num; static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index) { if (length < sizeof(*index->dev) + sizeof(*index->config)) return false; memset(index, 0, sizeof(*index)); index->dev = (struct usb_device_descriptor*)buffer; index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev)); index->bDeviceClass = index->dev->bDeviceClass; index->bMaxPower = index->config->bMaxPower; index->config_length = length - sizeof(*index->dev); index->iface_cur = -1; size_t offset = 0; while (true) { if (offset + 1 >= length) break; uint8_t desc_length = buffer[offset]; uint8_t desc_type = buffer[offset + 1]; if (desc_length <= 2) break; if (offset + desc_length > length) break; if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) { struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset); index->ifaces[index->ifaces_num].iface = iface; index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber; index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting; index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass; index->ifaces_num++; } if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) { struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1]; if (iface->eps_num < USB_MAX_EP_NUM) { memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc)); iface->eps_num++; } } offset += desc_length; } return true; } static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len) { int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED); if (i >= USB_MAX_FDS) return NULL; int rv = 0; rv = parse_usb_descriptor(dev, dev_len, &usb_devices[i].index); if (!rv) return NULL; __atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE); return &usb_devices[i].index; } static struct usb_device_index* lookup_usb_index(int fd) { int i; for (i = 0; i < USB_MAX_FDS; i++) { if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd) { return &usb_devices[i].index; } } return NULL; } struct vusb_connect_string_descriptor { uint32_t len; char* str; } __attribute__((packed)); struct vusb_connect_descriptors { uint32_t qual_len; char* qual; uint32_t bos_len; char* bos; uint32_t strs_len; struct vusb_connect_string_descriptor strs[0]; } __attribute__((packed)); static const char default_string[] = { 8, USB_DT_STRING, 's', 0, 'y', 0, 'z', 0 }; static const char default_lang_id[] = { 4, USB_DT_STRING, 0x09, 0x04 }; static bool lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, char** response_data, uint32_t* response_length) { struct usb_device_index* index = lookup_usb_index(fd); uint8_t str_idx; if (!index) return false; switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_GET_DESCRIPTOR: switch (ctrl->wValue >> 8) { case USB_DT_DEVICE: *response_data = (char*)index->dev; *response_length = sizeof(*index->dev); return true; case USB_DT_CONFIG: *response_data = (char*)index->config; *response_length = index->config_length; return true; case USB_DT_STRING: str_idx = (uint8_t)ctrl->wValue; if (descs && str_idx < descs->strs_len) { *response_data = descs->strs[str_idx].str; *response_length = descs->strs[str_idx].len; return true; } if (str_idx == 0) { *response_data = (char*)&default_lang_id[0]; *response_length = default_lang_id[0]; return true; } *response_data = (char*)&default_string[0]; *response_length = default_string[0]; return true; case USB_DT_BOS: *response_data = descs->bos; *response_length = descs->bos_len; return true; case USB_DT_DEVICE_QUALIFIER: if (!descs->qual) { struct usb_qualifier_descriptor* qual = (struct usb_qualifier_descriptor*)response_data; qual->bLength = sizeof(*qual); qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; qual->bcdUSB = index->dev->bcdUSB; qual->bDeviceClass = index->dev->bDeviceClass; qual->bDeviceSubClass = index->dev->bDeviceSubClass; qual->bDeviceProtocol = index->dev->bDeviceProtocol; qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0; qual->bNumConfigurations = index->dev->bNumConfigurations; qual->bRESERVED = 0; *response_length = sizeof(*qual); return true; } *response_data = descs->qual; *response_length = descs->qual_len; return true; default: break; } break; default: break; } break; default: break; } return false; } typedef bool (*lookup_connect_out_response_t)(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done); static bool lookup_connect_response_out_generic(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done) { switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_SET_CONFIGURATION: *done = true; return true; default: break; } break; } return false; } /* -------------------------------------------------------------------------- */ static int vhci_open(void) { return open("/dev/vhci", O_RDWR); } static int vhci_setport(int fd, u_int port) { struct vhci_ioc_set_port args; args.port = port; return ioctl(fd, VHCI_IOC_SET_PORT, &args); } static int vhci_usb_attach(int fd) { return ioctl(fd, VHCI_IOC_USB_ATTACH, NULL); } static int vhci_usb_recv(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = read(fd, ptr, size); if (done < 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } static int vhci_usb_send(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = write(fd, ptr, size); if (done <= 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } /* -------------------------------------------------------------------------- */ static volatile long syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev, const struct vusb_connect_descriptors* descs, lookup_connect_out_response_t lookup_connect_response_out) { struct usb_device_index* index; int portnum, fd, rv; bool done; portnum = procid + 1; if (!dev) { return -1; } if (portnum != 1) { /* For now, we support only one proc. */ return -1; } fd = vhci_open(); if (fd < 0) { return -1; } index = add_usb_index(fd, dev, dev_len); if (!index) { goto err; } rv = vhci_setport(fd, portnum); if (rv != 0) { goto err; } rv = vhci_usb_attach(fd); if (rv != 0) { goto err; } done = false; while (!done) { vhci_request_t req; rv = vhci_usb_recv(fd, &req, sizeof(req)); if (rv != 0) { goto err; } if (req.type != VHCI_REQ_CTRL) { goto err; } char* response_data = NULL; uint32_t response_length = 0; char data[4096]; if (req.u.ctrl.bmRequestType & UE_DIR_IN) { bool response_found = false; response_found = lookup_connect_response_in(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &response_data, &response_length); if (!response_found) { goto err; } } else { if (!lookup_connect_response_out(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &done)) { goto err; } response_data = NULL; response_length = UGETW(req.u.ctrl.wLength); } if ((req.u.ctrl.bmRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD && req.u.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) { /* TODO: possibly revisit */ } if (response_length > sizeof(data)) response_length = 0; if ((uint32_t)UGETW(req.u.ctrl.wLength) < response_length) response_length = UGETW(req.u.ctrl.wLength); if (response_data) memcpy(data, response_data, response_length); else memset(data, 0, response_length); if (req.u.ctrl.bmRequestType & UE_DIR_IN) { if (response_length > 0) { vhci_response_t res; res.size = response_length; rv = vhci_usb_send(fd, &res, sizeof(res)); if (rv == 0) rv = vhci_usb_send(fd, data, response_length); } } else { rv = vhci_usb_recv(fd, data, response_length); } if (rv < 0) { goto err; } } sleep_ms(200); return fd; err: close(fd); return -1; } static volatile long syz_usb_connect(volatile long a0, volatile long a1, volatile long a2, volatile long a3) { uint64_t speed = a0; uint64_t dev_len = a1; const char* dev = (const char*)a2; const struct vusb_connect_descriptors* descs = (const struct vusb_connect_descriptors*)a3; return syz_usb_connect_impl(speed, dev_len, dev, descs, &lookup_connect_response_out_generic); } static volatile long syz_usb_disconnect(volatile long a0) { int fd = a0; int rv = close(fd); sleep_ms(200); return rv; } static void sandbox_common() { if (setsid() == -1) exit(1); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 8 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 0; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); } static void loop(); static int do_sandbox_none(void) { sandbox_common(); loop(); return 0; } static long syz_execute_func(volatile long text) { volatile long p[8] = {0}; (void)p; asm volatile("" ::"r"(0l), "r"(1l), "r"(2l), "r"(3l), "r"(4l), "r"(5l), "r"(6l), "r"(7l), "r"(8l), "r"(9l), "r"(10l), "r"(11l), "r"(12l), "r"(13l)); ((void (*)(void))(text))(); return 0; } struct thread_t { int created, call; event_t ready, done; }; static struct thread_t threads[16]; static void execute_call(int call); static int running; static void* thr(void* arg) { struct thread_t* th = (struct thread_t*)arg; for (;;) { event_wait(&th->ready); event_reset(&th->ready); execute_call(th->call); __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED); event_set(&th->done); } return 0; } static void execute_one(void) { fprintf(stderr, "### start\n"); int i, call, thread; for (call = 0; call < 15; call++) { for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); thread++) { struct thread_t* th = &threads[thread]; if (!th->created) { th->created = 1; event_init(&th->ready); event_init(&th->done); event_set(&th->done); thread_start(thr, th); } if (!event_isset(&th->done)) continue; event_reset(&th->done); th->call = call; __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED); event_set(&th->ready); event_timedwait(&th->done, 45 + (call == 13 ? 3000 : 0) + (call == 14 ? 300 : 0)); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter; for (iter = 0;; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5 * 1000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } #ifndef SYS__lwp_create #define SYS__lwp_create 309 #endif #ifndef SYS__lwp_kill #define SYS__lwp_kill 318 #endif #ifndef SYS__lwp_self #define SYS__lwp_self 311 #endif #ifndef SYS__lwp_unpark_all #define SYS__lwp_unpark_all 322 #endif #ifndef SYS_compat_50__lwp_park #define SYS_compat_50__lwp_park 320 #endif #ifndef SYS_mmap #define SYS_mmap 197 #endif #ifndef SYS_shmctl #define SYS_shmctl 443 #endif uint64_t r[7] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: res = syscall(SYS__lwp_self); fprintf(stderr, "### call=0 errno=%u\n", res == -1 ? errno : 0); if (res != -1) r[0] = res; break; case 1: *(uint32_t*)0x20000100 = 0x400f0020; *(uint64_t*)0x20000108 = 0x20000080; *(uint32_t*)0x20000080 = 0; *(uint64_t*)0x20000088 = 0x20000000; *(uint32_t*)0x20000000 = 0x400f0023; *(uint64_t*)0x20000008 = 0; *(uint32_t*)0x20000010 = 7; *(uint32_t*)0x20000014 = 7; *(uint32_t*)0x20000018 = 0x101; *(uint32_t*)0x2000001c = 0x1f; *(uint64_t*)0x20000020 = 1; *(uint64_t*)0x20000028 = 0x400; *(uint32_t*)0x20000030 = 6; *(uint64_t*)0x20000038 = 1; *(uint64_t*)0x20000040 = 0x80000000; memcpy((void*)0x20000048, "\000", 1); *(uint32_t*)0x20000090 = 3; *(uint32_t*)0x20000094 = 5; *(uint32_t*)0x20000098 = 1; *(uint32_t*)0x2000009c = 0x35f7192e; *(uint64_t*)0x200000a0 = 0xd; *(uint64_t*)0x200000a8 = 0x100000000; *(uint32_t*)0x200000b0 = 5; *(uint64_t*)0x200000b8 = 6; *(uint64_t*)0x200000c0 = 0x10001; memcpy((void*)0x200000c8, "\000", 1); *(uint32_t*)0x20000110 = 0xf1b; *(uint32_t*)0x20000114 = 0x401; *(uint32_t*)0x20000118 = 0x69; *(uint32_t*)0x2000011c = 0x100; *(uint64_t*)0x20000120 = 1; *(uint64_t*)0x20000128 = 0; *(uint32_t*)0x20000130 = 0x2ecd3534; *(uint64_t*)0x20000138 = 1; *(uint64_t*)0x20000140 = 0x400; memcpy((void*)0x20000148, "-\000", 2); res = syscall(SYS__lwp_create, 0x20000100ul, 0x60ul, 0x20000180ul); fprintf(stderr, "### call=1 errno=%u\n", res == -1 ? errno : 0); if (res != -1) r[1] = *(uint32_t*)0x20000180; break; case 2: res = syscall(SYS__lwp_self); fprintf(stderr, "### call=2 errno=%u\n", res == -1 ? errno : 0); if (res != -1) r[2] = res; break; case 3: *(uint32_t*)0x200001c0 = 2; *(uint64_t*)0x200001c8 = 0; *(uint32_t*)0x200001d0 = 9; *(uint32_t*)0x200001d4 = 0x3ae; *(uint32_t*)0x200001d8 = 3; *(uint32_t*)0x200001dc = 0xc699; *(uint64_t*)0x200001e0 = 0x81; *(uint64_t*)0x200001e8 = 2; *(uint32_t*)0x200001f0 = 1; *(uint64_t*)0x200001f8 = 9; *(uint64_t*)0x20000200 = 7; memcpy((void*)0x20000208, "\000", 1); res = syscall(SYS__lwp_create, 0x200001c0ul, 0x40ul, 0x20000240ul); fprintf(stderr, "### call=3 errno=%u\n", res == -1 ? errno : 0); if (res != -1) r[3] = *(uint32_t*)0x20000240; break; case 4: res = syscall(SYS__lwp_self); fprintf(stderr, "### call=4 errno=%u\n", res == -1 ? errno : 0); if (res != -1) r[4] = res; break; case 5: *(uint32_t*)0x20000280 = r[0]; *(uint32_t*)0x20000284 = r[1]; *(uint32_t*)0x20000288 = r[2]; *(uint32_t*)0x2000028c = r[3]; *(uint32_t*)0x20000290 = r[4]; *(uint64_t*)0x200002c0 = 4; res = syscall(SYS__lwp_unpark_all, 0x20000280ul, 5ul, 0x200002c0ul); fprintf(stderr, "### call=5 errno=%u\n", res == -1 ? errno : 0); break; case 6: *(uint32_t*)0x20000380 = 8; *(uint64_t*)0x20000388 = 0x20000300; *(uint32_t*)0x20000300 = 0x400f002c; *(uint64_t*)0x20000308 = 0; *(uint32_t*)0x20000310 = 0x80000000; *(uint32_t*)0x20000314 = 0xa706; *(uint32_t*)0x20000318 = 0; *(uint32_t*)0x2000031c = 8; *(uint64_t*)0x20000320 = 0x1f; *(uint64_t*)0x20000328 = 6; *(uint32_t*)0x20000330 = 1; *(uint64_t*)0x20000338 = 0x101; *(uint64_t*)0x20000340 = 5; memcpy((void*)0x20000348, "\000", 1); *(uint32_t*)0x20000390 = 2; *(uint32_t*)0x20000394 = 9; *(uint32_t*)0x20000398 = 0x10000; *(uint32_t*)0x2000039c = 1; *(uint64_t*)0x200003a0 = 2; *(uint64_t*)0x200003a8 = 0xffffffff; *(uint32_t*)0x200003b0 = 5; *(uint64_t*)0x200003b8 = 0x20; *(uint64_t*)0x200003c0 = 0xfff; memcpy((void*)0x200003c8, "\000", 1); res = syscall(SYS__lwp_create, 0x20000380ul, 0x40ul, 0x20000400ul); fprintf(stderr, "### call=6 errno=%u\n", res == -1 ? errno : 0); if (res != -1) r[5] = *(uint32_t*)0x20000400; break; case 7: res = syscall(SYS__lwp_kill, r[5], 8); fprintf(stderr, "### call=7 errno=%u\n", res == -1 ? errno : 0); break; case 8: *(uint32_t*)0x20000440 = 0x101; *(uint64_t*)0x20000448 = 8; *(uint64_t*)0x20000480 = 1; res = syscall(SYS_compat_50__lwp_park, 0x20000440ul, 0, 0ul, 0x20000480ul); fprintf(stderr, "### call=8 errno=%u\n", res == -1 ? errno : 0); break; case 9: res = syscall(SYS_shmctl, 0, 3ul, 0); fprintf(stderr, "### call=9 errno=%u\n", res == -1 ? errno : 0); break; case 10: memcpy((void*)0x20000000, "\xaf\xe8\xd2\x38\x3c\x3e\xf2\xff\x5f\x6e\x8c\x9a\x4d\x35\xc6\x02\x19\x87\x66\xff\x84\xe3\xac\xfd\x84\xf7\x78\x35\x3f\x61\x84\x2e\x8a\x19\x8d\x7e\x50\x0c\xce\x4b\x81\x36\x90\x33\x85\x89\xcc\xa0\x7a\xcd\xe0\xe9\x83\x4f\xd0\x9b\x00\xf3\x14\x6b\xa0\xdd\xb9\xac\x17\xae\x01\x1f\xb8\x61\xaf\x25\x83\x25\x47\x6c\x96\x21\x38\x8e\xca\x4b\x11\xc0", 84); (void)res; break; case 11: memcpy((void*)0x20000080, "\x36\x42\x83\x3d\xd7\x0b\x58\x6a\x46\x3e\x64\x0f\xeb\xb1\x9b\x00\x00\x00\xc4\xc3\xf9\x7c\x6d\x4c\xa6\xf3\x44\xa6\xc4\x43\x85\x7f\xc6\x18\xfd\xc4\x03\x11\x6f\x26\xfa\x44\x7b\xa9\x64\x2e\x47\x0f\x12\xfc\x64\xf2\x0f\x11\x62\x00", 56); res = syz_execute_func(0x20000080); fprintf(stderr, "### call=11 errno=%u\n", res == -1 ? errno : 0); break; case 12: (void)res; break; case 13: memcpy((void*)0x20000100, "\x93\x5e\x05\x7a\xa8\xdd\xae\xce\x20\x39\x4d\x86\xc4\x5c\x4a\x50\xf7\x27\x8a\x87\x91\xb0\xcd\xbc\xf6\xe8\x32\xb9\x05\x2f\x3a\xb9\x90\x63\x70\x55\x06\xc6\x6e\xb3\x37\x4a\xad\x5e\x1f\xde\xd7\x51\x5c\x8c\xb7\xfe\x2c\x21\x69\x94\xff\xb8\x9e\x91\x54\x96\xa2\x07\x40\x41\x68\xe8\x6b\xa8\x2f\x7c\x0d\x49\x64\xe7\xe7\x56\xac\x68\x99\x19\xcf\x9f\x4b\xf8\x7d\x3e\x3b\x51\x74\x60\x5c\x8f\x25\x6d\x28\x20\x4e\x42\x8b\xf2\x95\xff\x31\x2e\x2e\x27\xa1\xc2\xaf\xb8\x93\xdb\xe7\x86\x70\x60\xc6\x50", 120); memcpy((void*)0x20000180, "\x2b\x06\x57\x4b\x32\xd8\xeb\x1d\x74\x79\x43\x1c\x2a\xb5\x3f\xc2\xd2\x4c\xbf\x1c\x43\x68\xa8\x24\xc8\xcd\x82\x17\xf4\x4d\x51\xb1\x18\x89\xf9\x29\xd6\x5e\x79\x00\xcc\xcb\x51\x66\x45\x3f\xfe\x7f\xa1\xf3\x28\xdf\x96\x95\xa4\x45\x7b\x58\xf1\x31\x91\xe9\x0c\xa7\x94\x18\x6e\x76\x32\x17\xe9\x23\xbd\xc4\xf2\x04\x34\xeb\xa9\x66\x16\xcc\x3b\x27\xea\x87\xd5\x4a\x6c\x43\x4d\x77\xb7\x37\xd1\x8d\x0d\x8d\x53\x7d\xce\x33\x0d\x27\x59\xbe\x67\xe2\x2e\x1d\xc6\x17\xf5\x17\x14\x7f\xae\xb4\x4e\x34\x3d\x63\x0b\x33\xd1\xd3\x16\x06\xb2\xa3\x97\x1e\xfc\xd0\x3e\x4d\x87\xb5\xe6\x00\x32\x37\x23\xab\xfe\xf8\x1a\xa7\x1b\xea\x16\xbd\xf9\x56\x1a\xca\x75\xce\x32\x65\xde\x3a\xc8\xa1\x59\x0a\x61\xed\xe7\x3a\x7b\x69\x43\xdd\xf3\xe3\xfc\xf1\x9e\x47\x8f\x55\x85\x9d\xb7\x8d\x6b\xaa\x46\xb7\xa7\xe2\x15\x9f\x6f\x4f\x20\xcc\xb1\x8d\x5c\x41\xfb\x43\x02\x49", 206); res = syz_usb_connect(0xfffffffffffffeff, 0x78, 0x20000100, 0x20000180); fprintf(stderr, "### call=13 errno=%u\n", res == -1 ? errno : 0); if (res != -1) r[6] = res; break; case 14: res = syz_usb_disconnect(r[6]); fprintf(stderr, "### call=14 errno=%u\n", res == -1 ? errno : 0); break; } } int main(void) { syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul, 0ul); use_temporary_dir(); do_sandbox_none(); return 0; } : In function 'syz_usb_connect_impl': :637:63: error: unknown type name 'usb_ctrlrequest' :642:55: error: unknown type name 'usb_ctrlrequest' compiler invocation: /syzkaller/netbsd/src/../tools/bin/x86_64--netbsd-g++ [-o /tmp/syz-executor797226883 -DGOOS_netbsd=1 -DGOARCH_amd64=1 -DHOSTGOOS_linux=1 -x c - -m64 --sysroot /syzkaller/netbsd/src/../dest/ -O2 -pthread -Wall -Werror -Wparentheses -Wframe-larger-than=16384] --- FAIL: TestGenerate/netbsd/amd64/1 (0.19s) csource_test.go:121: --- FAIL: TestGenerate/netbsd/amd64/7 (0.21s) csource_test.go:121: --- FAIL: TestGenerate/netbsd/amd64/11 (0.21s) csource_test.go:121: --- FAIL: TestGenerate/netbsd/amd64/9 (0.20s) csource_test.go:123: opts: {Threaded:true Collide:false Repeat:true RepeatTimes:0 Procs:0 Sandbox:none Fault:false FaultCall:0 FaultNth:0 Leak:false NetInjection:true NetDevices:false NetReset:false Cgroups:false BinfmtMisc:false CloseFDs:false KCSAN:false DevlinkPCI:false USB:false UseTmpDir:true HandleSegv:false Repro:false Trace:false} program: r0 = _lwp_self() _lwp_create(&(0x7f0000000100)={0x400f0020, &(0x7f0000000080)={0x0, &(0x7f0000000000)={0x400f0023, 0x0, {[0x7, 0x7, 0x101, 0x1f]}, {0x1, 0x400, 0x6}, {0x1, 0x80000000, '\x00'}}, {[0x3, 0x5, 0x1, 0x35f7192e]}, {0xd, 0x100000000, 0x5}, {0x6, 0x10001, '\x00'}}, {[0xf1b, 0x401, 0x69, 0x100]}, {0x1, 0x0, 0x16ac7582ecd3534}, {0x1, 0x400, '-\x00'}}, 0x60, &(0x7f0000000180)=0x0) r2 = _lwp_self() _lwp_create(&(0x7f00000001c0)={0x2, 0x0, {[0x9, 0x3ae, 0x3, 0xc699]}, {0x81, 0x2, 0x1}, {0x9, 0x7, '\x00'}}, 0x40, &(0x7f0000000240)=0x0) r4 = _lwp_self() _lwp_unpark_all(&(0x7f0000000280)=[r0, r1, r2, r3, r4], 0x5, &(0x7f00000002c0)=0x4) _lwp_create(&(0x7f0000000380)={0x8, &(0x7f0000000300)={0x400f002c, 0x0, {[0x80000000, 0xa706, 0x0, 0x8]}, {0x1f, 0x6, 0x1}, {0x101, 0x5, '\x00'}}, {[0x2, 0x9, 0x10000, 0x1]}, {0x2, 0xffffffff, 0x5}, {0x20, 0xfff, '\x00'}}, 0x40, &(0x7f0000000400)=0x0) _lwp_kill(r5, 0x8) compat_50__lwp_park(&(0x7f0000000440)={0x101, 0x8}, 0x0, 0x0, &(0x7f0000000480)=0x1) shmctl$SHM_LOCK(0x0, 0x3) syz_emit_ethernet(0x54, &(0x7f0000000000)="afe8d2383c3ef2ff5f6e8c9a4d35c602198766ff84e3acfd84f778353f61842e8a198d7e500cce4b813690338589cca07acde0e9834fd09b00f3146ba0ddb9ac17ae011fb861af258325476c9621388eca4b11c0") syz_execute_func(&(0x7f0000000080)="3642833dd70b586a463e640febb19b000000c4c3f97c6d4ca6f344a6c443857fc618fdc403116f26fa447ba9642e470f12fc64f20f116200") syz_extract_tcp_res(&(0x7f00000000c0), 0x3, 0xc5d) r6 = syz_usb_connect(0xfffffffffffffeff, 0x78, &(0x7f0000000100)="935e057aa8ddaece20394d86c45c4a50f7278a8791b0cdbcf6e832b9052f3ab99063705506c66eb3374aad5e1fded7515c8cb7fe2c216994ffb89e915496a207404168e86ba82f7c0d4964e7e756ac689919cf9f4bf87d3e3b5174605c8f256d28204e428bf295ff312e2e27a1c2afb893dbe7867060c650", &(0x7f0000000180)="2b06574b32d8eb1d7479431c2ab53fc2d24cbf1c4368a824c8cd8217f44d51b11889f929d65e7900cccb5166453ffe7fa1f328df9695a4457b58f13191e90ca794186e763217e923bdc4f20434eba96616cc3b27ea87d54a6c434d77b737d18d0d8d537dce330d2759be67e22e1dc617f517147faeb44e343d630b33d1d31606b2a3971efcd03e4d87b5e600323723abfef81aa71bea16bdf9561aca75ce3265de3ac8a1590a61ede73a7b6943ddf3e3fcf19e478f55859db78d6baa46b7a7e2159f6f4f20ccb18d5c41fb430249") syz_usb_disconnect(r6) csource_test.go:124: failed to build program: // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static unsigned long long procid; static void kill_and_wait(int pid, int* status) { kill(pid, SIGKILL); while (waitpid(-1, status, 0) != pid) { } } static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void use_temporary_dir(void) { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } static void remove_dir(const char* dir) { DIR* dp; struct dirent* ep; dp = opendir(dir); if (dp == NULL) exit(1); while ((ep = readdir(dp))) { if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue; char filename[FILENAME_MAX]; snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } if (unlink(filename)) exit(1); } closedir(dp); if (rmdir(dir)) exit(1); } static void thread_start(void* (*fn)(void*), void* arg) { pthread_t th; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 128 << 10); int i; for (i = 0; i < 100; i++) { if (pthread_create(&th, &attr, fn, arg) == 0) { pthread_attr_destroy(&attr); return; } if (errno == EAGAIN) { usleep(50); continue; } break; } exit(1); } typedef struct { pthread_mutex_t mu; pthread_cond_t cv; int state; } event_t; static void event_init(event_t* ev) { if (pthread_mutex_init(&ev->mu, 0)) exit(1); if (pthread_cond_init(&ev->cv, 0)) exit(1); ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { pthread_mutex_lock(&ev->mu); if (ev->state) exit(1); ev->state = 1; pthread_mutex_unlock(&ev->mu); pthread_cond_broadcast(&ev->cv); } static void event_wait(event_t* ev) { pthread_mutex_lock(&ev->mu); while (!ev->state) pthread_cond_wait(&ev->cv, &ev->mu); pthread_mutex_unlock(&ev->mu); } static int event_isset(event_t* ev) { pthread_mutex_lock(&ev->mu); int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; pthread_mutex_lock(&ev->mu); for (;;) { if (ev->state) break; uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; pthread_cond_timedwait(&ev->cv, &ev->mu, &ts); now = current_time_ms(); if (now - start > timeout) break; } int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } /* -------------------------------------------------------------------------- */ /* * Redefinitions to match the linux types used in common_usb.h. */ struct usb_endpoint_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; uint16_t wMaxPacketSize; uint8_t bInterval; uint8_t bRefresh; uint8_t bSynchAddress; } __attribute__((packed)); struct usb_device_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint16_t idVendor; uint16_t idProduct; uint16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; } __attribute__((packed)); struct usb_config_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; } __attribute__((packed)); struct usb_interface_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bNumEndpoints; uint8_t bInterfaceClass; uint8_t bInterfaceSubClass; uint8_t bInterfaceProtocol; uint8_t iInterface; } __attribute__((packed)); struct usb_ctrlrequest { uint8_t bRequestType; uint8_t bRequest; uint16_t wValue; uint16_t wIndex; uint16_t wLength; } __attribute__((packed)); struct usb_qualifier_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint8_t bNumConfigurations; uint8_t bRESERVED; } __attribute__((packed)); #define USB_TYPE_MASK (0x03 << 5) #define USB_TYPE_STANDARD (0x00 << 5) #define USB_TYPE_CLASS (0x01 << 5) #define USB_TYPE_VENDOR (0x02 << 5) #define USB_TYPE_RESERVED (0x03 << 5) #define USB_DT_DEVICE 0x01 #define USB_DT_CONFIG 0x02 #define USB_DT_STRING 0x03 #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_DEVICE_QUALIFIER 0x06 #define USB_DT_OTHER_SPEED_CONFIG 0x07 #define USB_DT_INTERFACE_POWER 0x08 #define USB_DT_OTG 0x09 #define USB_DT_DEBUG 0x0a #define USB_DT_INTERFACE_ASSOCIATION 0x0b #define USB_DT_SECURITY 0x0c #define USB_DT_KEY 0x0d #define USB_DT_ENCRYPTION_TYPE 0x0e #define USB_DT_BOS 0x0f #define USB_DT_DEVICE_CAPABILITY 0x10 #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 #define USB_DT_WIRE_ADAPTER 0x21 #define USB_DT_RPIPE 0x22 #define USB_DT_CS_RADIO_CONTROL 0x23 #define USB_DT_PIPE_USAGE 0x24 #define USB_DT_SS_ENDPOINT_COMP 0x30 #define USB_DT_SSP_ISOC_ENDPOINT_COMP 0x31 #define USB_REQ_GET_STATUS 0x00 #define USB_REQ_CLEAR_FEATURE 0x01 #define USB_REQ_SET_FEATURE 0x03 #define USB_REQ_SET_ADDRESS 0x05 #define USB_REQ_GET_DESCRIPTOR 0x06 #define USB_REQ_SET_DESCRIPTOR 0x07 #define USB_REQ_GET_CONFIGURATION 0x08 #define USB_REQ_SET_CONFIGURATION 0x09 #define USB_REQ_GET_INTERFACE 0x0A #define USB_REQ_SET_INTERFACE 0x0B #define USB_REQ_SYNCH_FRAME 0x0C #define USB_REQ_SET_SEL 0x30 #define USB_REQ_SET_ISOCH_DELAY 0x31 #define USB_REQ_SET_ENCRYPTION 0x0D #define USB_REQ_GET_ENCRYPTION 0x0E #define USB_REQ_RPIPE_ABORT 0x0E #define USB_REQ_SET_HANDSHAKE 0x0F #define USB_REQ_RPIPE_RESET 0x0F #define USB_REQ_GET_HANDSHAKE 0x10 #define USB_REQ_SET_CONNECTION 0x11 #define USB_REQ_SET_SECURITY_DATA 0x12 #define USB_REQ_GET_SECURITY_DATA 0x13 #define USB_REQ_SET_WUSB_DATA 0x14 #define USB_REQ_LOOPBACK_DATA_WRITE 0x15 #define USB_REQ_LOOPBACK_DATA_READ 0x16 #define USB_REQ_SET_INTERFACE_DS 0x17 #define USB_REQ_GET_PARTNER_PDO 20 #define USB_REQ_GET_BATTERY_STATUS 21 #define USB_REQ_SET_PDO 22 #define USB_REQ_GET_VDM 23 #define USB_REQ_SEND_VDM 24 #define USB_MAX_IFACE_NUM 4 #define USB_MAX_EP_NUM 32 #define USB_MAX_FDS 6 struct usb_endpoint_index { struct usb_endpoint_descriptor desc; int handle; }; struct usb_iface_index { struct usb_interface_descriptor* iface; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bInterfaceClass; struct usb_endpoint_index eps[USB_MAX_EP_NUM]; int eps_num; }; struct usb_device_index { struct usb_device_descriptor* dev; struct usb_config_descriptor* config; uint8_t bDeviceClass; uint8_t bMaxPower; int config_length; struct usb_iface_index ifaces[USB_MAX_IFACE_NUM]; int ifaces_num; int iface_cur; }; struct usb_info { int fd; struct usb_device_index index; }; static struct usb_info usb_devices[USB_MAX_FDS]; static int usb_devices_num; static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index) { if (length < sizeof(*index->dev) + sizeof(*index->config)) return false; memset(index, 0, sizeof(*index)); index->dev = (struct usb_device_descriptor*)buffer; index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev)); index->bDeviceClass = index->dev->bDeviceClass; index->bMaxPower = index->config->bMaxPower; index->config_length = length - sizeof(*index->dev); index->iface_cur = -1; size_t offset = 0; while (true) { if (offset + 1 >= length) break; uint8_t desc_length = buffer[offset]; uint8_t desc_type = buffer[offset + 1]; if (desc_length <= 2) break; if (offset + desc_length > length) break; if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) { struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset); index->ifaces[index->ifaces_num].iface = iface; index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber; index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting; index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass; index->ifaces_num++; } if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) { struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1]; if (iface->eps_num < USB_MAX_EP_NUM) { memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc)); iface->eps_num++; } } offset += desc_length; } return true; } static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len) { int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED); if (i >= USB_MAX_FDS) return NULL; int rv = 0; rv = parse_usb_descriptor(dev, dev_len, &usb_devices[i].index); if (!rv) return NULL; __atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE); return &usb_devices[i].index; } static struct usb_device_index* lookup_usb_index(int fd) { int i; for (i = 0; i < USB_MAX_FDS; i++) { if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd) { return &usb_devices[i].index; } } return NULL; } struct vusb_connect_string_descriptor { uint32_t len; char* str; } __attribute__((packed)); struct vusb_connect_descriptors { uint32_t qual_len; char* qual; uint32_t bos_len; char* bos; uint32_t strs_len; struct vusb_connect_string_descriptor strs[0]; } __attribute__((packed)); static const char default_string[] = { 8, USB_DT_STRING, 's', 0, 'y', 0, 'z', 0 }; static const char default_lang_id[] = { 4, USB_DT_STRING, 0x09, 0x04 }; static bool lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, char** response_data, uint32_t* response_length) { struct usb_device_index* index = lookup_usb_index(fd); uint8_t str_idx; if (!index) return false; switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_GET_DESCRIPTOR: switch (ctrl->wValue >> 8) { case USB_DT_DEVICE: *response_data = (char*)index->dev; *response_length = sizeof(*index->dev); return true; case USB_DT_CONFIG: *response_data = (char*)index->config; *response_length = index->config_length; return true; case USB_DT_STRING: str_idx = (uint8_t)ctrl->wValue; if (descs && str_idx < descs->strs_len) { *response_data = descs->strs[str_idx].str; *response_length = descs->strs[str_idx].len; return true; } if (str_idx == 0) { *response_data = (char*)&default_lang_id[0]; *response_length = default_lang_id[0]; return true; } *response_data = (char*)&default_string[0]; *response_length = default_string[0]; return true; case USB_DT_BOS: *response_data = descs->bos; *response_length = descs->bos_len; return true; case USB_DT_DEVICE_QUALIFIER: if (!descs->qual) { struct usb_qualifier_descriptor* qual = (struct usb_qualifier_descriptor*)response_data; qual->bLength = sizeof(*qual); qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; qual->bcdUSB = index->dev->bcdUSB; qual->bDeviceClass = index->dev->bDeviceClass; qual->bDeviceSubClass = index->dev->bDeviceSubClass; qual->bDeviceProtocol = index->dev->bDeviceProtocol; qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0; qual->bNumConfigurations = index->dev->bNumConfigurations; qual->bRESERVED = 0; *response_length = sizeof(*qual); return true; } *response_data = descs->qual; *response_length = descs->qual_len; return true; default: break; } break; default: break; } break; default: break; } return false; } typedef bool (*lookup_connect_out_response_t)(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done); static bool lookup_connect_response_out_generic(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done) { switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_SET_CONFIGURATION: *done = true; return true; default: break; } break; } return false; } /* -------------------------------------------------------------------------- */ static int vhci_open(void) { return open("/dev/vhci", O_RDWR); } static int vhci_setport(int fd, u_int port) { struct vhci_ioc_set_port args; args.port = port; return ioctl(fd, VHCI_IOC_SET_PORT, &args); } static int vhci_usb_attach(int fd) { return ioctl(fd, VHCI_IOC_USB_ATTACH, NULL); } static int vhci_usb_recv(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = read(fd, ptr, size); if (done < 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } static int vhci_usb_send(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = write(fd, ptr, size); if (done <= 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } /* -------------------------------------------------------------------------- */ static volatile long syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev, const struct vusb_connect_descriptors* descs, lookup_connect_out_response_t lookup_connect_response_out) { struct usb_device_index* index; int portnum, fd, rv; bool done; portnum = procid + 1; if (!dev) { return -1; } if (portnum != 1) { /* For now, we support only one proc. */ return -1; } fd = vhci_open(); if (fd < 0) { return -1; } index = add_usb_index(fd, dev, dev_len); if (!index) { goto err; } rv = vhci_setport(fd, portnum); if (rv != 0) { goto err; } rv = vhci_usb_attach(fd); if (rv != 0) { goto err; } done = false; while (!done) { vhci_request_t req; rv = vhci_usb_recv(fd, &req, sizeof(req)); if (rv != 0) { goto err; } if (req.type != VHCI_REQ_CTRL) { goto err; } char* response_data = NULL; uint32_t response_length = 0; char data[4096]; if (req.u.ctrl.bmRequestType & UE_DIR_IN) { bool response_found = false; response_found = lookup_connect_response_in(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &response_data, &response_length); if (!response_found) { goto err; } } else { if (!lookup_connect_response_out(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &done)) { goto err; } response_data = NULL; response_length = UGETW(req.u.ctrl.wLength); } if ((req.u.ctrl.bmRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD && req.u.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) { /* TODO: possibly revisit */ } if (response_length > sizeof(data)) response_length = 0; if ((uint32_t)UGETW(req.u.ctrl.wLength) < response_length) response_length = UGETW(req.u.ctrl.wLength); if (response_data) memcpy(data, response_data, response_length); else memset(data, 0, response_length); if (req.u.ctrl.bmRequestType & UE_DIR_IN) { if (response_length > 0) { vhci_response_t res; res.size = response_length; rv = vhci_usb_send(fd, &res, sizeof(res)); if (rv == 0) rv = vhci_usb_send(fd, data, response_length); } } else { rv = vhci_usb_recv(fd, data, response_length); } if (rv < 0) { goto err; } } sleep_ms(200); return fd; err: close(fd); return -1; } static volatile long syz_usb_connect(volatile long a0, volatile long a1, volatile long a2, volatile long a3) { uint64_t speed = a0; uint64_t dev_len = a1; const char* dev = (const char*)a2; const struct vusb_connect_descriptors* descs = (const struct vusb_connect_descriptors*)a3; return syz_usb_connect_impl(speed, dev_len, dev, descs, &lookup_connect_response_out_generic); } static volatile long syz_usb_disconnect(volatile long a0) { int fd = a0; int rv = close(fd); sleep_ms(200); return rv; } static int tunfd = -1; #define MAX_TUN 64 #define TUN_IFACE "tap%d" #define TUN_DEVICE "/dev/tap%d" #define LOCAL_MAC "aa:aa:aa:aa:aa:aa" #define REMOTE_MAC "aa:aa:aa:aa:aa:bb" #define LOCAL_IPV4 "172.20.%d.170" #define REMOTE_IPV4 "172.20.%d.187" #define LOCAL_IPV6 "fe80::%02hxaa" #define REMOTE_IPV6 "fe80::%02hxbb" static void vsnprintf_check(char* str, size_t size, const char* format, va_list args) { int rv; rv = vsnprintf(str, size, format, args); if (rv < 0) exit(1); if ((size_t)rv >= size) exit(1); } 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) static void execute_command(bool panic, const char* format, ...) { va_list args; char command[PATH_PREFIX_LEN + COMMAND_MAX_LEN]; int rv; va_start(args, format); memcpy(command, PATH_PREFIX, PATH_PREFIX_LEN); vsnprintf_check(command + PATH_PREFIX_LEN, COMMAND_MAX_LEN, format, args); va_end(args); rv = system(command); if (rv) { if (panic) exit(1); } } static void initialize_tun(int tun_id) { if (tun_id < 0 || tun_id >= MAX_TUN) { exit(1); } char tun_device[sizeof(TUN_DEVICE)]; snprintf_check(tun_device, sizeof(tun_device), TUN_DEVICE, tun_id); char tun_iface[sizeof(TUN_IFACE)]; snprintf_check(tun_iface, sizeof(tun_iface), TUN_IFACE, tun_id); execute_command(0, "ifconfig %s destroy", tun_iface); execute_command(0, "ifconfig %s create", tun_iface); tunfd = open(tun_device, O_RDWR | O_NONBLOCK); if (tunfd == -1) { printf("tun: can't open %s: errno=%d\n", tun_device, errno); return; } const int kTunFd = 240; if (dup2(tunfd, kTunFd) < 0) exit(1); close(tunfd); tunfd = kTunFd; char local_mac[sizeof(LOCAL_MAC)]; snprintf_check(local_mac, sizeof(local_mac), LOCAL_MAC); execute_command(1, "ifconfig %s link %s", tun_iface, local_mac); char local_ipv4[sizeof(LOCAL_IPV4)]; snprintf_check(local_ipv4, sizeof(local_ipv4), LOCAL_IPV4, tun_id); execute_command(1, "ifconfig %s inet %s netmask 255.255.255.0", tun_iface, local_ipv4); char remote_mac[sizeof(REMOTE_MAC)]; char remote_ipv4[sizeof(REMOTE_IPV4)]; snprintf_check(remote_mac, sizeof(remote_mac), REMOTE_MAC); snprintf_check(remote_ipv4, sizeof(remote_ipv4), REMOTE_IPV4, tun_id); execute_command(0, "arp -s %s %s", remote_ipv4, remote_mac); char local_ipv6[sizeof(LOCAL_IPV6)]; snprintf_check(local_ipv6, sizeof(local_ipv6), LOCAL_IPV6, tun_id); execute_command(1, "ifconfig %s inet6 %s", tun_iface, local_ipv6); char remote_ipv6[sizeof(REMOTE_IPV6)]; snprintf_check(remote_ipv6, sizeof(remote_ipv6), REMOTE_IPV6, tun_id); execute_command(0, "ndp -s %s%%%s %s", remote_ipv6, tun_iface, remote_mac); } static long syz_emit_ethernet(volatile long a0, volatile long a1) { if (tunfd < 0) return (uintptr_t)-1; size_t length = a0; const char* data = (char*)a1; return write(tunfd, data, length); } static int read_tun(char* data, int size) { if (tunfd < 0) return -1; int rv = read(tunfd, data, size); if (rv < 0) { if (errno == EAGAIN) return -1; exit(1); } return rv; } struct tcp_resources { uint32_t seq; uint32_t ack; }; static long syz_extract_tcp_res(volatile long a0, volatile long a1, volatile long a2) { if (tunfd < 0) return (uintptr_t)-1; char data[1000]; int rv = read_tun(&data[0], sizeof(data)); if (rv == -1) return (uintptr_t)-1; size_t length = rv; struct tcphdr* tcphdr; if (length < sizeof(struct ether_header)) return (uintptr_t)-1; struct ether_header* ethhdr = (struct ether_header*)&data[0]; if (ethhdr->ether_type == htons(ETHERTYPE_IP)) { if (length < sizeof(struct ether_header) + sizeof(struct ip)) return (uintptr_t)-1; struct ip* iphdr = (struct ip*)&data[sizeof(struct ether_header)]; if (iphdr->ip_p != IPPROTO_TCP) return (uintptr_t)-1; if (length < sizeof(struct ether_header) + iphdr->ip_hl * 4 + sizeof(struct tcphdr)) return (uintptr_t)-1; tcphdr = (struct tcphdr*)&data[sizeof(struct ether_header) + iphdr->ip_hl * 4]; } else { if (length < sizeof(struct ether_header) + sizeof(struct ip6_hdr)) return (uintptr_t)-1; struct ip6_hdr* ipv6hdr = (struct ip6_hdr*)&data[sizeof(struct ether_header)]; if (ipv6hdr->ip6_nxt != IPPROTO_TCP) return (uintptr_t)-1; if (length < sizeof(struct ether_header) + sizeof(struct ip6_hdr) + sizeof(struct tcphdr)) return (uintptr_t)-1; tcphdr = (struct tcphdr*)&data[sizeof(struct ether_header) + sizeof(struct ip6_hdr)]; } struct tcp_resources* res = (struct tcp_resources*)a0; res->seq = htonl((ntohl(tcphdr->th_seq) + (uint32_t)a1)); res->ack = htonl((ntohl(tcphdr->th_ack) + (uint32_t)a2)); return 0; } static void sandbox_common() { if (setsid() == -1) exit(1); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 8 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 0; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); } static void loop(); static int do_sandbox_none(void) { sandbox_common(); initialize_tun(procid); loop(); return 0; } static long syz_execute_func(volatile long text) { volatile long p[8] = {0}; (void)p; asm volatile("" ::"r"(0l), "r"(1l), "r"(2l), "r"(3l), "r"(4l), "r"(5l), "r"(6l), "r"(7l), "r"(8l), "r"(9l), "r"(10l), "r"(11l), "r"(12l), "r"(13l)); ((void (*)(void))(text))(); return 0; } struct thread_t { int created, call; event_t ready, done; }; static struct thread_t threads[16]; static void execute_call(int call); static int running; static void* thr(void* arg) { struct thread_t* th = (struct thread_t*)arg; for (;;) { event_wait(&th->ready); event_reset(&th->ready); execute_call(th->call); __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED); event_set(&th->done); } return 0; } static void execute_one(void) { int i, call, thread; for (call = 0; call < 15; call++) { for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); thread++) { struct thread_t* th = &threads[thread]; if (!th->created) { th->created = 1; event_init(&th->ready); event_init(&th->done); event_set(&th->done); thread_start(thr, th); } if (!event_isset(&th->done)) continue; event_reset(&th->done); th->call = call; __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED); event_set(&th->ready); event_timedwait(&th->done, 45 + (call == 13 ? 3000 : 0) + (call == 14 ? 300 : 0)); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter; for (iter = 0;; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5 * 1000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } #ifndef SYS__lwp_create #define SYS__lwp_create 309 #endif #ifndef SYS__lwp_kill #define SYS__lwp_kill 318 #endif #ifndef SYS__lwp_self #define SYS__lwp_self 311 #endif #ifndef SYS__lwp_unpark_all #define SYS__lwp_unpark_all 322 #endif #ifndef SYS_compat_50__lwp_park #define SYS_compat_50__lwp_park 320 #endif #ifndef SYS_mmap #define SYS_mmap 197 #endif #ifndef SYS_shmctl #define SYS_shmctl 443 #endif uint64_t r[7] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: res = syscall(SYS__lwp_self); if (res != -1) r[0] = res; break; case 1: *(uint32_t*)0x20000100 = 0x400f0020; *(uint64_t*)0x20000108 = 0x20000080; *(uint32_t*)0x20000080 = 0; *(uint64_t*)0x20000088 = 0x20000000; *(uint32_t*)0x20000000 = 0x400f0023; *(uint64_t*)0x20000008 = 0; *(uint32_t*)0x20000010 = 7; *(uint32_t*)0x20000014 = 7; *(uint32_t*)0x20000018 = 0x101; *(uint32_t*)0x2000001c = 0x1f; *(uint64_t*)0x20000020 = 1; *(uint64_t*)0x20000028 = 0x400; *(uint32_t*)0x20000030 = 6; *(uint64_t*)0x20000038 = 1; *(uint64_t*)0x20000040 = 0x80000000; memcpy((void*)0x20000048, "\000", 1); *(uint32_t*)0x20000090 = 3; *(uint32_t*)0x20000094 = 5; *(uint32_t*)0x20000098 = 1; *(uint32_t*)0x2000009c = 0x35f7192e; *(uint64_t*)0x200000a0 = 0xd; *(uint64_t*)0x200000a8 = 0x100000000; *(uint32_t*)0x200000b0 = 5; *(uint64_t*)0x200000b8 = 6; *(uint64_t*)0x200000c0 = 0x10001; memcpy((void*)0x200000c8, "\000", 1); *(uint32_t*)0x20000110 = 0xf1b; *(uint32_t*)0x20000114 = 0x401; *(uint32_t*)0x20000118 = 0x69; *(uint32_t*)0x2000011c = 0x100; *(uint64_t*)0x20000120 = 1; *(uint64_t*)0x20000128 = 0; *(uint32_t*)0x20000130 = 0x2ecd3534; *(uint64_t*)0x20000138 = 1; *(uint64_t*)0x20000140 = 0x400; memcpy((void*)0x20000148, "-\000", 2); res = syscall(SYS__lwp_create, 0x20000100ul, 0x60ul, 0x20000180ul); if (res != -1) r[1] = *(uint32_t*)0x20000180; break; case 2: res = syscall(SYS__lwp_self); if (res != -1) r[2] = res; break; case 3: *(uint32_t*)0x200001c0 = 2; *(uint64_t*)0x200001c8 = 0; *(uint32_t*)0x200001d0 = 9; *(uint32_t*)0x200001d4 = 0x3ae; *(uint32_t*)0x200001d8 = 3; *(uint32_t*)0x200001dc = 0xc699; *(uint64_t*)0x200001e0 = 0x81; *(uint64_t*)0x200001e8 = 2; *(uint32_t*)0x200001f0 = 1; *(uint64_t*)0x200001f8 = 9; *(uint64_t*)0x20000200 = 7; memcpy((void*)0x20000208, "\000", 1); res = syscall(SYS__lwp_create, 0x200001c0ul, 0x40ul, 0x20000240ul); if (res != -1) r[3] = *(uint32_t*)0x20000240; break; case 4: res = syscall(SYS__lwp_self); if (res != -1) r[4] = res; break; case 5: *(uint32_t*)0x20000280 = r[0]; *(uint32_t*)0x20000284 = r[1]; *(uint32_t*)0x20000288 = r[2]; *(uint32_t*)0x2000028c = r[3]; *(uint32_t*)0x20000290 = r[4]; *(uint64_t*)0x200002c0 = 4; syscall(SYS__lwp_unpark_all, 0x20000280ul, 5ul, 0x200002c0ul); break; case 6: *(uint32_t*)0x20000380 = 8; *(uint64_t*)0x20000388 = 0x20000300; *(uint32_t*)0x20000300 = 0x400f002c; *(uint64_t*)0x20000308 = 0; *(uint32_t*)0x20000310 = 0x80000000; *(uint32_t*)0x20000314 = 0xa706; *(uint32_t*)0x20000318 = 0; *(uint32_t*)0x2000031c = 8; *(uint64_t*)0x20000320 = 0x1f; *(uint64_t*)0x20000328 = 6; *(uint32_t*)0x20000330 = 1; *(uint64_t*)0x20000338 = 0x101; *(uint64_t*)0x20000340 = 5; memcpy((void*)0x20000348, "\000", 1); *(uint32_t*)0x20000390 = 2; *(uint32_t*)0x20000394 = 9; *(uint32_t*)0x20000398 = 0x10000; *(uint32_t*)0x2000039c = 1; *(uint64_t*)0x200003a0 = 2; *(uint64_t*)0x200003a8 = 0xffffffff; *(uint32_t*)0x200003b0 = 5; *(uint64_t*)0x200003b8 = 0x20; *(uint64_t*)0x200003c0 = 0xfff; memcpy((void*)0x200003c8, "\000", 1); res = syscall(SYS__lwp_create, 0x20000380ul, 0x40ul, 0x20000400ul); if (res != -1) r[5] = *(uint32_t*)0x20000400; break; case 7: syscall(SYS__lwp_kill, r[5], 8); break; case 8: *(uint32_t*)0x20000440 = 0x101; *(uint64_t*)0x20000448 = 8; *(uint64_t*)0x20000480 = 1; syscall(SYS_compat_50__lwp_park, 0x20000440ul, 0, 0ul, 0x20000480ul); break; case 9: syscall(SYS_shmctl, 0, 3ul, 0); break; case 10: memcpy((void*)0x20000000, "\xaf\xe8\xd2\x38\x3c\x3e\xf2\xff\x5f\x6e\x8c\x9a\x4d\x35\xc6\x02\x19\x87\x66\xff\x84\xe3\xac\xfd\x84\xf7\x78\x35\x3f\x61\x84\x2e\x8a\x19\x8d\x7e\x50\x0c\xce\x4b\x81\x36\x90\x33\x85\x89\xcc\xa0\x7a\xcd\xe0\xe9\x83\x4f\xd0\x9b\x00\xf3\x14\x6b\xa0\xdd\xb9\xac\x17\xae\x01\x1f\xb8\x61\xaf\x25\x83\x25\x47\x6c\x96\x21\x38\x8e\xca\x4b\x11\xc0", 84); syz_emit_ethernet(0x54, 0x20000000); break; case 11: memcpy((void*)0x20000080, "\x36\x42\x83\x3d\xd7\x0b\x58\x6a\x46\x3e\x64\x0f\xeb\xb1\x9b\x00\x00\x00\xc4\xc3\xf9\x7c\x6d\x4c\xa6\xf3\x44\xa6\xc4\x43\x85\x7f\xc6\x18\xfd\xc4\x03\x11\x6f\x26\xfa\x44\x7b\xa9\x64\x2e\x47\x0f\x12\xfc\x64\xf2\x0f\x11\x62\x00", 56); syz_execute_func(0x20000080); break; case 12: syz_extract_tcp_res(0x200000c0, 3, 0xc5d); break; case 13: memcpy((void*)0x20000100, "\x93\x5e\x05\x7a\xa8\xdd\xae\xce\x20\x39\x4d\x86\xc4\x5c\x4a\x50\xf7\x27\x8a\x87\x91\xb0\xcd\xbc\xf6\xe8\x32\xb9\x05\x2f\x3a\xb9\x90\x63\x70\x55\x06\xc6\x6e\xb3\x37\x4a\xad\x5e\x1f\xde\xd7\x51\x5c\x8c\xb7\xfe\x2c\x21\x69\x94\xff\xb8\x9e\x91\x54\x96\xa2\x07\x40\x41\x68\xe8\x6b\xa8\x2f\x7c\x0d\x49\x64\xe7\xe7\x56\xac\x68\x99\x19\xcf\x9f\x4b\xf8\x7d\x3e\x3b\x51\x74\x60\x5c\x8f\x25\x6d\x28\x20\x4e\x42\x8b\xf2\x95\xff\x31\x2e\x2e\x27\xa1\xc2\xaf\xb8\x93\xdb\xe7\x86\x70\x60\xc6\x50", 120); memcpy((void*)0x20000180, "\x2b\x06\x57\x4b\x32\xd8\xeb\x1d\x74\x79\x43\x1c\x2a\xb5\x3f\xc2\xd2\x4c\xbf\x1c\x43\x68\xa8\x24\xc8\xcd\x82\x17\xf4\x4d\x51\xb1\x18\x89\xf9\x29\xd6\x5e\x79\x00\xcc\xcb\x51\x66\x45\x3f\xfe\x7f\xa1\xf3\x28\xdf\x96\x95\xa4\x45\x7b\x58\xf1\x31\x91\xe9\x0c\xa7\x94\x18\x6e\x76\x32\x17\xe9\x23\xbd\xc4\xf2\x04\x34\xeb\xa9\x66\x16\xcc\x3b\x27\xea\x87\xd5\x4a\x6c\x43\x4d\x77\xb7\x37\xd1\x8d\x0d\x8d\x53\x7d\xce\x33\x0d\x27\x59\xbe\x67\xe2\x2e\x1d\xc6\x17\xf5\x17\x14\x7f\xae\xb4\x4e\x34\x3d\x63\x0b\x33\xd1\xd3\x16\x06\xb2\xa3\x97\x1e\xfc\xd0\x3e\x4d\x87\xb5\xe6\x00\x32\x37\x23\xab\xfe\xf8\x1a\xa7\x1b\xea\x16\xbd\xf9\x56\x1a\xca\x75\xce\x32\x65\xde\x3a\xc8\xa1\x59\x0a\x61\xed\xe7\x3a\x7b\x69\x43\xdd\xf3\xe3\xfc\xf1\x9e\x47\x8f\x55\x85\x9d\xb7\x8d\x6b\xaa\x46\xb7\xa7\xe2\x15\x9f\x6f\x4f\x20\xcc\xb1\x8d\x5c\x41\xfb\x43\x02\x49", 206); res = syz_usb_connect(0xfffffffffffffeff, 0x78, 0x20000100, 0x20000180); if (res != -1) r[6] = res; break; case 14: syz_usb_disconnect(r[6]); break; } } int main(void) { syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul, 0ul); use_temporary_dir(); do_sandbox_none(); return 0; } : In function 'syz_usb_connect_impl': :648:63: error: unknown type name 'usb_ctrlrequest' :653:55: error: unknown type name 'usb_ctrlrequest' compiler invocation: /syzkaller/netbsd/src/../tools/bin/x86_64--netbsd-g++ [-o /tmp/syz-executor776363484 -DGOOS_netbsd=1 -DGOARCH_amd64=1 -DHOSTGOOS_linux=1 -x c - -m64 --sysroot /syzkaller/netbsd/src/../dest/ -O2 -pthread -Wall -Werror -Wparentheses -Wframe-larger-than=16384] --- FAIL: TestGenerate/netbsd/amd64/4 (0.26s) csource_test.go:123: opts: {Threaded:true Collide:false Repeat:true RepeatTimes:10 Procs:0 Sandbox:none Fault:false FaultCall:0 FaultNth:0 Leak:false NetInjection:false NetDevices:false NetReset:false Cgroups:false BinfmtMisc:false CloseFDs:false KCSAN:false DevlinkPCI:false USB:false UseTmpDir:true HandleSegv:false Repro:false Trace:false} program: r0 = _lwp_self() _lwp_create(&(0x7f0000000100)={0x400f0020, &(0x7f0000000080)={0x0, &(0x7f0000000000)={0x400f0023, 0x0, {[0x7, 0x7, 0x101, 0x1f]}, {0x1, 0x400, 0x6}, {0x1, 0x80000000, '\x00'}}, {[0x3, 0x5, 0x1, 0x35f7192e]}, {0xd, 0x100000000, 0x5}, {0x6, 0x10001, '\x00'}}, {[0xf1b, 0x401, 0x69, 0x100]}, {0x1, 0x0, 0x16ac7582ecd3534}, {0x1, 0x400, '-\x00'}}, 0x60, &(0x7f0000000180)=0x0) r2 = _lwp_self() _lwp_create(&(0x7f00000001c0)={0x2, 0x0, {[0x9, 0x3ae, 0x3, 0xc699]}, {0x81, 0x2, 0x1}, {0x9, 0x7, '\x00'}}, 0x40, &(0x7f0000000240)=0x0) r4 = _lwp_self() _lwp_unpark_all(&(0x7f0000000280)=[r0, r1, r2, r3, r4], 0x5, &(0x7f00000002c0)=0x4) _lwp_create(&(0x7f0000000380)={0x8, &(0x7f0000000300)={0x400f002c, 0x0, {[0x80000000, 0xa706, 0x0, 0x8]}, {0x1f, 0x6, 0x1}, {0x101, 0x5, '\x00'}}, {[0x2, 0x9, 0x10000, 0x1]}, {0x2, 0xffffffff, 0x5}, {0x20, 0xfff, '\x00'}}, 0x40, &(0x7f0000000400)=0x0) _lwp_kill(r5, 0x8) compat_50__lwp_park(&(0x7f0000000440)={0x101, 0x8}, 0x0, 0x0, &(0x7f0000000480)=0x1) shmctl$SHM_LOCK(0x0, 0x3) syz_emit_ethernet(0x54, &(0x7f0000000000)="afe8d2383c3ef2ff5f6e8c9a4d35c602198766ff84e3acfd84f778353f61842e8a198d7e500cce4b813690338589cca07acde0e9834fd09b00f3146ba0ddb9ac17ae011fb861af258325476c9621388eca4b11c0") syz_execute_func(&(0x7f0000000080)="3642833dd70b586a463e640febb19b000000c4c3f97c6d4ca6f344a6c443857fc618fdc403116f26fa447ba9642e470f12fc64f20f116200") syz_extract_tcp_res(&(0x7f00000000c0), 0x3, 0xc5d) r6 = syz_usb_connect(0xfffffffffffffeff, 0x78, &(0x7f0000000100)="935e057aa8ddaece20394d86c45c4a50f7278a8791b0cdbcf6e832b9052f3ab99063705506c66eb3374aad5e1fded7515c8cb7fe2c216994ffb89e915496a207404168e86ba82f7c0d4964e7e756ac689919cf9f4bf87d3e3b5174605c8f256d28204e428bf295ff312e2e27a1c2afb893dbe7867060c650", &(0x7f0000000180)="2b06574b32d8eb1d7479431c2ab53fc2d24cbf1c4368a824c8cd8217f44d51b11889f929d65e7900cccb5166453ffe7fa1f328df9695a4457b58f13191e90ca794186e763217e923bdc4f20434eba96616cc3b27ea87d54a6c434d77b737d18d0d8d537dce330d2759be67e22e1dc617f517147faeb44e343d630b33d1d31606b2a3971efcd03e4d87b5e600323723abfef81aa71bea16bdf9561aca75ce3265de3ac8a1590a61ede73a7b6943ddf3e3fcf19e478f55859db78d6baa46b7a7e2159f6f4f20ccb18d5c41fb430249") syz_usb_disconnect(r6) csource_test.go:124: failed to build program: // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static unsigned long long procid; static void kill_and_wait(int pid, int* status) { kill(pid, SIGKILL); while (waitpid(-1, status, 0) != pid) { } } static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void use_temporary_dir(void) { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } static void remove_dir(const char* dir) { DIR* dp; struct dirent* ep; dp = opendir(dir); if (dp == NULL) exit(1); while ((ep = readdir(dp))) { if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue; char filename[FILENAME_MAX]; snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } if (unlink(filename)) exit(1); } closedir(dp); if (rmdir(dir)) exit(1); } static void thread_start(void* (*fn)(void*), void* arg) { pthread_t th; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 128 << 10); int i; for (i = 0; i < 100; i++) { if (pthread_create(&th, &attr, fn, arg) == 0) { pthread_attr_destroy(&attr); return; } if (errno == EAGAIN) { usleep(50); continue; } break; } exit(1); } typedef struct { pthread_mutex_t mu; pthread_cond_t cv; int state; } event_t; static void event_init(event_t* ev) { if (pthread_mutex_init(&ev->mu, 0)) exit(1); if (pthread_cond_init(&ev->cv, 0)) exit(1); ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { pthread_mutex_lock(&ev->mu); if (ev->state) exit(1); ev->state = 1; pthread_mutex_unlock(&ev->mu); pthread_cond_broadcast(&ev->cv); } static void event_wait(event_t* ev) { pthread_mutex_lock(&ev->mu); while (!ev->state) pthread_cond_wait(&ev->cv, &ev->mu); pthread_mutex_unlock(&ev->mu); } static int event_isset(event_t* ev) { pthread_mutex_lock(&ev->mu); int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; pthread_mutex_lock(&ev->mu); for (;;) { if (ev->state) break; uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; pthread_cond_timedwait(&ev->cv, &ev->mu, &ts); now = current_time_ms(); if (now - start > timeout) break; } int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } /* -------------------------------------------------------------------------- */ /* * Redefinitions to match the linux types used in common_usb.h. */ struct usb_endpoint_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; uint16_t wMaxPacketSize; uint8_t bInterval; uint8_t bRefresh; uint8_t bSynchAddress; } __attribute__((packed)); struct usb_device_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint16_t idVendor; uint16_t idProduct; uint16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; } __attribute__((packed)); struct usb_config_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; } __attribute__((packed)); struct usb_interface_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bNumEndpoints; uint8_t bInterfaceClass; uint8_t bInterfaceSubClass; uint8_t bInterfaceProtocol; uint8_t iInterface; } __attribute__((packed)); struct usb_ctrlrequest { uint8_t bRequestType; uint8_t bRequest; uint16_t wValue; uint16_t wIndex; uint16_t wLength; } __attribute__((packed)); struct usb_qualifier_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint8_t bNumConfigurations; uint8_t bRESERVED; } __attribute__((packed)); #define USB_TYPE_MASK (0x03 << 5) #define USB_TYPE_STANDARD (0x00 << 5) #define USB_TYPE_CLASS (0x01 << 5) #define USB_TYPE_VENDOR (0x02 << 5) #define USB_TYPE_RESERVED (0x03 << 5) #define USB_DT_DEVICE 0x01 #define USB_DT_CONFIG 0x02 #define USB_DT_STRING 0x03 #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_DEVICE_QUALIFIER 0x06 #define USB_DT_OTHER_SPEED_CONFIG 0x07 #define USB_DT_INTERFACE_POWER 0x08 #define USB_DT_OTG 0x09 #define USB_DT_DEBUG 0x0a #define USB_DT_INTERFACE_ASSOCIATION 0x0b #define USB_DT_SECURITY 0x0c #define USB_DT_KEY 0x0d #define USB_DT_ENCRYPTION_TYPE 0x0e #define USB_DT_BOS 0x0f #define USB_DT_DEVICE_CAPABILITY 0x10 #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 #define USB_DT_WIRE_ADAPTER 0x21 #define USB_DT_RPIPE 0x22 #define USB_DT_CS_RADIO_CONTROL 0x23 #define USB_DT_PIPE_USAGE 0x24 #define USB_DT_SS_ENDPOINT_COMP 0x30 #define USB_DT_SSP_ISOC_ENDPOINT_COMP 0x31 #define USB_REQ_GET_STATUS 0x00 #define USB_REQ_CLEAR_FEATURE 0x01 #define USB_REQ_SET_FEATURE 0x03 #define USB_REQ_SET_ADDRESS 0x05 #define USB_REQ_GET_DESCRIPTOR 0x06 #define USB_REQ_SET_DESCRIPTOR 0x07 #define USB_REQ_GET_CONFIGURATION 0x08 #define USB_REQ_SET_CONFIGURATION 0x09 #define USB_REQ_GET_INTERFACE 0x0A #define USB_REQ_SET_INTERFACE 0x0B #define USB_REQ_SYNCH_FRAME 0x0C #define USB_REQ_SET_SEL 0x30 #define USB_REQ_SET_ISOCH_DELAY 0x31 #define USB_REQ_SET_ENCRYPTION 0x0D #define USB_REQ_GET_ENCRYPTION 0x0E #define USB_REQ_RPIPE_ABORT 0x0E #define USB_REQ_SET_HANDSHAKE 0x0F #define USB_REQ_RPIPE_RESET 0x0F #define USB_REQ_GET_HANDSHAKE 0x10 #define USB_REQ_SET_CONNECTION 0x11 #define USB_REQ_SET_SECURITY_DATA 0x12 #define USB_REQ_GET_SECURITY_DATA 0x13 #define USB_REQ_SET_WUSB_DATA 0x14 #define USB_REQ_LOOPBACK_DATA_WRITE 0x15 #define USB_REQ_LOOPBACK_DATA_READ 0x16 #define USB_REQ_SET_INTERFACE_DS 0x17 #define USB_REQ_GET_PARTNER_PDO 20 #define USB_REQ_GET_BATTERY_STATUS 21 #define USB_REQ_SET_PDO 22 #define USB_REQ_GET_VDM 23 #define USB_REQ_SEND_VDM 24 #define USB_MAX_IFACE_NUM 4 #define USB_MAX_EP_NUM 32 #define USB_MAX_FDS 6 struct usb_endpoint_index { struct usb_endpoint_descriptor desc; int handle; }; struct usb_iface_index { struct usb_interface_descriptor* iface; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bInterfaceClass; struct usb_endpoint_index eps[USB_MAX_EP_NUM]; int eps_num; }; struct usb_device_index { struct usb_device_descriptor* dev; struct usb_config_descriptor* config; uint8_t bDeviceClass; uint8_t bMaxPower; int config_length; struct usb_iface_index ifaces[USB_MAX_IFACE_NUM]; int ifaces_num; int iface_cur; }; struct usb_info { int fd; struct usb_device_index index; }; static struct usb_info usb_devices[USB_MAX_FDS]; static int usb_devices_num; static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index) { if (length < sizeof(*index->dev) + sizeof(*index->config)) return false; memset(index, 0, sizeof(*index)); index->dev = (struct usb_device_descriptor*)buffer; index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev)); index->bDeviceClass = index->dev->bDeviceClass; index->bMaxPower = index->config->bMaxPower; index->config_length = length - sizeof(*index->dev); index->iface_cur = -1; size_t offset = 0; while (true) { if (offset + 1 >= length) break; uint8_t desc_length = buffer[offset]; uint8_t desc_type = buffer[offset + 1]; if (desc_length <= 2) break; if (offset + desc_length > length) break; if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) { struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset); index->ifaces[index->ifaces_num].iface = iface; index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber; index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting; index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass; index->ifaces_num++; } if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) { struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1]; if (iface->eps_num < USB_MAX_EP_NUM) { memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc)); iface->eps_num++; } } offset += desc_length; } return true; } static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len) { int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED); if (i >= USB_MAX_FDS) return NULL; int rv = 0; rv = parse_usb_descriptor(dev, dev_len, &usb_devices[i].index); if (!rv) return NULL; __atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE); return &usb_devices[i].index; } static struct usb_device_index* lookup_usb_index(int fd) { int i; for (i = 0; i < USB_MAX_FDS; i++) { if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd) { return &usb_devices[i].index; } } return NULL; } struct vusb_connect_string_descriptor { uint32_t len; char* str; } __attribute__((packed)); struct vusb_connect_descriptors { uint32_t qual_len; char* qual; uint32_t bos_len; char* bos; uint32_t strs_len; struct vusb_connect_string_descriptor strs[0]; } __attribute__((packed)); static const char default_string[] = { 8, USB_DT_STRING, 's', 0, 'y', 0, 'z', 0 }; static const char default_lang_id[] = { 4, USB_DT_STRING, 0x09, 0x04 }; static bool lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, char** response_data, uint32_t* response_length) { struct usb_device_index* index = lookup_usb_index(fd); uint8_t str_idx; if (!index) return false; switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_GET_DESCRIPTOR: switch (ctrl->wValue >> 8) { case USB_DT_DEVICE: *response_data = (char*)index->dev; *response_length = sizeof(*index->dev); return true; case USB_DT_CONFIG: *response_data = (char*)index->config; *response_length = index->config_length; return true; case USB_DT_STRING: str_idx = (uint8_t)ctrl->wValue; if (descs && str_idx < descs->strs_len) { *response_data = descs->strs[str_idx].str; *response_length = descs->strs[str_idx].len; return true; } if (str_idx == 0) { *response_data = (char*)&default_lang_id[0]; *response_length = default_lang_id[0]; return true; } *response_data = (char*)&default_string[0]; *response_length = default_string[0]; return true; case USB_DT_BOS: *response_data = descs->bos; *response_length = descs->bos_len; return true; case USB_DT_DEVICE_QUALIFIER: if (!descs->qual) { struct usb_qualifier_descriptor* qual = (struct usb_qualifier_descriptor*)response_data; qual->bLength = sizeof(*qual); qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; qual->bcdUSB = index->dev->bcdUSB; qual->bDeviceClass = index->dev->bDeviceClass; qual->bDeviceSubClass = index->dev->bDeviceSubClass; qual->bDeviceProtocol = index->dev->bDeviceProtocol; qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0; qual->bNumConfigurations = index->dev->bNumConfigurations; qual->bRESERVED = 0; *response_length = sizeof(*qual); return true; } *response_data = descs->qual; *response_length = descs->qual_len; return true; default: break; } break; default: break; } break; default: break; } return false; } typedef bool (*lookup_connect_out_response_t)(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done); static bool lookup_connect_response_out_generic(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done) { switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_SET_CONFIGURATION: *done = true; return true; default: break; } break; } return false; } /* -------------------------------------------------------------------------- */ static int vhci_open(void) { return open("/dev/vhci", O_RDWR); } static int vhci_setport(int fd, u_int port) { struct vhci_ioc_set_port args; args.port = port; return ioctl(fd, VHCI_IOC_SET_PORT, &args); } static int vhci_usb_attach(int fd) { return ioctl(fd, VHCI_IOC_USB_ATTACH, NULL); } static int vhci_usb_recv(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = read(fd, ptr, size); if (done < 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } static int vhci_usb_send(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = write(fd, ptr, size); if (done <= 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } /* -------------------------------------------------------------------------- */ static volatile long syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev, const struct vusb_connect_descriptors* descs, lookup_connect_out_response_t lookup_connect_response_out) { struct usb_device_index* index; int portnum, fd, rv; bool done; portnum = procid + 1; if (!dev) { return -1; } if (portnum != 1) { /* For now, we support only one proc. */ return -1; } fd = vhci_open(); if (fd < 0) { return -1; } index = add_usb_index(fd, dev, dev_len); if (!index) { goto err; } rv = vhci_setport(fd, portnum); if (rv != 0) { goto err; } rv = vhci_usb_attach(fd); if (rv != 0) { goto err; } done = false; while (!done) { vhci_request_t req; rv = vhci_usb_recv(fd, &req, sizeof(req)); if (rv != 0) { goto err; } if (req.type != VHCI_REQ_CTRL) { goto err; } char* response_data = NULL; uint32_t response_length = 0; char data[4096]; if (req.u.ctrl.bmRequestType & UE_DIR_IN) { bool response_found = false; response_found = lookup_connect_response_in(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &response_data, &response_length); if (!response_found) { goto err; } } else { if (!lookup_connect_response_out(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &done)) { goto err; } response_data = NULL; response_length = UGETW(req.u.ctrl.wLength); } if ((req.u.ctrl.bmRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD && req.u.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) { /* TODO: possibly revisit */ } if (response_length > sizeof(data)) response_length = 0; if ((uint32_t)UGETW(req.u.ctrl.wLength) < response_length) response_length = UGETW(req.u.ctrl.wLength); if (response_data) memcpy(data, response_data, response_length); else memset(data, 0, response_length); if (req.u.ctrl.bmRequestType & UE_DIR_IN) { if (response_length > 0) { vhci_response_t res; res.size = response_length; rv = vhci_usb_send(fd, &res, sizeof(res)); if (rv == 0) rv = vhci_usb_send(fd, data, response_length); } } else { rv = vhci_usb_recv(fd, data, response_length); } if (rv < 0) { goto err; } } sleep_ms(200); return fd; err: close(fd); return -1; } static volatile long syz_usb_connect(volatile long a0, volatile long a1, volatile long a2, volatile long a3) { uint64_t speed = a0; uint64_t dev_len = a1; const char* dev = (const char*)a2; const struct vusb_connect_descriptors* descs = (const struct vusb_connect_descriptors*)a3; return syz_usb_connect_impl(speed, dev_len, dev, descs, &lookup_connect_response_out_generic); } static volatile long syz_usb_disconnect(volatile long a0) { int fd = a0; int rv = close(fd); sleep_ms(200); return rv; } static void sandbox_common() { if (setsid() == -1) exit(1); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 8 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 0; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); } static void loop(); static int do_sandbox_none(void) { sandbox_common(); loop(); return 0; } static long syz_execute_func(volatile long text) { volatile long p[8] = {0}; (void)p; asm volatile("" ::"r"(0l), "r"(1l), "r"(2l), "r"(3l), "r"(4l), "r"(5l), "r"(6l), "r"(7l), "r"(8l), "r"(9l), "r"(10l), "r"(11l), "r"(12l), "r"(13l)); ((void (*)(void))(text))(); return 0; } struct thread_t { int created, call; event_t ready, done; }; static struct thread_t threads[16]; static void execute_call(int call); static int running; static void* thr(void* arg) { struct thread_t* th = (struct thread_t*)arg; for (;;) { event_wait(&th->ready); event_reset(&th->ready); execute_call(th->call); __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED); event_set(&th->done); } return 0; } static void execute_one(void) { int i, call, thread; for (call = 0; call < 15; call++) { for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); thread++) { struct thread_t* th = &threads[thread]; if (!th->created) { th->created = 1; event_init(&th->ready); event_init(&th->done); event_set(&th->done); thread_start(thr, th); } if (!event_isset(&th->done)) continue; event_reset(&th->done); th->call = call; __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED); event_set(&th->ready); event_timedwait(&th->done, 45 + (call == 13 ? 3000 : 0) + (call == 14 ? 300 : 0)); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter; for (iter = 0; iter < 10; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5 * 1000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } #ifndef SYS__lwp_create #define SYS__lwp_create 309 #endif #ifndef SYS__lwp_kill #define SYS__lwp_kill 318 #endif #ifndef SYS__lwp_self #define SYS__lwp_self 311 #endif #ifndef SYS__lwp_unpark_all #define SYS__lwp_unpark_all 322 #endif #ifndef SYS_compat_50__lwp_park #define SYS_compat_50__lwp_park 320 #endif #ifndef SYS_mmap #define SYS_mmap 197 #endif #ifndef SYS_shmctl #define SYS_shmctl 443 #endif uint64_t r[7] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: res = syscall(SYS__lwp_self); if (res != -1) r[0] = res; break; case 1: *(uint32_t*)0x20000100 = 0x400f0020; *(uint64_t*)0x20000108 = 0x20000080; *(uint32_t*)0x20000080 = 0; *(uint64_t*)0x20000088 = 0x20000000; *(uint32_t*)0x20000000 = 0x400f0023; *(uint64_t*)0x20000008 = 0; *(uint32_t*)0x20000010 = 7; *(uint32_t*)0x20000014 = 7; *(uint32_t*)0x20000018 = 0x101; *(uint32_t*)0x2000001c = 0x1f; *(uint64_t*)0x20000020 = 1; *(uint64_t*)0x20000028 = 0x400; *(uint32_t*)0x20000030 = 6; *(uint64_t*)0x20000038 = 1; *(uint64_t*)0x20000040 = 0x80000000; memcpy((void*)0x20000048, "\000", 1); *(uint32_t*)0x20000090 = 3; *(uint32_t*)0x20000094 = 5; *(uint32_t*)0x20000098 = 1; *(uint32_t*)0x2000009c = 0x35f7192e; *(uint64_t*)0x200000a0 = 0xd; *(uint64_t*)0x200000a8 = 0x100000000; *(uint32_t*)0x200000b0 = 5; *(uint64_t*)0x200000b8 = 6; *(uint64_t*)0x200000c0 = 0x10001; memcpy((void*)0x200000c8, "\000", 1); *(uint32_t*)0x20000110 = 0xf1b; *(uint32_t*)0x20000114 = 0x401; *(uint32_t*)0x20000118 = 0x69; *(uint32_t*)0x2000011c = 0x100; *(uint64_t*)0x20000120 = 1; *(uint64_t*)0x20000128 = 0; *(uint32_t*)0x20000130 = 0x2ecd3534; *(uint64_t*)0x20000138 = 1; *(uint64_t*)0x20000140 = 0x400; memcpy((void*)0x20000148, "-\000", 2); res = syscall(SYS__lwp_create, 0x20000100ul, 0x60ul, 0x20000180ul); if (res != -1) r[1] = *(uint32_t*)0x20000180; break; case 2: res = syscall(SYS__lwp_self); if (res != -1) r[2] = res; break; case 3: *(uint32_t*)0x200001c0 = 2; *(uint64_t*)0x200001c8 = 0; *(uint32_t*)0x200001d0 = 9; *(uint32_t*)0x200001d4 = 0x3ae; *(uint32_t*)0x200001d8 = 3; *(uint32_t*)0x200001dc = 0xc699; *(uint64_t*)0x200001e0 = 0x81; *(uint64_t*)0x200001e8 = 2; *(uint32_t*)0x200001f0 = 1; *(uint64_t*)0x200001f8 = 9; *(uint64_t*)0x20000200 = 7; memcpy((void*)0x20000208, "\000", 1); res = syscall(SYS__lwp_create, 0x200001c0ul, 0x40ul, 0x20000240ul); if (res != -1) r[3] = *(uint32_t*)0x20000240; break; case 4: res = syscall(SYS__lwp_self); if (res != -1) r[4] = res; break; case 5: *(uint32_t*)0x20000280 = r[0]; *(uint32_t*)0x20000284 = r[1]; *(uint32_t*)0x20000288 = r[2]; *(uint32_t*)0x2000028c = r[3]; *(uint32_t*)0x20000290 = r[4]; *(uint64_t*)0x200002c0 = 4; syscall(SYS__lwp_unpark_all, 0x20000280ul, 5ul, 0x200002c0ul); break; case 6: *(uint32_t*)0x20000380 = 8; *(uint64_t*)0x20000388 = 0x20000300; *(uint32_t*)0x20000300 = 0x400f002c; *(uint64_t*)0x20000308 = 0; *(uint32_t*)0x20000310 = 0x80000000; *(uint32_t*)0x20000314 = 0xa706; *(uint32_t*)0x20000318 = 0; *(uint32_t*)0x2000031c = 8; *(uint64_t*)0x20000320 = 0x1f; *(uint64_t*)0x20000328 = 6; *(uint32_t*)0x20000330 = 1; *(uint64_t*)0x20000338 = 0x101; *(uint64_t*)0x20000340 = 5; memcpy((void*)0x20000348, "\000", 1); *(uint32_t*)0x20000390 = 2; *(uint32_t*)0x20000394 = 9; *(uint32_t*)0x20000398 = 0x10000; *(uint32_t*)0x2000039c = 1; *(uint64_t*)0x200003a0 = 2; *(uint64_t*)0x200003a8 = 0xffffffff; *(uint32_t*)0x200003b0 = 5; *(uint64_t*)0x200003b8 = 0x20; *(uint64_t*)0x200003c0 = 0xfff; memcpy((void*)0x200003c8, "\000", 1); res = syscall(SYS__lwp_create, 0x20000380ul, 0x40ul, 0x20000400ul); if (res != -1) r[5] = *(uint32_t*)0x20000400; break; case 7: syscall(SYS__lwp_kill, r[5], 8); break; case 8: *(uint32_t*)0x20000440 = 0x101; *(uint64_t*)0x20000448 = 8; *(uint64_t*)0x20000480 = 1; syscall(SYS_compat_50__lwp_park, 0x20000440ul, 0, 0ul, 0x20000480ul); break; case 9: syscall(SYS_shmctl, 0, 3ul, 0); break; case 10: memcpy((void*)0x20000000, "\xaf\xe8\xd2\x38\x3c\x3e\xf2\xff\x5f\x6e\x8c\x9a\x4d\x35\xc6\x02\x19\x87\x66\xff\x84\xe3\xac\xfd\x84\xf7\x78\x35\x3f\x61\x84\x2e\x8a\x19\x8d\x7e\x50\x0c\xce\x4b\x81\x36\x90\x33\x85\x89\xcc\xa0\x7a\xcd\xe0\xe9\x83\x4f\xd0\x9b\x00\xf3\x14\x6b\xa0\xdd\xb9\xac\x17\xae\x01\x1f\xb8\x61\xaf\x25\x83\x25\x47\x6c\x96\x21\x38\x8e\xca\x4b\x11\xc0", 84); break; case 11: memcpy((void*)0x20000080, "\x36\x42\x83\x3d\xd7\x0b\x58\x6a\x46\x3e\x64\x0f\xeb\xb1\x9b\x00\x00\x00\xc4\xc3\xf9\x7c\x6d\x4c\xa6\xf3\x44\xa6\xc4\x43\x85\x7f\xc6\x18\xfd\xc4\x03\x11\x6f\x26\xfa\x44\x7b\xa9\x64\x2e\x47\x0f\x12\xfc\x64\xf2\x0f\x11\x62\x00", 56); syz_execute_func(0x20000080); break; case 12: break; case 13: memcpy((void*)0x20000100, "\x93\x5e\x05\x7a\xa8\xdd\xae\xce\x20\x39\x4d\x86\xc4\x5c\x4a\x50\xf7\x27\x8a\x87\x91\xb0\xcd\xbc\xf6\xe8\x32\xb9\x05\x2f\x3a\xb9\x90\x63\x70\x55\x06\xc6\x6e\xb3\x37\x4a\xad\x5e\x1f\xde\xd7\x51\x5c\x8c\xb7\xfe\x2c\x21\x69\x94\xff\xb8\x9e\x91\x54\x96\xa2\x07\x40\x41\x68\xe8\x6b\xa8\x2f\x7c\x0d\x49\x64\xe7\xe7\x56\xac\x68\x99\x19\xcf\x9f\x4b\xf8\x7d\x3e\x3b\x51\x74\x60\x5c\x8f\x25\x6d\x28\x20\x4e\x42\x8b\xf2\x95\xff\x31\x2e\x2e\x27\xa1\xc2\xaf\xb8\x93\xdb\xe7\x86\x70\x60\xc6\x50", 120); memcpy((void*)0x20000180, "\x2b\x06\x57\x4b\x32\xd8\xeb\x1d\x74\x79\x43\x1c\x2a\xb5\x3f\xc2\xd2\x4c\xbf\x1c\x43\x68\xa8\x24\xc8\xcd\x82\x17\xf4\x4d\x51\xb1\x18\x89\xf9\x29\xd6\x5e\x79\x00\xcc\xcb\x51\x66\x45\x3f\xfe\x7f\xa1\xf3\x28\xdf\x96\x95\xa4\x45\x7b\x58\xf1\x31\x91\xe9\x0c\xa7\x94\x18\x6e\x76\x32\x17\xe9\x23\xbd\xc4\xf2\x04\x34\xeb\xa9\x66\x16\xcc\x3b\x27\xea\x87\xd5\x4a\x6c\x43\x4d\x77\xb7\x37\xd1\x8d\x0d\x8d\x53\x7d\xce\x33\x0d\x27\x59\xbe\x67\xe2\x2e\x1d\xc6\x17\xf5\x17\x14\x7f\xae\xb4\x4e\x34\x3d\x63\x0b\x33\xd1\xd3\x16\x06\xb2\xa3\x97\x1e\xfc\xd0\x3e\x4d\x87\xb5\xe6\x00\x32\x37\x23\xab\xfe\xf8\x1a\xa7\x1b\xea\x16\xbd\xf9\x56\x1a\xca\x75\xce\x32\x65\xde\x3a\xc8\xa1\x59\x0a\x61\xed\xe7\x3a\x7b\x69\x43\xdd\xf3\xe3\xfc\xf1\x9e\x47\x8f\x55\x85\x9d\xb7\x8d\x6b\xaa\x46\xb7\xa7\xe2\x15\x9f\x6f\x4f\x20\xcc\xb1\x8d\x5c\x41\xfb\x43\x02\x49", 206); res = syz_usb_connect(0xfffffffffffffeff, 0x78, 0x20000100, 0x20000180); if (res != -1) r[6] = res; break; case 14: syz_usb_disconnect(r[6]); break; } } int main(void) { syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul, 0ul); use_temporary_dir(); do_sandbox_none(); return 0; } : In function 'syz_usb_connect_impl': :637:63: error: unknown type name 'usb_ctrlrequest' :642:55: error: unknown type name 'usb_ctrlrequest' compiler invocation: /syzkaller/netbsd/src/../tools/bin/x86_64--netbsd-g++ [-o /tmp/syz-executor139321094 -DGOOS_netbsd=1 -DGOARCH_amd64=1 -DHOSTGOOS_linux=1 -x c - -m64 --sysroot /syzkaller/netbsd/src/../dest/ -O2 -pthread -Wall -Werror -Wparentheses -Wframe-larger-than=16384] --- FAIL: TestGenerate/netbsd/amd64/5 (0.21s) csource_test.go:123: opts: {Threaded:true Collide:false Repeat:true RepeatTimes:0 Procs:1 Sandbox:none Fault:false FaultCall:0 FaultNth:0 Leak:false NetInjection:false NetDevices:false NetReset:false Cgroups:false BinfmtMisc:false CloseFDs:false KCSAN:false DevlinkPCI:false USB:false UseTmpDir:true HandleSegv:false Repro:false Trace:false} program: r0 = _lwp_self() _lwp_create(&(0x7f0000000100)={0x400f0020, &(0x7f0000000080)={0x0, &(0x7f0000000000)={0x400f0023, 0x0, {[0x7, 0x7, 0x101, 0x1f]}, {0x1, 0x400, 0x6}, {0x1, 0x80000000, '\x00'}}, {[0x3, 0x5, 0x1, 0x35f7192e]}, {0xd, 0x100000000, 0x5}, {0x6, 0x10001, '\x00'}}, {[0xf1b, 0x401, 0x69, 0x100]}, {0x1, 0x0, 0x16ac7582ecd3534}, {0x1, 0x400, '-\x00'}}, 0x60, &(0x7f0000000180)=0x0) r2 = _lwp_self() _lwp_create(&(0x7f00000001c0)={0x2, 0x0, {[0x9, 0x3ae, 0x3, 0xc699]}, {0x81, 0x2, 0x1}, {0x9, 0x7, '\x00'}}, 0x40, &(0x7f0000000240)=0x0) r4 = _lwp_self() _lwp_unpark_all(&(0x7f0000000280)=[r0, r1, r2, r3, r4], 0x5, &(0x7f00000002c0)=0x4) _lwp_create(&(0x7f0000000380)={0x8, &(0x7f0000000300)={0x400f002c, 0x0, {[0x80000000, 0xa706, 0x0, 0x8]}, {0x1f, 0x6, 0x1}, {0x101, 0x5, '\x00'}}, {[0x2, 0x9, 0x10000, 0x1]}, {0x2, 0xffffffff, 0x5}, {0x20, 0xfff, '\x00'}}, 0x40, &(0x7f0000000400)=0x0) _lwp_kill(r5, 0x8) compat_50__lwp_park(&(0x7f0000000440)={0x101, 0x8}, 0x0, 0x0, &(0x7f0000000480)=0x1) shmctl$SHM_LOCK(0x0, 0x3) syz_emit_ethernet(0x54, &(0x7f0000000000)="afe8d2383c3ef2ff5f6e8c9a4d35c602198766ff84e3acfd84f778353f61842e8a198d7e500cce4b813690338589cca07acde0e9834fd09b00f3146ba0ddb9ac17ae011fb861af258325476c9621388eca4b11c0") syz_execute_func(&(0x7f0000000080)="3642833dd70b586a463e640febb19b000000c4c3f97c6d4ca6f344a6c443857fc618fdc403116f26fa447ba9642e470f12fc64f20f116200") syz_extract_tcp_res(&(0x7f00000000c0), 0x3, 0xc5d) r6 = syz_usb_connect(0xfffffffffffffeff, 0x78, &(0x7f0000000100)="935e057aa8ddaece20394d86c45c4a50f7278a8791b0cdbcf6e832b9052f3ab99063705506c66eb3374aad5e1fded7515c8cb7fe2c216994ffb89e915496a207404168e86ba82f7c0d4964e7e756ac689919cf9f4bf87d3e3b5174605c8f256d28204e428bf295ff312e2e27a1c2afb893dbe7867060c650", &(0x7f0000000180)="2b06574b32d8eb1d7479431c2ab53fc2d24cbf1c4368a824c8cd8217f44d51b11889f929d65e7900cccb5166453ffe7fa1f328df9695a4457b58f13191e90ca794186e763217e923bdc4f20434eba96616cc3b27ea87d54a6c434d77b737d18d0d8d537dce330d2759be67e22e1dc617f517147faeb44e343d630b33d1d31606b2a3971efcd03e4d87b5e600323723abfef81aa71bea16bdf9561aca75ce3265de3ac8a1590a61ede73a7b6943ddf3e3fcf19e478f55859db78d6baa46b7a7e2159f6f4f20ccb18d5c41fb430249") syz_usb_disconnect(r6) csource_test.go:124: failed to build program: // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static unsigned long long procid; static void kill_and_wait(int pid, int* status) { kill(pid, SIGKILL); while (waitpid(-1, status, 0) != pid) { } } static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void use_temporary_dir(void) { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } static void remove_dir(const char* dir) { DIR* dp; struct dirent* ep; dp = opendir(dir); if (dp == NULL) exit(1); while ((ep = readdir(dp))) { if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue; char filename[FILENAME_MAX]; snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } if (unlink(filename)) exit(1); } closedir(dp); if (rmdir(dir)) exit(1); } static void thread_start(void* (*fn)(void*), void* arg) { pthread_t th; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 128 << 10); int i; for (i = 0; i < 100; i++) { if (pthread_create(&th, &attr, fn, arg) == 0) { pthread_attr_destroy(&attr); return; } if (errno == EAGAIN) { usleep(50); continue; } break; } exit(1); } typedef struct { pthread_mutex_t mu; pthread_cond_t cv; int state; } event_t; static void event_init(event_t* ev) { if (pthread_mutex_init(&ev->mu, 0)) exit(1); if (pthread_cond_init(&ev->cv, 0)) exit(1); ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { pthread_mutex_lock(&ev->mu); if (ev->state) exit(1); ev->state = 1; pthread_mutex_unlock(&ev->mu); pthread_cond_broadcast(&ev->cv); } static void event_wait(event_t* ev) { pthread_mutex_lock(&ev->mu); while (!ev->state) pthread_cond_wait(&ev->cv, &ev->mu); pthread_mutex_unlock(&ev->mu); } static int event_isset(event_t* ev) { pthread_mutex_lock(&ev->mu); int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; pthread_mutex_lock(&ev->mu); for (;;) { if (ev->state) break; uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; pthread_cond_timedwait(&ev->cv, &ev->mu, &ts); now = current_time_ms(); if (now - start > timeout) break; } int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } /* -------------------------------------------------------------------------- */ /* * Redefinitions to match the linux types used in common_usb.h. */ struct usb_endpoint_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; uint16_t wMaxPacketSize; uint8_t bInterval; uint8_t bRefresh; uint8_t bSynchAddress; } __attribute__((packed)); struct usb_device_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint16_t idVendor; uint16_t idProduct; uint16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; } __attribute__((packed)); struct usb_config_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; } __attribute__((packed)); struct usb_interface_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bNumEndpoints; uint8_t bInterfaceClass; uint8_t bInterfaceSubClass; uint8_t bInterfaceProtocol; uint8_t iInterface; } __attribute__((packed)); struct usb_ctrlrequest { uint8_t bRequestType; uint8_t bRequest; uint16_t wValue; uint16_t wIndex; uint16_t wLength; } __attribute__((packed)); struct usb_qualifier_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint8_t bNumConfigurations; uint8_t bRESERVED; } __attribute__((packed)); #define USB_TYPE_MASK (0x03 << 5) #define USB_TYPE_STANDARD (0x00 << 5) #define USB_TYPE_CLASS (0x01 << 5) #define USB_TYPE_VENDOR (0x02 << 5) #define USB_TYPE_RESERVED (0x03 << 5) #define USB_DT_DEVICE 0x01 #define USB_DT_CONFIG 0x02 #define USB_DT_STRING 0x03 #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_DEVICE_QUALIFIER 0x06 #define USB_DT_OTHER_SPEED_CONFIG 0x07 #define USB_DT_INTERFACE_POWER 0x08 #define USB_DT_OTG 0x09 #define USB_DT_DEBUG 0x0a #define USB_DT_INTERFACE_ASSOCIATION 0x0b #define USB_DT_SECURITY 0x0c #define USB_DT_KEY 0x0d #define USB_DT_ENCRYPTION_TYPE 0x0e #define USB_DT_BOS 0x0f #define USB_DT_DEVICE_CAPABILITY 0x10 #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 #define USB_DT_WIRE_ADAPTER 0x21 #define USB_DT_RPIPE 0x22 #define USB_DT_CS_RADIO_CONTROL 0x23 #define USB_DT_PIPE_USAGE 0x24 #define USB_DT_SS_ENDPOINT_COMP 0x30 #define USB_DT_SSP_ISOC_ENDPOINT_COMP 0x31 #define USB_REQ_GET_STATUS 0x00 #define USB_REQ_CLEAR_FEATURE 0x01 #define USB_REQ_SET_FEATURE 0x03 #define USB_REQ_SET_ADDRESS 0x05 #define USB_REQ_GET_DESCRIPTOR 0x06 #define USB_REQ_SET_DESCRIPTOR 0x07 #define USB_REQ_GET_CONFIGURATION 0x08 #define USB_REQ_SET_CONFIGURATION 0x09 #define USB_REQ_GET_INTERFACE 0x0A #define USB_REQ_SET_INTERFACE 0x0B #define USB_REQ_SYNCH_FRAME 0x0C #define USB_REQ_SET_SEL 0x30 #define USB_REQ_SET_ISOCH_DELAY 0x31 #define USB_REQ_SET_ENCRYPTION 0x0D #define USB_REQ_GET_ENCRYPTION 0x0E #define USB_REQ_RPIPE_ABORT 0x0E #define USB_REQ_SET_HANDSHAKE 0x0F #define USB_REQ_RPIPE_RESET 0x0F #define USB_REQ_GET_HANDSHAKE 0x10 #define USB_REQ_SET_CONNECTION 0x11 #define USB_REQ_SET_SECURITY_DATA 0x12 #define USB_REQ_GET_SECURITY_DATA 0x13 #define USB_REQ_SET_WUSB_DATA 0x14 #define USB_REQ_LOOPBACK_DATA_WRITE 0x15 #define USB_REQ_LOOPBACK_DATA_READ 0x16 #define USB_REQ_SET_INTERFACE_DS 0x17 #define USB_REQ_GET_PARTNER_PDO 20 #define USB_REQ_GET_BATTERY_STATUS 21 #define USB_REQ_SET_PDO 22 #define USB_REQ_GET_VDM 23 #define USB_REQ_SEND_VDM 24 #define USB_MAX_IFACE_NUM 4 #define USB_MAX_EP_NUM 32 #define USB_MAX_FDS 6 struct usb_endpoint_index { struct usb_endpoint_descriptor desc; int handle; }; struct usb_iface_index { struct usb_interface_descriptor* iface; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bInterfaceClass; struct usb_endpoint_index eps[USB_MAX_EP_NUM]; int eps_num; }; struct usb_device_index { struct usb_device_descriptor* dev; struct usb_config_descriptor* config; uint8_t bDeviceClass; uint8_t bMaxPower; int config_length; struct usb_iface_index ifaces[USB_MAX_IFACE_NUM]; int ifaces_num; int iface_cur; }; struct usb_info { int fd; struct usb_device_index index; }; static struct usb_info usb_devices[USB_MAX_FDS]; static int usb_devices_num; static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index) { if (length < sizeof(*index->dev) + sizeof(*index->config)) return false; memset(index, 0, sizeof(*index)); index->dev = (struct usb_device_descriptor*)buffer; index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev)); index->bDeviceClass = index->dev->bDeviceClass; index->bMaxPower = index->config->bMaxPower; index->config_length = length - sizeof(*index->dev); index->iface_cur = -1; size_t offset = 0; while (true) { if (offset + 1 >= length) break; uint8_t desc_length = buffer[offset]; uint8_t desc_type = buffer[offset + 1]; if (desc_length <= 2) break; if (offset + desc_length > length) break; if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) { struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset); index->ifaces[index->ifaces_num].iface = iface; index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber; index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting; index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass; index->ifaces_num++; } if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) { struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1]; if (iface->eps_num < USB_MAX_EP_NUM) { memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc)); iface->eps_num++; } } offset += desc_length; } return true; } static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len) { int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED); if (i >= USB_MAX_FDS) return NULL; int rv = 0; rv = parse_usb_descriptor(dev, dev_len, &usb_devices[i].index); if (!rv) return NULL; __atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE); return &usb_devices[i].index; } static struct usb_device_index* lookup_usb_index(int fd) { int i; for (i = 0; i < USB_MAX_FDS; i++) { if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd) { return &usb_devices[i].index; } } return NULL; } struct vusb_connect_string_descriptor { uint32_t len; char* str; } __attribute__((packed)); struct vusb_connect_descriptors { uint32_t qual_len; char* qual; uint32_t bos_len; char* bos; uint32_t strs_len; struct vusb_connect_string_descriptor strs[0]; } __attribute__((packed)); static const char default_string[] = { 8, USB_DT_STRING, 's', 0, 'y', 0, 'z', 0 }; static const char default_lang_id[] = { 4, USB_DT_STRING, 0x09, 0x04 }; static bool lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, char** response_data, uint32_t* response_length) { struct usb_device_index* index = lookup_usb_index(fd); uint8_t str_idx; if (!index) return false; switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_GET_DESCRIPTOR: switch (ctrl->wValue >> 8) { case USB_DT_DEVICE: *response_data = (char*)index->dev; *response_length = sizeof(*index->dev); return true; case USB_DT_CONFIG: *response_data = (char*)index->config; *response_length = index->config_length; return true; case USB_DT_STRING: str_idx = (uint8_t)ctrl->wValue; if (descs && str_idx < descs->strs_len) { *response_data = descs->strs[str_idx].str; *response_length = descs->strs[str_idx].len; return true; } if (str_idx == 0) { *response_data = (char*)&default_lang_id[0]; *response_length = default_lang_id[0]; return true; } *response_data = (char*)&default_string[0]; *response_length = default_string[0]; return true; case USB_DT_BOS: *response_data = descs->bos; *response_length = descs->bos_len; return true; case USB_DT_DEVICE_QUALIFIER: if (!descs->qual) { struct usb_qualifier_descriptor* qual = (struct usb_qualifier_descriptor*)response_data; qual->bLength = sizeof(*qual); qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; qual->bcdUSB = index->dev->bcdUSB; qual->bDeviceClass = index->dev->bDeviceClass; qual->bDeviceSubClass = index->dev->bDeviceSubClass; qual->bDeviceProtocol = index->dev->bDeviceProtocol; qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0; qual->bNumConfigurations = index->dev->bNumConfigurations; qual->bRESERVED = 0; *response_length = sizeof(*qual); return true; } *response_data = descs->qual; *response_length = descs->qual_len; return true; default: break; } break; default: break; } break; default: break; } return false; } typedef bool (*lookup_connect_out_response_t)(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done); static bool lookup_connect_response_out_generic(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done) { switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_SET_CONFIGURATION: *done = true; return true; default: break; } break; } return false; } /* -------------------------------------------------------------------------- */ static int vhci_open(void) { return open("/dev/vhci", O_RDWR); } static int vhci_setport(int fd, u_int port) { struct vhci_ioc_set_port args; args.port = port; return ioctl(fd, VHCI_IOC_SET_PORT, &args); } static int vhci_usb_attach(int fd) { return ioctl(fd, VHCI_IOC_USB_ATTACH, NULL); } static int vhci_usb_recv(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = read(fd, ptr, size); if (done < 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } static int vhci_usb_send(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = write(fd, ptr, size); if (done <= 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } /* -------------------------------------------------------------------------- */ static volatile long syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev, const struct vusb_connect_descriptors* descs, lookup_connect_out_response_t lookup_connect_response_out) { struct usb_device_index* index; int portnum, fd, rv; bool done; portnum = procid + 1; if (!dev) { return -1; } if (portnum != 1) { /* For now, we support only one proc. */ return -1; } fd = vhci_open(); if (fd < 0) { return -1; } index = add_usb_index(fd, dev, dev_len); if (!index) { goto err; } rv = vhci_setport(fd, portnum); if (rv != 0) { goto err; } rv = vhci_usb_attach(fd); if (rv != 0) { goto err; } done = false; while (!done) { vhci_request_t req; rv = vhci_usb_recv(fd, &req, sizeof(req)); if (rv != 0) { goto err; } if (req.type != VHCI_REQ_CTRL) { goto err; } char* response_data = NULL; uint32_t response_length = 0; char data[4096]; if (req.u.ctrl.bmRequestType & UE_DIR_IN) { bool response_found = false; response_found = lookup_connect_response_in(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &response_data, &response_length); if (!response_found) { goto err; } } else { if (!lookup_connect_response_out(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &done)) { goto err; } response_data = NULL; response_length = UGETW(req.u.ctrl.wLength); } if ((req.u.ctrl.bmRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD && req.u.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) { /* TODO: possibly revisit */ } if (response_length > sizeof(data)) response_length = 0; if ((uint32_t)UGETW(req.u.ctrl.wLength) < response_length) response_length = UGETW(req.u.ctrl.wLength); if (response_data) memcpy(data, response_data, response_length); else memset(data, 0, response_length); if (req.u.ctrl.bmRequestType & UE_DIR_IN) { if (response_length > 0) { vhci_response_t res; res.size = response_length; rv = vhci_usb_send(fd, &res, sizeof(res)); if (rv == 0) rv = vhci_usb_send(fd, data, response_length); } } else { rv = vhci_usb_recv(fd, data, response_length); } if (rv < 0) { goto err; } } sleep_ms(200); return fd; err: close(fd); return -1; } static volatile long syz_usb_connect(volatile long a0, volatile long a1, volatile long a2, volatile long a3) { uint64_t speed = a0; uint64_t dev_len = a1; const char* dev = (const char*)a2; const struct vusb_connect_descriptors* descs = (const struct vusb_connect_descriptors*)a3; return syz_usb_connect_impl(speed, dev_len, dev, descs, &lookup_connect_response_out_generic); } static volatile long syz_usb_disconnect(volatile long a0) { int fd = a0; int rv = close(fd); sleep_ms(200); return rv; } static void sandbox_common() { if (setsid() == -1) exit(1); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 8 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 0; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); } static void loop(); static int do_sandbox_none(void) { sandbox_common(); loop(); return 0; } static long syz_execute_func(volatile long text) { volatile long p[8] = {0}; (void)p; asm volatile("" ::"r"(0l), "r"(1l), "r"(2l), "r"(3l), "r"(4l), "r"(5l), "r"(6l), "r"(7l), "r"(8l), "r"(9l), "r"(10l), "r"(11l), "r"(12l), "r"(13l)); ((void (*)(void))(text))(); return 0; } struct thread_t { int created, call; event_t ready, done; }; static struct thread_t threads[16]; static void execute_call(int call); static int running; static void* thr(void* arg) { struct thread_t* th = (struct thread_t*)arg; for (;;) { event_wait(&th->ready); event_reset(&th->ready); execute_call(th->call); __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED); event_set(&th->done); } return 0; } static void execute_one(void) { int i, call, thread; for (call = 0; call < 15; call++) { for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); thread++) { struct thread_t* th = &threads[thread]; if (!th->created) { th->created = 1; event_init(&th->ready); event_init(&th->done); event_set(&th->done); thread_start(thr, th); } if (!event_isset(&th->done)) continue; event_reset(&th->done); th->call = call; __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED); event_set(&th->ready); event_timedwait(&th->done, 45 + (call == 13 ? 3000 : 0) + (call == 14 ? 300 : 0)); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter; for (iter = 0;; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5 * 1000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } #ifndef SYS__lwp_create #define SYS__lwp_create 309 #endif #ifndef SYS__lwp_kill #define SYS__lwp_kill 318 #endif #ifndef SYS__lwp_self #define SYS__lwp_self 311 #endif #ifndef SYS__lwp_unpark_all #define SYS__lwp_unpark_all 322 #endif #ifndef SYS_compat_50__lwp_park #define SYS_compat_50__lwp_park 320 #endif #ifndef SYS_mmap #define SYS_mmap 197 #endif #ifndef SYS_shmctl #define SYS_shmctl 443 #endif uint64_t r[7] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: res = syscall(SYS__lwp_self); if (res != -1) r[0] = res; break; case 1: *(uint32_t*)0x20000100 = 0x400f0020; *(uint64_t*)0x20000108 = 0x20000080; *(uint32_t*)0x20000080 = 0; *(uint64_t*)0x20000088 = 0x20000000; *(uint32_t*)0x20000000 = 0x400f0023; *(uint64_t*)0x20000008 = 0; *(uint32_t*)0x20000010 = 7; *(uint32_t*)0x20000014 = 7; *(uint32_t*)0x20000018 = 0x101; *(uint32_t*)0x2000001c = 0x1f; *(uint64_t*)0x20000020 = 1; *(uint64_t*)0x20000028 = 0x400; *(uint32_t*)0x20000030 = 6; *(uint64_t*)0x20000038 = 1; *(uint64_t*)0x20000040 = 0x80000000; memcpy((void*)0x20000048, "\000", 1); *(uint32_t*)0x20000090 = 3; *(uint32_t*)0x20000094 = 5; *(uint32_t*)0x20000098 = 1; *(uint32_t*)0x2000009c = 0x35f7192e; *(uint64_t*)0x200000a0 = 0xd; *(uint64_t*)0x200000a8 = 0x100000000; *(uint32_t*)0x200000b0 = 5; *(uint64_t*)0x200000b8 = 6; *(uint64_t*)0x200000c0 = 0x10001; memcpy((void*)0x200000c8, "\000", 1); *(uint32_t*)0x20000110 = 0xf1b; *(uint32_t*)0x20000114 = 0x401; *(uint32_t*)0x20000118 = 0x69; *(uint32_t*)0x2000011c = 0x100; *(uint64_t*)0x20000120 = 1; *(uint64_t*)0x20000128 = 0; *(uint32_t*)0x20000130 = 0x2ecd3534; *(uint64_t*)0x20000138 = 1; *(uint64_t*)0x20000140 = 0x400; memcpy((void*)0x20000148, "-\000", 2); res = syscall(SYS__lwp_create, 0x20000100ul, 0x60ul, 0x20000180ul); if (res != -1) r[1] = *(uint32_t*)0x20000180; break; case 2: res = syscall(SYS__lwp_self); if (res != -1) r[2] = res; break; case 3: *(uint32_t*)0x200001c0 = 2; *(uint64_t*)0x200001c8 = 0; *(uint32_t*)0x200001d0 = 9; *(uint32_t*)0x200001d4 = 0x3ae; *(uint32_t*)0x200001d8 = 3; *(uint32_t*)0x200001dc = 0xc699; *(uint64_t*)0x200001e0 = 0x81; *(uint64_t*)0x200001e8 = 2; *(uint32_t*)0x200001f0 = 1; *(uint64_t*)0x200001f8 = 9; *(uint64_t*)0x20000200 = 7; memcpy((void*)0x20000208, "\000", 1); res = syscall(SYS__lwp_create, 0x200001c0ul, 0x40ul, 0x20000240ul); if (res != -1) r[3] = *(uint32_t*)0x20000240; break; case 4: res = syscall(SYS__lwp_self); if (res != -1) r[4] = res; break; case 5: *(uint32_t*)0x20000280 = r[0]; *(uint32_t*)0x20000284 = r[1]; *(uint32_t*)0x20000288 = r[2]; *(uint32_t*)0x2000028c = r[3]; *(uint32_t*)0x20000290 = r[4]; *(uint64_t*)0x200002c0 = 4; syscall(SYS__lwp_unpark_all, 0x20000280ul, 5ul, 0x200002c0ul); break; case 6: *(uint32_t*)0x20000380 = 8; *(uint64_t*)0x20000388 = 0x20000300; *(uint32_t*)0x20000300 = 0x400f002c; *(uint64_t*)0x20000308 = 0; *(uint32_t*)0x20000310 = 0x80000000; *(uint32_t*)0x20000314 = 0xa706; *(uint32_t*)0x20000318 = 0; *(uint32_t*)0x2000031c = 8; *(uint64_t*)0x20000320 = 0x1f; *(uint64_t*)0x20000328 = 6; *(uint32_t*)0x20000330 = 1; *(uint64_t*)0x20000338 = 0x101; *(uint64_t*)0x20000340 = 5; memcpy((void*)0x20000348, "\000", 1); *(uint32_t*)0x20000390 = 2; *(uint32_t*)0x20000394 = 9; *(uint32_t*)0x20000398 = 0x10000; *(uint32_t*)0x2000039c = 1; *(uint64_t*)0x200003a0 = 2; *(uint64_t*)0x200003a8 = 0xffffffff; *(uint32_t*)0x200003b0 = 5; *(uint64_t*)0x200003b8 = 0x20; *(uint64_t*)0x200003c0 = 0xfff; memcpy((void*)0x200003c8, "\000", 1); res = syscall(SYS__lwp_create, 0x20000380ul, 0x40ul, 0x20000400ul); if (res != -1) r[5] = *(uint32_t*)0x20000400; break; case 7: syscall(SYS__lwp_kill, r[5], 8); break; case 8: *(uint32_t*)0x20000440 = 0x101; *(uint64_t*)0x20000448 = 8; *(uint64_t*)0x20000480 = 1; syscall(SYS_compat_50__lwp_park, 0x20000440ul, 0, 0ul, 0x20000480ul); break; case 9: syscall(SYS_shmctl, 0, 3ul, 0); break; case 10: memcpy((void*)0x20000000, "\xaf\xe8\xd2\x38\x3c\x3e\xf2\xff\x5f\x6e\x8c\x9a\x4d\x35\xc6\x02\x19\x87\x66\xff\x84\xe3\xac\xfd\x84\xf7\x78\x35\x3f\x61\x84\x2e\x8a\x19\x8d\x7e\x50\x0c\xce\x4b\x81\x36\x90\x33\x85\x89\xcc\xa0\x7a\xcd\xe0\xe9\x83\x4f\xd0\x9b\x00\xf3\x14\x6b\xa0\xdd\xb9\xac\x17\xae\x01\x1f\xb8\x61\xaf\x25\x83\x25\x47\x6c\x96\x21\x38\x8e\xca\x4b\x11\xc0", 84); break; case 11: memcpy((void*)0x20000080, "\x36\x42\x83\x3d\xd7\x0b\x58\x6a\x46\x3e\x64\x0f\xeb\xb1\x9b\x00\x00\x00\xc4\xc3\xf9\x7c\x6d\x4c\xa6\xf3\x44\xa6\xc4\x43\x85\x7f\xc6\x18\xfd\xc4\x03\x11\x6f\x26\xfa\x44\x7b\xa9\x64\x2e\x47\x0f\x12\xfc\x64\xf2\x0f\x11\x62\x00", 56); syz_execute_func(0x20000080); break; case 12: break; case 13: memcpy((void*)0x20000100, "\x93\x5e\x05\x7a\xa8\xdd\xae\xce\x20\x39\x4d\x86\xc4\x5c\x4a\x50\xf7\x27\x8a\x87\x91\xb0\xcd\xbc\xf6\xe8\x32\xb9\x05\x2f\x3a\xb9\x90\x63\x70\x55\x06\xc6\x6e\xb3\x37\x4a\xad\x5e\x1f\xde\xd7\x51\x5c\x8c\xb7\xfe\x2c\x21\x69\x94\xff\xb8\x9e\x91\x54\x96\xa2\x07\x40\x41\x68\xe8\x6b\xa8\x2f\x7c\x0d\x49\x64\xe7\xe7\x56\xac\x68\x99\x19\xcf\x9f\x4b\xf8\x7d\x3e\x3b\x51\x74\x60\x5c\x8f\x25\x6d\x28\x20\x4e\x42\x8b\xf2\x95\xff\x31\x2e\x2e\x27\xa1\xc2\xaf\xb8\x93\xdb\xe7\x86\x70\x60\xc6\x50", 120); memcpy((void*)0x20000180, "\x2b\x06\x57\x4b\x32\xd8\xeb\x1d\x74\x79\x43\x1c\x2a\xb5\x3f\xc2\xd2\x4c\xbf\x1c\x43\x68\xa8\x24\xc8\xcd\x82\x17\xf4\x4d\x51\xb1\x18\x89\xf9\x29\xd6\x5e\x79\x00\xcc\xcb\x51\x66\x45\x3f\xfe\x7f\xa1\xf3\x28\xdf\x96\x95\xa4\x45\x7b\x58\xf1\x31\x91\xe9\x0c\xa7\x94\x18\x6e\x76\x32\x17\xe9\x23\xbd\xc4\xf2\x04\x34\xeb\xa9\x66\x16\xcc\x3b\x27\xea\x87\xd5\x4a\x6c\x43\x4d\x77\xb7\x37\xd1\x8d\x0d\x8d\x53\x7d\xce\x33\x0d\x27\x59\xbe\x67\xe2\x2e\x1d\xc6\x17\xf5\x17\x14\x7f\xae\xb4\x4e\x34\x3d\x63\x0b\x33\xd1\xd3\x16\x06\xb2\xa3\x97\x1e\xfc\xd0\x3e\x4d\x87\xb5\xe6\x00\x32\x37\x23\xab\xfe\xf8\x1a\xa7\x1b\xea\x16\xbd\xf9\x56\x1a\xca\x75\xce\x32\x65\xde\x3a\xc8\xa1\x59\x0a\x61\xed\xe7\x3a\x7b\x69\x43\xdd\xf3\xe3\xfc\xf1\x9e\x47\x8f\x55\x85\x9d\xb7\x8d\x6b\xaa\x46\xb7\xa7\xe2\x15\x9f\x6f\x4f\x20\xcc\xb1\x8d\x5c\x41\xfb\x43\x02\x49", 206); res = syz_usb_connect(0xfffffffffffffeff, 0x78, 0x20000100, 0x20000180); if (res != -1) r[6] = res; break; case 14: syz_usb_disconnect(r[6]); break; } } int main(void) { syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul, 0ul); use_temporary_dir(); do_sandbox_none(); return 0; } : In function 'syz_usb_connect_impl': :637:63: error: unknown type name 'usb_ctrlrequest' :642:55: error: unknown type name 'usb_ctrlrequest' compiler invocation: /syzkaller/netbsd/src/../tools/bin/x86_64--netbsd-g++ [-o /tmp/syz-executor261432730 -DGOOS_netbsd=1 -DGOARCH_amd64=1 -DHOSTGOOS_linux=1 -x c - -m64 --sysroot /syzkaller/netbsd/src/../dest/ -O2 -pthread -Wall -Werror -Wparentheses -Wframe-larger-than=16384] --- FAIL: TestGenerate/netbsd/amd64/6 (0.21s) csource_test.go:123: opts: {Threaded:true Collide:false Repeat:true RepeatTimes:0 Procs:4 Sandbox:none Fault:false FaultCall:0 FaultNth:0 Leak:false NetInjection:false NetDevices:false NetReset:false Cgroups:false BinfmtMisc:false CloseFDs:false KCSAN:false DevlinkPCI:false USB:false UseTmpDir:true HandleSegv:false Repro:false Trace:false} program: r0 = _lwp_self() _lwp_create(&(0x7f0000000100)={0x400f0020, &(0x7f0000000080)={0x0, &(0x7f0000000000)={0x400f0023, 0x0, {[0x7, 0x7, 0x101, 0x1f]}, {0x1, 0x400, 0x6}, {0x1, 0x80000000, '\x00'}}, {[0x3, 0x5, 0x1, 0x35f7192e]}, {0xd, 0x100000000, 0x5}, {0x6, 0x10001, '\x00'}}, {[0xf1b, 0x401, 0x69, 0x100]}, {0x1, 0x0, 0x16ac7582ecd3534}, {0x1, 0x400, '-\x00'}}, 0x60, &(0x7f0000000180)=0x0) r2 = _lwp_self() _lwp_create(&(0x7f00000001c0)={0x2, 0x0, {[0x9, 0x3ae, 0x3, 0xc699]}, {0x81, 0x2, 0x1}, {0x9, 0x7, '\x00'}}, 0x40, &(0x7f0000000240)=0x0) r4 = _lwp_self() _lwp_unpark_all(&(0x7f0000000280)=[r0, r1, r2, r3, r4], 0x5, &(0x7f00000002c0)=0x4) _lwp_create(&(0x7f0000000380)={0x8, &(0x7f0000000300)={0x400f002c, 0x0, {[0x80000000, 0xa706, 0x0, 0x8]}, {0x1f, 0x6, 0x1}, {0x101, 0x5, '\x00'}}, {[0x2, 0x9, 0x10000, 0x1]}, {0x2, 0xffffffff, 0x5}, {0x20, 0xfff, '\x00'}}, 0x40, &(0x7f0000000400)=0x0) _lwp_kill(r5, 0x8) compat_50__lwp_park(&(0x7f0000000440)={0x101, 0x8}, 0x0, 0x0, &(0x7f0000000480)=0x1) shmctl$SHM_LOCK(0x0, 0x3) syz_emit_ethernet(0x54, &(0x7f0000000000)="afe8d2383c3ef2ff5f6e8c9a4d35c602198766ff84e3acfd84f778353f61842e8a198d7e500cce4b813690338589cca07acde0e9834fd09b00f3146ba0ddb9ac17ae011fb861af258325476c9621388eca4b11c0") syz_execute_func(&(0x7f0000000080)="3642833dd70b586a463e640febb19b000000c4c3f97c6d4ca6f344a6c443857fc618fdc403116f26fa447ba9642e470f12fc64f20f116200") syz_extract_tcp_res(&(0x7f00000000c0), 0x3, 0xc5d) r6 = syz_usb_connect(0xfffffffffffffeff, 0x78, &(0x7f0000000100)="935e057aa8ddaece20394d86c45c4a50f7278a8791b0cdbcf6e832b9052f3ab99063705506c66eb3374aad5e1fded7515c8cb7fe2c216994ffb89e915496a207404168e86ba82f7c0d4964e7e756ac689919cf9f4bf87d3e3b5174605c8f256d28204e428bf295ff312e2e27a1c2afb893dbe7867060c650", &(0x7f0000000180)="2b06574b32d8eb1d7479431c2ab53fc2d24cbf1c4368a824c8cd8217f44d51b11889f929d65e7900cccb5166453ffe7fa1f328df9695a4457b58f13191e90ca794186e763217e923bdc4f20434eba96616cc3b27ea87d54a6c434d77b737d18d0d8d537dce330d2759be67e22e1dc617f517147faeb44e343d630b33d1d31606b2a3971efcd03e4d87b5e600323723abfef81aa71bea16bdf9561aca75ce3265de3ac8a1590a61ede73a7b6943ddf3e3fcf19e478f55859db78d6baa46b7a7e2159f6f4f20ccb18d5c41fb430249") syz_usb_disconnect(r6) csource_test.go:124: failed to build program: // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static unsigned long long procid; static void kill_and_wait(int pid, int* status) { kill(pid, SIGKILL); while (waitpid(-1, status, 0) != pid) { } } static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void use_temporary_dir(void) { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } static void remove_dir(const char* dir) { DIR* dp; struct dirent* ep; dp = opendir(dir); if (dp == NULL) exit(1); while ((ep = readdir(dp))) { if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue; char filename[FILENAME_MAX]; snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } if (unlink(filename)) exit(1); } closedir(dp); if (rmdir(dir)) exit(1); } static void thread_start(void* (*fn)(void*), void* arg) { pthread_t th; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 128 << 10); int i; for (i = 0; i < 100; i++) { if (pthread_create(&th, &attr, fn, arg) == 0) { pthread_attr_destroy(&attr); return; } if (errno == EAGAIN) { usleep(50); continue; } break; } exit(1); } typedef struct { pthread_mutex_t mu; pthread_cond_t cv; int state; } event_t; static void event_init(event_t* ev) { if (pthread_mutex_init(&ev->mu, 0)) exit(1); if (pthread_cond_init(&ev->cv, 0)) exit(1); ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { pthread_mutex_lock(&ev->mu); if (ev->state) exit(1); ev->state = 1; pthread_mutex_unlock(&ev->mu); pthread_cond_broadcast(&ev->cv); } static void event_wait(event_t* ev) { pthread_mutex_lock(&ev->mu); while (!ev->state) pthread_cond_wait(&ev->cv, &ev->mu); pthread_mutex_unlock(&ev->mu); } static int event_isset(event_t* ev) { pthread_mutex_lock(&ev->mu); int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; pthread_mutex_lock(&ev->mu); for (;;) { if (ev->state) break; uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; pthread_cond_timedwait(&ev->cv, &ev->mu, &ts); now = current_time_ms(); if (now - start > timeout) break; } int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } /* -------------------------------------------------------------------------- */ /* * Redefinitions to match the linux types used in common_usb.h. */ struct usb_endpoint_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; uint16_t wMaxPacketSize; uint8_t bInterval; uint8_t bRefresh; uint8_t bSynchAddress; } __attribute__((packed)); struct usb_device_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint16_t idVendor; uint16_t idProduct; uint16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; } __attribute__((packed)); struct usb_config_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; } __attribute__((packed)); struct usb_interface_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bNumEndpoints; uint8_t bInterfaceClass; uint8_t bInterfaceSubClass; uint8_t bInterfaceProtocol; uint8_t iInterface; } __attribute__((packed)); struct usb_ctrlrequest { uint8_t bRequestType; uint8_t bRequest; uint16_t wValue; uint16_t wIndex; uint16_t wLength; } __attribute__((packed)); struct usb_qualifier_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint8_t bNumConfigurations; uint8_t bRESERVED; } __attribute__((packed)); #define USB_TYPE_MASK (0x03 << 5) #define USB_TYPE_STANDARD (0x00 << 5) #define USB_TYPE_CLASS (0x01 << 5) #define USB_TYPE_VENDOR (0x02 << 5) #define USB_TYPE_RESERVED (0x03 << 5) #define USB_DT_DEVICE 0x01 #define USB_DT_CONFIG 0x02 #define USB_DT_STRING 0x03 #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_DEVICE_QUALIFIER 0x06 #define USB_DT_OTHER_SPEED_CONFIG 0x07 #define USB_DT_INTERFACE_POWER 0x08 #define USB_DT_OTG 0x09 #define USB_DT_DEBUG 0x0a #define USB_DT_INTERFACE_ASSOCIATION 0x0b #define USB_DT_SECURITY 0x0c #define USB_DT_KEY 0x0d #define USB_DT_ENCRYPTION_TYPE 0x0e #define USB_DT_BOS 0x0f #define USB_DT_DEVICE_CAPABILITY 0x10 #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 #define USB_DT_WIRE_ADAPTER 0x21 #define USB_DT_RPIPE 0x22 #define USB_DT_CS_RADIO_CONTROL 0x23 #define USB_DT_PIPE_USAGE 0x24 #define USB_DT_SS_ENDPOINT_COMP 0x30 #define USB_DT_SSP_ISOC_ENDPOINT_COMP 0x31 #define USB_REQ_GET_STATUS 0x00 #define USB_REQ_CLEAR_FEATURE 0x01 #define USB_REQ_SET_FEATURE 0x03 #define USB_REQ_SET_ADDRESS 0x05 #define USB_REQ_GET_DESCRIPTOR 0x06 #define USB_REQ_SET_DESCRIPTOR 0x07 #define USB_REQ_GET_CONFIGURATION 0x08 #define USB_REQ_SET_CONFIGURATION 0x09 #define USB_REQ_GET_INTERFACE 0x0A #define USB_REQ_SET_INTERFACE 0x0B #define USB_REQ_SYNCH_FRAME 0x0C #define USB_REQ_SET_SEL 0x30 #define USB_REQ_SET_ISOCH_DELAY 0x31 #define USB_REQ_SET_ENCRYPTION 0x0D #define USB_REQ_GET_ENCRYPTION 0x0E #define USB_REQ_RPIPE_ABORT 0x0E #define USB_REQ_SET_HANDSHAKE 0x0F #define USB_REQ_RPIPE_RESET 0x0F #define USB_REQ_GET_HANDSHAKE 0x10 #define USB_REQ_SET_CONNECTION 0x11 #define USB_REQ_SET_SECURITY_DATA 0x12 #define USB_REQ_GET_SECURITY_DATA 0x13 #define USB_REQ_SET_WUSB_DATA 0x14 #define USB_REQ_LOOPBACK_DATA_WRITE 0x15 #define USB_REQ_LOOPBACK_DATA_READ 0x16 #define USB_REQ_SET_INTERFACE_DS 0x17 #define USB_REQ_GET_PARTNER_PDO 20 #define USB_REQ_GET_BATTERY_STATUS 21 #define USB_REQ_SET_PDO 22 #define USB_REQ_GET_VDM 23 #define USB_REQ_SEND_VDM 24 #define USB_MAX_IFACE_NUM 4 #define USB_MAX_EP_NUM 32 #define USB_MAX_FDS 6 struct usb_endpoint_index { struct usb_endpoint_descriptor desc; int handle; }; struct usb_iface_index { struct usb_interface_descriptor* iface; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bInterfaceClass; struct usb_endpoint_index eps[USB_MAX_EP_NUM]; int eps_num; }; struct usb_device_index { struct usb_device_descriptor* dev; struct usb_config_descriptor* config; uint8_t bDeviceClass; uint8_t bMaxPower; int config_length; struct usb_iface_index ifaces[USB_MAX_IFACE_NUM]; int ifaces_num; int iface_cur; }; struct usb_info { int fd; struct usb_device_index index; }; static struct usb_info usb_devices[USB_MAX_FDS]; static int usb_devices_num; static bool parse_usb_descriptor(const char* buffer, size_t length, struct usb_device_index* index) { if (length < sizeof(*index->dev) + sizeof(*index->config)) return false; memset(index, 0, sizeof(*index)); index->dev = (struct usb_device_descriptor*)buffer; index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev)); index->bDeviceClass = index->dev->bDeviceClass; index->bMaxPower = index->config->bMaxPower; index->config_length = length - sizeof(*index->dev); index->iface_cur = -1; size_t offset = 0; while (true) { if (offset + 1 >= length) break; uint8_t desc_length = buffer[offset]; uint8_t desc_type = buffer[offset + 1]; if (desc_length <= 2) break; if (offset + desc_length > length) break; if (desc_type == USB_DT_INTERFACE && index->ifaces_num < USB_MAX_IFACE_NUM) { struct usb_interface_descriptor* iface = (struct usb_interface_descriptor*)(buffer + offset); index->ifaces[index->ifaces_num].iface = iface; index->ifaces[index->ifaces_num].bInterfaceNumber = iface->bInterfaceNumber; index->ifaces[index->ifaces_num].bAlternateSetting = iface->bAlternateSetting; index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass; index->ifaces_num++; } if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) { struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1]; if (iface->eps_num < USB_MAX_EP_NUM) { memcpy(&iface->eps[iface->eps_num].desc, buffer + offset, sizeof(iface->eps[iface->eps_num].desc)); iface->eps_num++; } } offset += desc_length; } return true; } static struct usb_device_index* add_usb_index(int fd, const char* dev, size_t dev_len) { int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED); if (i >= USB_MAX_FDS) return NULL; int rv = 0; rv = parse_usb_descriptor(dev, dev_len, &usb_devices[i].index); if (!rv) return NULL; __atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE); return &usb_devices[i].index; } static struct usb_device_index* lookup_usb_index(int fd) { int i; for (i = 0; i < USB_MAX_FDS; i++) { if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd) { return &usb_devices[i].index; } } return NULL; } struct vusb_connect_string_descriptor { uint32_t len; char* str; } __attribute__((packed)); struct vusb_connect_descriptors { uint32_t qual_len; char* qual; uint32_t bos_len; char* bos; uint32_t strs_len; struct vusb_connect_string_descriptor strs[0]; } __attribute__((packed)); static const char default_string[] = { 8, USB_DT_STRING, 's', 0, 'y', 0, 'z', 0 }; static const char default_lang_id[] = { 4, USB_DT_STRING, 0x09, 0x04 }; static bool lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, char** response_data, uint32_t* response_length) { struct usb_device_index* index = lookup_usb_index(fd); uint8_t str_idx; if (!index) return false; switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_GET_DESCRIPTOR: switch (ctrl->wValue >> 8) { case USB_DT_DEVICE: *response_data = (char*)index->dev; *response_length = sizeof(*index->dev); return true; case USB_DT_CONFIG: *response_data = (char*)index->config; *response_length = index->config_length; return true; case USB_DT_STRING: str_idx = (uint8_t)ctrl->wValue; if (descs && str_idx < descs->strs_len) { *response_data = descs->strs[str_idx].str; *response_length = descs->strs[str_idx].len; return true; } if (str_idx == 0) { *response_data = (char*)&default_lang_id[0]; *response_length = default_lang_id[0]; return true; } *response_data = (char*)&default_string[0]; *response_length = default_string[0]; return true; case USB_DT_BOS: *response_data = descs->bos; *response_length = descs->bos_len; return true; case USB_DT_DEVICE_QUALIFIER: if (!descs->qual) { struct usb_qualifier_descriptor* qual = (struct usb_qualifier_descriptor*)response_data; qual->bLength = sizeof(*qual); qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; qual->bcdUSB = index->dev->bcdUSB; qual->bDeviceClass = index->dev->bDeviceClass; qual->bDeviceSubClass = index->dev->bDeviceSubClass; qual->bDeviceProtocol = index->dev->bDeviceProtocol; qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0; qual->bNumConfigurations = index->dev->bNumConfigurations; qual->bRESERVED = 0; *response_length = sizeof(*qual); return true; } *response_data = descs->qual; *response_length = descs->qual_len; return true; default: break; } break; default: break; } break; default: break; } return false; } typedef bool (*lookup_connect_out_response_t)(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done); static bool lookup_connect_response_out_generic(int fd, const struct vusb_connect_descriptors* descs, const struct usb_ctrlrequest* ctrl, bool* done) { switch (ctrl->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: switch (ctrl->bRequest) { case USB_REQ_SET_CONFIGURATION: *done = true; return true; default: break; } break; } return false; } /* -------------------------------------------------------------------------- */ static int vhci_open(void) { return open("/dev/vhci", O_RDWR); } static int vhci_setport(int fd, u_int port) { struct vhci_ioc_set_port args; args.port = port; return ioctl(fd, VHCI_IOC_SET_PORT, &args); } static int vhci_usb_attach(int fd) { return ioctl(fd, VHCI_IOC_USB_ATTACH, NULL); } static int vhci_usb_recv(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = read(fd, ptr, size); if (done < 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } static int vhci_usb_send(int fd, void* buf, size_t size) { uint8_t* ptr = (uint8_t*)buf; ssize_t done; while (1) { done = write(fd, ptr, size); if (done <= 0) return -1; if ((size_t)done == size) return 0; size -= done; ptr += done; } } /* -------------------------------------------------------------------------- */ static volatile long syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev, const struct vusb_connect_descriptors* descs, lookup_connect_out_response_t lookup_connect_response_out) { struct usb_device_index* index; int portnum, fd, rv; bool done; portnum = procid + 1; if (!dev) { return -1; } if (portnum != 1) { /* For now, we support only one proc. */ return -1; } fd = vhci_open(); if (fd < 0) { return -1; } index = add_usb_index(fd, dev, dev_len); if (!index) { goto err; } rv = vhci_setport(fd, portnum); if (rv != 0) { goto err; } rv = vhci_usb_attach(fd); if (rv != 0) { goto err; } done = false; while (!done) { vhci_request_t req; rv = vhci_usb_recv(fd, &req, sizeof(req)); if (rv != 0) { goto err; } if (req.type != VHCI_REQ_CTRL) { goto err; } char* response_data = NULL; uint32_t response_length = 0; char data[4096]; if (req.u.ctrl.bmRequestType & UE_DIR_IN) { bool response_found = false; response_found = lookup_connect_response_in(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &response_data, &response_length); if (!response_found) { goto err; } } else { if (!lookup_connect_response_out(fd, descs, (const usb_ctrlrequest*)&req.u.ctrl, &done)) { goto err; } response_data = NULL; response_length = UGETW(req.u.ctrl.wLength); } if ((req.u.ctrl.bmRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD && req.u.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) { /* TODO: possibly revisit */ } if (response_length > sizeof(data)) response_length = 0; if ((uint32_t)UGETW(req.u.ctrl.wLength) < response_length) response_length = UGETW(req.u.ctrl.wLength); if (response_data) memcpy(data, response_data, response_length); else memset(data, 0, response_length); if (req.u.ctrl.bmRequestType & UE_DIR_IN) { if (response_length > 0) { vhci_response_t res; res.size = response_length; rv = vhci_usb_send(fd, &res, sizeof(res)); if (rv == 0) rv = vhci_usb_send(fd, data, response_length); } } else { rv = vhci_usb_recv(fd, data, response_length); } if (rv < 0) { goto err; } } sleep_ms(200); return fd; err: close(fd); return -1; } static volatile long syz_usb_connect(volatile long a0, volatile long a1, volatile long a2, volatile long a3) { uint64_t speed = a0; uint64_t dev_len = a1; const char* dev = (const char*)a2; const struct vusb_connect_descriptors* descs = (const struct vusb_connect_descriptors*)a3; return syz_usb_connect_impl(speed, dev_len, dev, descs, &lookup_connect_response_out_generic); } static volatile long syz_usb_disconnect(volatile long a0) { int fd = a0; int rv = close(fd); sleep_ms(200); return rv; } static void sandbox_common() { if (setsid() == -1) exit(1); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 8 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 0; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); } static void loop(); static int do_sandbox_none(void) { sandbox_common(); loop(); return 0; } static long syz_execute_func(volatile long text) { volatile long p[8] = {0}; (void)p; asm volatile("" ::"r"(0l), "r"(1l), "r"(2l), "r"(3l), "r"(4l), "r"(5l), "r"(6l), "r"(7l), "r"(8l), "r"(9l), "r"(10l), "r"(11l), "r"(12l), "r"(13l)); ((void (*)(void))(text))(); return 0; } struct thread_t { int created, call; event_t ready, done; }; static struct thread_t threads[16]; static void execute_call(int call); static int running; static void* thr(void* arg) { struct thread_t* th = (struct thread_t*)arg; for (;;) { event_wait(&th->ready); event_reset(&th->ready); execute_call(th->call); __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED); event_set(&th->done); } return 0; } static void execute_one(void) { int i, call, thread; for (call = 0; call < 15; call++) { for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); thread++) { struct thread_t* th = &threads[thread]; if (!th->created) { th->created = 1; event_init(&th->ready); event_init(&th->done); event_set(&th->done); thread_start(thr, th); } if (!event_isset(&th->done)) continue; event_reset(&th->done); th->call = call; __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED); event_set(&th->ready); event_timedwait(&th->done, 45 + (call == 13 ? 3000 : 0) + (call == 14 ? 300 : 0)); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter; for (iter = 0;; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5 * 1000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } #ifndef SYS__lwp_create #define SYS__lwp_create 309 #endif #ifndef SYS__lwp_kill #define SYS__lwp_kill 318 #endif #ifndef SYS__lwp_self #define SYS__lwp_self 311 #endif #ifndef SYS__lwp_unpark_all #define SYS__lwp_unpark_all 322 #endif #ifndef SYS_compat_50__lwp_park #define SYS_compat_50__lwp_park 320 #endif #ifndef SYS_mmap #define SYS_mmap 197 #endif #ifndef SYS_shmctl #define SYS_shmctl 443 #endif uint64_t r[7] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: res = syscall(SYS__lwp_self); if (res != -1) r[0] = res; break; case 1: *(uint32_t*)0x20000100 = 0x400f0020; *(uint64_t*)0x20000108 = 0x20000080; *(uint32_t*)0x20000080 = 0; *(uint64_t*)0x20000088 = 0x20000000; *(uint32_t*)0x20000000 = 0x400f0023; *(uint64_t*)0x20000008 = 0; *(uint32_t*)0x20000010 = 7; *(uint32_t*)0x20000014 = 7; *(uint32_t*)0x20000018 = 0x101; *(uint32_t*)0x2000001c = 0x1f; *(uint64_t*)0x20000020 = 1; *(uint64_t*)0x20000028 = 0x400; *(uint32_t*)0x20000030 = 6; *(uint64_t*)0x20000038 = 1; *(uint64_t*)0x20000040 = 0x80000000; memcpy((void*)0x20000048, "\000", 1); *(uint32_t*)0x20000090 = 3; *(uint32_t*)0x20000094 = 5; *(uint32_t*)0x20000098 = 1; *(uint32_t*)0x2000009c = 0x35f7192e; *(uint64_t*)0x200000a0 = 0xd; *(uint64_t*)0x200000a8 = 0x100000000; *(uint32_t*)0x200000b0 = 5; *(uint64_t*)0x200000b8 = 6; *(uint64_t*)0x200000c0 = 0x10001; memcpy((void*)0x200000c8, "\000", 1); *(uint32_t*)0x20000110 = 0xf1b; *(uint32_t*)0x20000114 = 0x401; *(uint32_t*)0x20000118 = 0x69; *(uint32_t*)0x2000011c = 0x100; *(uint64_t*)0x20000120 = 1; *(uint64_t*)0x20000128 = 0; *(uint32_t*)0x20000130 = 0x2ecd3534; *(uint64_t*)0x20000138 = 1; *(uint64_t*)0x20000140 = 0x400; memcpy((void*)0x20000148, "-\000", 2); res = syscall(SYS__lwp_create, 0x20000100ul, 0x60ul, 0x20000180ul); if (res != -1) r[1] = *(uint32_t*)0x20000180; break; case 2: res = syscall(SYS__lwp_self); if (res != -1) r[2] = res; break; case 3: *(uint32_t*)0x200001c0 = 2; *(uint64_t*)0x200001c8 = 0; *(uint32_t*)0x200001d0 = 9; *(uint32_t*)0x200001d4 = 0x3ae; *(uint32_t*)0x200001d8 = 3; *(uint32_t*)0x200001dc = 0xc699; *(uint64_t*)0x200001e0 = 0x81; *(uint64_t*)0x200001e8 = 2; *(uint32_t*)0x200001f0 = 1; *(uint64_t*)0x200001f8 = 9; *(uint64_t*)0x20000200 = 7; memcpy((void*)0x20000208, "\000", 1); res = syscall(SYS__lwp_create, 0x200001c0ul, 0x40ul, 0x20000240ul); if (res != -1) r[3] = *(uint32_t*)0x20000240; break; case 4: res = syscall(SYS__lwp_self); if (res != -1) r[4] = res; break; case 5: *(uint32_t*)0x20000280 = r[0]; *(uint32_t*)0x20000284 = r[1]; *(uint32_t*)0x20000288 = r[2]; *(uint32_t*)0x2000028c = r[3]; *(uint32_t*)0x20000290 = r[4]; *(uint64_t*)0x200002c0 = 4; syscall(SYS__lwp_unpark_all, 0x20000280ul, 5ul, 0x200002c0ul); break; case 6: *(uint32_t*)0x20000380 = 8; *(uint64_t*)0x20000388 = 0x20000300; *(uint32_t*)0x20000300 = 0x400f002c; *(uint64_t*)0x20000308 = 0; *(uint32_t*)0x20000310 = 0x80000000; *(uint32_t*)0x20000314 = 0xa706; *(uint32_t*)0x20000318 = 0; *(uint32_t*)0x2000031c = 8; *(uint64_t*)0x20000320 = 0x1f; *(uint64_t*)0x20000328 = 6; *(uint32_t*)0x20000330 = 1; *(uint64_t*)0x20000338 = 0x101; *(uint64_t*)0x20000340 = 5; memcpy((void*)0x20000348, "\000", 1); *(uint32_t*)0x20000390 = 2; *(uint32_t*)0x20000394 = 9; *(uint32_t*)0x20000398 = 0x10000; *(uint32_t*)0x2000039c = 1; *(uint64_t*)0x200003a0 = 2; *(uint64_t*)0x200003a8 = 0xffffffff; *(uint32_t*)0x200003b0 = 5; *(uint64_t*)0x200003b8 = 0x20; *(uint64_t*)0x200003c0 = 0xfff; memcpy((void*)0x200003c8, "\000", 1); res = syscall(SYS__lwp_create, 0x20000380ul, 0x40ul, 0x20000400ul); if (res != -1) r[5] = *(uint32_t*)0x20000400; break; case 7: syscall(SYS__lwp_kill, r[5], 8); break; case 8: *(uint32_t*)0x20000440 = 0x101; *(uint64_t*)0x20000448 = 8; *(uint64_t*)0x20000480 = 1; syscall(SYS_compat_50__lwp_park, 0x20000440ul, 0, 0ul, 0x20000480ul); break; case 9: syscall(SYS_shmctl, 0, 3ul, 0); break; case 10: memcpy((void*)0x20000000, "\xaf\xe8\xd2\x38\x3c\x3e\xf2\xff\x5f\x6e\x8c\x9a\x4d\x35\xc6\x02\x19\x87\x66\xff\x84\xe3\xac\xfd\x84\xf7\x78\x35\x3f\x61\x84\x2e\x8a\x19\x8d\x7e\x50\x0c\xce\x4b\x81\x36\x90\x33\x85\x89\xcc\xa0\x7a\xcd\xe0\xe9\x83\x4f\xd0\x9b\x00\xf3\x14\x6b\xa0\xdd\xb9\xac\x17\xae\x01\x1f\xb8\x61\xaf\x25\x83\x25\x47\x6c\x96\x21\x38\x8e\xca\x4b\x11\xc0", 84); break; case 11: memcpy((void*)0x20000080, "\x36\x42\x83\x3d\xd7\x0b\x58\x6a\x46\x3e\x64\x0f\xeb\xb1\x9b\x00\x00\x00\xc4\xc3\xf9\x7c\x6d\x4c\xa6\xf3\x44\xa6\xc4\x43\x85\x7f\xc6\x18\xfd\xc4\x03\x11\x6f\x26\xfa\x44\x7b\xa9\x64\x2e\x47\x0f\x12\xfc\x64\xf2\x0f\x11\x62\x00", 56); syz_execute_func(0x20000080); break; case 12: break; case 13: memcpy((void*)0x20000100, "\x93\x5e\x05\x7a\xa8\xdd\xae\xce\x20\x39\x4d\x86\xc4\x5c\x4a\x50\xf7\x27\x8a\x87\x91\xb0\xcd\xbc\xf6\xe8\x32\xb9\x05\x2f\x3a\xb9\x90\x63\x70\x55\x06\xc6\x6e\xb3\x37\x4a\xad\x5e\x1f\xde\xd7\x51\x5c\x8c\xb7\xfe\x2c\x21\x69\x94\xff\xb8\x9e\x91\x54\x96\xa2\x07\x40\x41\x68\xe8\x6b\xa8\x2f\x7c\x0d\x49\x64\xe7\xe7\x56\xac\x68\x99\x19\xcf\x9f\x4b\xf8\x7d\x3e\x3b\x51\x74\x60\x5c\x8f\x25\x6d\x28\x20\x4e\x42\x8b\xf2\x95\xff\x31\x2e\x2e\x27\xa1\xc2\xaf\xb8\x93\xdb\xe7\x86\x70\x60\xc6\x50", 120); memcpy((void*)0x20000180, "\x2b\x06\x57\x4b\x32\xd8\xeb\x1d\x74\x79\x43\x1c\x2a\xb5\x3f\xc2\xd2\x4c\xbf\x1c\x43\x68\xa8\x24\xc8\xcd\x82\x17\xf4\x4d\x51\xb1\x18\x89\xf9\x29\xd6\x5e\x79\x00\xcc\xcb\x51\x66\x45\x3f\xfe\x7f\xa1\xf3\x28\xdf\x96\x95\xa4\x45\x7b\x58\xf1\x31\x91\xe9\x0c\xa7\x94\x18\x6e\x76\x32\x17\xe9\x23\xbd\xc4\xf2\x04\x34\xeb\xa9\x66\x16\xcc\x3b\x27\xea\x87\xd5\x4a\x6c\x43\x4d\x77\xb7\x37\xd1\x8d\x0d\x8d\x53\x7d\xce\x33\x0d\x27\x59\xbe\x67\xe2\x2e\x1d\xc6\x17\xf5\x17\x14\x7f\xae\xb4\x4e\x34\x3d\x63\x0b\x33\xd1\xd3\x16\x06\xb2\xa3\x97\x1e\xfc\xd0\x3e\x4d\x87\xb5\xe6\x00\x32\x37\x23\xab\xfe\xf8\x1a\xa7\x1b\xea\x16\xbd\xf9\x56\x1a\xca\x75\xce\x32\x65\xde\x3a\xc8\xa1\x59\x0a\x61\xed\xe7\x3a\x7b\x69\x43\xdd\xf3\xe3\xfc\xf1\x9e\x47\x8f\x55\x85\x9d\xb7\x8d\x6b\xaa\x46\xb7\xa7\xe2\x15\x9f\x6f\x4f\x20\xcc\xb1\x8d\x5c\x41\xfb\x43\x02\x49", 206); res = syz_usb_connect(0xfffffffffffffeff, 0x78, 0x20000100, 0x20000180); if (res != -1) r[6] = res; break; case 14: syz_usb_disconnect(r[6]); break; } } int main(void) { syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul, 0ul); for (procid = 0; procid < 4; procid++) { if (fork() == 0) { use_temporary_dir(); do_sandbox_none(); } } sleep(1000000); return 0; } : In function 'syz_usb_connect_impl': :637:63: error: unknown type name 'usb_ctrlrequest' :642:55: error: unknown type name 'usb_ctrlrequest' compiler invocation: /syzkaller/netbsd/src/../tools/bin/x86_64--netbsd-g++ [-o /tmp/syz-executor580379185 -DGOOS_netbsd=1 -DGOARCH_amd64=1 -DHOSTGOOS_linux=1 -x c - -m64 --sysroot /syzkaller/netbsd/src/../dest/ -O2 -pthread -Wall -Werror -Wparentheses -Wframe-larger-than=16384] --- FAIL: TestGenerate/netbsd/amd64/3 (0.25s) csource_test.go:121: --- FAIL: TestGenerate/netbsd/amd64/2 (0.26s) csource_test.go:121: FAIL FAIL github.com/google/syzkaller/pkg/csource 3.131s ok github.com/google/syzkaller/pkg/db (cached) ok github.com/google/syzkaller/pkg/email (cached) ? github.com/google/syzkaller/pkg/gce [no test files] ? github.com/google/syzkaller/pkg/gcs [no test files] ? github.com/google/syzkaller/pkg/hash [no test files] ok github.com/google/syzkaller/pkg/host (cached) ? github.com/google/syzkaller/pkg/html [no test files] ok github.com/google/syzkaller/pkg/ifuzz (cached) ? github.com/google/syzkaller/pkg/ifuzz/gen [no test files] ? github.com/google/syzkaller/pkg/ifuzz/generated [no test files] ok github.com/google/syzkaller/pkg/instance (cached) ok github.com/google/syzkaller/pkg/ipc (cached) ? github.com/google/syzkaller/pkg/ipc/ipcconfig [no test files] ok github.com/google/syzkaller/pkg/kd (cached) ok github.com/google/syzkaller/pkg/log (cached) ok github.com/google/syzkaller/pkg/mgrconfig (cached) ok github.com/google/syzkaller/pkg/osutil (cached) ok github.com/google/syzkaller/pkg/report (cached) ok github.com/google/syzkaller/pkg/repro (cached) ? github.com/google/syzkaller/pkg/rpctype [no test files] ok github.com/google/syzkaller/pkg/runtest (cached) ok github.com/google/syzkaller/pkg/serializer (cached) ? github.com/google/syzkaller/pkg/signal [no test files] ok github.com/google/syzkaller/pkg/symbolizer (cached) ok github.com/google/syzkaller/pkg/vcs (cached) ok github.com/google/syzkaller/prog (cached) ok github.com/google/syzkaller/prog/test (cached) ? github.com/google/syzkaller/sys [no test files] ? github.com/google/syzkaller/sys/akaros [no test files] ? github.com/google/syzkaller/sys/akaros/gen [no test files] ? github.com/google/syzkaller/sys/freebsd [no test files] ? github.com/google/syzkaller/sys/freebsd/gen [no test files] ? github.com/google/syzkaller/sys/fuchsia [no test files] ? github.com/google/syzkaller/sys/fuchsia/fidlgen [no test files] ? github.com/google/syzkaller/sys/fuchsia/gen [no test files] ? github.com/google/syzkaller/sys/fuchsia/layout [no test files] ok github.com/google/syzkaller/sys/linux (cached) ? github.com/google/syzkaller/sys/linux/gen [no test files] ? github.com/google/syzkaller/sys/netbsd [no test files] ? github.com/google/syzkaller/sys/netbsd/gen [no test files] ok github.com/google/syzkaller/sys/openbsd (cached) ? github.com/google/syzkaller/sys/openbsd/gen [no test files] ? github.com/google/syzkaller/sys/syz-extract [no test files] ? github.com/google/syzkaller/sys/syz-sysgen [no test files] ? github.com/google/syzkaller/sys/targets [no test files] ? github.com/google/syzkaller/sys/test [no test files] ? github.com/google/syzkaller/sys/test/gen [no test files] ? github.com/google/syzkaller/sys/trusty [no test files] ? github.com/google/syzkaller/sys/trusty/gen [no test files] ? github.com/google/syzkaller/sys/windows [no test files] ? github.com/google/syzkaller/sys/windows/gen [no test files] ok github.com/google/syzkaller/syz-ci (cached) ok github.com/google/syzkaller/syz-fuzzer (cached) ok github.com/google/syzkaller/syz-hub (cached) ok github.com/google/syzkaller/syz-hub/state (cached) ? github.com/google/syzkaller/syz-manager [no test files] ? github.com/google/syzkaller/tools/syz-benchcmp [no test files] ? github.com/google/syzkaller/tools/syz-bisect [no test files] ? github.com/google/syzkaller/tools/syz-check [no test files] ? github.com/google/syzkaller/tools/syz-cover [no test files] ? github.com/google/syzkaller/tools/syz-crush [no test files] ? github.com/google/syzkaller/tools/syz-db [no test files] ? github.com/google/syzkaller/tools/syz-execprog [no test files] ? github.com/google/syzkaller/tools/syz-expand [no test files] ? github.com/google/syzkaller/tools/syz-fmt [no test files] ? github.com/google/syzkaller/tools/syz-imagegen [no test files] ? github.com/google/syzkaller/tools/syz-make [no test files] ? github.com/google/syzkaller/tools/syz-mutate [no test files] ? github.com/google/syzkaller/tools/syz-prog2c [no test files] ? github.com/google/syzkaller/tools/syz-repro [no test files] ? github.com/google/syzkaller/tools/syz-reprolist [no test files] ? github.com/google/syzkaller/tools/syz-runtest [no test files] ? github.com/google/syzkaller/tools/syz-showprio [no test files] ? github.com/google/syzkaller/tools/syz-stress [no test files] ? github.com/google/syzkaller/tools/syz-symbolize [no test files] ? github.com/google/syzkaller/tools/syz-testbuild [no test files] ? github.com/google/syzkaller/tools/syz-trace2syz [no test files] ok github.com/google/syzkaller/tools/syz-trace2syz/parser (cached) ok github.com/google/syzkaller/tools/syz-trace2syz/proggen (cached) ? github.com/google/syzkaller/tools/syz-tty [no test files] ? github.com/google/syzkaller/tools/syz-upgrade [no test files] ? github.com/google/syzkaller/tools/syz-usbgen [no test files] ok github.com/google/syzkaller/vm (cached) ? github.com/google/syzkaller/vm/adb [no test files] ? github.com/google/syzkaller/vm/bhyve [no test files] ? github.com/google/syzkaller/vm/gce [no test files] ? github.com/google/syzkaller/vm/gvisor [no test files] ok github.com/google/syzkaller/vm/isolated (cached) ? github.com/google/syzkaller/vm/kvm [no test files] ? github.com/google/syzkaller/vm/odroid [no test files] ? github.com/google/syzkaller/vm/qemu [no test files] ok github.com/google/syzkaller/vm/vmimpl (cached) ? github.com/google/syzkaller/vm/vmm [no test files]