Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp844627pxb; Thu, 15 Apr 2021 07:57:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxVd/qlzCFlI0tFStzqi+95OWruEovpSegDNgbW3Dvzms8jKpDKOufrCO9PQV4gE4Jxo8U/ X-Received: by 2002:a63:360d:: with SMTP id d13mr1843094pga.295.1618498629287; Thu, 15 Apr 2021 07:57:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618498629; cv=none; d=google.com; s=arc-20160816; b=exzLzhhjp2Po+HSDFOvxIaUhirvDKoe6oqDoCEhDP6xhMrv7wJuvVUeori4v0n/U+I fxfEWg5KhLTJEeBKeH+SxrO+D6bjiu9eBqdJ3Sa+QvkmOvxz6yKLe+Gv9rjQqLTXEWTG t/6Q3oVZ7Z9N8jehfvK8N9H1c+Bl/mz4BiBD3Vd+5oMe0al43LRI8Z5ziOiYUtwry/VW nfXcXwsqjf4aQfRGV8UMD1Fs3oROnTwrR6akCiyZc3oF6bDPCZZ17JmL/WAB1hpFVIy6 ws/MomB44Q3D9m3XHL2Oh9dXCK1Po2KRUVVmejeMGG4trjRBqctT3g2aWRTUca4pjBWX RUCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ykQ3Ek1Rl2OyOmty4EhGbXNHcKx1SCakhPpp0n1qbeE=; b=ga5ITAQMQCQO1WR4HSRcXyvQf63OdN/D2DFLQR7oWYo+2zX3R+IW5sLPAj6ZZAxoS7 W1I9j8NMCmOnmhzAe1wd+aQTkFPksThjTCi0me2ATM8u34IBKI+Q3ErPeEPBFEmwOlT0 dewqAnbsBFTef1YYenQsE7Wev7LOp00eF5f1V+HlRIDh6O3TEq/PUHJ5KpzBkhXj+2kB epSRq8TMuI7Jy+YM/MA9RSqDBW5BbVPnlR1fStnL8xiSK5dbHxz+UHI/c6EWyd7KAXpC Y33qmwLpZF4OBOmEO2Y0EyTs+KJ3yNpJDeLDikS3AtyORXeRk2mWvlLE1mt9tTMIGY5K IWkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ys75ObbG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o26si3155733pgv.264.2021.04.15.07.56.55; Thu, 15 Apr 2021 07:57:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ys75ObbG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233672AbhDOO4X (ORCPT + 99 others); Thu, 15 Apr 2021 10:56:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:39920 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233692AbhDOOwD (ORCPT ); Thu, 15 Apr 2021 10:52:03 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1A61D613CF; Thu, 15 Apr 2021 14:51:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1618498300; bh=HPtoD8rfV4PdfEQYtYjkcpDbED7Bggou9Nnlh/4moUE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ys75ObbG+R55GgErZbvfzzSc6nkzaWCx+scmo3wK30XasxE8Y9qwVlh/z8Y6lKk/h bYLLSds2qUtdN2yryaEV7tRorhyDIUNxxgAS4CWcOPodCM5o3Ed7LZ+86lWnn8vz/B Ia0MubN33c+NU3gnBB5fThECPHu4QeWuXcFpY/gE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot , syzbot , syzbot , Tetsuo Handa , Shuah Khan , Tom Seewald Subject: [PATCH 4.9 28/47] usbip: fix vudc usbip_sockfd_store races leading to gpf Date: Thu, 15 Apr 2021 16:47:20 +0200 Message-Id: <20210415144414.362835324@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210415144413.487943796@linuxfoundation.org> References: <20210415144413.487943796@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Shuah Khan commit 46613c9dfa964c0c60b5385dbdf5aaa18be52a9c upstream. usbip_sockfd_store() is invoked when user requests attach (import) detach (unimport) usb gadget device from usbip host. vhci_hcd sends import request and usbip_sockfd_store() exports the device if it is free for export. Export and unexport are governed by local state and shared state - Shared state (usbip device status, sockfd) - sockfd and Device status are used to determine if stub should be brought up or shut down. Device status is shared between host and client. - Local state (tcp_socket, rx and tx thread task_struct ptrs) A valid tcp_socket controls rx and tx thread operations while the device is in exported state. - While the device is exported, device status is marked used and socket, sockfd, and thread pointers are valid. Export sequence (stub-up) includes validating the socket and creating receive (rx) and transmit (tx) threads to talk to the client to provide access to the exported device. rx and tx threads depends on local and shared state to be correct and in sync. Unexport (stub-down) sequence shuts the socket down and stops the rx and tx threads. Stub-down sequence relies on local and shared states to be in sync. There are races in updating the local and shared status in the current stub-up sequence resulting in crashes. These stem from starting rx and tx threads before local and global state is updated correctly to be in sync. 1. Doesn't handle kthread_create() error and saves invalid ptr in local state that drives rx and tx threads. 2. Updates tcp_socket and sockfd, starts stub_rx and stub_tx threads before updating usbip_device status to SDEV_ST_USED. This opens up a race condition between the threads and usbip_sockfd_store() stub up and down handling. Fix the above problems: - Stop using kthread_get_run() macro to create/start threads. - Create threads and get task struct reference. - Add kthread_create() failure handling and bail out. - Hold usbip_device lock to update local and shared states after creating rx and tx threads. - Update usbip_device status to SDEV_ST_USED. - Update usbip_device tcp_socket, sockfd, tcp_rx, and tcp_tx - Start threads after usbip_device (tcp_socket, sockfd, tcp_rx, tcp_tx, and status) is complete. Credit goes to syzbot and Tetsuo Handa for finding and root-causing the kthread_get_run() improper error handling problem and others. This is a hard problem to find and debug since the races aren't seen in a normal case. Fuzzing forces the race window to be small enough for the kthread_get_run() error path bug and starting threads before updating the local and shared state bug in the stub-up sequence. Fixes: 9720b4bc76a83807 ("staging/usbip: convert to kthread") Cc: stable@vger.kernel.org Reported-by: syzbot Reported-by: syzbot Reported-by: syzbot Reported-by: Tetsuo Handa Signed-off-by: Shuah Khan Link: https://lore.kernel.org/r/b1c08b983ffa185449c9f0f7d1021dc8c8454b60.1615171203.git.skhan@linuxfoundation.org Signed-off-by: Tom Seewald Signed-off-by: Greg Kroah-Hartman --- drivers/usb/usbip/vudc_sysfs.c | 42 +++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) --- a/drivers/usb/usbip/vudc_sysfs.c +++ b/drivers/usb/usbip/vudc_sysfs.c @@ -103,8 +103,9 @@ unlock: } static BIN_ATTR_RO(dev_desc, sizeof(struct usb_device_descriptor)); -static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr, - const char *in, size_t count) +static ssize_t store_sockfd(struct device *dev, + struct device_attribute *attr, + const char *in, size_t count) { struct vudc *udc = (struct vudc *) dev_get_drvdata(dev); int rv; @@ -113,6 +114,8 @@ static ssize_t store_sockfd(struct devic struct socket *socket; unsigned long flags; int ret; + struct task_struct *tcp_rx = NULL; + struct task_struct *tcp_tx = NULL; rv = kstrtoint(in, 0, &sockfd); if (rv != 0) @@ -158,24 +161,47 @@ static ssize_t store_sockfd(struct devic goto sock_err; } - udc->ud.tcp_socket = socket; - + /* unlock and create threads and get tasks */ spin_unlock_irq(&udc->ud.lock); spin_unlock_irqrestore(&udc->lock, flags); - udc->ud.tcp_rx = kthread_get_run(&v_rx_loop, - &udc->ud, "vudc_rx"); - udc->ud.tcp_tx = kthread_get_run(&v_tx_loop, - &udc->ud, "vudc_tx"); + tcp_rx = kthread_create(&v_rx_loop, &udc->ud, "vudc_rx"); + if (IS_ERR(tcp_rx)) { + sockfd_put(socket); + return -EINVAL; + } + tcp_tx = kthread_create(&v_tx_loop, &udc->ud, "vudc_tx"); + if (IS_ERR(tcp_tx)) { + kthread_stop(tcp_rx); + sockfd_put(socket); + return -EINVAL; + } + + /* get task structs now */ + get_task_struct(tcp_rx); + get_task_struct(tcp_tx); + /* lock and update udc->ud state */ spin_lock_irqsave(&udc->lock, flags); spin_lock_irq(&udc->ud.lock); + + udc->ud.tcp_socket = socket; + udc->ud.tcp_rx = tcp_rx; + udc->ud.tcp_rx = tcp_tx; udc->ud.status = SDEV_ST_USED; + spin_unlock_irq(&udc->ud.lock); do_gettimeofday(&udc->start_time); v_start_timer(udc); udc->connected = 1; + + spin_unlock_irqrestore(&udc->lock, flags); + + wake_up_process(udc->ud.tcp_rx); + wake_up_process(udc->ud.tcp_tx); + return count; + } else { if (!udc->connected) { dev_err(dev, "Device not connected");