Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3312602pxf; Mon, 15 Mar 2021 06:57:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz2a6g19hwoQ1IdblMytW6Q8cbdjs95D8Bn1f0wO1/kgn8NaBbAD9ys/W8Adu3R9b892vI0 X-Received: by 2002:a17:907:75c2:: with SMTP id jl2mr24091762ejc.334.1615816666518; Mon, 15 Mar 2021 06:57:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615816666; cv=none; d=google.com; s=arc-20160816; b=gbsCpk3ZgvAsmlyragNRkC2tX/qHUGUzHVz+7W3ryQ4rgu8tr/YoqohhX2LFN5+cA4 Qxq4qVj1sGa4eR+ueeqk6yTxv+LGQz/PgaMXIbIh19JBY/jKoTuOXWq4Oky3FEt7WkhI DG8fnVuGvSEbPJQUQg3Aey/IaCvrvvRxd5qLH9jGwt1sYVBEVkakyj2duABCkZ8bLWXz 71TNzFzyNFWWlCbZMC6HI6UH9pwLKuG7Q6g8u7i3SPJLkih15Xm9hV43KETWwAmGo2xG 33O0/YrNvCowxl7grRSlmJSCtISedmyXT2vpyGH85vDyWOxMC/aKV1gi+3gGE7VD/W1N FqBA== 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=fDiYjeS+A+AvmZL2MS0pHd08Loc6popa6myitYel32M=; b=FgJmpPborJcl7WRWFyWzCW6kfWYI5/8d2WkDY3uAKPuOxTffQ2+SwPR4Pu41+y4zB6 7sAMQE10JywJ61c2KMsSGT0ovCPSn4ItBR1hLPxPYQv8t1gMsTR4sgC1yvvdZJzUaSYY mcs7T60zB4FPCJsfyVLdgp2uYFFPlKLnS9HfeNMqhCwC8YuwrZhjHI7HqRqQttdm2V8B Z0F/sYkqDGHxdLj2IXbMvKj3mdhVoBvHIzupCmxux8vfUj2TTCZbGNnmBSgO2+ANmWR/ Sr0xcSlroQ3bR6PA1qcvcl4FFz0nIFArGIHkFT5DR7YZNKvGEwvStjbDYjsiK8o/NnIa u2Wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YOJ+Hmg3; 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 u5si11015589ejt.35.2021.03.15.06.57.23; Mon, 15 Mar 2021 06:57:46 -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=YOJ+Hmg3; 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 S230407AbhCON4c (ORCPT + 99 others); Mon, 15 Mar 2021 09:56:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:57042 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231194AbhCONxz (ORCPT ); Mon, 15 Mar 2021 09:53:55 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8BF3B64EF0; Mon, 15 Mar 2021 13:53:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816435; bh=+v8QAgvYgNIpdStiRG1288yKy4WwdsJSEJKqh8j7Gdg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YOJ+Hmg3+KEGIemiA6ArxqMxnGqIpJ2JUrmBLB4jReWmzC8umHQh6onEqMKEWwOHz XS7ZbsN8RNcNfz75f0MPI+pjVto5UYVN8zcovi7GVczBfv4RXqSLigaCkrMsESdnh5 mdfuLaC8UyGGlsKxm5ErzQBUgZle5N2X6cfoK7Lc= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot , syzbot , syzbot , Tetsuo Handa , Shuah Khan Subject: [PATCH 4.4 42/75] usbip: fix stub_dev usbip_sockfd_store() races leading to gpf Date: Mon, 15 Mar 2021 14:51:56 +0100 Message-Id: <20210315135209.630408255@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135208.252034256@linuxfoundation.org> References: <20210315135208.252034256@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: Greg Kroah-Hartman From: Shuah Khan commit 9380afd6df70e24eacbdbde33afc6a3950965d22 upstream. usbip_sockfd_store() is invoked when user requests attach (import) detach (unimport) usb 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. - 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. Tested with syzbot reproducer: - https://syzkaller.appspot.com/text?tag=ReproC&x=14801034d00000 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/268a0668144d5ff36ec7d87fdfa90faf583b7ccc.1615171203.git.skhan@linuxfoundation.org Signed-off-by: Greg Kroah-Hartman --- drivers/usb/usbip/stub_dev.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) --- a/drivers/usb/usbip/stub_dev.c +++ b/drivers/usb/usbip/stub_dev.c @@ -60,6 +60,8 @@ static ssize_t store_sockfd(struct devic int sockfd = 0; struct socket *socket; int rv; + struct task_struct *tcp_rx = NULL; + struct task_struct *tcp_tx = NULL; if (!sdev) { dev_err(dev, "sdev is null\n"); @@ -94,20 +96,36 @@ static ssize_t store_sockfd(struct devic goto sock_err; } - sdev->ud.tcp_socket = socket; - sdev->ud.sockfd = sockfd; - + /* unlock and create threads and get tasks */ spin_unlock_irq(&sdev->ud.lock); + tcp_rx = kthread_create(stub_rx_loop, &sdev->ud, "stub_rx"); + if (IS_ERR(tcp_rx)) { + sockfd_put(socket); + return -EINVAL; + } + tcp_tx = kthread_create(stub_tx_loop, &sdev->ud, "stub_tx"); + if (IS_ERR(tcp_tx)) { + kthread_stop(tcp_rx); + sockfd_put(socket); + return -EINVAL; + } - sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud, - "stub_rx"); - sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud, - "stub_tx"); + /* get task structs now */ + get_task_struct(tcp_rx); + get_task_struct(tcp_tx); + /* lock and update sdev->ud state */ spin_lock_irq(&sdev->ud.lock); + sdev->ud.tcp_socket = socket; + sdev->ud.sockfd = sockfd; + sdev->ud.tcp_rx = tcp_rx; + sdev->ud.tcp_tx = tcp_tx; sdev->ud.status = SDEV_ST_USED; spin_unlock_irq(&sdev->ud.lock); + wake_up_process(sdev->ud.tcp_rx); + wake_up_process(sdev->ud.tcp_tx); + } else { dev_info(dev, "stub down\n");