map[SimplifiedCRepro:// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/usb/ch9.h>
static unsigned long long procid;
static bool write_file(const char* file, const char* what, ...)
{
char buf[1024];
va_list args;
va_start(args, what);
vsnprintf(buf, sizeof(buf), what, args);
va_end(args);
buf[sizeof(buf) - 1] = 0;
int len = strlen(buf);
int fd = open(file, O_WRONLY | O_CLOEXEC);
if (fd == -1)
return false;
if (write(fd, buf, len) != len) {
int err = errno;
close(fd);
errno = err;
return false;
}
close(fd);
return true;
}
#define VHCI_HC_PORTS 8
#define VHCI_PORTS (VHCI_HC_PORTS * 2)
static long syz_usbip_server_init(volatile long a0)
{
static int port_alloc[2];
int speed = (int)a0;
bool usb3 = (speed == USB_SPEED_SUPER);
int socket_pair[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, socket_pair)) {
return -1;
}
int client_fd = socket_pair[0];
int server_fd = socket_pair[1];
int available_port_num = __atomic_fetch_add(&port_alloc[usb3], 1, __ATOMIC_RELAXED);
if (available_port_num > VHCI_HC_PORTS) {
return -1;
}
int port_num = procid * VHCI_PORTS + usb3 * VHCI_HC_PORTS + available_port_num;
char buffer[100];
sprintf(buffer, "%d %d %s %d", port_num, client_fd, "0", speed);
write_file("/sys/devices/platform/vhci_hcd.0/attach", buffer);
return server_fd;
}
static long syz_open_dev(volatile long a0, volatile long a1, volatile long a2)
{
if (a0 == 0xc || a0 == 0xb) {
char buf[128];
sprintf(buf, "/dev/%s/%d:%d", a0 == 0xc ? "char" : "block", (uint8_t)a1, (uint8_t)a2);
return open(buf, O_RDWR, 0);
} else {
unsigned long nb = a1;
char buf[1024];
char* hash;
strncpy(buf, (char*)a0, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = 0;
while ((hash = strchr(buf, '#'))) {
*hash = '0' + (char)(nb % 10);
nb /= 10;
}
return open(buf, a2 & ~O_CREAT, 0);
}
}
int main(void)
{
syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/7ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul);
const char* reason;
(void)reason;
if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {}
// syz_usbip_server_init arguments: [
// speed: usbip_device_speed = 0x4 (8 bytes)
// ]
// returns fd_usbip_server
syz_usbip_server_init(/*speed=USB_SPEED_WIRELESS*/4);
// syz_open_dev$usbfs arguments: [
// dev: ptr[in, buffer] {
// buffer: {2f 64 65 76 2f 62 75 73 2f 75 73 62 2f 30 30 23 2f 30 30 23 00} (length 0x15)
// }
// id: intptr = 0x70 (8 bytes)
// flags: open_flags = 0x101301 (8 bytes)
// ]
// returns fd_usbfs
memcpy((void*)0x200000000080, "/dev/bus/usb/00#/00#\000", 21);
syz_open_dev(/*dev=*/0x200000000080, /*id=*/0x70, /*flags=O_TRUNC|O_SYNC|O_NOCTTY|O_WRONLY*/0x101301);
return 0;
}
]