Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usbip: usbip_detach: fix to check for invalid ports

usbip detach doesn't check for invalid ports and ports that are already
detached. It attempts to remove state file(s) without validating the port
and sends detach request to the driver for ports that are already detached.

Add check for invalid ports (port > maxports) and ports that are already
detached (status == VDEV_ST_NULL). Don't remove state files and don't send
detach request for invalid ports and ports that are already detached.

Add error and information messages that make sense.

Signed-off-by: Shuah Khan (Samsung OSG) <shuah@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Shuah Khan (Samsung OSG) and committed by
Greg Kroah-Hartman
40ecdeb1 d179f99a

+30 -9
+30 -9
tools/usb/usbip/src/usbip_detach.c
··· 46 46 int ret = 0; 47 47 uint8_t portnum; 48 48 char path[PATH_MAX+1]; 49 + int i; 50 + struct usbip_imported_device *idev; 51 + int found = 0; 49 52 50 53 unsigned int port_len = strlen(port); 51 54 ··· 58 55 return -1; 59 56 } 60 57 61 - /* check max port */ 62 - 63 58 portnum = atoi(port); 64 - 65 - /* remove the port state file */ 66 - 67 - snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", portnum); 68 - 69 - remove(path); 70 - rmdir(VHCI_STATE_PATH); 71 59 72 60 ret = usbip_vhci_driver_open(); 73 61 if (ret < 0) { ··· 66 72 return -1; 67 73 } 68 74 75 + /* check for invalid port */ 76 + for (i = 0; i < vhci_driver->nports; i++) { 77 + idev = &vhci_driver->idev[i]; 78 + 79 + if (idev->port == portnum) { 80 + found = 1; 81 + if (idev->status != VDEV_ST_NULL) 82 + break; 83 + info("Port %d is already detached!\n", idev->port); 84 + goto call_driver_close; 85 + } 86 + } 87 + 88 + if (!found) { 89 + err("Invalid port %s > maxports %d", 90 + port, vhci_driver->nports); 91 + goto call_driver_close; 92 + } 93 + 94 + /* remove the port state file */ 95 + snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", portnum); 96 + 97 + remove(path); 98 + rmdir(VHCI_STATE_PATH); 99 + 69 100 ret = usbip_vhci_detach_device(portnum); 70 101 if (ret < 0) { 71 102 ret = -1; 103 + err("Port %d detach request failed!\n", portnum); 72 104 goto call_driver_close; 73 105 } 106 + info("Port %d is now detached!\n", portnum); 74 107 75 108 call_driver_close: 76 109 usbip_vhci_driver_close();