From 5910646c6c6fbb5e48801342677bff9c4609a2ea Mon Sep 17 00:00:00 2001 From: Nazime Hande Harputluoglu Date: Thu, 1 Oct 2020 21:53:57 +0000 Subject: sys/linux: descriptions for USB/IP --- pkg/csource/generated.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ pkg/host/syscalls_linux.go | 8 ++++++++ pkg/osutil/osutil.go | 10 ++++++++++ 3 files changed, 62 insertions(+) (limited to 'pkg') diff --git a/pkg/csource/generated.go b/pkg/csource/generated.go index cdef36c54..83c3616a6 100644 --- a/pkg/csource/generated.go +++ b/pkg/csource/generated.go @@ -3875,6 +3875,50 @@ static long syz_io_uring_submit(volatile long a0, volatile long a1, volatile lon #endif +#if SYZ_EXECUTOR || __NR_syz_usbip_server_init + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define VHCI_HC_PORTS 8 +#define VHCI_PORTS (VHCI_HC_PORTS * 2) + +static long syz_usbip_server_init(volatile long a0) +{ + int socket_pair[2]; + char buffer[100]; + static int port_alloc[2]; + + int speed = (int)a0; + bool usb3 = (speed == USB_SPEED_SUPER); + + int rc = socketpair(AF_UNIX, SOCK_STREAM, 0, socket_pair); + if (rc < 0) + fail("syz_usbip_server_init : socketpair failed: %d", rc); + + 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) { + debug("syz_usbip_server_init : no more available port for : %d\n", available_port_num); + return -1; + } + int port_num = procid * VHCI_PORTS + usb3 * VHCI_HC_PORTS + available_port_num; + 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; +} + +#endif + #if SYZ_EXECUTOR || __NR_syz_btf_id_by_name #include diff --git a/pkg/host/syscalls_linux.go b/pkg/host/syscalls_linux.go index c66342ddf..998f6f96c 100644 --- a/pkg/host/syscalls_linux.go +++ b/pkg/host/syscalls_linux.go @@ -265,6 +265,13 @@ func isSyzFuseSupported(c *prog.Syscall, target *prog.Target, sandbox string) (b return true, "" } +func isSyzUsbIPSupported(c *prog.Syscall, target *prog.Target, sandbox string) (bool, string) { + if err := osutil.IsWritable("/sys/devices/platform/vhci_hcd.0/attach"); err != nil { + return false, err.Error() + } + return onlySandboxNoneOrNamespace(sandbox) +} + var syzkallSupport = map[string]func(*prog.Syscall, *prog.Target, string) (bool, string){ "syz_open_dev": isSyzOpenDevSupported, "syz_open_procfs": alwaysSupported, @@ -294,6 +301,7 @@ var syzkallSupport = map[string]func(*prog.Syscall, *prog.Target, string) (bool, "syz_fuse_handle_req": isSyzFuseSupported, "syz_80211_inject_frame": isWifiEmulationSupported, "syz_80211_join_ibss": isWifiEmulationSupported, + "syz_usbip_server_init": isSyzUsbIPSupported, } func isSupportedSyzkall(c *prog.Syscall, target *prog.Target, sandbox string) (bool, string) { diff --git a/pkg/osutil/osutil.go b/pkg/osutil/osutil.go index b50b6e45b..5a7a342b1 100644 --- a/pkg/osutil/osutil.go +++ b/pkg/osutil/osutil.go @@ -127,6 +127,16 @@ func IsAccessible(name string) error { return nil } +// IsWritable checks if the file can be written. +func IsWritable(name string) error { + f, err := os.OpenFile(name, os.O_WRONLY, DefaultFilePerm) + if err != nil { + return fmt.Errorf("%v can't be written (%v)", name, err) + } + f.Close() + return nil +} + // FilesExist returns true if all files exist in dir. // Files are assumed to be relative names in slash notation. func FilesExist(dir string, files map[string]bool) bool { -- cgit mrf-deployment