Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp2675338pxb; Mon, 11 Jan 2021 16:52:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJyLPHbNaPVeBgus9qGCJ0y2Kx6hFmkr4wjgfMnfGNKwrm4pHkC/fDvvSq+bI6s3+dfHuZoL X-Received: by 2002:a05:6402:510f:: with SMTP id m15mr1395304edd.267.1610412762786; Mon, 11 Jan 2021 16:52:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610412762; cv=none; d=google.com; s=arc-20160816; b=vWLDNGMbGASPnTyUmL2LbhQoppv9uSVgMeFwAdWJEBjhixIWs288TQs05PRnQhlase dhsVRhrDJIh6C3QHarwbtjmqSJRIbRcV141KF5hBiqOT4SxBRj0hd/Q5APUqdqZXJdWA XlyXsfGgGnOIpRe/B7aRsPgN5Y90QgdYpo3hy+vlKxW94RmlXC9OkuPExgnxIjSIRz/T 6lzZpq/JtqTvpS47h8xkGDg4EvpH7SNT8/hFPLGjL7e4DqfdqR6DKxmmPPEdefdkn/yA 5uxUl7ILuvx4RgIj220qqzsAYu75c9IOD/cxO5oXLCw/rF5jLIDYoF71vpJaPE1WdyoE tugg== 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=sq7nVe/FFKiFvQ+i9f4CBTBFQLAvCGnRCxv/Bf2h7L8=; b=mloCimXffiY2XEEMEqpxF/U7cnOaEbjO+ScAjDqcclJD8w2CilYeoQ/mKmmVf6kdaz urQZ8RHsF7b/tvQLTJ2NEaj60rZ+b8BmeLTx/raEkg3EXKV67xst6hr+3FKwBR5Ic3c1 0bXltoETgZ0bjYXG+NMZwTuWUwyVqPy3vGjFyF4g79bEtssU/MC3h2foTNZopO7CeBqj QgEJL/5GpV7EyxzUVeNIG8osRAegpaeAmozBghHTnjD25/jOcjh5Mrg6PPwEsOlwVf9e U2xCfxBCk6OhvR1TJR3qAncMa8r5LkN6QkBd80zndAe/Bz+FFP3hnBycfgyFV6MHBayC f7KQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=DYnVuD5H; 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 e17si56491edz.241.2021.01.11.16.52.18; Mon, 11 Jan 2021 16:52:42 -0800 (PST) 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=DYnVuD5H; 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 S1731576AbhAKNNC (ORCPT + 99 others); Mon, 11 Jan 2021 08:13:02 -0500 Received: from mail.kernel.org ([198.145.29.99]:59254 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731350AbhAKNML (ORCPT ); Mon, 11 Jan 2021 08:12:11 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 124F022B51; Mon, 11 Jan 2021 13:11:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1610370690; bh=RMD2vaXVrjkPvjcRICMHAwtmJRNH0mjnsLDPtwWFHYs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DYnVuD5H1aRXFOqdegLJ1yCWBjH/JqsDG14NR78/V8uky97IhRexmaLPS9rGNA9Kg UXhNCJd6uzyq5VVKWNS8mWQJVlNacDZhdUmn4yIhj5LHMsOeJvj1PLjvByEyGBS1MJ 00MixTIf54oWETRuvEm6pPL2iOzFhqLcTFahR9BY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot , Tetsuo Handa Subject: [PATCH 5.4 55/92] USB: cdc-wdm: Fix use after free in service_outstanding_interrupt(). Date: Mon, 11 Jan 2021 14:01:59 +0100 Message-Id: <20210111130041.796849350@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210111130039.165470698@linuxfoundation.org> References: <20210111130039.165470698@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: Tetsuo Handa commit 5e5ff0b4b6bcb4d17b7a26ec8bcfc7dd4651684f upstream. syzbot is reporting UAF at usb_submit_urb() [1], for service_outstanding_interrupt() is not checking WDM_DISCONNECTING before calling usb_submit_urb(). Close the race by doing same checks wdm_read() does upon retry. Also, while wdm_read() checks WDM_DISCONNECTING with desc->rlock held, service_interrupt_work() does not hold desc->rlock. Thus, it is possible that usb_submit_urb() is called from service_outstanding_interrupt() from service_interrupt_work() after WDM_DISCONNECTING was set and kill_urbs() from wdm_disconnect() completed. Thus, move kill_urbs() in wdm_disconnect() to after cancel_work_sync() (which makes sure that service_interrupt_work() is no longer running) completed. Although it seems to be safe to dereference desc->intf->dev in service_outstanding_interrupt() even if WDM_DISCONNECTING was already set because desc->rlock or cancel_work_sync() prevents wdm_disconnect() from reaching list_del() before service_outstanding_interrupt() completes, let's not emit error message if WDM_DISCONNECTING is set by wdm_disconnect() while usb_submit_urb() is in progress. [1] https://syzkaller.appspot.com/bug?extid=9e04e2df4a32fb661daf Reported-by: syzbot Signed-off-by: Tetsuo Handa Cc: stable Link: https://lore.kernel.org/r/620e2ee0-b9a3-dbda-a25b-a93e0ed03ec5@i-love.sakura.ne.jp Signed-off-by: Greg Kroah-Hartman --- drivers/usb/class/cdc-wdm.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -465,13 +465,23 @@ static int service_outstanding_interrupt if (!desc->resp_count || !--desc->resp_count) goto out; + if (test_bit(WDM_DISCONNECTING, &desc->flags)) { + rv = -ENODEV; + goto out; + } + if (test_bit(WDM_RESETTING, &desc->flags)) { + rv = -EIO; + goto out; + } + set_bit(WDM_RESPONDING, &desc->flags); spin_unlock_irq(&desc->iuspin); rv = usb_submit_urb(desc->response, GFP_KERNEL); spin_lock_irq(&desc->iuspin); if (rv) { - dev_err(&desc->intf->dev, - "usb_submit_urb failed with result %d\n", rv); + if (!test_bit(WDM_DISCONNECTING, &desc->flags)) + dev_err(&desc->intf->dev, + "usb_submit_urb failed with result %d\n", rv); /* make sure the next notification trigger a submit */ clear_bit(WDM_RESPONDING, &desc->flags); @@ -1026,9 +1036,9 @@ static void wdm_disconnect(struct usb_in wake_up_all(&desc->wait); mutex_lock(&desc->rlock); mutex_lock(&desc->wlock); - kill_urbs(desc); cancel_work_sync(&desc->rxwork); cancel_work_sync(&desc->service_outs_intr); + kill_urbs(desc); mutex_unlock(&desc->wlock); mutex_unlock(&desc->rlock);