From patchwork Wed May 30 03:03:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shuah X-Patchwork-Id: 10437551 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 52BC4602BF for ; Wed, 30 May 2018 03:03:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4147A27FA1 for ; Wed, 30 May 2018 03:03:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 35B5A28418; Wed, 30 May 2018 03:03:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C6BC927FA1 for ; Wed, 30 May 2018 03:03:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935458AbeE3DDj (ORCPT ); Tue, 29 May 2018 23:03:39 -0400 Received: from mailout.easymail.ca ([64.68.200.34]:55719 "EHLO mailout.easymail.ca" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933740AbeE3DDi (ORCPT ); Tue, 29 May 2018 23:03:38 -0400 Received: from localhost (localhost [127.0.0.1]) by mailout.easymail.ca (Postfix) with ESMTP id B5706C0C86; Wed, 30 May 2018 03:03:37 +0000 (UTC) Received: from mailout.easymail.ca ([127.0.0.1]) by localhost (emo01-pco.easydns.vpn [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 8icksH6m-S9Q; Wed, 30 May 2018 03:03:37 +0000 (UTC) Received: from localhost.localdomain (c-24-9-64-241.hsd1.co.comcast.net [24.9.64.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mailout.easymail.ca (Postfix) with ESMTPSA id 95A6FC075B; Wed, 30 May 2018 03:03:29 +0000 (UTC) From: "Shuah Khan (Samsung OSG)" To: shuah@kernel.org, valentina.manea.m@gmail.com Cc: gregkh@linuxfoundation.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] usbip: usbip_detach: fix to check for invalid ports Date: Tue, 29 May 2018 21:03:08 -0600 Message-Id: <20180530030308.5566-1-shuah@kernel.org> X-Mailer: git-send-email 2.14.1 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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) --- tools/usb/usbip/src/usbip_detach.c | 39 +++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/tools/usb/usbip/src/usbip_detach.c b/tools/usb/usbip/src/usbip_detach.c index 6a8db858caa5..777f7286a0c5 100644 --- a/tools/usb/usbip/src/usbip_detach.c +++ b/tools/usb/usbip/src/usbip_detach.c @@ -46,6 +46,9 @@ static int detach_port(char *port) int ret = 0; uint8_t portnum; char path[PATH_MAX+1]; + int i; + struct usbip_imported_device *idev; + int found = 0; unsigned int port_len = strlen(port); @@ -55,28 +58,46 @@ static int detach_port(char *port) return -1; } - /* check max port */ - portnum = atoi(port); - /* remove the port state file */ - - snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", portnum); - - remove(path); - rmdir(VHCI_STATE_PATH); - ret = usbip_vhci_driver_open(); if (ret < 0) { err("open vhci_driver"); return -1; } + /* check for invalid port */ + for (i = 0; i < vhci_driver->nports; i++) { + idev = &vhci_driver->idev[i]; + + if (idev->port == portnum) { + found = 1; + if (idev->status != VDEV_ST_NULL) + break; + info("Port %d is already detached!\n", idev->port); + goto call_driver_close; + } + } + + if (!found) { + err("Invalid port %s > maxports %d", + port, vhci_driver->nports); + goto call_driver_close; + } + + /* remove the port state file */ + snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", portnum); + + remove(path); + rmdir(VHCI_STATE_PATH); + ret = usbip_vhci_detach_device(portnum); if (ret < 0) { ret = -1; + err("Port %d detach request failed!\n", portnum); goto call_driver_close; } + info("Port %d is now detached!\n", portnum); call_driver_close: usbip_vhci_driver_close();